blob: 8bc8319446bf0eba809706b8f8c6f650778d8a11 [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:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200744 memcpy(trash.area, argp[idx].data.str.area,
745 argp[idx].data.str.data);
746 trash.area[argp[idx].data.str.data] = 0;
747 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100748 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
749 argp[idx].type = ARGT_IPV4;
750 break;
751
752 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200753 memcpy(trash.area, argp[idx].data.str.area,
754 argp[idx].data.str.data);
755 trash.area[argp[idx].data.str.data] = 0;
756 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100757 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
758 argp[idx].type = ARGT_MSK4;
759 break;
760
761 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200762 memcpy(trash.area, argp[idx].data.str.area,
763 argp[idx].data.str.data);
764 trash.area[argp[idx].data.str.data] = 0;
765 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100766 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
767 argp[idx].type = ARGT_IPV6;
768 break;
769
770 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200771 memcpy(trash.area, argp[idx].data.str.area,
772 argp[idx].data.str.data);
773 trash.area[argp[idx].data.str.data] = 0;
774 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100775 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
776 argp[idx].type = ARGT_MSK6;
777 break;
778
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100779 case ARGT_MAP:
780 case ARGT_REG:
781 case ARGT_USR:
782 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100783 break;
784 }
785
786 /* Check for type of argument. */
787 if ((mask & ARGT_MASK) != argp[idx].type) {
788 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
789 arg_type_names[(mask & ARGT_MASK)],
790 arg_type_names[argp[idx].type & ARGT_MASK]);
791 WILL_LJMP(luaL_argerror(L, first + idx, msg));
792 }
793
794 /* Next argument. */
795 mask >>= ARGT_BITS;
796 idx++;
797 }
798}
799
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100800/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500801 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100802 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100803 *
804 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100805 * - hlua_sethlua : create the association between hlua context and lua_state.
806 */
807static inline struct hlua *hlua_gethlua(lua_State *L)
808{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100809 struct hlua **hlua = lua_getextraspace(L);
810 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100811}
812static inline void hlua_sethlua(struct hlua *hlua)
813{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100814 struct hlua **hlua_store = lua_getextraspace(hlua->T);
815 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100816}
817
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100818/* This function is used to send logs. It try to send on screen (stderr)
819 * and on the default syslog server.
820 */
821static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
822{
823 struct tm tm;
824 char *p;
825
826 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200827 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100828 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200829 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200830 /* Break the message if exceed the buffer size. */
831 *(p-4) = ' ';
832 *(p-3) = '.';
833 *(p-2) = '.';
834 *(p-1) = '.';
835 break;
836 }
Willy Tarreau90807112020-02-25 08:16:33 +0100837 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100838 *p = *msg;
839 else
840 *p = '.';
841 }
842 *p = '\0';
843
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200844 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100845 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200846 get_localtime(date.tv_sec, &tm);
847 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100848 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200849 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100850 fflush(stderr);
851 }
852}
853
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100854/* This function just ensure that the yield will be always
855 * returned with a timeout and permit to set some flags
856 */
857__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100858 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100859{
860 struct hlua *hlua = hlua_gethlua(L);
861
862 /* Set the wake timeout. If timeout is required, we set
863 * the expiration time.
864 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200865 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100866
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100867 hlua->flags |= flags;
868
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100869 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200870 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100871}
872
Willy Tarreau87b09662015-04-03 00:22:06 +0200873/* This function initialises the Lua environment stored in the stream.
874 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100875 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200876 *
877 * This function is particular. it initialises a new Lua thread. If the
878 * initialisation fails (example: out of memory error), the lua function
879 * throws an error (longjmp).
880 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100881 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500882 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100883 * threads appear, the safe environment set a lock to ensure only one
884 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500885 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100886 *
887 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500888 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100889 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800890 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200891 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800892 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500893 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100894 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100895int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100896{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100897 if (!already_safe) {
898 if (!SET_SAFE_LJMP(gL.T)) {
899 lua->Tref = LUA_REFNIL;
900 return 0;
901 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200902 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100903 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100904 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +0100905 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +0100906 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100907 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100908 lua->T = lua_newthread(gL.T);
909 if (!lua->T) {
910 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100911 if (!already_safe)
912 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100913 return 0;
914 }
915 hlua_sethlua(lua);
916 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
917 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100918 if (!already_safe)
919 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100920 return 1;
921}
922
Willy Tarreau87b09662015-04-03 00:22:06 +0200923/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100924 * is destroyed. The destroy also the memory context. The struct "lua"
925 * is not freed.
926 */
927void hlua_ctx_destroy(struct hlua *lua)
928{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100929 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100930 return;
931
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100932 if (!lua->T)
933 goto end;
934
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100935 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200936 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100937
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200938 if (!SET_SAFE_LJMP(lua->T))
939 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100940 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200941 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200942
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200943 if (!SET_SAFE_LJMP(gL.T))
944 return;
945 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
946 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200947 /* Forces a garbage collecting process. If the Lua program is finished
948 * without error, we run the GC on the thread pointer. Its freed all
949 * the unused memory.
950 * If the thread is finnish with an error or is currently yielded,
951 * it seems that the GC applied on the thread doesn't clean anything,
952 * so e run the GC on the main thread.
953 * NOTE: maybe this action locks all the Lua threads untiml the en of
954 * the garbage collection.
955 */
Willy Tarreauf31af932020-01-14 09:59:38 +0100956 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200957 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200958 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200959 lua_gc(gL.T, LUA_GCCOLLECT, 0);
960 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200961 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200962
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200963 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100964
965end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100966 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100967}
968
969/* This function is used to restore the Lua context when a coroutine
970 * fails. This function copy the common memory between old coroutine
971 * and the new coroutine. The old coroutine is destroyed, and its
972 * replaced by the new coroutine.
973 * If the flag "keep_msg" is set, the last entry of the old is assumed
974 * as string error message and it is copied in the new stack.
975 */
976static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
977{
978 lua_State *T;
979 int new_ref;
980
981 /* Renew the main LUA stack doesn't have sense. */
982 if (lua == &gL)
983 return 0;
984
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100985 /* New Lua coroutine. */
986 T = lua_newthread(gL.T);
987 if (!T)
988 return 0;
989
990 /* Copy last error message. */
991 if (keep_msg)
992 lua_xmove(lua->T, T, 1);
993
994 /* Copy data between the coroutines. */
995 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
996 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500997 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100998
999 /* Destroy old data. */
1000 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1001
1002 /* The thread is garbage collected by Lua. */
1003 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1004
1005 /* Fill the struct with the new coroutine values. */
1006 lua->Mref = new_ref;
1007 lua->T = T;
1008 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1009
1010 /* Set context. */
1011 hlua_sethlua(lua);
1012
1013 return 1;
1014}
1015
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001016void hlua_hook(lua_State *L, lua_Debug *ar)
1017{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001018 struct hlua *hlua = hlua_gethlua(L);
1019
1020 /* Lua cannot yield when its returning from a function,
1021 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001022 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001023 */
1024 if (lua_gethookmask(L) & LUA_MASKRET) {
1025 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1026 return;
1027 }
1028
1029 /* restore the interrupt condition. */
1030 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1031
1032 /* If we interrupt the Lua processing in yieldable state, we yield.
1033 * If the state is not yieldable, trying yield causes an error.
1034 */
1035 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001036 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001037
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001038 /* If we cannot yield, update the clock and check the timeout. */
1039 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001040 hlua->run_time += now_ms - hlua->start_time;
1041 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001042 lua_pushfstring(L, "execution timeout");
1043 WILL_LJMP(lua_error(L));
1044 }
1045
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001046 /* Update the start time. */
1047 hlua->start_time = now_ms;
1048
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001049 /* Try to interrupt the process at the end of the current
1050 * unyieldable function.
1051 */
1052 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001053}
1054
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001055/* This function start or resumes the Lua stack execution. If the flag
1056 * "yield_allowed" if no set and the LUA stack execution returns a yield
1057 * The function return an error.
1058 *
1059 * The function can returns 4 values:
1060 * - HLUA_E_OK : The execution is terminated without any errors.
1061 * - HLUA_E_AGAIN : The execution must continue at the next associated
1062 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001063 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001064 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001065 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001066 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001067 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001068 * LUA code.
1069 */
1070static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1071{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001072#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1073 int nres;
1074#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001075 int ret;
1076 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001077 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001078
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001079 /* Initialise run time counter. */
1080 if (!HLUA_IS_RUNNING(lua))
1081 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001082
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001083 /* Lock the whole Lua execution. This lock must be before the
1084 * label "resume_execution".
1085 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001086 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001087
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001088resume_execution:
1089
1090 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1091 * instructions. it is used for preventing infinite loops.
1092 */
1093 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1094
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001095 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001096 HLUA_SET_RUN(lua);
1097 HLUA_CLR_CTRLYIELD(lua);
1098 HLUA_CLR_WAKERESWR(lua);
1099 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001100
Christopher Fauletbc275a92020-02-26 14:55:16 +01001101 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001102 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001103 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001104
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001105 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001106#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1107 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1108#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001109 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001110#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001111 switch (ret) {
1112
1113 case LUA_OK:
1114 ret = HLUA_E_OK;
1115 break;
1116
1117 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001118 /* Check if the execution timeout is expired. It it is the case, we
1119 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001120 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001121 tv_update_date(0, 1);
1122 lua->run_time += now_ms - lua->start_time;
1123 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001124 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001125 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001126 break;
1127 }
1128 /* Process the forced yield. if the general yield is not allowed or
1129 * if no task were associated this the current Lua execution
1130 * coroutine, we resume the execution. Else we want to return in the
1131 * scheduler and we want to be waked up again, to continue the
1132 * current Lua execution. So we schedule our own task.
1133 */
1134 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001135 if (!yield_allowed || !lua->task)
1136 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001137 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001138 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001139 if (!yield_allowed) {
1140 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001141 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001142 break;
1143 }
1144 ret = HLUA_E_AGAIN;
1145 break;
1146
1147 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001148
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001149 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001150 * because the errors ares the only one mean to return immediately
1151 * from and lua execution.
1152 */
1153 if (lua->flags & HLUA_EXIT) {
1154 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001155 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001156 break;
1157 }
1158
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001159 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160 if (!lua_checkstack(lua->T, 1)) {
1161 ret = HLUA_E_ERR;
1162 break;
1163 }
1164 msg = lua_tostring(lua->T, -1);
1165 lua_settop(lua->T, 0); /* Empty the stack. */
1166 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001167 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001168 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001169 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001170 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001171 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001172 ret = HLUA_E_ERRMSG;
1173 break;
1174
1175 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001176 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001177 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001178 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001179 break;
1180
1181 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001182 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001183 if (!lua_checkstack(lua->T, 1)) {
1184 ret = HLUA_E_ERR;
1185 break;
1186 }
1187 msg = lua_tostring(lua->T, -1);
1188 lua_settop(lua->T, 0); /* Empty the stack. */
1189 lua_pop(lua->T, 1);
1190 if (msg)
1191 lua_pushfstring(lua->T, "message handler error: %s", msg);
1192 else
1193 lua_pushfstring(lua->T, "message handler error");
1194 ret = HLUA_E_ERRMSG;
1195 break;
1196
1197 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001198 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001199 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001200 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001201 break;
1202 }
1203
1204 switch (ret) {
1205 case HLUA_E_AGAIN:
1206 break;
1207
1208 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001209 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001210 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001211 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001212 break;
1213
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001214 case HLUA_E_ETMOUT:
1215 case HLUA_E_NOMEM:
1216 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001217 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001218 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001219 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001220 hlua_ctx_renew(lua, 0);
1221 break;
1222
1223 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001224 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001225 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001226 break;
1227 }
1228
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001229 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001230 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001231
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001232 return ret;
1233}
1234
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001235/* This function exit the current code. */
1236__LJMP static int hlua_done(lua_State *L)
1237{
1238 struct hlua *hlua = hlua_gethlua(L);
1239
1240 hlua->flags |= HLUA_EXIT;
1241 WILL_LJMP(lua_error(L));
1242
1243 return 0;
1244}
1245
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001246/* This function is an LUA binding. It provides a function
1247 * for deleting ACL from a referenced ACL file.
1248 */
1249__LJMP static int hlua_del_acl(lua_State *L)
1250{
1251 const char *name;
1252 const char *key;
1253 struct pat_ref *ref;
1254
1255 MAY_LJMP(check_args(L, 2, "del_acl"));
1256
1257 name = MAY_LJMP(luaL_checkstring(L, 1));
1258 key = MAY_LJMP(luaL_checkstring(L, 2));
1259
1260 ref = pat_ref_lookup(name);
1261 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001262 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001263
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001264 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001265 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001266 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001267 return 0;
1268}
1269
1270/* This function is an LUA binding. It provides a function
1271 * for deleting map entry from a referenced map file.
1272 */
1273static int hlua_del_map(lua_State *L)
1274{
1275 const char *name;
1276 const char *key;
1277 struct pat_ref *ref;
1278
1279 MAY_LJMP(check_args(L, 2, "del_map"));
1280
1281 name = MAY_LJMP(luaL_checkstring(L, 1));
1282 key = MAY_LJMP(luaL_checkstring(L, 2));
1283
1284 ref = pat_ref_lookup(name);
1285 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001286 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001287
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001288 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001289 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001290 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001291 return 0;
1292}
1293
1294/* This function is an LUA binding. It provides a function
1295 * for adding ACL pattern from a referenced ACL file.
1296 */
1297static int hlua_add_acl(lua_State *L)
1298{
1299 const char *name;
1300 const char *key;
1301 struct pat_ref *ref;
1302
1303 MAY_LJMP(check_args(L, 2, "add_acl"));
1304
1305 name = MAY_LJMP(luaL_checkstring(L, 1));
1306 key = MAY_LJMP(luaL_checkstring(L, 2));
1307
1308 ref = pat_ref_lookup(name);
1309 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001310 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001311
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001312 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001313 if (pat_ref_find_elt(ref, key) == NULL)
1314 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001315 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001316 return 0;
1317}
1318
1319/* This function is an LUA binding. It provides a function
1320 * for setting map pattern and sample from a referenced map
1321 * file.
1322 */
1323static int hlua_set_map(lua_State *L)
1324{
1325 const char *name;
1326 const char *key;
1327 const char *value;
1328 struct pat_ref *ref;
1329
1330 MAY_LJMP(check_args(L, 3, "set_map"));
1331
1332 name = MAY_LJMP(luaL_checkstring(L, 1));
1333 key = MAY_LJMP(luaL_checkstring(L, 2));
1334 value = MAY_LJMP(luaL_checkstring(L, 3));
1335
1336 ref = pat_ref_lookup(name);
1337 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001338 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001339
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001340 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001341 if (pat_ref_find_elt(ref, key) != NULL)
1342 pat_ref_set(ref, key, value, NULL);
1343 else
1344 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001345 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001346 return 0;
1347}
1348
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001349/* A class is a lot of memory that contain data. This data can be a table,
1350 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001351 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001352 * the name of the object (_G[<name>] = <metable> ).
1353 *
1354 * A metable is a table that modify the standard behavior of a standard
1355 * access to the associated data. The entries of this new metatable are
1356 * defined as is:
1357 *
1358 * http://lua-users.org/wiki/MetatableEvents
1359 *
1360 * __index
1361 *
1362 * we access an absent field in a table, the result is nil. This is
1363 * true, but it is not the whole truth. Actually, such access triggers
1364 * the interpreter to look for an __index metamethod: If there is no
1365 * such method, as usually happens, then the access results in nil;
1366 * otherwise, the metamethod will provide the result.
1367 *
1368 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1369 * the key does not appear in the table, but the metatable has an __index
1370 * property:
1371 *
1372 * - if the value is a function, the function is called, passing in the
1373 * table and the key; the return value of that function is returned as
1374 * the result.
1375 *
1376 * - if the value is another table, the value of the key in that table is
1377 * asked for and returned (and if it doesn't exist in that table, but that
1378 * table's metatable has an __index property, then it continues on up)
1379 *
1380 * - Use "rawget(myTable,key)" to skip this metamethod.
1381 *
1382 * http://www.lua.org/pil/13.4.1.html
1383 *
1384 * __newindex
1385 *
1386 * Like __index, but control property assignment.
1387 *
1388 * __mode - Control weak references. A string value with one or both
1389 * of the characters 'k' and 'v' which specifies that the the
1390 * keys and/or values in the table are weak references.
1391 *
1392 * __call - Treat a table like a function. When a table is followed by
1393 * parenthesis such as "myTable( 'foo' )" and the metatable has
1394 * a __call key pointing to a function, that function is invoked
1395 * (passing any specified arguments) and the return value is
1396 * returned.
1397 *
1398 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1399 * called, if the metatable for myTable has a __metatable
1400 * key, the value of that key is returned instead of the
1401 * actual metatable.
1402 *
1403 * __tostring - Control string representation. When the builtin
1404 * "tostring( myTable )" function is called, if the metatable
1405 * for myTable has a __tostring property set to a function,
1406 * that function is invoked (passing myTable to it) and the
1407 * return value is used as the string representation.
1408 *
1409 * __len - Control table length. When the table length is requested using
1410 * the length operator ( '#' ), if the metatable for myTable has
1411 * a __len key pointing to a function, that function is invoked
1412 * (passing myTable to it) and the return value used as the value
1413 * of "#myTable".
1414 *
1415 * __gc - Userdata finalizer code. When userdata is set to be garbage
1416 * collected, if the metatable has a __gc field pointing to a
1417 * function, that function is first invoked, passing the userdata
1418 * to it. The __gc metamethod is not called for tables.
1419 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1420 *
1421 * Special metamethods for redefining standard operators:
1422 * http://www.lua.org/pil/13.1.html
1423 *
1424 * __add "+"
1425 * __sub "-"
1426 * __mul "*"
1427 * __div "/"
1428 * __unm "!"
1429 * __pow "^"
1430 * __concat ".."
1431 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001432 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001433 * http://www.lua.org/pil/13.2.html
1434 *
1435 * __eq "=="
1436 * __lt "<"
1437 * __le "<="
1438 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001439
1440/*
1441 *
1442 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001443 * Class Map
1444 *
1445 *
1446 */
1447
1448/* Returns a struct hlua_map if the stack entry "ud" is
1449 * a class session, otherwise it throws an error.
1450 */
1451__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1452{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001453 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001454}
1455
1456/* This function is the map constructor. It don't need
1457 * the class Map object. It creates and return a new Map
1458 * object. It must be called only during "body" or "init"
1459 * context because it process some filesystem accesses.
1460 */
1461__LJMP static int hlua_map_new(struct lua_State *L)
1462{
1463 const char *fn;
1464 int match = PAT_MATCH_STR;
1465 struct sample_conv conv;
1466 const char *file = "";
1467 int line = 0;
1468 lua_Debug ar;
1469 char *err = NULL;
1470 struct arg args[2];
1471
1472 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1473 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1474
1475 fn = MAY_LJMP(luaL_checkstring(L, 1));
1476
1477 if (lua_gettop(L) >= 2) {
1478 match = MAY_LJMP(luaL_checkinteger(L, 2));
1479 if (match < 0 || match >= PAT_MATCH_NUM)
1480 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1481 }
1482
1483 /* Get Lua filename and line number. */
1484 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1485 lua_getinfo(L, "Sl", &ar); /* get info about it */
1486 if (ar.currentline > 0) { /* is there info? */
1487 file = ar.short_src;
1488 line = ar.currentline;
1489 }
1490 }
1491
1492 /* fill fake sample_conv struct. */
1493 conv.kw = ""; /* unused. */
1494 conv.process = NULL; /* unused. */
1495 conv.arg_mask = 0; /* unused. */
1496 conv.val_args = NULL; /* unused. */
1497 conv.out_type = SMP_T_STR;
1498 conv.private = (void *)(long)match;
1499 switch (match) {
1500 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1501 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1502 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1503 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1504 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1505 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1506 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001507 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001508 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1509 default:
1510 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1511 }
1512
1513 /* fill fake args. */
1514 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001515 args[0].data.str.area = strdup(fn);
1516 args[0].data.str.data = strlen(fn);
1517 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001518 args[1].type = ARGT_STOP;
1519
1520 /* load the map. */
1521 if (!sample_load_map(args, &conv, file, line, &err)) {
1522 /* error case: we cant use luaL_error because we must
1523 * free the err variable.
1524 */
1525 luaL_where(L, 1);
1526 lua_pushfstring(L, "'new': %s.", err);
1527 lua_concat(L, 2);
1528 free(err);
Christopher Faulet73292e92020-08-06 08:40:09 +02001529 free(args[0].data.str.area);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001530 WILL_LJMP(lua_error(L));
1531 }
1532
1533 /* create the lua object. */
1534 lua_newtable(L);
1535 lua_pushlightuserdata(L, args[0].data.map);
1536 lua_rawseti(L, -2, 0);
1537
1538 /* Pop a class Map metatable and affect it to the userdata. */
1539 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1540 lua_setmetatable(L, -2);
1541
1542
1543 return 1;
1544}
1545
1546__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1547{
1548 struct map_descriptor *desc;
1549 struct pattern *pat;
1550 struct sample smp;
1551
1552 MAY_LJMP(check_args(L, 2, "lookup"));
1553 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001554 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001555 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001556 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001557 }
1558 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001559 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001560 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001561 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 +02001562 }
1563
1564 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001565 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001566 if (str)
1567 lua_pushstring(L, "");
1568 else
1569 lua_pushnil(L);
1570 return 1;
1571 }
1572
1573 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001574 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001575 return 1;
1576}
1577
1578__LJMP static int hlua_map_lookup(struct lua_State *L)
1579{
1580 return _hlua_map_lookup(L, 0);
1581}
1582
1583__LJMP static int hlua_map_slookup(struct lua_State *L)
1584{
1585 return _hlua_map_lookup(L, 1);
1586}
1587
1588/*
1589 *
1590 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001591 * Class Socket
1592 *
1593 *
1594 */
1595
1596__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1597{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001598 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001599}
1600
1601/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001602 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001603 * received.
1604 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001605static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001606{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001607 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001608
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001609 if (appctx->ctx.hlua_cosocket.die) {
1610 si_shutw(si);
1611 si_shutr(si);
1612 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001613 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1614 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001615 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1616 }
1617
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001618 /* If we cant write, wakeup the pending write signals. */
1619 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001620 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001621
1622 /* If we cant read, wakeup the pending read signals. */
1623 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001624 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001625
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001626 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001627 * to be notified whenever the connection completes.
1628 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001629 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001630 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001631 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001632 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001633 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001634 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001635
1636 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001637 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001638
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001639 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001640 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001641 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001642
1643 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001644 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001645 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001646
1647 /* Some data were injected in the buffer, notify the stream
1648 * interface.
1649 */
1650 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001651 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001652
1653 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001654 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001655 */
1656 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001657 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001658}
1659
Willy Tarreau87b09662015-04-03 00:22:06 +02001660/* This function is called when the "struct stream" is destroyed.
1661 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001662 * Wake all the pending signals.
1663 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001664static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001665{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001666 struct xref *peer;
1667
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001668 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001669 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1670 if (peer)
1671 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001672
1673 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001674 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1675 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001676}
1677
1678/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001679 * uses this object. If the stream does not exists, just quit.
1680 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001681 * pending signal can rest in the read and write lists. destroy
1682 * it.
1683 */
1684__LJMP static int hlua_socket_gc(lua_State *L)
1685{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001686 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001687 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001688 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001689
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001690 MAY_LJMP(check_args(L, 1, "__gc"));
1691
1692 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001693 peer = xref_get_peer_and_lock(&socket->xref);
1694 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001695 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001696 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001697
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001698 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001699 appctx->ctx.hlua_cosocket.die = 1;
1700 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001702 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001703 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001704 return 0;
1705}
1706
1707/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001708 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001709 */
sada05ed3302018-05-11 11:48:18 -07001710__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001711{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001712 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001713 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001714 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001715 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001716
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001717 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001718
1719 /* Check if we run on the same thread than the xreator thread.
1720 * We cannot access to the socket if the thread is different.
1721 */
1722 if (socket->tid != tid)
1723 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1724
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001725 peer = xref_get_peer_and_lock(&socket->xref);
1726 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001727 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001728
1729 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001730 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001731
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001732 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001733 appctx->ctx.hlua_cosocket.die = 1;
1734 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001735
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001736 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001737 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001738 return 0;
1739}
1740
sada05ed3302018-05-11 11:48:18 -07001741/* The close function calls close_helper.
1742 */
1743__LJMP static int hlua_socket_close(lua_State *L)
1744{
1745 MAY_LJMP(check_args(L, 1, "close"));
1746 return hlua_socket_close_helper(L);
1747}
1748
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001749/* This Lua function assumes that the stack contain three parameters.
1750 * 1 - USERDATA containing a struct socket
1751 * 2 - INTEGER with values of the macro defined below
1752 * If the integer is -1, we must read at most one line.
1753 * If the integer is -2, we ust read all the data until the
1754 * end of the stream.
1755 * If the integer is positive value, we must read a number of
1756 * bytes corresponding to this value.
1757 */
1758#define HLSR_READ_LINE (-1)
1759#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001760__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001761{
1762 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1763 int wanted = lua_tointeger(L, 2);
1764 struct hlua *hlua = hlua_gethlua(L);
1765 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001766 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001767 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001768 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001769 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001770 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001771 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001772 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001773 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001774 struct stream_interface *si;
1775 struct stream *s;
1776 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001777 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001778
1779 /* Check if this lua stack is schedulable. */
1780 if (!hlua || !hlua->task)
1781 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1782 "'frontend', 'backend' or 'task'"));
1783
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001784 /* Check if we run on the same thread than the xreator thread.
1785 * We cannot access to the socket if the thread is different.
1786 */
1787 if (socket->tid != tid)
1788 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1789
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001790 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001791 peer = xref_get_peer_and_lock(&socket->xref);
1792 if (!peer)
1793 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001794 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1795 si = appctx->owner;
1796 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001797
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001798 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001799 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001800 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001801 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001802 if (nblk < 0) /* Connection close. */
1803 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001804 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001805 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001806
1807 /* remove final \r\n. */
1808 if (nblk == 1) {
1809 if (blk1[len1-1] == '\n') {
1810 len1--;
1811 skip_at_end++;
1812 if (blk1[len1-1] == '\r') {
1813 len1--;
1814 skip_at_end++;
1815 }
1816 }
1817 }
1818 else {
1819 if (blk2[len2-1] == '\n') {
1820 len2--;
1821 skip_at_end++;
1822 if (blk2[len2-1] == '\r') {
1823 len2--;
1824 skip_at_end++;
1825 }
1826 }
1827 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001828 }
1829
1830 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001831 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001832 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001833 if (nblk < 0) /* Connection close. */
1834 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001835 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001836 goto connection_empty;
1837 }
1838
1839 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001840 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001841 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001842 if (nblk < 0) /* Connection close. */
1843 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001844 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001845 goto connection_empty;
1846
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001847 missing_bytes = wanted - socket->b.n;
1848 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001849 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001850 len1 = missing_bytes;
1851 } if (nblk == 2 && len1 + len2 > missing_bytes)
1852 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001853 }
1854
1855 len = len1;
1856
1857 luaL_addlstring(&socket->b, blk1, len1);
1858 if (nblk == 2) {
1859 len += len2;
1860 luaL_addlstring(&socket->b, blk2, len2);
1861 }
1862
1863 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001864 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001865
1866 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001867 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001868
1869 /* If the pattern reclaim to read all the data
1870 * in the connection, got out.
1871 */
1872 if (wanted == HLSR_READ_ALL)
1873 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001874 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001875 goto connection_empty;
1876
1877 /* Return result. */
1878 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001879 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001880 return 1;
1881
1882connection_closed:
1883
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001884 xref_unlock(&socket->xref, peer);
1885
1886no_peer:
1887
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001888 /* If the buffer containds data. */
1889 if (socket->b.n > 0) {
1890 luaL_pushresult(&socket->b);
1891 return 1;
1892 }
1893 lua_pushnil(L);
1894 lua_pushstring(L, "connection closed.");
1895 return 2;
1896
1897connection_empty:
1898
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001899 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1900 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001901 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001902 }
1903 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001904 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001905 return 0;
1906}
1907
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001908/* This Lua function gets two parameters. The first one can be string
1909 * or a number. If the string is "*l", the user requires one line. If
1910 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001911 * If the value is a number, the user require a number of bytes equal
1912 * to the value. The default value is "*l" (a line).
1913 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001914 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001915 * integer takes this values:
1916 * -1 : read a line
1917 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001918 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001919 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001920 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001921 * concatenated with the read data.
1922 */
1923__LJMP static int hlua_socket_receive(struct lua_State *L)
1924{
1925 int wanted = HLSR_READ_LINE;
1926 const char *pattern;
1927 int type;
1928 char *error;
1929 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001930 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001931
1932 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1933 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1934
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001935 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001936
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001937 /* Check if we run on the same thread than the xreator thread.
1938 * We cannot access to the socket if the thread is different.
1939 */
1940 if (socket->tid != tid)
1941 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1942
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001943 /* check for pattern. */
1944 if (lua_gettop(L) >= 2) {
1945 type = lua_type(L, 2);
1946 if (type == LUA_TSTRING) {
1947 pattern = lua_tostring(L, 2);
1948 if (strcmp(pattern, "*a") == 0)
1949 wanted = HLSR_READ_ALL;
1950 else if (strcmp(pattern, "*l") == 0)
1951 wanted = HLSR_READ_LINE;
1952 else {
1953 wanted = strtoll(pattern, &error, 10);
1954 if (*error != '\0')
1955 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1956 }
1957 }
1958 else if (type == LUA_TNUMBER) {
1959 wanted = lua_tointeger(L, 2);
1960 if (wanted < 0)
1961 WILL_LJMP(luaL_error(L, "Unsupported size."));
1962 }
1963 }
1964
1965 /* Set pattern. */
1966 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001967
1968 /* Check if we would replace the top by itself. */
1969 if (lua_gettop(L) != 2)
1970 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001971
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001972 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001973 luaL_buffinit(L, &socket->b);
1974
1975 /* Check prefix. */
1976 if (lua_gettop(L) >= 3) {
1977 if (lua_type(L, 3) != LUA_TSTRING)
1978 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1979 pattern = lua_tolstring(L, 3, &len);
1980 luaL_addlstring(&socket->b, pattern, len);
1981 }
1982
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001983 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001984}
1985
1986/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001987 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001988 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001989static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001990{
1991 struct hlua_socket *socket;
1992 struct hlua *hlua = hlua_gethlua(L);
1993 struct appctx *appctx;
1994 size_t buf_len;
1995 const char *buf;
1996 int len;
1997 int send_len;
1998 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001999 struct xref *peer;
2000 struct stream_interface *si;
2001 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002002
2003 /* Check if this lua stack is schedulable. */
2004 if (!hlua || !hlua->task)
2005 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2006 "'frontend', 'backend' or 'task'"));
2007
2008 /* Get object */
2009 socket = MAY_LJMP(hlua_checksocket(L, 1));
2010 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002011 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002012
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002013 /* Check if we run on the same thread than the xreator thread.
2014 * We cannot access to the socket if the thread is different.
2015 */
2016 if (socket->tid != tid)
2017 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2018
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002019 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002020 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002021 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002022 lua_pushinteger(L, -1);
2023 return 1;
2024 }
2025 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2026 si = appctx->owner;
2027 s = si_strm(si);
2028
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002029 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002030 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002031 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002032 lua_pushinteger(L, -1);
2033 return 1;
2034 }
2035
2036 /* Update the input buffer data. */
2037 buf += sent;
2038 send_len = buf_len - sent;
2039
2040 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002041 if (sent >= buf_len) {
2042 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002043 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002044 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002045
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002046 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002047 * the request buffer if its not required.
2048 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002049 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002050 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002051 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002052 }
2053
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002054 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002055 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002056 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002057 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002058 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002059
2060 /* send data */
2061 if (len < send_len)
2062 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002063 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002064
2065 /* "Not enough space" (-1), "Buffer too little to contain
2066 * the data" (-2) are not expected because the available length
2067 * is tested.
2068 * Other unknown error are also not expected.
2069 */
2070 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002071 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002072 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002073
sada05ed3302018-05-11 11:48:18 -07002074 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002075 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002076 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002077 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002078 return 1;
2079 }
2080
2081 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002082 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002083
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002084 s->req.rex = TICK_ETERNITY;
2085 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002086
2087 /* Update length sent. */
2088 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002089 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002090
2091 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002092 if (sent + len >= buf_len) {
2093 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002094 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002095 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002096
2097hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002098 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2099 xref_unlock(&socket->xref, peer);
2100 WILL_LJMP(luaL_error(L, "out of memory"));
2101 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002102 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002103 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002104 return 0;
2105}
2106
2107/* This function initiate the send of data. It just check the input
2108 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002109 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002110 * "hlua_socket_write_yield" that can yield.
2111 *
2112 * The Lua function gets between 3 and 4 parameters. The first one is
2113 * the associated object. The second is a string buffer. The third is
2114 * a facultative integer that represents where is the buffer position
2115 * of the start of the data that can send. The first byte is the
2116 * position "1". The default value is "1". The fourth argument is a
2117 * facultative integer that represents where is the buffer position
2118 * of the end of the data that can send. The default is the last byte.
2119 */
2120static int hlua_socket_send(struct lua_State *L)
2121{
2122 int i;
2123 int j;
2124 const char *buf;
2125 size_t buf_len;
2126
2127 /* Check number of arguments. */
2128 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2129 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2130
2131 /* Get the string. */
2132 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2133
2134 /* Get and check j. */
2135 if (lua_gettop(L) == 4) {
2136 j = MAY_LJMP(luaL_checkinteger(L, 4));
2137 if (j < 0)
2138 j = buf_len + j + 1;
2139 if (j > buf_len)
2140 j = buf_len + 1;
2141 lua_pop(L, 1);
2142 }
2143 else
2144 j = buf_len;
2145
2146 /* Get and check i. */
2147 if (lua_gettop(L) == 3) {
2148 i = MAY_LJMP(luaL_checkinteger(L, 3));
2149 if (i < 0)
2150 i = buf_len + i + 1;
2151 if (i > buf_len)
2152 i = buf_len + 1;
2153 lua_pop(L, 1);
2154 } else
2155 i = 1;
2156
2157 /* Check bth i and j. */
2158 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002159 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002160 return 1;
2161 }
2162 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002163 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002164 return 1;
2165 }
2166 if (i == 0)
2167 i = 1;
2168 if (j == 0)
2169 j = 1;
2170
2171 /* Pop the string. */
2172 lua_pop(L, 1);
2173
2174 /* Update the buffer length. */
2175 buf += i - 1;
2176 buf_len = j - i + 1;
2177 lua_pushlstring(L, buf, buf_len);
2178
2179 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002180 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002181
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002182 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002183}
2184
Willy Tarreau22b0a682015-06-17 19:43:49 +02002185#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002186__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2187{
2188 static char buffer[SOCKET_INFO_MAX_LEN];
2189 int ret;
2190 int len;
2191 char *p;
2192
2193 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2194 if (ret <= 0) {
2195 lua_pushnil(L);
2196 return 1;
2197 }
2198
2199 if (ret == AF_UNIX) {
2200 lua_pushstring(L, buffer+1);
2201 return 1;
2202 }
2203 else if (ret == AF_INET6) {
2204 buffer[0] = '[';
2205 len = strlen(buffer);
2206 buffer[len] = ']';
2207 len++;
2208 buffer[len] = ':';
2209 len++;
2210 p = buffer;
2211 }
2212 else if (ret == AF_INET) {
2213 p = buffer + 1;
2214 len = strlen(p);
2215 p[len] = ':';
2216 len++;
2217 }
2218 else {
2219 lua_pushnil(L);
2220 return 1;
2221 }
2222
2223 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2224 lua_pushnil(L);
2225 return 1;
2226 }
2227
2228 lua_pushstring(L, p);
2229 return 1;
2230}
2231
2232/* Returns information about the peer of the connection. */
2233__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2234{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002235 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002236 struct xref *peer;
2237 struct appctx *appctx;
2238 struct stream_interface *si;
2239 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002240 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002241
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002242 MAY_LJMP(check_args(L, 1, "getpeername"));
2243
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002244 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002245
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002246 /* Check if we run on the same thread than the xreator thread.
2247 * We cannot access to the socket if the thread is different.
2248 */
2249 if (socket->tid != tid)
2250 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2251
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002252 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002253 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002254 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002255 lua_pushnil(L);
2256 return 1;
2257 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002258 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2259 si = appctx->owner;
2260 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002261
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002262 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002263 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002264 lua_pushnil(L);
2265 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002266 }
2267
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002268 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002269 xref_unlock(&socket->xref, peer);
2270 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002271}
2272
2273/* Returns information about my connection side. */
2274static int hlua_socket_getsockname(struct lua_State *L)
2275{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002276 struct hlua_socket *socket;
2277 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002278 struct appctx *appctx;
2279 struct xref *peer;
2280 struct stream_interface *si;
2281 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002282 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002283
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002284 MAY_LJMP(check_args(L, 1, "getsockname"));
2285
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002286 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002287
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002288 /* Check if we run on the same thread than the xreator thread.
2289 * We cannot access to the socket if the thread is different.
2290 */
2291 if (socket->tid != tid)
2292 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2293
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002294 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002295 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002296 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297 lua_pushnil(L);
2298 return 1;
2299 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002300 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2301 si = appctx->owner;
2302 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002304 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002305 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002306 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307 lua_pushnil(L);
2308 return 1;
2309 }
2310
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002311 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002312 xref_unlock(&socket->xref, peer);
2313 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002314}
2315
2316/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002317static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002318 .obj_type = OBJ_TYPE_APPLET,
2319 .name = "<LUA_TCP>",
2320 .fct = hlua_socket_handler,
2321 .release = hlua_socket_release,
2322};
2323
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002324__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002325{
2326 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2327 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002328 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002329 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002330 struct stream_interface *si;
2331 struct stream *s;
2332
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002333 /* Check if we run on the same thread than the xreator thread.
2334 * We cannot access to the socket if the thread is different.
2335 */
2336 if (socket->tid != tid)
2337 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2338
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002339 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002340 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002341 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002342 lua_pushnil(L);
2343 lua_pushstring(L, "Can't connect");
2344 return 2;
2345 }
2346 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2347 si = appctx->owner;
2348 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002349
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002350 /* Check if we run on the same thread than the xreator thread.
2351 * We cannot access to the socket if the thread is different.
2352 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002353 if (socket->tid != tid) {
2354 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002355 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002356 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002357
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002358 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002359 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002360 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002361 lua_pushnil(L);
2362 lua_pushstring(L, "Can't connect");
2363 return 2;
2364 }
2365
Willy Tarreaue09101e2018-10-16 17:37:12 +02002366 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002367
2368 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002369 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002370 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002371 lua_pushinteger(L, 1);
2372 return 1;
2373 }
2374
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002375 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2376 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002377 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002378 }
2379 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002380 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002381 return 0;
2382}
2383
2384/* This function fail or initite the connection. */
2385__LJMP static int hlua_socket_connect(struct lua_State *L)
2386{
2387 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002388 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002390 struct hlua *hlua;
2391 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002392 int low, high;
2393 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002394 struct xref *peer;
2395 struct stream_interface *si;
2396 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002397
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002398 if (lua_gettop(L) < 2)
2399 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002400
2401 /* Get args. */
2402 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002403
2404 /* Check if we run on the same thread than the xreator thread.
2405 * We cannot access to the socket if the thread is different.
2406 */
2407 if (socket->tid != tid)
2408 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2409
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002410 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002411 if (lua_gettop(L) >= 3) {
2412 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002413 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002414
Tim Duesterhus6edab862018-01-06 19:04:45 +01002415 /* Force the ip to end with a colon, to support IPv6 addresses
2416 * that are not enclosed within square brackets.
2417 */
2418 if (port > 0) {
2419 luaL_buffinit(L, &b);
2420 luaL_addstring(&b, ip);
2421 luaL_addchar(&b, ':');
2422 luaL_pushresult(&b);
2423 ip = lua_tolstring(L, lua_gettop(L), NULL);
2424 }
2425 }
2426
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002427 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002428 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002429 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002430 lua_pushnil(L);
2431 return 1;
2432 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002433
2434 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002435 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002436 if (!addr) {
2437 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002438 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002439 }
2440 if (low != high) {
2441 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002442 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002443 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002444
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002445 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002446 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002447 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002448 if (port == -1) {
2449 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002450 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002451 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002452 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2453 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002454 if (port == -1) {
2455 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002456 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002457 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002458 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002459 }
2460 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002461
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002462 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2463 si = appctx->owner;
2464 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002465
2466 if (!sockaddr_alloc(&s->target_addr)) {
2467 xref_unlock(&socket->xref, peer);
2468 WILL_LJMP(luaL_error(L, "connect: internal error"));
2469 }
2470 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002471 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002472
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002473 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002474
2475 /* inform the stream that we want to be notified whenever the
2476 * connection completes.
2477 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002478 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002479 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002480 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002481
Willy Tarreauf31af932020-01-14 09:59:38 +01002482 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002483
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002484 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2485 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002486 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002487 }
2488 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002489
2490 task_wakeup(s->task, TASK_WOKEN_INIT);
2491 /* Return yield waiting for connection. */
2492
Willy Tarreau9635e032018-10-16 17:52:55 +02002493 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002494
2495 return 0;
2496}
2497
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002498#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002499__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2500{
2501 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002502 struct xref *peer;
2503 struct appctx *appctx;
2504 struct stream_interface *si;
2505 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002506
2507 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2508 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002509
2510 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002511 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002512 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002513 lua_pushnil(L);
2514 return 1;
2515 }
2516 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2517 si = appctx->owner;
2518 s = si_strm(si);
2519
2520 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002521 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002522 return MAY_LJMP(hlua_socket_connect(L));
2523}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002524#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002525
2526__LJMP static int hlua_socket_setoption(struct lua_State *L)
2527{
2528 return 0;
2529}
2530
2531__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2532{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002533 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002534 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002535 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002536 struct xref *peer;
2537 struct appctx *appctx;
2538 struct stream_interface *si;
2539 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002540
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002541 MAY_LJMP(check_args(L, 2, "settimeout"));
2542
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002543 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002544
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002545 /* convert the timeout to millis */
2546 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002547
Thierry Fournier17a921b2018-03-08 09:59:02 +01002548 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002549 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002550 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2551
Mark Lakes56cc1252018-03-27 09:48:06 +02002552 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002553 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002554
2555 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002556 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002557 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002558
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002559 /* Check if we run on the same thread than the xreator thread.
2560 * We cannot access to the socket if the thread is different.
2561 */
2562 if (socket->tid != tid)
2563 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2564
Mark Lakes56cc1252018-03-27 09:48:06 +02002565 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002566 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002567 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002568 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2569 WILL_LJMP(lua_error(L));
2570 return 0;
2571 }
2572 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2573 si = appctx->owner;
2574 s = si_strm(si);
2575
Cyril Bonté7bb63452018-08-17 23:51:02 +02002576 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002577 s->req.rto = tmout;
2578 s->req.wto = tmout;
2579 s->res.rto = tmout;
2580 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002581 s->req.rex = tick_add_ifset(now_ms, tmout);
2582 s->req.wex = tick_add_ifset(now_ms, tmout);
2583 s->res.rex = tick_add_ifset(now_ms, tmout);
2584 s->res.wex = tick_add_ifset(now_ms, tmout);
2585
2586 s->task->expire = tick_add_ifset(now_ms, tmout);
2587 task_queue(s->task);
2588
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002589 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002590
Thierry Fourniere9636f12018-03-08 09:54:32 +01002591 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002592 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002593}
2594
2595__LJMP static int hlua_socket_new(lua_State *L)
2596{
2597 struct hlua_socket *socket;
2598 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002599 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002600 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002601
2602 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002603 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002604 hlua_pusherror(L, "socket: full stack");
2605 goto out_fail_conf;
2606 }
2607
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002608 /* Create the object: obj[0] = userdata. */
2609 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002610 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002611 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002612 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002613 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002614
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002615 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002616 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002617 hlua_pusherror(L, "socket: uninitialized pools.");
2618 goto out_fail_conf;
2619 }
2620
Willy Tarreau87b09662015-04-03 00:22:06 +02002621 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002622 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2623 lua_setmetatable(L, -2);
2624
Willy Tarreaud420a972015-04-06 00:39:18 +02002625 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002626 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002627 if (!appctx) {
2628 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002629 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002630 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002631
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002632 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002633 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002634 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2635 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002636
Willy Tarreaud420a972015-04-06 00:39:18 +02002637 /* Now create a session, task and stream for this applet */
2638 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2639 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002640 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002641 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002642 }
2643
Willy Tarreau87787ac2017-08-28 16:22:54 +02002644 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002645 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002646 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002647 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002648 }
2649
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002650 /* Initialise cross reference between stream and Lua socket object. */
2651 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002652
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002653 /* Configure "right" stream interface. this "si" is used to connect
2654 * and retrieve data from the server. The connection is initialized
2655 * with the "struct server".
2656 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002657 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002658
2659 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002660 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002661 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002662
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002663 return 1;
2664
Willy Tarreaud420a972015-04-06 00:39:18 +02002665 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002666 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002667 out_fail_sess:
2668 appctx_free(appctx);
2669 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002670 WILL_LJMP(lua_error(L));
2671 return 0;
2672}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002673
2674/*
2675 *
2676 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002677 * Class Channel
2678 *
2679 *
2680 */
2681
2682/* Returns the struct hlua_channel join to the class channel in the
2683 * stack entry "ud" or throws an argument error.
2684 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002685__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002686{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002687 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002688}
2689
Willy Tarreau47860ed2015-03-10 14:07:50 +01002690/* Pushes the channel onto the top of the stack. If the stask does not have a
2691 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002692 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002693static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002694{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002695 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002696 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002697 return 0;
2698
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002699 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002700 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002701 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002702
2703 /* Pop a class sesison metatable and affect it to the userdata. */
2704 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2705 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002706 return 1;
2707}
2708
2709/* Duplicate all the data present in the input channel and put it
2710 * in a string LUA variables. Returns -1 and push a nil value in
2711 * the stack if the channel is closed and all the data are consumed,
2712 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002713 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002714 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002715static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002716{
2717 char *blk1;
2718 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002719 size_t len1;
2720 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002721 int ret;
2722 luaL_Buffer b;
2723
Willy Tarreau06d80a92017-10-19 14:32:15 +02002724 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002725 if (unlikely(ret == 0))
2726 return 0;
2727
2728 if (unlikely(ret < 0)) {
2729 lua_pushnil(L);
2730 return -1;
2731 }
2732
2733 luaL_buffinit(L, &b);
2734 luaL_addlstring(&b, blk1, len1);
2735 if (unlikely(ret == 2))
2736 luaL_addlstring(&b, blk2, len2);
2737 luaL_pushresult(&b);
2738
2739 if (unlikely(ret == 2))
2740 return len1 + len2;
2741 return len1;
2742}
2743
2744/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2745 * a yield. This function keep the data in the buffer.
2746 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002747__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002748{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002749 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002750
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002751 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2752
Christopher Faulet3f829a42018-12-13 21:56:45 +01002753 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2754 WILL_LJMP(lua_error(L));
2755
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002756 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002757 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002758 return 1;
2759}
2760
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002761/* Check arguments for the function "hlua_channel_dup_yield". */
2762__LJMP static int hlua_channel_dup(lua_State *L)
2763{
2764 MAY_LJMP(check_args(L, 1, "dup"));
2765 MAY_LJMP(hlua_checkchannel(L, 1));
2766 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2767}
2768
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002769/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2770 * a yield. This function consumes the data in the buffer. It returns
2771 * a string containing the data or a nil pointer if no data are available
2772 * and the channel is closed.
2773 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002774__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002775{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002776 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002777 int ret;
2778
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002779 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002780
Christopher Faulet3f829a42018-12-13 21:56:45 +01002781 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2782 WILL_LJMP(lua_error(L));
2783
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002784 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002785 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002786 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002787
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002788 if (unlikely(ret == -1))
2789 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002790
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002791 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002792 return 1;
2793}
2794
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002795/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002796__LJMP static int hlua_channel_get(lua_State *L)
2797{
2798 MAY_LJMP(check_args(L, 1, "get"));
2799 MAY_LJMP(hlua_checkchannel(L, 1));
2800 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2801}
2802
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002803/* This functions consumes and returns one line. If the channel is closed,
2804 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002805 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002806 * value.
2807 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002808__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002809{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002810 char *blk1;
2811 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002812 size_t len1;
2813 size_t len2;
2814 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002815 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002816 int ret;
2817 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002818
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002819 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2820
Christopher Faulet3f829a42018-12-13 21:56:45 +01002821 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2822 WILL_LJMP(lua_error(L));
2823
Willy Tarreau06d80a92017-10-19 14:32:15 +02002824 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002825 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002826 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002827
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002828 if (ret == -1) {
2829 lua_pushnil(L);
2830 return 1;
2831 }
2832
2833 luaL_buffinit(L, &b);
2834 luaL_addlstring(&b, blk1, len1);
2835 len = len1;
2836 if (unlikely(ret == 2)) {
2837 luaL_addlstring(&b, blk2, len2);
2838 len += len2;
2839 }
2840 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002841 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002842 return 1;
2843}
2844
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002845/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002846__LJMP static int hlua_channel_getline(lua_State *L)
2847{
2848 MAY_LJMP(check_args(L, 1, "getline"));
2849 MAY_LJMP(hlua_checkchannel(L, 1));
2850 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2851}
2852
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002853/* This function takes a string as input, and append it at the
2854 * input side of channel. If the data is too big, but a space
2855 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002856 * yields. If the data is bigger than the buffer, or if the
2857 * channel is closed, it returns -1. Otherwise, it returns the
2858 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002859 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002860__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002861{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002862 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002863 size_t len;
2864 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2865 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2866 int ret;
2867 int max;
2868
Christopher Faulet3f829a42018-12-13 21:56:45 +01002869 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2870 WILL_LJMP(lua_error(L));
2871
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002872 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002873 * the request buffer if its not required.
2874 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002875 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002876 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002877 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002878 }
2879
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002880 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002881 if (max > len - l)
2882 max = len - l;
2883
Willy Tarreau06d80a92017-10-19 14:32:15 +02002884 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002885 if (ret == -2 || ret == -3) {
2886 lua_pushinteger(L, -1);
2887 return 1;
2888 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002889 if (ret == -1) {
2890 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002891 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002892 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002893 l += ret;
2894 lua_pop(L, 1);
2895 lua_pushinteger(L, l);
2896
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002897 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002898 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002899 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002900 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002901 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002902 */
2903 return 1;
2904 }
2905 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002906 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002907 return 1;
2908}
2909
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002910/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2911 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912 * buffer size is too little for the data.
2913 */
2914__LJMP static int hlua_channel_append(lua_State *L)
2915{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002916 size_t len;
2917
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002919 MAY_LJMP(hlua_checkchannel(L, 1));
2920 MAY_LJMP(luaL_checklstring(L, 2, &len));
2921 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002922 lua_pushinteger(L, 0);
2923
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002924 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002925}
2926
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002927/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002928 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002929 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002930 * or -1 if the channel is closed or if the buffer size is too
2931 * little for the data.
2932 */
2933__LJMP static int hlua_channel_set(lua_State *L)
2934{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002935 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002936
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002938 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002939 lua_pushinteger(L, 0);
2940
Christopher Faulet3f829a42018-12-13 21:56:45 +01002941 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2942 WILL_LJMP(lua_error(L));
2943
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002944 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002945
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002946 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002947}
2948
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002949/* Append data in the output side of the buffer. This data is immediately
2950 * sent. The function returns the amount of data written. If the buffer
2951 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002952 * if the channel is closed.
2953 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002954__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002955{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002956 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002957 size_t len;
2958 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2959 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2960 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002961 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002962
Christopher Faulet3f829a42018-12-13 21:56:45 +01002963 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2964 WILL_LJMP(lua_error(L));
2965
Willy Tarreau47860ed2015-03-10 14:07:50 +01002966 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002967 lua_pushinteger(L, -1);
2968 return 1;
2969 }
2970
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002971 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002972 * the request buffer if its not required.
2973 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002974 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002975 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002976 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002977 }
2978
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002979 /* The written data will be immediately sent, so we can check
2980 * the available space without taking in account the reserve.
2981 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002982 * data, because the buffer will be flushed.
2983 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002984 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002985
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002986 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002987 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002988 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002989 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02002990 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002991 return 1;
2992
2993 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994 if (max > len - l)
2995 max = len - l;
2996
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002997 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002998 * detects a non contiguous buffer and realign it.
2999 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003000 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003001 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003002
3003 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003004 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003005
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003006 /* buffer replace considers that the input part is filled.
3007 * so, I must forward these new data in the output part.
3008 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003009 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010
3011 l += max;
3012 lua_pop(L, 1);
3013 lua_pushinteger(L, l);
3014
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003015 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003016 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003017 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003018 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003019 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003020 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003021 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003022
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003023 if (l < len) {
3024 /* If we are waiting for space in the response buffer, we
3025 * must set the flag WAKERESWR. This flag required the task
3026 * wake up if any activity is detected on the response buffer.
3027 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003028 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003029 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003030 else
3031 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003032 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003033 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003034
3035 return 1;
3036}
3037
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003038/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003039 * yield the LUA process, and resume it without checking the
3040 * input arguments.
3041 */
3042__LJMP static int hlua_channel_send(lua_State *L)
3043{
3044 MAY_LJMP(check_args(L, 2, "send"));
3045 lua_pushinteger(L, 0);
3046
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003047 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003048}
3049
3050/* This function forward and amount of butes. The data pass from
3051 * the input side of the buffer to the output side, and can be
3052 * forwarded. This function never fails.
3053 *
3054 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003055 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003056 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003057__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003058{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003059 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003060 int len;
3061 int l;
3062 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003063 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003064
3065 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003066
3067 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3068 WILL_LJMP(lua_error(L));
3069
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003070 len = MAY_LJMP(luaL_checkinteger(L, 2));
3071 l = MAY_LJMP(luaL_checkinteger(L, -1));
3072
3073 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003074 if (max > ci_data(chn))
3075 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003076 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003077 l += max;
3078
3079 lua_pop(L, 1);
3080 lua_pushinteger(L, l);
3081
3082 /* Check if it miss bytes to forward. */
3083 if (l < len) {
3084 /* The the input channel or the output channel are closed, we
3085 * must return the amount of data forwarded.
3086 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003087 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003088 return 1;
3089
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003090 /* If we are waiting for space data in the response buffer, we
3091 * must set the flag WAKERESWR. This flag required the task
3092 * wake up if any activity is detected on the response buffer.
3093 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003094 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003095 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003096 else
3097 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003098
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003099 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003100 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003101 }
3102
3103 return 1;
3104}
3105
3106/* Just check the input and prepare the stack for the previous
3107 * function "hlua_channel_forward_yield"
3108 */
3109__LJMP static int hlua_channel_forward(lua_State *L)
3110{
3111 MAY_LJMP(check_args(L, 2, "forward"));
3112 MAY_LJMP(hlua_checkchannel(L, 1));
3113 MAY_LJMP(luaL_checkinteger(L, 2));
3114
3115 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003116 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003117}
3118
3119/* Just returns the number of bytes available in the input
3120 * side of the buffer. This function never fails.
3121 */
3122__LJMP static int hlua_channel_get_in_len(lua_State *L)
3123{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003124 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003125
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003126 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003127 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003128 if (IS_HTX_STRM(chn_strm(chn))) {
3129 struct htx *htx = htxbuf(&chn->buf);
3130 lua_pushinteger(L, htx->data - co_data(chn));
3131 }
3132 else
3133 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003134 return 1;
3135}
3136
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003137/* Returns true if the channel is full. */
3138__LJMP static int hlua_channel_is_full(lua_State *L)
3139{
3140 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003141
3142 MAY_LJMP(check_args(L, 1, "is_full"));
3143 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003144 /* ignore the reserve, we are not on a producer side (ie in an
3145 * applet).
3146 */
3147 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003148 return 1;
3149}
3150
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003151/* Returns true if the channel is the response channel. */
3152__LJMP static int hlua_channel_is_resp(lua_State *L)
3153{
3154 struct channel *chn;
3155
3156 MAY_LJMP(check_args(L, 1, "is_resp"));
3157 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3158
3159 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3160 return 1;
3161}
3162
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003163/* Just returns the number of bytes available in the output
3164 * side of the buffer. This function never fails.
3165 */
3166__LJMP static int hlua_channel_get_out_len(lua_State *L)
3167{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003168 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003169
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003170 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003171 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003172 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003173 return 1;
3174}
3175
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003176/*
3177 *
3178 *
3179 * Class Fetches
3180 *
3181 *
3182 */
3183
3184/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003185 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003186 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003187__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003188{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003189 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003190}
3191
3192/* This function creates and push in the stack a fetch object according
3193 * with a current TXN.
3194 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003195static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003196{
Willy Tarreau7073c472015-04-06 11:15:40 +02003197 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003198
3199 /* Check stack size. */
3200 if (!lua_checkstack(L, 3))
3201 return 0;
3202
3203 /* Create the object: obj[0] = userdata.
3204 * Note that the base of the Fetches object is the
3205 * transaction object.
3206 */
3207 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003208 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003209 lua_rawseti(L, -2, 0);
3210
Willy Tarreau7073c472015-04-06 11:15:40 +02003211 hsmp->s = txn->s;
3212 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003213 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003214 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003215
3216 /* Pop a class sesison metatable and affect it to the userdata. */
3217 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3218 lua_setmetatable(L, -2);
3219
3220 return 1;
3221}
3222
3223/* This function is an LUA binding. It is called with each sample-fetch.
3224 * It uses closure argument to store the associated sample-fetch. It
3225 * returns only one argument or throws an error. An error is thrown
3226 * only if an error is encountered during the argument parsing. If
3227 * the "sample-fetch" function fails, nil is returned.
3228 */
3229__LJMP static int hlua_run_sample_fetch(lua_State *L)
3230{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003231 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003232 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003233 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003234 int i;
3235 struct sample smp;
3236
3237 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003238 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003239
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003240 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003241 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003242
Thierry FOURNIERca988662015-12-20 18:43:03 +01003243 /* Check execution authorization. */
3244 if (f->use & SMP_USE_HTTP_ANY &&
3245 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3246 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3247 "is not available in Lua services", f->kw);
3248 WILL_LJMP(lua_error(L));
3249 }
3250
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003251 /* Get extra arguments. */
3252 for (i = 0; i < lua_gettop(L) - 1; i++) {
3253 if (i >= ARGM_NBARGS)
3254 break;
3255 hlua_lua2arg(L, i + 2, &args[i]);
3256 }
3257 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003258 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003259
3260 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003261 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003262
3263 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003264 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003265 lua_pushfstring(L, "error in arguments");
3266 WILL_LJMP(lua_error(L));
3267 }
3268
3269 /* Initialise the sample. */
3270 memset(&smp, 0, sizeof(smp));
3271
3272 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003273 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003274 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003275 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003276 lua_pushstring(L, "");
3277 else
3278 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003279 return 1;
3280 }
3281
3282 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003283 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003284 hlua_smp2lua_str(L, &smp);
3285 else
3286 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003287 return 1;
3288}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003289
3290/*
3291 *
3292 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003293 * Class Converters
3294 *
3295 *
3296 */
3297
3298/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003299 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003300 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003301__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003302{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003303 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003304}
3305
3306/* This function creates and push in the stack a Converters object
3307 * according with a current TXN.
3308 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003309static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003310{
Willy Tarreau7073c472015-04-06 11:15:40 +02003311 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003312
3313 /* Check stack size. */
3314 if (!lua_checkstack(L, 3))
3315 return 0;
3316
3317 /* Create the object: obj[0] = userdata.
3318 * Note that the base of the Converters object is the
3319 * same than the TXN object.
3320 */
3321 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003322 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003323 lua_rawseti(L, -2, 0);
3324
Willy Tarreau7073c472015-04-06 11:15:40 +02003325 hsmp->s = txn->s;
3326 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003327 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003328 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003329
Willy Tarreau87b09662015-04-03 00:22:06 +02003330 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003331 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3332 lua_setmetatable(L, -2);
3333
3334 return 1;
3335}
3336
3337/* This function is an LUA binding. It is called with each converter.
3338 * It uses closure argument to store the associated converter. It
3339 * returns only one argument or throws an error. An error is thrown
3340 * only if an error is encountered during the argument parsing. If
3341 * the converter function function fails, nil is returned.
3342 */
3343__LJMP static int hlua_run_sample_conv(lua_State *L)
3344{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003345 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003346 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003347 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003348 int i;
3349 struct sample smp;
3350
3351 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003352 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003353
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003354 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003355 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003356
3357 /* Get extra arguments. */
3358 for (i = 0; i < lua_gettop(L) - 2; i++) {
3359 if (i >= ARGM_NBARGS)
3360 break;
3361 hlua_lua2arg(L, i + 3, &args[i]);
3362 }
3363 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003364 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003365
3366 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003367 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003368
3369 /* Run the special args checker. */
3370 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3371 hlua_pusherror(L, "error in arguments");
3372 WILL_LJMP(lua_error(L));
3373 }
3374
3375 /* Initialise the sample. */
3376 if (!hlua_lua2smp(L, 2, &smp)) {
3377 hlua_pusherror(L, "error in the input argument");
3378 WILL_LJMP(lua_error(L));
3379 }
3380
Willy Tarreau1777ea62016-03-10 16:15:46 +01003381 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3382
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003383 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003384 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003385 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003386 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003387 WILL_LJMP(lua_error(L));
3388 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003389 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3390 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003391 hlua_pusherror(L, "error during the input argument casting");
3392 WILL_LJMP(lua_error(L));
3393 }
3394
3395 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003396 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003397 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003398 lua_pushstring(L, "");
3399 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003400 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003401 return 1;
3402 }
3403
3404 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003405 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003406 hlua_smp2lua_str(L, &smp);
3407 else
3408 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003409 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003410}
3411
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003412/*
3413 *
3414 *
3415 * Class AppletTCP
3416 *
3417 *
3418 */
3419
3420/* Returns a struct hlua_txn if the stack entry "ud" is
3421 * a class stream, otherwise it throws an error.
3422 */
3423__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3424{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003425 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003426}
3427
3428/* This function creates and push in the stack an Applet object
3429 * according with a current TXN.
3430 */
3431static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3432{
3433 struct hlua_appctx *appctx;
3434 struct stream_interface *si = ctx->owner;
3435 struct stream *s = si_strm(si);
3436 struct proxy *p = s->be;
3437
3438 /* Check stack size. */
3439 if (!lua_checkstack(L, 3))
3440 return 0;
3441
3442 /* Create the object: obj[0] = userdata.
3443 * Note that the base of the Converters object is the
3444 * same than the TXN object.
3445 */
3446 lua_newtable(L);
3447 appctx = lua_newuserdata(L, sizeof(*appctx));
3448 lua_rawseti(L, -2, 0);
3449 appctx->appctx = ctx;
3450 appctx->htxn.s = s;
3451 appctx->htxn.p = p;
3452
3453 /* Create the "f" field that contains a list of fetches. */
3454 lua_pushstring(L, "f");
3455 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3456 return 0;
3457 lua_settable(L, -3);
3458
3459 /* Create the "sf" field that contains a list of stringsafe fetches. */
3460 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003461 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003462 return 0;
3463 lua_settable(L, -3);
3464
3465 /* Create the "c" field that contains a list of converters. */
3466 lua_pushstring(L, "c");
3467 if (!hlua_converters_new(L, &appctx->htxn, 0))
3468 return 0;
3469 lua_settable(L, -3);
3470
3471 /* Create the "sc" field that contains a list of stringsafe converters. */
3472 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003473 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003474 return 0;
3475 lua_settable(L, -3);
3476
3477 /* Pop a class stream metatable and affect it to the table. */
3478 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3479 lua_setmetatable(L, -2);
3480
3481 return 1;
3482}
3483
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003484__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3485{
3486 struct hlua_appctx *appctx;
3487 struct stream *s;
3488 const char *name;
3489 size_t len;
3490 struct sample smp;
3491
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003492 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3493 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003494
3495 /* It is useles to retrieve the stream, but this function
3496 * runs only in a stream context.
3497 */
3498 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3499 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3500 s = appctx->htxn.s;
3501
3502 /* Converts the third argument in a sample. */
3503 hlua_lua2smp(L, 3, &smp);
3504
3505 /* Store the sample in a variable. */
3506 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003507
3508 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3509 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3510 else
3511 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3512
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003513 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003514}
3515
3516__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3517{
3518 struct hlua_appctx *appctx;
3519 struct stream *s;
3520 const char *name;
3521 size_t len;
3522 struct sample smp;
3523
3524 MAY_LJMP(check_args(L, 2, "unset_var"));
3525
3526 /* It is useles to retrieve the stream, but this function
3527 * runs only in a stream context.
3528 */
3529 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3530 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3531 s = appctx->htxn.s;
3532
3533 /* Unset the variable. */
3534 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003535 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3536 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003537}
3538
3539__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3540{
3541 struct hlua_appctx *appctx;
3542 struct stream *s;
3543 const char *name;
3544 size_t len;
3545 struct sample smp;
3546
3547 MAY_LJMP(check_args(L, 2, "get_var"));
3548
3549 /* It is useles to retrieve the stream, but this function
3550 * runs only in a stream context.
3551 */
3552 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3553 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3554 s = appctx->htxn.s;
3555
3556 smp_set_owner(&smp, s->be, s->sess, s, 0);
3557 if (!vars_get_by_name(name, len, &smp)) {
3558 lua_pushnil(L);
3559 return 1;
3560 }
3561
3562 return hlua_smp2lua(L, &smp);
3563}
3564
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003565__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3566{
3567 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3568 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003569 struct hlua *hlua;
3570
3571 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003572 if (!s->hlua)
3573 return 0;
3574 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003575
3576 MAY_LJMP(check_args(L, 2, "set_priv"));
3577
3578 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003579 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003580
3581 /* Get and store new value. */
3582 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3583 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3584
3585 return 0;
3586}
3587
3588__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3589{
3590 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3591 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003592 struct hlua *hlua;
3593
3594 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003595 if (!s->hlua) {
3596 lua_pushnil(L);
3597 return 1;
3598 }
3599 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003600
3601 /* Push configuration index in the stack. */
3602 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3603
3604 return 1;
3605}
3606
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003607/* If expected data not yet available, it returns a yield. This function
3608 * consumes the data in the buffer. It returns a string containing the
3609 * data. This string can be empty.
3610 */
3611__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3612{
3613 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3614 struct stream_interface *si = appctx->appctx->owner;
3615 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003616 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003617 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003618 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003619 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003620
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003621 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003622 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003623
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003624 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003625 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003626 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003627 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003628 }
3629
3630 /* End of data: commit the total strings and return. */
3631 if (ret < 0) {
3632 luaL_pushresult(&appctx->b);
3633 return 1;
3634 }
3635
3636 /* Ensure that the block 2 length is usable. */
3637 if (ret == 1)
3638 len2 = 0;
3639
3640 /* dont check the max length read and dont check. */
3641 luaL_addlstring(&appctx->b, blk1, len1);
3642 luaL_addlstring(&appctx->b, blk2, len2);
3643
3644 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003645 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003646 luaL_pushresult(&appctx->b);
3647 return 1;
3648}
3649
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003650/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003651__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3652{
3653 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3654
3655 /* Initialise the string catenation. */
3656 luaL_buffinit(L, &appctx->b);
3657
3658 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3659}
3660
3661/* If expected data not yet available, it returns a yield. This function
3662 * consumes the data in the buffer. It returns a string containing the
3663 * data. This string can be empty.
3664 */
3665__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3666{
3667 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3668 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003669 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003670 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003671 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003672 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003673 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003674 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003675
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003676 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003677 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003678
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003679 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003680 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003681 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003682 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003683 }
3684
3685 /* End of data: commit the total strings and return. */
3686 if (ret < 0) {
3687 luaL_pushresult(&appctx->b);
3688 return 1;
3689 }
3690
3691 /* Ensure that the block 2 length is usable. */
3692 if (ret == 1)
3693 len2 = 0;
3694
3695 if (len == -1) {
3696
3697 /* If len == -1, catenate all the data avalaile and
3698 * yield because we want to get all the data until
3699 * the end of data stream.
3700 */
3701 luaL_addlstring(&appctx->b, blk1, len1);
3702 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003703 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003704 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003705 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003706
3707 } else {
3708
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003709 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003710 if (len1 > len)
3711 len1 = len;
3712 luaL_addlstring(&appctx->b, blk1, len1);
3713 len -= len1;
3714
3715 /* Copy the second block. */
3716 if (len2 > len)
3717 len2 = len;
3718 luaL_addlstring(&appctx->b, blk2, len2);
3719 len -= len2;
3720
3721 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003722 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003723
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003724 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003725 if (len > 0) {
3726 lua_pushinteger(L, len);
3727 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003728 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003729 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003730 }
3731
3732 /* return the result. */
3733 luaL_pushresult(&appctx->b);
3734 return 1;
3735 }
3736
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003737 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003738 hlua_pusherror(L, "Lua: internal error");
3739 WILL_LJMP(lua_error(L));
3740 return 0;
3741}
3742
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003743/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003744__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3745{
3746 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3747 int len = -1;
3748
3749 if (lua_gettop(L) > 2)
3750 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3751 if (lua_gettop(L) >= 2) {
3752 len = MAY_LJMP(luaL_checkinteger(L, 2));
3753 lua_pop(L, 1);
3754 }
3755
3756 /* Confirm or set the required length */
3757 lua_pushinteger(L, len);
3758
3759 /* Initialise the string catenation. */
3760 luaL_buffinit(L, &appctx->b);
3761
3762 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3763}
3764
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003765/* Append data in the output side of the buffer. This data is immediately
3766 * sent. The function returns the amount of data written. If the buffer
3767 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003768 * if the channel is closed.
3769 */
3770__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3771{
3772 size_t len;
3773 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3774 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3775 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3776 struct stream_interface *si = appctx->appctx->owner;
3777 struct channel *chn = si_ic(si);
3778 int max;
3779
3780 /* Get the max amount of data which can write as input in the channel. */
3781 max = channel_recv_max(chn);
3782 if (max > (len - l))
3783 max = len - l;
3784
3785 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003786 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003787
3788 /* update counters. */
3789 l += max;
3790 lua_pop(L, 1);
3791 lua_pushinteger(L, l);
3792
3793 /* If some data is not send, declares the situation to the
3794 * applet, and returns a yield.
3795 */
3796 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003797 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003798 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003799 }
3800
3801 return 1;
3802}
3803
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003804/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003805 * yield the LUA process, and resume it without checking the
3806 * input arguments.
3807 */
3808__LJMP static int hlua_applet_tcp_send(lua_State *L)
3809{
3810 MAY_LJMP(check_args(L, 2, "send"));
3811 lua_pushinteger(L, 0);
3812
3813 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3814}
3815
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003816/*
3817 *
3818 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003819 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003820 *
3821 *
3822 */
3823
3824/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003825 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003826 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003827__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003828{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003829 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003830}
3831
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003832/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003833 * according with a current TXN.
3834 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003835static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003836{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003837 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003838 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003839 struct stream_interface *si = ctx->owner;
3840 struct stream *s = si_strm(si);
3841 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003842 struct htx *htx;
3843 struct htx_blk *blk;
3844 struct htx_sl *sl;
3845 struct ist path;
3846 unsigned long long len = 0;
3847 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003848
3849 /* Check stack size. */
3850 if (!lua_checkstack(L, 3))
3851 return 0;
3852
3853 /* Create the object: obj[0] = userdata.
3854 * Note that the base of the Converters object is the
3855 * same than the TXN object.
3856 */
3857 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003858 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003859 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003860 appctx->appctx = ctx;
3861 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003862 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003863 appctx->htxn.s = s;
3864 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003865
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003866 /* Create the "f" field that contains a list of fetches. */
3867 lua_pushstring(L, "f");
3868 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3869 return 0;
3870 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003871
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003872 /* Create the "sf" field that contains a list of stringsafe fetches. */
3873 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003874 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003875 return 0;
3876 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003877
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003878 /* Create the "c" field that contains a list of converters. */
3879 lua_pushstring(L, "c");
3880 if (!hlua_converters_new(L, &appctx->htxn, 0))
3881 return 0;
3882 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003883
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003884 /* Create the "sc" field that contains a list of stringsafe converters. */
3885 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003886 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003887 return 0;
3888 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003889
Christopher Fauleta2097962019-07-15 16:25:33 +02003890 htx = htxbuf(&s->req.buf);
3891 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003892 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003893 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003894
Christopher Fauleta2097962019-07-15 16:25:33 +02003895 /* Stores the request method. */
3896 lua_pushstring(L, "method");
3897 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3898 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003899
Christopher Fauleta2097962019-07-15 16:25:33 +02003900 /* Stores the http version. */
3901 lua_pushstring(L, "version");
3902 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3903 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003904
Christopher Fauleta2097962019-07-15 16:25:33 +02003905 /* creates an array of headers. hlua_http_get_headers() crates and push
3906 * the array on the top of the stack.
3907 */
3908 lua_pushstring(L, "headers");
3909 htxn.s = s;
3910 htxn.p = px;
3911 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01003912 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02003913 return 0;
3914 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003915
Christopher Fauleta2097962019-07-15 16:25:33 +02003916 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01003917 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02003918 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003919
Christopher Fauleta2097962019-07-15 16:25:33 +02003920 p = path.ptr;
3921 end = path.ptr + path.len;
3922 q = p;
3923 while (q < end && *q != '?')
3924 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003925
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003926 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003927 lua_pushstring(L, "path");
3928 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003929 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003930
Christopher Fauleta2097962019-07-15 16:25:33 +02003931 /* Stores the query string. */
3932 lua_pushstring(L, "qs");
3933 if (*q == '?')
3934 q++;
3935 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003936 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003937 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003938
Christopher Fauleta2097962019-07-15 16:25:33 +02003939 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3940 struct htx_blk *blk = htx_get_blk(htx, pos);
3941 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003942
Christopher Fauleta2097962019-07-15 16:25:33 +02003943 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3944 break;
3945 if (type == HTX_BLK_DATA)
3946 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003947 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003948 if (htx->extra != ULLONG_MAX)
3949 len += htx->extra;
3950
3951 /* Stores the request path. */
3952 lua_pushstring(L, "length");
3953 lua_pushinteger(L, len);
3954 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003955
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003956 /* Create an empty array of HTTP request headers. */
3957 lua_pushstring(L, "response");
3958 lua_newtable(L);
3959 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003960
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003961 /* Pop a class stream metatable and affect it to the table. */
3962 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3963 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003964
3965 return 1;
3966}
3967
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003968__LJMP static int hlua_applet_http_set_var(lua_State *L)
3969{
3970 struct hlua_appctx *appctx;
3971 struct stream *s;
3972 const char *name;
3973 size_t len;
3974 struct sample smp;
3975
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003976 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3977 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003978
3979 /* It is useles to retrieve the stream, but this function
3980 * runs only in a stream context.
3981 */
3982 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3983 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3984 s = appctx->htxn.s;
3985
3986 /* Converts the third argument in a sample. */
3987 hlua_lua2smp(L, 3, &smp);
3988
3989 /* Store the sample in a variable. */
3990 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003991
3992 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3993 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3994 else
3995 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3996
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003997 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003998}
3999
4000__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4001{
4002 struct hlua_appctx *appctx;
4003 struct stream *s;
4004 const char *name;
4005 size_t len;
4006 struct sample smp;
4007
4008 MAY_LJMP(check_args(L, 2, "unset_var"));
4009
4010 /* It is useles to retrieve the stream, but this function
4011 * runs only in a stream context.
4012 */
4013 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4014 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4015 s = appctx->htxn.s;
4016
4017 /* Unset the variable. */
4018 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004019 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4020 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004021}
4022
4023__LJMP static int hlua_applet_http_get_var(lua_State *L)
4024{
4025 struct hlua_appctx *appctx;
4026 struct stream *s;
4027 const char *name;
4028 size_t len;
4029 struct sample smp;
4030
4031 MAY_LJMP(check_args(L, 2, "get_var"));
4032
4033 /* It is useles to retrieve the stream, but this function
4034 * runs only in a stream context.
4035 */
4036 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4037 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4038 s = appctx->htxn.s;
4039
4040 smp_set_owner(&smp, s->be, s->sess, s, 0);
4041 if (!vars_get_by_name(name, len, &smp)) {
4042 lua_pushnil(L);
4043 return 1;
4044 }
4045
4046 return hlua_smp2lua(L, &smp);
4047}
4048
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004049__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4050{
4051 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4052 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004053 struct hlua *hlua;
4054
4055 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004056 if (!s->hlua)
4057 return 0;
4058 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004059
4060 MAY_LJMP(check_args(L, 2, "set_priv"));
4061
4062 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004063 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004064
4065 /* Get and store new value. */
4066 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4067 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4068
4069 return 0;
4070}
4071
4072__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4073{
4074 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4075 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004076 struct hlua *hlua;
4077
4078 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004079 if (!s->hlua) {
4080 lua_pushnil(L);
4081 return 1;
4082 }
4083 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004084
4085 /* Push configuration index in the stack. */
4086 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4087
4088 return 1;
4089}
4090
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004091/* If expected data not yet available, it returns a yield. This function
4092 * consumes the data in the buffer. It returns a string containing the
4093 * data. This string can be empty.
4094 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004095__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004096{
4097 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4098 struct stream_interface *si = appctx->appctx->owner;
4099 struct channel *req = si_oc(si);
4100 struct htx *htx;
4101 struct htx_blk *blk;
4102 size_t count;
4103 int stop = 0;
4104
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004105 htx = htx_from_buf(&req->buf);
4106 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004107 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004108
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004109 while (count && !stop && blk) {
4110 enum htx_blk_type type = htx_get_blk_type(blk);
4111 uint32_t sz = htx_get_blksz(blk);
4112 struct ist v;
4113 uint32_t vlen;
4114 char *nl;
4115
Christopher Faulete461e342018-12-18 16:43:35 +01004116 if (type == HTX_BLK_EOM) {
4117 stop = 1;
4118 break;
4119 }
4120
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004121 vlen = sz;
4122 if (vlen > count) {
4123 if (type != HTX_BLK_DATA)
4124 break;
4125 vlen = count;
4126 }
4127
4128 switch (type) {
4129 case HTX_BLK_UNUSED:
4130 break;
4131
4132 case HTX_BLK_DATA:
4133 v = htx_get_blk_value(htx, blk);
4134 v.len = vlen;
4135 nl = istchr(v, '\n');
4136 if (nl != NULL) {
4137 stop = 1;
4138 vlen = nl - v.ptr + 1;
4139 }
4140 luaL_addlstring(&appctx->b, v.ptr, vlen);
4141 break;
4142
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004143 case HTX_BLK_TLR:
4144 case HTX_BLK_EOM:
4145 stop = 1;
4146 break;
4147
4148 default:
4149 break;
4150 }
4151
4152 co_set_data(req, co_data(req) - vlen);
4153 count -= vlen;
4154 if (sz == vlen)
4155 blk = htx_remove_blk(htx, blk);
4156 else {
4157 htx_cut_data_blk(htx, blk, vlen);
4158 break;
4159 }
4160 }
4161
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004162 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004163 if (!stop) {
4164 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004165 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004166 }
4167
4168 /* return the result. */
4169 luaL_pushresult(&appctx->b);
4170 return 1;
4171}
4172
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004173
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004174/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004175__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004176{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004177 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004178
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004179 /* Initialise the string catenation. */
4180 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004181
Christopher Fauleta2097962019-07-15 16:25:33 +02004182 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004183}
4184
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004185/* If expected data not yet available, it returns a yield. This function
4186 * consumes the data in the buffer. It returns a string containing the
4187 * data. This string can be empty.
4188 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004189__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004190{
4191 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4192 struct stream_interface *si = appctx->appctx->owner;
4193 struct channel *req = si_oc(si);
4194 struct htx *htx;
4195 struct htx_blk *blk;
4196 size_t count;
4197 int len;
4198
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004199 htx = htx_from_buf(&req->buf);
4200 len = MAY_LJMP(luaL_checkinteger(L, 2));
4201 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004202 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004203 while (count && len && blk) {
4204 enum htx_blk_type type = htx_get_blk_type(blk);
4205 uint32_t sz = htx_get_blksz(blk);
4206 struct ist v;
4207 uint32_t vlen;
4208
Christopher Faulete461e342018-12-18 16:43:35 +01004209 if (type == HTX_BLK_EOM) {
4210 len = 0;
4211 break;
4212 }
4213
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004214 vlen = sz;
4215 if (len > 0 && vlen > len)
4216 vlen = len;
4217 if (vlen > count) {
4218 if (type != HTX_BLK_DATA)
4219 break;
4220 vlen = count;
4221 }
4222
4223 switch (type) {
4224 case HTX_BLK_UNUSED:
4225 break;
4226
4227 case HTX_BLK_DATA:
4228 v = htx_get_blk_value(htx, blk);
4229 luaL_addlstring(&appctx->b, v.ptr, vlen);
4230 break;
4231
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004232 case HTX_BLK_TLR:
4233 case HTX_BLK_EOM:
4234 len = 0;
4235 break;
4236
4237 default:
4238 break;
4239 }
4240
4241 co_set_data(req, co_data(req) - vlen);
4242 count -= vlen;
4243 if (len > 0)
4244 len -= vlen;
4245 if (sz == vlen)
4246 blk = htx_remove_blk(htx, blk);
4247 else {
4248 htx_cut_data_blk(htx, blk, vlen);
4249 break;
4250 }
4251 }
4252
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004253 htx_to_buf(htx, &req->buf);
4254
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004255 /* If we are no other data available, yield waiting for new data. */
4256 if (len) {
4257 if (len > 0) {
4258 lua_pushinteger(L, len);
4259 lua_replace(L, 2);
4260 }
4261 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004262 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004263 }
4264
4265 /* return the result. */
4266 luaL_pushresult(&appctx->b);
4267 return 1;
4268}
4269
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004270/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004271__LJMP static int hlua_applet_http_recv(lua_State *L)
4272{
4273 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4274 int len = -1;
4275
4276 /* Check arguments. */
4277 if (lua_gettop(L) > 2)
4278 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4279 if (lua_gettop(L) >= 2) {
4280 len = MAY_LJMP(luaL_checkinteger(L, 2));
4281 lua_pop(L, 1);
4282 }
4283
Christopher Fauleta2097962019-07-15 16:25:33 +02004284 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004285
Christopher Fauleta2097962019-07-15 16:25:33 +02004286 /* Initialise the string catenation. */
4287 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004288
Christopher Fauleta2097962019-07-15 16:25:33 +02004289 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004290}
4291
4292/* Append data in the output side of the buffer. This data is immediately
4293 * sent. The function returns the amount of data written. If the buffer
4294 * cannot contain the data, the function yields. The function returns -1
4295 * if the channel is closed.
4296 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004297__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004298{
4299 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4300 struct stream_interface *si = appctx->appctx->owner;
4301 struct channel *res = si_ic(si);
4302 struct htx *htx = htx_from_buf(&res->buf);
4303 const char *data;
4304 size_t len;
4305 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4306 int max;
4307
Christopher Faulet9060fc02019-07-03 11:39:30 +02004308 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004309 if (!max)
4310 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004311
4312 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4313
4314 /* Get the max amount of data which can write as input in the channel. */
4315 if (max > (len - l))
4316 max = len - l;
4317
4318 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004319 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004320 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004321
4322 /* update counters. */
4323 l += max;
4324 lua_pop(L, 1);
4325 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004326
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004327 /* If some data is not send, declares the situation to the
4328 * applet, and returns a yield.
4329 */
4330 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004331 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004332 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004333 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004334 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004335 }
4336
Christopher Fauleta2097962019-07-15 16:25:33 +02004337 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004338 return 1;
4339}
4340
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004341/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004342 * yield the LUA process, and resume it without checking the
4343 * input arguments.
4344 */
4345__LJMP static int hlua_applet_http_send(lua_State *L)
4346{
4347 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004348
4349 /* We want to send some data. Headers must be sent. */
4350 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4351 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4352 WILL_LJMP(lua_error(L));
4353 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004354
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004355 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004356 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004357
Christopher Fauleta2097962019-07-15 16:25:33 +02004358 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004359}
4360
4361__LJMP static int hlua_applet_http_addheader(lua_State *L)
4362{
4363 const char *name;
4364 int ret;
4365
4366 MAY_LJMP(hlua_checkapplet_http(L, 1));
4367 name = MAY_LJMP(luaL_checkstring(L, 2));
4368 MAY_LJMP(luaL_checkstring(L, 3));
4369
4370 /* Push in the stack the "response" entry. */
4371 ret = lua_getfield(L, 1, "response");
4372 if (ret != LUA_TTABLE) {
4373 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4374 "is expected as an array. %s found", lua_typename(L, ret));
4375 WILL_LJMP(lua_error(L));
4376 }
4377
4378 /* check if the header is already registered if it is not
4379 * the case, register it.
4380 */
4381 ret = lua_getfield(L, -1, name);
4382 if (ret == LUA_TNIL) {
4383
4384 /* Entry not found. */
4385 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4386
4387 /* Insert the new header name in the array in the top of the stack.
4388 * It left the new array in the top of the stack.
4389 */
4390 lua_newtable(L);
4391 lua_pushvalue(L, 2);
4392 lua_pushvalue(L, -2);
4393 lua_settable(L, -4);
4394
4395 } else if (ret != LUA_TTABLE) {
4396
4397 /* corruption error. */
4398 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4399 "is expected as an array. %s found", name, lua_typename(L, ret));
4400 WILL_LJMP(lua_error(L));
4401 }
4402
4403 /* Now the top od thestack is an array of values. We push
4404 * the header value as new entry.
4405 */
4406 lua_pushvalue(L, 3);
4407 ret = lua_rawlen(L, -2);
4408 lua_rawseti(L, -2, ret + 1);
4409 lua_pushboolean(L, 1);
4410 return 1;
4411}
4412
4413__LJMP static int hlua_applet_http_status(lua_State *L)
4414{
4415 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4416 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004417 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004418
4419 if (status < 100 || status > 599) {
4420 lua_pushboolean(L, 0);
4421 return 1;
4422 }
4423
4424 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004425 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004426 lua_pushboolean(L, 1);
4427 return 1;
4428}
4429
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004430
Christopher Fauleta2097962019-07-15 16:25:33 +02004431__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004432{
4433 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4434 struct stream_interface *si = appctx->appctx->owner;
4435 struct channel *res = si_ic(si);
4436 struct htx *htx;
4437 struct htx_sl *sl;
4438 struct h1m h1m;
4439 const char *status, *reason;
4440 const char *name, *value;
4441 size_t nlen, vlen;
4442 unsigned int flags;
4443
4444 /* Send the message at once. */
4445 htx = htx_from_buf(&res->buf);
4446 h1m_init_res(&h1m);
4447
4448 /* Use the same http version than the request. */
4449 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4450 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4451 if (reason == NULL)
4452 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4453 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4454 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4455 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4456 }
4457 else {
4458 flags = HTX_SL_F_IS_RESP;
4459 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4460 }
4461 if (!sl) {
4462 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4463 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4464 WILL_LJMP(lua_error(L));
4465 }
4466 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4467
4468 /* Get the array associated to the field "response" in the object AppletHTTP. */
4469 lua_pushvalue(L, 0);
4470 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4471 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4472 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4473 WILL_LJMP(lua_error(L));
4474 }
4475
4476 /* Browse the list of headers. */
4477 lua_pushnil(L);
4478 while(lua_next(L, -2) != 0) {
4479 /* We expect a string as -2. */
4480 if (lua_type(L, -2) != LUA_TSTRING) {
4481 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4482 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4483 lua_typename(L, lua_type(L, -2)));
4484 WILL_LJMP(lua_error(L));
4485 }
4486 name = lua_tolstring(L, -2, &nlen);
4487
4488 /* We expect an array as -1. */
4489 if (lua_type(L, -1) != LUA_TTABLE) {
4490 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4491 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4492 name,
4493 lua_typename(L, lua_type(L, -1)));
4494 WILL_LJMP(lua_error(L));
4495 }
4496
4497 /* Browse the table who is on the top of the stack. */
4498 lua_pushnil(L);
4499 while(lua_next(L, -2) != 0) {
4500 int id;
4501
4502 /* We expect a number as -2. */
4503 if (lua_type(L, -2) != LUA_TNUMBER) {
4504 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4505 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4506 name,
4507 lua_typename(L, lua_type(L, -2)));
4508 WILL_LJMP(lua_error(L));
4509 }
4510 id = lua_tointeger(L, -2);
4511
4512 /* We expect a string as -2. */
4513 if (lua_type(L, -1) != LUA_TSTRING) {
4514 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4515 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4516 name, id,
4517 lua_typename(L, lua_type(L, -1)));
4518 WILL_LJMP(lua_error(L));
4519 }
4520 value = lua_tolstring(L, -1, &vlen);
4521
4522 /* Simple Protocol checks. */
4523 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004524 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004525 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4526 struct ist v = ist2(value, vlen);
4527 int ret;
4528
4529 ret = h1_parse_cont_len_header(&h1m, &v);
4530 if (ret < 0) {
4531 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4532 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4533 name);
4534 WILL_LJMP(lua_error(L));
4535 }
4536 else if (ret == 0)
4537 goto next; /* Skip it */
4538 }
4539
4540 /* Add a new header */
4541 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4542 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4543 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4544 name);
4545 WILL_LJMP(lua_error(L));
4546 }
4547 next:
4548 /* Remove the array from the stack, and get next element with a remaining string. */
4549 lua_pop(L, 1);
4550 }
4551
4552 /* Remove the array from the stack, and get next element with a remaining string. */
4553 lua_pop(L, 1);
4554 }
4555
4556 if (h1m.flags & H1_MF_CHNK)
4557 h1m.flags &= ~H1_MF_CLEN;
4558 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4559 h1m.flags |= H1_MF_XFER_LEN;
4560
4561 /* Uset HTX start-line flags */
4562 if (h1m.flags & H1_MF_XFER_ENC)
4563 flags |= HTX_SL_F_XFER_ENC;
4564 if (h1m.flags & H1_MF_XFER_LEN) {
4565 flags |= HTX_SL_F_XFER_LEN;
4566 if (h1m.flags & H1_MF_CHNK)
4567 flags |= HTX_SL_F_CHNK;
4568 else if (h1m.flags & H1_MF_CLEN)
4569 flags |= HTX_SL_F_CLEN;
4570 if (h1m.body_len == 0)
4571 flags |= HTX_SL_F_BODYLESS;
4572 }
4573 sl->flags |= flags;
4574
4575 /* If we dont have a content-length set, and the HTTP version is 1.1
4576 * and the status code implies the presence of a message body, we must
4577 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004578 * for the keepalive compliance. If the applet announces a transfer-encoding
4579 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004580 */
4581 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4582 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4583 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4584 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4585 /* Add a new header */
4586 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4587 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4588 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4589 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4590 WILL_LJMP(lua_error(L));
4591 }
4592 }
4593
4594 /* Finalize headers. */
4595 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4596 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4597 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4598 WILL_LJMP(lua_error(L));
4599 }
4600
4601 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4602 b_reset(&res->buf);
4603 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4604 WILL_LJMP(lua_error(L));
4605 }
4606
4607 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004608 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004609
4610 /* Headers sent, set the flag. */
4611 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4612 return 0;
4613
4614}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004615/* We will build the status line and the headers of the HTTP response.
4616 * We will try send at once if its not possible, we give back the hand
4617 * waiting for more room.
4618 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004619__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004620{
4621 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4622 struct stream_interface *si = appctx->appctx->owner;
4623 struct channel *res = si_ic(si);
4624
4625 if (co_data(res)) {
4626 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004627 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004628 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004629 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004630}
4631
4632
Christopher Fauleta2097962019-07-15 16:25:33 +02004633__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004634{
Christopher Fauleta2097962019-07-15 16:25:33 +02004635 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004636}
4637
Christopher Fauleta2097962019-07-15 16:25:33 +02004638/*
4639 *
4640 *
4641 * Class HTTP
4642 *
4643 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004644 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004645
4646/* Returns a struct hlua_txn if the stack entry "ud" is
4647 * a class stream, otherwise it throws an error.
4648 */
4649__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004650{
Christopher Fauleta2097962019-07-15 16:25:33 +02004651 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4652}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004653
Christopher Fauleta2097962019-07-15 16:25:33 +02004654/* This function creates and push in the stack a HTTP object
4655 * according with a current TXN.
4656 */
4657static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4658{
4659 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004660
Christopher Fauleta2097962019-07-15 16:25:33 +02004661 /* Check stack size. */
4662 if (!lua_checkstack(L, 3))
4663 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004664
Christopher Fauleta2097962019-07-15 16:25:33 +02004665 /* Create the object: obj[0] = userdata.
4666 * Note that the base of the Converters object is the
4667 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004668 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004669 lua_newtable(L);
4670 htxn = lua_newuserdata(L, sizeof(*htxn));
4671 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004672
4673 htxn->s = txn->s;
4674 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004675 htxn->dir = txn->dir;
4676 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004677
4678 /* Pop a class stream metatable and affect it to the table. */
4679 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4680 lua_setmetatable(L, -2);
4681
4682 return 1;
4683}
4684
4685/* This function creates ans returns an array of HTTP headers.
4686 * This function does not fails. It is used as wrapper with the
4687 * 2 following functions.
4688 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004689__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004690{
Christopher Fauleta2097962019-07-15 16:25:33 +02004691 struct htx *htx;
4692 int32_t pos;
4693
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004694 /* Create the table. */
4695 lua_newtable(L);
4696
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004697
Christopher Fauleta2097962019-07-15 16:25:33 +02004698 htx = htxbuf(&msg->chn->buf);
4699 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4700 struct htx_blk *blk = htx_get_blk(htx, pos);
4701 enum htx_blk_type type = htx_get_blk_type(blk);
4702 struct ist n, v;
4703 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004704
Christopher Fauleta2097962019-07-15 16:25:33 +02004705 if (type == HTX_BLK_HDR) {
4706 n = htx_get_blk_name(htx,blk);
4707 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004708 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004709 else if (type == HTX_BLK_EOH)
4710 break;
4711 else
4712 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004713
Christopher Fauleta2097962019-07-15 16:25:33 +02004714 /* Check for existing entry:
4715 * assume that the table is on the top of the stack, and
4716 * push the key in the stack, the function lua_gettable()
4717 * perform the lookup.
4718 */
4719 lua_pushlstring(L, n.ptr, n.len);
4720 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004721
Christopher Fauleta2097962019-07-15 16:25:33 +02004722 switch (lua_type(L, -1)) {
4723 case LUA_TNIL:
4724 /* Table not found, create it. */
4725 lua_pop(L, 1); /* remove the nil value. */
4726 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4727 lua_newtable(L); /* create and push empty table. */
4728 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4729 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4730 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004731 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004732
Christopher Fauleta2097962019-07-15 16:25:33 +02004733 case LUA_TTABLE:
4734 /* Entry found: push the value in the table. */
4735 len = lua_rawlen(L, -1);
4736 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4737 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4738 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4739 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004740
Christopher Fauleta2097962019-07-15 16:25:33 +02004741 default:
4742 /* Other cases are errors. */
4743 hlua_pusherror(L, "internal error during the parsing of headers.");
4744 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004745 }
4746 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004747 return 1;
4748}
4749
4750__LJMP static int hlua_http_req_get_headers(lua_State *L)
4751{
4752 struct hlua_txn *htxn;
4753
4754 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4755 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4756
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004757 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004758 WILL_LJMP(lua_error(L));
4759
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004760 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004761}
4762
4763__LJMP static int hlua_http_res_get_headers(lua_State *L)
4764{
4765 struct hlua_txn *htxn;
4766
4767 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4768 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4769
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004770 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004771 WILL_LJMP(lua_error(L));
4772
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004773 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004774}
4775
4776/* This function replace full header, or just a value in
4777 * the request or in the response. It is a wrapper fir the
4778 * 4 following functions.
4779 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004780__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004781{
4782 size_t name_len;
4783 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4784 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4785 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004786 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004787 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004788
Dragan Dosen26743032019-04-30 15:54:36 +02004789 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004790 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4791
Christopher Fauleta2097962019-07-15 16:25:33 +02004792 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004793 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004794 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004795 return 0;
4796}
4797
4798__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4799{
4800 struct hlua_txn *htxn;
4801
4802 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4803 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4804
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004805 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004806 WILL_LJMP(lua_error(L));
4807
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004808 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004809}
4810
4811__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4812{
4813 struct hlua_txn *htxn;
4814
4815 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4816 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4817
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004818 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004819 WILL_LJMP(lua_error(L));
4820
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004821 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004822}
4823
4824__LJMP static int hlua_http_req_rep_val(lua_State *L)
4825{
4826 struct hlua_txn *htxn;
4827
4828 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4829 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4830
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004831 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004832 WILL_LJMP(lua_error(L));
4833
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004834 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004835}
4836
4837__LJMP static int hlua_http_res_rep_val(lua_State *L)
4838{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004839 struct hlua_txn *htxn;
4840
4841 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4842 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4843
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004844 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004845 WILL_LJMP(lua_error(L));
4846
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004847 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004848}
4849
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004850/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004851 * It is a wrapper for the 2 following functions.
4852 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004853__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004854{
4855 size_t len;
4856 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004857 struct htx *htx = htxbuf(&msg->chn->buf);
4858 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004859
Christopher Fauleta2097962019-07-15 16:25:33 +02004860 ctx.blk = NULL;
4861 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4862 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004863 return 0;
4864}
4865
4866__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4867{
4868 struct hlua_txn *htxn;
4869
4870 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4871 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4872
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004873 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004874 WILL_LJMP(lua_error(L));
4875
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004876 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004877}
4878
4879__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4880{
4881 struct hlua_txn *htxn;
4882
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004883 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004884 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4885
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004886 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004887 WILL_LJMP(lua_error(L));
4888
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004889 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004890}
4891
4892/* This function adds an header. It is a wrapper used by
4893 * the 2 following functions.
4894 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004895__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004896{
4897 size_t name_len;
4898 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4899 size_t value_len;
4900 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004901 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004902
Christopher Fauleta2097962019-07-15 16:25:33 +02004903 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4904 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004905 return 0;
4906}
4907
4908__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4909{
4910 struct hlua_txn *htxn;
4911
4912 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4913 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4914
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004915 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004916 WILL_LJMP(lua_error(L));
4917
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004918 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004919}
4920
4921__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4922{
4923 struct hlua_txn *htxn;
4924
4925 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4926 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4927
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004928 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004929 WILL_LJMP(lua_error(L));
4930
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004931 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004932}
4933
4934static int hlua_http_req_set_hdr(lua_State *L)
4935{
4936 struct hlua_txn *htxn;
4937
4938 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4939 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4940
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004941 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004942 WILL_LJMP(lua_error(L));
4943
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004944 hlua_http_del_hdr(L, &htxn->s->txn->req);
4945 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004946}
4947
4948static int hlua_http_res_set_hdr(lua_State *L)
4949{
4950 struct hlua_txn *htxn;
4951
4952 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4953 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4954
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004955 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004956 WILL_LJMP(lua_error(L));
4957
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004958 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
4959 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004960}
4961
4962/* This function set the method. */
4963static int hlua_http_req_set_meth(lua_State *L)
4964{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004965 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004966 size_t name_len;
4967 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004968
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004969 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004970 WILL_LJMP(lua_error(L));
4971
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004972 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004973 return 1;
4974}
4975
4976/* This function set the method. */
4977static int hlua_http_req_set_path(lua_State *L)
4978{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004979 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004980 size_t name_len;
4981 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004982
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004983 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004984 WILL_LJMP(lua_error(L));
4985
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004986 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004987 return 1;
4988}
4989
4990/* This function set the query-string. */
4991static int hlua_http_req_set_query(lua_State *L)
4992{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004993 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004994 size_t name_len;
4995 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004996
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004997 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004998 WILL_LJMP(lua_error(L));
4999
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005000 /* Check length. */
5001 if (name_len > trash.size - 1) {
5002 lua_pushboolean(L, 0);
5003 return 1;
5004 }
5005
5006 /* Add the mark question as prefix. */
5007 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005008 trash.area[trash.data++] = '?';
5009 memcpy(trash.area + trash.data, name, name_len);
5010 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005011
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005012 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005013 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005014 return 1;
5015}
5016
5017/* This function set the uri. */
5018static int hlua_http_req_set_uri(lua_State *L)
5019{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005020 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005021 size_t name_len;
5022 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005023
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005024 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005025 WILL_LJMP(lua_error(L));
5026
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005027 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005028 return 1;
5029}
5030
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005031/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005032static int hlua_http_res_set_status(lua_State *L)
5033{
5034 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5035 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005036 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5037 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005038
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005039 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005040 WILL_LJMP(lua_error(L));
5041
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005042 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005043 return 0;
5044}
5045
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005046/*
5047 *
5048 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005049 * Class TXN
5050 *
5051 *
5052 */
5053
5054/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005055 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005056 */
5057__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5058{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005059 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005060}
5061
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005062__LJMP static int hlua_set_var(lua_State *L)
5063{
5064 struct hlua_txn *htxn;
5065 const char *name;
5066 size_t len;
5067 struct sample smp;
5068
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005069 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5070 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005071
5072 /* It is useles to retrieve the stream, but this function
5073 * runs only in a stream context.
5074 */
5075 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5076 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5077
5078 /* Converts the third argument in a sample. */
5079 hlua_lua2smp(L, 3, &smp);
5080
5081 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005082 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005083
5084 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5085 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5086 else
5087 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5088
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005089 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005090}
5091
Christopher Faulet85d79c92016-11-09 16:54:56 +01005092__LJMP static int hlua_unset_var(lua_State *L)
5093{
5094 struct hlua_txn *htxn;
5095 const char *name;
5096 size_t len;
5097 struct sample smp;
5098
5099 MAY_LJMP(check_args(L, 2, "unset_var"));
5100
5101 /* It is useles to retrieve the stream, but this function
5102 * runs only in a stream context.
5103 */
5104 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5105 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5106
5107 /* Unset the variable. */
5108 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005109 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5110 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005111}
5112
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005113__LJMP static int hlua_get_var(lua_State *L)
5114{
5115 struct hlua_txn *htxn;
5116 const char *name;
5117 size_t len;
5118 struct sample smp;
5119
5120 MAY_LJMP(check_args(L, 2, "get_var"));
5121
5122 /* It is useles to retrieve the stream, but this function
5123 * runs only in a stream context.
5124 */
5125 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5126 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5127
Willy Tarreau7560dd42016-03-10 16:28:58 +01005128 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005129 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005130 lua_pushnil(L);
5131 return 1;
5132 }
5133
5134 return hlua_smp2lua(L, &smp);
5135}
5136
Willy Tarreau59551662015-03-10 14:23:13 +01005137__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005138{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005139 struct hlua *hlua;
5140
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005141 MAY_LJMP(check_args(L, 2, "set_priv"));
5142
Willy Tarreau87b09662015-04-03 00:22:06 +02005143 /* It is useles to retrieve the stream, but this function
5144 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005145 */
5146 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005147 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005148
5149 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005150 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005151
5152 /* Get and store new value. */
5153 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5154 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5155
5156 return 0;
5157}
5158
Willy Tarreau59551662015-03-10 14:23:13 +01005159__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005160{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005161 struct hlua *hlua;
5162
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005163 MAY_LJMP(check_args(L, 1, "get_priv"));
5164
Willy Tarreau87b09662015-04-03 00:22:06 +02005165 /* It is useles to retrieve the stream, but this function
5166 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005167 */
5168 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005169 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005170
5171 /* Push configuration index in the stack. */
5172 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5173
5174 return 1;
5175}
5176
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005177/* Create stack entry containing a class TXN. This function
5178 * return 0 if the stack does not contains free slots,
5179 * otherwise it returns 1.
5180 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005181static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005182{
Willy Tarreaude491382015-04-06 11:04:28 +02005183 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005184
5185 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005186 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005187 return 0;
5188
5189 /* NOTE: The allocation never fails. The failure
5190 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005191 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005192 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005193 /* Create the object: obj[0] = userdata. */
5194 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005195 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005196 lua_rawseti(L, -2, 0);
5197
Willy Tarreaude491382015-04-06 11:04:28 +02005198 htxn->s = s;
5199 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005200 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005201 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005202
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005203 /* Create the "f" field that contains a list of fetches. */
5204 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005205 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005206 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005207 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005208
5209 /* Create the "sf" field that contains a list of stringsafe fetches. */
5210 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005211 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005212 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005213 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005214
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005215 /* Create the "c" field that contains a list of converters. */
5216 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005217 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005218 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005219 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005220
5221 /* Create the "sc" field that contains a list of stringsafe converters. */
5222 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005223 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005224 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005225 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005226
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005227 /* Create the "req" field that contains the request channel object. */
5228 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005229 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005230 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005231 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005232
5233 /* Create the "res" field that contains the response channel object. */
5234 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005235 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005236 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005237 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005238
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005239 /* Creates the HTTP object is the current proxy allows http. */
5240 lua_pushstring(L, "http");
5241 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005242 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005243 return 0;
5244 }
5245 else
5246 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005247 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005248
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005249 /* Pop a class sesison metatable and affect it to the userdata. */
5250 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5251 lua_setmetatable(L, -2);
5252
5253 return 1;
5254}
5255
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005256__LJMP static int hlua_txn_deflog(lua_State *L)
5257{
5258 const char *msg;
5259 struct hlua_txn *htxn;
5260
5261 MAY_LJMP(check_args(L, 2, "deflog"));
5262 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5263 msg = MAY_LJMP(luaL_checkstring(L, 2));
5264
5265 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5266 return 0;
5267}
5268
5269__LJMP static int hlua_txn_log(lua_State *L)
5270{
5271 int level;
5272 const char *msg;
5273 struct hlua_txn *htxn;
5274
5275 MAY_LJMP(check_args(L, 3, "log"));
5276 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5277 level = MAY_LJMP(luaL_checkinteger(L, 2));
5278 msg = MAY_LJMP(luaL_checkstring(L, 3));
5279
5280 if (level < 0 || level >= NB_LOG_LEVELS)
5281 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5282
5283 hlua_sendlog(htxn->s->be, level, msg);
5284 return 0;
5285}
5286
5287__LJMP static int hlua_txn_log_debug(lua_State *L)
5288{
5289 const char *msg;
5290 struct hlua_txn *htxn;
5291
5292 MAY_LJMP(check_args(L, 2, "Debug"));
5293 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5294 msg = MAY_LJMP(luaL_checkstring(L, 2));
5295 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5296 return 0;
5297}
5298
5299__LJMP static int hlua_txn_log_info(lua_State *L)
5300{
5301 const char *msg;
5302 struct hlua_txn *htxn;
5303
5304 MAY_LJMP(check_args(L, 2, "Info"));
5305 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5306 msg = MAY_LJMP(luaL_checkstring(L, 2));
5307 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5308 return 0;
5309}
5310
5311__LJMP static int hlua_txn_log_warning(lua_State *L)
5312{
5313 const char *msg;
5314 struct hlua_txn *htxn;
5315
5316 MAY_LJMP(check_args(L, 2, "Warning"));
5317 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5318 msg = MAY_LJMP(luaL_checkstring(L, 2));
5319 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5320 return 0;
5321}
5322
5323__LJMP static int hlua_txn_log_alert(lua_State *L)
5324{
5325 const char *msg;
5326 struct hlua_txn *htxn;
5327
5328 MAY_LJMP(check_args(L, 2, "Alert"));
5329 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5330 msg = MAY_LJMP(luaL_checkstring(L, 2));
5331 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5332 return 0;
5333}
5334
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005335__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5336{
5337 struct hlua_txn *htxn;
5338 int ll;
5339
5340 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5341 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5342 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5343
5344 if (ll < 0 || ll > 7)
5345 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5346
5347 htxn->s->logs.level = ll;
5348 return 0;
5349}
5350
5351__LJMP static int hlua_txn_set_tos(lua_State *L)
5352{
5353 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005354 int tos;
5355
5356 MAY_LJMP(check_args(L, 2, "set_tos"));
5357 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5358 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5359
Willy Tarreau1a18b542018-12-11 16:37:42 +01005360 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005361 return 0;
5362}
5363
5364__LJMP static int hlua_txn_set_mark(lua_State *L)
5365{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005366 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005367 int mark;
5368
5369 MAY_LJMP(check_args(L, 2, "set_mark"));
5370 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5371 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5372
Lukas Tribus579e3e32019-08-11 18:03:45 +02005373 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005374 return 0;
5375}
5376
Patrick Hemmer268a7072018-05-11 12:52:31 -04005377__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5378{
5379 struct hlua_txn *htxn;
5380
5381 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5382 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5383 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5384 return 0;
5385}
5386
5387__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5388{
5389 struct hlua_txn *htxn;
5390
5391 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5392 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5393 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5394 return 0;
5395}
5396
Christopher Faulet700d9e82020-01-31 12:21:52 +01005397/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005398 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005399 * message and terminate the transaction. It returns 1 on success and 0 on
5400 * error. The Reply must be on top of the stack.
5401 */
5402__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5403{
5404 struct htx *htx;
5405 struct htx_sl *sl;
5406 struct h1m h1m;
5407 const char *status, *reason, *body;
5408 size_t status_len, reason_len, body_len;
5409 int ret, code, flags;
5410
5411 code = 200;
5412 status = "200";
5413 status_len = 3;
5414 ret = lua_getfield(L, -1, "status");
5415 if (ret == LUA_TNUMBER) {
5416 code = lua_tointeger(L, -1);
5417 status = lua_tolstring(L, -1, &status_len);
5418 }
5419 lua_pop(L, 1);
5420
5421 reason = http_get_reason(code);
5422 reason_len = strlen(reason);
5423 ret = lua_getfield(L, -1, "reason");
5424 if (ret == LUA_TSTRING)
5425 reason = lua_tolstring(L, -1, &reason_len);
5426 lua_pop(L, 1);
5427
5428 body = NULL;
5429 body_len = 0;
5430 ret = lua_getfield(L, -1, "body");
5431 if (ret == LUA_TSTRING)
5432 body = lua_tolstring(L, -1, &body_len);
5433 lua_pop(L, 1);
5434
5435 /* Prepare the response before inserting the headers */
5436 h1m_init_res(&h1m);
5437 htx = htx_from_buf(&s->res.buf);
5438 channel_htx_truncate(&s->res, htx);
5439 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5440 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5441 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5442 ist2(status, status_len), ist2(reason, reason_len));
5443 }
5444 else {
5445 flags = HTX_SL_F_IS_RESP;
5446 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5447 ist2(status, status_len), ist2(reason, reason_len));
5448 }
5449 if (!sl)
5450 goto fail;
5451 sl->info.res.status = code;
5452
5453 /* Push in the stack the "headers" entry. */
5454 ret = lua_getfield(L, -1, "headers");
5455 if (ret != LUA_TTABLE)
5456 goto skip_headers;
5457
5458 lua_pushnil(L);
5459 while (lua_next(L, -2) != 0) {
5460 struct ist name, value;
5461 const char *n, *v;
5462 size_t nlen, vlen;
5463
5464 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5465 /* Skip element if the key is not a string or if the value is not a table */
5466 goto next_hdr;
5467 }
5468
5469 n = lua_tolstring(L, -2, &nlen);
5470 name = ist2(n, nlen);
5471 if (isteqi(name, ist("content-length"))) {
5472 /* Always skip content-length header. It will be added
5473 * later with the correct len
5474 */
5475 goto next_hdr;
5476 }
5477
5478 /* Loop on header's values */
5479 lua_pushnil(L);
5480 while (lua_next(L, -2)) {
5481 if (!lua_isstring(L, -1)) {
5482 /* Skip the value if it is not a string */
5483 goto next_value;
5484 }
5485
5486 v = lua_tolstring(L, -1, &vlen);
5487 value = ist2(v, vlen);
5488
5489 if (isteqi(name, ist("transfer-encoding")))
5490 h1_parse_xfer_enc_header(&h1m, value);
5491 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5492 goto fail;
5493
5494 next_value:
5495 lua_pop(L, 1);
5496 }
5497
5498 next_hdr:
5499 lua_pop(L, 1);
5500 }
5501 skip_headers:
5502 lua_pop(L, 1);
5503
5504 /* Update h1m flags: CLEN is set if CHNK is not present */
5505 if (!(h1m.flags & H1_MF_CHNK)) {
5506 const char *clen = ultoa(body_len);
5507
5508 h1m.flags |= H1_MF_CLEN;
5509 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5510 goto fail;
5511 }
5512 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5513 h1m.flags |= H1_MF_XFER_LEN;
5514
5515 /* Update HTX start-line flags */
5516 if (h1m.flags & H1_MF_XFER_ENC)
5517 flags |= HTX_SL_F_XFER_ENC;
5518 if (h1m.flags & H1_MF_XFER_LEN) {
5519 flags |= HTX_SL_F_XFER_LEN;
5520 if (h1m.flags & H1_MF_CHNK)
5521 flags |= HTX_SL_F_CHNK;
5522 else if (h1m.flags & H1_MF_CLEN)
5523 flags |= HTX_SL_F_CLEN;
5524 if (h1m.body_len == 0)
5525 flags |= HTX_SL_F_BODYLESS;
5526 }
5527 sl->flags |= flags;
5528
5529
5530 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5531 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5532 !htx_add_endof(htx, HTX_BLK_EOM))
5533 goto fail;
5534
5535 /* Now, forward the response and terminate the transaction */
5536 s->txn->status = code;
5537 htx_to_buf(htx, &s->res.buf);
5538 if (!http_forward_proxy_resp(s, 1))
5539 goto fail;
5540
5541 return 1;
5542
5543 fail:
5544 channel_htx_truncate(&s->res, htx);
5545 return 0;
5546}
5547
5548/* Terminate a transaction if called from a lua action. For TCP streams,
5549 * processing is just aborted. Nothing is returned to the client and all
5550 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5551 * is forwarded to the client before terminating the transaction. On success,
5552 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5553 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5554 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005555 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005556__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005557{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005558 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005559 struct stream *s;
5560 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005561
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005562 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005563
Christopher Faulet700d9e82020-01-31 12:21:52 +01005564 /* If the flags NOTERM is set, we cannot terminate the session, so we
5565 * just end the execution of the current lua code. */
5566 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005567 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005568
Christopher Faulet700d9e82020-01-31 12:21:52 +01005569 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005570 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005571 struct channel *req = &s->req;
5572 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005573
Christopher Faulet700d9e82020-01-31 12:21:52 +01005574 channel_auto_read(req);
5575 channel_abort(req);
5576 channel_auto_close(req);
5577 channel_erase(req);
5578
5579 res->wex = tick_add_ifset(now_ms, res->wto);
5580 channel_auto_read(res);
5581 channel_auto_close(res);
5582 channel_shutr_now(res);
5583
5584 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5585 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005586 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005587
Christopher Faulet700d9e82020-01-31 12:21:52 +01005588 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5589 /* No reply or invalid reply */
5590 s->txn->status = 0;
5591 http_reply_and_close(s, 0, NULL);
5592 }
5593 else {
5594 /* Remove extra args to have the reply on top of the stack */
5595 if (lua_gettop(L) > 2)
5596 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005597
Christopher Faulet700d9e82020-01-31 12:21:52 +01005598 if (!hlua_txn_forward_reply(L, s)) {
5599 if (!(s->flags & SF_ERR_MASK))
5600 s->flags |= SF_ERR_PRXCOND;
5601 lua_pushinteger(L, ACT_RET_ERR);
5602 WILL_LJMP(hlua_done(L));
5603 return 0; /* Never reached */
5604 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005605 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005606
Christopher Faulet700d9e82020-01-31 12:21:52 +01005607 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5608 if (htxn->dir == SMP_OPT_DIR_REQ) {
5609 /* let's log the request time */
5610 s->logs.tv_request = now;
5611 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5612 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5613 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005614
Christopher Faulet700d9e82020-01-31 12:21:52 +01005615 done:
5616 if (!(s->flags & SF_ERR_MASK))
5617 s->flags |= SF_ERR_LOCAL;
5618 if (!(s->flags & SF_FINST_MASK))
5619 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005620
Christopher Faulet4ad73102020-03-05 11:07:31 +01005621 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005622 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005623 return 0;
5624}
5625
Christopher Faulet700d9e82020-01-31 12:21:52 +01005626/*
5627 *
5628 *
5629 * Class REPLY
5630 *
5631 *
5632 */
5633
5634/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5635 * free slots, the function fails and returns 0;
5636 */
5637static int hlua_txn_reply_new(lua_State *L)
5638{
5639 struct hlua_txn *htxn;
5640 const char *reason, *body = NULL;
5641 int ret, status;
5642
5643 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005644 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005645 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5646 WILL_LJMP(lua_error(L));
5647 }
5648
5649 /* Default value */
5650 status = 200;
5651 reason = http_get_reason(status);
5652
5653 if (lua_istable(L, 2)) {
5654 /* load status and reason from the table argument at index 2 */
5655 ret = lua_getfield(L, 2, "status");
5656 if (ret == LUA_TNIL)
5657 goto reason;
5658 else if (ret != LUA_TNUMBER) {
5659 /* invalid status: ignore the reason */
5660 goto body;
5661 }
5662 status = lua_tointeger(L, -1);
5663
5664 reason:
5665 lua_pop(L, 1); /* restore the stack: remove status */
5666 ret = lua_getfield(L, 2, "reason");
5667 if (ret == LUA_TSTRING)
5668 reason = lua_tostring(L, -1);
5669
5670 body:
5671 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5672 ret = lua_getfield(L, 2, "body");
5673 if (ret == LUA_TSTRING)
5674 body = lua_tostring(L, -1);
5675 lua_pop(L, 1); /* restore the stack: remove body */
5676 }
5677
5678 /* Create the Reply table */
5679 lua_newtable(L);
5680
5681 /* Add status element */
5682 lua_pushstring(L, "status");
5683 lua_pushinteger(L, status);
5684 lua_settable(L, -3);
5685
5686 /* Add reason element */
5687 reason = http_get_reason(status);
5688 lua_pushstring(L, "reason");
5689 lua_pushstring(L, reason);
5690 lua_settable(L, -3);
5691
5692 /* Add body element, nil if undefined */
5693 lua_pushstring(L, "body");
5694 if (body)
5695 lua_pushstring(L, body);
5696 else
5697 lua_pushnil(L);
5698 lua_settable(L, -3);
5699
5700 /* Add headers element */
5701 lua_pushstring(L, "headers");
5702 lua_newtable(L);
5703
5704 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5705 if (lua_istable(L, 2)) {
5706 /* load headers from the table argument at index 2. If it is a table, copy it. */
5707 ret = lua_getfield(L, 2, "headers");
5708 if (ret == LUA_TTABLE) {
5709 /* stack: [ ... <headers:table>, <table> ] */
5710 lua_pushnil(L);
5711 while (lua_next(L, -2) != 0) {
5712 /* stack: [ ... <headers:table>, <table>, k, v] */
5713 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5714 /* invalid value type, skip it */
5715 lua_pop(L, 1);
5716 continue;
5717 }
5718
5719
5720 /* Duplicate the key and swap it with the value. */
5721 lua_pushvalue(L, -2);
5722 lua_insert(L, -2);
5723 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5724
5725 lua_newtable(L);
5726 lua_insert(L, -2);
5727 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5728
5729 if (lua_isstring(L, -1)) {
5730 /* push the value in the inner table */
5731 lua_rawseti(L, -2, 1);
5732 }
5733 else { /* table */
5734 lua_pushnil(L);
5735 while (lua_next(L, -2) != 0) {
5736 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5737 if (!lua_isstring(L, -1)) {
5738 /* invalid value type, skip it*/
5739 lua_pop(L, 1);
5740 continue;
5741 }
5742 /* push the value in the inner table */
5743 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5744 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5745 }
5746 lua_pop(L, 1);
5747 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5748 }
5749
5750 /* push (k,v) on the stack in the headers table:
5751 * stack: [ ... <headers:table>, <table>, k, k, v ]
5752 */
5753 lua_settable(L, -5);
5754 /* stack: [ ... <headers:table>, <table>, k ] */
5755 }
5756 }
5757 lua_pop(L, 1);
5758 }
5759 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5760 lua_settable(L, -3);
5761 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5762
5763 /* Pop a class sesison metatable and affect it to the userdata. */
5764 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5765 lua_setmetatable(L, -2);
5766 return 1;
5767}
5768
5769/* Set the reply status code, and optionally the reason. If no reason is
5770 * provided, the default one corresponding to the status code is used.
5771 */
5772__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5773{
5774 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5775 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5776
5777 /* First argument (self) must be a table */
5778 luaL_checktype(L, 1, LUA_TTABLE);
5779
5780 if (status < 100 || status > 599) {
5781 lua_pushboolean(L, 0);
5782 return 1;
5783 }
5784 if (!reason)
5785 reason = http_get_reason(status);
5786
5787 lua_pushinteger(L, status);
5788 lua_setfield(L, 1, "status");
5789
5790 lua_pushstring(L, reason);
5791 lua_setfield(L, 1, "reason");
5792
5793 lua_pushboolean(L, 1);
5794 return 1;
5795}
5796
5797/* Add a header into the reply object. Each header name is associated to an
5798 * array of values in the "headers" table. If the header name is not found, a
5799 * new entry is created.
5800 */
5801__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5802{
5803 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5804 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5805 int ret;
5806
5807 /* First argument (self) must be a table */
5808 luaL_checktype(L, 1, LUA_TTABLE);
5809
5810 /* Push in the stack the "headers" entry. */
5811 ret = lua_getfield(L, 1, "headers");
5812 if (ret != LUA_TTABLE) {
5813 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5814 WILL_LJMP(lua_error(L));
5815 }
5816
5817 /* check if the header is already registered. If not, register it. */
5818 ret = lua_getfield(L, -1, name);
5819 if (ret == LUA_TNIL) {
5820 /* Entry not found. */
5821 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5822
5823 /* Insert the new header name in the array in the top of the stack.
5824 * It left the new array in the top of the stack.
5825 */
5826 lua_newtable(L);
5827 lua_pushstring(L, name);
5828 lua_pushvalue(L, -2);
5829 lua_settable(L, -4);
5830 }
5831 else if (ret != LUA_TTABLE) {
5832 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5833 WILL_LJMP(lua_error(L));
5834 }
5835
5836 /* Now the top od thestack is an array of values. We push
5837 * the header value as new entry.
5838 */
5839 lua_pushstring(L, value);
5840 ret = lua_rawlen(L, -2);
5841 lua_rawseti(L, -2, ret + 1);
5842
5843 lua_pushboolean(L, 1);
5844 return 1;
5845}
5846
5847/* Remove all occurrences of a given header name. */
5848__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5849{
5850 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5851 int ret;
5852
5853 /* First argument (self) must be a table */
5854 luaL_checktype(L, 1, LUA_TTABLE);
5855
5856 /* Push in the stack the "headers" entry. */
5857 ret = lua_getfield(L, 1, "headers");
5858 if (ret != LUA_TTABLE) {
5859 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5860 WILL_LJMP(lua_error(L));
5861 }
5862
5863 lua_pushstring(L, name);
5864 lua_pushnil(L);
5865 lua_settable(L, -3);
5866
5867 lua_pushboolean(L, 1);
5868 return 1;
5869}
5870
5871/* Set the reply's body. Overwrite any existing entry. */
5872__LJMP static int hlua_txn_reply_set_body(lua_State *L)
5873{
5874 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
5875
5876 /* First argument (self) must be a table */
5877 luaL_checktype(L, 1, LUA_TTABLE);
5878
5879 lua_pushstring(L, payload);
5880 lua_setfield(L, 1, "body");
5881
5882 lua_pushboolean(L, 1);
5883 return 1;
5884}
5885
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005886__LJMP static int hlua_log(lua_State *L)
5887{
5888 int level;
5889 const char *msg;
5890
5891 MAY_LJMP(check_args(L, 2, "log"));
5892 level = MAY_LJMP(luaL_checkinteger(L, 1));
5893 msg = MAY_LJMP(luaL_checkstring(L, 2));
5894
5895 if (level < 0 || level >= NB_LOG_LEVELS)
5896 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5897
5898 hlua_sendlog(NULL, level, msg);
5899 return 0;
5900}
5901
5902__LJMP static int hlua_log_debug(lua_State *L)
5903{
5904 const char *msg;
5905
5906 MAY_LJMP(check_args(L, 1, "debug"));
5907 msg = MAY_LJMP(luaL_checkstring(L, 1));
5908 hlua_sendlog(NULL, LOG_DEBUG, msg);
5909 return 0;
5910}
5911
5912__LJMP static int hlua_log_info(lua_State *L)
5913{
5914 const char *msg;
5915
5916 MAY_LJMP(check_args(L, 1, "info"));
5917 msg = MAY_LJMP(luaL_checkstring(L, 1));
5918 hlua_sendlog(NULL, LOG_INFO, msg);
5919 return 0;
5920}
5921
5922__LJMP static int hlua_log_warning(lua_State *L)
5923{
5924 const char *msg;
5925
5926 MAY_LJMP(check_args(L, 1, "warning"));
5927 msg = MAY_LJMP(luaL_checkstring(L, 1));
5928 hlua_sendlog(NULL, LOG_WARNING, msg);
5929 return 0;
5930}
5931
5932__LJMP static int hlua_log_alert(lua_State *L)
5933{
5934 const char *msg;
5935
5936 MAY_LJMP(check_args(L, 1, "alert"));
5937 msg = MAY_LJMP(luaL_checkstring(L, 1));
5938 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005939 return 0;
5940}
5941
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005942__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005943{
5944 int wakeup_ms = lua_tointeger(L, -1);
5945 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005946 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005947 return 0;
5948}
5949
5950__LJMP static int hlua_sleep(lua_State *L)
5951{
5952 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005953 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005954
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005955 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005956
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005957 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005958 wakeup_ms = tick_add(now_ms, delay);
5959 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005960
Willy Tarreau9635e032018-10-16 17:52:55 +02005961 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005962 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005963}
5964
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005965__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005966{
5967 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005968 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005969
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005970 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005971
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005972 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005973 wakeup_ms = tick_add(now_ms, delay);
5974 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005975
Willy Tarreau9635e032018-10-16 17:52:55 +02005976 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005977 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005978}
5979
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005980/* This functionis an LUA binding. it permits to give back
5981 * the hand at the HAProxy scheduler. It is used when the
5982 * LUA processing consumes a lot of time.
5983 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005984__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005985{
5986 return 0;
5987}
5988
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005989__LJMP static int hlua_yield(lua_State *L)
5990{
Willy Tarreau9635e032018-10-16 17:52:55 +02005991 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005992 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005993}
5994
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005995/* This function change the nice of the currently executed
5996 * task. It is used set low or high priority at the current
5997 * task.
5998 */
Willy Tarreau59551662015-03-10 14:23:13 +01005999__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006000{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006001 struct hlua *hlua;
6002 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006003
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006004 MAY_LJMP(check_args(L, 1, "set_nice"));
6005 hlua = hlua_gethlua(L);
6006 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006007
6008 /* If he task is not set, I'm in a start mode. */
6009 if (!hlua || !hlua->task)
6010 return 0;
6011
6012 if (nice < -1024)
6013 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006014 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006015 nice = 1024;
6016
6017 hlua->task->nice = nice;
6018 return 0;
6019}
6020
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006021/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006022 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6023 * return an E_AGAIN signal, the emmiter of this signal must set a
6024 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006025 *
6026 * Task wrapper are longjmp safe because the only one Lua code
6027 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006028 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006029struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006030{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006031 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006032 enum hlua_exec status;
6033
Christopher Faulet5bc99722018-04-25 10:34:45 +02006034 if (task->thread_mask == MAX_THREADS_MASK)
6035 task_set_affinity(task, tid_bit);
6036
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006037 /* If it is the first call to the task, we must initialize the
6038 * execution timeouts.
6039 */
6040 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006041 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006042
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006043 /* Execute the Lua code. */
6044 status = hlua_ctx_resume(hlua, 1);
6045
6046 switch (status) {
6047 /* finished or yield */
6048 case HLUA_E_OK:
6049 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006050 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006051 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006052 break;
6053
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006054 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006055 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006056 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006057 break;
6058
6059 /* finished with error. */
6060 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006061 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006062 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006063 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006064 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006065 break;
6066
6067 case HLUA_E_ERR:
6068 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006069 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006070 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006071 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006072 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006073 break;
6074 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006075 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006076}
6077
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006078/* This function is an LUA binding that register LUA function to be
6079 * executed after the HAProxy configuration parsing and before the
6080 * HAProxy scheduler starts. This function expect only one LUA
6081 * argument that is a function. This function returns nothing, but
6082 * throws if an error is encountered.
6083 */
6084__LJMP static int hlua_register_init(lua_State *L)
6085{
6086 struct hlua_init_function *init;
6087 int ref;
6088
6089 MAY_LJMP(check_args(L, 1, "register_init"));
6090
6091 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6092
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006093 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006094 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006095 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006096
6097 init->function_ref = ref;
6098 LIST_ADDQ(&hlua_init_functions, &init->l);
6099 return 0;
6100}
6101
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006102/* This functio is an LUA binding. It permits to register a task
6103 * executed in parallel of the main HAroxy activity. The task is
6104 * created and it is set in the HAProxy scheduler. It can be called
6105 * from the "init" section, "post init" or during the runtime.
6106 *
6107 * Lua prototype:
6108 *
6109 * <none> core.register_task(<function>)
6110 */
6111static int hlua_register_task(lua_State *L)
6112{
6113 struct hlua *hlua;
6114 struct task *task;
6115 int ref;
6116
6117 MAY_LJMP(check_args(L, 1, "register_task"));
6118
6119 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6120
Willy Tarreaubafbe012017-11-24 17:34:44 +01006121 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006122 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006123 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006124
Emeric Brunc60def82017-09-27 14:59:38 +02006125 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006126 if (!task)
6127 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6128
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006129 task->context = hlua;
6130 task->process = hlua_process_task;
6131
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006132 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006133 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006134
6135 /* Restore the function in the stack. */
6136 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6137 hlua->nargs = 0;
6138
6139 /* Schedule task. */
6140 task_schedule(task, now_ms);
6141
6142 return 0;
6143}
6144
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006145/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6146 * doesn't allow "yield" functions because the HAProxy engine cannot
6147 * resume converters.
6148 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006149static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006150{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006151 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006152 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006153 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006154
Willy Tarreaube508f12016-03-10 11:47:01 +01006155 if (!stream)
6156 return 0;
6157
Willy Tarreau87b09662015-04-03 00:22:06 +02006158 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006159 * Lua context can be not initialized. This behavior
6160 * permits to save performances because a systematic
6161 * Lua initialization cause 5% performances loss.
6162 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006163 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006164 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006165 if (!stream->hlua) {
6166 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6167 return 0;
6168 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006169 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006170 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6171 return 0;
6172 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006173 }
6174
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006175 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006176 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006177
6178 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006179 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6180 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6181 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006182 else
6183 error = "critical error";
6184 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006185 return 0;
6186 }
6187
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006188 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006189 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006190 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006191 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006192 return 0;
6193 }
6194
6195 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006196 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006197
6198 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006199 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006200 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006201 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006202 return 0;
6203 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006204 hlua_smp2lua(stream->hlua->T, smp);
6205 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006206
6207 /* push keywords in the stack. */
6208 if (arg_p) {
6209 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006210 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006211 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006212 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006213 return 0;
6214 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006215 hlua_arg2lua(stream->hlua->T, arg_p);
6216 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006217 }
6218 }
6219
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006220 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006221 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006222
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006223 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006224 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006225 }
6226
6227 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006228 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006229 /* finished. */
6230 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006231 /* If the stack is empty, the function fails. */
6232 if (lua_gettop(stream->hlua->T) <= 0)
6233 return 0;
6234
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006235 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006236 hlua_lua2smp(stream->hlua->T, -1, smp);
6237 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006238 return 1;
6239
6240 /* yield. */
6241 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006242 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006243 return 0;
6244
6245 /* finished with error. */
6246 case HLUA_E_ERRMSG:
6247 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006248 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006249 fcn->name, lua_tostring(stream->hlua->T, -1));
6250 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006251 return 0;
6252
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006253 case HLUA_E_ETMOUT:
6254 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6255 return 0;
6256
6257 case HLUA_E_NOMEM:
6258 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6259 return 0;
6260
6261 case HLUA_E_YIELD:
6262 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6263 return 0;
6264
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006265 case HLUA_E_ERR:
6266 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006267 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006268 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006269
6270 default:
6271 return 0;
6272 }
6273}
6274
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006275/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6276 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006277 * resume sample-fetches. This function will be called by the sample
6278 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006279 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006280static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6281 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006282{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006283 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006284 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006285 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006286 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006287
Willy Tarreaube508f12016-03-10 11:47:01 +01006288 if (!stream)
6289 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006290
Willy Tarreau87b09662015-04-03 00:22:06 +02006291 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006292 * Lua context can be not initialized. This behavior
6293 * permits to save performances because a systematic
6294 * Lua initialization cause 5% performances loss.
6295 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006296 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006297 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006298 if (!stream->hlua) {
6299 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6300 return 0;
6301 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006302 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006303 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6304 return 0;
6305 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006306 }
6307
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006308 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006309 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006310
6311 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006312 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6313 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6314 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006315 else
6316 error = "critical error";
6317 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006318 return 0;
6319 }
6320
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006321 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006322 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006323 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006324 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006325 return 0;
6326 }
6327
6328 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006329 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006330
6331 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006332 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006333 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006334 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006335 return 0;
6336 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006337 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006338
6339 /* push keywords in the stack. */
6340 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6341 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006342 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006343 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006344 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006345 return 0;
6346 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006347 hlua_arg2lua(stream->hlua->T, arg_p);
6348 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006349 }
6350
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006351 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006352 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006353
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006354 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006355 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006356 }
6357
6358 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006359 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006360 /* finished. */
6361 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006362 /* If the stack is empty, the function fails. */
6363 if (lua_gettop(stream->hlua->T) <= 0)
6364 return 0;
6365
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006366 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006367 hlua_lua2smp(stream->hlua->T, -1, smp);
6368 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006369
6370 /* Set the end of execution flag. */
6371 smp->flags &= ~SMP_F_MAY_CHANGE;
6372 return 1;
6373
6374 /* yield. */
6375 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006376 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006377 return 0;
6378
6379 /* finished with error. */
6380 case HLUA_E_ERRMSG:
6381 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006382 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006383 fcn->name, lua_tostring(stream->hlua->T, -1));
6384 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006385 return 0;
6386
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006387 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006388 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6389 return 0;
6390
6391 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006392 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6393 return 0;
6394
6395 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006396 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6397 return 0;
6398
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006399 case HLUA_E_ERR:
6400 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006401 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006402 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006403
6404 default:
6405 return 0;
6406 }
6407}
6408
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006409/* This function is an LUA binding used for registering
6410 * "sample-conv" functions. It expects a converter name used
6411 * in the haproxy configuration file, and an LUA function.
6412 */
6413__LJMP static int hlua_register_converters(lua_State *L)
6414{
6415 struct sample_conv_kw_list *sck;
6416 const char *name;
6417 int ref;
6418 int len;
6419 struct hlua_function *fcn;
6420
6421 MAY_LJMP(check_args(L, 2, "register_converters"));
6422
6423 /* First argument : converter name. */
6424 name = MAY_LJMP(luaL_checkstring(L, 1));
6425
6426 /* Second argument : lua function. */
6427 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6428
6429 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006430 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006431 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006432 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006433 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006434 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006435 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006436
6437 /* Fill fcn. */
6438 fcn->name = strdup(name);
6439 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006440 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006441 fcn->function_ref = ref;
6442
6443 /* List head */
6444 sck->list.n = sck->list.p = NULL;
6445
6446 /* converter keyword. */
6447 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006448 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006449 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006450 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006451
6452 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6453 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006454 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 +01006455 sck->kw[0].val_args = NULL;
6456 sck->kw[0].in_type = SMP_T_STR;
6457 sck->kw[0].out_type = SMP_T_STR;
6458 sck->kw[0].private = fcn;
6459
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006460 /* Register this new converter */
6461 sample_register_convs(sck);
6462
6463 return 0;
6464}
6465
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006466/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006467 * "sample-fetch" functions. It expects a converter name used
6468 * in the haproxy configuration file, and an LUA function.
6469 */
6470__LJMP static int hlua_register_fetches(lua_State *L)
6471{
6472 const char *name;
6473 int ref;
6474 int len;
6475 struct sample_fetch_kw_list *sfk;
6476 struct hlua_function *fcn;
6477
6478 MAY_LJMP(check_args(L, 2, "register_fetches"));
6479
6480 /* First argument : sample-fetch name. */
6481 name = MAY_LJMP(luaL_checkstring(L, 1));
6482
6483 /* Second argument : lua function. */
6484 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6485
6486 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006487 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006488 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006489 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006490 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006491 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006492 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006493
6494 /* Fill fcn. */
6495 fcn->name = strdup(name);
6496 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006497 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006498 fcn->function_ref = ref;
6499
6500 /* List head */
6501 sfk->list.n = sfk->list.p = NULL;
6502
6503 /* sample-fetch keyword. */
6504 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006505 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006506 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006507 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006508
6509 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6510 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006511 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 +01006512 sfk->kw[0].val_args = NULL;
6513 sfk->kw[0].out_type = SMP_T_STR;
6514 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6515 sfk->kw[0].val = 0;
6516 sfk->kw[0].private = fcn;
6517
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006518 /* Register this new fetch. */
6519 sample_register_fetches(sfk);
6520
6521 return 0;
6522}
6523
Christopher Faulet501465d2020-02-26 14:54:16 +01006524/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006525 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006526__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006527{
6528 struct hlua *hlua = hlua_gethlua(L);
6529 unsigned int delay;
6530 unsigned int wakeup_ms;
6531
6532 MAY_LJMP(check_args(L, 1, "wake_time"));
6533
6534 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6535 wakeup_ms = tick_add(now_ms, delay);
6536 hlua->wake_time = wakeup_ms;
6537 return 0;
6538}
6539
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006540/* This function is a wrapper to execute each LUA function declared as an action
6541 * wrapper during the initialisation period. This function may return any
6542 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6543 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6544 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006545 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006546static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006547 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006548{
6549 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006550 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006551 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006552 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006553
6554 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006555 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6556 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6557 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6558 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006559 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006560 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006561 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006562 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006563
Willy Tarreau87b09662015-04-03 00:22:06 +02006564 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006565 * Lua context can be not initialized. This behavior
6566 * permits to save performances because a systematic
6567 * Lua initialization cause 5% performances loss.
6568 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006569 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006570 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006571 if (!s->hlua) {
6572 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6573 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006574 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006575 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006576 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006577 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6578 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006579 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006580 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006581 }
6582
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006583 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006584 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006585
6586 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006587 if (!SET_SAFE_LJMP(s->hlua->T)) {
6588 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6589 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006590 else
6591 error = "critical error";
6592 SEND_ERR(px, "Lua function '%s': %s.\n",
6593 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006594 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006595 }
6596
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006597 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006598 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006599 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006600 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006601 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006602 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006603 }
6604
6605 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006606 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006607
Willy Tarreau87b09662015-04-03 00:22:06 +02006608 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006609 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006610 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006611 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006612 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006613 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006614 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006615 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006616
6617 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006618 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006619 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006620 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006621 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006622 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006623 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006624 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006625 lua_pushstring(s->hlua->T, *arg);
6626 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006627 }
6628
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006629 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006630 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006631
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006632 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006633 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006634 }
6635
6636 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006637 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006638 /* finished. */
6639 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006640 /* Catch the return value */
6641 if (lua_gettop(s->hlua->T) > 0)
6642 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006643
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006644 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006645 if (act_ret == ACT_RET_YIELD) {
6646 if (flags & ACT_OPT_FINAL)
6647 goto err_yield;
6648
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006649 if (dir == SMP_OPT_DIR_REQ)
6650 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6651 s->hlua->wake_time);
6652 else
6653 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6654 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006655 }
6656 goto end;
6657
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006658 /* yield. */
6659 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006660 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006661 if (dir == SMP_OPT_DIR_REQ)
6662 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6663 s->hlua->wake_time);
6664 else
6665 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6666 s->hlua->wake_time);
6667
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006668 /* Some actions can be wake up when a "write" event
6669 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006670 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006671 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006672 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006673 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006674 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006675 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006676 act_ret = ACT_RET_YIELD;
6677 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006678
6679 /* finished with error. */
6680 case HLUA_E_ERRMSG:
6681 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006682 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006683 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6684 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006685 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006686
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006687 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006688 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006689 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006690
6691 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006692 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006693 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006694
6695 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02006696 err_yield:
6697 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006698 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6699 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006700 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006701
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006702 case HLUA_E_ERR:
6703 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006704 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006705 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006706
6707 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006708 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006709 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006710
6711 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02006712 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006713 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006714 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006715}
6716
Olivier Houchard9f6af332018-05-25 14:04:04 +02006717struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006718{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006719 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006720
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006721 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006722 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006723 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006724}
6725
6726static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6727{
6728 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006729 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006730 struct task *task;
6731 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006732 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006733
Willy Tarreaubafbe012017-11-24 17:34:44 +01006734 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006735 if (!hlua) {
6736 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6737 ctx->rule->arg.hlua_rule->fcn.name);
6738 return 0;
6739 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006740 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006741 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006742 ctx->ctx.hlua_apptcp.flags = 0;
6743
6744 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006745 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006746 if (!task) {
6747 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6748 ctx->rule->arg.hlua_rule->fcn.name);
6749 return 0;
6750 }
6751 task->nice = 0;
6752 task->context = ctx;
6753 task->process = hlua_applet_wakeup;
6754 ctx->ctx.hlua_apptcp.task = task;
6755
6756 /* In the execution wrappers linked with a stream, the
6757 * Lua context can be not initialized. This behavior
6758 * permits to save performances because a systematic
6759 * Lua initialization cause 5% performances loss.
6760 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006761 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006762 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6763 ctx->rule->arg.hlua_rule->fcn.name);
6764 return 0;
6765 }
6766
6767 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006768 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006769
6770 /* The following Lua calls can fail. */
6771 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006772 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6773 error = lua_tostring(hlua->T, -1);
6774 else
6775 error = "critical error";
6776 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6777 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006778 return 0;
6779 }
6780
6781 /* Check stack available size. */
6782 if (!lua_checkstack(hlua->T, 1)) {
6783 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6784 ctx->rule->arg.hlua_rule->fcn.name);
6785 RESET_SAFE_LJMP(hlua->T);
6786 return 0;
6787 }
6788
6789 /* Restore the function in the stack. */
6790 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6791
6792 /* Create and and push object stream in the stack. */
6793 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6794 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6795 ctx->rule->arg.hlua_rule->fcn.name);
6796 RESET_SAFE_LJMP(hlua->T);
6797 return 0;
6798 }
6799 hlua->nargs = 1;
6800
6801 /* push keywords in the stack. */
6802 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6803 if (!lua_checkstack(hlua->T, 1)) {
6804 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6805 ctx->rule->arg.hlua_rule->fcn.name);
6806 RESET_SAFE_LJMP(hlua->T);
6807 return 0;
6808 }
6809 lua_pushstring(hlua->T, *arg);
6810 hlua->nargs++;
6811 }
6812
6813 RESET_SAFE_LJMP(hlua->T);
6814
6815 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006816 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006817 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006818
6819 return 1;
6820}
6821
Willy Tarreau60409db2019-08-21 14:14:50 +02006822void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006823{
6824 struct stream_interface *si = ctx->owner;
6825 struct stream *strm = si_strm(si);
6826 struct channel *res = si_ic(si);
6827 struct act_rule *rule = ctx->rule;
6828 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006829 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006830
6831 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006832 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6833 /* eat the whole request */
6834 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006835 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006836 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006837
6838 /* If the stream is disconnect or closed, ldo nothing. */
6839 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6840 return;
6841
6842 /* Execute the function. */
6843 switch (hlua_ctx_resume(hlua, 1)) {
6844 /* finished. */
6845 case HLUA_E_OK:
6846 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6847
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006848 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006849 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006850 res->flags |= CF_READ_NULL;
6851 si_shutr(si);
6852 return;
6853
6854 /* yield. */
6855 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006856 if (hlua->wake_time != TICK_ETERNITY)
6857 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006858 return;
6859
6860 /* finished with error. */
6861 case HLUA_E_ERRMSG:
6862 /* Display log. */
6863 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6864 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6865 lua_pop(hlua->T, 1);
6866 goto error;
6867
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006868 case HLUA_E_ETMOUT:
6869 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6870 rule->arg.hlua_rule->fcn.name);
6871 goto error;
6872
6873 case HLUA_E_NOMEM:
6874 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6875 rule->arg.hlua_rule->fcn.name);
6876 goto error;
6877
6878 case HLUA_E_YIELD: /* unexpected */
6879 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6880 rule->arg.hlua_rule->fcn.name);
6881 goto error;
6882
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006883 case HLUA_E_ERR:
6884 /* Display log. */
6885 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6886 rule->arg.hlua_rule->fcn.name);
6887 goto error;
6888
6889 default:
6890 goto error;
6891 }
6892
6893error:
6894
6895 /* For all other cases, just close the stream. */
6896 si_shutw(si);
6897 si_shutr(si);
6898 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6899}
6900
6901static void hlua_applet_tcp_release(struct appctx *ctx)
6902{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006903 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006904 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006905 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006906 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006907}
6908
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006909/* The function returns 1 if the initialisation is complete, 0 if
6910 * an errors occurs and -1 if more data are required for initializing
6911 * the applet.
6912 */
6913static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6914{
6915 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006916 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006917 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006918 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006919 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006920 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006921
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006922 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006923 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006924 if (!hlua) {
6925 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6926 ctx->rule->arg.hlua_rule->fcn.name);
6927 return 0;
6928 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006929 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006930 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006931 ctx->ctx.hlua_apphttp.left_bytes = -1;
6932 ctx->ctx.hlua_apphttp.flags = 0;
6933
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006934 if (txn->req.flags & HTTP_MSGF_VER_11)
6935 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6936
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006937 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006938 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006939 if (!task) {
6940 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6941 ctx->rule->arg.hlua_rule->fcn.name);
6942 return 0;
6943 }
6944 task->nice = 0;
6945 task->context = ctx;
6946 task->process = hlua_applet_wakeup;
6947 ctx->ctx.hlua_apphttp.task = task;
6948
6949 /* In the execution wrappers linked with a stream, the
6950 * Lua context can be not initialized. This behavior
6951 * permits to save performances because a systematic
6952 * Lua initialization cause 5% performances loss.
6953 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006954 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006955 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6956 ctx->rule->arg.hlua_rule->fcn.name);
6957 return 0;
6958 }
6959
6960 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006961 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006962
6963 /* The following Lua calls can fail. */
6964 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006965 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6966 error = lua_tostring(hlua->T, -1);
6967 else
6968 error = "critical error";
6969 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6970 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006971 return 0;
6972 }
6973
6974 /* Check stack available size. */
6975 if (!lua_checkstack(hlua->T, 1)) {
6976 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6977 ctx->rule->arg.hlua_rule->fcn.name);
6978 RESET_SAFE_LJMP(hlua->T);
6979 return 0;
6980 }
6981
6982 /* Restore the function in the stack. */
6983 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6984
6985 /* Create and and push object stream in the stack. */
6986 if (!hlua_applet_http_new(hlua->T, ctx)) {
6987 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6988 ctx->rule->arg.hlua_rule->fcn.name);
6989 RESET_SAFE_LJMP(hlua->T);
6990 return 0;
6991 }
6992 hlua->nargs = 1;
6993
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006994 /* push keywords in the stack. */
6995 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6996 if (!lua_checkstack(hlua->T, 1)) {
6997 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6998 ctx->rule->arg.hlua_rule->fcn.name);
6999 RESET_SAFE_LJMP(hlua->T);
7000 return 0;
7001 }
7002 lua_pushstring(hlua->T, *arg);
7003 hlua->nargs++;
7004 }
7005
7006 RESET_SAFE_LJMP(hlua->T);
7007
7008 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007009 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007010
7011 return 1;
7012}
7013
Willy Tarreau60409db2019-08-21 14:14:50 +02007014void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007015{
7016 struct stream_interface *si = ctx->owner;
7017 struct stream *strm = si_strm(si);
7018 struct channel *req = si_oc(si);
7019 struct channel *res = si_ic(si);
7020 struct act_rule *rule = ctx->rule;
7021 struct proxy *px = strm->be;
7022 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7023 struct htx *req_htx, *res_htx;
7024
7025 res_htx = htx_from_buf(&res->buf);
7026
7027 /* If the stream is disconnect or closed, ldo nothing. */
7028 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7029 goto out;
7030
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007031 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007032 if (!b_size(&res->buf)) {
7033 si_rx_room_blk(si);
7034 goto out;
7035 }
7036 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007037 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007038 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7039
7040 /* Set the currently running flag. */
7041 if (!HLUA_IS_RUNNING(hlua) &&
7042 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7043 struct htx_blk *blk;
7044 size_t count = co_data(req);
7045
7046 if (!count) {
7047 si_cant_get(si);
7048 goto out;
7049 }
7050
7051 /* We need to flush the request header. This left the body for
7052 * the Lua.
7053 */
7054 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007055 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007056 while (count && blk) {
7057 enum htx_blk_type type = htx_get_blk_type(blk);
7058 uint32_t sz = htx_get_blksz(blk);
7059
7060 if (sz > count) {
7061 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007062 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007063 goto out;
7064 }
7065
7066 count -= sz;
7067 co_set_data(req, co_data(req) - sz);
7068 blk = htx_remove_blk(req_htx, blk);
7069
7070 if (type == HTX_BLK_EOH)
7071 break;
7072 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007073 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007074 }
7075
7076 /* Executes The applet if it is not done. */
7077 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7078
7079 /* Execute the function. */
7080 switch (hlua_ctx_resume(hlua, 1)) {
7081 /* finished. */
7082 case HLUA_E_OK:
7083 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7084 break;
7085
7086 /* yield. */
7087 case HLUA_E_AGAIN:
7088 if (hlua->wake_time != TICK_ETERNITY)
7089 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007090 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007091
7092 /* finished with error. */
7093 case HLUA_E_ERRMSG:
7094 /* Display log. */
7095 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7096 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7097 lua_pop(hlua->T, 1);
7098 goto error;
7099
7100 case HLUA_E_ETMOUT:
7101 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7102 rule->arg.hlua_rule->fcn.name);
7103 goto error;
7104
7105 case HLUA_E_NOMEM:
7106 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7107 rule->arg.hlua_rule->fcn.name);
7108 goto error;
7109
7110 case HLUA_E_YIELD: /* unexpected */
7111 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7112 rule->arg.hlua_rule->fcn.name);
7113 goto error;
7114
7115 case HLUA_E_ERR:
7116 /* Display log. */
7117 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7118 rule->arg.hlua_rule->fcn.name);
7119 goto error;
7120
7121 default:
7122 goto error;
7123 }
7124 }
7125
7126 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007127 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7128 goto done;
7129
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007130 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7131 goto error;
7132
Christopher Faulet54b5e212019-06-04 10:08:28 +02007133 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02007134 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007135 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7136 si_rx_room_blk(si);
7137 goto out;
7138 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007139 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007140 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7141 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007142 }
7143
7144 done:
7145 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007146 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007147 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007148 si_shutr(si);
7149 }
7150
7151 /* eat the whole request */
7152 if (co_data(req)) {
7153 req_htx = htx_from_buf(&req->buf);
7154 co_htx_skip(req, req_htx, co_data(req));
7155 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007156 }
7157 }
7158
7159 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007160 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007161 return;
7162
7163 error:
7164
7165 /* If we are in HTTP mode, and we are not send any
7166 * data, return a 500 server error in best effort:
7167 * if there is no room available in the buffer,
7168 * just close the connection.
7169 */
7170 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007171 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007172
7173 channel_erase(res);
7174 res->buf.data = b_data(err);
7175 memcpy(res->buf.area, b_head(err), b_data(err));
7176 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007177 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007178 }
7179 if (!(strm->flags & SF_ERR_MASK))
7180 strm->flags |= SF_ERR_RESOURCE;
7181 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7182 goto done;
7183}
7184
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007185static void hlua_applet_http_release(struct appctx *ctx)
7186{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007187 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007188 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007189 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007190 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007191}
7192
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007193/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007194 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007195 *
7196 * This function can fail with an abort() due to an Lua critical error.
7197 * We are in the configuration parsing process of HAProxy, this abort() is
7198 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007199 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007200static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7201 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007202{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007203 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007204 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007205
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007206 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007207 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007208 if (!rule->arg.hlua_rule) {
7209 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007210 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007211 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007212
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007213 /* Memory for arguments. */
7214 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7215 if (!rule->arg.hlua_rule->args) {
7216 memprintf(err, "out of memory error");
7217 return ACT_RET_PRS_ERR;
7218 }
7219
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007220 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007221 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007222
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007223 /* Expect some arguments */
7224 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007225 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007226 memprintf(err, "expect %d arguments", fcn->nargs);
7227 return ACT_RET_PRS_ERR;
7228 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007229 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007230 if (!rule->arg.hlua_rule->args[i]) {
7231 memprintf(err, "out of memory error");
7232 return ACT_RET_PRS_ERR;
7233 }
7234 (*cur_arg)++;
7235 }
7236 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007237
Thierry FOURNIER42148732015-09-02 17:17:33 +02007238 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007239 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007240 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007241}
7242
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007243static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7244 struct act_rule *rule, char **err)
7245{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007246 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007247
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007248 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007249 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007250 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007251 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007252 * the call of this analyzer.
7253 */
7254 if (rule->from != ACT_F_HTTP_REQ) {
7255 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7256 return ACT_RET_PRS_ERR;
7257 }
7258
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007259 /* Memory for the rule. */
7260 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7261 if (!rule->arg.hlua_rule) {
7262 memprintf(err, "out of memory error");
7263 return ACT_RET_PRS_ERR;
7264 }
7265
7266 /* Reference the Lua function and store the reference. */
7267 rule->arg.hlua_rule->fcn = *fcn;
7268
7269 /* TODO: later accept arguments. */
7270 rule->arg.hlua_rule->args = NULL;
7271
7272 /* Add applet pointer in the rule. */
7273 rule->applet.obj_type = OBJ_TYPE_APPLET;
7274 rule->applet.name = fcn->name;
7275 rule->applet.init = hlua_applet_http_init;
7276 rule->applet.fct = hlua_applet_http_fct;
7277 rule->applet.release = hlua_applet_http_release;
7278 rule->applet.timeout = hlua_timeout_applet;
7279
7280 return ACT_RET_PRS_OK;
7281}
7282
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007283/* This function is an LUA binding used for registering
7284 * "sample-conv" functions. It expects a converter name used
7285 * in the haproxy configuration file, and an LUA function.
7286 */
7287__LJMP static int hlua_register_action(lua_State *L)
7288{
7289 struct action_kw_list *akl;
7290 const char *name;
7291 int ref;
7292 int len;
7293 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007294 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007295
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007296 /* Initialise the number of expected arguments at 0. */
7297 nargs = 0;
7298
7299 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7300 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007301
7302 /* First argument : converter name. */
7303 name = MAY_LJMP(luaL_checkstring(L, 1));
7304
7305 /* Second argument : environment. */
7306 if (lua_type(L, 2) != LUA_TTABLE)
7307 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7308
7309 /* Third argument : lua function. */
7310 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7311
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007312 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007313 if (lua_gettop(L) >= 4)
7314 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7315
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007316 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007317 lua_pushnil(L);
7318 while (lua_next(L, 2) != 0) {
7319 if (lua_type(L, -1) != LUA_TSTRING)
7320 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7321
7322 /* Check required environment. Only accepted "http" or "tcp". */
7323 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007324 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007325 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007326 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007327 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007328 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007329 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007330
7331 /* Fill fcn. */
7332 fcn->name = strdup(name);
7333 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007334 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007335 fcn->function_ref = ref;
7336
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007337 /* Set the expected number od arguments. */
7338 fcn->nargs = nargs;
7339
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007340 /* List head */
7341 akl->list.n = akl->list.p = NULL;
7342
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007343 /* action keyword. */
7344 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007345 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007346 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007347 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007348
7349 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7350
7351 akl->kw[0].match_pfx = 0;
7352 akl->kw[0].private = fcn;
7353 akl->kw[0].parse = action_register_lua;
7354
7355 /* select the action registering point. */
7356 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7357 tcp_req_cont_keywords_register(akl);
7358 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7359 tcp_res_cont_keywords_register(akl);
7360 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7361 http_req_keywords_register(akl);
7362 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7363 http_res_keywords_register(akl);
7364 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007365 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007366 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7367 "are expected.", lua_tostring(L, -1)));
7368
7369 /* pop the environment string. */
7370 lua_pop(L, 1);
7371 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007372 return ACT_RET_PRS_OK;
7373}
7374
7375static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7376 struct act_rule *rule, char **err)
7377{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007378 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007379
Christopher Faulet280f85b2019-07-15 15:02:04 +02007380 if (px->mode == PR_MODE_HTTP) {
7381 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007382 return ACT_RET_PRS_ERR;
7383 }
7384
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007385 /* Memory for the rule. */
7386 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7387 if (!rule->arg.hlua_rule) {
7388 memprintf(err, "out of memory error");
7389 return ACT_RET_PRS_ERR;
7390 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007391
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007392 /* Reference the Lua function and store the reference. */
7393 rule->arg.hlua_rule->fcn = *fcn;
7394
7395 /* TODO: later accept arguments. */
7396 rule->arg.hlua_rule->args = NULL;
7397
7398 /* Add applet pointer in the rule. */
7399 rule->applet.obj_type = OBJ_TYPE_APPLET;
7400 rule->applet.name = fcn->name;
7401 rule->applet.init = hlua_applet_tcp_init;
7402 rule->applet.fct = hlua_applet_tcp_fct;
7403 rule->applet.release = hlua_applet_tcp_release;
7404 rule->applet.timeout = hlua_timeout_applet;
7405
7406 return 0;
7407}
7408
7409/* This function is an LUA binding used for registering
7410 * "sample-conv" functions. It expects a converter name used
7411 * in the haproxy configuration file, and an LUA function.
7412 */
7413__LJMP static int hlua_register_service(lua_State *L)
7414{
7415 struct action_kw_list *akl;
7416 const char *name;
7417 const char *env;
7418 int ref;
7419 int len;
7420 struct hlua_function *fcn;
7421
7422 MAY_LJMP(check_args(L, 3, "register_service"));
7423
7424 /* First argument : converter name. */
7425 name = MAY_LJMP(luaL_checkstring(L, 1));
7426
7427 /* Second argument : environment. */
7428 env = MAY_LJMP(luaL_checkstring(L, 2));
7429
7430 /* Third argument : lua function. */
7431 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7432
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007433 /* Allocate and fill the sample fetch keyword struct. */
7434 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7435 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007436 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007437 fcn = calloc(1, sizeof(*fcn));
7438 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007439 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007440
7441 /* Fill fcn. */
7442 len = strlen("<lua.>") + strlen(name) + 1;
7443 fcn->name = calloc(1, len);
7444 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007445 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007446 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7447 fcn->function_ref = ref;
7448
7449 /* List head */
7450 akl->list.n = akl->list.p = NULL;
7451
7452 /* converter keyword. */
7453 len = strlen("lua.") + strlen(name) + 1;
7454 akl->kw[0].kw = calloc(1, len);
7455 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007456 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007457
7458 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7459
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007460 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007461 if (strcmp(env, "tcp") == 0)
7462 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007463 else if (strcmp(env, "http") == 0)
7464 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007465 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007466 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007467 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007468
7469 akl->kw[0].match_pfx = 0;
7470 akl->kw[0].private = fcn;
7471
7472 /* End of array. */
7473 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7474
7475 /* Register this new converter */
7476 service_keywords_register(akl);
7477
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007478 return 0;
7479}
7480
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007481/* This function initialises Lua cli handler. It copies the
7482 * arguments in the Lua stack and create channel IO objects.
7483 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007484static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007485{
7486 struct hlua *hlua;
7487 struct hlua_function *fcn;
7488 int i;
7489 const char *error;
7490
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007491 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007492 appctx->ctx.hlua_cli.fcn = private;
7493
Willy Tarreaubafbe012017-11-24 17:34:44 +01007494 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007495 if (!hlua) {
7496 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007497 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007498 }
7499 HLUA_INIT(hlua);
7500 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007501
7502 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007503 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007504 * applet_http. It is absolutely compatible.
7505 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007506 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007507 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007508 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007509 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007510 }
7511 appctx->ctx.hlua_cli.task->nice = 0;
7512 appctx->ctx.hlua_cli.task->context = appctx;
7513 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7514
7515 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007516 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007517 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007518 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007519 }
7520
7521 /* The following Lua calls can fail. */
7522 if (!SET_SAFE_LJMP(hlua->T)) {
7523 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7524 error = lua_tostring(hlua->T, -1);
7525 else
7526 error = "critical error";
7527 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7528 goto error;
7529 }
7530
7531 /* Check stack available size. */
7532 if (!lua_checkstack(hlua->T, 2)) {
7533 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7534 goto error;
7535 }
7536
7537 /* Restore the function in the stack. */
7538 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7539
7540 /* Once the arguments parsed, the CLI is like an AppletTCP,
7541 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007542 */
7543 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7544 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7545 goto error;
7546 }
7547 hlua->nargs = 1;
7548
7549 /* push keywords in the stack. */
7550 for (i = 0; *args[i]; i++) {
7551 /* Check stack available size. */
7552 if (!lua_checkstack(hlua->T, 1)) {
7553 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7554 goto error;
7555 }
7556 lua_pushstring(hlua->T, args[i]);
7557 hlua->nargs++;
7558 }
7559
7560 /* We must initialize the execution timeouts. */
7561 hlua->max_time = hlua_timeout_session;
7562
7563 /* At this point the execution is safe. */
7564 RESET_SAFE_LJMP(hlua->T);
7565
7566 /* It's ok */
7567 return 0;
7568
7569 /* It's not ok. */
7570error:
7571 RESET_SAFE_LJMP(hlua->T);
7572 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007573 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007574 return 1;
7575}
7576
7577static int hlua_cli_io_handler_fct(struct appctx *appctx)
7578{
7579 struct hlua *hlua;
7580 struct stream_interface *si;
7581 struct hlua_function *fcn;
7582
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007583 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007584 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007585 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007586
7587 /* If the stream is disconnect or closed, ldo nothing. */
7588 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7589 return 1;
7590
7591 /* Execute the function. */
7592 switch (hlua_ctx_resume(hlua, 1)) {
7593
7594 /* finished. */
7595 case HLUA_E_OK:
7596 return 1;
7597
7598 /* yield. */
7599 case HLUA_E_AGAIN:
7600 /* We want write. */
7601 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007602 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007603 /* Set the timeout. */
7604 if (hlua->wake_time != TICK_ETERNITY)
7605 task_schedule(hlua->task, hlua->wake_time);
7606 return 0;
7607
7608 /* finished with error. */
7609 case HLUA_E_ERRMSG:
7610 /* Display log. */
7611 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7612 fcn->name, lua_tostring(hlua->T, -1));
7613 lua_pop(hlua->T, 1);
7614 return 1;
7615
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007616 case HLUA_E_ETMOUT:
7617 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7618 fcn->name);
7619 return 1;
7620
7621 case HLUA_E_NOMEM:
7622 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7623 fcn->name);
7624 return 1;
7625
7626 case HLUA_E_YIELD: /* unexpected */
7627 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7628 fcn->name);
7629 return 1;
7630
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007631 case HLUA_E_ERR:
7632 /* Display log. */
7633 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7634 fcn->name);
7635 return 1;
7636
7637 default:
7638 return 1;
7639 }
7640
7641 return 1;
7642}
7643
7644static void hlua_cli_io_release_fct(struct appctx *appctx)
7645{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007646 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007647 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007648}
7649
7650/* This function is an LUA binding used for registering
7651 * new keywords in the cli. It expects a list of keywords
7652 * which are the "path". It is limited to 5 keywords. A
7653 * description of the command, a function to be executed
7654 * for the parsing and a function for io handlers.
7655 */
7656__LJMP static int hlua_register_cli(lua_State *L)
7657{
7658 struct cli_kw_list *cli_kws;
7659 const char *message;
7660 int ref_io;
7661 int len;
7662 struct hlua_function *fcn;
7663 int index;
7664 int i;
7665
7666 MAY_LJMP(check_args(L, 3, "register_cli"));
7667
7668 /* First argument : an array of maximum 5 keywords. */
7669 if (!lua_istable(L, 1))
7670 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7671
7672 /* Second argument : string with contextual message. */
7673 message = MAY_LJMP(luaL_checkstring(L, 2));
7674
7675 /* Third and fourth argument : lua function. */
7676 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7677
7678 /* Allocate and fill the sample fetch keyword struct. */
7679 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7680 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007681 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007682 fcn = calloc(1, sizeof(*fcn));
7683 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007684 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007685
7686 /* Fill path. */
7687 index = 0;
7688 lua_pushnil(L);
7689 while(lua_next(L, 1) != 0) {
7690 if (index >= 5)
7691 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7692 if (lua_type(L, -1) != LUA_TSTRING)
7693 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7694 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7695 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007696 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007697 index++;
7698 lua_pop(L, 1);
7699 }
7700
7701 /* Copy help message. */
7702 cli_kws->kw[0].usage = strdup(message);
7703 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007704 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007705
7706 /* Fill fcn io handler. */
7707 len = strlen("<lua.cli>") + 1;
7708 for (i = 0; i < index; i++)
7709 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7710 fcn->name = calloc(1, len);
7711 if (!fcn->name)
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 strncat((char *)fcn->name, "<lua.cli", len);
7714 for (i = 0; i < index; i++) {
7715 strncat((char *)fcn->name, ".", len);
7716 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7717 }
7718 strncat((char *)fcn->name, ">", len);
7719 fcn->function_ref = ref_io;
7720
7721 /* Fill last entries. */
7722 cli_kws->kw[0].private = fcn;
7723 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7724 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7725 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7726
7727 /* Register this new converter */
7728 cli_register_kw(cli_kws);
7729
7730 return 0;
7731}
7732
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007733static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7734 struct proxy *defpx, const char *file, int line,
7735 char **err, unsigned int *timeout)
7736{
7737 const char *error;
7738
7739 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007740 if (error == PARSE_TIME_OVER) {
7741 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7742 args[1], args[0]);
7743 return -1;
7744 }
7745 else if (error == PARSE_TIME_UNDER) {
7746 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7747 args[1], args[0]);
7748 return -1;
7749 }
7750 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007751 memprintf(err, "%s: invalid timeout", args[0]);
7752 return -1;
7753 }
7754 return 0;
7755}
7756
7757static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7758 struct proxy *defpx, const char *file, int line,
7759 char **err)
7760{
7761 return hlua_read_timeout(args, section_type, curpx, defpx,
7762 file, line, err, &hlua_timeout_session);
7763}
7764
7765static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7766 struct proxy *defpx, const char *file, int line,
7767 char **err)
7768{
7769 return hlua_read_timeout(args, section_type, curpx, defpx,
7770 file, line, err, &hlua_timeout_task);
7771}
7772
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007773static int hlua_applet_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_applet);
7779}
7780
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007781static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7782 struct proxy *defpx, const char *file, int line,
7783 char **err)
7784{
7785 char *error;
7786
7787 hlua_nb_instruction = strtoll(args[1], &error, 10);
7788 if (*error != '\0') {
7789 memprintf(err, "%s: invalid number", args[0]);
7790 return -1;
7791 }
7792 return 0;
7793}
7794
Willy Tarreau32f61e22015-03-18 17:54:59 +01007795static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7796 struct proxy *defpx, const char *file, int line,
7797 char **err)
7798{
7799 char *error;
7800
7801 if (*(args[1]) == 0) {
7802 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7803 return -1;
7804 }
7805 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7806 if (*error != '\0') {
7807 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7808 return -1;
7809 }
7810 return 0;
7811}
7812
7813
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007814/* This function is called by the main configuration key "lua-load". It loads and
7815 * execute an lua file during the parsing of the HAProxy configuration file. It is
7816 * the main lua entry point.
7817 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007818 * This function runs with the HAProxy keywords API. It returns -1 if an error
7819 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007820 *
7821 * In some error case, LUA set an error message in top of the stack. This function
7822 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007823 *
7824 * This function can fail with an abort() due to an Lua critical error.
7825 * We are in the configuration parsing process of HAProxy, this abort() is
7826 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007827 */
7828static int hlua_load(char **args, int section_type, struct proxy *curpx,
7829 struct proxy *defpx, const char *file, int line,
7830 char **err)
7831{
7832 int error;
7833
7834 /* Just load and compile the file. */
7835 error = luaL_loadfile(gL.T, args[1]);
7836 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007837 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007838 lua_pop(gL.T, 1);
7839 return -1;
7840 }
7841
7842 /* If no syntax error where detected, execute the code. */
7843 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7844 switch (error) {
7845 case LUA_OK:
7846 break;
7847 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007848 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007849 lua_pop(gL.T, 1);
7850 return -1;
7851 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007852 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007853 return -1;
7854 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007855 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007856 lua_pop(gL.T, 1);
7857 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02007858#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007859 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007860 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007861 lua_pop(gL.T, 1);
7862 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02007863#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007864 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05007865 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007866 lua_pop(gL.T, 1);
7867 return -1;
7868 }
7869
7870 return 0;
7871}
7872
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01007873/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
7874 * in the given <ctx>.
7875 */
7876static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
7877{
7878 lua_getglobal(ctx.T, "package"); /* push package variable */
7879 lua_pushstring(ctx.T, path); /* push given path */
7880 lua_pushstring(ctx.T, ";"); /* push semicolon */
7881 lua_getfield(ctx.T, -3, type); /* push old path */
7882 lua_concat(ctx.T, 3); /* concatenate to new path */
7883 lua_setfield(ctx.T, -2, type); /* store new path */
7884 lua_pop(ctx.T, 1); /* pop package variable */
7885
7886 return 0;
7887}
7888
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007889static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
7890 struct proxy *defpx, const char *file, int line,
7891 char **err)
7892{
7893 char *path;
7894 char *type = "path";
7895 if (too_many_args(2, args, err, NULL)) {
7896 return -1;
7897 }
7898
7899 if (!(*args[1])) {
7900 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
7901 return -1;
7902 }
7903 path = args[1];
7904
7905 if (*args[2]) {
7906 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
7907 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
7908 return -1;
7909 }
7910 type = args[2];
7911 }
7912
7913 return hlua_prepend_path(gL, type, path);
7914}
7915
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007916/* configuration keywords declaration */
7917static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007918 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007919 { CFG_GLOBAL, "lua-load", hlua_load },
7920 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7921 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007922 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007923 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007924 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007925 { 0, NULL, NULL },
7926}};
7927
Willy Tarreau0108d902018-11-25 19:14:37 +01007928INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7929
Christopher Fauletafd8f102018-11-08 11:34:21 +01007930
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007931/* This function can fail with an abort() due to an Lua critical error.
7932 * We are in the initialisation process of HAProxy, this abort() is
7933 * tolerated.
7934 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007935int hlua_post_init()
7936{
7937 struct hlua_init_function *init;
7938 const char *msg;
7939 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007940 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007941
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007942 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007943 if (!SET_SAFE_LJMP(gL.T)) {
7944 if (lua_type(gL.T, -1) == LUA_TSTRING)
7945 error = lua_tostring(gL.T, -1);
7946 else
7947 error = "critical error";
7948 fprintf(stderr, "Lua post-init: %s.\n", error);
7949 exit(1);
7950 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007951
7952#if USE_OPENSSL
7953 /* Initialize SSL server. */
7954 if (socket_ssl.xprt->prepare_srv) {
7955 int saved_used_backed = global.ssl_used_backend;
7956 // don't affect maxconn automatic computation
7957 socket_ssl.xprt->prepare_srv(&socket_ssl);
7958 global.ssl_used_backend = saved_used_backed;
7959 }
7960#endif
7961
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007962 hlua_fcn_post_init(gL.T);
7963 RESET_SAFE_LJMP(gL.T);
7964
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007965 list_for_each_entry(init, &hlua_init_functions, l) {
7966 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7967 ret = hlua_ctx_resume(&gL, 0);
7968 switch (ret) {
7969 case HLUA_E_OK:
7970 lua_pop(gL.T, -1);
7971 return 1;
7972 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007973 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007974 return 0;
7975 case HLUA_E_ERRMSG:
7976 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007977 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007978 return 0;
7979 case HLUA_E_ERR:
7980 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007981 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007982 return 0;
7983 }
7984 }
7985 return 1;
7986}
7987
Willy Tarreau32f61e22015-03-18 17:54:59 +01007988/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7989 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7990 * is the previously allocated size or the kind of object in case of a new
7991 * allocation. <nsize> is the requested new size.
7992 */
7993static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7994{
7995 struct hlua_mem_allocator *zone = ud;
7996
7997 if (nsize == 0) {
7998 /* it's a free */
7999 if (ptr)
8000 zone->allocated -= osize;
8001 free(ptr);
8002 return NULL;
8003 }
8004
8005 if (!ptr) {
8006 /* it's a new allocation */
8007 if (zone->limit && zone->allocated + nsize > zone->limit)
8008 return NULL;
8009
8010 ptr = malloc(nsize);
8011 if (ptr)
8012 zone->allocated += nsize;
8013 return ptr;
8014 }
8015
8016 /* it's a realloc */
8017 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8018 return NULL;
8019
8020 ptr = realloc(ptr, nsize);
8021 if (ptr)
8022 zone->allocated += nsize - osize;
8023 return ptr;
8024}
8025
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008026/* Ithis function can fail with an abort() due to an Lua critical error.
8027 * We are in the initialisation process of HAProxy, this abort() is
8028 * tolerated.
8029 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008030void hlua_init(void)
8031{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008032 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008033 int idx;
8034 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008035 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008036 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008037 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008038#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008039 struct srv_kw *kw;
8040 int tmp_error;
8041 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008042 char *args[] = { /* SSL client configuration. */
8043 "ssl",
8044 "verify",
8045 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008046 NULL
8047 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008048#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008049
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008050 /* Init main lua stack. */
8051 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008052 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008053 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008054 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008055 hlua_sethlua(&gL);
8056 gL.Tref = LUA_REFNIL;
8057 gL.task = NULL;
8058
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008059 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008060 * the Lua function can fail with an abort. We are in the initialisation
8061 * process of HAProxy, this abort() is tolerated.
8062 */
8063
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008064 /* Initialise lua. */
8065 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008066#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8067#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8068#ifdef HLUA_PREPEND_PATH
8069 hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
8070#endif
8071#ifdef HLUA_PREPEND_CPATH
8072 hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
8073#endif
8074#undef HLUA_PREPEND_PATH_TOSTRING
8075#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008076
Thierry Fournier75933d42016-01-21 09:30:18 +01008077 /* Set safe environment for the initialisation. */
8078 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008079 if (lua_type(gL.T, -1) == LUA_TSTRING)
8080 error_msg = lua_tostring(gL.T, -1);
8081 else
8082 error_msg = "critical error";
8083 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008084 exit(1);
8085 }
8086
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008087 /*
8088 *
8089 * Create "core" object.
8090 *
8091 */
8092
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008093 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008094 lua_newtable(gL.T);
8095
8096 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008097 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008098 hlua_class_const_int(gL.T, log_levels[i], i);
8099
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008100 /* Register special functions. */
8101 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008102 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008103 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008104 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008105 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008106 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008107 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008108 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008109 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008110 hlua_class_function(gL.T, "sleep", hlua_sleep);
8111 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008112 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8113 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8114 hlua_class_function(gL.T, "set_map", hlua_set_map);
8115 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008116 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008117 hlua_class_function(gL.T, "log", hlua_log);
8118 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8119 hlua_class_function(gL.T, "Info", hlua_log_info);
8120 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8121 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008122 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008123 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008124
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008125 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008126
8127 /*
8128 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008129 * Create "act" object.
8130 *
8131 */
8132
8133 /* This table entry is the object "act" base. */
8134 lua_newtable(gL.T);
8135
8136 /* push action return constants */
8137 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8138 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8139 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8140 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8141 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8142 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8143 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8144 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8145
Christopher Faulet501465d2020-02-26 14:54:16 +01008146 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008147
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008148 lua_setglobal(gL.T, "act");
8149
8150 /*
8151 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008152 * Register class Map
8153 *
8154 */
8155
8156 /* This table entry is the object "Map" base. */
8157 lua_newtable(gL.T);
8158
8159 /* register pattern types. */
8160 for (i=0; i<PAT_MATCH_NUM; i++)
8161 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008162 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008163 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8164 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008165 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008166
8167 /* register constructor. */
8168 hlua_class_function(gL.T, "new", hlua_map_new);
8169
8170 /* Create and fill the metatable. */
8171 lua_newtable(gL.T);
8172
Ilya Shipitsind4259502020-04-08 01:07:56 +05008173 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008174 lua_pushstring(gL.T, "__index");
8175 lua_newtable(gL.T);
8176
8177 /* Register . */
8178 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8179 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8180
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008181 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008182
Thierry Fournier45e78d72016-02-19 18:34:46 +01008183 /* Register previous table in the registry with reference and named entry.
8184 * The function hlua_register_metatable() pops the stack, so we
8185 * previously create a copy of the table.
8186 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008187 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008188 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008189
8190 /* Assign the metatable to the mai Map object. */
8191 lua_setmetatable(gL.T, -2);
8192
8193 /* Set a name to the table. */
8194 lua_setglobal(gL.T, "Map");
8195
8196 /*
8197 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008198 * Register class Channel
8199 *
8200 */
8201
8202 /* Create and fill the metatable. */
8203 lua_newtable(gL.T);
8204
Ilya Shipitsind4259502020-04-08 01:07:56 +05008205 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008206 lua_pushstring(gL.T, "__index");
8207 lua_newtable(gL.T);
8208
8209 /* Register . */
8210 hlua_class_function(gL.T, "get", hlua_channel_get);
8211 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8212 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8213 hlua_class_function(gL.T, "set", hlua_channel_set);
8214 hlua_class_function(gL.T, "append", hlua_channel_append);
8215 hlua_class_function(gL.T, "send", hlua_channel_send);
8216 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8217 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8218 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008219 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008220 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008221
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008222 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008223
8224 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008225 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008226
8227 /*
8228 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008229 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008230 *
8231 */
8232
8233 /* Create and fill the metatable. */
8234 lua_newtable(gL.T);
8235
Ilya Shipitsind4259502020-04-08 01:07:56 +05008236 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008237 lua_pushstring(gL.T, "__index");
8238 lua_newtable(gL.T);
8239
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008240 /* Browse existing fetches and create the associated
8241 * object method.
8242 */
8243 sf = NULL;
8244 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8245
8246 /* Dont register the keywork if the arguments check function are
8247 * not safe during the runtime.
8248 */
8249 if ((sf->val_args != NULL) &&
8250 (sf->val_args != val_payload_lv) &&
8251 (sf->val_args != val_hdr))
8252 continue;
8253
8254 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8255 * by an underscore.
8256 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008257 strncpy(trash.area, sf->kw, trash.size);
8258 trash.area[trash.size - 1] = '\0';
8259 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008260 if (*p == '.' || *p == '-' || *p == '+')
8261 *p = '_';
8262
8263 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008264 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008265 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008266 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008267 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008268 }
8269
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008270 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008271
8272 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008273 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008274
8275 /*
8276 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008277 * Register class Converters
8278 *
8279 */
8280
8281 /* Create and fill the metatable. */
8282 lua_newtable(gL.T);
8283
8284 /* Create and fill the __index entry. */
8285 lua_pushstring(gL.T, "__index");
8286 lua_newtable(gL.T);
8287
8288 /* Browse existing converters and create the associated
8289 * object method.
8290 */
8291 sc = NULL;
8292 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8293 /* Dont register the keywork if the arguments check function are
8294 * not safe during the runtime.
8295 */
8296 if (sc->val_args != NULL)
8297 continue;
8298
8299 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8300 * by an underscore.
8301 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008302 strncpy(trash.area, sc->kw, trash.size);
8303 trash.area[trash.size - 1] = '\0';
8304 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008305 if (*p == '.' || *p == '-' || *p == '+')
8306 *p = '_';
8307
8308 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008309 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008310 lua_pushlightuserdata(gL.T, sc);
8311 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008312 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008313 }
8314
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008315 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008316
8317 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008318 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008319
8320 /*
8321 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008322 * Register class HTTP
8323 *
8324 */
8325
8326 /* Create and fill the metatable. */
8327 lua_newtable(gL.T);
8328
Ilya Shipitsind4259502020-04-08 01:07:56 +05008329 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008330 lua_pushstring(gL.T, "__index");
8331 lua_newtable(gL.T);
8332
8333 /* Register Lua functions. */
8334 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8335 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8336 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8337 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8338 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8339 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8340 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8341 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8342 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8343 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8344
8345 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8346 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8347 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8348 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8349 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8350 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008351 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008352
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008353 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008354
8355 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008356 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008357
8358 /*
8359 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008360 * Register class AppletTCP
8361 *
8362 */
8363
8364 /* Create and fill the metatable. */
8365 lua_newtable(gL.T);
8366
Ilya Shipitsind4259502020-04-08 01:07:56 +05008367 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008368 lua_pushstring(gL.T, "__index");
8369 lua_newtable(gL.T);
8370
8371 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008372 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8373 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8374 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8375 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8376 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008377 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8378 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8379 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008380
8381 lua_settable(gL.T, -3);
8382
8383 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008384 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008385
8386 /*
8387 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008388 * Register class AppletHTTP
8389 *
8390 */
8391
8392 /* Create and fill the metatable. */
8393 lua_newtable(gL.T);
8394
Ilya Shipitsind4259502020-04-08 01:07:56 +05008395 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008396 lua_pushstring(gL.T, "__index");
8397 lua_newtable(gL.T);
8398
8399 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008400 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8401 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008402 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8403 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8404 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008405 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8406 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8407 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8408 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8409 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8410 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8411
8412 lua_settable(gL.T, -3);
8413
8414 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008415 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008416
8417 /*
8418 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008419 * Register class TXN
8420 *
8421 */
8422
8423 /* Create and fill the metatable. */
8424 lua_newtable(gL.T);
8425
Ilya Shipitsind4259502020-04-08 01:07:56 +05008426 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008427 lua_pushstring(gL.T, "__index");
8428 lua_newtable(gL.T);
8429
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008430 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008431 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8432 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8433 hlua_class_function(gL.T, "set_var", hlua_set_var);
8434 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8435 hlua_class_function(gL.T, "get_var", hlua_get_var);
8436 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008437 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008438 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8439 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8440 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8441 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8442 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8443 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8444 hlua_class_function(gL.T, "log", hlua_txn_log);
8445 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8446 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8447 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8448 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008449
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008450 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008451
8452 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008453 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008454
8455 /*
8456 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008457 * Register class reply
8458 *
8459 */
8460 lua_newtable(gL.T);
8461 lua_pushstring(gL.T, "__index");
8462 lua_newtable(gL.T);
8463 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8464 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8465 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8466 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8467 lua_settable(gL.T, -3); /* Sets the __index entry. */
8468 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8469
8470
8471 /*
8472 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008473 * Register class Socket
8474 *
8475 */
8476
8477 /* Create and fill the metatable. */
8478 lua_newtable(gL.T);
8479
Ilya Shipitsind4259502020-04-08 01:07:56 +05008480 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008481 lua_pushstring(gL.T, "__index");
8482 lua_newtable(gL.T);
8483
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008484#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008485 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008486#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008487 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8488 hlua_class_function(gL.T, "send", hlua_socket_send);
8489 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8490 hlua_class_function(gL.T, "close", hlua_socket_close);
8491 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8492 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8493 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8494 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8495
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008496 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008497
8498 /* Register the garbage collector entry. */
8499 lua_pushstring(gL.T, "__gc");
8500 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008501 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008502
8503 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008504 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008505
8506 /* Proxy and server configuration initialisation. */
8507 memset(&socket_proxy, 0, sizeof(socket_proxy));
8508 init_new_proxy(&socket_proxy);
8509 socket_proxy.parent = NULL;
8510 socket_proxy.last_change = now.tv_sec;
8511 socket_proxy.id = "LUA-SOCKET";
8512 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8513 socket_proxy.maxconn = 0;
8514 socket_proxy.accept = NULL;
8515 socket_proxy.options2 |= PR_O2_INDEPSTR;
8516 socket_proxy.srv = NULL;
8517 socket_proxy.conn_retries = 0;
8518 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8519
8520 /* Init TCP server: unchanged parameters */
8521 memset(&socket_tcp, 0, sizeof(socket_tcp));
8522 socket_tcp.next = NULL;
8523 socket_tcp.proxy = &socket_proxy;
8524 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8525 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008526 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008527 socket_tcp.idle_conns = NULL;
8528 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008529 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008530 socket_tcp.last_change = 0;
8531 socket_tcp.id = "LUA-TCP-CONN";
8532 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8533 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8534 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8535
8536 /* XXX: Copy default parameter from default server,
8537 * but the default server is not initialized.
8538 */
8539 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8540 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8541 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8542 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8543 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8544 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8545 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8546 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8547 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8548 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8549
8550 socket_tcp.check.status = HCHK_STATUS_INI;
8551 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8552 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8553 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8554 socket_tcp.check.server = &socket_tcp;
8555
8556 socket_tcp.agent.status = HCHK_STATUS_INI;
8557 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8558 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8559 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8560 socket_tcp.agent.server = &socket_tcp;
8561
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008562 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008563
8564#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008565 /* Init TCP server: unchanged parameters */
8566 memset(&socket_ssl, 0, sizeof(socket_ssl));
8567 socket_ssl.next = NULL;
8568 socket_ssl.proxy = &socket_proxy;
8569 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8570 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008571 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008572 socket_ssl.idle_conns = NULL;
8573 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008574 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008575 socket_ssl.last_change = 0;
8576 socket_ssl.id = "LUA-SSL-CONN";
8577 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8578 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8579 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8580
8581 /* XXX: Copy default parameter from default server,
8582 * but the default server is not initialized.
8583 */
8584 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8585 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8586 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8587 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8588 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8589 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8590 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8591 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8592 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8593 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8594
8595 socket_ssl.check.status = HCHK_STATUS_INI;
8596 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8597 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8598 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8599 socket_ssl.check.server = &socket_ssl;
8600
8601 socket_ssl.agent.status = HCHK_STATUS_INI;
8602 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8603 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8604 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8605 socket_ssl.agent.server = &socket_ssl;
8606
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008607 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008608 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008609
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008610 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008611 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8612 /*
8613 *
8614 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008615 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008616 * features like client certificates and ssl_verify.
8617 *
8618 */
8619 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8620 if (tmp_error != 0) {
8621 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8622 abort(); /* This must be never arrives because the command line
8623 not editable by the user. */
8624 }
8625 idx += kw->skip;
8626 }
8627 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008628#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008629
8630 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008631}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008632
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02008633static void hlua_deinit()
8634{
8635 lua_close(gL.T);
8636}
8637
8638REGISTER_POST_DEINIT(hlua_deinit);
8639
Willy Tarreau80713382018-11-26 10:19:54 +01008640static void hlua_register_build_options(void)
8641{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008642 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008643
Willy Tarreaubb57d942016-12-21 19:04:56 +01008644 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8645 hap_register_build_opts(ptr, 1);
8646}
Willy Tarreau80713382018-11-26 10:19:54 +01008647
8648INITCALL0(STG_REGISTER, hlua_register_build_options);