blob: f99bdf9d2059b4f33b6cdc9af427492e38d5af3a [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua unsafe core engine
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010013#include <ctype.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020014#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010015
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010016#include <lauxlib.h>
17#include <lua.h>
18#include <lualib.h>
19
Thierry FOURNIER463119c2015-03-10 00:35:36 +010020#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
21#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010022#endif
23
Willy Tarreau8d2b7772020-05-27 10:58:19 +020024#include <import/ebpttree.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010025
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
27#include <haproxy/applet.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020028#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020029#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020030#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020031#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020032#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020033#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020034#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020035#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020036#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020037#include <haproxy/http_fetch.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020039#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020040#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020041#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020042#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020043#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020044#include <haproxy/payload.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020045#include <haproxy/proxy-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020047#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020048#include <haproxy/server-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020049#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020050#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020051#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020052#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020053#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020054#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020055#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020056#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020057#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020058#include <haproxy/xref.h>
59
Thierry FOURNIER380d0932015-01-23 14:27:52 +010060
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010061/* Lua uses longjmp to perform yield or throwing errors. This
62 * macro is used only for identifying the function that can
63 * not return because a longjmp is executed.
64 * __LJMP marks a prototype of hlua file that can use longjmp.
65 * WILL_LJMP() marks an lua function that will use longjmp.
66 * MAY_LJMP() marks an lua function that may use longjmp.
67 */
68#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020069#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010070#define MAY_LJMP(func) func
71
Thierry FOURNIERbabae282015-09-17 11:36:37 +020072/* This couple of function executes securely some Lua calls outside of
73 * the lua runtime environment. Each Lua call can return a longjmp
74 * if it encounter a memory error.
75 *
76 * Lua documentation extract:
77 *
78 * If an error happens outside any protected environment, Lua calls
79 * a panic function (see lua_atpanic) and then calls abort, thus
80 * exiting the host application. Your panic function can avoid this
81 * exit by never returning (e.g., doing a long jump to your own
82 * recovery point outside Lua).
83 *
84 * The panic function runs as if it were a message handler (see
85 * §2.3); in particular, the error message is at the top of the
86 * stack. However, there is no guarantee about stack space. To push
87 * anything on the stack, the panic function must first check the
88 * available space (see §4.2).
89 *
90 * We must check all the Lua entry point. This includes:
91 * - The include/proto/hlua.h exported functions
92 * - the task wrapper function
93 * - The action wrapper function
94 * - The converters wrapper function
95 * - The sample-fetch wrapper functions
96 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +050097 * It is tolerated that the initialisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080098 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +020099 *
100 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
101 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
102 * because they must be exists in the program stack when the longjmp
103 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200104 *
105 * Note that the Lua processing is not really thread safe. It provides
106 * heavy system which consists to add our own lock function in the Lua
107 * code and recompile the library. This system will probably not accepted
108 * by maintainers of various distribs.
109 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500110 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200111 * quick looking on the Lua sources displays a lua_lock() a the start
112 * of function and a lua_unlock() at the end of the function. So I
113 * conclude that the Lua thread safe mode just perform a mutex around
114 * all execution. So I prefer to do this in the HAProxy code, it will be
115 * easier for distro maintainers.
116 *
117 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
118 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
119 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200120 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100121__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200122THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100124static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200125
126#define SET_SAFE_LJMP(__L) \
127 ({ \
128 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100129 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200130 if (setjmp(safe_ljmp_env) != 0) { \
131 lua_atpanic(__L, hlua_panic_safe); \
132 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100133 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200134 } else { \
135 lua_atpanic(__L, hlua_panic_ljmp); \
136 ret = 1; \
137 } \
138 ret; \
139 })
140
141/* If we are the last function catching Lua errors, we
142 * must reset the panic function.
143 */
144#define RESET_SAFE_LJMP(__L) \
145 do { \
146 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100147 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200148 } while(0)
149
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200150/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200151#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100152/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200153#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200154/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100155#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100156#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200157
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100158/* The main Lua execution context. */
159struct hlua gL;
160
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100161/* This is the memory pool containing struct lua for applets
162 * (including cli).
163 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100164DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100165
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100166/* Used for Socket connection. */
167static struct proxy socket_proxy;
168static struct server socket_tcp;
169#ifdef USE_OPENSSL
170static struct server socket_ssl;
171#endif
172
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100173/* List head of the function called at the initialisation time. */
174struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
175
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100176/* The following variables contains the reference of the different
177 * Lua classes. These references are useful for identify metadata
178 * associated with an object.
179 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100180static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100181static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100182static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100183static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100184static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100185static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200186static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200187static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200188static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100189static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100190
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100191/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200192 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100193 * short timeout. Lua linked with tasks doesn't have a timeout
194 * because a task may remain alive during all the haproxy execution.
195 */
196static unsigned int hlua_timeout_session = 4000; /* session timeout. */
197static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200198static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100199
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100200/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
201 * it is used for preventing infinite loops.
202 *
203 * I test the scheer with an infinite loop containing one incrementation
204 * and one test. I run this loop between 10 seconds, I raise a ceil of
205 * 710M loops from one interrupt each 9000 instructions, so I fix the value
206 * to one interrupt each 10 000 instructions.
207 *
208 * configured | Number of
209 * instructions | loops executed
210 * between two | in milions
211 * forced yields |
212 * ---------------+---------------
213 * 10 | 160
214 * 500 | 670
215 * 1000 | 680
216 * 5000 | 700
217 * 7000 | 700
218 * 8000 | 700
219 * 9000 | 710 <- ceil
220 * 10000 | 710
221 * 100000 | 710
222 * 1000000 | 710
223 *
224 */
225static unsigned int hlua_nb_instruction = 10000;
226
Willy Tarreau32f61e22015-03-18 17:54:59 +0100227/* Descriptor for the memory allocation state. If limit is not null, it will
228 * be enforced on any memory allocation.
229 */
230struct hlua_mem_allocator {
231 size_t allocated;
232 size_t limit;
233};
234
235static struct hlua_mem_allocator hlua_global_allocator;
236
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100237/* These functions converts types between HAProxy internal args or
238 * sample and LUA types. Another function permits to check if the
239 * LUA stack contains arguments according with an required ARG_T
240 * format.
241 */
242static int hlua_arg2lua(lua_State *L, const struct arg *arg);
243static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100244__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100245 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100246static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100247static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100248static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
249
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100250__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200251
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200252#define SEND_ERR(__be, __fmt, __args...) \
253 do { \
254 send_log(__be, LOG_ERR, __fmt, ## __args); \
255 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100256 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200257 } while (0)
258
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100259/* Used to check an Lua function type in the stack. It creates and
260 * returns a reference of the function. This function throws an
261 * error if the rgument is not a "function".
262 */
263__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
264{
265 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100266 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100267 WILL_LJMP(luaL_argerror(L, argno, msg));
268 }
269 lua_pushvalue(L, argno);
270 return luaL_ref(L, LUA_REGISTRYINDEX);
271}
272
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200273/* Return the string that is of the top of the stack. */
274const char *hlua_get_top_error_string(lua_State *L)
275{
276 if (lua_gettop(L) < 1)
277 return "unknown error";
278 if (lua_type(L, -1) != LUA_TSTRING)
279 return "unknown error";
280 return lua_tostring(L, -1);
281}
282
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200283__LJMP static const char *hlua_traceback(lua_State *L)
284{
285 lua_Debug ar;
286 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200287 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200288 int filled = 0;
289
290 while (lua_getstack(L, level++, &ar)) {
291
292 /* Add separator */
293 if (filled)
294 chunk_appendf(msg, ", ");
295 filled = 1;
296
297 /* Fill fields:
298 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
299 * 'l': fills in the field currentline;
300 * 'n': fills in the field name and namewhat;
301 * 't': fills in the field istailcall;
302 */
303 lua_getinfo(L, "Slnt", &ar);
304
305 /* Append code localisation */
306 if (ar.currentline > 0)
307 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
308 else
309 chunk_appendf(msg, "%s ", ar.short_src);
310
311 /*
312 * Get function name
313 *
314 * if namewhat is no empty, name is defined.
315 * what contains "Lua" for Lua function, "C" for C function,
316 * or "main" for main code.
317 */
318 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
319 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
320
321 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
322 chunk_appendf(msg, "main chunk");
323
324 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
325 chunk_appendf(msg, "C function line %d", ar.linedefined);
326
327 else /* nothing left... */
328 chunk_appendf(msg, "?");
329
330
331 /* Display tailed call */
332 if (ar.istailcall)
333 chunk_appendf(msg, " ...");
334 }
335
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200336 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200337}
338
339
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100340/* This function check the number of arguments available in the
341 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500342 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100343 */
344__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
345{
346 if (lua_gettop(L) == nb)
347 return;
348 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
349}
350
Mark Lakes22154b42018-01-29 14:38:40 -0800351/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100352 * and the line number where the error is encountered.
353 */
354static int hlua_pusherror(lua_State *L, const char *fmt, ...)
355{
356 va_list argp;
357 va_start(argp, fmt);
358 luaL_where(L, 1);
359 lua_pushvfstring(L, fmt, argp);
360 va_end(argp);
361 lua_concat(L, 2);
362 return 1;
363}
364
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100365/* This functions is used with sample fetch and converters. It
366 * converts the HAProxy configuration argument in a lua stack
367 * values.
368 *
369 * It takes an array of "arg", and each entry of the array is
370 * converted and pushed in the LUA stack.
371 */
372static int hlua_arg2lua(lua_State *L, const struct arg *arg)
373{
374 switch (arg->type) {
375 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100376 case ARGT_TIME:
377 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100378 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100379 break;
380
381 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200382 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100383 break;
384
385 case ARGT_IPV4:
386 case ARGT_IPV6:
387 case ARGT_MSK4:
388 case ARGT_MSK6:
389 case ARGT_FE:
390 case ARGT_BE:
391 case ARGT_TAB:
392 case ARGT_SRV:
393 case ARGT_USR:
394 case ARGT_MAP:
395 default:
396 lua_pushnil(L);
397 break;
398 }
399 return 1;
400}
401
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500402/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100403 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500404 * with sample fetch wrappers. The input arguments are given to the
405 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100406 */
407static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
408{
409 switch (lua_type(L, ud)) {
410
411 case LUA_TNUMBER:
412 case LUA_TBOOLEAN:
413 arg->type = ARGT_SINT;
414 arg->data.sint = lua_tointeger(L, ud);
415 break;
416
417 case LUA_TSTRING:
418 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200419 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200420 /* We don't know the actual size of the underlying allocation, so be conservative. */
421 arg->data.str.size = arg->data.str.data;
422 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100423 break;
424
425 case LUA_TUSERDATA:
426 case LUA_TNIL:
427 case LUA_TTABLE:
428 case LUA_TFUNCTION:
429 case LUA_TTHREAD:
430 case LUA_TLIGHTUSERDATA:
431 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200432 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100433 break;
434 }
435 return 1;
436}
437
438/* the following functions are used to convert a struct sample
439 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500440 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100441 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100442static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100443{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200444 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100445 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200447 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100448 break;
449
450 case SMP_T_BIN:
451 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200452 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100453 break;
454
455 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200456 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
458 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
459 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
460 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
461 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
462 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
463 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
464 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
465 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200466 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100467 break;
468 default:
469 lua_pushnil(L);
470 break;
471 }
472 break;
473
474 case SMP_T_IPV4:
475 case SMP_T_IPV6:
476 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200477 if (sample_casts[smp->data.type][SMP_T_STR] &&
478 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100480 else
481 lua_pushnil(L);
482 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100483 default:
484 lua_pushnil(L);
485 break;
486 }
487 return 1;
488}
489
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100490/* the following functions are used to convert a struct sample
491 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500492 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100493 */
494static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
495{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200496 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100497
498 case SMP_T_BIN:
499 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100501 break;
502
503 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200504 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
506 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
507 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
508 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
509 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
510 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
511 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
512 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
513 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100515 break;
516 default:
517 lua_pushstring(L, "");
518 break;
519 }
520 break;
521
522 case SMP_T_SINT:
523 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100524 case SMP_T_IPV4:
525 case SMP_T_IPV6:
526 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200527 if (sample_casts[smp->data.type][SMP_T_STR] &&
528 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200529 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100530 else
531 lua_pushstring(L, "");
532 break;
533 default:
534 lua_pushstring(L, "");
535 break;
536 }
537 return 1;
538}
539
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100540/* the following functions are used to convert an Lua type in a
541 * struct sample. This is useful to provide data from a converter
542 * to the LUA code.
543 */
544static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
545{
546 switch (lua_type(L, ud)) {
547
548 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200549 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200550 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100551 break;
552
553
554 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200555 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100557 break;
558
559 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200560 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200562 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200563 /* We don't know the actual size of the underlying allocation, so be conservative. */
564 smp->data.u.str.size = smp->data.u.str.data;
565 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100566 break;
567
568 case LUA_TUSERDATA:
569 case LUA_TNIL:
570 case LUA_TTABLE:
571 case LUA_TFUNCTION:
572 case LUA_TTHREAD:
573 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200574 case LUA_TNONE:
575 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200576 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200577 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100578 break;
579 }
580 return 1;
581}
582
Ilya Shipitsind4259502020-04-08 01:07:56 +0500583/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800584 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100585 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100586 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500587 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100588 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100589 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100590__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100591 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100592{
593 int min_arg;
594 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100595 struct proxy *px;
596 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100597
598 idx = 0;
599 min_arg = ARGM(mask);
600 mask >>= ARGM_BITS;
601
602 while (1) {
603
604 /* Check oversize. */
605 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100606 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100607 }
608
609 /* Check for mandatory arguments. */
610 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100611 if (idx < min_arg) {
612
613 /* If miss other argument than the first one, we return an error. */
614 if (idx > 0)
615 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
616
617 /* If first argument have a certain type, some default values
618 * may be used. See the function smp_resolve_args().
619 */
620 switch (mask & ARGT_MASK) {
621
622 case ARGT_FE:
623 if (!(p->cap & PR_CAP_FE))
624 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
625 argp[idx].data.prx = p;
626 argp[idx].type = ARGT_FE;
627 argp[idx+1].type = ARGT_STOP;
628 break;
629
630 case ARGT_BE:
631 if (!(p->cap & PR_CAP_BE))
632 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
633 argp[idx].data.prx = p;
634 argp[idx].type = ARGT_BE;
635 argp[idx+1].type = ARGT_STOP;
636 break;
637
638 case ARGT_TAB:
639 argp[idx].data.prx = p;
640 argp[idx].type = ARGT_TAB;
641 argp[idx+1].type = ARGT_STOP;
642 break;
643
644 default:
645 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
646 break;
647 }
648 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100649 return 0;
650 }
651
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500652 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100653 if ((mask & ARGT_MASK) == ARGT_STOP &&
654 argp[idx].type != ARGT_STOP) {
655 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
656 }
657
658 if ((mask & ARGT_MASK) == ARGT_STOP &&
659 argp[idx].type == ARGT_STOP) {
660 return 0;
661 }
662
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100663 /* Convert some argument types. */
664 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100665 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100666 if (argp[idx].type != ARGT_SINT)
667 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
668 argp[idx].type = ARGT_SINT;
669 break;
670
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100671 case ARGT_TIME:
672 if (argp[idx].type != ARGT_SINT)
673 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200674 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100675 break;
676
677 case ARGT_SIZE:
678 if (argp[idx].type != ARGT_SINT)
679 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200680 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100681 break;
682
683 case ARGT_FE:
684 if (argp[idx].type != ARGT_STR)
685 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200686 memcpy(trash.area, argp[idx].data.str.area,
687 argp[idx].data.str.data);
688 trash.area[argp[idx].data.str.data] = 0;
689 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100690 if (!argp[idx].data.prx)
691 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
692 argp[idx].type = ARGT_FE;
693 break;
694
695 case ARGT_BE:
696 if (argp[idx].type != ARGT_STR)
697 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200698 memcpy(trash.area, argp[idx].data.str.area,
699 argp[idx].data.str.data);
700 trash.area[argp[idx].data.str.data] = 0;
701 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100702 if (!argp[idx].data.prx)
703 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
704 argp[idx].type = ARGT_BE;
705 break;
706
707 case ARGT_TAB:
708 if (argp[idx].type != ARGT_STR)
709 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200710 memcpy(trash.area, argp[idx].data.str.area,
711 argp[idx].data.str.data);
712 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhus9fe7c632019-09-29 23:03:09 +0200713 argp[idx].data.t = stktable_find_by_name(trash.area);
714 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100715 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
716 argp[idx].type = ARGT_TAB;
717 break;
718
719 case ARGT_SRV:
720 if (argp[idx].type != ARGT_STR)
721 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200722 memcpy(trash.area, argp[idx].data.str.area,
723 argp[idx].data.str.data);
724 trash.area[argp[idx].data.str.data] = 0;
725 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100726 if (sname) {
727 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200728 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200729 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100730 if (!px)
731 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
732 }
733 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200734 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100735 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100736 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100737 argp[idx].data.srv = findserver(px, sname);
738 if (!argp[idx].data.srv)
739 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
740 argp[idx].type = ARGT_SRV;
741 break;
742
743 case ARGT_IPV4:
Christopher Faulet8e09ac82020-08-07 09:07:26 +0200744 if (argp[idx].type != ARGT_STR)
745 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200746 memcpy(trash.area, argp[idx].data.str.area,
747 argp[idx].data.str.data);
748 trash.area[argp[idx].data.str.data] = 0;
749 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100750 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
751 argp[idx].type = ARGT_IPV4;
752 break;
753
754 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200755 memcpy(trash.area, argp[idx].data.str.area,
756 argp[idx].data.str.data);
757 trash.area[argp[idx].data.str.data] = 0;
758 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100759 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
760 argp[idx].type = ARGT_MSK4;
761 break;
762
763 case ARGT_IPV6:
Christopher Faulet8e09ac82020-08-07 09:07:26 +0200764 if (argp[idx].type != ARGT_STR)
765 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200766 memcpy(trash.area, argp[idx].data.str.area,
767 argp[idx].data.str.data);
768 trash.area[argp[idx].data.str.data] = 0;
769 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100770 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
771 argp[idx].type = ARGT_IPV6;
772 break;
773
774 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200775 memcpy(trash.area, argp[idx].data.str.area,
776 argp[idx].data.str.data);
777 trash.area[argp[idx].data.str.data] = 0;
778 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100779 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
780 argp[idx].type = ARGT_MSK6;
781 break;
782
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100783 case ARGT_MAP:
784 case ARGT_REG:
785 case ARGT_USR:
786 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100787 break;
788 }
789
790 /* Check for type of argument. */
791 if ((mask & ARGT_MASK) != argp[idx].type) {
792 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
793 arg_type_names[(mask & ARGT_MASK)],
794 arg_type_names[argp[idx].type & ARGT_MASK]);
795 WILL_LJMP(luaL_argerror(L, first + idx, msg));
796 }
797
798 /* Next argument. */
799 mask >>= ARGT_BITS;
800 idx++;
801 }
802}
803
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100804/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500805 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100806 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100807 *
808 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100809 * - hlua_sethlua : create the association between hlua context and lua_state.
810 */
811static inline struct hlua *hlua_gethlua(lua_State *L)
812{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100813 struct hlua **hlua = lua_getextraspace(L);
814 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100815}
816static inline void hlua_sethlua(struct hlua *hlua)
817{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100818 struct hlua **hlua_store = lua_getextraspace(hlua->T);
819 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100820}
821
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100822/* This function is used to send logs. It try to send on screen (stderr)
823 * and on the default syslog server.
824 */
825static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
826{
827 struct tm tm;
828 char *p;
829
830 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200831 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100832 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200833 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200834 /* Break the message if exceed the buffer size. */
835 *(p-4) = ' ';
836 *(p-3) = '.';
837 *(p-2) = '.';
838 *(p-1) = '.';
839 break;
840 }
Willy Tarreau90807112020-02-25 08:16:33 +0100841 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100842 *p = *msg;
843 else
844 *p = '.';
845 }
846 *p = '\0';
847
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200848 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100849 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200850 get_localtime(date.tv_sec, &tm);
851 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100852 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200853 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100854 fflush(stderr);
855 }
856}
857
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100858/* This function just ensure that the yield will be always
859 * returned with a timeout and permit to set some flags
860 */
861__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100862 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100863{
864 struct hlua *hlua = hlua_gethlua(L);
865
866 /* Set the wake timeout. If timeout is required, we set
867 * the expiration time.
868 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200869 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100870
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100871 hlua->flags |= flags;
872
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100873 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200874 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100875}
876
Willy Tarreau87b09662015-04-03 00:22:06 +0200877/* This function initialises the Lua environment stored in the stream.
878 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100879 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200880 *
881 * This function is particular. it initialises a new Lua thread. If the
882 * initialisation fails (example: out of memory error), the lua function
883 * throws an error (longjmp).
884 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100885 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500886 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100887 * threads appear, the safe environment set a lock to ensure only one
888 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500889 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100890 *
891 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500892 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100893 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800894 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200895 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800896 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500897 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100898 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100899int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100900{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100901 if (!already_safe) {
902 if (!SET_SAFE_LJMP(gL.T)) {
903 lua->Tref = LUA_REFNIL;
904 return 0;
905 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200906 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100908 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +0100909 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +0100910 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100911 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100912 lua->T = lua_newthread(gL.T);
913 if (!lua->T) {
914 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100915 if (!already_safe)
916 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100917 return 0;
918 }
919 hlua_sethlua(lua);
920 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
921 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100922 if (!already_safe)
923 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100924 return 1;
925}
926
Willy Tarreau87b09662015-04-03 00:22:06 +0200927/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100928 * is destroyed. The destroy also the memory context. The struct "lua"
929 * is not freed.
930 */
931void hlua_ctx_destroy(struct hlua *lua)
932{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100933 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100934 return;
935
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100936 if (!lua->T)
937 goto end;
938
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100939 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200940 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100941
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200942 if (!SET_SAFE_LJMP(lua->T))
943 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100944 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200945 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200946
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200947 if (!SET_SAFE_LJMP(gL.T))
948 return;
949 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
950 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200951 /* Forces a garbage collecting process. If the Lua program is finished
952 * without error, we run the GC on the thread pointer. Its freed all
953 * the unused memory.
954 * If the thread is finnish with an error or is currently yielded,
955 * it seems that the GC applied on the thread doesn't clean anything,
956 * so e run the GC on the main thread.
957 * NOTE: maybe this action locks all the Lua threads untiml the en of
958 * the garbage collection.
959 */
Willy Tarreauf31af932020-01-14 09:59:38 +0100960 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200961 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200962 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200963 lua_gc(gL.T, LUA_GCCOLLECT, 0);
964 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200965 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200966
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200967 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100968
969end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100970 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100971}
972
973/* This function is used to restore the Lua context when a coroutine
974 * fails. This function copy the common memory between old coroutine
975 * and the new coroutine. The old coroutine is destroyed, and its
976 * replaced by the new coroutine.
977 * If the flag "keep_msg" is set, the last entry of the old is assumed
978 * as string error message and it is copied in the new stack.
979 */
980static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
981{
982 lua_State *T;
983 int new_ref;
984
985 /* Renew the main LUA stack doesn't have sense. */
986 if (lua == &gL)
987 return 0;
988
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100989 /* New Lua coroutine. */
990 T = lua_newthread(gL.T);
991 if (!T)
992 return 0;
993
994 /* Copy last error message. */
995 if (keep_msg)
996 lua_xmove(lua->T, T, 1);
997
998 /* Copy data between the coroutines. */
999 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1000 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001001 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001002
1003 /* Destroy old data. */
1004 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1005
1006 /* The thread is garbage collected by Lua. */
1007 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1008
1009 /* Fill the struct with the new coroutine values. */
1010 lua->Mref = new_ref;
1011 lua->T = T;
1012 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1013
1014 /* Set context. */
1015 hlua_sethlua(lua);
1016
1017 return 1;
1018}
1019
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001020void hlua_hook(lua_State *L, lua_Debug *ar)
1021{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001022 struct hlua *hlua = hlua_gethlua(L);
1023
1024 /* Lua cannot yield when its returning from a function,
1025 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001026 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001027 */
1028 if (lua_gethookmask(L) & LUA_MASKRET) {
1029 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1030 return;
1031 }
1032
1033 /* restore the interrupt condition. */
1034 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1035
1036 /* If we interrupt the Lua processing in yieldable state, we yield.
1037 * If the state is not yieldable, trying yield causes an error.
1038 */
1039 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001040 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001041
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001042 /* If we cannot yield, update the clock and check the timeout. */
1043 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001044 hlua->run_time += now_ms - hlua->start_time;
1045 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001046 lua_pushfstring(L, "execution timeout");
1047 WILL_LJMP(lua_error(L));
1048 }
1049
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001050 /* Update the start time. */
1051 hlua->start_time = now_ms;
1052
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001053 /* Try to interrupt the process at the end of the current
1054 * unyieldable function.
1055 */
1056 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001057}
1058
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001059/* This function start or resumes the Lua stack execution. If the flag
1060 * "yield_allowed" if no set and the LUA stack execution returns a yield
1061 * The function return an error.
1062 *
1063 * The function can returns 4 values:
1064 * - HLUA_E_OK : The execution is terminated without any errors.
1065 * - HLUA_E_AGAIN : The execution must continue at the next associated
1066 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001067 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001068 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001069 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001070 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001071 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001072 * LUA code.
1073 */
1074static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1075{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001076#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1077 int nres;
1078#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001079 int ret;
1080 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001081 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001082
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001083 /* Initialise run time counter. */
1084 if (!HLUA_IS_RUNNING(lua))
1085 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001086
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001087 /* Lock the whole Lua execution. This lock must be before the
1088 * label "resume_execution".
1089 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001090 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001091
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001092resume_execution:
1093
1094 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1095 * instructions. it is used for preventing infinite loops.
1096 */
1097 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1098
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001099 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001100 HLUA_SET_RUN(lua);
1101 HLUA_CLR_CTRLYIELD(lua);
1102 HLUA_CLR_WAKERESWR(lua);
1103 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001104
Christopher Fauletbc275a92020-02-26 14:55:16 +01001105 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001106 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001107 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001108
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001109 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001110#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1111 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1112#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001113 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001114#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001115 switch (ret) {
1116
1117 case LUA_OK:
1118 ret = HLUA_E_OK;
1119 break;
1120
1121 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001122 /* Check if the execution timeout is expired. It it is the case, we
1123 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001124 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001125 tv_update_date(0, 1);
1126 lua->run_time += now_ms - lua->start_time;
1127 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001128 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001129 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001130 break;
1131 }
1132 /* Process the forced yield. if the general yield is not allowed or
1133 * if no task were associated this the current Lua execution
1134 * coroutine, we resume the execution. Else we want to return in the
1135 * scheduler and we want to be waked up again, to continue the
1136 * current Lua execution. So we schedule our own task.
1137 */
1138 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001139 if (!yield_allowed || !lua->task)
1140 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001141 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001142 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001143 if (!yield_allowed) {
1144 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001145 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001146 break;
1147 }
1148 ret = HLUA_E_AGAIN;
1149 break;
1150
1151 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001152
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001153 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001154 * because the errors ares the only one mean to return immediately
1155 * from and lua execution.
1156 */
1157 if (lua->flags & HLUA_EXIT) {
1158 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001159 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001160 break;
1161 }
1162
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001163 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001164 if (!lua_checkstack(lua->T, 1)) {
1165 ret = HLUA_E_ERR;
1166 break;
1167 }
1168 msg = lua_tostring(lua->T, -1);
1169 lua_settop(lua->T, 0); /* Empty the stack. */
1170 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001171 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001172 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001173 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001174 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001175 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001176 ret = HLUA_E_ERRMSG;
1177 break;
1178
1179 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001180 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001181 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001182 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001183 break;
1184
1185 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001186 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001187 if (!lua_checkstack(lua->T, 1)) {
1188 ret = HLUA_E_ERR;
1189 break;
1190 }
1191 msg = lua_tostring(lua->T, -1);
1192 lua_settop(lua->T, 0); /* Empty the stack. */
1193 lua_pop(lua->T, 1);
1194 if (msg)
1195 lua_pushfstring(lua->T, "message handler error: %s", msg);
1196 else
1197 lua_pushfstring(lua->T, "message handler error");
1198 ret = HLUA_E_ERRMSG;
1199 break;
1200
1201 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001202 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001203 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001204 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001205 break;
1206 }
1207
1208 switch (ret) {
1209 case HLUA_E_AGAIN:
1210 break;
1211
1212 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001213 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001214 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001215 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001216 break;
1217
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001218 case HLUA_E_ETMOUT:
1219 case HLUA_E_NOMEM:
1220 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001221 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001222 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001223 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001224 hlua_ctx_renew(lua, 0);
1225 break;
1226
1227 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001228 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001229 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001230 break;
1231 }
1232
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001233 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001234 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001235
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001236 return ret;
1237}
1238
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001239/* This function exit the current code. */
1240__LJMP static int hlua_done(lua_State *L)
1241{
1242 struct hlua *hlua = hlua_gethlua(L);
1243
1244 hlua->flags |= HLUA_EXIT;
1245 WILL_LJMP(lua_error(L));
1246
1247 return 0;
1248}
1249
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001250/* This function is an LUA binding. It provides a function
1251 * for deleting ACL from a referenced ACL file.
1252 */
1253__LJMP static int hlua_del_acl(lua_State *L)
1254{
1255 const char *name;
1256 const char *key;
1257 struct pat_ref *ref;
1258
1259 MAY_LJMP(check_args(L, 2, "del_acl"));
1260
1261 name = MAY_LJMP(luaL_checkstring(L, 1));
1262 key = MAY_LJMP(luaL_checkstring(L, 2));
1263
1264 ref = pat_ref_lookup(name);
1265 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001266 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001267
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001268 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001269 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001270 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001271 return 0;
1272}
1273
1274/* This function is an LUA binding. It provides a function
1275 * for deleting map entry from a referenced map file.
1276 */
1277static int hlua_del_map(lua_State *L)
1278{
1279 const char *name;
1280 const char *key;
1281 struct pat_ref *ref;
1282
1283 MAY_LJMP(check_args(L, 2, "del_map"));
1284
1285 name = MAY_LJMP(luaL_checkstring(L, 1));
1286 key = MAY_LJMP(luaL_checkstring(L, 2));
1287
1288 ref = pat_ref_lookup(name);
1289 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001290 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001291
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001292 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001293 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001294 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001295 return 0;
1296}
1297
1298/* This function is an LUA binding. It provides a function
1299 * for adding ACL pattern from a referenced ACL file.
1300 */
1301static int hlua_add_acl(lua_State *L)
1302{
1303 const char *name;
1304 const char *key;
1305 struct pat_ref *ref;
1306
1307 MAY_LJMP(check_args(L, 2, "add_acl"));
1308
1309 name = MAY_LJMP(luaL_checkstring(L, 1));
1310 key = MAY_LJMP(luaL_checkstring(L, 2));
1311
1312 ref = pat_ref_lookup(name);
1313 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001314 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001315
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001316 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001317 if (pat_ref_find_elt(ref, key) == NULL)
1318 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001319 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001320 return 0;
1321}
1322
1323/* This function is an LUA binding. It provides a function
1324 * for setting map pattern and sample from a referenced map
1325 * file.
1326 */
1327static int hlua_set_map(lua_State *L)
1328{
1329 const char *name;
1330 const char *key;
1331 const char *value;
1332 struct pat_ref *ref;
1333
1334 MAY_LJMP(check_args(L, 3, "set_map"));
1335
1336 name = MAY_LJMP(luaL_checkstring(L, 1));
1337 key = MAY_LJMP(luaL_checkstring(L, 2));
1338 value = MAY_LJMP(luaL_checkstring(L, 3));
1339
1340 ref = pat_ref_lookup(name);
1341 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001342 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001343
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001344 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001345 if (pat_ref_find_elt(ref, key) != NULL)
1346 pat_ref_set(ref, key, value, NULL);
1347 else
1348 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001349 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001350 return 0;
1351}
1352
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001353/* A class is a lot of memory that contain data. This data can be a table,
1354 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001355 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001356 * the name of the object (_G[<name>] = <metable> ).
1357 *
1358 * A metable is a table that modify the standard behavior of a standard
1359 * access to the associated data. The entries of this new metatable are
1360 * defined as is:
1361 *
1362 * http://lua-users.org/wiki/MetatableEvents
1363 *
1364 * __index
1365 *
1366 * we access an absent field in a table, the result is nil. This is
1367 * true, but it is not the whole truth. Actually, such access triggers
1368 * the interpreter to look for an __index metamethod: If there is no
1369 * such method, as usually happens, then the access results in nil;
1370 * otherwise, the metamethod will provide the result.
1371 *
1372 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1373 * the key does not appear in the table, but the metatable has an __index
1374 * property:
1375 *
1376 * - if the value is a function, the function is called, passing in the
1377 * table and the key; the return value of that function is returned as
1378 * the result.
1379 *
1380 * - if the value is another table, the value of the key in that table is
1381 * asked for and returned (and if it doesn't exist in that table, but that
1382 * table's metatable has an __index property, then it continues on up)
1383 *
1384 * - Use "rawget(myTable,key)" to skip this metamethod.
1385 *
1386 * http://www.lua.org/pil/13.4.1.html
1387 *
1388 * __newindex
1389 *
1390 * Like __index, but control property assignment.
1391 *
1392 * __mode - Control weak references. A string value with one or both
1393 * of the characters 'k' and 'v' which specifies that the the
1394 * keys and/or values in the table are weak references.
1395 *
1396 * __call - Treat a table like a function. When a table is followed by
1397 * parenthesis such as "myTable( 'foo' )" and the metatable has
1398 * a __call key pointing to a function, that function is invoked
1399 * (passing any specified arguments) and the return value is
1400 * returned.
1401 *
1402 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1403 * called, if the metatable for myTable has a __metatable
1404 * key, the value of that key is returned instead of the
1405 * actual metatable.
1406 *
1407 * __tostring - Control string representation. When the builtin
1408 * "tostring( myTable )" function is called, if the metatable
1409 * for myTable has a __tostring property set to a function,
1410 * that function is invoked (passing myTable to it) and the
1411 * return value is used as the string representation.
1412 *
1413 * __len - Control table length. When the table length is requested using
1414 * the length operator ( '#' ), if the metatable for myTable has
1415 * a __len key pointing to a function, that function is invoked
1416 * (passing myTable to it) and the return value used as the value
1417 * of "#myTable".
1418 *
1419 * __gc - Userdata finalizer code. When userdata is set to be garbage
1420 * collected, if the metatable has a __gc field pointing to a
1421 * function, that function is first invoked, passing the userdata
1422 * to it. The __gc metamethod is not called for tables.
1423 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1424 *
1425 * Special metamethods for redefining standard operators:
1426 * http://www.lua.org/pil/13.1.html
1427 *
1428 * __add "+"
1429 * __sub "-"
1430 * __mul "*"
1431 * __div "/"
1432 * __unm "!"
1433 * __pow "^"
1434 * __concat ".."
1435 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001436 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001437 * http://www.lua.org/pil/13.2.html
1438 *
1439 * __eq "=="
1440 * __lt "<"
1441 * __le "<="
1442 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001443
1444/*
1445 *
1446 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001447 * Class Map
1448 *
1449 *
1450 */
1451
1452/* Returns a struct hlua_map if the stack entry "ud" is
1453 * a class session, otherwise it throws an error.
1454 */
1455__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1456{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001457 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001458}
1459
1460/* This function is the map constructor. It don't need
1461 * the class Map object. It creates and return a new Map
1462 * object. It must be called only during "body" or "init"
1463 * context because it process some filesystem accesses.
1464 */
1465__LJMP static int hlua_map_new(struct lua_State *L)
1466{
1467 const char *fn;
1468 int match = PAT_MATCH_STR;
1469 struct sample_conv conv;
1470 const char *file = "";
1471 int line = 0;
1472 lua_Debug ar;
1473 char *err = NULL;
1474 struct arg args[2];
1475
1476 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1477 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1478
1479 fn = MAY_LJMP(luaL_checkstring(L, 1));
1480
1481 if (lua_gettop(L) >= 2) {
1482 match = MAY_LJMP(luaL_checkinteger(L, 2));
1483 if (match < 0 || match >= PAT_MATCH_NUM)
1484 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1485 }
1486
1487 /* Get Lua filename and line number. */
1488 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1489 lua_getinfo(L, "Sl", &ar); /* get info about it */
1490 if (ar.currentline > 0) { /* is there info? */
1491 file = ar.short_src;
1492 line = ar.currentline;
1493 }
1494 }
1495
1496 /* fill fake sample_conv struct. */
1497 conv.kw = ""; /* unused. */
1498 conv.process = NULL; /* unused. */
1499 conv.arg_mask = 0; /* unused. */
1500 conv.val_args = NULL; /* unused. */
1501 conv.out_type = SMP_T_STR;
1502 conv.private = (void *)(long)match;
1503 switch (match) {
1504 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1505 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1506 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1507 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1508 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1509 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1510 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001511 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001512 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1513 default:
1514 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1515 }
1516
1517 /* fill fake args. */
1518 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001519 args[0].data.str.area = strdup(fn);
1520 args[0].data.str.data = strlen(fn);
1521 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001522 args[1].type = ARGT_STOP;
1523
1524 /* load the map. */
1525 if (!sample_load_map(args, &conv, file, line, &err)) {
1526 /* error case: we cant use luaL_error because we must
1527 * free the err variable.
1528 */
1529 luaL_where(L, 1);
1530 lua_pushfstring(L, "'new': %s.", err);
1531 lua_concat(L, 2);
1532 free(err);
Christopher Faulet73292e92020-08-06 08:40:09 +02001533 free(args[0].data.str.area);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001534 WILL_LJMP(lua_error(L));
1535 }
1536
1537 /* create the lua object. */
1538 lua_newtable(L);
1539 lua_pushlightuserdata(L, args[0].data.map);
1540 lua_rawseti(L, -2, 0);
1541
1542 /* Pop a class Map metatable and affect it to the userdata. */
1543 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1544 lua_setmetatable(L, -2);
1545
1546
1547 return 1;
1548}
1549
1550__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1551{
1552 struct map_descriptor *desc;
1553 struct pattern *pat;
1554 struct sample smp;
1555
1556 MAY_LJMP(check_args(L, 2, "lookup"));
1557 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001558 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001559 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001560 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001561 }
1562 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001563 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001564 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001565 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 +02001566 }
1567
1568 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001569 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001570 if (str)
1571 lua_pushstring(L, "");
1572 else
1573 lua_pushnil(L);
1574 return 1;
1575 }
1576
1577 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001578 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001579 return 1;
1580}
1581
1582__LJMP static int hlua_map_lookup(struct lua_State *L)
1583{
1584 return _hlua_map_lookup(L, 0);
1585}
1586
1587__LJMP static int hlua_map_slookup(struct lua_State *L)
1588{
1589 return _hlua_map_lookup(L, 1);
1590}
1591
1592/*
1593 *
1594 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001595 * Class Socket
1596 *
1597 *
1598 */
1599
1600__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1601{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001602 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001603}
1604
1605/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001606 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001607 * received.
1608 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001609static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001610{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001611 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001612
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001613 if (appctx->ctx.hlua_cosocket.die) {
1614 si_shutw(si);
1615 si_shutr(si);
1616 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001617 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1618 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001619 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1620 }
1621
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001622 /* If we cant write, wakeup the pending write signals. */
1623 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001624 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001625
1626 /* If we cant read, wakeup the pending read signals. */
1627 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001628 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001629
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001630 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001631 * to be notified whenever the connection completes.
1632 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001633 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001634 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001635 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001636 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001637 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001638 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001639
1640 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001641 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001642
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001643 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001644 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001645 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001646
1647 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001648 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001649 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001650
1651 /* Some data were injected in the buffer, notify the stream
1652 * interface.
1653 */
1654 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001655 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001656
1657 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001658 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001659 */
1660 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001661 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001662}
1663
Willy Tarreau87b09662015-04-03 00:22:06 +02001664/* This function is called when the "struct stream" is destroyed.
1665 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001666 * Wake all the pending signals.
1667 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001668static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001669{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001670 struct xref *peer;
1671
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001672 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001673 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1674 if (peer)
1675 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001676
1677 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001678 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1679 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001680}
1681
1682/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001683 * uses this object. If the stream does not exists, just quit.
1684 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685 * pending signal can rest in the read and write lists. destroy
1686 * it.
1687 */
1688__LJMP static int hlua_socket_gc(lua_State *L)
1689{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001690 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001691 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001692 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001693
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001694 MAY_LJMP(check_args(L, 1, "__gc"));
1695
1696 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001697 peer = xref_get_peer_and_lock(&socket->xref);
1698 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001700 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001702 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001703 appctx->ctx.hlua_cosocket.die = 1;
1704 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001706 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001707 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001708 return 0;
1709}
1710
1711/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001712 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001713 */
sada05ed3302018-05-11 11:48:18 -07001714__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001715{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001716 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001717 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001718 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001719 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001720
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001721 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001722
1723 /* Check if we run on the same thread than the xreator thread.
1724 * We cannot access to the socket if the thread is different.
1725 */
1726 if (socket->tid != tid)
1727 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1728
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001729 peer = xref_get_peer_and_lock(&socket->xref);
1730 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001731 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001732
1733 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001734 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001735
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001736 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001737 appctx->ctx.hlua_cosocket.die = 1;
1738 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001739
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001740 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001741 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001742 return 0;
1743}
1744
sada05ed3302018-05-11 11:48:18 -07001745/* The close function calls close_helper.
1746 */
1747__LJMP static int hlua_socket_close(lua_State *L)
1748{
1749 MAY_LJMP(check_args(L, 1, "close"));
1750 return hlua_socket_close_helper(L);
1751}
1752
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001753/* This Lua function assumes that the stack contain three parameters.
1754 * 1 - USERDATA containing a struct socket
1755 * 2 - INTEGER with values of the macro defined below
1756 * If the integer is -1, we must read at most one line.
1757 * If the integer is -2, we ust read all the data until the
1758 * end of the stream.
1759 * If the integer is positive value, we must read a number of
1760 * bytes corresponding to this value.
1761 */
1762#define HLSR_READ_LINE (-1)
1763#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001764__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001765{
1766 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1767 int wanted = lua_tointeger(L, 2);
1768 struct hlua *hlua = hlua_gethlua(L);
1769 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001770 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001771 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001772 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001773 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001774 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001775 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001776 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001777 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001778 struct stream_interface *si;
1779 struct stream *s;
1780 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001781 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001782
1783 /* Check if this lua stack is schedulable. */
1784 if (!hlua || !hlua->task)
1785 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1786 "'frontend', 'backend' or 'task'"));
1787
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001788 /* Check if we run on the same thread than the xreator thread.
1789 * We cannot access to the socket if the thread is different.
1790 */
1791 if (socket->tid != tid)
1792 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1793
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001794 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001795 peer = xref_get_peer_and_lock(&socket->xref);
1796 if (!peer)
1797 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001798 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1799 si = appctx->owner;
1800 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001801
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001802 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001803 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001804 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001805 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001806 if (nblk < 0) /* Connection close. */
1807 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001808 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001809 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001810
1811 /* remove final \r\n. */
1812 if (nblk == 1) {
1813 if (blk1[len1-1] == '\n') {
1814 len1--;
1815 skip_at_end++;
1816 if (blk1[len1-1] == '\r') {
1817 len1--;
1818 skip_at_end++;
1819 }
1820 }
1821 }
1822 else {
1823 if (blk2[len2-1] == '\n') {
1824 len2--;
1825 skip_at_end++;
1826 if (blk2[len2-1] == '\r') {
1827 len2--;
1828 skip_at_end++;
1829 }
1830 }
1831 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832 }
1833
1834 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001835 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001836 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001837 if (nblk < 0) /* Connection close. */
1838 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001839 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001840 goto connection_empty;
1841 }
1842
1843 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001844 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001845 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001846 if (nblk < 0) /* Connection close. */
1847 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001848 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001849 goto connection_empty;
1850
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001851 missing_bytes = wanted - socket->b.n;
1852 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001853 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001854 len1 = missing_bytes;
1855 } if (nblk == 2 && len1 + len2 > missing_bytes)
1856 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001857 }
1858
1859 len = len1;
1860
1861 luaL_addlstring(&socket->b, blk1, len1);
1862 if (nblk == 2) {
1863 len += len2;
1864 luaL_addlstring(&socket->b, blk2, len2);
1865 }
1866
1867 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001868 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001869
1870 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001871 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001872
1873 /* If the pattern reclaim to read all the data
1874 * in the connection, got out.
1875 */
1876 if (wanted == HLSR_READ_ALL)
1877 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001878 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001879 goto connection_empty;
1880
1881 /* Return result. */
1882 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001883 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001884 return 1;
1885
1886connection_closed:
1887
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001888 xref_unlock(&socket->xref, peer);
1889
1890no_peer:
1891
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001892 /* If the buffer containds data. */
1893 if (socket->b.n > 0) {
1894 luaL_pushresult(&socket->b);
1895 return 1;
1896 }
1897 lua_pushnil(L);
1898 lua_pushstring(L, "connection closed.");
1899 return 2;
1900
1901connection_empty:
1902
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001903 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1904 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001905 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001906 }
1907 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001908 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909 return 0;
1910}
1911
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001912/* This Lua function gets two parameters. The first one can be string
1913 * or a number. If the string is "*l", the user requires one line. If
1914 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001915 * If the value is a number, the user require a number of bytes equal
1916 * to the value. The default value is "*l" (a line).
1917 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001918 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001919 * integer takes this values:
1920 * -1 : read a line
1921 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001922 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001923 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001924 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001925 * concatenated with the read data.
1926 */
1927__LJMP static int hlua_socket_receive(struct lua_State *L)
1928{
1929 int wanted = HLSR_READ_LINE;
1930 const char *pattern;
1931 int type;
1932 char *error;
1933 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001934 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001935
1936 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1937 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1938
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001939 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001940
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001941 /* Check if we run on the same thread than the xreator thread.
1942 * We cannot access to the socket if the thread is different.
1943 */
1944 if (socket->tid != tid)
1945 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1946
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001947 /* check for pattern. */
1948 if (lua_gettop(L) >= 2) {
1949 type = lua_type(L, 2);
1950 if (type == LUA_TSTRING) {
1951 pattern = lua_tostring(L, 2);
1952 if (strcmp(pattern, "*a") == 0)
1953 wanted = HLSR_READ_ALL;
1954 else if (strcmp(pattern, "*l") == 0)
1955 wanted = HLSR_READ_LINE;
1956 else {
1957 wanted = strtoll(pattern, &error, 10);
1958 if (*error != '\0')
1959 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1960 }
1961 }
1962 else if (type == LUA_TNUMBER) {
1963 wanted = lua_tointeger(L, 2);
1964 if (wanted < 0)
1965 WILL_LJMP(luaL_error(L, "Unsupported size."));
1966 }
1967 }
1968
1969 /* Set pattern. */
1970 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001971
1972 /* Check if we would replace the top by itself. */
1973 if (lua_gettop(L) != 2)
1974 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001975
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001976 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001977 luaL_buffinit(L, &socket->b);
1978
1979 /* Check prefix. */
1980 if (lua_gettop(L) >= 3) {
1981 if (lua_type(L, 3) != LUA_TSTRING)
1982 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1983 pattern = lua_tolstring(L, 3, &len);
1984 luaL_addlstring(&socket->b, pattern, len);
1985 }
1986
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001987 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001988}
1989
1990/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001991 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001992 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001993static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001994{
1995 struct hlua_socket *socket;
1996 struct hlua *hlua = hlua_gethlua(L);
1997 struct appctx *appctx;
1998 size_t buf_len;
1999 const char *buf;
2000 int len;
2001 int send_len;
2002 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002003 struct xref *peer;
2004 struct stream_interface *si;
2005 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002006
2007 /* Check if this lua stack is schedulable. */
2008 if (!hlua || !hlua->task)
2009 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2010 "'frontend', 'backend' or 'task'"));
2011
2012 /* Get object */
2013 socket = MAY_LJMP(hlua_checksocket(L, 1));
2014 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002015 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002016
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002017 /* Check if we run on the same thread than the xreator thread.
2018 * We cannot access to the socket if the thread is different.
2019 */
2020 if (socket->tid != tid)
2021 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2022
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002023 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002024 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002025 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002026 lua_pushinteger(L, -1);
2027 return 1;
2028 }
2029 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2030 si = appctx->owner;
2031 s = si_strm(si);
2032
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002033 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002034 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002035 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002036 lua_pushinteger(L, -1);
2037 return 1;
2038 }
2039
2040 /* Update the input buffer data. */
2041 buf += sent;
2042 send_len = buf_len - sent;
2043
2044 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002045 if (sent >= buf_len) {
2046 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002047 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002048 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002049
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002050 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002051 * the request buffer if its not required.
2052 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002053 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002054 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002055 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002056 }
2057
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002058 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002059 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002060 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002061 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002062 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002063
2064 /* send data */
2065 if (len < send_len)
2066 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002067 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002068
2069 /* "Not enough space" (-1), "Buffer too little to contain
2070 * the data" (-2) are not expected because the available length
2071 * is tested.
2072 * Other unknown error are also not expected.
2073 */
2074 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002075 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002076 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002077
sada05ed3302018-05-11 11:48:18 -07002078 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002079 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002080 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002081 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002082 return 1;
2083 }
2084
2085 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002086 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002087
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002088 s->req.rex = TICK_ETERNITY;
2089 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002090
2091 /* Update length sent. */
2092 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002093 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002094
2095 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002096 if (sent + len >= buf_len) {
2097 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002098 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002099 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002100
2101hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002102 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2103 xref_unlock(&socket->xref, peer);
2104 WILL_LJMP(luaL_error(L, "out of memory"));
2105 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002106 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002107 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002108 return 0;
2109}
2110
2111/* This function initiate the send of data. It just check the input
2112 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002113 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002114 * "hlua_socket_write_yield" that can yield.
2115 *
2116 * The Lua function gets between 3 and 4 parameters. The first one is
2117 * the associated object. The second is a string buffer. The third is
2118 * a facultative integer that represents where is the buffer position
2119 * of the start of the data that can send. The first byte is the
2120 * position "1". The default value is "1". The fourth argument is a
2121 * facultative integer that represents where is the buffer position
2122 * of the end of the data that can send. The default is the last byte.
2123 */
2124static int hlua_socket_send(struct lua_State *L)
2125{
2126 int i;
2127 int j;
2128 const char *buf;
2129 size_t buf_len;
2130
2131 /* Check number of arguments. */
2132 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2133 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2134
2135 /* Get the string. */
2136 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2137
2138 /* Get and check j. */
2139 if (lua_gettop(L) == 4) {
2140 j = MAY_LJMP(luaL_checkinteger(L, 4));
2141 if (j < 0)
2142 j = buf_len + j + 1;
2143 if (j > buf_len)
2144 j = buf_len + 1;
2145 lua_pop(L, 1);
2146 }
2147 else
2148 j = buf_len;
2149
2150 /* Get and check i. */
2151 if (lua_gettop(L) == 3) {
2152 i = MAY_LJMP(luaL_checkinteger(L, 3));
2153 if (i < 0)
2154 i = buf_len + i + 1;
2155 if (i > buf_len)
2156 i = buf_len + 1;
2157 lua_pop(L, 1);
2158 } else
2159 i = 1;
2160
2161 /* Check bth i and j. */
2162 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002163 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002164 return 1;
2165 }
2166 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002167 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002168 return 1;
2169 }
2170 if (i == 0)
2171 i = 1;
2172 if (j == 0)
2173 j = 1;
2174
2175 /* Pop the string. */
2176 lua_pop(L, 1);
2177
2178 /* Update the buffer length. */
2179 buf += i - 1;
2180 buf_len = j - i + 1;
2181 lua_pushlstring(L, buf, buf_len);
2182
2183 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002184 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002185
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002186 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002187}
2188
Willy Tarreau22b0a682015-06-17 19:43:49 +02002189#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002190__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2191{
2192 static char buffer[SOCKET_INFO_MAX_LEN];
2193 int ret;
2194 int len;
2195 char *p;
2196
2197 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2198 if (ret <= 0) {
2199 lua_pushnil(L);
2200 return 1;
2201 }
2202
2203 if (ret == AF_UNIX) {
2204 lua_pushstring(L, buffer+1);
2205 return 1;
2206 }
2207 else if (ret == AF_INET6) {
2208 buffer[0] = '[';
2209 len = strlen(buffer);
2210 buffer[len] = ']';
2211 len++;
2212 buffer[len] = ':';
2213 len++;
2214 p = buffer;
2215 }
2216 else if (ret == AF_INET) {
2217 p = buffer + 1;
2218 len = strlen(p);
2219 p[len] = ':';
2220 len++;
2221 }
2222 else {
2223 lua_pushnil(L);
2224 return 1;
2225 }
2226
2227 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2228 lua_pushnil(L);
2229 return 1;
2230 }
2231
2232 lua_pushstring(L, p);
2233 return 1;
2234}
2235
2236/* Returns information about the peer of the connection. */
2237__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2238{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002239 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002240 struct xref *peer;
2241 struct appctx *appctx;
2242 struct stream_interface *si;
2243 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002244 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002245
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002246 MAY_LJMP(check_args(L, 1, "getpeername"));
2247
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002248 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002249
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002250 /* Check if we run on the same thread than the xreator thread.
2251 * We cannot access to the socket if the thread is different.
2252 */
2253 if (socket->tid != tid)
2254 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2255
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002256 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002257 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002258 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002259 lua_pushnil(L);
2260 return 1;
2261 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002262 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2263 si = appctx->owner;
2264 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002265
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002266 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002267 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002268 lua_pushnil(L);
2269 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002270 }
2271
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002272 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002273 xref_unlock(&socket->xref, peer);
2274 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002275}
2276
2277/* Returns information about my connection side. */
2278static int hlua_socket_getsockname(struct lua_State *L)
2279{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002280 struct hlua_socket *socket;
2281 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002282 struct appctx *appctx;
2283 struct xref *peer;
2284 struct stream_interface *si;
2285 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002286 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002287
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002288 MAY_LJMP(check_args(L, 1, "getsockname"));
2289
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002290 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002291
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002292 /* Check if we run on the same thread than the xreator thread.
2293 * We cannot access to the socket if the thread is different.
2294 */
2295 if (socket->tid != tid)
2296 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2297
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002298 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002299 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002300 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002301 lua_pushnil(L);
2302 return 1;
2303 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002304 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2305 si = appctx->owner;
2306 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002308 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002309 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002310 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002311 lua_pushnil(L);
2312 return 1;
2313 }
2314
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002315 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002316 xref_unlock(&socket->xref, peer);
2317 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002318}
2319
2320/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002321static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002322 .obj_type = OBJ_TYPE_APPLET,
2323 .name = "<LUA_TCP>",
2324 .fct = hlua_socket_handler,
2325 .release = hlua_socket_release,
2326};
2327
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002328__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002329{
2330 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2331 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002332 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002333 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002334 struct stream_interface *si;
2335 struct stream *s;
2336
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002337 /* Check if we run on the same thread than the xreator thread.
2338 * We cannot access to the socket if the thread is different.
2339 */
2340 if (socket->tid != tid)
2341 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2342
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002343 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002344 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002345 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002346 lua_pushnil(L);
2347 lua_pushstring(L, "Can't connect");
2348 return 2;
2349 }
2350 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2351 si = appctx->owner;
2352 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002353
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002354 /* Check if we run on the same thread than the xreator thread.
2355 * We cannot access to the socket if the thread is different.
2356 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002357 if (socket->tid != tid) {
2358 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002359 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002360 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002361
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002362 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002363 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002364 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002365 lua_pushnil(L);
2366 lua_pushstring(L, "Can't connect");
2367 return 2;
2368 }
2369
Willy Tarreaue09101e2018-10-16 17:37:12 +02002370 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002371
2372 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002373 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002374 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002375 lua_pushinteger(L, 1);
2376 return 1;
2377 }
2378
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002379 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2380 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002381 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002382 }
2383 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002384 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002385 return 0;
2386}
2387
2388/* This function fail or initite the connection. */
2389__LJMP static int hlua_socket_connect(struct lua_State *L)
2390{
2391 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002392 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002393 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002394 struct hlua *hlua;
2395 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002396 int low, high;
2397 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002398 struct xref *peer;
2399 struct stream_interface *si;
2400 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002401
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002402 if (lua_gettop(L) < 2)
2403 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002404
2405 /* Get args. */
2406 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002407
2408 /* Check if we run on the same thread than the xreator thread.
2409 * We cannot access to the socket if the thread is different.
2410 */
2411 if (socket->tid != tid)
2412 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2413
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002414 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002415 if (lua_gettop(L) >= 3) {
2416 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002417 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002418
Tim Duesterhus6edab862018-01-06 19:04:45 +01002419 /* Force the ip to end with a colon, to support IPv6 addresses
2420 * that are not enclosed within square brackets.
2421 */
2422 if (port > 0) {
2423 luaL_buffinit(L, &b);
2424 luaL_addstring(&b, ip);
2425 luaL_addchar(&b, ':');
2426 luaL_pushresult(&b);
2427 ip = lua_tolstring(L, lua_gettop(L), NULL);
2428 }
2429 }
2430
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002431 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002432 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002433 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002434 lua_pushnil(L);
2435 return 1;
2436 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002437
2438 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002439 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002440 if (!addr) {
2441 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002442 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002443 }
2444 if (low != high) {
2445 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002446 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002447 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002448
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002449 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002450 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002451 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002452 if (port == -1) {
2453 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002454 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002455 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002456 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2457 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002458 if (port == -1) {
2459 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002460 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002461 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002462 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002463 }
2464 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002465
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002466 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2467 si = appctx->owner;
2468 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002469
2470 if (!sockaddr_alloc(&s->target_addr)) {
2471 xref_unlock(&socket->xref, peer);
2472 WILL_LJMP(luaL_error(L, "connect: internal error"));
2473 }
2474 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002475 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002476
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002477 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002478
2479 /* inform the stream that we want to be notified whenever the
2480 * connection completes.
2481 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002482 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002483 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002484 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002485
Willy Tarreauf31af932020-01-14 09:59:38 +01002486 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002487
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002488 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2489 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002490 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002491 }
2492 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002493
2494 task_wakeup(s->task, TASK_WOKEN_INIT);
2495 /* Return yield waiting for connection. */
2496
Willy Tarreau9635e032018-10-16 17:52:55 +02002497 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002498
2499 return 0;
2500}
2501
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002502#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002503__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2504{
2505 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002506 struct xref *peer;
2507 struct appctx *appctx;
2508 struct stream_interface *si;
2509 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002510
2511 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2512 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002513
2514 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002515 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002516 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002517 lua_pushnil(L);
2518 return 1;
2519 }
2520 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2521 si = appctx->owner;
2522 s = si_strm(si);
2523
2524 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002525 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002526 return MAY_LJMP(hlua_socket_connect(L));
2527}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002528#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002529
2530__LJMP static int hlua_socket_setoption(struct lua_State *L)
2531{
2532 return 0;
2533}
2534
2535__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2536{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002537 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002538 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002539 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002540 struct xref *peer;
2541 struct appctx *appctx;
2542 struct stream_interface *si;
2543 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002544
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002545 MAY_LJMP(check_args(L, 2, "settimeout"));
2546
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002547 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002548
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002549 /* convert the timeout to millis */
2550 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002551
Thierry Fournier17a921b2018-03-08 09:59:02 +01002552 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002553 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002554 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2555
Mark Lakes56cc1252018-03-27 09:48:06 +02002556 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002557 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002558
2559 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002560 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002561 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002562
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002563 /* Check if we run on the same thread than the xreator thread.
2564 * We cannot access to the socket if the thread is different.
2565 */
2566 if (socket->tid != tid)
2567 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2568
Mark Lakes56cc1252018-03-27 09:48:06 +02002569 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002570 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002571 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002572 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2573 WILL_LJMP(lua_error(L));
2574 return 0;
2575 }
2576 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2577 si = appctx->owner;
2578 s = si_strm(si);
2579
Cyril Bonté7bb63452018-08-17 23:51:02 +02002580 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002581 s->req.rto = tmout;
2582 s->req.wto = tmout;
2583 s->res.rto = tmout;
2584 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002585 s->req.rex = tick_add_ifset(now_ms, tmout);
2586 s->req.wex = tick_add_ifset(now_ms, tmout);
2587 s->res.rex = tick_add_ifset(now_ms, tmout);
2588 s->res.wex = tick_add_ifset(now_ms, tmout);
2589
2590 s->task->expire = tick_add_ifset(now_ms, tmout);
2591 task_queue(s->task);
2592
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002593 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002594
Thierry Fourniere9636f12018-03-08 09:54:32 +01002595 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002596 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002597}
2598
2599__LJMP static int hlua_socket_new(lua_State *L)
2600{
2601 struct hlua_socket *socket;
2602 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002603 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002604 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002605
2606 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002607 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002608 hlua_pusherror(L, "socket: full stack");
2609 goto out_fail_conf;
2610 }
2611
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002612 /* Create the object: obj[0] = userdata. */
2613 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002614 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002615 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002616 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002617 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002618
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002619 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002620 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002621 hlua_pusherror(L, "socket: uninitialized pools.");
2622 goto out_fail_conf;
2623 }
2624
Willy Tarreau87b09662015-04-03 00:22:06 +02002625 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002626 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2627 lua_setmetatable(L, -2);
2628
Willy Tarreaud420a972015-04-06 00:39:18 +02002629 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002630 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002631 if (!appctx) {
2632 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002633 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002634 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002635
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002636 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002637 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002638 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2639 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002640
Willy Tarreaud420a972015-04-06 00:39:18 +02002641 /* Now create a session, task and stream for this applet */
2642 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2643 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002644 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002645 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002646 }
2647
Willy Tarreau87787ac2017-08-28 16:22:54 +02002648 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002649 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002650 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002651 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002652 }
2653
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002654 /* Initialise cross reference between stream and Lua socket object. */
2655 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002656
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002657 /* Configure "right" stream interface. this "si" is used to connect
2658 * and retrieve data from the server. The connection is initialized
2659 * with the "struct server".
2660 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002661 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002662
2663 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002664 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002665 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002666
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002667 return 1;
2668
Willy Tarreaud420a972015-04-06 00:39:18 +02002669 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002670 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002671 out_fail_sess:
2672 appctx_free(appctx);
2673 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002674 WILL_LJMP(lua_error(L));
2675 return 0;
2676}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002677
2678/*
2679 *
2680 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002681 * Class Channel
2682 *
2683 *
2684 */
2685
2686/* Returns the struct hlua_channel join to the class channel in the
2687 * stack entry "ud" or throws an argument error.
2688 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002689__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002690{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002691 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002692}
2693
Willy Tarreau47860ed2015-03-10 14:07:50 +01002694/* Pushes the channel onto the top of the stack. If the stask does not have a
2695 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002696 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002697static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002698{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002699 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002700 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002701 return 0;
2702
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002703 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002704 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002705 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002706
2707 /* Pop a class sesison metatable and affect it to the userdata. */
2708 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2709 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002710 return 1;
2711}
2712
2713/* Duplicate all the data present in the input channel and put it
2714 * in a string LUA variables. Returns -1 and push a nil value in
2715 * the stack if the channel is closed and all the data are consumed,
2716 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002717 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002718 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002719static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002720{
2721 char *blk1;
2722 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002723 size_t len1;
2724 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002725 int ret;
2726 luaL_Buffer b;
2727
Willy Tarreau06d80a92017-10-19 14:32:15 +02002728 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002729 if (unlikely(ret == 0))
2730 return 0;
2731
2732 if (unlikely(ret < 0)) {
2733 lua_pushnil(L);
2734 return -1;
2735 }
2736
2737 luaL_buffinit(L, &b);
2738 luaL_addlstring(&b, blk1, len1);
2739 if (unlikely(ret == 2))
2740 luaL_addlstring(&b, blk2, len2);
2741 luaL_pushresult(&b);
2742
2743 if (unlikely(ret == 2))
2744 return len1 + len2;
2745 return len1;
2746}
2747
2748/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2749 * a yield. This function keep the data in the buffer.
2750 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002751__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002752{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002753 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002754
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002755 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2756
Christopher Faulet3f829a42018-12-13 21:56:45 +01002757 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2758 WILL_LJMP(lua_error(L));
2759
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002760 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002761 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002762 return 1;
2763}
2764
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002765/* Check arguments for the function "hlua_channel_dup_yield". */
2766__LJMP static int hlua_channel_dup(lua_State *L)
2767{
2768 MAY_LJMP(check_args(L, 1, "dup"));
2769 MAY_LJMP(hlua_checkchannel(L, 1));
2770 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2771}
2772
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002773/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2774 * a yield. This function consumes the data in the buffer. It returns
2775 * a string containing the data or a nil pointer if no data are available
2776 * and the channel is closed.
2777 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002778__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002779{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002780 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002781 int ret;
2782
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002783 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002784
Christopher Faulet3f829a42018-12-13 21:56:45 +01002785 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2786 WILL_LJMP(lua_error(L));
2787
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002788 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002789 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002790 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002791
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002792 if (unlikely(ret == -1))
2793 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002794
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002795 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002796 return 1;
2797}
2798
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002799/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002800__LJMP static int hlua_channel_get(lua_State *L)
2801{
2802 MAY_LJMP(check_args(L, 1, "get"));
2803 MAY_LJMP(hlua_checkchannel(L, 1));
2804 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2805}
2806
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002807/* This functions consumes and returns one line. If the channel is closed,
2808 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002809 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002810 * value.
2811 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002812__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002813{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002814 char *blk1;
2815 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002816 size_t len1;
2817 size_t len2;
2818 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002819 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002820 int ret;
2821 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002822
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002823 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2824
Christopher Faulet3f829a42018-12-13 21:56:45 +01002825 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2826 WILL_LJMP(lua_error(L));
2827
Willy Tarreau06d80a92017-10-19 14:32:15 +02002828 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002829 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002830 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002831
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002832 if (ret == -1) {
2833 lua_pushnil(L);
2834 return 1;
2835 }
2836
2837 luaL_buffinit(L, &b);
2838 luaL_addlstring(&b, blk1, len1);
2839 len = len1;
2840 if (unlikely(ret == 2)) {
2841 luaL_addlstring(&b, blk2, len2);
2842 len += len2;
2843 }
2844 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002845 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002846 return 1;
2847}
2848
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002849/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002850__LJMP static int hlua_channel_getline(lua_State *L)
2851{
2852 MAY_LJMP(check_args(L, 1, "getline"));
2853 MAY_LJMP(hlua_checkchannel(L, 1));
2854 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2855}
2856
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002857/* This function takes a string as input, and append it at the
2858 * input side of channel. If the data is too big, but a space
2859 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002860 * yields. If the data is bigger than the buffer, or if the
2861 * channel is closed, it returns -1. Otherwise, it returns the
2862 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002863 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002864__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002865{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002866 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002867 size_t len;
2868 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2869 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2870 int ret;
2871 int max;
2872
Christopher Faulet3f829a42018-12-13 21:56:45 +01002873 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2874 WILL_LJMP(lua_error(L));
2875
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002876 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002877 * the request buffer if its not required.
2878 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002879 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002880 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002881 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002882 }
2883
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002884 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002885 if (max > len - l)
2886 max = len - l;
2887
Willy Tarreau06d80a92017-10-19 14:32:15 +02002888 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002889 if (ret == -2 || ret == -3) {
2890 lua_pushinteger(L, -1);
2891 return 1;
2892 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002893 if (ret == -1) {
2894 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002895 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002896 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897 l += ret;
2898 lua_pop(L, 1);
2899 lua_pushinteger(L, l);
2900
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002901 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002902 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002903 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002904 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002905 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002906 */
2907 return 1;
2908 }
2909 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002910 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002911 return 1;
2912}
2913
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002914/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2915 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002916 * buffer size is too little for the data.
2917 */
2918__LJMP static int hlua_channel_append(lua_State *L)
2919{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002920 size_t len;
2921
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002922 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002923 MAY_LJMP(hlua_checkchannel(L, 1));
2924 MAY_LJMP(luaL_checklstring(L, 2, &len));
2925 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002926 lua_pushinteger(L, 0);
2927
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002928 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002929}
2930
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002931/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002932 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002933 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002934 * or -1 if the channel is closed or if the buffer size is too
2935 * little for the data.
2936 */
2937__LJMP static int hlua_channel_set(lua_State *L)
2938{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002939 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002940
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002941 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002942 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002943 lua_pushinteger(L, 0);
2944
Christopher Faulet3f829a42018-12-13 21:56:45 +01002945 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2946 WILL_LJMP(lua_error(L));
2947
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002948 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002949
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002950 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002951}
2952
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002953/* Append data in the output side of the buffer. This data is immediately
2954 * sent. The function returns the amount of data written. If the buffer
2955 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002956 * if the channel is closed.
2957 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002958__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002959{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002960 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002961 size_t len;
2962 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2963 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2964 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002965 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002966
Christopher Faulet3f829a42018-12-13 21:56:45 +01002967 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2968 WILL_LJMP(lua_error(L));
2969
Willy Tarreau47860ed2015-03-10 14:07:50 +01002970 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971 lua_pushinteger(L, -1);
2972 return 1;
2973 }
2974
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002975 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002976 * the request buffer if its not required.
2977 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002978 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002979 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002980 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002981 }
2982
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002983 /* The written data will be immediately sent, so we can check
2984 * the available space without taking in account the reserve.
2985 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002986 * data, because the buffer will be flushed.
2987 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002988 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002989
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002990 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002991 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002992 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002993 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02002994 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002995 return 1;
2996
2997 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002998 if (max > len - l)
2999 max = len - l;
3000
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003001 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003002 * detects a non contiguous buffer and realign it.
3003 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003004 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003005 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003006
3007 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003008 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003009
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010 /* buffer replace considers that the input part is filled.
3011 * so, I must forward these new data in the output part.
3012 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003013 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003014
3015 l += max;
3016 lua_pop(L, 1);
3017 lua_pushinteger(L, l);
3018
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003019 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003020 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003021 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003022 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003023 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003024 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003025 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003026
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003027 if (l < len) {
3028 /* If we are waiting for space in the response buffer, we
3029 * must set the flag WAKERESWR. This flag required the task
3030 * wake up if any activity is detected on the response buffer.
3031 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003032 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003033 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003034 else
3035 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003036 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003037 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003038
3039 return 1;
3040}
3041
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003042/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003043 * yield the LUA process, and resume it without checking the
3044 * input arguments.
3045 */
3046__LJMP static int hlua_channel_send(lua_State *L)
3047{
3048 MAY_LJMP(check_args(L, 2, "send"));
3049 lua_pushinteger(L, 0);
3050
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003051 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003052}
3053
3054/* This function forward and amount of butes. The data pass from
3055 * the input side of the buffer to the output side, and can be
3056 * forwarded. This function never fails.
3057 *
3058 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003059 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003060 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003061__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003062{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003063 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003064 int len;
3065 int l;
3066 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003067 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003068
3069 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003070
3071 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3072 WILL_LJMP(lua_error(L));
3073
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003074 len = MAY_LJMP(luaL_checkinteger(L, 2));
3075 l = MAY_LJMP(luaL_checkinteger(L, -1));
3076
3077 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003078 if (max > ci_data(chn))
3079 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003080 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003081 l += max;
3082
3083 lua_pop(L, 1);
3084 lua_pushinteger(L, l);
3085
3086 /* Check if it miss bytes to forward. */
3087 if (l < len) {
3088 /* The the input channel or the output channel are closed, we
3089 * must return the amount of data forwarded.
3090 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003091 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003092 return 1;
3093
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003094 /* If we are waiting for space data in the response buffer, we
3095 * must set the flag WAKERESWR. This flag required the task
3096 * wake up if any activity is detected on the response buffer.
3097 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003098 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003099 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003100 else
3101 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003102
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003103 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003104 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003105 }
3106
3107 return 1;
3108}
3109
3110/* Just check the input and prepare the stack for the previous
3111 * function "hlua_channel_forward_yield"
3112 */
3113__LJMP static int hlua_channel_forward(lua_State *L)
3114{
3115 MAY_LJMP(check_args(L, 2, "forward"));
3116 MAY_LJMP(hlua_checkchannel(L, 1));
3117 MAY_LJMP(luaL_checkinteger(L, 2));
3118
3119 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003120 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003121}
3122
3123/* Just returns the number of bytes available in the input
3124 * side of the buffer. This function never fails.
3125 */
3126__LJMP static int hlua_channel_get_in_len(lua_State *L)
3127{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003128 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003129
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003130 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003131 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003132 if (IS_HTX_STRM(chn_strm(chn))) {
3133 struct htx *htx = htxbuf(&chn->buf);
3134 lua_pushinteger(L, htx->data - co_data(chn));
3135 }
3136 else
3137 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003138 return 1;
3139}
3140
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003141/* Returns true if the channel is full. */
3142__LJMP static int hlua_channel_is_full(lua_State *L)
3143{
3144 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003145
3146 MAY_LJMP(check_args(L, 1, "is_full"));
3147 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003148 /* ignore the reserve, we are not on a producer side (ie in an
3149 * applet).
3150 */
3151 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003152 return 1;
3153}
3154
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003155/* Returns true if the channel is the response channel. */
3156__LJMP static int hlua_channel_is_resp(lua_State *L)
3157{
3158 struct channel *chn;
3159
3160 MAY_LJMP(check_args(L, 1, "is_resp"));
3161 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3162
3163 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3164 return 1;
3165}
3166
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003167/* Just returns the number of bytes available in the output
3168 * side of the buffer. This function never fails.
3169 */
3170__LJMP static int hlua_channel_get_out_len(lua_State *L)
3171{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003172 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003173
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003174 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003175 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003176 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003177 return 1;
3178}
3179
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003180/*
3181 *
3182 *
3183 * Class Fetches
3184 *
3185 *
3186 */
3187
3188/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003189 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003190 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003191__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003192{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003193 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003194}
3195
3196/* This function creates and push in the stack a fetch object according
3197 * with a current TXN.
3198 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003199static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003200{
Willy Tarreau7073c472015-04-06 11:15:40 +02003201 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003202
3203 /* Check stack size. */
3204 if (!lua_checkstack(L, 3))
3205 return 0;
3206
3207 /* Create the object: obj[0] = userdata.
3208 * Note that the base of the Fetches object is the
3209 * transaction object.
3210 */
3211 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003212 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003213 lua_rawseti(L, -2, 0);
3214
Willy Tarreau7073c472015-04-06 11:15:40 +02003215 hsmp->s = txn->s;
3216 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003217 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003218 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003219
3220 /* Pop a class sesison metatable and affect it to the userdata. */
3221 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3222 lua_setmetatable(L, -2);
3223
3224 return 1;
3225}
3226
3227/* This function is an LUA binding. It is called with each sample-fetch.
3228 * It uses closure argument to store the associated sample-fetch. It
3229 * returns only one argument or throws an error. An error is thrown
3230 * only if an error is encountered during the argument parsing. If
3231 * the "sample-fetch" function fails, nil is returned.
3232 */
3233__LJMP static int hlua_run_sample_fetch(lua_State *L)
3234{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003235 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003236 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003237 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003238 int i;
3239 struct sample smp;
3240
3241 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003242 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003243
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003244 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003245 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003246
Thierry FOURNIERca988662015-12-20 18:43:03 +01003247 /* Check execution authorization. */
3248 if (f->use & SMP_USE_HTTP_ANY &&
3249 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3250 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3251 "is not available in Lua services", f->kw);
3252 WILL_LJMP(lua_error(L));
3253 }
3254
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003255 /* Get extra arguments. */
3256 for (i = 0; i < lua_gettop(L) - 1; i++) {
3257 if (i >= ARGM_NBARGS)
3258 break;
3259 hlua_lua2arg(L, i + 2, &args[i]);
3260 }
3261 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003262 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003263
3264 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003265 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003266
3267 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003268 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003269 lua_pushfstring(L, "error in arguments");
3270 WILL_LJMP(lua_error(L));
3271 }
3272
3273 /* Initialise the sample. */
3274 memset(&smp, 0, sizeof(smp));
3275
3276 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003277 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003278 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003279 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003280 lua_pushstring(L, "");
3281 else
3282 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003283 return 1;
3284 }
3285
3286 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003287 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003288 hlua_smp2lua_str(L, &smp);
3289 else
3290 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003291 return 1;
3292}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003293
3294/*
3295 *
3296 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003297 * Class Converters
3298 *
3299 *
3300 */
3301
3302/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003303 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003304 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003305__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003306{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003307 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003308}
3309
3310/* This function creates and push in the stack a Converters object
3311 * according with a current TXN.
3312 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003313static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003314{
Willy Tarreau7073c472015-04-06 11:15:40 +02003315 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003316
3317 /* Check stack size. */
3318 if (!lua_checkstack(L, 3))
3319 return 0;
3320
3321 /* Create the object: obj[0] = userdata.
3322 * Note that the base of the Converters object is the
3323 * same than the TXN object.
3324 */
3325 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003326 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003327 lua_rawseti(L, -2, 0);
3328
Willy Tarreau7073c472015-04-06 11:15:40 +02003329 hsmp->s = txn->s;
3330 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003331 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003332 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003333
Willy Tarreau87b09662015-04-03 00:22:06 +02003334 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003335 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3336 lua_setmetatable(L, -2);
3337
3338 return 1;
3339}
3340
3341/* This function is an LUA binding. It is called with each converter.
3342 * It uses closure argument to store the associated converter. It
3343 * returns only one argument or throws an error. An error is thrown
3344 * only if an error is encountered during the argument parsing. If
3345 * the converter function function fails, nil is returned.
3346 */
3347__LJMP static int hlua_run_sample_conv(lua_State *L)
3348{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003349 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003350 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003351 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003352 int i;
3353 struct sample smp;
3354
3355 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003356 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003357
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003358 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003359 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003360
3361 /* Get extra arguments. */
3362 for (i = 0; i < lua_gettop(L) - 2; i++) {
3363 if (i >= ARGM_NBARGS)
3364 break;
3365 hlua_lua2arg(L, i + 3, &args[i]);
3366 }
3367 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003368 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003369
3370 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003371 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003372
3373 /* Run the special args checker. */
3374 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3375 hlua_pusherror(L, "error in arguments");
3376 WILL_LJMP(lua_error(L));
3377 }
3378
3379 /* Initialise the sample. */
3380 if (!hlua_lua2smp(L, 2, &smp)) {
3381 hlua_pusherror(L, "error in the input argument");
3382 WILL_LJMP(lua_error(L));
3383 }
3384
Willy Tarreau1777ea62016-03-10 16:15:46 +01003385 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3386
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003387 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003388 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003389 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003390 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003391 WILL_LJMP(lua_error(L));
3392 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003393 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3394 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003395 hlua_pusherror(L, "error during the input argument casting");
3396 WILL_LJMP(lua_error(L));
3397 }
3398
3399 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003400 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003401 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003402 lua_pushstring(L, "");
3403 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003404 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003405 return 1;
3406 }
3407
3408 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003409 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003410 hlua_smp2lua_str(L, &smp);
3411 else
3412 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003413 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003414}
3415
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003416/*
3417 *
3418 *
3419 * Class AppletTCP
3420 *
3421 *
3422 */
3423
3424/* Returns a struct hlua_txn if the stack entry "ud" is
3425 * a class stream, otherwise it throws an error.
3426 */
3427__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3428{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003429 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003430}
3431
3432/* This function creates and push in the stack an Applet object
3433 * according with a current TXN.
3434 */
3435static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3436{
3437 struct hlua_appctx *appctx;
3438 struct stream_interface *si = ctx->owner;
3439 struct stream *s = si_strm(si);
3440 struct proxy *p = s->be;
3441
3442 /* Check stack size. */
3443 if (!lua_checkstack(L, 3))
3444 return 0;
3445
3446 /* Create the object: obj[0] = userdata.
3447 * Note that the base of the Converters object is the
3448 * same than the TXN object.
3449 */
3450 lua_newtable(L);
3451 appctx = lua_newuserdata(L, sizeof(*appctx));
3452 lua_rawseti(L, -2, 0);
3453 appctx->appctx = ctx;
3454 appctx->htxn.s = s;
3455 appctx->htxn.p = p;
3456
3457 /* Create the "f" field that contains a list of fetches. */
3458 lua_pushstring(L, "f");
3459 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3460 return 0;
3461 lua_settable(L, -3);
3462
3463 /* Create the "sf" field that contains a list of stringsafe fetches. */
3464 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003465 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003466 return 0;
3467 lua_settable(L, -3);
3468
3469 /* Create the "c" field that contains a list of converters. */
3470 lua_pushstring(L, "c");
3471 if (!hlua_converters_new(L, &appctx->htxn, 0))
3472 return 0;
3473 lua_settable(L, -3);
3474
3475 /* Create the "sc" field that contains a list of stringsafe converters. */
3476 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003477 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003478 return 0;
3479 lua_settable(L, -3);
3480
3481 /* Pop a class stream metatable and affect it to the table. */
3482 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3483 lua_setmetatable(L, -2);
3484
3485 return 1;
3486}
3487
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003488__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3489{
3490 struct hlua_appctx *appctx;
3491 struct stream *s;
3492 const char *name;
3493 size_t len;
3494 struct sample smp;
3495
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003496 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3497 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003498
3499 /* It is useles to retrieve the stream, but this function
3500 * runs only in a stream context.
3501 */
3502 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3503 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3504 s = appctx->htxn.s;
3505
3506 /* Converts the third argument in a sample. */
3507 hlua_lua2smp(L, 3, &smp);
3508
3509 /* Store the sample in a variable. */
3510 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003511
3512 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3513 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3514 else
3515 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3516
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003517 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003518}
3519
3520__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3521{
3522 struct hlua_appctx *appctx;
3523 struct stream *s;
3524 const char *name;
3525 size_t len;
3526 struct sample smp;
3527
3528 MAY_LJMP(check_args(L, 2, "unset_var"));
3529
3530 /* It is useles to retrieve the stream, but this function
3531 * runs only in a stream context.
3532 */
3533 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3534 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3535 s = appctx->htxn.s;
3536
3537 /* Unset the variable. */
3538 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003539 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3540 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003541}
3542
3543__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3544{
3545 struct hlua_appctx *appctx;
3546 struct stream *s;
3547 const char *name;
3548 size_t len;
3549 struct sample smp;
3550
3551 MAY_LJMP(check_args(L, 2, "get_var"));
3552
3553 /* It is useles to retrieve the stream, but this function
3554 * runs only in a stream context.
3555 */
3556 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3557 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3558 s = appctx->htxn.s;
3559
3560 smp_set_owner(&smp, s->be, s->sess, s, 0);
3561 if (!vars_get_by_name(name, len, &smp)) {
3562 lua_pushnil(L);
3563 return 1;
3564 }
3565
3566 return hlua_smp2lua(L, &smp);
3567}
3568
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003569__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3570{
3571 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3572 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003573 struct hlua *hlua;
3574
3575 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003576 if (!s->hlua)
3577 return 0;
3578 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003579
3580 MAY_LJMP(check_args(L, 2, "set_priv"));
3581
3582 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003583 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003584
3585 /* Get and store new value. */
3586 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3587 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3588
3589 return 0;
3590}
3591
3592__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3593{
3594 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3595 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003596 struct hlua *hlua;
3597
3598 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003599 if (!s->hlua) {
3600 lua_pushnil(L);
3601 return 1;
3602 }
3603 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003604
3605 /* Push configuration index in the stack. */
3606 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3607
3608 return 1;
3609}
3610
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003611/* If expected data not yet available, it returns a yield. This function
3612 * consumes the data in the buffer. It returns a string containing the
3613 * data. This string can be empty.
3614 */
3615__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3616{
3617 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3618 struct stream_interface *si = appctx->appctx->owner;
3619 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003620 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003621 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003622 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003623 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003624
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003625 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003626 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003627
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003628 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003629 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003630 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003631 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003632 }
3633
3634 /* End of data: commit the total strings and return. */
3635 if (ret < 0) {
3636 luaL_pushresult(&appctx->b);
3637 return 1;
3638 }
3639
3640 /* Ensure that the block 2 length is usable. */
3641 if (ret == 1)
3642 len2 = 0;
3643
3644 /* dont check the max length read and dont check. */
3645 luaL_addlstring(&appctx->b, blk1, len1);
3646 luaL_addlstring(&appctx->b, blk2, len2);
3647
3648 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003649 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003650 luaL_pushresult(&appctx->b);
3651 return 1;
3652}
3653
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003654/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003655__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3656{
3657 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3658
3659 /* Initialise the string catenation. */
3660 luaL_buffinit(L, &appctx->b);
3661
3662 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3663}
3664
3665/* If expected data not yet available, it returns a yield. This function
3666 * consumes the data in the buffer. It returns a string containing the
3667 * data. This string can be empty.
3668 */
3669__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3670{
3671 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3672 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003673 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003674 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003675 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003676 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003677 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003678 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003679
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003680 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003681 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003682
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003683 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003684 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003685 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003686 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003687 }
3688
3689 /* End of data: commit the total strings and return. */
3690 if (ret < 0) {
3691 luaL_pushresult(&appctx->b);
3692 return 1;
3693 }
3694
3695 /* Ensure that the block 2 length is usable. */
3696 if (ret == 1)
3697 len2 = 0;
3698
3699 if (len == -1) {
3700
3701 /* If len == -1, catenate all the data avalaile and
3702 * yield because we want to get all the data until
3703 * the end of data stream.
3704 */
3705 luaL_addlstring(&appctx->b, blk1, len1);
3706 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003707 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003708 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003709 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003710
3711 } else {
3712
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003713 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003714 if (len1 > len)
3715 len1 = len;
3716 luaL_addlstring(&appctx->b, blk1, len1);
3717 len -= len1;
3718
3719 /* Copy the second block. */
3720 if (len2 > len)
3721 len2 = len;
3722 luaL_addlstring(&appctx->b, blk2, len2);
3723 len -= len2;
3724
3725 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003726 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003727
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003728 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003729 if (len > 0) {
3730 lua_pushinteger(L, len);
3731 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003732 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003733 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003734 }
3735
3736 /* return the result. */
3737 luaL_pushresult(&appctx->b);
3738 return 1;
3739 }
3740
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003741 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003742 hlua_pusherror(L, "Lua: internal error");
3743 WILL_LJMP(lua_error(L));
3744 return 0;
3745}
3746
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003747/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003748__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3749{
3750 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3751 int len = -1;
3752
3753 if (lua_gettop(L) > 2)
3754 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3755 if (lua_gettop(L) >= 2) {
3756 len = MAY_LJMP(luaL_checkinteger(L, 2));
3757 lua_pop(L, 1);
3758 }
3759
3760 /* Confirm or set the required length */
3761 lua_pushinteger(L, len);
3762
3763 /* Initialise the string catenation. */
3764 luaL_buffinit(L, &appctx->b);
3765
3766 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3767}
3768
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003769/* Append data in the output side of the buffer. This data is immediately
3770 * sent. The function returns the amount of data written. If the buffer
3771 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003772 * if the channel is closed.
3773 */
3774__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3775{
3776 size_t len;
3777 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3778 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3779 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3780 struct stream_interface *si = appctx->appctx->owner;
3781 struct channel *chn = si_ic(si);
3782 int max;
3783
3784 /* Get the max amount of data which can write as input in the channel. */
3785 max = channel_recv_max(chn);
3786 if (max > (len - l))
3787 max = len - l;
3788
3789 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003790 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003791
3792 /* update counters. */
3793 l += max;
3794 lua_pop(L, 1);
3795 lua_pushinteger(L, l);
3796
3797 /* If some data is not send, declares the situation to the
3798 * applet, and returns a yield.
3799 */
3800 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003801 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003802 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003803 }
3804
3805 return 1;
3806}
3807
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003808/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003809 * yield the LUA process, and resume it without checking the
3810 * input arguments.
3811 */
3812__LJMP static int hlua_applet_tcp_send(lua_State *L)
3813{
3814 MAY_LJMP(check_args(L, 2, "send"));
3815 lua_pushinteger(L, 0);
3816
3817 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3818}
3819
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003820/*
3821 *
3822 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003823 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003824 *
3825 *
3826 */
3827
3828/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003829 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003830 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003831__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003832{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003833 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003834}
3835
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003836/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003837 * according with a current TXN.
3838 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003839static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003840{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003841 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003842 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003843 struct stream_interface *si = ctx->owner;
3844 struct stream *s = si_strm(si);
3845 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003846 struct htx *htx;
3847 struct htx_blk *blk;
3848 struct htx_sl *sl;
3849 struct ist path;
3850 unsigned long long len = 0;
3851 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003852
3853 /* Check stack size. */
3854 if (!lua_checkstack(L, 3))
3855 return 0;
3856
3857 /* Create the object: obj[0] = userdata.
3858 * Note that the base of the Converters object is the
3859 * same than the TXN object.
3860 */
3861 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003862 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003863 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003864 appctx->appctx = ctx;
3865 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003866 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003867 appctx->htxn.s = s;
3868 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003869
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003870 /* Create the "f" field that contains a list of fetches. */
3871 lua_pushstring(L, "f");
3872 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3873 return 0;
3874 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003875
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003876 /* Create the "sf" field that contains a list of stringsafe fetches. */
3877 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003878 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003879 return 0;
3880 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003881
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003882 /* Create the "c" field that contains a list of converters. */
3883 lua_pushstring(L, "c");
3884 if (!hlua_converters_new(L, &appctx->htxn, 0))
3885 return 0;
3886 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003887
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003888 /* Create the "sc" field that contains a list of stringsafe converters. */
3889 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003890 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003891 return 0;
3892 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003893
Christopher Fauleta2097962019-07-15 16:25:33 +02003894 htx = htxbuf(&s->req.buf);
3895 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003896 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003897 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003898
Christopher Fauleta2097962019-07-15 16:25:33 +02003899 /* Stores the request method. */
3900 lua_pushstring(L, "method");
3901 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3902 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003903
Christopher Fauleta2097962019-07-15 16:25:33 +02003904 /* Stores the http version. */
3905 lua_pushstring(L, "version");
3906 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3907 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003908
Christopher Fauleta2097962019-07-15 16:25:33 +02003909 /* creates an array of headers. hlua_http_get_headers() crates and push
3910 * the array on the top of the stack.
3911 */
3912 lua_pushstring(L, "headers");
3913 htxn.s = s;
3914 htxn.p = px;
3915 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01003916 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02003917 return 0;
3918 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003919
Christopher Fauleta2097962019-07-15 16:25:33 +02003920 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01003921 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02003922 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003923
Christopher Fauleta2097962019-07-15 16:25:33 +02003924 p = path.ptr;
3925 end = path.ptr + path.len;
3926 q = p;
3927 while (q < end && *q != '?')
3928 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003929
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003930 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003931 lua_pushstring(L, "path");
3932 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003933 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003934
Christopher Fauleta2097962019-07-15 16:25:33 +02003935 /* Stores the query string. */
3936 lua_pushstring(L, "qs");
3937 if (*q == '?')
3938 q++;
3939 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003940 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003941 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003942
Christopher Fauleta2097962019-07-15 16:25:33 +02003943 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3944 struct htx_blk *blk = htx_get_blk(htx, pos);
3945 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003946
Christopher Fauleta2097962019-07-15 16:25:33 +02003947 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3948 break;
3949 if (type == HTX_BLK_DATA)
3950 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003951 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003952 if (htx->extra != ULLONG_MAX)
3953 len += htx->extra;
3954
3955 /* Stores the request path. */
3956 lua_pushstring(L, "length");
3957 lua_pushinteger(L, len);
3958 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003959
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003960 /* Create an empty array of HTTP request headers. */
3961 lua_pushstring(L, "response");
3962 lua_newtable(L);
3963 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003964
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003965 /* Pop a class stream metatable and affect it to the table. */
3966 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3967 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003968
3969 return 1;
3970}
3971
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003972__LJMP static int hlua_applet_http_set_var(lua_State *L)
3973{
3974 struct hlua_appctx *appctx;
3975 struct stream *s;
3976 const char *name;
3977 size_t len;
3978 struct sample smp;
3979
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003980 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3981 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003982
3983 /* It is useles to retrieve the stream, but this function
3984 * runs only in a stream context.
3985 */
3986 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3987 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3988 s = appctx->htxn.s;
3989
3990 /* Converts the third argument in a sample. */
3991 hlua_lua2smp(L, 3, &smp);
3992
3993 /* Store the sample in a variable. */
3994 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003995
3996 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3997 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3998 else
3999 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4000
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004001 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004002}
4003
4004__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4005{
4006 struct hlua_appctx *appctx;
4007 struct stream *s;
4008 const char *name;
4009 size_t len;
4010 struct sample smp;
4011
4012 MAY_LJMP(check_args(L, 2, "unset_var"));
4013
4014 /* It is useles to retrieve the stream, but this function
4015 * runs only in a stream context.
4016 */
4017 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4018 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4019 s = appctx->htxn.s;
4020
4021 /* Unset the variable. */
4022 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004023 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4024 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004025}
4026
4027__LJMP static int hlua_applet_http_get_var(lua_State *L)
4028{
4029 struct hlua_appctx *appctx;
4030 struct stream *s;
4031 const char *name;
4032 size_t len;
4033 struct sample smp;
4034
4035 MAY_LJMP(check_args(L, 2, "get_var"));
4036
4037 /* It is useles to retrieve the stream, but this function
4038 * runs only in a stream context.
4039 */
4040 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4041 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4042 s = appctx->htxn.s;
4043
4044 smp_set_owner(&smp, s->be, s->sess, s, 0);
4045 if (!vars_get_by_name(name, len, &smp)) {
4046 lua_pushnil(L);
4047 return 1;
4048 }
4049
4050 return hlua_smp2lua(L, &smp);
4051}
4052
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004053__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4054{
4055 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4056 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004057 struct hlua *hlua;
4058
4059 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004060 if (!s->hlua)
4061 return 0;
4062 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004063
4064 MAY_LJMP(check_args(L, 2, "set_priv"));
4065
4066 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004067 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004068
4069 /* Get and store new value. */
4070 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4071 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4072
4073 return 0;
4074}
4075
4076__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4077{
4078 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4079 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004080 struct hlua *hlua;
4081
4082 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004083 if (!s->hlua) {
4084 lua_pushnil(L);
4085 return 1;
4086 }
4087 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004088
4089 /* Push configuration index in the stack. */
4090 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4091
4092 return 1;
4093}
4094
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004095/* If expected data not yet available, it returns a yield. This function
4096 * consumes the data in the buffer. It returns a string containing the
4097 * data. This string can be empty.
4098 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004099__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004100{
4101 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4102 struct stream_interface *si = appctx->appctx->owner;
4103 struct channel *req = si_oc(si);
4104 struct htx *htx;
4105 struct htx_blk *blk;
4106 size_t count;
4107 int stop = 0;
4108
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004109 htx = htx_from_buf(&req->buf);
4110 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004111 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004112
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004113 while (count && !stop && blk) {
4114 enum htx_blk_type type = htx_get_blk_type(blk);
4115 uint32_t sz = htx_get_blksz(blk);
4116 struct ist v;
4117 uint32_t vlen;
4118 char *nl;
4119
Christopher Faulete461e342018-12-18 16:43:35 +01004120 if (type == HTX_BLK_EOM) {
4121 stop = 1;
4122 break;
4123 }
4124
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004125 vlen = sz;
4126 if (vlen > count) {
4127 if (type != HTX_BLK_DATA)
4128 break;
4129 vlen = count;
4130 }
4131
4132 switch (type) {
4133 case HTX_BLK_UNUSED:
4134 break;
4135
4136 case HTX_BLK_DATA:
4137 v = htx_get_blk_value(htx, blk);
4138 v.len = vlen;
4139 nl = istchr(v, '\n');
4140 if (nl != NULL) {
4141 stop = 1;
4142 vlen = nl - v.ptr + 1;
4143 }
4144 luaL_addlstring(&appctx->b, v.ptr, vlen);
4145 break;
4146
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004147 case HTX_BLK_TLR:
4148 case HTX_BLK_EOM:
4149 stop = 1;
4150 break;
4151
4152 default:
4153 break;
4154 }
4155
4156 co_set_data(req, co_data(req) - vlen);
4157 count -= vlen;
4158 if (sz == vlen)
4159 blk = htx_remove_blk(htx, blk);
4160 else {
4161 htx_cut_data_blk(htx, blk, vlen);
4162 break;
4163 }
4164 }
4165
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004166 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004167 if (!stop) {
4168 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004169 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004170 }
4171
4172 /* return the result. */
4173 luaL_pushresult(&appctx->b);
4174 return 1;
4175}
4176
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004177
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004178/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004179__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004180{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004181 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004182
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004183 /* Initialise the string catenation. */
4184 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004185
Christopher Fauleta2097962019-07-15 16:25:33 +02004186 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004187}
4188
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004189/* If expected data not yet available, it returns a yield. This function
4190 * consumes the data in the buffer. It returns a string containing the
4191 * data. This string can be empty.
4192 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004193__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004194{
4195 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4196 struct stream_interface *si = appctx->appctx->owner;
4197 struct channel *req = si_oc(si);
4198 struct htx *htx;
4199 struct htx_blk *blk;
4200 size_t count;
4201 int len;
4202
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004203 htx = htx_from_buf(&req->buf);
4204 len = MAY_LJMP(luaL_checkinteger(L, 2));
4205 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004206 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004207 while (count && len && blk) {
4208 enum htx_blk_type type = htx_get_blk_type(blk);
4209 uint32_t sz = htx_get_blksz(blk);
4210 struct ist v;
4211 uint32_t vlen;
4212
Christopher Faulete461e342018-12-18 16:43:35 +01004213 if (type == HTX_BLK_EOM) {
4214 len = 0;
4215 break;
4216 }
4217
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004218 vlen = sz;
4219 if (len > 0 && vlen > len)
4220 vlen = len;
4221 if (vlen > count) {
4222 if (type != HTX_BLK_DATA)
4223 break;
4224 vlen = count;
4225 }
4226
4227 switch (type) {
4228 case HTX_BLK_UNUSED:
4229 break;
4230
4231 case HTX_BLK_DATA:
4232 v = htx_get_blk_value(htx, blk);
4233 luaL_addlstring(&appctx->b, v.ptr, vlen);
4234 break;
4235
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004236 case HTX_BLK_TLR:
4237 case HTX_BLK_EOM:
4238 len = 0;
4239 break;
4240
4241 default:
4242 break;
4243 }
4244
4245 co_set_data(req, co_data(req) - vlen);
4246 count -= vlen;
4247 if (len > 0)
4248 len -= vlen;
4249 if (sz == vlen)
4250 blk = htx_remove_blk(htx, blk);
4251 else {
4252 htx_cut_data_blk(htx, blk, vlen);
4253 break;
4254 }
4255 }
4256
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004257 htx_to_buf(htx, &req->buf);
4258
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004259 /* If we are no other data available, yield waiting for new data. */
4260 if (len) {
4261 if (len > 0) {
4262 lua_pushinteger(L, len);
4263 lua_replace(L, 2);
4264 }
4265 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004266 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004267 }
4268
4269 /* return the result. */
4270 luaL_pushresult(&appctx->b);
4271 return 1;
4272}
4273
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004274/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004275__LJMP static int hlua_applet_http_recv(lua_State *L)
4276{
4277 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4278 int len = -1;
4279
4280 /* Check arguments. */
4281 if (lua_gettop(L) > 2)
4282 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4283 if (lua_gettop(L) >= 2) {
4284 len = MAY_LJMP(luaL_checkinteger(L, 2));
4285 lua_pop(L, 1);
4286 }
4287
Christopher Fauleta2097962019-07-15 16:25:33 +02004288 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004289
Christopher Fauleta2097962019-07-15 16:25:33 +02004290 /* Initialise the string catenation. */
4291 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004292
Christopher Fauleta2097962019-07-15 16:25:33 +02004293 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004294}
4295
4296/* Append data in the output side of the buffer. This data is immediately
4297 * sent. The function returns the amount of data written. If the buffer
4298 * cannot contain the data, the function yields. The function returns -1
4299 * if the channel is closed.
4300 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004301__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004302{
4303 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4304 struct stream_interface *si = appctx->appctx->owner;
4305 struct channel *res = si_ic(si);
4306 struct htx *htx = htx_from_buf(&res->buf);
4307 const char *data;
4308 size_t len;
4309 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4310 int max;
4311
Christopher Faulet9060fc02019-07-03 11:39:30 +02004312 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004313 if (!max)
4314 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004315
4316 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4317
4318 /* Get the max amount of data which can write as input in the channel. */
4319 if (max > (len - l))
4320 max = len - l;
4321
4322 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004323 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004324 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004325
4326 /* update counters. */
4327 l += max;
4328 lua_pop(L, 1);
4329 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004330
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004331 /* If some data is not send, declares the situation to the
4332 * applet, and returns a yield.
4333 */
4334 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004335 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004336 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004337 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004338 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004339 }
4340
Christopher Fauleta2097962019-07-15 16:25:33 +02004341 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004342 return 1;
4343}
4344
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004345/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004346 * yield the LUA process, and resume it without checking the
4347 * input arguments.
4348 */
4349__LJMP static int hlua_applet_http_send(lua_State *L)
4350{
4351 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004352
4353 /* We want to send some data. Headers must be sent. */
4354 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4355 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4356 WILL_LJMP(lua_error(L));
4357 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004358
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004359 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004360 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004361
Christopher Fauleta2097962019-07-15 16:25:33 +02004362 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004363}
4364
4365__LJMP static int hlua_applet_http_addheader(lua_State *L)
4366{
4367 const char *name;
4368 int ret;
4369
4370 MAY_LJMP(hlua_checkapplet_http(L, 1));
4371 name = MAY_LJMP(luaL_checkstring(L, 2));
4372 MAY_LJMP(luaL_checkstring(L, 3));
4373
4374 /* Push in the stack the "response" entry. */
4375 ret = lua_getfield(L, 1, "response");
4376 if (ret != LUA_TTABLE) {
4377 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4378 "is expected as an array. %s found", lua_typename(L, ret));
4379 WILL_LJMP(lua_error(L));
4380 }
4381
4382 /* check if the header is already registered if it is not
4383 * the case, register it.
4384 */
4385 ret = lua_getfield(L, -1, name);
4386 if (ret == LUA_TNIL) {
4387
4388 /* Entry not found. */
4389 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4390
4391 /* Insert the new header name in the array in the top of the stack.
4392 * It left the new array in the top of the stack.
4393 */
4394 lua_newtable(L);
4395 lua_pushvalue(L, 2);
4396 lua_pushvalue(L, -2);
4397 lua_settable(L, -4);
4398
4399 } else if (ret != LUA_TTABLE) {
4400
4401 /* corruption error. */
4402 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4403 "is expected as an array. %s found", name, lua_typename(L, ret));
4404 WILL_LJMP(lua_error(L));
4405 }
4406
4407 /* Now the top od thestack is an array of values. We push
4408 * the header value as new entry.
4409 */
4410 lua_pushvalue(L, 3);
4411 ret = lua_rawlen(L, -2);
4412 lua_rawseti(L, -2, ret + 1);
4413 lua_pushboolean(L, 1);
4414 return 1;
4415}
4416
4417__LJMP static int hlua_applet_http_status(lua_State *L)
4418{
4419 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4420 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004421 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004422
4423 if (status < 100 || status > 599) {
4424 lua_pushboolean(L, 0);
4425 return 1;
4426 }
4427
4428 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004429 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004430 lua_pushboolean(L, 1);
4431 return 1;
4432}
4433
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004434
Christopher Fauleta2097962019-07-15 16:25:33 +02004435__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004436{
4437 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4438 struct stream_interface *si = appctx->appctx->owner;
4439 struct channel *res = si_ic(si);
4440 struct htx *htx;
4441 struct htx_sl *sl;
4442 struct h1m h1m;
4443 const char *status, *reason;
4444 const char *name, *value;
4445 size_t nlen, vlen;
4446 unsigned int flags;
4447
4448 /* Send the message at once. */
4449 htx = htx_from_buf(&res->buf);
4450 h1m_init_res(&h1m);
4451
4452 /* Use the same http version than the request. */
4453 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4454 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4455 if (reason == NULL)
4456 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4457 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4458 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4459 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4460 }
4461 else {
4462 flags = HTX_SL_F_IS_RESP;
4463 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4464 }
4465 if (!sl) {
4466 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4467 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4468 WILL_LJMP(lua_error(L));
4469 }
4470 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4471
4472 /* Get the array associated to the field "response" in the object AppletHTTP. */
4473 lua_pushvalue(L, 0);
4474 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4475 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4476 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4477 WILL_LJMP(lua_error(L));
4478 }
4479
4480 /* Browse the list of headers. */
4481 lua_pushnil(L);
4482 while(lua_next(L, -2) != 0) {
4483 /* We expect a string as -2. */
4484 if (lua_type(L, -2) != LUA_TSTRING) {
4485 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4486 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4487 lua_typename(L, lua_type(L, -2)));
4488 WILL_LJMP(lua_error(L));
4489 }
4490 name = lua_tolstring(L, -2, &nlen);
4491
4492 /* We expect an array as -1. */
4493 if (lua_type(L, -1) != LUA_TTABLE) {
4494 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4495 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4496 name,
4497 lua_typename(L, lua_type(L, -1)));
4498 WILL_LJMP(lua_error(L));
4499 }
4500
4501 /* Browse the table who is on the top of the stack. */
4502 lua_pushnil(L);
4503 while(lua_next(L, -2) != 0) {
4504 int id;
4505
4506 /* We expect a number as -2. */
4507 if (lua_type(L, -2) != LUA_TNUMBER) {
4508 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4509 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4510 name,
4511 lua_typename(L, lua_type(L, -2)));
4512 WILL_LJMP(lua_error(L));
4513 }
4514 id = lua_tointeger(L, -2);
4515
4516 /* We expect a string as -2. */
4517 if (lua_type(L, -1) != LUA_TSTRING) {
4518 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4519 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4520 name, id,
4521 lua_typename(L, lua_type(L, -1)));
4522 WILL_LJMP(lua_error(L));
4523 }
4524 value = lua_tolstring(L, -1, &vlen);
4525
4526 /* Simple Protocol checks. */
4527 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004528 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004529 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4530 struct ist v = ist2(value, vlen);
4531 int ret;
4532
4533 ret = h1_parse_cont_len_header(&h1m, &v);
4534 if (ret < 0) {
4535 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4536 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4537 name);
4538 WILL_LJMP(lua_error(L));
4539 }
4540 else if (ret == 0)
4541 goto next; /* Skip it */
4542 }
4543
4544 /* Add a new header */
4545 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4546 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4547 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4548 name);
4549 WILL_LJMP(lua_error(L));
4550 }
4551 next:
4552 /* Remove the array from the stack, and get next element with a remaining string. */
4553 lua_pop(L, 1);
4554 }
4555
4556 /* Remove the array from the stack, and get next element with a remaining string. */
4557 lua_pop(L, 1);
4558 }
4559
4560 if (h1m.flags & H1_MF_CHNK)
4561 h1m.flags &= ~H1_MF_CLEN;
4562 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4563 h1m.flags |= H1_MF_XFER_LEN;
4564
4565 /* Uset HTX start-line flags */
4566 if (h1m.flags & H1_MF_XFER_ENC)
4567 flags |= HTX_SL_F_XFER_ENC;
4568 if (h1m.flags & H1_MF_XFER_LEN) {
4569 flags |= HTX_SL_F_XFER_LEN;
4570 if (h1m.flags & H1_MF_CHNK)
4571 flags |= HTX_SL_F_CHNK;
4572 else if (h1m.flags & H1_MF_CLEN)
4573 flags |= HTX_SL_F_CLEN;
4574 if (h1m.body_len == 0)
4575 flags |= HTX_SL_F_BODYLESS;
4576 }
4577 sl->flags |= flags;
4578
4579 /* If we dont have a content-length set, and the HTTP version is 1.1
4580 * and the status code implies the presence of a message body, we must
4581 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004582 * for the keepalive compliance. If the applet announces a transfer-encoding
4583 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004584 */
4585 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4586 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4587 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4588 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4589 /* Add a new header */
4590 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4591 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4592 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4593 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4594 WILL_LJMP(lua_error(L));
4595 }
4596 }
4597
4598 /* Finalize headers. */
4599 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4600 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4601 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4602 WILL_LJMP(lua_error(L));
4603 }
4604
4605 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4606 b_reset(&res->buf);
4607 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4608 WILL_LJMP(lua_error(L));
4609 }
4610
4611 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004612 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004613
4614 /* Headers sent, set the flag. */
4615 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4616 return 0;
4617
4618}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004619/* We will build the status line and the headers of the HTTP response.
4620 * We will try send at once if its not possible, we give back the hand
4621 * waiting for more room.
4622 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004623__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004624{
4625 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4626 struct stream_interface *si = appctx->appctx->owner;
4627 struct channel *res = si_ic(si);
4628
4629 if (co_data(res)) {
4630 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004631 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004632 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004633 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004634}
4635
4636
Christopher Fauleta2097962019-07-15 16:25:33 +02004637__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004638{
Christopher Fauleta2097962019-07-15 16:25:33 +02004639 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004640}
4641
Christopher Fauleta2097962019-07-15 16:25:33 +02004642/*
4643 *
4644 *
4645 * Class HTTP
4646 *
4647 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004648 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004649
4650/* Returns a struct hlua_txn if the stack entry "ud" is
4651 * a class stream, otherwise it throws an error.
4652 */
4653__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004654{
Christopher Fauleta2097962019-07-15 16:25:33 +02004655 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4656}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004657
Christopher Fauleta2097962019-07-15 16:25:33 +02004658/* This function creates and push in the stack a HTTP object
4659 * according with a current TXN.
4660 */
4661static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4662{
4663 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004664
Christopher Fauleta2097962019-07-15 16:25:33 +02004665 /* Check stack size. */
4666 if (!lua_checkstack(L, 3))
4667 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004668
Christopher Fauleta2097962019-07-15 16:25:33 +02004669 /* Create the object: obj[0] = userdata.
4670 * Note that the base of the Converters object is the
4671 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004672 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004673 lua_newtable(L);
4674 htxn = lua_newuserdata(L, sizeof(*htxn));
4675 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004676
4677 htxn->s = txn->s;
4678 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004679 htxn->dir = txn->dir;
4680 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004681
4682 /* Pop a class stream metatable and affect it to the table. */
4683 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4684 lua_setmetatable(L, -2);
4685
4686 return 1;
4687}
4688
4689/* This function creates ans returns an array of HTTP headers.
4690 * This function does not fails. It is used as wrapper with the
4691 * 2 following functions.
4692 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004693__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004694{
Christopher Fauleta2097962019-07-15 16:25:33 +02004695 struct htx *htx;
4696 int32_t pos;
4697
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004698 /* Create the table. */
4699 lua_newtable(L);
4700
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004701
Christopher Fauleta2097962019-07-15 16:25:33 +02004702 htx = htxbuf(&msg->chn->buf);
4703 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4704 struct htx_blk *blk = htx_get_blk(htx, pos);
4705 enum htx_blk_type type = htx_get_blk_type(blk);
4706 struct ist n, v;
4707 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004708
Christopher Fauleta2097962019-07-15 16:25:33 +02004709 if (type == HTX_BLK_HDR) {
4710 n = htx_get_blk_name(htx,blk);
4711 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004712 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004713 else if (type == HTX_BLK_EOH)
4714 break;
4715 else
4716 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004717
Christopher Fauleta2097962019-07-15 16:25:33 +02004718 /* Check for existing entry:
4719 * assume that the table is on the top of the stack, and
4720 * push the key in the stack, the function lua_gettable()
4721 * perform the lookup.
4722 */
4723 lua_pushlstring(L, n.ptr, n.len);
4724 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004725
Christopher Fauleta2097962019-07-15 16:25:33 +02004726 switch (lua_type(L, -1)) {
4727 case LUA_TNIL:
4728 /* Table not found, create it. */
4729 lua_pop(L, 1); /* remove the nil value. */
4730 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4731 lua_newtable(L); /* create and push empty table. */
4732 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4733 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4734 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004735 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004736
Christopher Fauleta2097962019-07-15 16:25:33 +02004737 case LUA_TTABLE:
4738 /* Entry found: push the value in the table. */
4739 len = lua_rawlen(L, -1);
4740 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4741 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4742 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4743 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004744
Christopher Fauleta2097962019-07-15 16:25:33 +02004745 default:
4746 /* Other cases are errors. */
4747 hlua_pusherror(L, "internal error during the parsing of headers.");
4748 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004749 }
4750 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004751 return 1;
4752}
4753
4754__LJMP static int hlua_http_req_get_headers(lua_State *L)
4755{
4756 struct hlua_txn *htxn;
4757
4758 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4759 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4760
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004761 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004762 WILL_LJMP(lua_error(L));
4763
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004764 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004765}
4766
4767__LJMP static int hlua_http_res_get_headers(lua_State *L)
4768{
4769 struct hlua_txn *htxn;
4770
4771 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4772 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4773
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004774 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004775 WILL_LJMP(lua_error(L));
4776
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004777 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004778}
4779
4780/* This function replace full header, or just a value in
4781 * the request or in the response. It is a wrapper fir the
4782 * 4 following functions.
4783 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004784__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004785{
4786 size_t name_len;
4787 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4788 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4789 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004790 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004791 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004792
Dragan Dosen26743032019-04-30 15:54:36 +02004793 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004794 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4795
Christopher Fauleta2097962019-07-15 16:25:33 +02004796 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004797 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004798 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004799 return 0;
4800}
4801
4802__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4803{
4804 struct hlua_txn *htxn;
4805
4806 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4807 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4808
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004809 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004810 WILL_LJMP(lua_error(L));
4811
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004812 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004813}
4814
4815__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4816{
4817 struct hlua_txn *htxn;
4818
4819 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4820 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4821
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004822 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004823 WILL_LJMP(lua_error(L));
4824
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004825 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004826}
4827
4828__LJMP static int hlua_http_req_rep_val(lua_State *L)
4829{
4830 struct hlua_txn *htxn;
4831
4832 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4833 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4834
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004835 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004836 WILL_LJMP(lua_error(L));
4837
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004838 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004839}
4840
4841__LJMP static int hlua_http_res_rep_val(lua_State *L)
4842{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004843 struct hlua_txn *htxn;
4844
4845 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4846 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4847
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004848 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004849 WILL_LJMP(lua_error(L));
4850
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004851 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004852}
4853
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004854/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004855 * It is a wrapper for the 2 following functions.
4856 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004857__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004858{
4859 size_t len;
4860 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004861 struct htx *htx = htxbuf(&msg->chn->buf);
4862 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004863
Christopher Fauleta2097962019-07-15 16:25:33 +02004864 ctx.blk = NULL;
4865 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4866 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004867 return 0;
4868}
4869
4870__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4871{
4872 struct hlua_txn *htxn;
4873
4874 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4875 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4876
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004877 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004878 WILL_LJMP(lua_error(L));
4879
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004880 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004881}
4882
4883__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4884{
4885 struct hlua_txn *htxn;
4886
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004887 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004888 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4889
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004890 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004891 WILL_LJMP(lua_error(L));
4892
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004893 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004894}
4895
4896/* This function adds an header. It is a wrapper used by
4897 * the 2 following functions.
4898 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004899__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004900{
4901 size_t name_len;
4902 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4903 size_t value_len;
4904 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004905 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004906
Christopher Fauleta2097962019-07-15 16:25:33 +02004907 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4908 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004909 return 0;
4910}
4911
4912__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4913{
4914 struct hlua_txn *htxn;
4915
4916 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4917 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4918
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004919 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004920 WILL_LJMP(lua_error(L));
4921
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004922 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004923}
4924
4925__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4926{
4927 struct hlua_txn *htxn;
4928
4929 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4930 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4931
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004932 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004933 WILL_LJMP(lua_error(L));
4934
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004935 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004936}
4937
4938static int hlua_http_req_set_hdr(lua_State *L)
4939{
4940 struct hlua_txn *htxn;
4941
4942 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4943 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4944
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004945 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004946 WILL_LJMP(lua_error(L));
4947
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004948 hlua_http_del_hdr(L, &htxn->s->txn->req);
4949 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004950}
4951
4952static int hlua_http_res_set_hdr(lua_State *L)
4953{
4954 struct hlua_txn *htxn;
4955
4956 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4957 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4958
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004959 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004960 WILL_LJMP(lua_error(L));
4961
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004962 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
4963 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004964}
4965
4966/* This function set the method. */
4967static int hlua_http_req_set_meth(lua_State *L)
4968{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004969 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004970 size_t name_len;
4971 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004972
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004973 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004974 WILL_LJMP(lua_error(L));
4975
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004976 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004977 return 1;
4978}
4979
4980/* This function set the method. */
4981static int hlua_http_req_set_path(lua_State *L)
4982{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004983 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004984 size_t name_len;
4985 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004986
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004987 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004988 WILL_LJMP(lua_error(L));
4989
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004990 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004991 return 1;
4992}
4993
4994/* This function set the query-string. */
4995static int hlua_http_req_set_query(lua_State *L)
4996{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004997 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004998 size_t name_len;
4999 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005000
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005001 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005002 WILL_LJMP(lua_error(L));
5003
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005004 /* Check length. */
5005 if (name_len > trash.size - 1) {
5006 lua_pushboolean(L, 0);
5007 return 1;
5008 }
5009
5010 /* Add the mark question as prefix. */
5011 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005012 trash.area[trash.data++] = '?';
5013 memcpy(trash.area + trash.data, name, name_len);
5014 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005015
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005016 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005017 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005018 return 1;
5019}
5020
5021/* This function set the uri. */
5022static int hlua_http_req_set_uri(lua_State *L)
5023{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005024 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005025 size_t name_len;
5026 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005027
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005028 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005029 WILL_LJMP(lua_error(L));
5030
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005031 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005032 return 1;
5033}
5034
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005035/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005036static int hlua_http_res_set_status(lua_State *L)
5037{
5038 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5039 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005040 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5041 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005042
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005043 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005044 WILL_LJMP(lua_error(L));
5045
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005046 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005047 return 0;
5048}
5049
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005050/*
5051 *
5052 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005053 * Class TXN
5054 *
5055 *
5056 */
5057
5058/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005059 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005060 */
5061__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5062{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005063 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005064}
5065
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005066__LJMP static int hlua_set_var(lua_State *L)
5067{
5068 struct hlua_txn *htxn;
5069 const char *name;
5070 size_t len;
5071 struct sample smp;
5072
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005073 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5074 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005075
5076 /* It is useles to retrieve the stream, but this function
5077 * runs only in a stream context.
5078 */
5079 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5080 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5081
5082 /* Converts the third argument in a sample. */
5083 hlua_lua2smp(L, 3, &smp);
5084
5085 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005086 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005087
5088 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5089 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5090 else
5091 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5092
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005093 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005094}
5095
Christopher Faulet85d79c92016-11-09 16:54:56 +01005096__LJMP static int hlua_unset_var(lua_State *L)
5097{
5098 struct hlua_txn *htxn;
5099 const char *name;
5100 size_t len;
5101 struct sample smp;
5102
5103 MAY_LJMP(check_args(L, 2, "unset_var"));
5104
5105 /* It is useles to retrieve the stream, but this function
5106 * runs only in a stream context.
5107 */
5108 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5109 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5110
5111 /* Unset the variable. */
5112 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005113 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5114 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005115}
5116
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005117__LJMP static int hlua_get_var(lua_State *L)
5118{
5119 struct hlua_txn *htxn;
5120 const char *name;
5121 size_t len;
5122 struct sample smp;
5123
5124 MAY_LJMP(check_args(L, 2, "get_var"));
5125
5126 /* It is useles to retrieve the stream, but this function
5127 * runs only in a stream context.
5128 */
5129 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5130 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5131
Willy Tarreau7560dd42016-03-10 16:28:58 +01005132 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005133 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005134 lua_pushnil(L);
5135 return 1;
5136 }
5137
5138 return hlua_smp2lua(L, &smp);
5139}
5140
Willy Tarreau59551662015-03-10 14:23:13 +01005141__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005142{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005143 struct hlua *hlua;
5144
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005145 MAY_LJMP(check_args(L, 2, "set_priv"));
5146
Willy Tarreau87b09662015-04-03 00:22:06 +02005147 /* It is useles to retrieve the stream, but this function
5148 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005149 */
5150 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005151 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005152
5153 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005154 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005155
5156 /* Get and store new value. */
5157 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5158 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5159
5160 return 0;
5161}
5162
Willy Tarreau59551662015-03-10 14:23:13 +01005163__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005164{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005165 struct hlua *hlua;
5166
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005167 MAY_LJMP(check_args(L, 1, "get_priv"));
5168
Willy Tarreau87b09662015-04-03 00:22:06 +02005169 /* It is useles to retrieve the stream, but this function
5170 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005171 */
5172 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005173 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005174
5175 /* Push configuration index in the stack. */
5176 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5177
5178 return 1;
5179}
5180
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005181/* Create stack entry containing a class TXN. This function
5182 * return 0 if the stack does not contains free slots,
5183 * otherwise it returns 1.
5184 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005185static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005186{
Willy Tarreaude491382015-04-06 11:04:28 +02005187 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005188
5189 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005190 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005191 return 0;
5192
5193 /* NOTE: The allocation never fails. The failure
5194 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005195 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005196 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005197 /* Create the object: obj[0] = userdata. */
5198 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005199 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005200 lua_rawseti(L, -2, 0);
5201
Willy Tarreaude491382015-04-06 11:04:28 +02005202 htxn->s = s;
5203 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005204 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005205 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005206
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005207 /* Create the "f" field that contains a list of fetches. */
5208 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005209 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005210 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005211 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005212
5213 /* Create the "sf" field that contains a list of stringsafe fetches. */
5214 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005215 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005216 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005217 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005218
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005219 /* Create the "c" field that contains a list of converters. */
5220 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005221 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005222 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005223 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005224
5225 /* Create the "sc" field that contains a list of stringsafe converters. */
5226 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005227 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005228 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005229 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005230
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005231 /* Create the "req" field that contains the request channel object. */
5232 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005233 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005234 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005235 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005236
5237 /* Create the "res" field that contains the response channel object. */
5238 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005239 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005240 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005241 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005242
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005243 /* Creates the HTTP object is the current proxy allows http. */
5244 lua_pushstring(L, "http");
5245 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005246 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005247 return 0;
5248 }
5249 else
5250 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005251 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005252
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005253 /* Pop a class sesison metatable and affect it to the userdata. */
5254 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5255 lua_setmetatable(L, -2);
5256
5257 return 1;
5258}
5259
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005260__LJMP static int hlua_txn_deflog(lua_State *L)
5261{
5262 const char *msg;
5263 struct hlua_txn *htxn;
5264
5265 MAY_LJMP(check_args(L, 2, "deflog"));
5266 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5267 msg = MAY_LJMP(luaL_checkstring(L, 2));
5268
5269 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5270 return 0;
5271}
5272
5273__LJMP static int hlua_txn_log(lua_State *L)
5274{
5275 int level;
5276 const char *msg;
5277 struct hlua_txn *htxn;
5278
5279 MAY_LJMP(check_args(L, 3, "log"));
5280 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5281 level = MAY_LJMP(luaL_checkinteger(L, 2));
5282 msg = MAY_LJMP(luaL_checkstring(L, 3));
5283
5284 if (level < 0 || level >= NB_LOG_LEVELS)
5285 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5286
5287 hlua_sendlog(htxn->s->be, level, msg);
5288 return 0;
5289}
5290
5291__LJMP static int hlua_txn_log_debug(lua_State *L)
5292{
5293 const char *msg;
5294 struct hlua_txn *htxn;
5295
5296 MAY_LJMP(check_args(L, 2, "Debug"));
5297 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5298 msg = MAY_LJMP(luaL_checkstring(L, 2));
5299 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5300 return 0;
5301}
5302
5303__LJMP static int hlua_txn_log_info(lua_State *L)
5304{
5305 const char *msg;
5306 struct hlua_txn *htxn;
5307
5308 MAY_LJMP(check_args(L, 2, "Info"));
5309 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5310 msg = MAY_LJMP(luaL_checkstring(L, 2));
5311 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5312 return 0;
5313}
5314
5315__LJMP static int hlua_txn_log_warning(lua_State *L)
5316{
5317 const char *msg;
5318 struct hlua_txn *htxn;
5319
5320 MAY_LJMP(check_args(L, 2, "Warning"));
5321 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5322 msg = MAY_LJMP(luaL_checkstring(L, 2));
5323 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5324 return 0;
5325}
5326
5327__LJMP static int hlua_txn_log_alert(lua_State *L)
5328{
5329 const char *msg;
5330 struct hlua_txn *htxn;
5331
5332 MAY_LJMP(check_args(L, 2, "Alert"));
5333 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5334 msg = MAY_LJMP(luaL_checkstring(L, 2));
5335 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5336 return 0;
5337}
5338
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005339__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5340{
5341 struct hlua_txn *htxn;
5342 int ll;
5343
5344 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5345 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5346 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5347
5348 if (ll < 0 || ll > 7)
5349 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5350
5351 htxn->s->logs.level = ll;
5352 return 0;
5353}
5354
5355__LJMP static int hlua_txn_set_tos(lua_State *L)
5356{
5357 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005358 int tos;
5359
5360 MAY_LJMP(check_args(L, 2, "set_tos"));
5361 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5362 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5363
Willy Tarreau1a18b542018-12-11 16:37:42 +01005364 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005365 return 0;
5366}
5367
5368__LJMP static int hlua_txn_set_mark(lua_State *L)
5369{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005370 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005371 int mark;
5372
5373 MAY_LJMP(check_args(L, 2, "set_mark"));
5374 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5375 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5376
Lukas Tribus579e3e32019-08-11 18:03:45 +02005377 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005378 return 0;
5379}
5380
Patrick Hemmer268a7072018-05-11 12:52:31 -04005381__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5382{
5383 struct hlua_txn *htxn;
5384
5385 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5386 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5387 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5388 return 0;
5389}
5390
5391__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5392{
5393 struct hlua_txn *htxn;
5394
5395 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5396 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5397 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5398 return 0;
5399}
5400
Christopher Faulet700d9e82020-01-31 12:21:52 +01005401/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005402 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005403 * message and terminate the transaction. It returns 1 on success and 0 on
5404 * error. The Reply must be on top of the stack.
5405 */
5406__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5407{
5408 struct htx *htx;
5409 struct htx_sl *sl;
5410 struct h1m h1m;
5411 const char *status, *reason, *body;
5412 size_t status_len, reason_len, body_len;
5413 int ret, code, flags;
5414
5415 code = 200;
5416 status = "200";
5417 status_len = 3;
5418 ret = lua_getfield(L, -1, "status");
5419 if (ret == LUA_TNUMBER) {
5420 code = lua_tointeger(L, -1);
5421 status = lua_tolstring(L, -1, &status_len);
5422 }
5423 lua_pop(L, 1);
5424
5425 reason = http_get_reason(code);
5426 reason_len = strlen(reason);
5427 ret = lua_getfield(L, -1, "reason");
5428 if (ret == LUA_TSTRING)
5429 reason = lua_tolstring(L, -1, &reason_len);
5430 lua_pop(L, 1);
5431
5432 body = NULL;
5433 body_len = 0;
5434 ret = lua_getfield(L, -1, "body");
5435 if (ret == LUA_TSTRING)
5436 body = lua_tolstring(L, -1, &body_len);
5437 lua_pop(L, 1);
5438
5439 /* Prepare the response before inserting the headers */
5440 h1m_init_res(&h1m);
5441 htx = htx_from_buf(&s->res.buf);
5442 channel_htx_truncate(&s->res, htx);
5443 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5444 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5445 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5446 ist2(status, status_len), ist2(reason, reason_len));
5447 }
5448 else {
5449 flags = HTX_SL_F_IS_RESP;
5450 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5451 ist2(status, status_len), ist2(reason, reason_len));
5452 }
5453 if (!sl)
5454 goto fail;
5455 sl->info.res.status = code;
5456
5457 /* Push in the stack the "headers" entry. */
5458 ret = lua_getfield(L, -1, "headers");
5459 if (ret != LUA_TTABLE)
5460 goto skip_headers;
5461
5462 lua_pushnil(L);
5463 while (lua_next(L, -2) != 0) {
5464 struct ist name, value;
5465 const char *n, *v;
5466 size_t nlen, vlen;
5467
5468 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5469 /* Skip element if the key is not a string or if the value is not a table */
5470 goto next_hdr;
5471 }
5472
5473 n = lua_tolstring(L, -2, &nlen);
5474 name = ist2(n, nlen);
5475 if (isteqi(name, ist("content-length"))) {
5476 /* Always skip content-length header. It will be added
5477 * later with the correct len
5478 */
5479 goto next_hdr;
5480 }
5481
5482 /* Loop on header's values */
5483 lua_pushnil(L);
5484 while (lua_next(L, -2)) {
5485 if (!lua_isstring(L, -1)) {
5486 /* Skip the value if it is not a string */
5487 goto next_value;
5488 }
5489
5490 v = lua_tolstring(L, -1, &vlen);
5491 value = ist2(v, vlen);
5492
5493 if (isteqi(name, ist("transfer-encoding")))
5494 h1_parse_xfer_enc_header(&h1m, value);
5495 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5496 goto fail;
5497
5498 next_value:
5499 lua_pop(L, 1);
5500 }
5501
5502 next_hdr:
5503 lua_pop(L, 1);
5504 }
5505 skip_headers:
5506 lua_pop(L, 1);
5507
5508 /* Update h1m flags: CLEN is set if CHNK is not present */
5509 if (!(h1m.flags & H1_MF_CHNK)) {
5510 const char *clen = ultoa(body_len);
5511
5512 h1m.flags |= H1_MF_CLEN;
5513 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5514 goto fail;
5515 }
5516 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5517 h1m.flags |= H1_MF_XFER_LEN;
5518
5519 /* Update HTX start-line flags */
5520 if (h1m.flags & H1_MF_XFER_ENC)
5521 flags |= HTX_SL_F_XFER_ENC;
5522 if (h1m.flags & H1_MF_XFER_LEN) {
5523 flags |= HTX_SL_F_XFER_LEN;
5524 if (h1m.flags & H1_MF_CHNK)
5525 flags |= HTX_SL_F_CHNK;
5526 else if (h1m.flags & H1_MF_CLEN)
5527 flags |= HTX_SL_F_CLEN;
5528 if (h1m.body_len == 0)
5529 flags |= HTX_SL_F_BODYLESS;
5530 }
5531 sl->flags |= flags;
5532
5533
5534 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5535 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5536 !htx_add_endof(htx, HTX_BLK_EOM))
5537 goto fail;
5538
5539 /* Now, forward the response and terminate the transaction */
5540 s->txn->status = code;
5541 htx_to_buf(htx, &s->res.buf);
5542 if (!http_forward_proxy_resp(s, 1))
5543 goto fail;
5544
5545 return 1;
5546
5547 fail:
5548 channel_htx_truncate(&s->res, htx);
5549 return 0;
5550}
5551
5552/* Terminate a transaction if called from a lua action. For TCP streams,
5553 * processing is just aborted. Nothing is returned to the client and all
5554 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5555 * is forwarded to the client before terminating the transaction. On success,
5556 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5557 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5558 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005559 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005560__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005561{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005562 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005563 struct stream *s;
5564 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005565
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005566 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005567
Christopher Faulet700d9e82020-01-31 12:21:52 +01005568 /* If the flags NOTERM is set, we cannot terminate the session, so we
5569 * just end the execution of the current lua code. */
5570 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005571 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005572
Christopher Faulet700d9e82020-01-31 12:21:52 +01005573 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005574 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005575 struct channel *req = &s->req;
5576 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005577
Christopher Faulet700d9e82020-01-31 12:21:52 +01005578 channel_auto_read(req);
5579 channel_abort(req);
5580 channel_auto_close(req);
5581 channel_erase(req);
5582
5583 res->wex = tick_add_ifset(now_ms, res->wto);
5584 channel_auto_read(res);
5585 channel_auto_close(res);
5586 channel_shutr_now(res);
5587
5588 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5589 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005590 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005591
Christopher Faulet700d9e82020-01-31 12:21:52 +01005592 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5593 /* No reply or invalid reply */
5594 s->txn->status = 0;
5595 http_reply_and_close(s, 0, NULL);
5596 }
5597 else {
5598 /* Remove extra args to have the reply on top of the stack */
5599 if (lua_gettop(L) > 2)
5600 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005601
Christopher Faulet700d9e82020-01-31 12:21:52 +01005602 if (!hlua_txn_forward_reply(L, s)) {
5603 if (!(s->flags & SF_ERR_MASK))
5604 s->flags |= SF_ERR_PRXCOND;
5605 lua_pushinteger(L, ACT_RET_ERR);
5606 WILL_LJMP(hlua_done(L));
5607 return 0; /* Never reached */
5608 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005609 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005610
Christopher Faulet700d9e82020-01-31 12:21:52 +01005611 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5612 if (htxn->dir == SMP_OPT_DIR_REQ) {
5613 /* let's log the request time */
5614 s->logs.tv_request = now;
5615 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5616 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5617 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005618
Christopher Faulet700d9e82020-01-31 12:21:52 +01005619 done:
5620 if (!(s->flags & SF_ERR_MASK))
5621 s->flags |= SF_ERR_LOCAL;
5622 if (!(s->flags & SF_FINST_MASK))
5623 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005624
Christopher Faulet4ad73102020-03-05 11:07:31 +01005625 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005626 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005627 return 0;
5628}
5629
Christopher Faulet700d9e82020-01-31 12:21:52 +01005630/*
5631 *
5632 *
5633 * Class REPLY
5634 *
5635 *
5636 */
5637
5638/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5639 * free slots, the function fails and returns 0;
5640 */
5641static int hlua_txn_reply_new(lua_State *L)
5642{
5643 struct hlua_txn *htxn;
5644 const char *reason, *body = NULL;
5645 int ret, status;
5646
5647 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005648 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005649 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5650 WILL_LJMP(lua_error(L));
5651 }
5652
5653 /* Default value */
5654 status = 200;
5655 reason = http_get_reason(status);
5656
5657 if (lua_istable(L, 2)) {
5658 /* load status and reason from the table argument at index 2 */
5659 ret = lua_getfield(L, 2, "status");
5660 if (ret == LUA_TNIL)
5661 goto reason;
5662 else if (ret != LUA_TNUMBER) {
5663 /* invalid status: ignore the reason */
5664 goto body;
5665 }
5666 status = lua_tointeger(L, -1);
5667
5668 reason:
5669 lua_pop(L, 1); /* restore the stack: remove status */
5670 ret = lua_getfield(L, 2, "reason");
5671 if (ret == LUA_TSTRING)
5672 reason = lua_tostring(L, -1);
5673
5674 body:
5675 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5676 ret = lua_getfield(L, 2, "body");
5677 if (ret == LUA_TSTRING)
5678 body = lua_tostring(L, -1);
5679 lua_pop(L, 1); /* restore the stack: remove body */
5680 }
5681
5682 /* Create the Reply table */
5683 lua_newtable(L);
5684
5685 /* Add status element */
5686 lua_pushstring(L, "status");
5687 lua_pushinteger(L, status);
5688 lua_settable(L, -3);
5689
5690 /* Add reason element */
5691 reason = http_get_reason(status);
5692 lua_pushstring(L, "reason");
5693 lua_pushstring(L, reason);
5694 lua_settable(L, -3);
5695
5696 /* Add body element, nil if undefined */
5697 lua_pushstring(L, "body");
5698 if (body)
5699 lua_pushstring(L, body);
5700 else
5701 lua_pushnil(L);
5702 lua_settable(L, -3);
5703
5704 /* Add headers element */
5705 lua_pushstring(L, "headers");
5706 lua_newtable(L);
5707
5708 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5709 if (lua_istable(L, 2)) {
5710 /* load headers from the table argument at index 2. If it is a table, copy it. */
5711 ret = lua_getfield(L, 2, "headers");
5712 if (ret == LUA_TTABLE) {
5713 /* stack: [ ... <headers:table>, <table> ] */
5714 lua_pushnil(L);
5715 while (lua_next(L, -2) != 0) {
5716 /* stack: [ ... <headers:table>, <table>, k, v] */
5717 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5718 /* invalid value type, skip it */
5719 lua_pop(L, 1);
5720 continue;
5721 }
5722
5723
5724 /* Duplicate the key and swap it with the value. */
5725 lua_pushvalue(L, -2);
5726 lua_insert(L, -2);
5727 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5728
5729 lua_newtable(L);
5730 lua_insert(L, -2);
5731 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5732
5733 if (lua_isstring(L, -1)) {
5734 /* push the value in the inner table */
5735 lua_rawseti(L, -2, 1);
5736 }
5737 else { /* table */
5738 lua_pushnil(L);
5739 while (lua_next(L, -2) != 0) {
5740 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5741 if (!lua_isstring(L, -1)) {
5742 /* invalid value type, skip it*/
5743 lua_pop(L, 1);
5744 continue;
5745 }
5746 /* push the value in the inner table */
5747 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5748 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5749 }
5750 lua_pop(L, 1);
5751 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5752 }
5753
5754 /* push (k,v) on the stack in the headers table:
5755 * stack: [ ... <headers:table>, <table>, k, k, v ]
5756 */
5757 lua_settable(L, -5);
5758 /* stack: [ ... <headers:table>, <table>, k ] */
5759 }
5760 }
5761 lua_pop(L, 1);
5762 }
5763 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5764 lua_settable(L, -3);
5765 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5766
5767 /* Pop a class sesison metatable and affect it to the userdata. */
5768 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5769 lua_setmetatable(L, -2);
5770 return 1;
5771}
5772
5773/* Set the reply status code, and optionally the reason. If no reason is
5774 * provided, the default one corresponding to the status code is used.
5775 */
5776__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5777{
5778 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5779 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5780
5781 /* First argument (self) must be a table */
5782 luaL_checktype(L, 1, LUA_TTABLE);
5783
5784 if (status < 100 || status > 599) {
5785 lua_pushboolean(L, 0);
5786 return 1;
5787 }
5788 if (!reason)
5789 reason = http_get_reason(status);
5790
5791 lua_pushinteger(L, status);
5792 lua_setfield(L, 1, "status");
5793
5794 lua_pushstring(L, reason);
5795 lua_setfield(L, 1, "reason");
5796
5797 lua_pushboolean(L, 1);
5798 return 1;
5799}
5800
5801/* Add a header into the reply object. Each header name is associated to an
5802 * array of values in the "headers" table. If the header name is not found, a
5803 * new entry is created.
5804 */
5805__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5806{
5807 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5808 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5809 int ret;
5810
5811 /* First argument (self) must be a table */
5812 luaL_checktype(L, 1, LUA_TTABLE);
5813
5814 /* Push in the stack the "headers" entry. */
5815 ret = lua_getfield(L, 1, "headers");
5816 if (ret != LUA_TTABLE) {
5817 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5818 WILL_LJMP(lua_error(L));
5819 }
5820
5821 /* check if the header is already registered. If not, register it. */
5822 ret = lua_getfield(L, -1, name);
5823 if (ret == LUA_TNIL) {
5824 /* Entry not found. */
5825 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5826
5827 /* Insert the new header name in the array in the top of the stack.
5828 * It left the new array in the top of the stack.
5829 */
5830 lua_newtable(L);
5831 lua_pushstring(L, name);
5832 lua_pushvalue(L, -2);
5833 lua_settable(L, -4);
5834 }
5835 else if (ret != LUA_TTABLE) {
5836 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5837 WILL_LJMP(lua_error(L));
5838 }
5839
5840 /* Now the top od thestack is an array of values. We push
5841 * the header value as new entry.
5842 */
5843 lua_pushstring(L, value);
5844 ret = lua_rawlen(L, -2);
5845 lua_rawseti(L, -2, ret + 1);
5846
5847 lua_pushboolean(L, 1);
5848 return 1;
5849}
5850
5851/* Remove all occurrences of a given header name. */
5852__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5853{
5854 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5855 int ret;
5856
5857 /* First argument (self) must be a table */
5858 luaL_checktype(L, 1, LUA_TTABLE);
5859
5860 /* Push in the stack the "headers" entry. */
5861 ret = lua_getfield(L, 1, "headers");
5862 if (ret != LUA_TTABLE) {
5863 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5864 WILL_LJMP(lua_error(L));
5865 }
5866
5867 lua_pushstring(L, name);
5868 lua_pushnil(L);
5869 lua_settable(L, -3);
5870
5871 lua_pushboolean(L, 1);
5872 return 1;
5873}
5874
5875/* Set the reply's body. Overwrite any existing entry. */
5876__LJMP static int hlua_txn_reply_set_body(lua_State *L)
5877{
5878 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
5879
5880 /* First argument (self) must be a table */
5881 luaL_checktype(L, 1, LUA_TTABLE);
5882
5883 lua_pushstring(L, payload);
5884 lua_setfield(L, 1, "body");
5885
5886 lua_pushboolean(L, 1);
5887 return 1;
5888}
5889
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005890__LJMP static int hlua_log(lua_State *L)
5891{
5892 int level;
5893 const char *msg;
5894
5895 MAY_LJMP(check_args(L, 2, "log"));
5896 level = MAY_LJMP(luaL_checkinteger(L, 1));
5897 msg = MAY_LJMP(luaL_checkstring(L, 2));
5898
5899 if (level < 0 || level >= NB_LOG_LEVELS)
5900 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5901
5902 hlua_sendlog(NULL, level, msg);
5903 return 0;
5904}
5905
5906__LJMP static int hlua_log_debug(lua_State *L)
5907{
5908 const char *msg;
5909
5910 MAY_LJMP(check_args(L, 1, "debug"));
5911 msg = MAY_LJMP(luaL_checkstring(L, 1));
5912 hlua_sendlog(NULL, LOG_DEBUG, msg);
5913 return 0;
5914}
5915
5916__LJMP static int hlua_log_info(lua_State *L)
5917{
5918 const char *msg;
5919
5920 MAY_LJMP(check_args(L, 1, "info"));
5921 msg = MAY_LJMP(luaL_checkstring(L, 1));
5922 hlua_sendlog(NULL, LOG_INFO, msg);
5923 return 0;
5924}
5925
5926__LJMP static int hlua_log_warning(lua_State *L)
5927{
5928 const char *msg;
5929
5930 MAY_LJMP(check_args(L, 1, "warning"));
5931 msg = MAY_LJMP(luaL_checkstring(L, 1));
5932 hlua_sendlog(NULL, LOG_WARNING, msg);
5933 return 0;
5934}
5935
5936__LJMP static int hlua_log_alert(lua_State *L)
5937{
5938 const char *msg;
5939
5940 MAY_LJMP(check_args(L, 1, "alert"));
5941 msg = MAY_LJMP(luaL_checkstring(L, 1));
5942 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005943 return 0;
5944}
5945
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005946__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005947{
5948 int wakeup_ms = lua_tointeger(L, -1);
5949 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005950 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005951 return 0;
5952}
5953
5954__LJMP static int hlua_sleep(lua_State *L)
5955{
5956 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005957 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005958
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005959 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005960
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005961 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005962 wakeup_ms = tick_add(now_ms, delay);
5963 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005964
Willy Tarreau9635e032018-10-16 17:52:55 +02005965 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005966 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005967}
5968
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005969__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005970{
5971 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005972 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005973
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005974 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005975
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005976 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005977 wakeup_ms = tick_add(now_ms, delay);
5978 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005979
Willy Tarreau9635e032018-10-16 17:52:55 +02005980 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005981 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005982}
5983
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005984/* This functionis an LUA binding. it permits to give back
5985 * the hand at the HAProxy scheduler. It is used when the
5986 * LUA processing consumes a lot of time.
5987 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005988__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005989{
5990 return 0;
5991}
5992
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005993__LJMP static int hlua_yield(lua_State *L)
5994{
Willy Tarreau9635e032018-10-16 17:52:55 +02005995 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005996 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005997}
5998
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005999/* This function change the nice of the currently executed
6000 * task. It is used set low or high priority at the current
6001 * task.
6002 */
Willy Tarreau59551662015-03-10 14:23:13 +01006003__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006004{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006005 struct hlua *hlua;
6006 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006007
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006008 MAY_LJMP(check_args(L, 1, "set_nice"));
6009 hlua = hlua_gethlua(L);
6010 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006011
6012 /* If he task is not set, I'm in a start mode. */
6013 if (!hlua || !hlua->task)
6014 return 0;
6015
6016 if (nice < -1024)
6017 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006018 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006019 nice = 1024;
6020
6021 hlua->task->nice = nice;
6022 return 0;
6023}
6024
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006025/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006026 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6027 * return an E_AGAIN signal, the emmiter of this signal must set a
6028 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006029 *
6030 * Task wrapper are longjmp safe because the only one Lua code
6031 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006032 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006033struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006034{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006035 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006036 enum hlua_exec status;
6037
Christopher Faulet5bc99722018-04-25 10:34:45 +02006038 if (task->thread_mask == MAX_THREADS_MASK)
6039 task_set_affinity(task, tid_bit);
6040
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006041 /* If it is the first call to the task, we must initialize the
6042 * execution timeouts.
6043 */
6044 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006045 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006046
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006047 /* Execute the Lua code. */
6048 status = hlua_ctx_resume(hlua, 1);
6049
6050 switch (status) {
6051 /* finished or yield */
6052 case HLUA_E_OK:
6053 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006054 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006055 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006056 break;
6057
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006058 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006059 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006060 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006061 break;
6062
6063 /* finished with error. */
6064 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006065 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006066 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006067 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006068 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006069 break;
6070
6071 case HLUA_E_ERR:
6072 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006073 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006074 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006075 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006076 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006077 break;
6078 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006079 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006080}
6081
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006082/* This function is an LUA binding that register LUA function to be
6083 * executed after the HAProxy configuration parsing and before the
6084 * HAProxy scheduler starts. This function expect only one LUA
6085 * argument that is a function. This function returns nothing, but
6086 * throws if an error is encountered.
6087 */
6088__LJMP static int hlua_register_init(lua_State *L)
6089{
6090 struct hlua_init_function *init;
6091 int ref;
6092
6093 MAY_LJMP(check_args(L, 1, "register_init"));
6094
6095 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6096
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006097 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006098 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006099 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006100
6101 init->function_ref = ref;
6102 LIST_ADDQ(&hlua_init_functions, &init->l);
6103 return 0;
6104}
6105
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006106/* This functio is an LUA binding. It permits to register a task
6107 * executed in parallel of the main HAroxy activity. The task is
6108 * created and it is set in the HAProxy scheduler. It can be called
6109 * from the "init" section, "post init" or during the runtime.
6110 *
6111 * Lua prototype:
6112 *
6113 * <none> core.register_task(<function>)
6114 */
6115static int hlua_register_task(lua_State *L)
6116{
6117 struct hlua *hlua;
6118 struct task *task;
6119 int ref;
6120
6121 MAY_LJMP(check_args(L, 1, "register_task"));
6122
6123 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6124
Willy Tarreaubafbe012017-11-24 17:34:44 +01006125 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006126 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006127 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006128
Emeric Brunc60def82017-09-27 14:59:38 +02006129 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006130 if (!task)
6131 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6132
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006133 task->context = hlua;
6134 task->process = hlua_process_task;
6135
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006136 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006137 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006138
6139 /* Restore the function in the stack. */
6140 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6141 hlua->nargs = 0;
6142
6143 /* Schedule task. */
6144 task_schedule(task, now_ms);
6145
6146 return 0;
6147}
6148
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006149/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6150 * doesn't allow "yield" functions because the HAProxy engine cannot
6151 * resume converters.
6152 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006153static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006154{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006155 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006156 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006157 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006158
Willy Tarreaube508f12016-03-10 11:47:01 +01006159 if (!stream)
6160 return 0;
6161
Willy Tarreau87b09662015-04-03 00:22:06 +02006162 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006163 * Lua context can be not initialized. This behavior
6164 * permits to save performances because a systematic
6165 * Lua initialization cause 5% performances loss.
6166 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006167 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006168 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006169 if (!stream->hlua) {
6170 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6171 return 0;
6172 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006173 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006174 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6175 return 0;
6176 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006177 }
6178
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006179 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006180 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006181
6182 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006183 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6184 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6185 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006186 else
6187 error = "critical error";
6188 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006189 return 0;
6190 }
6191
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006192 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006193 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006194 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006195 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006196 return 0;
6197 }
6198
6199 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006200 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006201
6202 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006203 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006204 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006205 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006206 return 0;
6207 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006208 hlua_smp2lua(stream->hlua->T, smp);
6209 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006210
6211 /* push keywords in the stack. */
6212 if (arg_p) {
6213 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006214 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006215 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006216 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006217 return 0;
6218 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006219 hlua_arg2lua(stream->hlua->T, arg_p);
6220 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006221 }
6222 }
6223
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006224 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006225 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006226
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006227 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006228 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006229 }
6230
6231 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006232 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006233 /* finished. */
6234 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006235 /* If the stack is empty, the function fails. */
6236 if (lua_gettop(stream->hlua->T) <= 0)
6237 return 0;
6238
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006239 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006240 hlua_lua2smp(stream->hlua->T, -1, smp);
6241 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006242 return 1;
6243
6244 /* yield. */
6245 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006246 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006247 return 0;
6248
6249 /* finished with error. */
6250 case HLUA_E_ERRMSG:
6251 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006252 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006253 fcn->name, lua_tostring(stream->hlua->T, -1));
6254 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006255 return 0;
6256
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006257 case HLUA_E_ETMOUT:
6258 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6259 return 0;
6260
6261 case HLUA_E_NOMEM:
6262 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6263 return 0;
6264
6265 case HLUA_E_YIELD:
6266 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6267 return 0;
6268
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006269 case HLUA_E_ERR:
6270 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006271 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006272 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006273
6274 default:
6275 return 0;
6276 }
6277}
6278
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006279/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6280 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006281 * resume sample-fetches. This function will be called by the sample
6282 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006283 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006284static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6285 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006286{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006287 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006288 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006289 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006290 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006291
Willy Tarreaube508f12016-03-10 11:47:01 +01006292 if (!stream)
6293 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006294
Willy Tarreau87b09662015-04-03 00:22:06 +02006295 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006296 * Lua context can be not initialized. This behavior
6297 * permits to save performances because a systematic
6298 * Lua initialization cause 5% performances loss.
6299 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006300 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006301 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006302 if (!stream->hlua) {
6303 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6304 return 0;
6305 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006306 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006307 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6308 return 0;
6309 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006310 }
6311
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006312 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006313 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006314
6315 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006316 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6317 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6318 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006319 else
6320 error = "critical error";
6321 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006322 return 0;
6323 }
6324
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006325 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006326 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006327 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006328 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006329 return 0;
6330 }
6331
6332 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006333 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006334
6335 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006336 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006337 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006338 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006339 return 0;
6340 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006341 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006342
6343 /* push keywords in the stack. */
6344 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6345 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006346 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006347 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006348 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006349 return 0;
6350 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006351 hlua_arg2lua(stream->hlua->T, arg_p);
6352 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006353 }
6354
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006355 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006356 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006357
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006358 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006359 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006360 }
6361
6362 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006363 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006364 /* finished. */
6365 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006366 /* If the stack is empty, the function fails. */
6367 if (lua_gettop(stream->hlua->T) <= 0)
6368 return 0;
6369
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006370 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006371 hlua_lua2smp(stream->hlua->T, -1, smp);
6372 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006373
6374 /* Set the end of execution flag. */
6375 smp->flags &= ~SMP_F_MAY_CHANGE;
6376 return 1;
6377
6378 /* yield. */
6379 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006380 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006381 return 0;
6382
6383 /* finished with error. */
6384 case HLUA_E_ERRMSG:
6385 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006386 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006387 fcn->name, lua_tostring(stream->hlua->T, -1));
6388 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006389 return 0;
6390
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006391 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006392 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6393 return 0;
6394
6395 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006396 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6397 return 0;
6398
6399 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006400 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6401 return 0;
6402
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006403 case HLUA_E_ERR:
6404 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006405 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006406 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006407
6408 default:
6409 return 0;
6410 }
6411}
6412
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006413/* This function is an LUA binding used for registering
6414 * "sample-conv" functions. It expects a converter name used
6415 * in the haproxy configuration file, and an LUA function.
6416 */
6417__LJMP static int hlua_register_converters(lua_State *L)
6418{
6419 struct sample_conv_kw_list *sck;
6420 const char *name;
6421 int ref;
6422 int len;
6423 struct hlua_function *fcn;
6424
6425 MAY_LJMP(check_args(L, 2, "register_converters"));
6426
6427 /* First argument : converter name. */
6428 name = MAY_LJMP(luaL_checkstring(L, 1));
6429
6430 /* Second argument : lua function. */
6431 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6432
6433 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006434 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006435 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006436 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006437 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006438 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006439 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006440
6441 /* Fill fcn. */
6442 fcn->name = strdup(name);
6443 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006444 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006445 fcn->function_ref = ref;
6446
6447 /* List head */
6448 sck->list.n = sck->list.p = NULL;
6449
6450 /* converter keyword. */
6451 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006452 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006453 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006454 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006455
6456 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6457 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006458 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 +01006459 sck->kw[0].val_args = NULL;
6460 sck->kw[0].in_type = SMP_T_STR;
6461 sck->kw[0].out_type = SMP_T_STR;
6462 sck->kw[0].private = fcn;
6463
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006464 /* Register this new converter */
6465 sample_register_convs(sck);
6466
6467 return 0;
6468}
6469
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006470/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006471 * "sample-fetch" functions. It expects a converter name used
6472 * in the haproxy configuration file, and an LUA function.
6473 */
6474__LJMP static int hlua_register_fetches(lua_State *L)
6475{
6476 const char *name;
6477 int ref;
6478 int len;
6479 struct sample_fetch_kw_list *sfk;
6480 struct hlua_function *fcn;
6481
6482 MAY_LJMP(check_args(L, 2, "register_fetches"));
6483
6484 /* First argument : sample-fetch name. */
6485 name = MAY_LJMP(luaL_checkstring(L, 1));
6486
6487 /* Second argument : lua function. */
6488 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6489
6490 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006491 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006492 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006493 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006494 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006495 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006496 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006497
6498 /* Fill fcn. */
6499 fcn->name = strdup(name);
6500 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006501 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006502 fcn->function_ref = ref;
6503
6504 /* List head */
6505 sfk->list.n = sfk->list.p = NULL;
6506
6507 /* sample-fetch keyword. */
6508 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006509 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006510 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006511 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006512
6513 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6514 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006515 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 +01006516 sfk->kw[0].val_args = NULL;
6517 sfk->kw[0].out_type = SMP_T_STR;
6518 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6519 sfk->kw[0].val = 0;
6520 sfk->kw[0].private = fcn;
6521
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006522 /* Register this new fetch. */
6523 sample_register_fetches(sfk);
6524
6525 return 0;
6526}
6527
Christopher Faulet501465d2020-02-26 14:54:16 +01006528/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006529 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006530__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006531{
6532 struct hlua *hlua = hlua_gethlua(L);
6533 unsigned int delay;
6534 unsigned int wakeup_ms;
6535
6536 MAY_LJMP(check_args(L, 1, "wake_time"));
6537
6538 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6539 wakeup_ms = tick_add(now_ms, delay);
6540 hlua->wake_time = wakeup_ms;
6541 return 0;
6542}
6543
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006544/* This function is a wrapper to execute each LUA function declared as an action
6545 * wrapper during the initialisation period. This function may return any
6546 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6547 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6548 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006549 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006550static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006551 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006552{
6553 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006554 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006555 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006556 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006557
6558 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006559 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6560 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6561 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6562 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006563 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006564 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006565 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006566 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006567
Willy Tarreau87b09662015-04-03 00:22:06 +02006568 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006569 * Lua context can be not initialized. This behavior
6570 * permits to save performances because a systematic
6571 * Lua initialization cause 5% performances loss.
6572 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006573 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006574 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006575 if (!s->hlua) {
6576 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6577 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006578 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006579 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006580 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006581 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6582 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006583 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006584 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006585 }
6586
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006587 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006588 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006589
6590 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006591 if (!SET_SAFE_LJMP(s->hlua->T)) {
6592 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6593 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006594 else
6595 error = "critical error";
6596 SEND_ERR(px, "Lua function '%s': %s.\n",
6597 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006598 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006599 }
6600
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006601 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006602 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006603 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006604 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006605 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006606 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006607 }
6608
6609 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006610 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006611
Willy Tarreau87b09662015-04-03 00:22:06 +02006612 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006613 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006614 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006615 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006616 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006617 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006618 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006619 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006620
6621 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006622 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006623 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006624 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006625 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006626 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006627 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006628 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006629 lua_pushstring(s->hlua->T, *arg);
6630 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006631 }
6632
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006633 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006634 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006635
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006636 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006637 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006638 }
6639
6640 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006641 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006642 /* finished. */
6643 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006644 /* Catch the return value */
6645 if (lua_gettop(s->hlua->T) > 0)
6646 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006647
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006648 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006649 if (act_ret == ACT_RET_YIELD) {
6650 if (flags & ACT_OPT_FINAL)
6651 goto err_yield;
6652
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006653 if (dir == SMP_OPT_DIR_REQ)
6654 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6655 s->hlua->wake_time);
6656 else
6657 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6658 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006659 }
6660 goto end;
6661
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006662 /* yield. */
6663 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006664 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006665 if (dir == SMP_OPT_DIR_REQ)
6666 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6667 s->hlua->wake_time);
6668 else
6669 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6670 s->hlua->wake_time);
6671
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006672 /* Some actions can be wake up when a "write" event
6673 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006674 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006675 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006676 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006677 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006678 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006679 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006680 act_ret = ACT_RET_YIELD;
6681 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006682
6683 /* finished with error. */
6684 case HLUA_E_ERRMSG:
6685 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006686 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006687 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6688 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006689 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006690
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006691 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006692 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006693 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006694
6695 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006696 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006697 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006698
6699 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02006700 err_yield:
6701 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006702 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6703 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006704 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006705
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006706 case HLUA_E_ERR:
6707 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006708 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006709 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006710
6711 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006712 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006713 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006714
6715 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02006716 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006717 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006718 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);