blob: 63ca26b88b98ca71e9176056197b5c169c6bdf25 [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;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001515 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001516 args[1].type = ARGT_STOP;
1517
1518 /* load the map. */
1519 if (!sample_load_map(args, &conv, file, line, &err)) {
1520 /* error case: we cant use luaL_error because we must
1521 * free the err variable.
1522 */
1523 luaL_where(L, 1);
1524 lua_pushfstring(L, "'new': %s.", err);
1525 lua_concat(L, 2);
1526 free(err);
1527 WILL_LJMP(lua_error(L));
1528 }
1529
1530 /* create the lua object. */
1531 lua_newtable(L);
1532 lua_pushlightuserdata(L, args[0].data.map);
1533 lua_rawseti(L, -2, 0);
1534
1535 /* Pop a class Map metatable and affect it to the userdata. */
1536 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1537 lua_setmetatable(L, -2);
1538
1539
1540 return 1;
1541}
1542
1543__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1544{
1545 struct map_descriptor *desc;
1546 struct pattern *pat;
1547 struct sample smp;
1548
1549 MAY_LJMP(check_args(L, 2, "lookup"));
1550 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001551 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001552 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001553 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001554 }
1555 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001556 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001557 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001558 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 +02001559 }
1560
1561 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001562 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001563 if (str)
1564 lua_pushstring(L, "");
1565 else
1566 lua_pushnil(L);
1567 return 1;
1568 }
1569
1570 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001571 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001572 return 1;
1573}
1574
1575__LJMP static int hlua_map_lookup(struct lua_State *L)
1576{
1577 return _hlua_map_lookup(L, 0);
1578}
1579
1580__LJMP static int hlua_map_slookup(struct lua_State *L)
1581{
1582 return _hlua_map_lookup(L, 1);
1583}
1584
1585/*
1586 *
1587 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001588 * Class Socket
1589 *
1590 *
1591 */
1592
1593__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1594{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001595 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001596}
1597
1598/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001599 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001600 * received.
1601 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001602static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001603{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001604 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001605
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001606 if (appctx->ctx.hlua_cosocket.die) {
1607 si_shutw(si);
1608 si_shutr(si);
1609 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001610 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1611 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001612 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1613 }
1614
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001615 /* If we cant write, wakeup the pending write signals. */
1616 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001617 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001618
1619 /* If we cant read, wakeup the pending read signals. */
1620 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001621 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001622
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001623 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001624 * to be notified whenever the connection completes.
1625 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001626 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001627 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001628 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001629 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001630 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001631 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001632
1633 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001634 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001635
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001636 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001637 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001638 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001639
1640 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001641 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001642 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001643
1644 /* Some data were injected in the buffer, notify the stream
1645 * interface.
1646 */
1647 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001648 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001649
1650 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001651 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001652 */
1653 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001654 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001655}
1656
Willy Tarreau87b09662015-04-03 00:22:06 +02001657/* This function is called when the "struct stream" is destroyed.
1658 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001659 * Wake all the pending signals.
1660 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001661static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001662{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001663 struct xref *peer;
1664
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001665 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001666 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1667 if (peer)
1668 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001669
1670 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001671 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1672 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001673}
1674
1675/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001676 * uses this object. If the stream does not exists, just quit.
1677 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001678 * pending signal can rest in the read and write lists. destroy
1679 * it.
1680 */
1681__LJMP static int hlua_socket_gc(lua_State *L)
1682{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001683 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001684 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001685 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001686
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001687 MAY_LJMP(check_args(L, 1, "__gc"));
1688
1689 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001690 peer = xref_get_peer_and_lock(&socket->xref);
1691 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001692 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001693 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001694
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001695 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001696 appctx->ctx.hlua_cosocket.die = 1;
1697 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001698
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001699 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001700 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701 return 0;
1702}
1703
1704/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001705 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001706 */
sada05ed3302018-05-11 11:48:18 -07001707__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001708{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001709 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001710 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001711 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001712 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001713
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001714 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001715
1716 /* Check if we run on the same thread than the xreator thread.
1717 * We cannot access to the socket if the thread is different.
1718 */
1719 if (socket->tid != tid)
1720 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1721
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001722 peer = xref_get_peer_and_lock(&socket->xref);
1723 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001724 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001725
1726 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001727 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001728
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001729 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001730 appctx->ctx.hlua_cosocket.die = 1;
1731 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001732
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001733 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001734 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001735 return 0;
1736}
1737
sada05ed3302018-05-11 11:48:18 -07001738/* The close function calls close_helper.
1739 */
1740__LJMP static int hlua_socket_close(lua_State *L)
1741{
1742 MAY_LJMP(check_args(L, 1, "close"));
1743 return hlua_socket_close_helper(L);
1744}
1745
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001746/* This Lua function assumes that the stack contain three parameters.
1747 * 1 - USERDATA containing a struct socket
1748 * 2 - INTEGER with values of the macro defined below
1749 * If the integer is -1, we must read at most one line.
1750 * If the integer is -2, we ust read all the data until the
1751 * end of the stream.
1752 * If the integer is positive value, we must read a number of
1753 * bytes corresponding to this value.
1754 */
1755#define HLSR_READ_LINE (-1)
1756#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001757__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001758{
1759 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1760 int wanted = lua_tointeger(L, 2);
1761 struct hlua *hlua = hlua_gethlua(L);
1762 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001763 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001764 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001765 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001766 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001767 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001768 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001769 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001770 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001771 struct stream_interface *si;
1772 struct stream *s;
1773 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001774 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001775
1776 /* Check if this lua stack is schedulable. */
1777 if (!hlua || !hlua->task)
1778 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1779 "'frontend', 'backend' or 'task'"));
1780
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001781 /* Check if we run on the same thread than the xreator thread.
1782 * We cannot access to the socket if the thread is different.
1783 */
1784 if (socket->tid != tid)
1785 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1786
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001787 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001788 peer = xref_get_peer_and_lock(&socket->xref);
1789 if (!peer)
1790 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001791 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1792 si = appctx->owner;
1793 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001794
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001795 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001796 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001797 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001798 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001799 if (nblk < 0) /* Connection close. */
1800 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001801 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001802 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001803
1804 /* remove final \r\n. */
1805 if (nblk == 1) {
1806 if (blk1[len1-1] == '\n') {
1807 len1--;
1808 skip_at_end++;
1809 if (blk1[len1-1] == '\r') {
1810 len1--;
1811 skip_at_end++;
1812 }
1813 }
1814 }
1815 else {
1816 if (blk2[len2-1] == '\n') {
1817 len2--;
1818 skip_at_end++;
1819 if (blk2[len2-1] == '\r') {
1820 len2--;
1821 skip_at_end++;
1822 }
1823 }
1824 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825 }
1826
1827 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001828 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001829 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001830 if (nblk < 0) /* Connection close. */
1831 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001832 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001833 goto connection_empty;
1834 }
1835
1836 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001837 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001838 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001839 if (nblk < 0) /* Connection close. */
1840 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001841 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001842 goto connection_empty;
1843
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001844 missing_bytes = wanted - socket->b.n;
1845 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001846 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001847 len1 = missing_bytes;
1848 } if (nblk == 2 && len1 + len2 > missing_bytes)
1849 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001850 }
1851
1852 len = len1;
1853
1854 luaL_addlstring(&socket->b, blk1, len1);
1855 if (nblk == 2) {
1856 len += len2;
1857 luaL_addlstring(&socket->b, blk2, len2);
1858 }
1859
1860 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001861 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001862
1863 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001864 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001865
1866 /* If the pattern reclaim to read all the data
1867 * in the connection, got out.
1868 */
1869 if (wanted == HLSR_READ_ALL)
1870 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001871 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001872 goto connection_empty;
1873
1874 /* Return result. */
1875 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001876 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001877 return 1;
1878
1879connection_closed:
1880
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001881 xref_unlock(&socket->xref, peer);
1882
1883no_peer:
1884
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001885 /* If the buffer containds data. */
1886 if (socket->b.n > 0) {
1887 luaL_pushresult(&socket->b);
1888 return 1;
1889 }
1890 lua_pushnil(L);
1891 lua_pushstring(L, "connection closed.");
1892 return 2;
1893
1894connection_empty:
1895
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001896 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1897 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001898 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001899 }
1900 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001901 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001902 return 0;
1903}
1904
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001905/* This Lua function gets two parameters. The first one can be string
1906 * or a number. If the string is "*l", the user requires one line. If
1907 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908 * If the value is a number, the user require a number of bytes equal
1909 * to the value. The default value is "*l" (a line).
1910 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001911 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001912 * integer takes this values:
1913 * -1 : read a line
1914 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001915 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001916 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001917 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001918 * concatenated with the read data.
1919 */
1920__LJMP static int hlua_socket_receive(struct lua_State *L)
1921{
1922 int wanted = HLSR_READ_LINE;
1923 const char *pattern;
1924 int type;
1925 char *error;
1926 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001927 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001928
1929 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1930 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1931
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001932 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001933
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001934 /* Check if we run on the same thread than the xreator thread.
1935 * We cannot access to the socket if the thread is different.
1936 */
1937 if (socket->tid != tid)
1938 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1939
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001940 /* check for pattern. */
1941 if (lua_gettop(L) >= 2) {
1942 type = lua_type(L, 2);
1943 if (type == LUA_TSTRING) {
1944 pattern = lua_tostring(L, 2);
1945 if (strcmp(pattern, "*a") == 0)
1946 wanted = HLSR_READ_ALL;
1947 else if (strcmp(pattern, "*l") == 0)
1948 wanted = HLSR_READ_LINE;
1949 else {
1950 wanted = strtoll(pattern, &error, 10);
1951 if (*error != '\0')
1952 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1953 }
1954 }
1955 else if (type == LUA_TNUMBER) {
1956 wanted = lua_tointeger(L, 2);
1957 if (wanted < 0)
1958 WILL_LJMP(luaL_error(L, "Unsupported size."));
1959 }
1960 }
1961
1962 /* Set pattern. */
1963 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001964
1965 /* Check if we would replace the top by itself. */
1966 if (lua_gettop(L) != 2)
1967 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001968
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001969 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001970 luaL_buffinit(L, &socket->b);
1971
1972 /* Check prefix. */
1973 if (lua_gettop(L) >= 3) {
1974 if (lua_type(L, 3) != LUA_TSTRING)
1975 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1976 pattern = lua_tolstring(L, 3, &len);
1977 luaL_addlstring(&socket->b, pattern, len);
1978 }
1979
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001980 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001981}
1982
1983/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001984 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001985 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001986static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001987{
1988 struct hlua_socket *socket;
1989 struct hlua *hlua = hlua_gethlua(L);
1990 struct appctx *appctx;
1991 size_t buf_len;
1992 const char *buf;
1993 int len;
1994 int send_len;
1995 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001996 struct xref *peer;
1997 struct stream_interface *si;
1998 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001999
2000 /* Check if this lua stack is schedulable. */
2001 if (!hlua || !hlua->task)
2002 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2003 "'frontend', 'backend' or 'task'"));
2004
2005 /* Get object */
2006 socket = MAY_LJMP(hlua_checksocket(L, 1));
2007 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002008 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002009
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002010 /* Check if we run on the same thread than the xreator thread.
2011 * We cannot access to the socket if the thread is different.
2012 */
2013 if (socket->tid != tid)
2014 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2015
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002016 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002017 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002018 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002019 lua_pushinteger(L, -1);
2020 return 1;
2021 }
2022 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2023 si = appctx->owner;
2024 s = si_strm(si);
2025
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002026 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002027 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002028 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002029 lua_pushinteger(L, -1);
2030 return 1;
2031 }
2032
2033 /* Update the input buffer data. */
2034 buf += sent;
2035 send_len = buf_len - sent;
2036
2037 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002038 if (sent >= buf_len) {
2039 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002040 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002041 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002042
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002043 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002044 * the request buffer if its not required.
2045 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002046 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002047 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002048 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002049 }
2050
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002051 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002052 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002053 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002054 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002055 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002056
2057 /* send data */
2058 if (len < send_len)
2059 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002060 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002061
2062 /* "Not enough space" (-1), "Buffer too little to contain
2063 * the data" (-2) are not expected because the available length
2064 * is tested.
2065 * Other unknown error are also not expected.
2066 */
2067 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002068 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002069 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002070
sada05ed3302018-05-11 11:48:18 -07002071 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002072 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002073 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002074 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002075 return 1;
2076 }
2077
2078 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002079 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002080
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002081 s->req.rex = TICK_ETERNITY;
2082 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002083
2084 /* Update length sent. */
2085 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002086 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002087
2088 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002089 if (sent + len >= buf_len) {
2090 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002091 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002092 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002093
2094hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002095 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2096 xref_unlock(&socket->xref, peer);
2097 WILL_LJMP(luaL_error(L, "out of memory"));
2098 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002099 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002100 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002101 return 0;
2102}
2103
2104/* This function initiate the send of data. It just check the input
2105 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002106 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002107 * "hlua_socket_write_yield" that can yield.
2108 *
2109 * The Lua function gets between 3 and 4 parameters. The first one is
2110 * the associated object. The second is a string buffer. The third is
2111 * a facultative integer that represents where is the buffer position
2112 * of the start of the data that can send. The first byte is the
2113 * position "1". The default value is "1". The fourth argument is a
2114 * facultative integer that represents where is the buffer position
2115 * of the end of the data that can send. The default is the last byte.
2116 */
2117static int hlua_socket_send(struct lua_State *L)
2118{
2119 int i;
2120 int j;
2121 const char *buf;
2122 size_t buf_len;
2123
2124 /* Check number of arguments. */
2125 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2126 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2127
2128 /* Get the string. */
2129 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2130
2131 /* Get and check j. */
2132 if (lua_gettop(L) == 4) {
2133 j = MAY_LJMP(luaL_checkinteger(L, 4));
2134 if (j < 0)
2135 j = buf_len + j + 1;
2136 if (j > buf_len)
2137 j = buf_len + 1;
2138 lua_pop(L, 1);
2139 }
2140 else
2141 j = buf_len;
2142
2143 /* Get and check i. */
2144 if (lua_gettop(L) == 3) {
2145 i = MAY_LJMP(luaL_checkinteger(L, 3));
2146 if (i < 0)
2147 i = buf_len + i + 1;
2148 if (i > buf_len)
2149 i = buf_len + 1;
2150 lua_pop(L, 1);
2151 } else
2152 i = 1;
2153
2154 /* Check bth i and j. */
2155 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002156 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002157 return 1;
2158 }
2159 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002160 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002161 return 1;
2162 }
2163 if (i == 0)
2164 i = 1;
2165 if (j == 0)
2166 j = 1;
2167
2168 /* Pop the string. */
2169 lua_pop(L, 1);
2170
2171 /* Update the buffer length. */
2172 buf += i - 1;
2173 buf_len = j - i + 1;
2174 lua_pushlstring(L, buf, buf_len);
2175
2176 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002177 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002178
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002179 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002180}
2181
Willy Tarreau22b0a682015-06-17 19:43:49 +02002182#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002183__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2184{
2185 static char buffer[SOCKET_INFO_MAX_LEN];
2186 int ret;
2187 int len;
2188 char *p;
2189
2190 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2191 if (ret <= 0) {
2192 lua_pushnil(L);
2193 return 1;
2194 }
2195
2196 if (ret == AF_UNIX) {
2197 lua_pushstring(L, buffer+1);
2198 return 1;
2199 }
2200 else if (ret == AF_INET6) {
2201 buffer[0] = '[';
2202 len = strlen(buffer);
2203 buffer[len] = ']';
2204 len++;
2205 buffer[len] = ':';
2206 len++;
2207 p = buffer;
2208 }
2209 else if (ret == AF_INET) {
2210 p = buffer + 1;
2211 len = strlen(p);
2212 p[len] = ':';
2213 len++;
2214 }
2215 else {
2216 lua_pushnil(L);
2217 return 1;
2218 }
2219
2220 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2221 lua_pushnil(L);
2222 return 1;
2223 }
2224
2225 lua_pushstring(L, p);
2226 return 1;
2227}
2228
2229/* Returns information about the peer of the connection. */
2230__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2231{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002232 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002233 struct xref *peer;
2234 struct appctx *appctx;
2235 struct stream_interface *si;
2236 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002237 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002238
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002239 MAY_LJMP(check_args(L, 1, "getpeername"));
2240
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002241 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002242
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002243 /* Check if we run on the same thread than the xreator thread.
2244 * We cannot access to the socket if the thread is different.
2245 */
2246 if (socket->tid != tid)
2247 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2248
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002249 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002250 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002251 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002252 lua_pushnil(L);
2253 return 1;
2254 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002255 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2256 si = appctx->owner;
2257 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002258
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002259 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002260 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002261 lua_pushnil(L);
2262 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002263 }
2264
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002265 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002266 xref_unlock(&socket->xref, peer);
2267 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002268}
2269
2270/* Returns information about my connection side. */
2271static int hlua_socket_getsockname(struct lua_State *L)
2272{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002273 struct hlua_socket *socket;
2274 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002275 struct appctx *appctx;
2276 struct xref *peer;
2277 struct stream_interface *si;
2278 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002279 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002280
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002281 MAY_LJMP(check_args(L, 1, "getsockname"));
2282
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002283 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002284
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002285 /* Check if we run on the same thread than the xreator thread.
2286 * We cannot access to the socket if the thread is different.
2287 */
2288 if (socket->tid != tid)
2289 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2290
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002291 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002292 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002293 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294 lua_pushnil(L);
2295 return 1;
2296 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002297 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2298 si = appctx->owner;
2299 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002300
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002301 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002302 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002303 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002304 lua_pushnil(L);
2305 return 1;
2306 }
2307
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002308 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002309 xref_unlock(&socket->xref, peer);
2310 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002311}
2312
2313/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002314static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002315 .obj_type = OBJ_TYPE_APPLET,
2316 .name = "<LUA_TCP>",
2317 .fct = hlua_socket_handler,
2318 .release = hlua_socket_release,
2319};
2320
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002321__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002322{
2323 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2324 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002325 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002326 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002327 struct stream_interface *si;
2328 struct stream *s;
2329
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002330 /* Check if we run on the same thread than the xreator thread.
2331 * We cannot access to the socket if the thread is different.
2332 */
2333 if (socket->tid != tid)
2334 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2335
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002336 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002337 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002338 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002339 lua_pushnil(L);
2340 lua_pushstring(L, "Can't connect");
2341 return 2;
2342 }
2343 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2344 si = appctx->owner;
2345 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002346
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002347 /* Check if we run on the same thread than the xreator thread.
2348 * We cannot access to the socket if the thread is different.
2349 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002350 if (socket->tid != tid) {
2351 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002352 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002353 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002354
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002355 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002356 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002357 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002358 lua_pushnil(L);
2359 lua_pushstring(L, "Can't connect");
2360 return 2;
2361 }
2362
Willy Tarreaue09101e2018-10-16 17:37:12 +02002363 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002364
2365 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002366 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002367 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002368 lua_pushinteger(L, 1);
2369 return 1;
2370 }
2371
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002372 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2373 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002374 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002375 }
2376 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002377 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378 return 0;
2379}
2380
2381/* This function fail or initite the connection. */
2382__LJMP static int hlua_socket_connect(struct lua_State *L)
2383{
2384 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002385 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002386 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002387 struct hlua *hlua;
2388 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002389 int low, high;
2390 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002391 struct xref *peer;
2392 struct stream_interface *si;
2393 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002394
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002395 if (lua_gettop(L) < 2)
2396 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002397
2398 /* Get args. */
2399 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002400
2401 /* Check if we run on the same thread than the xreator thread.
2402 * We cannot access to the socket if the thread is different.
2403 */
2404 if (socket->tid != tid)
2405 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2406
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002407 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002408 if (lua_gettop(L) >= 3) {
2409 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002410 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002411
Tim Duesterhus6edab862018-01-06 19:04:45 +01002412 /* Force the ip to end with a colon, to support IPv6 addresses
2413 * that are not enclosed within square brackets.
2414 */
2415 if (port > 0) {
2416 luaL_buffinit(L, &b);
2417 luaL_addstring(&b, ip);
2418 luaL_addchar(&b, ':');
2419 luaL_pushresult(&b);
2420 ip = lua_tolstring(L, lua_gettop(L), NULL);
2421 }
2422 }
2423
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002424 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002425 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002426 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002427 lua_pushnil(L);
2428 return 1;
2429 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002430
2431 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002432 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002433 if (!addr) {
2434 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002435 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002436 }
2437 if (low != high) {
2438 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002439 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002440 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002441
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002442 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002443 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002444 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002445 if (port == -1) {
2446 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002447 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002448 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002449 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2450 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002451 if (port == -1) {
2452 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002453 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002454 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002455 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002456 }
2457 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002458
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002459 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2460 si = appctx->owner;
2461 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002462
2463 if (!sockaddr_alloc(&s->target_addr)) {
2464 xref_unlock(&socket->xref, peer);
2465 WILL_LJMP(luaL_error(L, "connect: internal error"));
2466 }
2467 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002468 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002469
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002470 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002471
2472 /* inform the stream that we want to be notified whenever the
2473 * connection completes.
2474 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002475 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002476 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002477 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002478
Willy Tarreauf31af932020-01-14 09:59:38 +01002479 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002480
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002481 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2482 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002483 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002484 }
2485 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002486
2487 task_wakeup(s->task, TASK_WOKEN_INIT);
2488 /* Return yield waiting for connection. */
2489
Willy Tarreau9635e032018-10-16 17:52:55 +02002490 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002491
2492 return 0;
2493}
2494
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002495#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002496__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2497{
2498 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002499 struct xref *peer;
2500 struct appctx *appctx;
2501 struct stream_interface *si;
2502 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002503
2504 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2505 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002506
2507 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002508 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002509 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002510 lua_pushnil(L);
2511 return 1;
2512 }
2513 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2514 si = appctx->owner;
2515 s = si_strm(si);
2516
2517 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002518 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002519 return MAY_LJMP(hlua_socket_connect(L));
2520}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002521#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002522
2523__LJMP static int hlua_socket_setoption(struct lua_State *L)
2524{
2525 return 0;
2526}
2527
2528__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2529{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002530 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002531 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002532 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002533 struct xref *peer;
2534 struct appctx *appctx;
2535 struct stream_interface *si;
2536 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002537
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002538 MAY_LJMP(check_args(L, 2, "settimeout"));
2539
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002540 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002541
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002542 /* convert the timeout to millis */
2543 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002544
Thierry Fournier17a921b2018-03-08 09:59:02 +01002545 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002546 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002547 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2548
Mark Lakes56cc1252018-03-27 09:48:06 +02002549 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002550 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002551
2552 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002553 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002554 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002555
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002556 /* Check if we run on the same thread than the xreator thread.
2557 * We cannot access to the socket if the thread is different.
2558 */
2559 if (socket->tid != tid)
2560 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2561
Mark Lakes56cc1252018-03-27 09:48:06 +02002562 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002563 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002564 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002565 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2566 WILL_LJMP(lua_error(L));
2567 return 0;
2568 }
2569 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2570 si = appctx->owner;
2571 s = si_strm(si);
2572
Cyril Bonté7bb63452018-08-17 23:51:02 +02002573 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002574 s->req.rto = tmout;
2575 s->req.wto = tmout;
2576 s->res.rto = tmout;
2577 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002578 s->req.rex = tick_add_ifset(now_ms, tmout);
2579 s->req.wex = tick_add_ifset(now_ms, tmout);
2580 s->res.rex = tick_add_ifset(now_ms, tmout);
2581 s->res.wex = tick_add_ifset(now_ms, tmout);
2582
2583 s->task->expire = tick_add_ifset(now_ms, tmout);
2584 task_queue(s->task);
2585
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002586 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002587
Thierry Fourniere9636f12018-03-08 09:54:32 +01002588 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002589 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002590}
2591
2592__LJMP static int hlua_socket_new(lua_State *L)
2593{
2594 struct hlua_socket *socket;
2595 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002596 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002597 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002598
2599 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002600 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002601 hlua_pusherror(L, "socket: full stack");
2602 goto out_fail_conf;
2603 }
2604
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002605 /* Create the object: obj[0] = userdata. */
2606 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002607 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002608 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002609 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002610 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002611
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002612 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002613 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002614 hlua_pusherror(L, "socket: uninitialized pools.");
2615 goto out_fail_conf;
2616 }
2617
Willy Tarreau87b09662015-04-03 00:22:06 +02002618 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002619 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2620 lua_setmetatable(L, -2);
2621
Willy Tarreaud420a972015-04-06 00:39:18 +02002622 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002623 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002624 if (!appctx) {
2625 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002626 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002627 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002628
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002629 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002630 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002631 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2632 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002633
Willy Tarreaud420a972015-04-06 00:39:18 +02002634 /* Now create a session, task and stream for this applet */
2635 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2636 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002637 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002638 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002639 }
2640
Willy Tarreau87787ac2017-08-28 16:22:54 +02002641 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002642 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002643 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002644 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002645 }
2646
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002647 /* Initialise cross reference between stream and Lua socket object. */
2648 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002649
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002650 /* Configure "right" stream interface. this "si" is used to connect
2651 * and retrieve data from the server. The connection is initialized
2652 * with the "struct server".
2653 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002654 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002655
2656 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002657 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002658 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002659
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002660 return 1;
2661
Willy Tarreaud420a972015-04-06 00:39:18 +02002662 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002663 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002664 out_fail_sess:
2665 appctx_free(appctx);
2666 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002667 WILL_LJMP(lua_error(L));
2668 return 0;
2669}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002670
2671/*
2672 *
2673 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002674 * Class Channel
2675 *
2676 *
2677 */
2678
2679/* Returns the struct hlua_channel join to the class channel in the
2680 * stack entry "ud" or throws an argument error.
2681 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002682__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002683{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002684 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002685}
2686
Willy Tarreau47860ed2015-03-10 14:07:50 +01002687/* Pushes the channel onto the top of the stack. If the stask does not have a
2688 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002689 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002690static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002691{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002692 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002693 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002694 return 0;
2695
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002696 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002697 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002698 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002699
2700 /* Pop a class sesison metatable and affect it to the userdata. */
2701 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2702 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002703 return 1;
2704}
2705
2706/* Duplicate all the data present in the input channel and put it
2707 * in a string LUA variables. Returns -1 and push a nil value in
2708 * the stack if the channel is closed and all the data are consumed,
2709 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002710 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002711 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002712static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002713{
2714 char *blk1;
2715 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002716 size_t len1;
2717 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002718 int ret;
2719 luaL_Buffer b;
2720
Willy Tarreau06d80a92017-10-19 14:32:15 +02002721 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002722 if (unlikely(ret == 0))
2723 return 0;
2724
2725 if (unlikely(ret < 0)) {
2726 lua_pushnil(L);
2727 return -1;
2728 }
2729
2730 luaL_buffinit(L, &b);
2731 luaL_addlstring(&b, blk1, len1);
2732 if (unlikely(ret == 2))
2733 luaL_addlstring(&b, blk2, len2);
2734 luaL_pushresult(&b);
2735
2736 if (unlikely(ret == 2))
2737 return len1 + len2;
2738 return len1;
2739}
2740
2741/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2742 * a yield. This function keep the data in the buffer.
2743 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002744__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002745{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002746 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002747
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002748 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2749
Christopher Faulet3f829a42018-12-13 21:56:45 +01002750 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2751 WILL_LJMP(lua_error(L));
2752
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002753 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002754 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002755 return 1;
2756}
2757
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002758/* Check arguments for the function "hlua_channel_dup_yield". */
2759__LJMP static int hlua_channel_dup(lua_State *L)
2760{
2761 MAY_LJMP(check_args(L, 1, "dup"));
2762 MAY_LJMP(hlua_checkchannel(L, 1));
2763 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2764}
2765
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002766/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2767 * a yield. This function consumes the data in the buffer. It returns
2768 * a string containing the data or a nil pointer if no data are available
2769 * and the channel is closed.
2770 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002771__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002772{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002773 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002774 int ret;
2775
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002776 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002777
Christopher Faulet3f829a42018-12-13 21:56:45 +01002778 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2779 WILL_LJMP(lua_error(L));
2780
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002781 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002782 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002783 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002784
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002785 if (unlikely(ret == -1))
2786 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002787
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002788 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002789 return 1;
2790}
2791
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002792/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002793__LJMP static int hlua_channel_get(lua_State *L)
2794{
2795 MAY_LJMP(check_args(L, 1, "get"));
2796 MAY_LJMP(hlua_checkchannel(L, 1));
2797 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2798}
2799
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002800/* This functions consumes and returns one line. If the channel is closed,
2801 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002802 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002803 * value.
2804 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002805__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002806{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002807 char *blk1;
2808 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002809 size_t len1;
2810 size_t len2;
2811 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002812 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002813 int ret;
2814 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002815
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002816 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2817
Christopher Faulet3f829a42018-12-13 21:56:45 +01002818 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2819 WILL_LJMP(lua_error(L));
2820
Willy Tarreau06d80a92017-10-19 14:32:15 +02002821 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002822 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002823 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002824
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002825 if (ret == -1) {
2826 lua_pushnil(L);
2827 return 1;
2828 }
2829
2830 luaL_buffinit(L, &b);
2831 luaL_addlstring(&b, blk1, len1);
2832 len = len1;
2833 if (unlikely(ret == 2)) {
2834 luaL_addlstring(&b, blk2, len2);
2835 len += len2;
2836 }
2837 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002838 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002839 return 1;
2840}
2841
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002842/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002843__LJMP static int hlua_channel_getline(lua_State *L)
2844{
2845 MAY_LJMP(check_args(L, 1, "getline"));
2846 MAY_LJMP(hlua_checkchannel(L, 1));
2847 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2848}
2849
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002850/* This function takes a string as input, and append it at the
2851 * input side of channel. If the data is too big, but a space
2852 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002853 * yields. If the data is bigger than the buffer, or if the
2854 * channel is closed, it returns -1. Otherwise, it returns the
2855 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002856 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002857__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002858{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002859 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002860 size_t len;
2861 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2862 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2863 int ret;
2864 int max;
2865
Christopher Faulet3f829a42018-12-13 21:56:45 +01002866 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2867 WILL_LJMP(lua_error(L));
2868
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002869 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002870 * the request buffer if its not required.
2871 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002872 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002873 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002874 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002875 }
2876
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002877 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002878 if (max > len - l)
2879 max = len - l;
2880
Willy Tarreau06d80a92017-10-19 14:32:15 +02002881 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002882 if (ret == -2 || ret == -3) {
2883 lua_pushinteger(L, -1);
2884 return 1;
2885 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002886 if (ret == -1) {
2887 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002888 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002889 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002890 l += ret;
2891 lua_pop(L, 1);
2892 lua_pushinteger(L, l);
2893
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002894 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002895 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002896 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002898 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002899 */
2900 return 1;
2901 }
2902 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002903 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002904 return 1;
2905}
2906
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002907/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2908 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002909 * buffer size is too little for the data.
2910 */
2911__LJMP static int hlua_channel_append(lua_State *L)
2912{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002913 size_t len;
2914
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002915 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002916 MAY_LJMP(hlua_checkchannel(L, 1));
2917 MAY_LJMP(luaL_checklstring(L, 2, &len));
2918 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002919 lua_pushinteger(L, 0);
2920
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002921 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002922}
2923
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002924/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002925 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002926 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002927 * or -1 if the channel is closed or if the buffer size is too
2928 * little for the data.
2929 */
2930__LJMP static int hlua_channel_set(lua_State *L)
2931{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002932 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002933
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002934 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002935 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002936 lua_pushinteger(L, 0);
2937
Christopher Faulet3f829a42018-12-13 21:56:45 +01002938 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2939 WILL_LJMP(lua_error(L));
2940
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002941 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002942
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002943 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002944}
2945
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002946/* Append data in the output side of the buffer. This data is immediately
2947 * sent. The function returns the amount of data written. If the buffer
2948 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002949 * if the channel is closed.
2950 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002951__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002952{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002953 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002954 size_t len;
2955 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2956 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2957 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002958 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002959
Christopher Faulet3f829a42018-12-13 21:56:45 +01002960 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2961 WILL_LJMP(lua_error(L));
2962
Willy Tarreau47860ed2015-03-10 14:07:50 +01002963 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002964 lua_pushinteger(L, -1);
2965 return 1;
2966 }
2967
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002968 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002969 * the request buffer if its not required.
2970 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002971 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002972 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002973 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002974 }
2975
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002976 /* The written data will be immediately sent, so we can check
2977 * the available space without taking in account the reserve.
2978 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002979 * data, because the buffer will be flushed.
2980 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002981 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002982
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002983 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002984 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002985 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002986 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02002987 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002988 return 1;
2989
2990 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002991 if (max > len - l)
2992 max = len - l;
2993
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002994 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002995 * detects a non contiguous buffer and realign it.
2996 */
Willy Tarreau3f679992018-06-15 15:06:42 +02002997 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002998 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002999
3000 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003001 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003002
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003003 /* buffer replace considers that the input part is filled.
3004 * so, I must forward these new data in the output part.
3005 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003006 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003007
3008 l += max;
3009 lua_pop(L, 1);
3010 lua_pushinteger(L, l);
3011
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003012 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003013 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003014 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003015 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003016 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003017 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003018 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003019
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003020 if (l < len) {
3021 /* If we are waiting for space in the response buffer, we
3022 * must set the flag WAKERESWR. This flag required the task
3023 * wake up if any activity is detected on the response buffer.
3024 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003025 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003026 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003027 else
3028 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003029 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003030 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031
3032 return 1;
3033}
3034
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003035/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003036 * yield the LUA process, and resume it without checking the
3037 * input arguments.
3038 */
3039__LJMP static int hlua_channel_send(lua_State *L)
3040{
3041 MAY_LJMP(check_args(L, 2, "send"));
3042 lua_pushinteger(L, 0);
3043
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003044 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003045}
3046
3047/* This function forward and amount of butes. The data pass from
3048 * the input side of the buffer to the output side, and can be
3049 * forwarded. This function never fails.
3050 *
3051 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003052 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003053 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003054__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003055{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003056 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003057 int len;
3058 int l;
3059 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003060 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003061
3062 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003063
3064 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3065 WILL_LJMP(lua_error(L));
3066
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003067 len = MAY_LJMP(luaL_checkinteger(L, 2));
3068 l = MAY_LJMP(luaL_checkinteger(L, -1));
3069
3070 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003071 if (max > ci_data(chn))
3072 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003073 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003074 l += max;
3075
3076 lua_pop(L, 1);
3077 lua_pushinteger(L, l);
3078
3079 /* Check if it miss bytes to forward. */
3080 if (l < len) {
3081 /* The the input channel or the output channel are closed, we
3082 * must return the amount of data forwarded.
3083 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003084 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003085 return 1;
3086
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003087 /* If we are waiting for space data in the response buffer, we
3088 * must set the flag WAKERESWR. This flag required the task
3089 * wake up if any activity is detected on the response buffer.
3090 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003091 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003092 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003093 else
3094 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003095
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003096 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003097 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003098 }
3099
3100 return 1;
3101}
3102
3103/* Just check the input and prepare the stack for the previous
3104 * function "hlua_channel_forward_yield"
3105 */
3106__LJMP static int hlua_channel_forward(lua_State *L)
3107{
3108 MAY_LJMP(check_args(L, 2, "forward"));
3109 MAY_LJMP(hlua_checkchannel(L, 1));
3110 MAY_LJMP(luaL_checkinteger(L, 2));
3111
3112 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003113 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003114}
3115
3116/* Just returns the number of bytes available in the input
3117 * side of the buffer. This function never fails.
3118 */
3119__LJMP static int hlua_channel_get_in_len(lua_State *L)
3120{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003121 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003122
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003123 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003124 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003125 if (IS_HTX_STRM(chn_strm(chn))) {
3126 struct htx *htx = htxbuf(&chn->buf);
3127 lua_pushinteger(L, htx->data - co_data(chn));
3128 }
3129 else
3130 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003131 return 1;
3132}
3133
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003134/* Returns true if the channel is full. */
3135__LJMP static int hlua_channel_is_full(lua_State *L)
3136{
3137 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003138
3139 MAY_LJMP(check_args(L, 1, "is_full"));
3140 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003141 /* ignore the reserve, we are not on a producer side (ie in an
3142 * applet).
3143 */
3144 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003145 return 1;
3146}
3147
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003148/* Returns true if the channel is the response channel. */
3149__LJMP static int hlua_channel_is_resp(lua_State *L)
3150{
3151 struct channel *chn;
3152
3153 MAY_LJMP(check_args(L, 1, "is_resp"));
3154 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3155
3156 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3157 return 1;
3158}
3159
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003160/* Just returns the number of bytes available in the output
3161 * side of the buffer. This function never fails.
3162 */
3163__LJMP static int hlua_channel_get_out_len(lua_State *L)
3164{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003165 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003166
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003167 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003168 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003169 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003170 return 1;
3171}
3172
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003173/*
3174 *
3175 *
3176 * Class Fetches
3177 *
3178 *
3179 */
3180
3181/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003182 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003183 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003184__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003185{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003186 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003187}
3188
3189/* This function creates and push in the stack a fetch object according
3190 * with a current TXN.
3191 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003192static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003193{
Willy Tarreau7073c472015-04-06 11:15:40 +02003194 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003195
3196 /* Check stack size. */
3197 if (!lua_checkstack(L, 3))
3198 return 0;
3199
3200 /* Create the object: obj[0] = userdata.
3201 * Note that the base of the Fetches object is the
3202 * transaction object.
3203 */
3204 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003205 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003206 lua_rawseti(L, -2, 0);
3207
Willy Tarreau7073c472015-04-06 11:15:40 +02003208 hsmp->s = txn->s;
3209 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003210 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003211 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003212
3213 /* Pop a class sesison metatable and affect it to the userdata. */
3214 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3215 lua_setmetatable(L, -2);
3216
3217 return 1;
3218}
3219
3220/* This function is an LUA binding. It is called with each sample-fetch.
3221 * It uses closure argument to store the associated sample-fetch. It
3222 * returns only one argument or throws an error. An error is thrown
3223 * only if an error is encountered during the argument parsing. If
3224 * the "sample-fetch" function fails, nil is returned.
3225 */
3226__LJMP static int hlua_run_sample_fetch(lua_State *L)
3227{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003228 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003229 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003230 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003231 int i;
3232 struct sample smp;
3233
3234 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003235 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003236
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003237 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003238 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003239
Thierry FOURNIERca988662015-12-20 18:43:03 +01003240 /* Check execution authorization. */
3241 if (f->use & SMP_USE_HTTP_ANY &&
3242 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3243 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3244 "is not available in Lua services", f->kw);
3245 WILL_LJMP(lua_error(L));
3246 }
3247
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003248 /* Get extra arguments. */
3249 for (i = 0; i < lua_gettop(L) - 1; i++) {
3250 if (i >= ARGM_NBARGS)
3251 break;
3252 hlua_lua2arg(L, i + 2, &args[i]);
3253 }
3254 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003255 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003256
3257 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003258 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003259
3260 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003261 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003262 lua_pushfstring(L, "error in arguments");
3263 WILL_LJMP(lua_error(L));
3264 }
3265
3266 /* Initialise the sample. */
3267 memset(&smp, 0, sizeof(smp));
3268
3269 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003270 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003271 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003272 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003273 lua_pushstring(L, "");
3274 else
3275 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003276 return 1;
3277 }
3278
3279 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003280 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003281 hlua_smp2lua_str(L, &smp);
3282 else
3283 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003284 return 1;
3285}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003286
3287/*
3288 *
3289 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003290 * Class Converters
3291 *
3292 *
3293 */
3294
3295/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003296 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003297 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003298__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003299{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003300 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003301}
3302
3303/* This function creates and push in the stack a Converters object
3304 * according with a current TXN.
3305 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003306static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003307{
Willy Tarreau7073c472015-04-06 11:15:40 +02003308 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003309
3310 /* Check stack size. */
3311 if (!lua_checkstack(L, 3))
3312 return 0;
3313
3314 /* Create the object: obj[0] = userdata.
3315 * Note that the base of the Converters object is the
3316 * same than the TXN object.
3317 */
3318 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003319 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003320 lua_rawseti(L, -2, 0);
3321
Willy Tarreau7073c472015-04-06 11:15:40 +02003322 hsmp->s = txn->s;
3323 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003324 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003325 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003326
Willy Tarreau87b09662015-04-03 00:22:06 +02003327 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003328 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3329 lua_setmetatable(L, -2);
3330
3331 return 1;
3332}
3333
3334/* This function is an LUA binding. It is called with each converter.
3335 * It uses closure argument to store the associated converter. It
3336 * returns only one argument or throws an error. An error is thrown
3337 * only if an error is encountered during the argument parsing. If
3338 * the converter function function fails, nil is returned.
3339 */
3340__LJMP static int hlua_run_sample_conv(lua_State *L)
3341{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003342 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003343 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003344 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003345 int i;
3346 struct sample smp;
3347
3348 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003349 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003350
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003351 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003352 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003353
3354 /* Get extra arguments. */
3355 for (i = 0; i < lua_gettop(L) - 2; i++) {
3356 if (i >= ARGM_NBARGS)
3357 break;
3358 hlua_lua2arg(L, i + 3, &args[i]);
3359 }
3360 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003361 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003362
3363 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003364 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003365
3366 /* Run the special args checker. */
3367 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3368 hlua_pusherror(L, "error in arguments");
3369 WILL_LJMP(lua_error(L));
3370 }
3371
3372 /* Initialise the sample. */
3373 if (!hlua_lua2smp(L, 2, &smp)) {
3374 hlua_pusherror(L, "error in the input argument");
3375 WILL_LJMP(lua_error(L));
3376 }
3377
Willy Tarreau1777ea62016-03-10 16:15:46 +01003378 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3379
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003380 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003381 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003382 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003383 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003384 WILL_LJMP(lua_error(L));
3385 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003386 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3387 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003388 hlua_pusherror(L, "error during the input argument casting");
3389 WILL_LJMP(lua_error(L));
3390 }
3391
3392 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003393 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003394 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003395 lua_pushstring(L, "");
3396 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003397 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003398 return 1;
3399 }
3400
3401 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003402 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003403 hlua_smp2lua_str(L, &smp);
3404 else
3405 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003406 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003407}
3408
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003409/*
3410 *
3411 *
3412 * Class AppletTCP
3413 *
3414 *
3415 */
3416
3417/* Returns a struct hlua_txn if the stack entry "ud" is
3418 * a class stream, otherwise it throws an error.
3419 */
3420__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3421{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003422 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003423}
3424
3425/* This function creates and push in the stack an Applet object
3426 * according with a current TXN.
3427 */
3428static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3429{
3430 struct hlua_appctx *appctx;
3431 struct stream_interface *si = ctx->owner;
3432 struct stream *s = si_strm(si);
3433 struct proxy *p = s->be;
3434
3435 /* Check stack size. */
3436 if (!lua_checkstack(L, 3))
3437 return 0;
3438
3439 /* Create the object: obj[0] = userdata.
3440 * Note that the base of the Converters object is the
3441 * same than the TXN object.
3442 */
3443 lua_newtable(L);
3444 appctx = lua_newuserdata(L, sizeof(*appctx));
3445 lua_rawseti(L, -2, 0);
3446 appctx->appctx = ctx;
3447 appctx->htxn.s = s;
3448 appctx->htxn.p = p;
3449
3450 /* Create the "f" field that contains a list of fetches. */
3451 lua_pushstring(L, "f");
3452 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3453 return 0;
3454 lua_settable(L, -3);
3455
3456 /* Create the "sf" field that contains a list of stringsafe fetches. */
3457 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003458 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003459 return 0;
3460 lua_settable(L, -3);
3461
3462 /* Create the "c" field that contains a list of converters. */
3463 lua_pushstring(L, "c");
3464 if (!hlua_converters_new(L, &appctx->htxn, 0))
3465 return 0;
3466 lua_settable(L, -3);
3467
3468 /* Create the "sc" field that contains a list of stringsafe converters. */
3469 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003470 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003471 return 0;
3472 lua_settable(L, -3);
3473
3474 /* Pop a class stream metatable and affect it to the table. */
3475 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3476 lua_setmetatable(L, -2);
3477
3478 return 1;
3479}
3480
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003481__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3482{
3483 struct hlua_appctx *appctx;
3484 struct stream *s;
3485 const char *name;
3486 size_t len;
3487 struct sample smp;
3488
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003489 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3490 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003491
3492 /* It is useles to retrieve the stream, but this function
3493 * runs only in a stream context.
3494 */
3495 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3496 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3497 s = appctx->htxn.s;
3498
3499 /* Converts the third argument in a sample. */
3500 hlua_lua2smp(L, 3, &smp);
3501
3502 /* Store the sample in a variable. */
3503 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003504
3505 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3506 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3507 else
3508 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3509
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003510 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003511}
3512
3513__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3514{
3515 struct hlua_appctx *appctx;
3516 struct stream *s;
3517 const char *name;
3518 size_t len;
3519 struct sample smp;
3520
3521 MAY_LJMP(check_args(L, 2, "unset_var"));
3522
3523 /* It is useles to retrieve the stream, but this function
3524 * runs only in a stream context.
3525 */
3526 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3527 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3528 s = appctx->htxn.s;
3529
3530 /* Unset the variable. */
3531 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003532 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3533 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003534}
3535
3536__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3537{
3538 struct hlua_appctx *appctx;
3539 struct stream *s;
3540 const char *name;
3541 size_t len;
3542 struct sample smp;
3543
3544 MAY_LJMP(check_args(L, 2, "get_var"));
3545
3546 /* It is useles to retrieve the stream, but this function
3547 * runs only in a stream context.
3548 */
3549 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3550 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3551 s = appctx->htxn.s;
3552
3553 smp_set_owner(&smp, s->be, s->sess, s, 0);
3554 if (!vars_get_by_name(name, len, &smp)) {
3555 lua_pushnil(L);
3556 return 1;
3557 }
3558
3559 return hlua_smp2lua(L, &smp);
3560}
3561
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003562__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3563{
3564 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3565 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003566 struct hlua *hlua;
3567
3568 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003569 if (!s->hlua)
3570 return 0;
3571 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003572
3573 MAY_LJMP(check_args(L, 2, "set_priv"));
3574
3575 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003576 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003577
3578 /* Get and store new value. */
3579 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3580 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3581
3582 return 0;
3583}
3584
3585__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3586{
3587 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3588 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003589 struct hlua *hlua;
3590
3591 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003592 if (!s->hlua) {
3593 lua_pushnil(L);
3594 return 1;
3595 }
3596 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003597
3598 /* Push configuration index in the stack. */
3599 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3600
3601 return 1;
3602}
3603
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003604/* If expected data not yet available, it returns a yield. This function
3605 * consumes the data in the buffer. It returns a string containing the
3606 * data. This string can be empty.
3607 */
3608__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3609{
3610 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3611 struct stream_interface *si = appctx->appctx->owner;
3612 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003613 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003614 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003615 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003616 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003617
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003618 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003619 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003620
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003621 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003622 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003623 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003624 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003625 }
3626
3627 /* End of data: commit the total strings and return. */
3628 if (ret < 0) {
3629 luaL_pushresult(&appctx->b);
3630 return 1;
3631 }
3632
3633 /* Ensure that the block 2 length is usable. */
3634 if (ret == 1)
3635 len2 = 0;
3636
3637 /* dont check the max length read and dont check. */
3638 luaL_addlstring(&appctx->b, blk1, len1);
3639 luaL_addlstring(&appctx->b, blk2, len2);
3640
3641 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003642 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003643 luaL_pushresult(&appctx->b);
3644 return 1;
3645}
3646
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003647/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003648__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3649{
3650 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3651
3652 /* Initialise the string catenation. */
3653 luaL_buffinit(L, &appctx->b);
3654
3655 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3656}
3657
3658/* If expected data not yet available, it returns a yield. This function
3659 * consumes the data in the buffer. It returns a string containing the
3660 * data. This string can be empty.
3661 */
3662__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3663{
3664 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3665 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003666 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003667 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003668 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003669 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003670 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003671 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003672
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003673 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003674 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003675
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003676 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003677 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003678 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003679 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003680 }
3681
3682 /* End of data: commit the total strings and return. */
3683 if (ret < 0) {
3684 luaL_pushresult(&appctx->b);
3685 return 1;
3686 }
3687
3688 /* Ensure that the block 2 length is usable. */
3689 if (ret == 1)
3690 len2 = 0;
3691
3692 if (len == -1) {
3693
3694 /* If len == -1, catenate all the data avalaile and
3695 * yield because we want to get all the data until
3696 * the end of data stream.
3697 */
3698 luaL_addlstring(&appctx->b, blk1, len1);
3699 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003700 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003701 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003702 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003703
3704 } else {
3705
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003706 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003707 if (len1 > len)
3708 len1 = len;
3709 luaL_addlstring(&appctx->b, blk1, len1);
3710 len -= len1;
3711
3712 /* Copy the second block. */
3713 if (len2 > len)
3714 len2 = len;
3715 luaL_addlstring(&appctx->b, blk2, len2);
3716 len -= len2;
3717
3718 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003719 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003720
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003721 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003722 if (len > 0) {
3723 lua_pushinteger(L, len);
3724 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003725 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003726 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003727 }
3728
3729 /* return the result. */
3730 luaL_pushresult(&appctx->b);
3731 return 1;
3732 }
3733
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003734 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003735 hlua_pusherror(L, "Lua: internal error");
3736 WILL_LJMP(lua_error(L));
3737 return 0;
3738}
3739
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003740/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003741__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3742{
3743 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3744 int len = -1;
3745
3746 if (lua_gettop(L) > 2)
3747 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3748 if (lua_gettop(L) >= 2) {
3749 len = MAY_LJMP(luaL_checkinteger(L, 2));
3750 lua_pop(L, 1);
3751 }
3752
3753 /* Confirm or set the required length */
3754 lua_pushinteger(L, len);
3755
3756 /* Initialise the string catenation. */
3757 luaL_buffinit(L, &appctx->b);
3758
3759 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3760}
3761
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003762/* Append data in the output side of the buffer. This data is immediately
3763 * sent. The function returns the amount of data written. If the buffer
3764 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003765 * if the channel is closed.
3766 */
3767__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3768{
3769 size_t len;
3770 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3771 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3772 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3773 struct stream_interface *si = appctx->appctx->owner;
3774 struct channel *chn = si_ic(si);
3775 int max;
3776
3777 /* Get the max amount of data which can write as input in the channel. */
3778 max = channel_recv_max(chn);
3779 if (max > (len - l))
3780 max = len - l;
3781
3782 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003783 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003784
3785 /* update counters. */
3786 l += max;
3787 lua_pop(L, 1);
3788 lua_pushinteger(L, l);
3789
3790 /* If some data is not send, declares the situation to the
3791 * applet, and returns a yield.
3792 */
3793 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003794 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003795 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003796 }
3797
3798 return 1;
3799}
3800
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003801/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003802 * yield the LUA process, and resume it without checking the
3803 * input arguments.
3804 */
3805__LJMP static int hlua_applet_tcp_send(lua_State *L)
3806{
3807 MAY_LJMP(check_args(L, 2, "send"));
3808 lua_pushinteger(L, 0);
3809
3810 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3811}
3812
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003813/*
3814 *
3815 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003816 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003817 *
3818 *
3819 */
3820
3821/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003822 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003823 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003824__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003825{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003826 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003827}
3828
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003829/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003830 * according with a current TXN.
3831 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003832static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003833{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003834 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003835 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003836 struct stream_interface *si = ctx->owner;
3837 struct stream *s = si_strm(si);
3838 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003839 struct htx *htx;
3840 struct htx_blk *blk;
3841 struct htx_sl *sl;
3842 struct ist path;
3843 unsigned long long len = 0;
3844 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003845
3846 /* Check stack size. */
3847 if (!lua_checkstack(L, 3))
3848 return 0;
3849
3850 /* Create the object: obj[0] = userdata.
3851 * Note that the base of the Converters object is the
3852 * same than the TXN object.
3853 */
3854 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003855 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003856 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003857 appctx->appctx = ctx;
3858 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003859 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003860 appctx->htxn.s = s;
3861 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003862
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003863 /* Create the "f" field that contains a list of fetches. */
3864 lua_pushstring(L, "f");
3865 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3866 return 0;
3867 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003868
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003869 /* Create the "sf" field that contains a list of stringsafe fetches. */
3870 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003871 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003872 return 0;
3873 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003874
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003875 /* Create the "c" field that contains a list of converters. */
3876 lua_pushstring(L, "c");
3877 if (!hlua_converters_new(L, &appctx->htxn, 0))
3878 return 0;
3879 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003880
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003881 /* Create the "sc" field that contains a list of stringsafe converters. */
3882 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003883 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003884 return 0;
3885 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003886
Christopher Fauleta2097962019-07-15 16:25:33 +02003887 htx = htxbuf(&s->req.buf);
3888 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003889 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003890 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003891
Christopher Fauleta2097962019-07-15 16:25:33 +02003892 /* Stores the request method. */
3893 lua_pushstring(L, "method");
3894 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3895 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003896
Christopher Fauleta2097962019-07-15 16:25:33 +02003897 /* Stores the http version. */
3898 lua_pushstring(L, "version");
3899 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3900 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003901
Christopher Fauleta2097962019-07-15 16:25:33 +02003902 /* creates an array of headers. hlua_http_get_headers() crates and push
3903 * the array on the top of the stack.
3904 */
3905 lua_pushstring(L, "headers");
3906 htxn.s = s;
3907 htxn.p = px;
3908 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01003909 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02003910 return 0;
3911 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003912
Christopher Fauleta2097962019-07-15 16:25:33 +02003913 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01003914 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02003915 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003916
Christopher Fauleta2097962019-07-15 16:25:33 +02003917 p = path.ptr;
3918 end = path.ptr + path.len;
3919 q = p;
3920 while (q < end && *q != '?')
3921 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003922
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003923 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003924 lua_pushstring(L, "path");
3925 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003926 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003927
Christopher Fauleta2097962019-07-15 16:25:33 +02003928 /* Stores the query string. */
3929 lua_pushstring(L, "qs");
3930 if (*q == '?')
3931 q++;
3932 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003933 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003934 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003935
Christopher Fauleta2097962019-07-15 16:25:33 +02003936 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3937 struct htx_blk *blk = htx_get_blk(htx, pos);
3938 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003939
Christopher Fauleta2097962019-07-15 16:25:33 +02003940 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3941 break;
3942 if (type == HTX_BLK_DATA)
3943 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003944 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003945 if (htx->extra != ULLONG_MAX)
3946 len += htx->extra;
3947
3948 /* Stores the request path. */
3949 lua_pushstring(L, "length");
3950 lua_pushinteger(L, len);
3951 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003952
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003953 /* Create an empty array of HTTP request headers. */
3954 lua_pushstring(L, "response");
3955 lua_newtable(L);
3956 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003957
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003958 /* Pop a class stream metatable and affect it to the table. */
3959 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3960 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003961
3962 return 1;
3963}
3964
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003965__LJMP static int hlua_applet_http_set_var(lua_State *L)
3966{
3967 struct hlua_appctx *appctx;
3968 struct stream *s;
3969 const char *name;
3970 size_t len;
3971 struct sample smp;
3972
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003973 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3974 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003975
3976 /* It is useles to retrieve the stream, but this function
3977 * runs only in a stream context.
3978 */
3979 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3980 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3981 s = appctx->htxn.s;
3982
3983 /* Converts the third argument in a sample. */
3984 hlua_lua2smp(L, 3, &smp);
3985
3986 /* Store the sample in a variable. */
3987 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003988
3989 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3990 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3991 else
3992 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3993
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003994 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003995}
3996
3997__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3998{
3999 struct hlua_appctx *appctx;
4000 struct stream *s;
4001 const char *name;
4002 size_t len;
4003 struct sample smp;
4004
4005 MAY_LJMP(check_args(L, 2, "unset_var"));
4006
4007 /* It is useles to retrieve the stream, but this function
4008 * runs only in a stream context.
4009 */
4010 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4011 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4012 s = appctx->htxn.s;
4013
4014 /* Unset the variable. */
4015 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004016 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4017 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004018}
4019
4020__LJMP static int hlua_applet_http_get_var(lua_State *L)
4021{
4022 struct hlua_appctx *appctx;
4023 struct stream *s;
4024 const char *name;
4025 size_t len;
4026 struct sample smp;
4027
4028 MAY_LJMP(check_args(L, 2, "get_var"));
4029
4030 /* It is useles to retrieve the stream, but this function
4031 * runs only in a stream context.
4032 */
4033 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4034 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4035 s = appctx->htxn.s;
4036
4037 smp_set_owner(&smp, s->be, s->sess, s, 0);
4038 if (!vars_get_by_name(name, len, &smp)) {
4039 lua_pushnil(L);
4040 return 1;
4041 }
4042
4043 return hlua_smp2lua(L, &smp);
4044}
4045
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004046__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4047{
4048 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4049 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004050 struct hlua *hlua;
4051
4052 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004053 if (!s->hlua)
4054 return 0;
4055 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004056
4057 MAY_LJMP(check_args(L, 2, "set_priv"));
4058
4059 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004060 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004061
4062 /* Get and store new value. */
4063 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4064 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4065
4066 return 0;
4067}
4068
4069__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4070{
4071 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4072 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004073 struct hlua *hlua;
4074
4075 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004076 if (!s->hlua) {
4077 lua_pushnil(L);
4078 return 1;
4079 }
4080 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004081
4082 /* Push configuration index in the stack. */
4083 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4084
4085 return 1;
4086}
4087
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004088/* If expected data not yet available, it returns a yield. This function
4089 * consumes the data in the buffer. It returns a string containing the
4090 * data. This string can be empty.
4091 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004092__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004093{
4094 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4095 struct stream_interface *si = appctx->appctx->owner;
4096 struct channel *req = si_oc(si);
4097 struct htx *htx;
4098 struct htx_blk *blk;
4099 size_t count;
4100 int stop = 0;
4101
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004102 htx = htx_from_buf(&req->buf);
4103 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004104 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004105
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004106 while (count && !stop && blk) {
4107 enum htx_blk_type type = htx_get_blk_type(blk);
4108 uint32_t sz = htx_get_blksz(blk);
4109 struct ist v;
4110 uint32_t vlen;
4111 char *nl;
4112
Christopher Faulete461e342018-12-18 16:43:35 +01004113 if (type == HTX_BLK_EOM) {
4114 stop = 1;
4115 break;
4116 }
4117
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004118 vlen = sz;
4119 if (vlen > count) {
4120 if (type != HTX_BLK_DATA)
4121 break;
4122 vlen = count;
4123 }
4124
4125 switch (type) {
4126 case HTX_BLK_UNUSED:
4127 break;
4128
4129 case HTX_BLK_DATA:
4130 v = htx_get_blk_value(htx, blk);
4131 v.len = vlen;
4132 nl = istchr(v, '\n');
4133 if (nl != NULL) {
4134 stop = 1;
4135 vlen = nl - v.ptr + 1;
4136 }
4137 luaL_addlstring(&appctx->b, v.ptr, vlen);
4138 break;
4139
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004140 case HTX_BLK_TLR:
4141 case HTX_BLK_EOM:
4142 stop = 1;
4143 break;
4144
4145 default:
4146 break;
4147 }
4148
4149 co_set_data(req, co_data(req) - vlen);
4150 count -= vlen;
4151 if (sz == vlen)
4152 blk = htx_remove_blk(htx, blk);
4153 else {
4154 htx_cut_data_blk(htx, blk, vlen);
4155 break;
4156 }
4157 }
4158
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004159 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004160 if (!stop) {
4161 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004162 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004163 }
4164
4165 /* return the result. */
4166 luaL_pushresult(&appctx->b);
4167 return 1;
4168}
4169
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004170
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004171/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004172__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004173{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004174 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004175
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004176 /* Initialise the string catenation. */
4177 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004178
Christopher Fauleta2097962019-07-15 16:25:33 +02004179 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004180}
4181
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004182/* If expected data not yet available, it returns a yield. This function
4183 * consumes the data in the buffer. It returns a string containing the
4184 * data. This string can be empty.
4185 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004186__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004187{
4188 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4189 struct stream_interface *si = appctx->appctx->owner;
4190 struct channel *req = si_oc(si);
4191 struct htx *htx;
4192 struct htx_blk *blk;
4193 size_t count;
4194 int len;
4195
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004196 htx = htx_from_buf(&req->buf);
4197 len = MAY_LJMP(luaL_checkinteger(L, 2));
4198 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004199 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004200 while (count && len && blk) {
4201 enum htx_blk_type type = htx_get_blk_type(blk);
4202 uint32_t sz = htx_get_blksz(blk);
4203 struct ist v;
4204 uint32_t vlen;
4205
Christopher Faulete461e342018-12-18 16:43:35 +01004206 if (type == HTX_BLK_EOM) {
4207 len = 0;
4208 break;
4209 }
4210
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004211 vlen = sz;
4212 if (len > 0 && vlen > len)
4213 vlen = len;
4214 if (vlen > count) {
4215 if (type != HTX_BLK_DATA)
4216 break;
4217 vlen = count;
4218 }
4219
4220 switch (type) {
4221 case HTX_BLK_UNUSED:
4222 break;
4223
4224 case HTX_BLK_DATA:
4225 v = htx_get_blk_value(htx, blk);
4226 luaL_addlstring(&appctx->b, v.ptr, vlen);
4227 break;
4228
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004229 case HTX_BLK_TLR:
4230 case HTX_BLK_EOM:
4231 len = 0;
4232 break;
4233
4234 default:
4235 break;
4236 }
4237
4238 co_set_data(req, co_data(req) - vlen);
4239 count -= vlen;
4240 if (len > 0)
4241 len -= vlen;
4242 if (sz == vlen)
4243 blk = htx_remove_blk(htx, blk);
4244 else {
4245 htx_cut_data_blk(htx, blk, vlen);
4246 break;
4247 }
4248 }
4249
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004250 htx_to_buf(htx, &req->buf);
4251
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004252 /* If we are no other data available, yield waiting for new data. */
4253 if (len) {
4254 if (len > 0) {
4255 lua_pushinteger(L, len);
4256 lua_replace(L, 2);
4257 }
4258 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004259 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004260 }
4261
4262 /* return the result. */
4263 luaL_pushresult(&appctx->b);
4264 return 1;
4265}
4266
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004267/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004268__LJMP static int hlua_applet_http_recv(lua_State *L)
4269{
4270 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4271 int len = -1;
4272
4273 /* Check arguments. */
4274 if (lua_gettop(L) > 2)
4275 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4276 if (lua_gettop(L) >= 2) {
4277 len = MAY_LJMP(luaL_checkinteger(L, 2));
4278 lua_pop(L, 1);
4279 }
4280
Christopher Fauleta2097962019-07-15 16:25:33 +02004281 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004282
Christopher Fauleta2097962019-07-15 16:25:33 +02004283 /* Initialise the string catenation. */
4284 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004285
Christopher Fauleta2097962019-07-15 16:25:33 +02004286 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004287}
4288
4289/* Append data in the output side of the buffer. This data is immediately
4290 * sent. The function returns the amount of data written. If the buffer
4291 * cannot contain the data, the function yields. The function returns -1
4292 * if the channel is closed.
4293 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004294__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004295{
4296 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4297 struct stream_interface *si = appctx->appctx->owner;
4298 struct channel *res = si_ic(si);
4299 struct htx *htx = htx_from_buf(&res->buf);
4300 const char *data;
4301 size_t len;
4302 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4303 int max;
4304
Christopher Faulet9060fc02019-07-03 11:39:30 +02004305 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004306 if (!max)
4307 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004308
4309 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4310
4311 /* Get the max amount of data which can write as input in the channel. */
4312 if (max > (len - l))
4313 max = len - l;
4314
4315 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004316 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004317 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004318
4319 /* update counters. */
4320 l += max;
4321 lua_pop(L, 1);
4322 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004323
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004324 /* If some data is not send, declares the situation to the
4325 * applet, and returns a yield.
4326 */
4327 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004328 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004329 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004330 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004331 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004332 }
4333
Christopher Fauleta2097962019-07-15 16:25:33 +02004334 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004335 return 1;
4336}
4337
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004338/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004339 * yield the LUA process, and resume it without checking the
4340 * input arguments.
4341 */
4342__LJMP static int hlua_applet_http_send(lua_State *L)
4343{
4344 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004345
4346 /* We want to send some data. Headers must be sent. */
4347 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4348 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4349 WILL_LJMP(lua_error(L));
4350 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004351
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004352 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004353 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004354
Christopher Fauleta2097962019-07-15 16:25:33 +02004355 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004356}
4357
4358__LJMP static int hlua_applet_http_addheader(lua_State *L)
4359{
4360 const char *name;
4361 int ret;
4362
4363 MAY_LJMP(hlua_checkapplet_http(L, 1));
4364 name = MAY_LJMP(luaL_checkstring(L, 2));
4365 MAY_LJMP(luaL_checkstring(L, 3));
4366
4367 /* Push in the stack the "response" entry. */
4368 ret = lua_getfield(L, 1, "response");
4369 if (ret != LUA_TTABLE) {
4370 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4371 "is expected as an array. %s found", lua_typename(L, ret));
4372 WILL_LJMP(lua_error(L));
4373 }
4374
4375 /* check if the header is already registered if it is not
4376 * the case, register it.
4377 */
4378 ret = lua_getfield(L, -1, name);
4379 if (ret == LUA_TNIL) {
4380
4381 /* Entry not found. */
4382 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4383
4384 /* Insert the new header name in the array in the top of the stack.
4385 * It left the new array in the top of the stack.
4386 */
4387 lua_newtable(L);
4388 lua_pushvalue(L, 2);
4389 lua_pushvalue(L, -2);
4390 lua_settable(L, -4);
4391
4392 } else if (ret != LUA_TTABLE) {
4393
4394 /* corruption error. */
4395 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4396 "is expected as an array. %s found", name, lua_typename(L, ret));
4397 WILL_LJMP(lua_error(L));
4398 }
4399
4400 /* Now the top od thestack is an array of values. We push
4401 * the header value as new entry.
4402 */
4403 lua_pushvalue(L, 3);
4404 ret = lua_rawlen(L, -2);
4405 lua_rawseti(L, -2, ret + 1);
4406 lua_pushboolean(L, 1);
4407 return 1;
4408}
4409
4410__LJMP static int hlua_applet_http_status(lua_State *L)
4411{
4412 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4413 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004414 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004415
4416 if (status < 100 || status > 599) {
4417 lua_pushboolean(L, 0);
4418 return 1;
4419 }
4420
4421 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004422 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004423 lua_pushboolean(L, 1);
4424 return 1;
4425}
4426
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004427
Christopher Fauleta2097962019-07-15 16:25:33 +02004428__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004429{
4430 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4431 struct stream_interface *si = appctx->appctx->owner;
4432 struct channel *res = si_ic(si);
4433 struct htx *htx;
4434 struct htx_sl *sl;
4435 struct h1m h1m;
4436 const char *status, *reason;
4437 const char *name, *value;
4438 size_t nlen, vlen;
4439 unsigned int flags;
4440
4441 /* Send the message at once. */
4442 htx = htx_from_buf(&res->buf);
4443 h1m_init_res(&h1m);
4444
4445 /* Use the same http version than the request. */
4446 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4447 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4448 if (reason == NULL)
4449 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4450 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4451 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4452 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4453 }
4454 else {
4455 flags = HTX_SL_F_IS_RESP;
4456 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4457 }
4458 if (!sl) {
4459 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4460 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4461 WILL_LJMP(lua_error(L));
4462 }
4463 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4464
4465 /* Get the array associated to the field "response" in the object AppletHTTP. */
4466 lua_pushvalue(L, 0);
4467 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4468 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4469 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4470 WILL_LJMP(lua_error(L));
4471 }
4472
4473 /* Browse the list of headers. */
4474 lua_pushnil(L);
4475 while(lua_next(L, -2) != 0) {
4476 /* We expect a string as -2. */
4477 if (lua_type(L, -2) != LUA_TSTRING) {
4478 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4479 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4480 lua_typename(L, lua_type(L, -2)));
4481 WILL_LJMP(lua_error(L));
4482 }
4483 name = lua_tolstring(L, -2, &nlen);
4484
4485 /* We expect an array as -1. */
4486 if (lua_type(L, -1) != LUA_TTABLE) {
4487 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4488 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4489 name,
4490 lua_typename(L, lua_type(L, -1)));
4491 WILL_LJMP(lua_error(L));
4492 }
4493
4494 /* Browse the table who is on the top of the stack. */
4495 lua_pushnil(L);
4496 while(lua_next(L, -2) != 0) {
4497 int id;
4498
4499 /* We expect a number as -2. */
4500 if (lua_type(L, -2) != LUA_TNUMBER) {
4501 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4502 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4503 name,
4504 lua_typename(L, lua_type(L, -2)));
4505 WILL_LJMP(lua_error(L));
4506 }
4507 id = lua_tointeger(L, -2);
4508
4509 /* We expect a string as -2. */
4510 if (lua_type(L, -1) != LUA_TSTRING) {
4511 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4512 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4513 name, id,
4514 lua_typename(L, lua_type(L, -1)));
4515 WILL_LJMP(lua_error(L));
4516 }
4517 value = lua_tolstring(L, -1, &vlen);
4518
4519 /* Simple Protocol checks. */
4520 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004521 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004522 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4523 struct ist v = ist2(value, vlen);
4524 int ret;
4525
4526 ret = h1_parse_cont_len_header(&h1m, &v);
4527 if (ret < 0) {
4528 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4529 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4530 name);
4531 WILL_LJMP(lua_error(L));
4532 }
4533 else if (ret == 0)
4534 goto next; /* Skip it */
4535 }
4536
4537 /* Add a new header */
4538 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4539 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4540 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4541 name);
4542 WILL_LJMP(lua_error(L));
4543 }
4544 next:
4545 /* Remove the array from the stack, and get next element with a remaining string. */
4546 lua_pop(L, 1);
4547 }
4548
4549 /* Remove the array from the stack, and get next element with a remaining string. */
4550 lua_pop(L, 1);
4551 }
4552
4553 if (h1m.flags & H1_MF_CHNK)
4554 h1m.flags &= ~H1_MF_CLEN;
4555 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4556 h1m.flags |= H1_MF_XFER_LEN;
4557
4558 /* Uset HTX start-line flags */
4559 if (h1m.flags & H1_MF_XFER_ENC)
4560 flags |= HTX_SL_F_XFER_ENC;
4561 if (h1m.flags & H1_MF_XFER_LEN) {
4562 flags |= HTX_SL_F_XFER_LEN;
4563 if (h1m.flags & H1_MF_CHNK)
4564 flags |= HTX_SL_F_CHNK;
4565 else if (h1m.flags & H1_MF_CLEN)
4566 flags |= HTX_SL_F_CLEN;
4567 if (h1m.body_len == 0)
4568 flags |= HTX_SL_F_BODYLESS;
4569 }
4570 sl->flags |= flags;
4571
4572 /* If we dont have a content-length set, and the HTTP version is 1.1
4573 * and the status code implies the presence of a message body, we must
4574 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004575 * for the keepalive compliance. If the applet announces a transfer-encoding
4576 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004577 */
4578 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4579 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4580 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4581 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4582 /* Add a new header */
4583 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4584 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4585 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4586 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4587 WILL_LJMP(lua_error(L));
4588 }
4589 }
4590
4591 /* Finalize headers. */
4592 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4593 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4594 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4595 WILL_LJMP(lua_error(L));
4596 }
4597
4598 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4599 b_reset(&res->buf);
4600 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4601 WILL_LJMP(lua_error(L));
4602 }
4603
4604 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004605 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004606
4607 /* Headers sent, set the flag. */
4608 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4609 return 0;
4610
4611}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004612/* We will build the status line and the headers of the HTTP response.
4613 * We will try send at once if its not possible, we give back the hand
4614 * waiting for more room.
4615 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004616__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004617{
4618 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4619 struct stream_interface *si = appctx->appctx->owner;
4620 struct channel *res = si_ic(si);
4621
4622 if (co_data(res)) {
4623 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004624 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004625 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004626 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004627}
4628
4629
Christopher Fauleta2097962019-07-15 16:25:33 +02004630__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004631{
Christopher Fauleta2097962019-07-15 16:25:33 +02004632 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004633}
4634
Christopher Fauleta2097962019-07-15 16:25:33 +02004635/*
4636 *
4637 *
4638 * Class HTTP
4639 *
4640 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004641 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004642
4643/* Returns a struct hlua_txn if the stack entry "ud" is
4644 * a class stream, otherwise it throws an error.
4645 */
4646__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004647{
Christopher Fauleta2097962019-07-15 16:25:33 +02004648 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4649}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004650
Christopher Fauleta2097962019-07-15 16:25:33 +02004651/* This function creates and push in the stack a HTTP object
4652 * according with a current TXN.
4653 */
4654static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4655{
4656 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004657
Christopher Fauleta2097962019-07-15 16:25:33 +02004658 /* Check stack size. */
4659 if (!lua_checkstack(L, 3))
4660 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004661
Christopher Fauleta2097962019-07-15 16:25:33 +02004662 /* Create the object: obj[0] = userdata.
4663 * Note that the base of the Converters object is the
4664 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004665 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004666 lua_newtable(L);
4667 htxn = lua_newuserdata(L, sizeof(*htxn));
4668 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004669
4670 htxn->s = txn->s;
4671 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004672 htxn->dir = txn->dir;
4673 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004674
4675 /* Pop a class stream metatable and affect it to the table. */
4676 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4677 lua_setmetatable(L, -2);
4678
4679 return 1;
4680}
4681
4682/* This function creates ans returns an array of HTTP headers.
4683 * This function does not fails. It is used as wrapper with the
4684 * 2 following functions.
4685 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004686__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004687{
Christopher Fauleta2097962019-07-15 16:25:33 +02004688 struct htx *htx;
4689 int32_t pos;
4690
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004691 /* Create the table. */
4692 lua_newtable(L);
4693
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004694
Christopher Fauleta2097962019-07-15 16:25:33 +02004695 htx = htxbuf(&msg->chn->buf);
4696 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4697 struct htx_blk *blk = htx_get_blk(htx, pos);
4698 enum htx_blk_type type = htx_get_blk_type(blk);
4699 struct ist n, v;
4700 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004701
Christopher Fauleta2097962019-07-15 16:25:33 +02004702 if (type == HTX_BLK_HDR) {
4703 n = htx_get_blk_name(htx,blk);
4704 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004705 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004706 else if (type == HTX_BLK_EOH)
4707 break;
4708 else
4709 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004710
Christopher Fauleta2097962019-07-15 16:25:33 +02004711 /* Check for existing entry:
4712 * assume that the table is on the top of the stack, and
4713 * push the key in the stack, the function lua_gettable()
4714 * perform the lookup.
4715 */
4716 lua_pushlstring(L, n.ptr, n.len);
4717 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004718
Christopher Fauleta2097962019-07-15 16:25:33 +02004719 switch (lua_type(L, -1)) {
4720 case LUA_TNIL:
4721 /* Table not found, create it. */
4722 lua_pop(L, 1); /* remove the nil value. */
4723 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4724 lua_newtable(L); /* create and push empty table. */
4725 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4726 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4727 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004728 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004729
Christopher Fauleta2097962019-07-15 16:25:33 +02004730 case LUA_TTABLE:
4731 /* Entry found: push the value in the table. */
4732 len = lua_rawlen(L, -1);
4733 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4734 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4735 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4736 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004737
Christopher Fauleta2097962019-07-15 16:25:33 +02004738 default:
4739 /* Other cases are errors. */
4740 hlua_pusherror(L, "internal error during the parsing of headers.");
4741 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004742 }
4743 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004744 return 1;
4745}
4746
4747__LJMP static int hlua_http_req_get_headers(lua_State *L)
4748{
4749 struct hlua_txn *htxn;
4750
4751 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4752 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4753
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004754 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004755 WILL_LJMP(lua_error(L));
4756
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004757 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004758}
4759
4760__LJMP static int hlua_http_res_get_headers(lua_State *L)
4761{
4762 struct hlua_txn *htxn;
4763
4764 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4765 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4766
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004767 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004768 WILL_LJMP(lua_error(L));
4769
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004770 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004771}
4772
4773/* This function replace full header, or just a value in
4774 * the request or in the response. It is a wrapper fir the
4775 * 4 following functions.
4776 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004777__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004778{
4779 size_t name_len;
4780 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4781 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4782 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004783 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004784 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004785
Dragan Dosen26743032019-04-30 15:54:36 +02004786 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004787 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4788
Christopher Fauleta2097962019-07-15 16:25:33 +02004789 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004790 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004791 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004792 return 0;
4793}
4794
4795__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4796{
4797 struct hlua_txn *htxn;
4798
4799 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4800 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4801
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004802 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004803 WILL_LJMP(lua_error(L));
4804
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004805 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004806}
4807
4808__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4809{
4810 struct hlua_txn *htxn;
4811
4812 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4813 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4814
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004815 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004816 WILL_LJMP(lua_error(L));
4817
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004818 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004819}
4820
4821__LJMP static int hlua_http_req_rep_val(lua_State *L)
4822{
4823 struct hlua_txn *htxn;
4824
4825 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4826 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4827
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004828 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004829 WILL_LJMP(lua_error(L));
4830
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004831 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004832}
4833
4834__LJMP static int hlua_http_res_rep_val(lua_State *L)
4835{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004836 struct hlua_txn *htxn;
4837
4838 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4839 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4840
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004841 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004842 WILL_LJMP(lua_error(L));
4843
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004844 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004845}
4846
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004847/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004848 * It is a wrapper for the 2 following functions.
4849 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004850__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004851{
4852 size_t len;
4853 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004854 struct htx *htx = htxbuf(&msg->chn->buf);
4855 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004856
Christopher Fauleta2097962019-07-15 16:25:33 +02004857 ctx.blk = NULL;
4858 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4859 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004860 return 0;
4861}
4862
4863__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4864{
4865 struct hlua_txn *htxn;
4866
4867 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4868 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4869
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004870 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004871 WILL_LJMP(lua_error(L));
4872
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004873 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004874}
4875
4876__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4877{
4878 struct hlua_txn *htxn;
4879
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004880 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004881 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4882
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004883 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004884 WILL_LJMP(lua_error(L));
4885
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004886 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004887}
4888
4889/* This function adds an header. It is a wrapper used by
4890 * the 2 following functions.
4891 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004892__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004893{
4894 size_t name_len;
4895 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4896 size_t value_len;
4897 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004898 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004899
Christopher Fauleta2097962019-07-15 16:25:33 +02004900 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4901 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004902 return 0;
4903}
4904
4905__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4906{
4907 struct hlua_txn *htxn;
4908
4909 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4910 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4911
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004912 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004913 WILL_LJMP(lua_error(L));
4914
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004915 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004916}
4917
4918__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4919{
4920 struct hlua_txn *htxn;
4921
4922 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4923 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4924
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004925 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004926 WILL_LJMP(lua_error(L));
4927
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004928 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004929}
4930
4931static int hlua_http_req_set_hdr(lua_State *L)
4932{
4933 struct hlua_txn *htxn;
4934
4935 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4936 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4937
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004938 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004939 WILL_LJMP(lua_error(L));
4940
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004941 hlua_http_del_hdr(L, &htxn->s->txn->req);
4942 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004943}
4944
4945static int hlua_http_res_set_hdr(lua_State *L)
4946{
4947 struct hlua_txn *htxn;
4948
4949 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4950 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4951
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004952 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004953 WILL_LJMP(lua_error(L));
4954
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004955 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
4956 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004957}
4958
4959/* This function set the method. */
4960static int hlua_http_req_set_meth(lua_State *L)
4961{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004962 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004963 size_t name_len;
4964 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004965
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004966 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004967 WILL_LJMP(lua_error(L));
4968
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004969 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004970 return 1;
4971}
4972
4973/* This function set the method. */
4974static int hlua_http_req_set_path(lua_State *L)
4975{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004976 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004977 size_t name_len;
4978 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004979
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004980 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004981 WILL_LJMP(lua_error(L));
4982
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004983 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004984 return 1;
4985}
4986
4987/* This function set the query-string. */
4988static int hlua_http_req_set_query(lua_State *L)
4989{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004990 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004991 size_t name_len;
4992 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004993
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004994 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004995 WILL_LJMP(lua_error(L));
4996
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004997 /* Check length. */
4998 if (name_len > trash.size - 1) {
4999 lua_pushboolean(L, 0);
5000 return 1;
5001 }
5002
5003 /* Add the mark question as prefix. */
5004 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005005 trash.area[trash.data++] = '?';
5006 memcpy(trash.area + trash.data, name, name_len);
5007 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005008
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005009 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005010 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005011 return 1;
5012}
5013
5014/* This function set the uri. */
5015static int hlua_http_req_set_uri(lua_State *L)
5016{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005017 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005018 size_t name_len;
5019 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005020
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005021 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005022 WILL_LJMP(lua_error(L));
5023
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005024 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005025 return 1;
5026}
5027
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005028/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005029static int hlua_http_res_set_status(lua_State *L)
5030{
5031 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5032 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005033 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5034 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005035
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005036 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005037 WILL_LJMP(lua_error(L));
5038
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005039 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005040 return 0;
5041}
5042
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005043/*
5044 *
5045 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005046 * Class TXN
5047 *
5048 *
5049 */
5050
5051/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005052 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005053 */
5054__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5055{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005056 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005057}
5058
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005059__LJMP static int hlua_set_var(lua_State *L)
5060{
5061 struct hlua_txn *htxn;
5062 const char *name;
5063 size_t len;
5064 struct sample smp;
5065
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005066 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5067 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005068
5069 /* It is useles to retrieve the stream, but this function
5070 * runs only in a stream context.
5071 */
5072 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5073 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5074
5075 /* Converts the third argument in a sample. */
5076 hlua_lua2smp(L, 3, &smp);
5077
5078 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005079 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005080
5081 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5082 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5083 else
5084 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5085
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005086 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005087}
5088
Christopher Faulet85d79c92016-11-09 16:54:56 +01005089__LJMP static int hlua_unset_var(lua_State *L)
5090{
5091 struct hlua_txn *htxn;
5092 const char *name;
5093 size_t len;
5094 struct sample smp;
5095
5096 MAY_LJMP(check_args(L, 2, "unset_var"));
5097
5098 /* It is useles to retrieve the stream, but this function
5099 * runs only in a stream context.
5100 */
5101 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5102 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5103
5104 /* Unset the variable. */
5105 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005106 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5107 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005108}
5109
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005110__LJMP static int hlua_get_var(lua_State *L)
5111{
5112 struct hlua_txn *htxn;
5113 const char *name;
5114 size_t len;
5115 struct sample smp;
5116
5117 MAY_LJMP(check_args(L, 2, "get_var"));
5118
5119 /* It is useles to retrieve the stream, but this function
5120 * runs only in a stream context.
5121 */
5122 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5123 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5124
Willy Tarreau7560dd42016-03-10 16:28:58 +01005125 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005126 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005127 lua_pushnil(L);
5128 return 1;
5129 }
5130
5131 return hlua_smp2lua(L, &smp);
5132}
5133
Willy Tarreau59551662015-03-10 14:23:13 +01005134__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005135{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005136 struct hlua *hlua;
5137
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005138 MAY_LJMP(check_args(L, 2, "set_priv"));
5139
Willy Tarreau87b09662015-04-03 00:22:06 +02005140 /* It is useles to retrieve the stream, but this function
5141 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005142 */
5143 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005144 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005145
5146 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005147 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005148
5149 /* Get and store new value. */
5150 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5151 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5152
5153 return 0;
5154}
5155
Willy Tarreau59551662015-03-10 14:23:13 +01005156__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005157{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005158 struct hlua *hlua;
5159
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005160 MAY_LJMP(check_args(L, 1, "get_priv"));
5161
Willy Tarreau87b09662015-04-03 00:22:06 +02005162 /* It is useles to retrieve the stream, but this function
5163 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005164 */
5165 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005166 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005167
5168 /* Push configuration index in the stack. */
5169 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5170
5171 return 1;
5172}
5173
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005174/* Create stack entry containing a class TXN. This function
5175 * return 0 if the stack does not contains free slots,
5176 * otherwise it returns 1.
5177 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005178static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005179{
Willy Tarreaude491382015-04-06 11:04:28 +02005180 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005181
5182 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005183 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005184 return 0;
5185
5186 /* NOTE: The allocation never fails. The failure
5187 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005188 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005189 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005190 /* Create the object: obj[0] = userdata. */
5191 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005192 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005193 lua_rawseti(L, -2, 0);
5194
Willy Tarreaude491382015-04-06 11:04:28 +02005195 htxn->s = s;
5196 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005197 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005198 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005199
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005200 /* Create the "f" field that contains a list of fetches. */
5201 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005202 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005203 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005204 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005205
5206 /* Create the "sf" field that contains a list of stringsafe fetches. */
5207 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005208 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005209 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005210 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005211
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005212 /* Create the "c" field that contains a list of converters. */
5213 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005214 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005215 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005216 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005217
5218 /* Create the "sc" field that contains a list of stringsafe converters. */
5219 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005220 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005221 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005222 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005223
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005224 /* Create the "req" field that contains the request channel object. */
5225 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005226 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005227 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005228 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005229
5230 /* Create the "res" field that contains the response channel object. */
5231 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005232 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005233 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005234 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005235
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005236 /* Creates the HTTP object is the current proxy allows http. */
5237 lua_pushstring(L, "http");
5238 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005239 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005240 return 0;
5241 }
5242 else
5243 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005244 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005245
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005246 /* Pop a class sesison metatable and affect it to the userdata. */
5247 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5248 lua_setmetatable(L, -2);
5249
5250 return 1;
5251}
5252
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005253__LJMP static int hlua_txn_deflog(lua_State *L)
5254{
5255 const char *msg;
5256 struct hlua_txn *htxn;
5257
5258 MAY_LJMP(check_args(L, 2, "deflog"));
5259 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5260 msg = MAY_LJMP(luaL_checkstring(L, 2));
5261
5262 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5263 return 0;
5264}
5265
5266__LJMP static int hlua_txn_log(lua_State *L)
5267{
5268 int level;
5269 const char *msg;
5270 struct hlua_txn *htxn;
5271
5272 MAY_LJMP(check_args(L, 3, "log"));
5273 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5274 level = MAY_LJMP(luaL_checkinteger(L, 2));
5275 msg = MAY_LJMP(luaL_checkstring(L, 3));
5276
5277 if (level < 0 || level >= NB_LOG_LEVELS)
5278 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5279
5280 hlua_sendlog(htxn->s->be, level, msg);
5281 return 0;
5282}
5283
5284__LJMP static int hlua_txn_log_debug(lua_State *L)
5285{
5286 const char *msg;
5287 struct hlua_txn *htxn;
5288
5289 MAY_LJMP(check_args(L, 2, "Debug"));
5290 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5291 msg = MAY_LJMP(luaL_checkstring(L, 2));
5292 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5293 return 0;
5294}
5295
5296__LJMP static int hlua_txn_log_info(lua_State *L)
5297{
5298 const char *msg;
5299 struct hlua_txn *htxn;
5300
5301 MAY_LJMP(check_args(L, 2, "Info"));
5302 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5303 msg = MAY_LJMP(luaL_checkstring(L, 2));
5304 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5305 return 0;
5306}
5307
5308__LJMP static int hlua_txn_log_warning(lua_State *L)
5309{
5310 const char *msg;
5311 struct hlua_txn *htxn;
5312
5313 MAY_LJMP(check_args(L, 2, "Warning"));
5314 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5315 msg = MAY_LJMP(luaL_checkstring(L, 2));
5316 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5317 return 0;
5318}
5319
5320__LJMP static int hlua_txn_log_alert(lua_State *L)
5321{
5322 const char *msg;
5323 struct hlua_txn *htxn;
5324
5325 MAY_LJMP(check_args(L, 2, "Alert"));
5326 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5327 msg = MAY_LJMP(luaL_checkstring(L, 2));
5328 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5329 return 0;
5330}
5331
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005332__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5333{
5334 struct hlua_txn *htxn;
5335 int ll;
5336
5337 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5338 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5339 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5340
5341 if (ll < 0 || ll > 7)
5342 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5343
5344 htxn->s->logs.level = ll;
5345 return 0;
5346}
5347
5348__LJMP static int hlua_txn_set_tos(lua_State *L)
5349{
5350 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005351 int tos;
5352
5353 MAY_LJMP(check_args(L, 2, "set_tos"));
5354 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5355 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5356
Willy Tarreau1a18b542018-12-11 16:37:42 +01005357 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005358 return 0;
5359}
5360
5361__LJMP static int hlua_txn_set_mark(lua_State *L)
5362{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005363 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005364 int mark;
5365
5366 MAY_LJMP(check_args(L, 2, "set_mark"));
5367 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5368 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5369
Lukas Tribus579e3e32019-08-11 18:03:45 +02005370 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005371 return 0;
5372}
5373
Patrick Hemmer268a7072018-05-11 12:52:31 -04005374__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5375{
5376 struct hlua_txn *htxn;
5377
5378 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5379 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5380 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5381 return 0;
5382}
5383
5384__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5385{
5386 struct hlua_txn *htxn;
5387
5388 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5389 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5390 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5391 return 0;
5392}
5393
Christopher Faulet700d9e82020-01-31 12:21:52 +01005394/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005395 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005396 * message and terminate the transaction. It returns 1 on success and 0 on
5397 * error. The Reply must be on top of the stack.
5398 */
5399__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5400{
5401 struct htx *htx;
5402 struct htx_sl *sl;
5403 struct h1m h1m;
5404 const char *status, *reason, *body;
5405 size_t status_len, reason_len, body_len;
5406 int ret, code, flags;
5407
5408 code = 200;
5409 status = "200";
5410 status_len = 3;
5411 ret = lua_getfield(L, -1, "status");
5412 if (ret == LUA_TNUMBER) {
5413 code = lua_tointeger(L, -1);
5414 status = lua_tolstring(L, -1, &status_len);
5415 }
5416 lua_pop(L, 1);
5417
5418 reason = http_get_reason(code);
5419 reason_len = strlen(reason);
5420 ret = lua_getfield(L, -1, "reason");
5421 if (ret == LUA_TSTRING)
5422 reason = lua_tolstring(L, -1, &reason_len);
5423 lua_pop(L, 1);
5424
5425 body = NULL;
5426 body_len = 0;
5427 ret = lua_getfield(L, -1, "body");
5428 if (ret == LUA_TSTRING)
5429 body = lua_tolstring(L, -1, &body_len);
5430 lua_pop(L, 1);
5431
5432 /* Prepare the response before inserting the headers */
5433 h1m_init_res(&h1m);
5434 htx = htx_from_buf(&s->res.buf);
5435 channel_htx_truncate(&s->res, htx);
5436 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5437 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5438 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5439 ist2(status, status_len), ist2(reason, reason_len));
5440 }
5441 else {
5442 flags = HTX_SL_F_IS_RESP;
5443 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5444 ist2(status, status_len), ist2(reason, reason_len));
5445 }
5446 if (!sl)
5447 goto fail;
5448 sl->info.res.status = code;
5449
5450 /* Push in the stack the "headers" entry. */
5451 ret = lua_getfield(L, -1, "headers");
5452 if (ret != LUA_TTABLE)
5453 goto skip_headers;
5454
5455 lua_pushnil(L);
5456 while (lua_next(L, -2) != 0) {
5457 struct ist name, value;
5458 const char *n, *v;
5459 size_t nlen, vlen;
5460
5461 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5462 /* Skip element if the key is not a string or if the value is not a table */
5463 goto next_hdr;
5464 }
5465
5466 n = lua_tolstring(L, -2, &nlen);
5467 name = ist2(n, nlen);
5468 if (isteqi(name, ist("content-length"))) {
5469 /* Always skip content-length header. It will be added
5470 * later with the correct len
5471 */
5472 goto next_hdr;
5473 }
5474
5475 /* Loop on header's values */
5476 lua_pushnil(L);
5477 while (lua_next(L, -2)) {
5478 if (!lua_isstring(L, -1)) {
5479 /* Skip the value if it is not a string */
5480 goto next_value;
5481 }
5482
5483 v = lua_tolstring(L, -1, &vlen);
5484 value = ist2(v, vlen);
5485
5486 if (isteqi(name, ist("transfer-encoding")))
5487 h1_parse_xfer_enc_header(&h1m, value);
5488 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5489 goto fail;
5490
5491 next_value:
5492 lua_pop(L, 1);
5493 }
5494
5495 next_hdr:
5496 lua_pop(L, 1);
5497 }
5498 skip_headers:
5499 lua_pop(L, 1);
5500
5501 /* Update h1m flags: CLEN is set if CHNK is not present */
5502 if (!(h1m.flags & H1_MF_CHNK)) {
5503 const char *clen = ultoa(body_len);
5504
5505 h1m.flags |= H1_MF_CLEN;
5506 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5507 goto fail;
5508 }
5509 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5510 h1m.flags |= H1_MF_XFER_LEN;
5511
5512 /* Update HTX start-line flags */
5513 if (h1m.flags & H1_MF_XFER_ENC)
5514 flags |= HTX_SL_F_XFER_ENC;
5515 if (h1m.flags & H1_MF_XFER_LEN) {
5516 flags |= HTX_SL_F_XFER_LEN;
5517 if (h1m.flags & H1_MF_CHNK)
5518 flags |= HTX_SL_F_CHNK;
5519 else if (h1m.flags & H1_MF_CLEN)
5520 flags |= HTX_SL_F_CLEN;
5521 if (h1m.body_len == 0)
5522 flags |= HTX_SL_F_BODYLESS;
5523 }
5524 sl->flags |= flags;
5525
5526
5527 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5528 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5529 !htx_add_endof(htx, HTX_BLK_EOM))
5530 goto fail;
5531
5532 /* Now, forward the response and terminate the transaction */
5533 s->txn->status = code;
5534 htx_to_buf(htx, &s->res.buf);
5535 if (!http_forward_proxy_resp(s, 1))
5536 goto fail;
5537
5538 return 1;
5539
5540 fail:
5541 channel_htx_truncate(&s->res, htx);
5542 return 0;
5543}
5544
5545/* Terminate a transaction if called from a lua action. For TCP streams,
5546 * processing is just aborted. Nothing is returned to the client and all
5547 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5548 * is forwarded to the client before terminating the transaction. On success,
5549 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5550 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5551 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005552 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005553__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005554{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005555 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005556 struct stream *s;
5557 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005558
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005559 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005560
Christopher Faulet700d9e82020-01-31 12:21:52 +01005561 /* If the flags NOTERM is set, we cannot terminate the session, so we
5562 * just end the execution of the current lua code. */
5563 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005564 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005565
Christopher Faulet700d9e82020-01-31 12:21:52 +01005566 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005567 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005568 struct channel *req = &s->req;
5569 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005570
Christopher Faulet700d9e82020-01-31 12:21:52 +01005571 channel_auto_read(req);
5572 channel_abort(req);
5573 channel_auto_close(req);
5574 channel_erase(req);
5575
5576 res->wex = tick_add_ifset(now_ms, res->wto);
5577 channel_auto_read(res);
5578 channel_auto_close(res);
5579 channel_shutr_now(res);
5580
5581 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5582 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005583 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005584
Christopher Faulet700d9e82020-01-31 12:21:52 +01005585 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5586 /* No reply or invalid reply */
5587 s->txn->status = 0;
5588 http_reply_and_close(s, 0, NULL);
5589 }
5590 else {
5591 /* Remove extra args to have the reply on top of the stack */
5592 if (lua_gettop(L) > 2)
5593 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005594
Christopher Faulet700d9e82020-01-31 12:21:52 +01005595 if (!hlua_txn_forward_reply(L, s)) {
5596 if (!(s->flags & SF_ERR_MASK))
5597 s->flags |= SF_ERR_PRXCOND;
5598 lua_pushinteger(L, ACT_RET_ERR);
5599 WILL_LJMP(hlua_done(L));
5600 return 0; /* Never reached */
5601 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005602 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005603
Christopher Faulet700d9e82020-01-31 12:21:52 +01005604 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5605 if (htxn->dir == SMP_OPT_DIR_REQ) {
5606 /* let's log the request time */
5607 s->logs.tv_request = now;
5608 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5609 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5610 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005611
Christopher Faulet700d9e82020-01-31 12:21:52 +01005612 done:
5613 if (!(s->flags & SF_ERR_MASK))
5614 s->flags |= SF_ERR_LOCAL;
5615 if (!(s->flags & SF_FINST_MASK))
5616 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005617
Christopher Faulet4ad73102020-03-05 11:07:31 +01005618 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005619 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005620 return 0;
5621}
5622
Christopher Faulet700d9e82020-01-31 12:21:52 +01005623/*
5624 *
5625 *
5626 * Class REPLY
5627 *
5628 *
5629 */
5630
5631/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5632 * free slots, the function fails and returns 0;
5633 */
5634static int hlua_txn_reply_new(lua_State *L)
5635{
5636 struct hlua_txn *htxn;
5637 const char *reason, *body = NULL;
5638 int ret, status;
5639
5640 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005641 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005642 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5643 WILL_LJMP(lua_error(L));
5644 }
5645
5646 /* Default value */
5647 status = 200;
5648 reason = http_get_reason(status);
5649
5650 if (lua_istable(L, 2)) {
5651 /* load status and reason from the table argument at index 2 */
5652 ret = lua_getfield(L, 2, "status");
5653 if (ret == LUA_TNIL)
5654 goto reason;
5655 else if (ret != LUA_TNUMBER) {
5656 /* invalid status: ignore the reason */
5657 goto body;
5658 }
5659 status = lua_tointeger(L, -1);
5660
5661 reason:
5662 lua_pop(L, 1); /* restore the stack: remove status */
5663 ret = lua_getfield(L, 2, "reason");
5664 if (ret == LUA_TSTRING)
5665 reason = lua_tostring(L, -1);
5666
5667 body:
5668 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5669 ret = lua_getfield(L, 2, "body");
5670 if (ret == LUA_TSTRING)
5671 body = lua_tostring(L, -1);
5672 lua_pop(L, 1); /* restore the stack: remove body */
5673 }
5674
5675 /* Create the Reply table */
5676 lua_newtable(L);
5677
5678 /* Add status element */
5679 lua_pushstring(L, "status");
5680 lua_pushinteger(L, status);
5681 lua_settable(L, -3);
5682
5683 /* Add reason element */
5684 reason = http_get_reason(status);
5685 lua_pushstring(L, "reason");
5686 lua_pushstring(L, reason);
5687 lua_settable(L, -3);
5688
5689 /* Add body element, nil if undefined */
5690 lua_pushstring(L, "body");
5691 if (body)
5692 lua_pushstring(L, body);
5693 else
5694 lua_pushnil(L);
5695 lua_settable(L, -3);
5696
5697 /* Add headers element */
5698 lua_pushstring(L, "headers");
5699 lua_newtable(L);
5700
5701 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5702 if (lua_istable(L, 2)) {
5703 /* load headers from the table argument at index 2. If it is a table, copy it. */
5704 ret = lua_getfield(L, 2, "headers");
5705 if (ret == LUA_TTABLE) {
5706 /* stack: [ ... <headers:table>, <table> ] */
5707 lua_pushnil(L);
5708 while (lua_next(L, -2) != 0) {
5709 /* stack: [ ... <headers:table>, <table>, k, v] */
5710 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5711 /* invalid value type, skip it */
5712 lua_pop(L, 1);
5713 continue;
5714 }
5715
5716
5717 /* Duplicate the key and swap it with the value. */
5718 lua_pushvalue(L, -2);
5719 lua_insert(L, -2);
5720 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5721
5722 lua_newtable(L);
5723 lua_insert(L, -2);
5724 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5725
5726 if (lua_isstring(L, -1)) {
5727 /* push the value in the inner table */
5728 lua_rawseti(L, -2, 1);
5729 }
5730 else { /* table */
5731 lua_pushnil(L);
5732 while (lua_next(L, -2) != 0) {
5733 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5734 if (!lua_isstring(L, -1)) {
5735 /* invalid value type, skip it*/
5736 lua_pop(L, 1);
5737 continue;
5738 }
5739 /* push the value in the inner table */
5740 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5741 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5742 }
5743 lua_pop(L, 1);
5744 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5745 }
5746
5747 /* push (k,v) on the stack in the headers table:
5748 * stack: [ ... <headers:table>, <table>, k, k, v ]
5749 */
5750 lua_settable(L, -5);
5751 /* stack: [ ... <headers:table>, <table>, k ] */
5752 }
5753 }
5754 lua_pop(L, 1);
5755 }
5756 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5757 lua_settable(L, -3);
5758 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5759
5760 /* Pop a class sesison metatable and affect it to the userdata. */
5761 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5762 lua_setmetatable(L, -2);
5763 return 1;
5764}
5765
5766/* Set the reply status code, and optionally the reason. If no reason is
5767 * provided, the default one corresponding to the status code is used.
5768 */
5769__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5770{
5771 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5772 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5773
5774 /* First argument (self) must be a table */
5775 luaL_checktype(L, 1, LUA_TTABLE);
5776
5777 if (status < 100 || status > 599) {
5778 lua_pushboolean(L, 0);
5779 return 1;
5780 }
5781 if (!reason)
5782 reason = http_get_reason(status);
5783
5784 lua_pushinteger(L, status);
5785 lua_setfield(L, 1, "status");
5786
5787 lua_pushstring(L, reason);
5788 lua_setfield(L, 1, "reason");
5789
5790 lua_pushboolean(L, 1);
5791 return 1;
5792}
5793
5794/* Add a header into the reply object. Each header name is associated to an
5795 * array of values in the "headers" table. If the header name is not found, a
5796 * new entry is created.
5797 */
5798__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5799{
5800 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5801 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5802 int ret;
5803
5804 /* First argument (self) must be a table */
5805 luaL_checktype(L, 1, LUA_TTABLE);
5806
5807 /* Push in the stack the "headers" entry. */
5808 ret = lua_getfield(L, 1, "headers");
5809 if (ret != LUA_TTABLE) {
5810 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5811 WILL_LJMP(lua_error(L));
5812 }
5813
5814 /* check if the header is already registered. If not, register it. */
5815 ret = lua_getfield(L, -1, name);
5816 if (ret == LUA_TNIL) {
5817 /* Entry not found. */
5818 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5819
5820 /* Insert the new header name in the array in the top of the stack.
5821 * It left the new array in the top of the stack.
5822 */
5823 lua_newtable(L);
5824 lua_pushstring(L, name);
5825 lua_pushvalue(L, -2);
5826 lua_settable(L, -4);
5827 }
5828 else if (ret != LUA_TTABLE) {
5829 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5830 WILL_LJMP(lua_error(L));
5831 }
5832
5833 /* Now the top od thestack is an array of values. We push
5834 * the header value as new entry.
5835 */
5836 lua_pushstring(L, value);
5837 ret = lua_rawlen(L, -2);
5838 lua_rawseti(L, -2, ret + 1);
5839
5840 lua_pushboolean(L, 1);
5841 return 1;
5842}
5843
5844/* Remove all occurrences of a given header name. */
5845__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5846{
5847 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5848 int ret;
5849
5850 /* First argument (self) must be a table */
5851 luaL_checktype(L, 1, LUA_TTABLE);
5852
5853 /* Push in the stack the "headers" entry. */
5854 ret = lua_getfield(L, 1, "headers");
5855 if (ret != LUA_TTABLE) {
5856 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5857 WILL_LJMP(lua_error(L));
5858 }
5859
5860 lua_pushstring(L, name);
5861 lua_pushnil(L);
5862 lua_settable(L, -3);
5863
5864 lua_pushboolean(L, 1);
5865 return 1;
5866}
5867
5868/* Set the reply's body. Overwrite any existing entry. */
5869__LJMP static int hlua_txn_reply_set_body(lua_State *L)
5870{
5871 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
5872
5873 /* First argument (self) must be a table */
5874 luaL_checktype(L, 1, LUA_TTABLE);
5875
5876 lua_pushstring(L, payload);
5877 lua_setfield(L, 1, "body");
5878
5879 lua_pushboolean(L, 1);
5880 return 1;
5881}
5882
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005883__LJMP static int hlua_log(lua_State *L)
5884{
5885 int level;
5886 const char *msg;
5887
5888 MAY_LJMP(check_args(L, 2, "log"));
5889 level = MAY_LJMP(luaL_checkinteger(L, 1));
5890 msg = MAY_LJMP(luaL_checkstring(L, 2));
5891
5892 if (level < 0 || level >= NB_LOG_LEVELS)
5893 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5894
5895 hlua_sendlog(NULL, level, msg);
5896 return 0;
5897}
5898
5899__LJMP static int hlua_log_debug(lua_State *L)
5900{
5901 const char *msg;
5902
5903 MAY_LJMP(check_args(L, 1, "debug"));
5904 msg = MAY_LJMP(luaL_checkstring(L, 1));
5905 hlua_sendlog(NULL, LOG_DEBUG, msg);
5906 return 0;
5907}
5908
5909__LJMP static int hlua_log_info(lua_State *L)
5910{
5911 const char *msg;
5912
5913 MAY_LJMP(check_args(L, 1, "info"));
5914 msg = MAY_LJMP(luaL_checkstring(L, 1));
5915 hlua_sendlog(NULL, LOG_INFO, msg);
5916 return 0;
5917}
5918
5919__LJMP static int hlua_log_warning(lua_State *L)
5920{
5921 const char *msg;
5922
5923 MAY_LJMP(check_args(L, 1, "warning"));
5924 msg = MAY_LJMP(luaL_checkstring(L, 1));
5925 hlua_sendlog(NULL, LOG_WARNING, msg);
5926 return 0;
5927}
5928
5929__LJMP static int hlua_log_alert(lua_State *L)
5930{
5931 const char *msg;
5932
5933 MAY_LJMP(check_args(L, 1, "alert"));
5934 msg = MAY_LJMP(luaL_checkstring(L, 1));
5935 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005936 return 0;
5937}
5938
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005939__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005940{
5941 int wakeup_ms = lua_tointeger(L, -1);
5942 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005943 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005944 return 0;
5945}
5946
5947__LJMP static int hlua_sleep(lua_State *L)
5948{
5949 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005950 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005951
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005952 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005953
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005954 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005955 wakeup_ms = tick_add(now_ms, delay);
5956 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005957
Willy Tarreau9635e032018-10-16 17:52:55 +02005958 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005959 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005960}
5961
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005962__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005963{
5964 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005965 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005966
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005967 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005968
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005969 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005970 wakeup_ms = tick_add(now_ms, delay);
5971 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005972
Willy Tarreau9635e032018-10-16 17:52:55 +02005973 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005974 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005975}
5976
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005977/* This functionis an LUA binding. it permits to give back
5978 * the hand at the HAProxy scheduler. It is used when the
5979 * LUA processing consumes a lot of time.
5980 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005981__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005982{
5983 return 0;
5984}
5985
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005986__LJMP static int hlua_yield(lua_State *L)
5987{
Willy Tarreau9635e032018-10-16 17:52:55 +02005988 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005989 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005990}
5991
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005992/* This function change the nice of the currently executed
5993 * task. It is used set low or high priority at the current
5994 * task.
5995 */
Willy Tarreau59551662015-03-10 14:23:13 +01005996__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005997{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005998 struct hlua *hlua;
5999 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006000
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006001 MAY_LJMP(check_args(L, 1, "set_nice"));
6002 hlua = hlua_gethlua(L);
6003 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006004
6005 /* If he task is not set, I'm in a start mode. */
6006 if (!hlua || !hlua->task)
6007 return 0;
6008
6009 if (nice < -1024)
6010 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006011 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006012 nice = 1024;
6013
6014 hlua->task->nice = nice;
6015 return 0;
6016}
6017
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006018/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006019 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6020 * return an E_AGAIN signal, the emmiter of this signal must set a
6021 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006022 *
6023 * Task wrapper are longjmp safe because the only one Lua code
6024 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006025 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006026struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006027{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006028 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006029 enum hlua_exec status;
6030
Christopher Faulet5bc99722018-04-25 10:34:45 +02006031 if (task->thread_mask == MAX_THREADS_MASK)
6032 task_set_affinity(task, tid_bit);
6033
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006034 /* If it is the first call to the task, we must initialize the
6035 * execution timeouts.
6036 */
6037 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006038 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006039
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006040 /* Execute the Lua code. */
6041 status = hlua_ctx_resume(hlua, 1);
6042
6043 switch (status) {
6044 /* finished or yield */
6045 case HLUA_E_OK:
6046 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006047 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006048 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006049 break;
6050
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006051 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006052 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006053 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006054 break;
6055
6056 /* finished with error. */
6057 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006058 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006059 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006060 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006061 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006062 break;
6063
6064 case HLUA_E_ERR:
6065 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006066 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006067 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006068 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006069 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006070 break;
6071 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006072 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006073}
6074
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006075/* This function is an LUA binding that register LUA function to be
6076 * executed after the HAProxy configuration parsing and before the
6077 * HAProxy scheduler starts. This function expect only one LUA
6078 * argument that is a function. This function returns nothing, but
6079 * throws if an error is encountered.
6080 */
6081__LJMP static int hlua_register_init(lua_State *L)
6082{
6083 struct hlua_init_function *init;
6084 int ref;
6085
6086 MAY_LJMP(check_args(L, 1, "register_init"));
6087
6088 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6089
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006090 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006091 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006092 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006093
6094 init->function_ref = ref;
6095 LIST_ADDQ(&hlua_init_functions, &init->l);
6096 return 0;
6097}
6098
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006099/* This functio is an LUA binding. It permits to register a task
6100 * executed in parallel of the main HAroxy activity. The task is
6101 * created and it is set in the HAProxy scheduler. It can be called
6102 * from the "init" section, "post init" or during the runtime.
6103 *
6104 * Lua prototype:
6105 *
6106 * <none> core.register_task(<function>)
6107 */
6108static int hlua_register_task(lua_State *L)
6109{
6110 struct hlua *hlua;
6111 struct task *task;
6112 int ref;
6113
6114 MAY_LJMP(check_args(L, 1, "register_task"));
6115
6116 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6117
Willy Tarreaubafbe012017-11-24 17:34:44 +01006118 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006119 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006120 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006121
Emeric Brunc60def82017-09-27 14:59:38 +02006122 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006123 if (!task)
6124 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6125
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006126 task->context = hlua;
6127 task->process = hlua_process_task;
6128
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006129 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006130 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006131
6132 /* Restore the function in the stack. */
6133 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6134 hlua->nargs = 0;
6135
6136 /* Schedule task. */
6137 task_schedule(task, now_ms);
6138
6139 return 0;
6140}
6141
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006142/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6143 * doesn't allow "yield" functions because the HAProxy engine cannot
6144 * resume converters.
6145 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006146static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006147{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006148 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006149 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006150 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006151
Willy Tarreaube508f12016-03-10 11:47:01 +01006152 if (!stream)
6153 return 0;
6154
Willy Tarreau87b09662015-04-03 00:22:06 +02006155 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006156 * Lua context can be not initialized. This behavior
6157 * permits to save performances because a systematic
6158 * Lua initialization cause 5% performances loss.
6159 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006160 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006161 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006162 if (!stream->hlua) {
6163 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6164 return 0;
6165 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006166 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006167 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6168 return 0;
6169 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006170 }
6171
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006172 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006173 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006174
6175 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006176 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6177 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6178 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006179 else
6180 error = "critical error";
6181 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006182 return 0;
6183 }
6184
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006185 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006186 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006187 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006188 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006189 return 0;
6190 }
6191
6192 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006193 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006194
6195 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006196 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006197 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006198 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006199 return 0;
6200 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006201 hlua_smp2lua(stream->hlua->T, smp);
6202 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006203
6204 /* push keywords in the stack. */
6205 if (arg_p) {
6206 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006207 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006208 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006209 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006210 return 0;
6211 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006212 hlua_arg2lua(stream->hlua->T, arg_p);
6213 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006214 }
6215 }
6216
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006217 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006218 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006219
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006220 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006221 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006222 }
6223
6224 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006225 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006226 /* finished. */
6227 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006228 /* If the stack is empty, the function fails. */
6229 if (lua_gettop(stream->hlua->T) <= 0)
6230 return 0;
6231
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006232 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006233 hlua_lua2smp(stream->hlua->T, -1, smp);
6234 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006235 return 1;
6236
6237 /* yield. */
6238 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006239 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006240 return 0;
6241
6242 /* finished with error. */
6243 case HLUA_E_ERRMSG:
6244 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006245 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006246 fcn->name, lua_tostring(stream->hlua->T, -1));
6247 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006248 return 0;
6249
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006250 case HLUA_E_ETMOUT:
6251 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6252 return 0;
6253
6254 case HLUA_E_NOMEM:
6255 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6256 return 0;
6257
6258 case HLUA_E_YIELD:
6259 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6260 return 0;
6261
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006262 case HLUA_E_ERR:
6263 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006264 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006265 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006266
6267 default:
6268 return 0;
6269 }
6270}
6271
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006272/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6273 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006274 * resume sample-fetches. This function will be called by the sample
6275 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006276 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006277static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6278 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006279{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006280 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006281 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006282 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006283 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006284
Willy Tarreaube508f12016-03-10 11:47:01 +01006285 if (!stream)
6286 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006287
Willy Tarreau87b09662015-04-03 00:22:06 +02006288 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006289 * Lua context can be not initialized. This behavior
6290 * permits to save performances because a systematic
6291 * Lua initialization cause 5% performances loss.
6292 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006293 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006294 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006295 if (!stream->hlua) {
6296 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6297 return 0;
6298 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006299 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006300 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6301 return 0;
6302 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006303 }
6304
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006305 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006306 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006307
6308 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006309 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6310 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6311 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006312 else
6313 error = "critical error";
6314 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006315 return 0;
6316 }
6317
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006318 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006319 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006320 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006321 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006322 return 0;
6323 }
6324
6325 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006326 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006327
6328 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006329 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006330 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006331 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006332 return 0;
6333 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006334 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006335
6336 /* push keywords in the stack. */
6337 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6338 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006339 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006340 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006341 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006342 return 0;
6343 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006344 hlua_arg2lua(stream->hlua->T, arg_p);
6345 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006346 }
6347
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006348 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006349 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006350
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006351 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006352 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006353 }
6354
6355 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006356 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006357 /* finished. */
6358 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006359 /* If the stack is empty, the function fails. */
6360 if (lua_gettop(stream->hlua->T) <= 0)
6361 return 0;
6362
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006363 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006364 hlua_lua2smp(stream->hlua->T, -1, smp);
6365 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006366
6367 /* Set the end of execution flag. */
6368 smp->flags &= ~SMP_F_MAY_CHANGE;
6369 return 1;
6370
6371 /* yield. */
6372 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006373 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006374 return 0;
6375
6376 /* finished with error. */
6377 case HLUA_E_ERRMSG:
6378 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006379 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006380 fcn->name, lua_tostring(stream->hlua->T, -1));
6381 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006382 return 0;
6383
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006384 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006385 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6386 return 0;
6387
6388 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006389 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6390 return 0;
6391
6392 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006393 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6394 return 0;
6395
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006396 case HLUA_E_ERR:
6397 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006398 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006399 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006400
6401 default:
6402 return 0;
6403 }
6404}
6405
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006406/* This function is an LUA binding used for registering
6407 * "sample-conv" functions. It expects a converter name used
6408 * in the haproxy configuration file, and an LUA function.
6409 */
6410__LJMP static int hlua_register_converters(lua_State *L)
6411{
6412 struct sample_conv_kw_list *sck;
6413 const char *name;
6414 int ref;
6415 int len;
6416 struct hlua_function *fcn;
6417
6418 MAY_LJMP(check_args(L, 2, "register_converters"));
6419
6420 /* First argument : converter name. */
6421 name = MAY_LJMP(luaL_checkstring(L, 1));
6422
6423 /* Second argument : lua function. */
6424 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6425
6426 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006427 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006428 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006429 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006430 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006431 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006432 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006433
6434 /* Fill fcn. */
6435 fcn->name = strdup(name);
6436 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006437 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006438 fcn->function_ref = ref;
6439
6440 /* List head */
6441 sck->list.n = sck->list.p = NULL;
6442
6443 /* converter keyword. */
6444 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006445 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006446 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006447 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006448
6449 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6450 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006451 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 +01006452 sck->kw[0].val_args = NULL;
6453 sck->kw[0].in_type = SMP_T_STR;
6454 sck->kw[0].out_type = SMP_T_STR;
6455 sck->kw[0].private = fcn;
6456
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006457 /* Register this new converter */
6458 sample_register_convs(sck);
6459
6460 return 0;
6461}
6462
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006463/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006464 * "sample-fetch" functions. It expects a converter name used
6465 * in the haproxy configuration file, and an LUA function.
6466 */
6467__LJMP static int hlua_register_fetches(lua_State *L)
6468{
6469 const char *name;
6470 int ref;
6471 int len;
6472 struct sample_fetch_kw_list *sfk;
6473 struct hlua_function *fcn;
6474
6475 MAY_LJMP(check_args(L, 2, "register_fetches"));
6476
6477 /* First argument : sample-fetch name. */
6478 name = MAY_LJMP(luaL_checkstring(L, 1));
6479
6480 /* Second argument : lua function. */
6481 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6482
6483 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006484 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006485 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006486 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006487 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006488 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006489 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006490
6491 /* Fill fcn. */
6492 fcn->name = strdup(name);
6493 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006494 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006495 fcn->function_ref = ref;
6496
6497 /* List head */
6498 sfk->list.n = sfk->list.p = NULL;
6499
6500 /* sample-fetch keyword. */
6501 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006502 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006503 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006504 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006505
6506 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6507 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006508 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 +01006509 sfk->kw[0].val_args = NULL;
6510 sfk->kw[0].out_type = SMP_T_STR;
6511 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6512 sfk->kw[0].val = 0;
6513 sfk->kw[0].private = fcn;
6514
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006515 /* Register this new fetch. */
6516 sample_register_fetches(sfk);
6517
6518 return 0;
6519}
6520
Christopher Faulet501465d2020-02-26 14:54:16 +01006521/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006522 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006523__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006524{
6525 struct hlua *hlua = hlua_gethlua(L);
6526 unsigned int delay;
6527 unsigned int wakeup_ms;
6528
6529 MAY_LJMP(check_args(L, 1, "wake_time"));
6530
6531 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6532 wakeup_ms = tick_add(now_ms, delay);
6533 hlua->wake_time = wakeup_ms;
6534 return 0;
6535}
6536
6537
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006538/* This function is a wrapper to execute each LUA function declared as an action
6539 * wrapper during the initialisation period. This function may return any
6540 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6541 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6542 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006543 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006544static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006545 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006546{
6547 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006548 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006549 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006550 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006551
6552 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006553 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6554 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6555 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6556 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006557 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006558 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006559 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006560 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006561
Willy Tarreau87b09662015-04-03 00:22:06 +02006562 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006563 * Lua context can be not initialized. This behavior
6564 * permits to save performances because a systematic
6565 * Lua initialization cause 5% performances loss.
6566 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006567 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006568 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006569 if (!s->hlua) {
6570 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6571 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006572 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006573 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006574 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006575 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6576 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006577 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006578 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006579 }
6580
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006581 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006582 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006583
6584 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006585 if (!SET_SAFE_LJMP(s->hlua->T)) {
6586 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6587 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006588 else
6589 error = "critical error";
6590 SEND_ERR(px, "Lua function '%s': %s.\n",
6591 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006592 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006593 }
6594
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006595 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006596 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006597 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006598 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006599 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006600 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006601 }
6602
6603 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006604 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006605
Willy Tarreau87b09662015-04-03 00:22:06 +02006606 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006607 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006608 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006609 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006610 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006611 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006612 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006613 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006614
6615 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006616 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006617 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006618 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006619 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006620 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006621 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006622 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006623 lua_pushstring(s->hlua->T, *arg);
6624 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006625 }
6626
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006627 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006628 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006629
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006630 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006631 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006632 }
6633
Christopher Faulet23308eb2020-06-02 18:46:07 +02006634 /* Always reset the analyse expiration timeout for the corresponding
6635 * channel in case the lua script yield, to be sure to not keep an
6636 * expired timeout.
6637 */
6638 if (dir == SMP_OPT_DIR_REQ)
6639 s->req.analyse_exp = TICK_ETERNITY;
6640 else
6641 s->res.analyse_exp = TICK_ETERNITY;
6642
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006643 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006644 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006645 /* finished. */
6646 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006647 /* Catch the return value */
6648 if (lua_gettop(s->hlua->T) > 0)
6649 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006650
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006651 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006652 if (act_ret == ACT_RET_YIELD) {
6653 if (flags & ACT_OPT_FINAL)
6654 goto err_yield;
6655
6656 if (s->hlua->wake_time != TICK_ETERNITY) {
6657 if (dir == SMP_OPT_DIR_REQ)
6658 s->req.analyse_exp = s->hlua->wake_time;
6659 else
6660 s->res.analyse_exp = s->hlua->wake_time;
6661 }
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006662 }
6663 goto end;
6664
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006665 /* yield. */
6666 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006667 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006668 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Faulet81921b12019-08-14 23:19:45 +02006669 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006670 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006671 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006672 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006673 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006674 /* Some actions can be wake up when a "write" event
6675 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006676 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006677 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006678 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006679 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006680 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006681 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006682 act_ret = ACT_RET_YIELD;
6683 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006684
6685 /* finished with error. */
6686 case HLUA_E_ERRMSG:
6687 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006688 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006689 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6690 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006691 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006692
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006693 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006694 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006695 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006696
6697 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006698 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006699 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006700
6701 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02006702 err_yield:
6703 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006704 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6705 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006706 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006707
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006708 case HLUA_E_ERR:
6709 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006710 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006711 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006712
6713 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006714 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006715 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006716
6717 end:
6718 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006719}
6720
Olivier Houchard9f6af332018-05-25 14:04:04 +02006721struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006722{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006723 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006724
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006725 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006726 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006727 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006728}
6729
6730static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6731{
6732 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006733 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006734 struct task *task;
6735 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006736 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006737
Willy Tarreaubafbe012017-11-24 17:34:44 +01006738 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006739 if (!hlua) {
6740 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6741 ctx->rule->arg.hlua_rule->fcn.name);
6742 return 0;
6743 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006744 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006745 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006746 ctx->ctx.hlua_apptcp.flags = 0;
6747
6748 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006749 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006750 if (!task) {
6751 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6752 ctx->rule->arg.hlua_rule->fcn.name);
6753 return 0;
6754 }
6755 task->nice = 0;
6756 task->context = ctx;
6757 task->process = hlua_applet_wakeup;
6758 ctx->ctx.hlua_apptcp.task = task;
6759
6760 /* In the execution wrappers linked with a stream, the
6761 * Lua context can be not initialized. This behavior
6762 * permits to save performances because a systematic
6763 * Lua initialization cause 5% performances loss.
6764 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006765 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006766 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6767 ctx->rule->arg.hlua_rule->fcn.name);
6768 return 0;
6769 }
6770
6771 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006772 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006773
6774 /* The following Lua calls can fail. */
6775 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006776 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6777 error = lua_tostring(hlua->T, -1);
6778 else
6779 error = "critical error";
6780 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6781 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006782 return 0;
6783 }
6784
6785 /* Check stack available size. */
6786 if (!lua_checkstack(hlua->T, 1)) {
6787 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6788 ctx->rule->arg.hlua_rule->fcn.name);
6789 RESET_SAFE_LJMP(hlua->T);
6790 return 0;
6791 }
6792
6793 /* Restore the function in the stack. */
6794 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6795
6796 /* Create and and push object stream in the stack. */
6797 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6798 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6799 ctx->rule->arg.hlua_rule->fcn.name);
6800 RESET_SAFE_LJMP(hlua->T);
6801 return 0;
6802 }
6803 hlua->nargs = 1;
6804
6805 /* push keywords in the stack. */
6806 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6807 if (!lua_checkstack(hlua->T, 1)) {
6808 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6809 ctx->rule->arg.hlua_rule->fcn.name);
6810 RESET_SAFE_LJMP(hlua->T);
6811 return 0;
6812 }
6813 lua_pushstring(hlua->T, *arg);
6814 hlua->nargs++;
6815 }
6816
6817 RESET_SAFE_LJMP(hlua->T);
6818
6819 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006820 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006821 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006822
6823 return 1;
6824}
6825
Willy Tarreau60409db2019-08-21 14:14:50 +02006826void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006827{
6828 struct stream_interface *si = ctx->owner;
6829 struct stream *strm = si_strm(si);
6830 struct channel *res = si_ic(si);
6831 struct act_rule *rule = ctx->rule;
6832 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006833 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006834
6835 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006836 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6837 /* eat the whole request */
6838 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006839 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006840 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006841
6842 /* If the stream is disconnect or closed, ldo nothing. */
6843 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6844 return;
6845
6846 /* Execute the function. */
6847 switch (hlua_ctx_resume(hlua, 1)) {
6848 /* finished. */
6849 case HLUA_E_OK:
6850 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6851
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006852 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006853 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006854 res->flags |= CF_READ_NULL;
6855 si_shutr(si);
6856 return;
6857
6858 /* yield. */
6859 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006860 if (hlua->wake_time != TICK_ETERNITY)
6861 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006862 return;
6863
6864 /* finished with error. */
6865 case HLUA_E_ERRMSG:
6866 /* Display log. */
6867 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6868 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6869 lua_pop(hlua->T, 1);
6870 goto error;
6871
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006872 case HLUA_E_ETMOUT:
6873 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6874 rule->arg.hlua_rule->fcn.name);
6875 goto error;
6876
6877 case HLUA_E_NOMEM:
6878 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6879 rule->arg.hlua_rule->fcn.name);
6880 goto error;
6881
6882 case HLUA_E_YIELD: /* unexpected */
6883 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6884 rule->arg.hlua_rule->fcn.name);
6885 goto error;
6886
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006887 case HLUA_E_ERR:
6888 /* Display log. */
6889 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6890 rule->arg.hlua_rule->fcn.name);
6891 goto error;
6892
6893 default:
6894 goto error;
6895 }
6896
6897error:
6898
6899 /* For all other cases, just close the stream. */
6900 si_shutw(si);
6901 si_shutr(si);
6902 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6903}
6904
6905static void hlua_applet_tcp_release(struct appctx *ctx)
6906{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006907 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006908 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006909 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006910 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006911}
6912
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006913/* The function returns 1 if the initialisation is complete, 0 if
6914 * an errors occurs and -1 if more data are required for initializing
6915 * the applet.
6916 */
6917static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6918{
6919 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006920 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006921 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006922 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006923 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006924 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006925
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006926 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006927 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006928 if (!hlua) {
6929 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6930 ctx->rule->arg.hlua_rule->fcn.name);
6931 return 0;
6932 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006933 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006934 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006935 ctx->ctx.hlua_apphttp.left_bytes = -1;
6936 ctx->ctx.hlua_apphttp.flags = 0;
6937
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006938 if (txn->req.flags & HTTP_MSGF_VER_11)
6939 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6940
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006941 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006942 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006943 if (!task) {
6944 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6945 ctx->rule->arg.hlua_rule->fcn.name);
6946 return 0;
6947 }
6948 task->nice = 0;
6949 task->context = ctx;
6950 task->process = hlua_applet_wakeup;
6951 ctx->ctx.hlua_apphttp.task = task;
6952
6953 /* In the execution wrappers linked with a stream, the
6954 * Lua context can be not initialized. This behavior
6955 * permits to save performances because a systematic
6956 * Lua initialization cause 5% performances loss.
6957 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006958 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006959 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6960 ctx->rule->arg.hlua_rule->fcn.name);
6961 return 0;
6962 }
6963
6964 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006965 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006966
6967 /* The following Lua calls can fail. */
6968 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006969 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6970 error = lua_tostring(hlua->T, -1);
6971 else
6972 error = "critical error";
6973 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6974 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006975 return 0;
6976 }
6977
6978 /* Check stack available size. */
6979 if (!lua_checkstack(hlua->T, 1)) {
6980 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6981 ctx->rule->arg.hlua_rule->fcn.name);
6982 RESET_SAFE_LJMP(hlua->T);
6983 return 0;
6984 }
6985
6986 /* Restore the function in the stack. */
6987 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6988
6989 /* Create and and push object stream in the stack. */
6990 if (!hlua_applet_http_new(hlua->T, ctx)) {
6991 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6992 ctx->rule->arg.hlua_rule->fcn.name);
6993 RESET_SAFE_LJMP(hlua->T);
6994 return 0;
6995 }
6996 hlua->nargs = 1;
6997
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006998 /* push keywords in the stack. */
6999 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7000 if (!lua_checkstack(hlua->T, 1)) {
7001 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7002 ctx->rule->arg.hlua_rule->fcn.name);
7003 RESET_SAFE_LJMP(hlua->T);
7004 return 0;
7005 }
7006 lua_pushstring(hlua->T, *arg);
7007 hlua->nargs++;
7008 }
7009
7010 RESET_SAFE_LJMP(hlua->T);
7011
7012 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007013 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007014
7015 return 1;
7016}
7017
Willy Tarreau60409db2019-08-21 14:14:50 +02007018void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007019{
7020 struct stream_interface *si = ctx->owner;
7021 struct stream *strm = si_strm(si);
7022 struct channel *req = si_oc(si);
7023 struct channel *res = si_ic(si);
7024 struct act_rule *rule = ctx->rule;
7025 struct proxy *px = strm->be;
7026 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7027 struct htx *req_htx, *res_htx;
7028
7029 res_htx = htx_from_buf(&res->buf);
7030
7031 /* If the stream is disconnect or closed, ldo nothing. */
7032 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7033 goto out;
7034
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007035 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007036 if (!b_size(&res->buf)) {
7037 si_rx_room_blk(si);
7038 goto out;
7039 }
7040 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007041 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007042 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7043
7044 /* Set the currently running flag. */
7045 if (!HLUA_IS_RUNNING(hlua) &&
7046 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7047 struct htx_blk *blk;
7048 size_t count = co_data(req);
7049
7050 if (!count) {
7051 si_cant_get(si);
7052 goto out;
7053 }
7054
7055 /* We need to flush the request header. This left the body for
7056 * the Lua.
7057 */
7058 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007059 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007060 while (count && blk) {
7061 enum htx_blk_type type = htx_get_blk_type(blk);
7062 uint32_t sz = htx_get_blksz(blk);
7063
7064 if (sz > count) {
7065 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007066 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007067 goto out;
7068 }
7069
7070 count -= sz;
7071 co_set_data(req, co_data(req) - sz);
7072 blk = htx_remove_blk(req_htx, blk);
7073
7074 if (type == HTX_BLK_EOH)
7075 break;
7076 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007077 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007078 }
7079
7080 /* Executes The applet if it is not done. */
7081 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7082
7083 /* Execute the function. */
7084 switch (hlua_ctx_resume(hlua, 1)) {
7085 /* finished. */
7086 case HLUA_E_OK:
7087 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7088 break;
7089
7090 /* yield. */
7091 case HLUA_E_AGAIN:
7092 if (hlua->wake_time != TICK_ETERNITY)
7093 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007094 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007095
7096 /* finished with error. */
7097 case HLUA_E_ERRMSG:
7098 /* Display log. */
7099 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7100 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7101 lua_pop(hlua->T, 1);
7102 goto error;
7103
7104 case HLUA_E_ETMOUT:
7105 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7106 rule->arg.hlua_rule->fcn.name);
7107 goto error;
7108
7109 case HLUA_E_NOMEM:
7110 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7111 rule->arg.hlua_rule->fcn.name);
7112 goto error;
7113
7114 case HLUA_E_YIELD: /* unexpected */
7115 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7116 rule->arg.hlua_rule->fcn.name);
7117 goto error;
7118
7119 case HLUA_E_ERR:
7120 /* Display log. */
7121 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7122 rule->arg.hlua_rule->fcn.name);
7123 goto error;
7124
7125 default:
7126 goto error;
7127 }
7128 }
7129
7130 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007131 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7132 goto done;
7133
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007134 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7135 goto error;
7136
Christopher Faulet54b5e212019-06-04 10:08:28 +02007137 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02007138 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007139 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7140 si_rx_room_blk(si);
7141 goto out;
7142 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007143 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007144 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7145 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007146 }
7147
7148 done:
7149 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007150 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007151 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007152 si_shutr(si);
7153 }
7154
7155 /* eat the whole request */
7156 if (co_data(req)) {
7157 req_htx = htx_from_buf(&req->buf);
7158 co_htx_skip(req, req_htx, co_data(req));
7159 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007160 }
7161 }
7162
7163 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007164 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007165 return;
7166
7167 error:
7168
7169 /* If we are in HTTP mode, and we are not send any
7170 * data, return a 500 server error in best effort:
7171 * if there is no room available in the buffer,
7172 * just close the connection.
7173 */
7174 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007175 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007176
7177 channel_erase(res);
7178 res->buf.data = b_data(err);
7179 memcpy(res->buf.area, b_head(err), b_data(err));
7180 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007181 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007182 }
7183 if (!(strm->flags & SF_ERR_MASK))
7184 strm->flags |= SF_ERR_RESOURCE;
7185 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7186 goto done;
7187}
7188
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007189static void hlua_applet_http_release(struct appctx *ctx)
7190{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007191 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007192 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007193 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007194 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007195}
7196
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007197/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007198 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007199 *
7200 * This function can fail with an abort() due to an Lua critical error.
7201 * We are in the configuration parsing process of HAProxy, this abort() is
7202 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007203 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007204static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7205 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007206{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007207 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007208 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007209
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007210 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007211 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007212 if (!rule->arg.hlua_rule) {
7213 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007214 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007215 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007216
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007217 /* Memory for arguments. */
7218 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7219 if (!rule->arg.hlua_rule->args) {
7220 memprintf(err, "out of memory error");
7221 return ACT_RET_PRS_ERR;
7222 }
7223
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007224 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007225 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007226
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007227 /* Expect some arguments */
7228 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007229 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007230 memprintf(err, "expect %d arguments", fcn->nargs);
7231 return ACT_RET_PRS_ERR;
7232 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007233 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007234 if (!rule->arg.hlua_rule->args[i]) {
7235 memprintf(err, "out of memory error");
7236 return ACT_RET_PRS_ERR;
7237 }
7238 (*cur_arg)++;
7239 }
7240 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007241
Thierry FOURNIER42148732015-09-02 17:17:33 +02007242 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007243 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007244 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007245}
7246
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007247static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7248 struct act_rule *rule, char **err)
7249{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007250 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007251
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007252 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007253 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007254 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007255 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007256 * the call of this analyzer.
7257 */
7258 if (rule->from != ACT_F_HTTP_REQ) {
7259 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7260 return ACT_RET_PRS_ERR;
7261 }
7262
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007263 /* Memory for the rule. */
7264 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7265 if (!rule->arg.hlua_rule) {
7266 memprintf(err, "out of memory error");
7267 return ACT_RET_PRS_ERR;
7268 }
7269
7270 /* Reference the Lua function and store the reference. */
7271 rule->arg.hlua_rule->fcn = *fcn;
7272
7273 /* TODO: later accept arguments. */
7274 rule->arg.hlua_rule->args = NULL;
7275
7276 /* Add applet pointer in the rule. */
7277 rule->applet.obj_type = OBJ_TYPE_APPLET;
7278 rule->applet.name = fcn->name;
7279 rule->applet.init = hlua_applet_http_init;
7280 rule->applet.fct = hlua_applet_http_fct;
7281 rule->applet.release = hlua_applet_http_release;
7282 rule->applet.timeout = hlua_timeout_applet;
7283
7284 return ACT_RET_PRS_OK;
7285}
7286
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007287/* This function is an LUA binding used for registering
7288 * "sample-conv" functions. It expects a converter name used
7289 * in the haproxy configuration file, and an LUA function.
7290 */
7291__LJMP static int hlua_register_action(lua_State *L)
7292{
7293 struct action_kw_list *akl;
7294 const char *name;
7295 int ref;
7296 int len;
7297 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007298 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007299
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007300 /* Initialise the number of expected arguments at 0. */
7301 nargs = 0;
7302
7303 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7304 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007305
7306 /* First argument : converter name. */
7307 name = MAY_LJMP(luaL_checkstring(L, 1));
7308
7309 /* Second argument : environment. */
7310 if (lua_type(L, 2) != LUA_TTABLE)
7311 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7312
7313 /* Third argument : lua function. */
7314 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7315
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007316 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007317 if (lua_gettop(L) >= 4)
7318 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7319
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007320 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007321 lua_pushnil(L);
7322 while (lua_next(L, 2) != 0) {
7323 if (lua_type(L, -1) != LUA_TSTRING)
7324 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7325
7326 /* Check required environment. Only accepted "http" or "tcp". */
7327 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007328 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007329 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007330 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007331 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007332 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007333 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007334
7335 /* Fill fcn. */
7336 fcn->name = strdup(name);
7337 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007338 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007339 fcn->function_ref = ref;
7340
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007341 /* Set the expected number od arguments. */
7342 fcn->nargs = nargs;
7343
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007344 /* List head */
7345 akl->list.n = akl->list.p = NULL;
7346
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007347 /* action keyword. */
7348 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007349 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007350 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007351 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007352
7353 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7354
7355 akl->kw[0].match_pfx = 0;
7356 akl->kw[0].private = fcn;
7357 akl->kw[0].parse = action_register_lua;
7358
7359 /* select the action registering point. */
7360 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7361 tcp_req_cont_keywords_register(akl);
7362 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7363 tcp_res_cont_keywords_register(akl);
7364 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7365 http_req_keywords_register(akl);
7366 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7367 http_res_keywords_register(akl);
7368 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007369 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007370 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7371 "are expected.", lua_tostring(L, -1)));
7372
7373 /* pop the environment string. */
7374 lua_pop(L, 1);
7375 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007376 return ACT_RET_PRS_OK;
7377}
7378
7379static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7380 struct act_rule *rule, char **err)
7381{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007382 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007383
Christopher Faulet280f85b2019-07-15 15:02:04 +02007384 if (px->mode == PR_MODE_HTTP) {
7385 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007386 return ACT_RET_PRS_ERR;
7387 }
7388
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007389 /* Memory for the rule. */
7390 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7391 if (!rule->arg.hlua_rule) {
7392 memprintf(err, "out of memory error");
7393 return ACT_RET_PRS_ERR;
7394 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007395
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007396 /* Reference the Lua function and store the reference. */
7397 rule->arg.hlua_rule->fcn = *fcn;
7398
7399 /* TODO: later accept arguments. */
7400 rule->arg.hlua_rule->args = NULL;
7401
7402 /* Add applet pointer in the rule. */
7403 rule->applet.obj_type = OBJ_TYPE_APPLET;
7404 rule->applet.name = fcn->name;
7405 rule->applet.init = hlua_applet_tcp_init;
7406 rule->applet.fct = hlua_applet_tcp_fct;
7407 rule->applet.release = hlua_applet_tcp_release;
7408 rule->applet.timeout = hlua_timeout_applet;
7409
7410 return 0;
7411}
7412
7413/* This function is an LUA binding used for registering
7414 * "sample-conv" functions. It expects a converter name used
7415 * in the haproxy configuration file, and an LUA function.
7416 */
7417__LJMP static int hlua_register_service(lua_State *L)
7418{
7419 struct action_kw_list *akl;
7420 const char *name;
7421 const char *env;
7422 int ref;
7423 int len;
7424 struct hlua_function *fcn;
7425
7426 MAY_LJMP(check_args(L, 3, "register_service"));
7427
7428 /* First argument : converter name. */
7429 name = MAY_LJMP(luaL_checkstring(L, 1));
7430
7431 /* Second argument : environment. */
7432 env = MAY_LJMP(luaL_checkstring(L, 2));
7433
7434 /* Third argument : lua function. */
7435 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7436
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007437 /* Allocate and fill the sample fetch keyword struct. */
7438 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7439 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007440 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007441 fcn = calloc(1, sizeof(*fcn));
7442 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007443 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007444
7445 /* Fill fcn. */
7446 len = strlen("<lua.>") + strlen(name) + 1;
7447 fcn->name = calloc(1, len);
7448 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007449 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007450 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7451 fcn->function_ref = ref;
7452
7453 /* List head */
7454 akl->list.n = akl->list.p = NULL;
7455
7456 /* converter keyword. */
7457 len = strlen("lua.") + strlen(name) + 1;
7458 akl->kw[0].kw = calloc(1, len);
7459 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007460 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007461
7462 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7463
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007464 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007465 if (strcmp(env, "tcp") == 0)
7466 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007467 else if (strcmp(env, "http") == 0)
7468 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007469 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007470 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007471 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007472
7473 akl->kw[0].match_pfx = 0;
7474 akl->kw[0].private = fcn;
7475
7476 /* End of array. */
7477 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7478
7479 /* Register this new converter */
7480 service_keywords_register(akl);
7481
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007482 return 0;
7483}
7484
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007485/* This function initialises Lua cli handler. It copies the
7486 * arguments in the Lua stack and create channel IO objects.
7487 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007488static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007489{
7490 struct hlua *hlua;
7491 struct hlua_function *fcn;
7492 int i;
7493 const char *error;
7494
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007495 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007496 appctx->ctx.hlua_cli.fcn = private;
7497
Willy Tarreaubafbe012017-11-24 17:34:44 +01007498 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007499 if (!hlua) {
7500 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007501 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007502 }
7503 HLUA_INIT(hlua);
7504 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007505
7506 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007507 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007508 * applet_http. It is absolutely compatible.
7509 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007510 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007511 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007512 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007513 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007514 }
7515 appctx->ctx.hlua_cli.task->nice = 0;
7516 appctx->ctx.hlua_cli.task->context = appctx;
7517 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7518
7519 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007520 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007521 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007522 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007523 }
7524
7525 /* The following Lua calls can fail. */
7526 if (!SET_SAFE_LJMP(hlua->T)) {
7527 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7528 error = lua_tostring(hlua->T, -1);
7529 else
7530 error = "critical error";
7531 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7532 goto error;
7533 }
7534
7535 /* Check stack available size. */
7536 if (!lua_checkstack(hlua->T, 2)) {
7537 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7538 goto error;
7539 }
7540
7541 /* Restore the function in the stack. */
7542 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7543
7544 /* Once the arguments parsed, the CLI is like an AppletTCP,
7545 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007546 */
7547 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7548 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7549 goto error;
7550 }
7551 hlua->nargs = 1;
7552
7553 /* push keywords in the stack. */
7554 for (i = 0; *args[i]; i++) {
7555 /* Check stack available size. */
7556 if (!lua_checkstack(hlua->T, 1)) {
7557 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7558 goto error;
7559 }
7560 lua_pushstring(hlua->T, args[i]);
7561 hlua->nargs++;
7562 }
7563
7564 /* We must initialize the execution timeouts. */
7565 hlua->max_time = hlua_timeout_session;
7566
7567 /* At this point the execution is safe. */
7568 RESET_SAFE_LJMP(hlua->T);
7569
7570 /* It's ok */
7571 return 0;
7572
7573 /* It's not ok. */
7574error:
7575 RESET_SAFE_LJMP(hlua->T);
7576 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007577 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007578 return 1;
7579}
7580
7581static int hlua_cli_io_handler_fct(struct appctx *appctx)
7582{
7583 struct hlua *hlua;
7584 struct stream_interface *si;
7585 struct hlua_function *fcn;
7586
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007587 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007588 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007589 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007590
7591 /* If the stream is disconnect or closed, ldo nothing. */
7592 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7593 return 1;
7594
7595 /* Execute the function. */
7596 switch (hlua_ctx_resume(hlua, 1)) {
7597
7598 /* finished. */
7599 case HLUA_E_OK:
7600 return 1;
7601
7602 /* yield. */
7603 case HLUA_E_AGAIN:
7604 /* We want write. */
7605 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007606 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007607 /* Set the timeout. */
7608 if (hlua->wake_time != TICK_ETERNITY)
7609 task_schedule(hlua->task, hlua->wake_time);
7610 return 0;
7611
7612 /* finished with error. */
7613 case HLUA_E_ERRMSG:
7614 /* Display log. */
7615 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7616 fcn->name, lua_tostring(hlua->T, -1));
7617 lua_pop(hlua->T, 1);
7618 return 1;
7619
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007620 case HLUA_E_ETMOUT:
7621 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7622 fcn->name);
7623 return 1;
7624
7625 case HLUA_E_NOMEM:
7626 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7627 fcn->name);
7628 return 1;
7629
7630 case HLUA_E_YIELD: /* unexpected */
7631 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7632 fcn->name);
7633 return 1;
7634
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007635 case HLUA_E_ERR:
7636 /* Display log. */
7637 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7638 fcn->name);
7639 return 1;
7640
7641 default:
7642 return 1;
7643 }
7644
7645 return 1;
7646}
7647
7648static void hlua_cli_io_release_fct(struct appctx *appctx)
7649{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007650 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007651 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007652}
7653
7654/* This function is an LUA binding used for registering
7655 * new keywords in the cli. It expects a list of keywords
7656 * which are the "path". It is limited to 5 keywords. A
7657 * description of the command, a function to be executed
7658 * for the parsing and a function for io handlers.
7659 */
7660__LJMP static int hlua_register_cli(lua_State *L)
7661{
7662 struct cli_kw_list *cli_kws;
7663 const char *message;
7664 int ref_io;
7665 int len;
7666 struct hlua_function *fcn;
7667 int index;
7668 int i;
7669
7670 MAY_LJMP(check_args(L, 3, "register_cli"));
7671
7672 /* First argument : an array of maximum 5 keywords. */
7673 if (!lua_istable(L, 1))
7674 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7675
7676 /* Second argument : string with contextual message. */
7677 message = MAY_LJMP(luaL_checkstring(L, 2));
7678
7679 /* Third and fourth argument : lua function. */
7680 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7681
7682 /* Allocate and fill the sample fetch keyword struct. */
7683 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7684 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007685 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007686 fcn = calloc(1, sizeof(*fcn));
7687 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007688 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007689
7690 /* Fill path. */
7691 index = 0;
7692 lua_pushnil(L);
7693 while(lua_next(L, 1) != 0) {
7694 if (index >= 5)
7695 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7696 if (lua_type(L, -1) != LUA_TSTRING)
7697 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7698 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7699 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007700 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007701 index++;
7702 lua_pop(L, 1);
7703 }
7704
7705 /* Copy help message. */
7706 cli_kws->kw[0].usage = strdup(message);
7707 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007708 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007709
7710 /* Fill fcn io handler. */
7711 len = strlen("<lua.cli>") + 1;
7712 for (i = 0; i < index; i++)
7713 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7714 fcn->name = calloc(1, len);
7715 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007716 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007717 strncat((char *)fcn->name, "<lua.cli", len);
7718 for (i = 0; i < index; i++) {
7719 strncat((char *)fcn->name, ".", len);
7720 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7721 }
7722 strncat((char *)fcn->name, ">", len);
7723 fcn->function_ref = ref_io;
7724
7725 /* Fill last entries. */
7726 cli_kws->kw[0].private = fcn;
7727 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7728 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7729 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7730
7731 /* Register this new converter */
7732 cli_register_kw(cli_kws);
7733
7734 return 0;
7735}
7736
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007737static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7738 struct proxy *defpx, const char *file, int line,
7739 char **err, unsigned int *timeout)
7740{
7741 const char *error;
7742
7743 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007744 if (error == PARSE_TIME_OVER) {
7745 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7746 args[1], args[0]);
7747 return -1;
7748 }
7749 else if (error == PARSE_TIME_UNDER) {
7750 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7751 args[1], args[0]);
7752 return -1;
7753 }
7754 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007755 memprintf(err, "%s: invalid timeout", args[0]);
7756 return -1;
7757 }
7758 return 0;
7759}
7760
7761static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7762 struct proxy *defpx, const char *file, int line,
7763 char **err)
7764{
7765 return hlua_read_timeout(args, section_type, curpx, defpx,
7766 file, line, err, &hlua_timeout_session);
7767}
7768
7769static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7770 struct proxy *defpx, const char *file, int line,
7771 char **err)
7772{
7773 return hlua_read_timeout(args, section_type, curpx, defpx,
7774 file, line, err, &hlua_timeout_task);
7775}
7776
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007777static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7778 struct proxy *defpx, const char *file, int line,
7779 char **err)
7780{
7781 return hlua_read_timeout(args, section_type, curpx, defpx,
7782 file, line, err, &hlua_timeout_applet);
7783}
7784
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007785static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7786 struct proxy *defpx, const char *file, int line,
7787 char **err)
7788{
7789 char *error;
7790
7791 hlua_nb_instruction = strtoll(args[1], &error, 10);
7792 if (*error != '\0') {
7793 memprintf(err, "%s: invalid number", args[0]);
7794 return -1;
7795 }
7796 return 0;
7797}
7798
Willy Tarreau32f61e22015-03-18 17:54:59 +01007799static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7800 struct proxy *defpx, const char *file, int line,
7801 char **err)
7802{
7803 char *error;
7804
7805 if (*(args[1]) == 0) {
7806 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7807 return -1;
7808 }
7809 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7810 if (*error != '\0') {
7811 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7812 return -1;
7813 }
7814 return 0;
7815}
7816
7817
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007818/* This function is called by the main configuration key "lua-load". It loads and
7819 * execute an lua file during the parsing of the HAProxy configuration file. It is
7820 * the main lua entry point.
7821 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007822 * This function runs with the HAProxy keywords API. It returns -1 if an error
7823 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007824 *
7825 * In some error case, LUA set an error message in top of the stack. This function
7826 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007827 *
7828 * This function can fail with an abort() due to an Lua critical error.
7829 * We are in the configuration parsing process of HAProxy, this abort() is
7830 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007831 */
7832static int hlua_load(char **args, int section_type, struct proxy *curpx,
7833 struct proxy *defpx, const char *file, int line,
7834 char **err)
7835{
7836 int error;
7837
7838 /* Just load and compile the file. */
7839 error = luaL_loadfile(gL.T, args[1]);
7840 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007841 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007842 lua_pop(gL.T, 1);
7843 return -1;
7844 }
7845
7846 /* If no syntax error where detected, execute the code. */
7847 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7848 switch (error) {
7849 case LUA_OK:
7850 break;
7851 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007852 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007853 lua_pop(gL.T, 1);
7854 return -1;
7855 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007856 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007857 return -1;
7858 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007859 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007860 lua_pop(gL.T, 1);
7861 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02007862#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007863 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007864 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007865 lua_pop(gL.T, 1);
7866 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02007867#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007868 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05007869 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007870 lua_pop(gL.T, 1);
7871 return -1;
7872 }
7873
7874 return 0;
7875}
7876
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01007877/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
7878 * in the given <ctx>.
7879 */
7880static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
7881{
7882 lua_getglobal(ctx.T, "package"); /* push package variable */
7883 lua_pushstring(ctx.T, path); /* push given path */
7884 lua_pushstring(ctx.T, ";"); /* push semicolon */
7885 lua_getfield(ctx.T, -3, type); /* push old path */
7886 lua_concat(ctx.T, 3); /* concatenate to new path */
7887 lua_setfield(ctx.T, -2, type); /* store new path */
7888 lua_pop(ctx.T, 1); /* pop package variable */
7889
7890 return 0;
7891}
7892
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007893static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
7894 struct proxy *defpx, const char *file, int line,
7895 char **err)
7896{
7897 char *path;
7898 char *type = "path";
7899 if (too_many_args(2, args, err, NULL)) {
7900 return -1;
7901 }
7902
7903 if (!(*args[1])) {
7904 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
7905 return -1;
7906 }
7907 path = args[1];
7908
7909 if (*args[2]) {
7910 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
7911 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
7912 return -1;
7913 }
7914 type = args[2];
7915 }
7916
7917 return hlua_prepend_path(gL, type, path);
7918}
7919
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007920/* configuration keywords declaration */
7921static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007922 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007923 { CFG_GLOBAL, "lua-load", hlua_load },
7924 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7925 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007926 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007927 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007928 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007929 { 0, NULL, NULL },
7930}};
7931
Willy Tarreau0108d902018-11-25 19:14:37 +01007932INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7933
Christopher Fauletafd8f102018-11-08 11:34:21 +01007934
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007935/* This function can fail with an abort() due to an Lua critical error.
7936 * We are in the initialisation process of HAProxy, this abort() is
7937 * tolerated.
7938 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007939int hlua_post_init()
7940{
7941 struct hlua_init_function *init;
7942 const char *msg;
7943 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007944 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007945
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007946 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007947 if (!SET_SAFE_LJMP(gL.T)) {
7948 if (lua_type(gL.T, -1) == LUA_TSTRING)
7949 error = lua_tostring(gL.T, -1);
7950 else
7951 error = "critical error";
7952 fprintf(stderr, "Lua post-init: %s.\n", error);
7953 exit(1);
7954 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007955
7956#if USE_OPENSSL
7957 /* Initialize SSL server. */
7958 if (socket_ssl.xprt->prepare_srv) {
7959 int saved_used_backed = global.ssl_used_backend;
7960 // don't affect maxconn automatic computation
7961 socket_ssl.xprt->prepare_srv(&socket_ssl);
7962 global.ssl_used_backend = saved_used_backed;
7963 }
7964#endif
7965
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007966 hlua_fcn_post_init(gL.T);
7967 RESET_SAFE_LJMP(gL.T);
7968
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007969 list_for_each_entry(init, &hlua_init_functions, l) {
7970 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7971 ret = hlua_ctx_resume(&gL, 0);
7972 switch (ret) {
7973 case HLUA_E_OK:
7974 lua_pop(gL.T, -1);
7975 return 1;
7976 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007977 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007978 return 0;
7979 case HLUA_E_ERRMSG:
7980 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007981 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007982 return 0;
7983 case HLUA_E_ERR:
7984 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007985 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007986 return 0;
7987 }
7988 }
7989 return 1;
7990}
7991
Willy Tarreau32f61e22015-03-18 17:54:59 +01007992/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7993 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7994 * is the previously allocated size or the kind of object in case of a new
7995 * allocation. <nsize> is the requested new size.
7996 */
7997static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7998{
7999 struct hlua_mem_allocator *zone = ud;
8000
8001 if (nsize == 0) {
8002 /* it's a free */
8003 if (ptr)
8004 zone->allocated -= osize;
8005 free(ptr);
8006 return NULL;
8007 }
8008
8009 if (!ptr) {
8010 /* it's a new allocation */
8011 if (zone->limit && zone->allocated + nsize > zone->limit)
8012 return NULL;
8013
8014 ptr = malloc(nsize);
8015 if (ptr)
8016 zone->allocated += nsize;
8017 return ptr;
8018 }
8019
8020 /* it's a realloc */
8021 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8022 return NULL;
8023
8024 ptr = realloc(ptr, nsize);
8025 if (ptr)
8026 zone->allocated += nsize - osize;
8027 return ptr;
8028}
8029
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008030/* Ithis function can fail with an abort() due to an Lua critical error.
8031 * We are in the initialisation process of HAProxy, this abort() is
8032 * tolerated.
8033 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008034void hlua_init(void)
8035{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008036 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008037 int idx;
8038 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008039 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008040 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008041 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008042#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008043 struct srv_kw *kw;
8044 int tmp_error;
8045 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008046 char *args[] = { /* SSL client configuration. */
8047 "ssl",
8048 "verify",
8049 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008050 NULL
8051 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008052#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008053
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008054 /* Init main lua stack. */
8055 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008056 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008057 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008058 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008059 hlua_sethlua(&gL);
8060 gL.Tref = LUA_REFNIL;
8061 gL.task = NULL;
8062
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008063 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008064 * the Lua function can fail with an abort. We are in the initialisation
8065 * process of HAProxy, this abort() is tolerated.
8066 */
8067
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008068 /* Initialise lua. */
8069 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008070#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8071#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8072#ifdef HLUA_PREPEND_PATH
8073 hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
8074#endif
8075#ifdef HLUA_PREPEND_CPATH
8076 hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
8077#endif
8078#undef HLUA_PREPEND_PATH_TOSTRING
8079#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008080
Thierry Fournier75933d42016-01-21 09:30:18 +01008081 /* Set safe environment for the initialisation. */
8082 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008083 if (lua_type(gL.T, -1) == LUA_TSTRING)
8084 error_msg = lua_tostring(gL.T, -1);
8085 else
8086 error_msg = "critical error";
8087 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008088 exit(1);
8089 }
8090
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008091 /*
8092 *
8093 * Create "core" object.
8094 *
8095 */
8096
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008097 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008098 lua_newtable(gL.T);
8099
8100 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008101 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008102 hlua_class_const_int(gL.T, log_levels[i], i);
8103
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008104 /* Register special functions. */
8105 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008106 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008107 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008108 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008109 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008110 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008111 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008112 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008113 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008114 hlua_class_function(gL.T, "sleep", hlua_sleep);
8115 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008116 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8117 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8118 hlua_class_function(gL.T, "set_map", hlua_set_map);
8119 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008120 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008121 hlua_class_function(gL.T, "log", hlua_log);
8122 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8123 hlua_class_function(gL.T, "Info", hlua_log_info);
8124 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8125 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008126 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008127 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008128
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008129 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008130
8131 /*
8132 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008133 * Create "act" object.
8134 *
8135 */
8136
8137 /* This table entry is the object "act" base. */
8138 lua_newtable(gL.T);
8139
8140 /* push action return constants */
8141 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8142 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8143 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8144 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8145 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8146 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8147 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8148 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8149
Christopher Faulet501465d2020-02-26 14:54:16 +01008150 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008151
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008152 lua_setglobal(gL.T, "act");
8153
8154 /*
8155 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008156 * Register class Map
8157 *
8158 */
8159
8160 /* This table entry is the object "Map" base. */
8161 lua_newtable(gL.T);
8162
8163 /* register pattern types. */
8164 for (i=0; i<PAT_MATCH_NUM; i++)
8165 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008166 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008167 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8168 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008169 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008170
8171 /* register constructor. */
8172 hlua_class_function(gL.T, "new", hlua_map_new);
8173
8174 /* Create and fill the metatable. */
8175 lua_newtable(gL.T);
8176
Ilya Shipitsind4259502020-04-08 01:07:56 +05008177 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008178 lua_pushstring(gL.T, "__index");
8179 lua_newtable(gL.T);
8180
8181 /* Register . */
8182 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8183 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8184
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008185 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008186
Thierry Fournier45e78d72016-02-19 18:34:46 +01008187 /* Register previous table in the registry with reference and named entry.
8188 * The function hlua_register_metatable() pops the stack, so we
8189 * previously create a copy of the table.
8190 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008191 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008192 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008193
8194 /* Assign the metatable to the mai Map object. */
8195 lua_setmetatable(gL.T, -2);
8196
8197 /* Set a name to the table. */
8198 lua_setglobal(gL.T, "Map");
8199
8200 /*
8201 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008202 * Register class Channel
8203 *
8204 */
8205
8206 /* Create and fill the metatable. */
8207 lua_newtable(gL.T);
8208
Ilya Shipitsind4259502020-04-08 01:07:56 +05008209 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008210 lua_pushstring(gL.T, "__index");
8211 lua_newtable(gL.T);
8212
8213 /* Register . */
8214 hlua_class_function(gL.T, "get", hlua_channel_get);
8215 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8216 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8217 hlua_class_function(gL.T, "set", hlua_channel_set);
8218 hlua_class_function(gL.T, "append", hlua_channel_append);
8219 hlua_class_function(gL.T, "send", hlua_channel_send);
8220 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8221 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8222 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008223 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008224 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008225
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008226 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008227
8228 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008229 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008230
8231 /*
8232 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008233 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008234 *
8235 */
8236
8237 /* Create and fill the metatable. */
8238 lua_newtable(gL.T);
8239
Ilya Shipitsind4259502020-04-08 01:07:56 +05008240 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008241 lua_pushstring(gL.T, "__index");
8242 lua_newtable(gL.T);
8243
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008244 /* Browse existing fetches and create the associated
8245 * object method.
8246 */
8247 sf = NULL;
8248 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8249
8250 /* Dont register the keywork if the arguments check function are
8251 * not safe during the runtime.
8252 */
8253 if ((sf->val_args != NULL) &&
8254 (sf->val_args != val_payload_lv) &&
8255 (sf->val_args != val_hdr))
8256 continue;
8257
8258 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8259 * by an underscore.
8260 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008261 strncpy(trash.area, sf->kw, trash.size);
8262 trash.area[trash.size - 1] = '\0';
8263 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008264 if (*p == '.' || *p == '-' || *p == '+')
8265 *p = '_';
8266
8267 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008268 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008269 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008270 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008271 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008272 }
8273
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008274 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008275
8276 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008277 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008278
8279 /*
8280 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008281 * Register class Converters
8282 *
8283 */
8284
8285 /* Create and fill the metatable. */
8286 lua_newtable(gL.T);
8287
8288 /* Create and fill the __index entry. */
8289 lua_pushstring(gL.T, "__index");
8290 lua_newtable(gL.T);
8291
8292 /* Browse existing converters and create the associated
8293 * object method.
8294 */
8295 sc = NULL;
8296 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8297 /* Dont register the keywork if the arguments check function are
8298 * not safe during the runtime.
8299 */
8300 if (sc->val_args != NULL)
8301 continue;
8302
8303 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8304 * by an underscore.
8305 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008306 strncpy(trash.area, sc->kw, trash.size);
8307 trash.area[trash.size - 1] = '\0';
8308 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008309 if (*p == '.' || *p == '-' || *p == '+')
8310 *p = '_';
8311
8312 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008313 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008314 lua_pushlightuserdata(gL.T, sc);
8315 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008316 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008317 }
8318
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008319 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008320
8321 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008322 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008323
8324 /*
8325 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008326 * Register class HTTP
8327 *
8328 */
8329
8330 /* Create and fill the metatable. */
8331 lua_newtable(gL.T);
8332
Ilya Shipitsind4259502020-04-08 01:07:56 +05008333 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008334 lua_pushstring(gL.T, "__index");
8335 lua_newtable(gL.T);
8336
8337 /* Register Lua functions. */
8338 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8339 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8340 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8341 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8342 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8343 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8344 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8345 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8346 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8347 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8348
8349 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8350 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8351 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8352 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8353 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8354 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008355 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008356
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008357 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008358
8359 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008360 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008361
8362 /*
8363 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008364 * Register class AppletTCP
8365 *
8366 */
8367
8368 /* Create and fill the metatable. */
8369 lua_newtable(gL.T);
8370
Ilya Shipitsind4259502020-04-08 01:07:56 +05008371 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008372 lua_pushstring(gL.T, "__index");
8373 lua_newtable(gL.T);
8374
8375 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008376 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8377 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8378 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8379 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8380 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008381 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8382 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8383 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008384
8385 lua_settable(gL.T, -3);
8386
8387 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008388 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008389
8390 /*
8391 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008392 * Register class AppletHTTP
8393 *
8394 */
8395
8396 /* Create and fill the metatable. */
8397 lua_newtable(gL.T);
8398
Ilya Shipitsind4259502020-04-08 01:07:56 +05008399 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008400 lua_pushstring(gL.T, "__index");
8401 lua_newtable(gL.T);
8402
8403 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008404 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8405 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008406 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8407 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8408 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008409 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8410 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8411 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8412 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8413 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8414 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8415
8416 lua_settable(gL.T, -3);
8417
8418 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008419 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008420
8421 /*
8422 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008423 * Register class TXN
8424 *
8425 */
8426
8427 /* Create and fill the metatable. */
8428 lua_newtable(gL.T);
8429
Ilya Shipitsind4259502020-04-08 01:07:56 +05008430 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008431 lua_pushstring(gL.T, "__index");
8432 lua_newtable(gL.T);
8433
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008434 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008435 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8436 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8437 hlua_class_function(gL.T, "set_var", hlua_set_var);
8438 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8439 hlua_class_function(gL.T, "get_var", hlua_get_var);
8440 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008441 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008442 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8443 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8444 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8445 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8446 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8447 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8448 hlua_class_function(gL.T, "log", hlua_txn_log);
8449 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8450 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8451 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8452 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008453
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008454 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008455
8456 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008457 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008458
8459 /*
8460 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008461 * Register class reply
8462 *
8463 */
8464 lua_newtable(gL.T);
8465 lua_pushstring(gL.T, "__index");
8466 lua_newtable(gL.T);
8467 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8468 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8469 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8470 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8471 lua_settable(gL.T, -3); /* Sets the __index entry. */
8472 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8473
8474
8475 /*
8476 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008477 * Register class Socket
8478 *
8479 */
8480
8481 /* Create and fill the metatable. */
8482 lua_newtable(gL.T);
8483
Ilya Shipitsind4259502020-04-08 01:07:56 +05008484 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008485 lua_pushstring(gL.T, "__index");
8486 lua_newtable(gL.T);
8487
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008488#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008489 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008490#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008491 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8492 hlua_class_function(gL.T, "send", hlua_socket_send);
8493 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8494 hlua_class_function(gL.T, "close", hlua_socket_close);
8495 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8496 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8497 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8498 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8499
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008500 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008501
8502 /* Register the garbage collector entry. */
8503 lua_pushstring(gL.T, "__gc");
8504 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008505 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008506
8507 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008508 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008509
8510 /* Proxy and server configuration initialisation. */
8511 memset(&socket_proxy, 0, sizeof(socket_proxy));
8512 init_new_proxy(&socket_proxy);
8513 socket_proxy.parent = NULL;
8514 socket_proxy.last_change = now.tv_sec;
8515 socket_proxy.id = "LUA-SOCKET";
8516 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8517 socket_proxy.maxconn = 0;
8518 socket_proxy.accept = NULL;
8519 socket_proxy.options2 |= PR_O2_INDEPSTR;
8520 socket_proxy.srv = NULL;
8521 socket_proxy.conn_retries = 0;
8522 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8523
8524 /* Init TCP server: unchanged parameters */
8525 memset(&socket_tcp, 0, sizeof(socket_tcp));
8526 socket_tcp.next = NULL;
8527 socket_tcp.proxy = &socket_proxy;
8528 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8529 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008530 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008531 socket_tcp.idle_conns = NULL;
8532 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008533 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008534 socket_tcp.last_change = 0;
8535 socket_tcp.id = "LUA-TCP-CONN";
8536 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8537 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8538 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8539
8540 /* XXX: Copy default parameter from default server,
8541 * but the default server is not initialized.
8542 */
8543 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8544 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8545 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8546 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8547 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8548 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8549 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8550 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8551 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8552 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8553
8554 socket_tcp.check.status = HCHK_STATUS_INI;
8555 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8556 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8557 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8558 socket_tcp.check.server = &socket_tcp;
8559
8560 socket_tcp.agent.status = HCHK_STATUS_INI;
8561 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8562 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8563 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8564 socket_tcp.agent.server = &socket_tcp;
8565
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008566 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008567
8568#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008569 /* Init TCP server: unchanged parameters */
8570 memset(&socket_ssl, 0, sizeof(socket_ssl));
8571 socket_ssl.next = NULL;
8572 socket_ssl.proxy = &socket_proxy;
8573 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8574 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008575 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008576 socket_ssl.idle_conns = NULL;
8577 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008578 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008579 socket_ssl.last_change = 0;
8580 socket_ssl.id = "LUA-SSL-CONN";
8581 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8582 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8583 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8584
8585 /* XXX: Copy default parameter from default server,
8586 * but the default server is not initialized.
8587 */
8588 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8589 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8590 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8591 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8592 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8593 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8594 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8595 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8596 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8597 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8598
8599 socket_ssl.check.status = HCHK_STATUS_INI;
8600 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8601 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8602 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8603 socket_ssl.check.server = &socket_ssl;
8604
8605 socket_ssl.agent.status = HCHK_STATUS_INI;
8606 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8607 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8608 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8609 socket_ssl.agent.server = &socket_ssl;
8610
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008611 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008612 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008613
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008614 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008615 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8616 /*
8617 *
8618 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008619 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008620 * features like client certificates and ssl_verify.
8621 *
8622 */
8623 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8624 if (tmp_error != 0) {
8625 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8626 abort(); /* This must be never arrives because the command line
8627 not editable by the user. */
8628 }
8629 idx += kw->skip;
8630 }
8631 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008632#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008633
8634 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008635}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008636
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02008637static void hlua_deinit()
8638{
8639 lua_close(gL.T);
8640}
8641
8642REGISTER_POST_DEINIT(hlua_deinit);
8643
Willy Tarreau80713382018-11-26 10:19:54 +01008644static void hlua_register_build_options(void)
8645{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008646 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008647
Willy Tarreaubb57d942016-12-21 19:04:56 +01008648 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8649 hap_register_build_opts(ptr, 1);
8650}
Willy Tarreau80713382018-11-26 10:19:54 +01008651
8652INITCALL0(STG_REGISTER, hlua_register_build_options);