blob: 6ec69e4cf3ea1554fbd24b37926b426908e52ea3 [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>
Christopher Fauletd25d9262020-08-06 11:04:46 +020029#include <haproxy/auth.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020030#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020031#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020032#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020033#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020034#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020035#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020036#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020037#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020038#include <haproxy/http_fetch.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020040#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020041#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020042#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020043#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020044#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020045#include <haproxy/payload.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020046#include <haproxy/proxy-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020047#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020048#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020049#include <haproxy/server-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020050#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020051#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020052#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020053#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020054#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020055#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020056#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020057#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020058#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020059#include <haproxy/xref.h>
60
Thierry FOURNIER380d0932015-01-23 14:27:52 +010061
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010062/* Lua uses longjmp to perform yield or throwing errors. This
63 * macro is used only for identifying the function that can
64 * not return because a longjmp is executed.
65 * __LJMP marks a prototype of hlua file that can use longjmp.
66 * WILL_LJMP() marks an lua function that will use longjmp.
67 * MAY_LJMP() marks an lua function that may use longjmp.
68 */
69#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020070#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010071#define MAY_LJMP(func) func
72
Thierry FOURNIERbabae282015-09-17 11:36:37 +020073/* This couple of function executes securely some Lua calls outside of
74 * the lua runtime environment. Each Lua call can return a longjmp
75 * if it encounter a memory error.
76 *
77 * Lua documentation extract:
78 *
79 * If an error happens outside any protected environment, Lua calls
80 * a panic function (see lua_atpanic) and then calls abort, thus
81 * exiting the host application. Your panic function can avoid this
82 * exit by never returning (e.g., doing a long jump to your own
83 * recovery point outside Lua).
84 *
85 * The panic function runs as if it were a message handler (see
86 * §2.3); in particular, the error message is at the top of the
87 * stack. However, there is no guarantee about stack space. To push
88 * anything on the stack, the panic function must first check the
89 * available space (see §4.2).
90 *
91 * We must check all the Lua entry point. This includes:
92 * - The include/proto/hlua.h exported functions
93 * - the task wrapper function
94 * - The action wrapper function
95 * - The converters wrapper function
96 * - The sample-fetch wrapper functions
97 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +050098 * It is tolerated that the initialisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080099 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200100 *
101 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
102 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
103 * because they must be exists in the program stack when the longjmp
104 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200105 *
106 * Note that the Lua processing is not really thread safe. It provides
107 * heavy system which consists to add our own lock function in the Lua
108 * code and recompile the library. This system will probably not accepted
109 * by maintainers of various distribs.
110 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500111 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200112 * quick looking on the Lua sources displays a lua_lock() a the start
113 * of function and a lua_unlock() at the end of the function. So I
114 * conclude that the Lua thread safe mode just perform a mutex around
115 * all execution. So I prefer to do this in the HAProxy code, it will be
116 * easier for distro maintainers.
117 *
118 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
119 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
120 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200121 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100122__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200123THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200124static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100125static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200126
127#define SET_SAFE_LJMP(__L) \
128 ({ \
129 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100130 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200131 if (setjmp(safe_ljmp_env) != 0) { \
132 lua_atpanic(__L, hlua_panic_safe); \
133 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100134 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200135 } else { \
136 lua_atpanic(__L, hlua_panic_ljmp); \
137 ret = 1; \
138 } \
139 ret; \
140 })
141
142/* If we are the last function catching Lua errors, we
143 * must reset the panic function.
144 */
145#define RESET_SAFE_LJMP(__L) \
146 do { \
147 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100148 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200149 } while(0)
150
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200151/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200152#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100153/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200154#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200155/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100156#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100157#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200158
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100159/* The main Lua execution context. */
160struct hlua gL;
161
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100162/* This is the memory pool containing struct lua for applets
163 * (including cli).
164 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100165DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100166
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100167/* Used for Socket connection. */
168static struct proxy socket_proxy;
169static struct server socket_tcp;
170#ifdef USE_OPENSSL
171static struct server socket_ssl;
172#endif
173
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100174/* List head of the function called at the initialisation time. */
175struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
176
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100177/* The following variables contains the reference of the different
178 * Lua classes. These references are useful for identify metadata
179 * associated with an object.
180 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100181static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100182static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100183static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100184static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100185static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100186static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200187static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200188static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200189static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100190static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100191
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100192/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200193 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100194 * short timeout. Lua linked with tasks doesn't have a timeout
195 * because a task may remain alive during all the haproxy execution.
196 */
197static unsigned int hlua_timeout_session = 4000; /* session timeout. */
198static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200199static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100200
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100201/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
202 * it is used for preventing infinite loops.
203 *
204 * I test the scheer with an infinite loop containing one incrementation
205 * and one test. I run this loop between 10 seconds, I raise a ceil of
206 * 710M loops from one interrupt each 9000 instructions, so I fix the value
207 * to one interrupt each 10 000 instructions.
208 *
209 * configured | Number of
210 * instructions | loops executed
211 * between two | in milions
212 * forced yields |
213 * ---------------+---------------
214 * 10 | 160
215 * 500 | 670
216 * 1000 | 680
217 * 5000 | 700
218 * 7000 | 700
219 * 8000 | 700
220 * 9000 | 710 <- ceil
221 * 10000 | 710
222 * 100000 | 710
223 * 1000000 | 710
224 *
225 */
226static unsigned int hlua_nb_instruction = 10000;
227
Willy Tarreau32f61e22015-03-18 17:54:59 +0100228/* Descriptor for the memory allocation state. If limit is not null, it will
229 * be enforced on any memory allocation.
230 */
231struct hlua_mem_allocator {
232 size_t allocated;
233 size_t limit;
234};
235
236static struct hlua_mem_allocator hlua_global_allocator;
237
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100238/* These functions converts types between HAProxy internal args or
239 * sample and LUA types. Another function permits to check if the
240 * LUA stack contains arguments according with an required ARG_T
241 * format.
242 */
243static int hlua_arg2lua(lua_State *L, const struct arg *arg);
244static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100245__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100246 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100247static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100248static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100249static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
250
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100251__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200252
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200253#define SEND_ERR(__be, __fmt, __args...) \
254 do { \
255 send_log(__be, LOG_ERR, __fmt, ## __args); \
256 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100257 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200258 } while (0)
259
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100260/* Used to check an Lua function type in the stack. It creates and
261 * returns a reference of the function. This function throws an
262 * error if the rgument is not a "function".
263 */
264__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
265{
266 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100267 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100268 WILL_LJMP(luaL_argerror(L, argno, msg));
269 }
270 lua_pushvalue(L, argno);
271 return luaL_ref(L, LUA_REGISTRYINDEX);
272}
273
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200274/* Return the string that is of the top of the stack. */
275const char *hlua_get_top_error_string(lua_State *L)
276{
277 if (lua_gettop(L) < 1)
278 return "unknown error";
279 if (lua_type(L, -1) != LUA_TSTRING)
280 return "unknown error";
281 return lua_tostring(L, -1);
282}
283
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200284__LJMP static const char *hlua_traceback(lua_State *L)
285{
286 lua_Debug ar;
287 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200288 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200289 int filled = 0;
290
291 while (lua_getstack(L, level++, &ar)) {
292
293 /* Add separator */
294 if (filled)
295 chunk_appendf(msg, ", ");
296 filled = 1;
297
298 /* Fill fields:
299 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
300 * 'l': fills in the field currentline;
301 * 'n': fills in the field name and namewhat;
302 * 't': fills in the field istailcall;
303 */
304 lua_getinfo(L, "Slnt", &ar);
305
306 /* Append code localisation */
307 if (ar.currentline > 0)
308 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
309 else
310 chunk_appendf(msg, "%s ", ar.short_src);
311
312 /*
313 * Get function name
314 *
315 * if namewhat is no empty, name is defined.
316 * what contains "Lua" for Lua function, "C" for C function,
317 * or "main" for main code.
318 */
319 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
320 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
321
322 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
323 chunk_appendf(msg, "main chunk");
324
325 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
326 chunk_appendf(msg, "C function line %d", ar.linedefined);
327
328 else /* nothing left... */
329 chunk_appendf(msg, "?");
330
331
332 /* Display tailed call */
333 if (ar.istailcall)
334 chunk_appendf(msg, " ...");
335 }
336
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200337 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200338}
339
340
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100341/* This function check the number of arguments available in the
342 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500343 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100344 */
345__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
346{
347 if (lua_gettop(L) == nb)
348 return;
349 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
350}
351
Mark Lakes22154b42018-01-29 14:38:40 -0800352/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100353 * and the line number where the error is encountered.
354 */
355static int hlua_pusherror(lua_State *L, const char *fmt, ...)
356{
357 va_list argp;
358 va_start(argp, fmt);
359 luaL_where(L, 1);
360 lua_pushvfstring(L, fmt, argp);
361 va_end(argp);
362 lua_concat(L, 2);
363 return 1;
364}
365
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100366/* This functions is used with sample fetch and converters. It
367 * converts the HAProxy configuration argument in a lua stack
368 * values.
369 *
370 * It takes an array of "arg", and each entry of the array is
371 * converted and pushed in the LUA stack.
372 */
373static int hlua_arg2lua(lua_State *L, const struct arg *arg)
374{
375 switch (arg->type) {
376 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100377 case ARGT_TIME:
378 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100379 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100380 break;
381
382 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200383 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100384 break;
385
386 case ARGT_IPV4:
387 case ARGT_IPV6:
388 case ARGT_MSK4:
389 case ARGT_MSK6:
390 case ARGT_FE:
391 case ARGT_BE:
392 case ARGT_TAB:
393 case ARGT_SRV:
394 case ARGT_USR:
395 case ARGT_MAP:
396 default:
397 lua_pushnil(L);
398 break;
399 }
400 return 1;
401}
402
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500403/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100404 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500405 * with sample fetch wrappers. The input arguments are given to the
406 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100407 */
408static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
409{
410 switch (lua_type(L, ud)) {
411
412 case LUA_TNUMBER:
413 case LUA_TBOOLEAN:
414 arg->type = ARGT_SINT;
415 arg->data.sint = lua_tointeger(L, ud);
416 break;
417
418 case LUA_TSTRING:
419 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200420 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200421 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200422 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200423 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100424 break;
425
426 case LUA_TUSERDATA:
427 case LUA_TNIL:
428 case LUA_TTABLE:
429 case LUA_TFUNCTION:
430 case LUA_TTHREAD:
431 case LUA_TLIGHTUSERDATA:
432 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200433 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100434 break;
435 }
436 return 1;
437}
438
439/* the following functions are used to convert a struct sample
440 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500441 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100442 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100443static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100444{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200445 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200448 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449 break;
450
451 case SMP_T_BIN:
452 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200453 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100454 break;
455
456 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200457 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100458 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
459 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
460 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
461 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
462 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
463 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
464 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
465 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
466 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200467 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100468 break;
469 default:
470 lua_pushnil(L);
471 break;
472 }
473 break;
474
475 case SMP_T_IPV4:
476 case SMP_T_IPV6:
477 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200478 if (sample_casts[smp->data.type][SMP_T_STR] &&
479 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200480 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100481 else
482 lua_pushnil(L);
483 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100484 default:
485 lua_pushnil(L);
486 break;
487 }
488 return 1;
489}
490
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100491/* the following functions are used to convert a struct sample
492 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500493 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100494 */
495static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
496{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200497 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100498
499 case SMP_T_BIN:
500 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100502 break;
503
504 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200505 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100506 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
507 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
508 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
509 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
510 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
511 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
512 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
513 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
514 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200515 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100516 break;
517 default:
518 lua_pushstring(L, "");
519 break;
520 }
521 break;
522
523 case SMP_T_SINT:
524 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100525 case SMP_T_IPV4:
526 case SMP_T_IPV6:
527 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200528 if (sample_casts[smp->data.type][SMP_T_STR] &&
529 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200530 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100531 else
532 lua_pushstring(L, "");
533 break;
534 default:
535 lua_pushstring(L, "");
536 break;
537 }
538 return 1;
539}
540
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100541/* the following functions are used to convert an Lua type in a
542 * struct sample. This is useful to provide data from a converter
543 * to the LUA code.
544 */
545static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
546{
547 switch (lua_type(L, ud)) {
548
549 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200550 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200551 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100552 break;
553
554
555 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200556 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200557 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100558 break;
559
560 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200561 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100562 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200563 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200564 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200565 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200566 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100567 break;
568
569 case LUA_TUSERDATA:
570 case LUA_TNIL:
571 case LUA_TTABLE:
572 case LUA_TFUNCTION:
573 case LUA_TTHREAD:
574 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200575 case LUA_TNONE:
576 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200577 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200578 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100579 break;
580 }
581 return 1;
582}
583
Ilya Shipitsind4259502020-04-08 01:07:56 +0500584/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800585 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100586 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100587 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500588 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100589 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100590 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100591__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100592 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100593{
594 int min_arg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200595 int i, idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100596 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200597 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200598 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200599 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200600 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100601
602 idx = 0;
603 min_arg = ARGM(mask);
604 mask >>= ARGM_BITS;
605
606 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200607 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100608
609 /* Check oversize. */
610 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200611 msg = "Malformed argument mask";
612 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100613 }
614
615 /* Check for mandatory arguments. */
616 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100617 if (idx < min_arg) {
618
619 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200620 if (idx > 0) {
621 msg = "Mandatory argument expected";
622 goto error;
623 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100624
625 /* If first argument have a certain type, some default values
626 * may be used. See the function smp_resolve_args().
627 */
628 switch (mask & ARGT_MASK) {
629
630 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200631 if (!(p->cap & PR_CAP_FE)) {
632 msg = "Mandatory argument expected";
633 goto error;
634 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100635 argp[idx].data.prx = p;
636 argp[idx].type = ARGT_FE;
637 argp[idx+1].type = ARGT_STOP;
638 break;
639
640 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200641 if (!(p->cap & PR_CAP_BE)) {
642 msg = "Mandatory argument expected";
643 goto error;
644 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100645 argp[idx].data.prx = p;
646 argp[idx].type = ARGT_BE;
647 argp[idx+1].type = ARGT_STOP;
648 break;
649
650 case ARGT_TAB:
651 argp[idx].data.prx = p;
652 argp[idx].type = ARGT_TAB;
653 argp[idx+1].type = ARGT_STOP;
654 break;
655
656 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200657 msg = "Mandatory argument expected";
658 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100659 break;
660 }
661 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200662 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100663 }
664
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500665 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100666 if ((mask & ARGT_MASK) == ARGT_STOP &&
667 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200668 msg = "Last argument expected";
669 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100670 }
671
672 if ((mask & ARGT_MASK) == ARGT_STOP &&
673 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200674 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100675 }
676
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200677 /* Convert some argument types. All string in argp[] are for not
678 * duplicated yet.
679 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100680 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100681 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200682 if (argp[idx].type != ARGT_SINT) {
683 msg = "integer expected";
684 goto error;
685 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100686 argp[idx].type = ARGT_SINT;
687 break;
688
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100689 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200690 if (argp[idx].type != ARGT_SINT) {
691 msg = "integer expected";
692 goto error;
693 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200694 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100695 break;
696
697 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200698 if (argp[idx].type != ARGT_SINT) {
699 msg = "integer expected";
700 goto error;
701 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200702 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100703 break;
704
705 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200706 if (argp[idx].type != ARGT_STR) {
707 msg = "string expected";
708 goto error;
709 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200710 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200711 if (!argp[idx].data.prx) {
712 msg = "frontend doesn't exist";
713 goto error;
714 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100715 argp[idx].type = ARGT_FE;
716 break;
717
718 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200719 if (argp[idx].type != ARGT_STR) {
720 msg = "string expected";
721 goto error;
722 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200723 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200724 if (!argp[idx].data.prx) {
725 msg = "backend doesn't exist";
726 goto error;
727 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100728 argp[idx].type = ARGT_BE;
729 break;
730
731 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200732 if (argp[idx].type != ARGT_STR) {
733 msg = "string expected";
734 goto error;
735 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200736 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200737 if (!argp[idx].data.t) {
738 msg = "table doesn't exist";
739 goto error;
740 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100741 argp[idx].type = ARGT_TAB;
742 break;
743
744 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200745 if (argp[idx].type != ARGT_STR) {
746 msg = "string expected";
747 goto error;
748 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200749 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100750 if (sname) {
751 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200752 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200753 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200754 if (!px) {
755 msg = "backend doesn't exist";
756 goto error;
757 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100758 }
759 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200760 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100761 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100762 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100763 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200764 if (!argp[idx].data.srv) {
765 msg = "server doesn't exist";
766 goto error;
767 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100768 argp[idx].type = ARGT_SRV;
769 break;
770
771 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200772 if (argp[idx].type != ARGT_STR) {
773 msg = "string expected";
774 goto error;
775 }
776 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
777 msg = "invalid IPv4 address";
778 goto error;
779 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100780 argp[idx].type = ARGT_IPV4;
781 break;
782
783 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200784 if (argp[idx].type == ARGT_SINT)
785 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
786 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200787 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
788 msg = "invalid IPv4 mask";
789 goto error;
790 }
791 }
792 else {
793 msg = "integer or string expected";
794 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200795 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100796 argp[idx].type = ARGT_MSK4;
797 break;
798
799 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200800 if (argp[idx].type != ARGT_STR) {
801 msg = "string expected";
802 goto error;
803 }
804 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
805 msg = "invalid IPv6 address";
806 goto error;
807 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100808 argp[idx].type = ARGT_IPV6;
809 break;
810
811 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200812 if (argp[idx].type == ARGT_SINT)
813 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
814 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200815 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
816 msg = "invalid IPv6 mask";
817 goto error;
818 }
819 }
820 else {
821 msg = "integer or string expected";
822 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200823 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100824 argp[idx].type = ARGT_MSK6;
825 break;
826
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200827 case ARGT_REG:
828 if (argp[idx].type != ARGT_STR) {
829 msg = "string expected";
830 goto error;
831 }
832 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
833 if (!reg) {
834 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
835 argp[idx].data.str.area, err);
836 free(err);
837 goto error;
838 }
839 argp[idx].type = ARGT_REG;
840 argp[idx].data.reg = reg;
841 break;
842
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100843 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200844 if (argp[idx].type != ARGT_STR) {
845 msg = "string expected";
846 goto error;
847 }
848 if (p->uri_auth && p->uri_auth->userlist &&
849 !strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area))
850 ul = p->uri_auth->userlist;
851 else
852 ul = auth_find_userlist(argp[idx].data.str.area);
853
854 if (!ul) {
855 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
856 goto error;
857 }
858 argp[idx].type = ARGT_USR;
859 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200860 break;
861
862 case ARGT_STR:
863 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
864 msg = "unable to duplicate string arg";
865 goto error;
866 }
867 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100868 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200869
Christopher Fauletd25d9262020-08-06 11:04:46 +0200870 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200871 msg = "type not yet supported";
872 goto error;
873 break;
874
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100875 }
876
877 /* Check for type of argument. */
878 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200879 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
880 arg_type_names[(mask & ARGT_MASK)],
881 arg_type_names[argp[idx].type & ARGT_MASK]);
882 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100883 }
884
885 /* Next argument. */
886 mask >>= ARGT_BITS;
887 idx++;
888 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200889 return 0;
890
891 error:
892 for (i = 0; i < idx; i++) {
893 if (argp[i].type == ARGT_STR)
894 chunk_destroy(&argp[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200895 else if (argp[i].type == ARGT_REG)
896 regex_free(argp[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200897 }
898 WILL_LJMP(luaL_argerror(L, first + idx, msg));
899 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100900}
901
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100902/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500903 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100904 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100905 *
906 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907 * - hlua_sethlua : create the association between hlua context and lua_state.
908 */
909static inline struct hlua *hlua_gethlua(lua_State *L)
910{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100911 struct hlua **hlua = lua_getextraspace(L);
912 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100913}
914static inline void hlua_sethlua(struct hlua *hlua)
915{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100916 struct hlua **hlua_store = lua_getextraspace(hlua->T);
917 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100918}
919
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100920/* This function is used to send logs. It try to send on screen (stderr)
921 * and on the default syslog server.
922 */
923static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
924{
925 struct tm tm;
926 char *p;
927
928 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200929 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100930 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200931 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200932 /* Break the message if exceed the buffer size. */
933 *(p-4) = ' ';
934 *(p-3) = '.';
935 *(p-2) = '.';
936 *(p-1) = '.';
937 break;
938 }
Willy Tarreau90807112020-02-25 08:16:33 +0100939 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100940 *p = *msg;
941 else
942 *p = '.';
943 }
944 *p = '\0';
945
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200946 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100947 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200948 get_localtime(date.tv_sec, &tm);
949 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100950 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200951 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100952 fflush(stderr);
953 }
954}
955
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100956/* This function just ensure that the yield will be always
957 * returned with a timeout and permit to set some flags
958 */
959__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100960 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100961{
962 struct hlua *hlua = hlua_gethlua(L);
963
964 /* Set the wake timeout. If timeout is required, we set
965 * the expiration time.
966 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200967 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100968
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100969 hlua->flags |= flags;
970
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100971 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200972 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100973}
974
Willy Tarreau87b09662015-04-03 00:22:06 +0200975/* This function initialises the Lua environment stored in the stream.
976 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100977 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200978 *
979 * This function is particular. it initialises a new Lua thread. If the
980 * initialisation fails (example: out of memory error), the lua function
981 * throws an error (longjmp).
982 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100983 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500984 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100985 * threads appear, the safe environment set a lock to ensure only one
986 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500987 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100988 *
989 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500990 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100991 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800992 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200993 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800994 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500995 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100996 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100997int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100998{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100999 if (!already_safe) {
1000 if (!SET_SAFE_LJMP(gL.T)) {
1001 lua->Tref = LUA_REFNIL;
1002 return 0;
1003 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001004 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001005 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001006 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001007 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001008 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001009 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001010 lua->T = lua_newthread(gL.T);
1011 if (!lua->T) {
1012 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001013 if (!already_safe)
1014 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001015 return 0;
1016 }
1017 hlua_sethlua(lua);
1018 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1019 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001020 if (!already_safe)
1021 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001022 return 1;
1023}
1024
Willy Tarreau87b09662015-04-03 00:22:06 +02001025/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001026 * is destroyed. The destroy also the memory context. The struct "lua"
1027 * is not freed.
1028 */
1029void hlua_ctx_destroy(struct hlua *lua)
1030{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001031 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001032 return;
1033
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001034 if (!lua->T)
1035 goto end;
1036
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001037 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001038 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001039
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001040 if (!SET_SAFE_LJMP(lua->T))
1041 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001042 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001043 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001044
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001045 if (!SET_SAFE_LJMP(gL.T))
1046 return;
1047 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1048 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001049 /* Forces a garbage collecting process. If the Lua program is finished
1050 * without error, we run the GC on the thread pointer. Its freed all
1051 * the unused memory.
1052 * If the thread is finnish with an error or is currently yielded,
1053 * it seems that the GC applied on the thread doesn't clean anything,
1054 * so e run the GC on the main thread.
1055 * NOTE: maybe this action locks all the Lua threads untiml the en of
1056 * the garbage collection.
1057 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001058 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +02001059 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001060 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +02001061 lua_gc(gL.T, LUA_GCCOLLECT, 0);
1062 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001063 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001064
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001065 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001066
1067end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001068 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001069}
1070
1071/* This function is used to restore the Lua context when a coroutine
1072 * fails. This function copy the common memory between old coroutine
1073 * and the new coroutine. The old coroutine is destroyed, and its
1074 * replaced by the new coroutine.
1075 * If the flag "keep_msg" is set, the last entry of the old is assumed
1076 * as string error message and it is copied in the new stack.
1077 */
1078static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1079{
1080 lua_State *T;
1081 int new_ref;
1082
1083 /* Renew the main LUA stack doesn't have sense. */
1084 if (lua == &gL)
1085 return 0;
1086
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001087 /* New Lua coroutine. */
1088 T = lua_newthread(gL.T);
1089 if (!T)
1090 return 0;
1091
1092 /* Copy last error message. */
1093 if (keep_msg)
1094 lua_xmove(lua->T, T, 1);
1095
1096 /* Copy data between the coroutines. */
1097 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1098 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001099 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001100
1101 /* Destroy old data. */
1102 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1103
1104 /* The thread is garbage collected by Lua. */
1105 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1106
1107 /* Fill the struct with the new coroutine values. */
1108 lua->Mref = new_ref;
1109 lua->T = T;
1110 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1111
1112 /* Set context. */
1113 hlua_sethlua(lua);
1114
1115 return 1;
1116}
1117
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001118void hlua_hook(lua_State *L, lua_Debug *ar)
1119{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001120 struct hlua *hlua = hlua_gethlua(L);
1121
1122 /* Lua cannot yield when its returning from a function,
1123 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001124 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001125 */
1126 if (lua_gethookmask(L) & LUA_MASKRET) {
1127 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1128 return;
1129 }
1130
1131 /* restore the interrupt condition. */
1132 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1133
1134 /* If we interrupt the Lua processing in yieldable state, we yield.
1135 * If the state is not yieldable, trying yield causes an error.
1136 */
1137 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001138 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001139
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001140 /* If we cannot yield, update the clock and check the timeout. */
1141 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001142 hlua->run_time += now_ms - hlua->start_time;
1143 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001144 lua_pushfstring(L, "execution timeout");
1145 WILL_LJMP(lua_error(L));
1146 }
1147
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001148 /* Update the start time. */
1149 hlua->start_time = now_ms;
1150
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001151 /* Try to interrupt the process at the end of the current
1152 * unyieldable function.
1153 */
1154 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001155}
1156
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001157/* This function start or resumes the Lua stack execution. If the flag
1158 * "yield_allowed" if no set and the LUA stack execution returns a yield
1159 * The function return an error.
1160 *
1161 * The function can returns 4 values:
1162 * - HLUA_E_OK : The execution is terminated without any errors.
1163 * - HLUA_E_AGAIN : The execution must continue at the next associated
1164 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001165 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001166 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001167 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001168 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001169 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001170 * LUA code.
1171 */
1172static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1173{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001174#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1175 int nres;
1176#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001177 int ret;
1178 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001179 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001180
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001181 /* Initialise run time counter. */
1182 if (!HLUA_IS_RUNNING(lua))
1183 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001184
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001185 /* Lock the whole Lua execution. This lock must be before the
1186 * label "resume_execution".
1187 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001188 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001189
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001190resume_execution:
1191
1192 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1193 * instructions. it is used for preventing infinite loops.
1194 */
1195 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1196
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001197 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001198 HLUA_SET_RUN(lua);
1199 HLUA_CLR_CTRLYIELD(lua);
1200 HLUA_CLR_WAKERESWR(lua);
1201 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001202
Christopher Fauletbc275a92020-02-26 14:55:16 +01001203 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001204 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001205 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001206
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001207 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001208#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1209 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1210#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001211 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001212#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001213 switch (ret) {
1214
1215 case LUA_OK:
1216 ret = HLUA_E_OK;
1217 break;
1218
1219 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001220 /* Check if the execution timeout is expired. It it is the case, we
1221 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001222 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001223 tv_update_date(0, 1);
1224 lua->run_time += now_ms - lua->start_time;
1225 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001226 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001227 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001228 break;
1229 }
1230 /* Process the forced yield. if the general yield is not allowed or
1231 * if no task were associated this the current Lua execution
1232 * coroutine, we resume the execution. Else we want to return in the
1233 * scheduler and we want to be waked up again, to continue the
1234 * current Lua execution. So we schedule our own task.
1235 */
1236 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001237 if (!yield_allowed || !lua->task)
1238 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001239 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001240 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001241 if (!yield_allowed) {
1242 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001243 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001244 break;
1245 }
1246 ret = HLUA_E_AGAIN;
1247 break;
1248
1249 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001250
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001251 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001252 * because the errors ares the only one mean to return immediately
1253 * from and lua execution.
1254 */
1255 if (lua->flags & HLUA_EXIT) {
1256 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001257 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001258 break;
1259 }
1260
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001261 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001262 if (!lua_checkstack(lua->T, 1)) {
1263 ret = HLUA_E_ERR;
1264 break;
1265 }
1266 msg = lua_tostring(lua->T, -1);
1267 lua_settop(lua->T, 0); /* Empty the stack. */
1268 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001269 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001270 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001271 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001272 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001273 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001274 ret = HLUA_E_ERRMSG;
1275 break;
1276
1277 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001278 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001279 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001280 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001281 break;
1282
1283 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001284 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001285 if (!lua_checkstack(lua->T, 1)) {
1286 ret = HLUA_E_ERR;
1287 break;
1288 }
1289 msg = lua_tostring(lua->T, -1);
1290 lua_settop(lua->T, 0); /* Empty the stack. */
1291 lua_pop(lua->T, 1);
1292 if (msg)
1293 lua_pushfstring(lua->T, "message handler error: %s", msg);
1294 else
1295 lua_pushfstring(lua->T, "message handler error");
1296 ret = HLUA_E_ERRMSG;
1297 break;
1298
1299 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001300 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001301 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001302 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001303 break;
1304 }
1305
1306 switch (ret) {
1307 case HLUA_E_AGAIN:
1308 break;
1309
1310 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001311 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001312 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001313 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001314 break;
1315
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001316 case HLUA_E_ETMOUT:
1317 case HLUA_E_NOMEM:
1318 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001319 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001320 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001321 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001322 hlua_ctx_renew(lua, 0);
1323 break;
1324
1325 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001326 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001327 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001328 break;
1329 }
1330
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001331 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001332 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001333
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001334 return ret;
1335}
1336
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001337/* This function exit the current code. */
1338__LJMP static int hlua_done(lua_State *L)
1339{
1340 struct hlua *hlua = hlua_gethlua(L);
1341
1342 hlua->flags |= HLUA_EXIT;
1343 WILL_LJMP(lua_error(L));
1344
1345 return 0;
1346}
1347
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001348/* This function is an LUA binding. It provides a function
1349 * for deleting ACL from a referenced ACL file.
1350 */
1351__LJMP static int hlua_del_acl(lua_State *L)
1352{
1353 const char *name;
1354 const char *key;
1355 struct pat_ref *ref;
1356
1357 MAY_LJMP(check_args(L, 2, "del_acl"));
1358
1359 name = MAY_LJMP(luaL_checkstring(L, 1));
1360 key = MAY_LJMP(luaL_checkstring(L, 2));
1361
1362 ref = pat_ref_lookup(name);
1363 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001364 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001365
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001366 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001367 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001368 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001369 return 0;
1370}
1371
1372/* This function is an LUA binding. It provides a function
1373 * for deleting map entry from a referenced map file.
1374 */
1375static int hlua_del_map(lua_State *L)
1376{
1377 const char *name;
1378 const char *key;
1379 struct pat_ref *ref;
1380
1381 MAY_LJMP(check_args(L, 2, "del_map"));
1382
1383 name = MAY_LJMP(luaL_checkstring(L, 1));
1384 key = MAY_LJMP(luaL_checkstring(L, 2));
1385
1386 ref = pat_ref_lookup(name);
1387 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001388 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001389
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001390 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001391 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001392 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001393 return 0;
1394}
1395
1396/* This function is an LUA binding. It provides a function
1397 * for adding ACL pattern from a referenced ACL file.
1398 */
1399static int hlua_add_acl(lua_State *L)
1400{
1401 const char *name;
1402 const char *key;
1403 struct pat_ref *ref;
1404
1405 MAY_LJMP(check_args(L, 2, "add_acl"));
1406
1407 name = MAY_LJMP(luaL_checkstring(L, 1));
1408 key = MAY_LJMP(luaL_checkstring(L, 2));
1409
1410 ref = pat_ref_lookup(name);
1411 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001412 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001413
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001414 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001415 if (pat_ref_find_elt(ref, key) == NULL)
1416 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001417 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001418 return 0;
1419}
1420
1421/* This function is an LUA binding. It provides a function
1422 * for setting map pattern and sample from a referenced map
1423 * file.
1424 */
1425static int hlua_set_map(lua_State *L)
1426{
1427 const char *name;
1428 const char *key;
1429 const char *value;
1430 struct pat_ref *ref;
1431
1432 MAY_LJMP(check_args(L, 3, "set_map"));
1433
1434 name = MAY_LJMP(luaL_checkstring(L, 1));
1435 key = MAY_LJMP(luaL_checkstring(L, 2));
1436 value = MAY_LJMP(luaL_checkstring(L, 3));
1437
1438 ref = pat_ref_lookup(name);
1439 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001440 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001441
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001442 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001443 if (pat_ref_find_elt(ref, key) != NULL)
1444 pat_ref_set(ref, key, value, NULL);
1445 else
1446 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001447 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001448 return 0;
1449}
1450
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001451/* A class is a lot of memory that contain data. This data can be a table,
1452 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001453 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001454 * the name of the object (_G[<name>] = <metable> ).
1455 *
1456 * A metable is a table that modify the standard behavior of a standard
1457 * access to the associated data. The entries of this new metatable are
1458 * defined as is:
1459 *
1460 * http://lua-users.org/wiki/MetatableEvents
1461 *
1462 * __index
1463 *
1464 * we access an absent field in a table, the result is nil. This is
1465 * true, but it is not the whole truth. Actually, such access triggers
1466 * the interpreter to look for an __index metamethod: If there is no
1467 * such method, as usually happens, then the access results in nil;
1468 * otherwise, the metamethod will provide the result.
1469 *
1470 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1471 * the key does not appear in the table, but the metatable has an __index
1472 * property:
1473 *
1474 * - if the value is a function, the function is called, passing in the
1475 * table and the key; the return value of that function is returned as
1476 * the result.
1477 *
1478 * - if the value is another table, the value of the key in that table is
1479 * asked for and returned (and if it doesn't exist in that table, but that
1480 * table's metatable has an __index property, then it continues on up)
1481 *
1482 * - Use "rawget(myTable,key)" to skip this metamethod.
1483 *
1484 * http://www.lua.org/pil/13.4.1.html
1485 *
1486 * __newindex
1487 *
1488 * Like __index, but control property assignment.
1489 *
1490 * __mode - Control weak references. A string value with one or both
1491 * of the characters 'k' and 'v' which specifies that the the
1492 * keys and/or values in the table are weak references.
1493 *
1494 * __call - Treat a table like a function. When a table is followed by
1495 * parenthesis such as "myTable( 'foo' )" and the metatable has
1496 * a __call key pointing to a function, that function is invoked
1497 * (passing any specified arguments) and the return value is
1498 * returned.
1499 *
1500 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1501 * called, if the metatable for myTable has a __metatable
1502 * key, the value of that key is returned instead of the
1503 * actual metatable.
1504 *
1505 * __tostring - Control string representation. When the builtin
1506 * "tostring( myTable )" function is called, if the metatable
1507 * for myTable has a __tostring property set to a function,
1508 * that function is invoked (passing myTable to it) and the
1509 * return value is used as the string representation.
1510 *
1511 * __len - Control table length. When the table length is requested using
1512 * the length operator ( '#' ), if the metatable for myTable has
1513 * a __len key pointing to a function, that function is invoked
1514 * (passing myTable to it) and the return value used as the value
1515 * of "#myTable".
1516 *
1517 * __gc - Userdata finalizer code. When userdata is set to be garbage
1518 * collected, if the metatable has a __gc field pointing to a
1519 * function, that function is first invoked, passing the userdata
1520 * to it. The __gc metamethod is not called for tables.
1521 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1522 *
1523 * Special metamethods for redefining standard operators:
1524 * http://www.lua.org/pil/13.1.html
1525 *
1526 * __add "+"
1527 * __sub "-"
1528 * __mul "*"
1529 * __div "/"
1530 * __unm "!"
1531 * __pow "^"
1532 * __concat ".."
1533 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001534 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001535 * http://www.lua.org/pil/13.2.html
1536 *
1537 * __eq "=="
1538 * __lt "<"
1539 * __le "<="
1540 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001541
1542/*
1543 *
1544 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001545 * Class Map
1546 *
1547 *
1548 */
1549
1550/* Returns a struct hlua_map if the stack entry "ud" is
1551 * a class session, otherwise it throws an error.
1552 */
1553__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1554{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001555 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001556}
1557
1558/* This function is the map constructor. It don't need
1559 * the class Map object. It creates and return a new Map
1560 * object. It must be called only during "body" or "init"
1561 * context because it process some filesystem accesses.
1562 */
1563__LJMP static int hlua_map_new(struct lua_State *L)
1564{
1565 const char *fn;
1566 int match = PAT_MATCH_STR;
1567 struct sample_conv conv;
1568 const char *file = "";
1569 int line = 0;
1570 lua_Debug ar;
1571 char *err = NULL;
1572 struct arg args[2];
1573
1574 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1575 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1576
1577 fn = MAY_LJMP(luaL_checkstring(L, 1));
1578
1579 if (lua_gettop(L) >= 2) {
1580 match = MAY_LJMP(luaL_checkinteger(L, 2));
1581 if (match < 0 || match >= PAT_MATCH_NUM)
1582 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1583 }
1584
1585 /* Get Lua filename and line number. */
1586 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1587 lua_getinfo(L, "Sl", &ar); /* get info about it */
1588 if (ar.currentline > 0) { /* is there info? */
1589 file = ar.short_src;
1590 line = ar.currentline;
1591 }
1592 }
1593
1594 /* fill fake sample_conv struct. */
1595 conv.kw = ""; /* unused. */
1596 conv.process = NULL; /* unused. */
1597 conv.arg_mask = 0; /* unused. */
1598 conv.val_args = NULL; /* unused. */
1599 conv.out_type = SMP_T_STR;
1600 conv.private = (void *)(long)match;
1601 switch (match) {
1602 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1603 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1604 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1605 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1606 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1607 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1608 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001609 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001610 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1611 default:
1612 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1613 }
1614
1615 /* fill fake args. */
1616 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001617 args[0].data.str.area = strdup(fn);
1618 args[0].data.str.data = strlen(fn);
1619 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001620 args[1].type = ARGT_STOP;
1621
1622 /* load the map. */
1623 if (!sample_load_map(args, &conv, file, line, &err)) {
1624 /* error case: we cant use luaL_error because we must
1625 * free the err variable.
1626 */
1627 luaL_where(L, 1);
1628 lua_pushfstring(L, "'new': %s.", err);
1629 lua_concat(L, 2);
1630 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001631 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001632 WILL_LJMP(lua_error(L));
1633 }
1634
1635 /* create the lua object. */
1636 lua_newtable(L);
1637 lua_pushlightuserdata(L, args[0].data.map);
1638 lua_rawseti(L, -2, 0);
1639
1640 /* Pop a class Map metatable and affect it to the userdata. */
1641 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1642 lua_setmetatable(L, -2);
1643
1644
1645 return 1;
1646}
1647
1648__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1649{
1650 struct map_descriptor *desc;
1651 struct pattern *pat;
1652 struct sample smp;
1653
1654 MAY_LJMP(check_args(L, 2, "lookup"));
1655 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001656 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001657 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001658 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001659 }
1660 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001661 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001662 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001663 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 +02001664 }
1665
1666 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001667 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001668 if (str)
1669 lua_pushstring(L, "");
1670 else
1671 lua_pushnil(L);
1672 return 1;
1673 }
1674
1675 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001676 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001677 return 1;
1678}
1679
1680__LJMP static int hlua_map_lookup(struct lua_State *L)
1681{
1682 return _hlua_map_lookup(L, 0);
1683}
1684
1685__LJMP static int hlua_map_slookup(struct lua_State *L)
1686{
1687 return _hlua_map_lookup(L, 1);
1688}
1689
1690/*
1691 *
1692 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001693 * Class Socket
1694 *
1695 *
1696 */
1697
1698__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1699{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001700 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701}
1702
1703/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001704 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705 * received.
1706 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001707static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001708{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001709 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001710
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001711 if (appctx->ctx.hlua_cosocket.die) {
1712 si_shutw(si);
1713 si_shutr(si);
1714 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001715 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1716 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001717 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1718 }
1719
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001720 /* If we cant write, wakeup the pending write signals. */
1721 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001722 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001723
1724 /* If we cant read, wakeup the pending read signals. */
1725 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001726 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001727
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001728 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001729 * to be notified whenever the connection completes.
1730 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001731 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001732 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001733 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001734 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001735 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001736 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001737
1738 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001739 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001740
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001741 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001742 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001743 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001744
1745 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001746 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001747 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001748
1749 /* Some data were injected in the buffer, notify the stream
1750 * interface.
1751 */
1752 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001753 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001754
1755 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001756 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001757 */
1758 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001759 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001760}
1761
Willy Tarreau87b09662015-04-03 00:22:06 +02001762/* This function is called when the "struct stream" is destroyed.
1763 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001764 * Wake all the pending signals.
1765 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001766static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001767{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001768 struct xref *peer;
1769
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001770 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001771 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1772 if (peer)
1773 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001774
1775 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001776 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1777 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001778}
1779
1780/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001781 * uses this object. If the stream does not exists, just quit.
1782 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001783 * pending signal can rest in the read and write lists. destroy
1784 * it.
1785 */
1786__LJMP static int hlua_socket_gc(lua_State *L)
1787{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001788 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001790 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001791
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001792 MAY_LJMP(check_args(L, 1, "__gc"));
1793
1794 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001795 peer = xref_get_peer_and_lock(&socket->xref);
1796 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001797 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001798 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001799
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001800 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001801 appctx->ctx.hlua_cosocket.die = 1;
1802 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001803
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001804 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001805 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001806 return 0;
1807}
1808
1809/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001810 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001811 */
sada05ed3302018-05-11 11:48:18 -07001812__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001813{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001814 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001815 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001816 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001817 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001819 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001820
1821 /* Check if we run on the same thread than the xreator thread.
1822 * We cannot access to the socket if the thread is different.
1823 */
1824 if (socket->tid != tid)
1825 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1826
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001827 peer = xref_get_peer_and_lock(&socket->xref);
1828 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001829 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001830
1831 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001832 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001833
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001834 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001835 appctx->ctx.hlua_cosocket.die = 1;
1836 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001837
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001838 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001839 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001840 return 0;
1841}
1842
sada05ed3302018-05-11 11:48:18 -07001843/* The close function calls close_helper.
1844 */
1845__LJMP static int hlua_socket_close(lua_State *L)
1846{
1847 MAY_LJMP(check_args(L, 1, "close"));
1848 return hlua_socket_close_helper(L);
1849}
1850
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001851/* This Lua function assumes that the stack contain three parameters.
1852 * 1 - USERDATA containing a struct socket
1853 * 2 - INTEGER with values of the macro defined below
1854 * If the integer is -1, we must read at most one line.
1855 * If the integer is -2, we ust read all the data until the
1856 * end of the stream.
1857 * If the integer is positive value, we must read a number of
1858 * bytes corresponding to this value.
1859 */
1860#define HLSR_READ_LINE (-1)
1861#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001862__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001863{
1864 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1865 int wanted = lua_tointeger(L, 2);
1866 struct hlua *hlua = hlua_gethlua(L);
1867 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001868 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001869 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001870 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001871 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001872 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001873 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001874 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001875 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001876 struct stream_interface *si;
1877 struct stream *s;
1878 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001879 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001880
1881 /* Check if this lua stack is schedulable. */
1882 if (!hlua || !hlua->task)
1883 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1884 "'frontend', 'backend' or 'task'"));
1885
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001886 /* Check if we run on the same thread than the xreator thread.
1887 * We cannot access to the socket if the thread is different.
1888 */
1889 if (socket->tid != tid)
1890 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1891
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001892 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001893 peer = xref_get_peer_and_lock(&socket->xref);
1894 if (!peer)
1895 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001896 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1897 si = appctx->owner;
1898 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001899
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001900 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001901 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001902 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001903 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904 if (nblk < 0) /* Connection close. */
1905 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001906 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001907 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001908
1909 /* remove final \r\n. */
1910 if (nblk == 1) {
1911 if (blk1[len1-1] == '\n') {
1912 len1--;
1913 skip_at_end++;
1914 if (blk1[len1-1] == '\r') {
1915 len1--;
1916 skip_at_end++;
1917 }
1918 }
1919 }
1920 else {
1921 if (blk2[len2-1] == '\n') {
1922 len2--;
1923 skip_at_end++;
1924 if (blk2[len2-1] == '\r') {
1925 len2--;
1926 skip_at_end++;
1927 }
1928 }
1929 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001930 }
1931
1932 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001933 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001934 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001935 if (nblk < 0) /* Connection close. */
1936 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001937 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001938 goto connection_empty;
1939 }
1940
1941 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001942 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001943 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001944 if (nblk < 0) /* Connection close. */
1945 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001946 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001947 goto connection_empty;
1948
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001949 missing_bytes = wanted - socket->b.n;
1950 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001951 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001952 len1 = missing_bytes;
1953 } if (nblk == 2 && len1 + len2 > missing_bytes)
1954 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001955 }
1956
1957 len = len1;
1958
1959 luaL_addlstring(&socket->b, blk1, len1);
1960 if (nblk == 2) {
1961 len += len2;
1962 luaL_addlstring(&socket->b, blk2, len2);
1963 }
1964
1965 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001966 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001967
1968 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001969 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001970
1971 /* If the pattern reclaim to read all the data
1972 * in the connection, got out.
1973 */
1974 if (wanted == HLSR_READ_ALL)
1975 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001976 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001977 goto connection_empty;
1978
1979 /* Return result. */
1980 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001981 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001982 return 1;
1983
1984connection_closed:
1985
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001986 xref_unlock(&socket->xref, peer);
1987
1988no_peer:
1989
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001990 /* If the buffer containds data. */
1991 if (socket->b.n > 0) {
1992 luaL_pushresult(&socket->b);
1993 return 1;
1994 }
1995 lua_pushnil(L);
1996 lua_pushstring(L, "connection closed.");
1997 return 2;
1998
1999connection_empty:
2000
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002001 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2002 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002003 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002004 }
2005 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002006 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002007 return 0;
2008}
2009
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002010/* This Lua function gets two parameters. The first one can be string
2011 * or a number. If the string is "*l", the user requires one line. If
2012 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002013 * If the value is a number, the user require a number of bytes equal
2014 * to the value. The default value is "*l" (a line).
2015 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002016 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002017 * integer takes this values:
2018 * -1 : read a line
2019 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002020 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002021 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002022 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002023 * concatenated with the read data.
2024 */
2025__LJMP static int hlua_socket_receive(struct lua_State *L)
2026{
2027 int wanted = HLSR_READ_LINE;
2028 const char *pattern;
2029 int type;
2030 char *error;
2031 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002032 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002033
2034 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2035 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2036
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002037 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002038
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002039 /* Check if we run on the same thread than the xreator thread.
2040 * We cannot access to the socket if the thread is different.
2041 */
2042 if (socket->tid != tid)
2043 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2044
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002045 /* check for pattern. */
2046 if (lua_gettop(L) >= 2) {
2047 type = lua_type(L, 2);
2048 if (type == LUA_TSTRING) {
2049 pattern = lua_tostring(L, 2);
2050 if (strcmp(pattern, "*a") == 0)
2051 wanted = HLSR_READ_ALL;
2052 else if (strcmp(pattern, "*l") == 0)
2053 wanted = HLSR_READ_LINE;
2054 else {
2055 wanted = strtoll(pattern, &error, 10);
2056 if (*error != '\0')
2057 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2058 }
2059 }
2060 else if (type == LUA_TNUMBER) {
2061 wanted = lua_tointeger(L, 2);
2062 if (wanted < 0)
2063 WILL_LJMP(luaL_error(L, "Unsupported size."));
2064 }
2065 }
2066
2067 /* Set pattern. */
2068 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002069
2070 /* Check if we would replace the top by itself. */
2071 if (lua_gettop(L) != 2)
2072 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002073
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002074 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002075 luaL_buffinit(L, &socket->b);
2076
2077 /* Check prefix. */
2078 if (lua_gettop(L) >= 3) {
2079 if (lua_type(L, 3) != LUA_TSTRING)
2080 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2081 pattern = lua_tolstring(L, 3, &len);
2082 luaL_addlstring(&socket->b, pattern, len);
2083 }
2084
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002085 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002086}
2087
2088/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002089 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002090 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002091static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002092{
2093 struct hlua_socket *socket;
2094 struct hlua *hlua = hlua_gethlua(L);
2095 struct appctx *appctx;
2096 size_t buf_len;
2097 const char *buf;
2098 int len;
2099 int send_len;
2100 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002101 struct xref *peer;
2102 struct stream_interface *si;
2103 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002104
2105 /* Check if this lua stack is schedulable. */
2106 if (!hlua || !hlua->task)
2107 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2108 "'frontend', 'backend' or 'task'"));
2109
2110 /* Get object */
2111 socket = MAY_LJMP(hlua_checksocket(L, 1));
2112 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002113 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002114
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002115 /* Check if we run on the same thread than the xreator thread.
2116 * We cannot access to the socket if the thread is different.
2117 */
2118 if (socket->tid != tid)
2119 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2120
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002121 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002122 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002123 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002124 lua_pushinteger(L, -1);
2125 return 1;
2126 }
2127 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2128 si = appctx->owner;
2129 s = si_strm(si);
2130
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002131 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002132 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002133 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002134 lua_pushinteger(L, -1);
2135 return 1;
2136 }
2137
2138 /* Update the input buffer data. */
2139 buf += sent;
2140 send_len = buf_len - sent;
2141
2142 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002143 if (sent >= buf_len) {
2144 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002145 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002146 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002147
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002148 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002149 * the request buffer if its not required.
2150 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002151 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002152 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002153 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002154 }
2155
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002156 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002157 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002158 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002159 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002160 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002161
2162 /* send data */
2163 if (len < send_len)
2164 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002165 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002166
2167 /* "Not enough space" (-1), "Buffer too little to contain
2168 * the data" (-2) are not expected because the available length
2169 * is tested.
2170 * Other unknown error are also not expected.
2171 */
2172 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002173 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002174 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002175
sada05ed3302018-05-11 11:48:18 -07002176 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002177 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002178 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002179 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002180 return 1;
2181 }
2182
2183 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002184 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002185
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002186 s->req.rex = TICK_ETERNITY;
2187 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002188
2189 /* Update length sent. */
2190 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002191 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002192
2193 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002194 if (sent + len >= buf_len) {
2195 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002196 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002197 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002198
2199hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002200 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2201 xref_unlock(&socket->xref, peer);
2202 WILL_LJMP(luaL_error(L, "out of memory"));
2203 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002204 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002205 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002206 return 0;
2207}
2208
2209/* This function initiate the send of data. It just check the input
2210 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002211 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002212 * "hlua_socket_write_yield" that can yield.
2213 *
2214 * The Lua function gets between 3 and 4 parameters. The first one is
2215 * the associated object. The second is a string buffer. The third is
2216 * a facultative integer that represents where is the buffer position
2217 * of the start of the data that can send. The first byte is the
2218 * position "1". The default value is "1". The fourth argument is a
2219 * facultative integer that represents where is the buffer position
2220 * of the end of the data that can send. The default is the last byte.
2221 */
2222static int hlua_socket_send(struct lua_State *L)
2223{
2224 int i;
2225 int j;
2226 const char *buf;
2227 size_t buf_len;
2228
2229 /* Check number of arguments. */
2230 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2231 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2232
2233 /* Get the string. */
2234 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2235
2236 /* Get and check j. */
2237 if (lua_gettop(L) == 4) {
2238 j = MAY_LJMP(luaL_checkinteger(L, 4));
2239 if (j < 0)
2240 j = buf_len + j + 1;
2241 if (j > buf_len)
2242 j = buf_len + 1;
2243 lua_pop(L, 1);
2244 }
2245 else
2246 j = buf_len;
2247
2248 /* Get and check i. */
2249 if (lua_gettop(L) == 3) {
2250 i = MAY_LJMP(luaL_checkinteger(L, 3));
2251 if (i < 0)
2252 i = buf_len + i + 1;
2253 if (i > buf_len)
2254 i = buf_len + 1;
2255 lua_pop(L, 1);
2256 } else
2257 i = 1;
2258
2259 /* Check bth i and j. */
2260 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002261 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002262 return 1;
2263 }
2264 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002265 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002266 return 1;
2267 }
2268 if (i == 0)
2269 i = 1;
2270 if (j == 0)
2271 j = 1;
2272
2273 /* Pop the string. */
2274 lua_pop(L, 1);
2275
2276 /* Update the buffer length. */
2277 buf += i - 1;
2278 buf_len = j - i + 1;
2279 lua_pushlstring(L, buf, buf_len);
2280
2281 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002282 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002283
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002284 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002285}
2286
Willy Tarreau22b0a682015-06-17 19:43:49 +02002287#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002288__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2289{
2290 static char buffer[SOCKET_INFO_MAX_LEN];
2291 int ret;
2292 int len;
2293 char *p;
2294
2295 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2296 if (ret <= 0) {
2297 lua_pushnil(L);
2298 return 1;
2299 }
2300
2301 if (ret == AF_UNIX) {
2302 lua_pushstring(L, buffer+1);
2303 return 1;
2304 }
2305 else if (ret == AF_INET6) {
2306 buffer[0] = '[';
2307 len = strlen(buffer);
2308 buffer[len] = ']';
2309 len++;
2310 buffer[len] = ':';
2311 len++;
2312 p = buffer;
2313 }
2314 else if (ret == AF_INET) {
2315 p = buffer + 1;
2316 len = strlen(p);
2317 p[len] = ':';
2318 len++;
2319 }
2320 else {
2321 lua_pushnil(L);
2322 return 1;
2323 }
2324
2325 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2326 lua_pushnil(L);
2327 return 1;
2328 }
2329
2330 lua_pushstring(L, p);
2331 return 1;
2332}
2333
2334/* Returns information about the peer of the connection. */
2335__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2336{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002337 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002338 struct xref *peer;
2339 struct appctx *appctx;
2340 struct stream_interface *si;
2341 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002342 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002343
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002344 MAY_LJMP(check_args(L, 1, "getpeername"));
2345
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002346 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002347
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002348 /* Check if we run on the same thread than the xreator thread.
2349 * We cannot access to the socket if the thread is different.
2350 */
2351 if (socket->tid != tid)
2352 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2353
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002354 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002355 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002356 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002357 lua_pushnil(L);
2358 return 1;
2359 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002360 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2361 si = appctx->owner;
2362 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002363
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002364 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002365 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002366 lua_pushnil(L);
2367 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002368 }
2369
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002370 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002371 xref_unlock(&socket->xref, peer);
2372 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002373}
2374
2375/* Returns information about my connection side. */
2376static int hlua_socket_getsockname(struct lua_State *L)
2377{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002378 struct hlua_socket *socket;
2379 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002380 struct appctx *appctx;
2381 struct xref *peer;
2382 struct stream_interface *si;
2383 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002384 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002385
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002386 MAY_LJMP(check_args(L, 1, "getsockname"));
2387
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002388 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002390 /* Check if we run on the same thread than the xreator thread.
2391 * We cannot access to the socket if the thread is different.
2392 */
2393 if (socket->tid != tid)
2394 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2395
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002396 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002397 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002398 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002399 lua_pushnil(L);
2400 return 1;
2401 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002402 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2403 si = appctx->owner;
2404 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002405
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002406 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002407 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002408 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002409 lua_pushnil(L);
2410 return 1;
2411 }
2412
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002413 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002414 xref_unlock(&socket->xref, peer);
2415 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002416}
2417
2418/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002419static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002420 .obj_type = OBJ_TYPE_APPLET,
2421 .name = "<LUA_TCP>",
2422 .fct = hlua_socket_handler,
2423 .release = hlua_socket_release,
2424};
2425
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002426__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002427{
2428 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2429 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002430 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002431 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002432 struct stream_interface *si;
2433 struct stream *s;
2434
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002435 /* Check if we run on the same thread than the xreator thread.
2436 * We cannot access to the socket if the thread is different.
2437 */
2438 if (socket->tid != tid)
2439 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2440
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002441 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002442 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002443 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002444 lua_pushnil(L);
2445 lua_pushstring(L, "Can't connect");
2446 return 2;
2447 }
2448 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2449 si = appctx->owner;
2450 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002451
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002452 /* Check if we run on the same thread than the xreator thread.
2453 * We cannot access to the socket if the thread is different.
2454 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002455 if (socket->tid != tid) {
2456 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002457 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002458 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002459
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002460 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002461 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002462 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002463 lua_pushnil(L);
2464 lua_pushstring(L, "Can't connect");
2465 return 2;
2466 }
2467
Willy Tarreaue09101e2018-10-16 17:37:12 +02002468 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002469
2470 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002471 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002472 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002473 lua_pushinteger(L, 1);
2474 return 1;
2475 }
2476
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002477 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2478 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002479 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002480 }
2481 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002482 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002483 return 0;
2484}
2485
2486/* This function fail or initite the connection. */
2487__LJMP static int hlua_socket_connect(struct lua_State *L)
2488{
2489 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002490 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002491 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002492 struct hlua *hlua;
2493 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002494 int low, high;
2495 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002496 struct xref *peer;
2497 struct stream_interface *si;
2498 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002499
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002500 if (lua_gettop(L) < 2)
2501 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002502
2503 /* Get args. */
2504 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002505
2506 /* Check if we run on the same thread than the xreator thread.
2507 * We cannot access to the socket if the thread is different.
2508 */
2509 if (socket->tid != tid)
2510 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2511
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002512 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002513 if (lua_gettop(L) >= 3) {
2514 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002515 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002516
Tim Duesterhus6edab862018-01-06 19:04:45 +01002517 /* Force the ip to end with a colon, to support IPv6 addresses
2518 * that are not enclosed within square brackets.
2519 */
2520 if (port > 0) {
2521 luaL_buffinit(L, &b);
2522 luaL_addstring(&b, ip);
2523 luaL_addchar(&b, ':');
2524 luaL_pushresult(&b);
2525 ip = lua_tolstring(L, lua_gettop(L), NULL);
2526 }
2527 }
2528
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002529 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002530 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002531 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002532 lua_pushnil(L);
2533 return 1;
2534 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002535
2536 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002537 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002538 if (!addr) {
2539 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002540 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002541 }
2542 if (low != high) {
2543 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002544 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002545 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002546
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002547 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002548 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002549 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002550 if (port == -1) {
2551 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002552 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002553 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002554 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2555 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002556 if (port == -1) {
2557 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002558 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002559 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002560 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002561 }
2562 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002563
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002564 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2565 si = appctx->owner;
2566 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002567
2568 if (!sockaddr_alloc(&s->target_addr)) {
2569 xref_unlock(&socket->xref, peer);
2570 WILL_LJMP(luaL_error(L, "connect: internal error"));
2571 }
2572 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002573 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002574
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002575 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002576
2577 /* inform the stream that we want to be notified whenever the
2578 * connection completes.
2579 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002580 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002581 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002582 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002583
Willy Tarreauf31af932020-01-14 09:59:38 +01002584 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002585
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002586 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2587 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002588 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002589 }
2590 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002591
2592 task_wakeup(s->task, TASK_WOKEN_INIT);
2593 /* Return yield waiting for connection. */
2594
Willy Tarreau9635e032018-10-16 17:52:55 +02002595 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002596
2597 return 0;
2598}
2599
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002600#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002601__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2602{
2603 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002604 struct xref *peer;
2605 struct appctx *appctx;
2606 struct stream_interface *si;
2607 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002608
2609 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2610 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002611
2612 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002613 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002614 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002615 lua_pushnil(L);
2616 return 1;
2617 }
2618 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2619 si = appctx->owner;
2620 s = si_strm(si);
2621
2622 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002623 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002624 return MAY_LJMP(hlua_socket_connect(L));
2625}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002626#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002627
2628__LJMP static int hlua_socket_setoption(struct lua_State *L)
2629{
2630 return 0;
2631}
2632
2633__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2634{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002635 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002636 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002637 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002638 struct xref *peer;
2639 struct appctx *appctx;
2640 struct stream_interface *si;
2641 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002642
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002643 MAY_LJMP(check_args(L, 2, "settimeout"));
2644
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002645 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002646
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002647 /* convert the timeout to millis */
2648 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002649
Thierry Fournier17a921b2018-03-08 09:59:02 +01002650 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002651 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002652 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2653
Mark Lakes56cc1252018-03-27 09:48:06 +02002654 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002655 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002656
2657 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002658 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002659 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002660
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002661 /* Check if we run on the same thread than the xreator thread.
2662 * We cannot access to the socket if the thread is different.
2663 */
2664 if (socket->tid != tid)
2665 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2666
Mark Lakes56cc1252018-03-27 09:48:06 +02002667 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002668 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002669 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002670 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2671 WILL_LJMP(lua_error(L));
2672 return 0;
2673 }
2674 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2675 si = appctx->owner;
2676 s = si_strm(si);
2677
Cyril Bonté7bb63452018-08-17 23:51:02 +02002678 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002679 s->req.rto = tmout;
2680 s->req.wto = tmout;
2681 s->res.rto = tmout;
2682 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002683 s->req.rex = tick_add_ifset(now_ms, tmout);
2684 s->req.wex = tick_add_ifset(now_ms, tmout);
2685 s->res.rex = tick_add_ifset(now_ms, tmout);
2686 s->res.wex = tick_add_ifset(now_ms, tmout);
2687
2688 s->task->expire = tick_add_ifset(now_ms, tmout);
2689 task_queue(s->task);
2690
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002691 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002692
Thierry Fourniere9636f12018-03-08 09:54:32 +01002693 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002694 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002695}
2696
2697__LJMP static int hlua_socket_new(lua_State *L)
2698{
2699 struct hlua_socket *socket;
2700 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002701 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002702 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002703
2704 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002705 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002706 hlua_pusherror(L, "socket: full stack");
2707 goto out_fail_conf;
2708 }
2709
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002710 /* Create the object: obj[0] = userdata. */
2711 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002712 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002713 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002714 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002715 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002716
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002717 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002718 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002719 hlua_pusherror(L, "socket: uninitialized pools.");
2720 goto out_fail_conf;
2721 }
2722
Willy Tarreau87b09662015-04-03 00:22:06 +02002723 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002724 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2725 lua_setmetatable(L, -2);
2726
Willy Tarreaud420a972015-04-06 00:39:18 +02002727 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002728 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002729 if (!appctx) {
2730 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002731 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002732 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002733
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002734 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002735 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002736 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2737 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002738
Willy Tarreaud420a972015-04-06 00:39:18 +02002739 /* Now create a session, task and stream for this applet */
2740 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2741 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002742 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002743 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002744 }
2745
Willy Tarreau87787ac2017-08-28 16:22:54 +02002746 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002747 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002748 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002749 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002750 }
2751
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002752 /* Initialise cross reference between stream and Lua socket object. */
2753 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002754
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002755 /* Configure "right" stream interface. this "si" is used to connect
2756 * and retrieve data from the server. The connection is initialized
2757 * with the "struct server".
2758 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002759 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002760
2761 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002762 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002763 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002764
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002765 return 1;
2766
Willy Tarreaud420a972015-04-06 00:39:18 +02002767 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002768 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002769 out_fail_sess:
2770 appctx_free(appctx);
2771 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002772 WILL_LJMP(lua_error(L));
2773 return 0;
2774}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002775
2776/*
2777 *
2778 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002779 * Class Channel
2780 *
2781 *
2782 */
2783
2784/* Returns the struct hlua_channel join to the class channel in the
2785 * stack entry "ud" or throws an argument error.
2786 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002787__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002788{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002789 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002790}
2791
Willy Tarreau47860ed2015-03-10 14:07:50 +01002792/* Pushes the channel onto the top of the stack. If the stask does not have a
2793 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002794 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002795static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002796{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002797 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002798 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002799 return 0;
2800
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002801 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002802 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002803 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002804
2805 /* Pop a class sesison metatable and affect it to the userdata. */
2806 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2807 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002808 return 1;
2809}
2810
2811/* Duplicate all the data present in the input channel and put it
2812 * in a string LUA variables. Returns -1 and push a nil value in
2813 * the stack if the channel is closed and all the data are consumed,
2814 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002815 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002816 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002817static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002818{
2819 char *blk1;
2820 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002821 size_t len1;
2822 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002823 int ret;
2824 luaL_Buffer b;
2825
Willy Tarreau06d80a92017-10-19 14:32:15 +02002826 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002827 if (unlikely(ret == 0))
2828 return 0;
2829
2830 if (unlikely(ret < 0)) {
2831 lua_pushnil(L);
2832 return -1;
2833 }
2834
2835 luaL_buffinit(L, &b);
2836 luaL_addlstring(&b, blk1, len1);
2837 if (unlikely(ret == 2))
2838 luaL_addlstring(&b, blk2, len2);
2839 luaL_pushresult(&b);
2840
2841 if (unlikely(ret == 2))
2842 return len1 + len2;
2843 return len1;
2844}
2845
2846/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2847 * a yield. This function keep the data in the buffer.
2848 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002849__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002850{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002851 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002852
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002853 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2854
Christopher Faulet3f829a42018-12-13 21:56:45 +01002855 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2856 WILL_LJMP(lua_error(L));
2857
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002858 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002859 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002860 return 1;
2861}
2862
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002863/* Check arguments for the function "hlua_channel_dup_yield". */
2864__LJMP static int hlua_channel_dup(lua_State *L)
2865{
2866 MAY_LJMP(check_args(L, 1, "dup"));
2867 MAY_LJMP(hlua_checkchannel(L, 1));
2868 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2869}
2870
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002871/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2872 * a yield. This function consumes the data in the buffer. It returns
2873 * a string containing the data or a nil pointer if no data are available
2874 * and the channel is closed.
2875 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002876__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002877{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002878 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002879 int ret;
2880
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002881 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002882
Christopher Faulet3f829a42018-12-13 21:56:45 +01002883 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2884 WILL_LJMP(lua_error(L));
2885
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002886 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002887 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002888 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002889
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002890 if (unlikely(ret == -1))
2891 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002892
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002893 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002894 return 1;
2895}
2896
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002897/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002898__LJMP static int hlua_channel_get(lua_State *L)
2899{
2900 MAY_LJMP(check_args(L, 1, "get"));
2901 MAY_LJMP(hlua_checkchannel(L, 1));
2902 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2903}
2904
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002905/* This functions consumes and returns one line. If the channel is closed,
2906 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002907 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002908 * value.
2909 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002910__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002911{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912 char *blk1;
2913 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002914 size_t len1;
2915 size_t len2;
2916 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002917 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918 int ret;
2919 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002920
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002921 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2922
Christopher Faulet3f829a42018-12-13 21:56:45 +01002923 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2924 WILL_LJMP(lua_error(L));
2925
Willy Tarreau06d80a92017-10-19 14:32:15 +02002926 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002927 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002928 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002929
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002930 if (ret == -1) {
2931 lua_pushnil(L);
2932 return 1;
2933 }
2934
2935 luaL_buffinit(L, &b);
2936 luaL_addlstring(&b, blk1, len1);
2937 len = len1;
2938 if (unlikely(ret == 2)) {
2939 luaL_addlstring(&b, blk2, len2);
2940 len += len2;
2941 }
2942 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002943 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002944 return 1;
2945}
2946
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002947/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002948__LJMP static int hlua_channel_getline(lua_State *L)
2949{
2950 MAY_LJMP(check_args(L, 1, "getline"));
2951 MAY_LJMP(hlua_checkchannel(L, 1));
2952 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2953}
2954
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002955/* This function takes a string as input, and append it at the
2956 * input side of channel. If the data is too big, but a space
2957 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002958 * yields. If the data is bigger than the buffer, or if the
2959 * channel is closed, it returns -1. Otherwise, it returns the
2960 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002961 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002962__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002963{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002964 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002965 size_t len;
2966 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2967 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2968 int ret;
2969 int max;
2970
Christopher Faulet3f829a42018-12-13 21:56:45 +01002971 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2972 WILL_LJMP(lua_error(L));
2973
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002974 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002975 * the request buffer if its not required.
2976 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002977 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002978 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002979 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002980 }
2981
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002982 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002983 if (max > len - l)
2984 max = len - l;
2985
Willy Tarreau06d80a92017-10-19 14:32:15 +02002986 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002987 if (ret == -2 || ret == -3) {
2988 lua_pushinteger(L, -1);
2989 return 1;
2990 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002991 if (ret == -1) {
2992 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002993 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002994 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002995 l += ret;
2996 lua_pop(L, 1);
2997 lua_pushinteger(L, l);
2998
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002999 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003000 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003001 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003002 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003003 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003004 */
3005 return 1;
3006 }
3007 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003008 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003009 return 1;
3010}
3011
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003012/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3013 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003014 * buffer size is too little for the data.
3015 */
3016__LJMP static int hlua_channel_append(lua_State *L)
3017{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003018 size_t len;
3019
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003020 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003021 MAY_LJMP(hlua_checkchannel(L, 1));
3022 MAY_LJMP(luaL_checklstring(L, 2, &len));
3023 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024 lua_pushinteger(L, 0);
3025
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003026 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003027}
3028
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003029/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003030 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003031 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003032 * or -1 if the channel is closed or if the buffer size is too
3033 * little for the data.
3034 */
3035__LJMP static int hlua_channel_set(lua_State *L)
3036{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003037 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003038
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003039 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003040 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003041 lua_pushinteger(L, 0);
3042
Christopher Faulet3f829a42018-12-13 21:56:45 +01003043 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3044 WILL_LJMP(lua_error(L));
3045
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003046 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003047
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003048 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003049}
3050
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003051/* Append data in the output side of the buffer. This data is immediately
3052 * sent. The function returns the amount of data written. If the buffer
3053 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003054 * if the channel is closed.
3055 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003056__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003057{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003058 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003059 size_t len;
3060 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3061 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3062 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003063 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003064
Christopher Faulet3f829a42018-12-13 21:56:45 +01003065 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3066 WILL_LJMP(lua_error(L));
3067
Willy Tarreau47860ed2015-03-10 14:07:50 +01003068 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003069 lua_pushinteger(L, -1);
3070 return 1;
3071 }
3072
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003073 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003074 * the request buffer if its not required.
3075 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003076 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003077 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003078 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003079 }
3080
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003081 /* The written data will be immediately sent, so we can check
3082 * the available space without taking in account the reserve.
3083 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003084 * data, because the buffer will be flushed.
3085 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003086 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003087
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003088 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003089 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003090 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003091 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003092 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003093 return 1;
3094
3095 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003096 if (max > len - l)
3097 max = len - l;
3098
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003099 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003100 * detects a non contiguous buffer and realign it.
3101 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003102 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003103 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003104
3105 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003106 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003107
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003108 /* buffer replace considers that the input part is filled.
3109 * so, I must forward these new data in the output part.
3110 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003111 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003112
3113 l += max;
3114 lua_pop(L, 1);
3115 lua_pushinteger(L, l);
3116
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003117 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003118 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003119 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003120 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003121 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003122 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003123 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003124
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003125 if (l < len) {
3126 /* If we are waiting for space in the response buffer, we
3127 * must set the flag WAKERESWR. This flag required the task
3128 * wake up if any activity is detected on the response buffer.
3129 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003130 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003131 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003132 else
3133 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003134 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003135 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003136
3137 return 1;
3138}
3139
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003140/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003141 * yield the LUA process, and resume it without checking the
3142 * input arguments.
3143 */
3144__LJMP static int hlua_channel_send(lua_State *L)
3145{
3146 MAY_LJMP(check_args(L, 2, "send"));
3147 lua_pushinteger(L, 0);
3148
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003149 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003150}
3151
3152/* This function forward and amount of butes. The data pass from
3153 * the input side of the buffer to the output side, and can be
3154 * forwarded. This function never fails.
3155 *
3156 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003157 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003158 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003159__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003160{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003161 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003162 int len;
3163 int l;
3164 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003165 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003166
3167 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003168
3169 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3170 WILL_LJMP(lua_error(L));
3171
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003172 len = MAY_LJMP(luaL_checkinteger(L, 2));
3173 l = MAY_LJMP(luaL_checkinteger(L, -1));
3174
3175 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003176 if (max > ci_data(chn))
3177 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003178 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003179 l += max;
3180
3181 lua_pop(L, 1);
3182 lua_pushinteger(L, l);
3183
3184 /* Check if it miss bytes to forward. */
3185 if (l < len) {
3186 /* The the input channel or the output channel are closed, we
3187 * must return the amount of data forwarded.
3188 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003189 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003190 return 1;
3191
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003192 /* If we are waiting for space data in the response buffer, we
3193 * must set the flag WAKERESWR. This flag required the task
3194 * wake up if any activity is detected on the response buffer.
3195 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003196 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003197 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003198 else
3199 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003200
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003201 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003202 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003203 }
3204
3205 return 1;
3206}
3207
3208/* Just check the input and prepare the stack for the previous
3209 * function "hlua_channel_forward_yield"
3210 */
3211__LJMP static int hlua_channel_forward(lua_State *L)
3212{
3213 MAY_LJMP(check_args(L, 2, "forward"));
3214 MAY_LJMP(hlua_checkchannel(L, 1));
3215 MAY_LJMP(luaL_checkinteger(L, 2));
3216
3217 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003218 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003219}
3220
3221/* Just returns the number of bytes available in the input
3222 * side of the buffer. This function never fails.
3223 */
3224__LJMP static int hlua_channel_get_in_len(lua_State *L)
3225{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003226 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003227
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003228 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003229 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003230 if (IS_HTX_STRM(chn_strm(chn))) {
3231 struct htx *htx = htxbuf(&chn->buf);
3232 lua_pushinteger(L, htx->data - co_data(chn));
3233 }
3234 else
3235 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003236 return 1;
3237}
3238
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003239/* Returns true if the channel is full. */
3240__LJMP static int hlua_channel_is_full(lua_State *L)
3241{
3242 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003243
3244 MAY_LJMP(check_args(L, 1, "is_full"));
3245 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003246 /* ignore the reserve, we are not on a producer side (ie in an
3247 * applet).
3248 */
3249 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003250 return 1;
3251}
3252
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003253/* Returns true if the channel is the response channel. */
3254__LJMP static int hlua_channel_is_resp(lua_State *L)
3255{
3256 struct channel *chn;
3257
3258 MAY_LJMP(check_args(L, 1, "is_resp"));
3259 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3260
3261 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3262 return 1;
3263}
3264
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003265/* Just returns the number of bytes available in the output
3266 * side of the buffer. This function never fails.
3267 */
3268__LJMP static int hlua_channel_get_out_len(lua_State *L)
3269{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003270 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003271
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003272 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003273 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003274 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003275 return 1;
3276}
3277
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003278/*
3279 *
3280 *
3281 * Class Fetches
3282 *
3283 *
3284 */
3285
3286/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003287 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003288 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003289__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003290{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003291 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003292}
3293
3294/* This function creates and push in the stack a fetch object according
3295 * with a current TXN.
3296 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003297static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003298{
Willy Tarreau7073c472015-04-06 11:15:40 +02003299 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003300
3301 /* Check stack size. */
3302 if (!lua_checkstack(L, 3))
3303 return 0;
3304
3305 /* Create the object: obj[0] = userdata.
3306 * Note that the base of the Fetches object is the
3307 * transaction object.
3308 */
3309 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003310 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003311 lua_rawseti(L, -2, 0);
3312
Willy Tarreau7073c472015-04-06 11:15:40 +02003313 hsmp->s = txn->s;
3314 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003315 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003316 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003317
3318 /* Pop a class sesison metatable and affect it to the userdata. */
3319 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3320 lua_setmetatable(L, -2);
3321
3322 return 1;
3323}
3324
3325/* This function is an LUA binding. It is called with each sample-fetch.
3326 * It uses closure argument to store the associated sample-fetch. It
3327 * returns only one argument or throws an error. An error is thrown
3328 * only if an error is encountered during the argument parsing. If
3329 * the "sample-fetch" function fails, nil is returned.
3330 */
3331__LJMP static int hlua_run_sample_fetch(lua_State *L)
3332{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003333 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003334 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003335 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003336 int i;
3337 struct sample smp;
3338
3339 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003340 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003341
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003342 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003343 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003344
Thierry FOURNIERca988662015-12-20 18:43:03 +01003345 /* Check execution authorization. */
3346 if (f->use & SMP_USE_HTTP_ANY &&
3347 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3348 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3349 "is not available in Lua services", f->kw);
3350 WILL_LJMP(lua_error(L));
3351 }
3352
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003353 /* Get extra arguments. */
3354 for (i = 0; i < lua_gettop(L) - 1; i++) {
3355 if (i >= ARGM_NBARGS)
3356 break;
3357 hlua_lua2arg(L, i + 2, &args[i]);
3358 }
3359 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003360 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003361
3362 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003363 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003364
3365 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003366 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003367 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003368 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003369 }
3370
3371 /* Initialise the sample. */
3372 memset(&smp, 0, sizeof(smp));
3373
3374 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003375 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003376 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003377 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003378 lua_pushstring(L, "");
3379 else
3380 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003381 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003382 }
3383
3384 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003385 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003386 hlua_smp2lua_str(L, &smp);
3387 else
3388 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003389
3390 end:
3391 for (i = 0; args[i].type != ARGT_STOP; i++) {
3392 if (args[i].type == ARGT_STR)
3393 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003394 else if (args[i].type == ARGT_REG)
3395 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003396 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003397 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003398
3399 error:
3400 for (i = 0; args[i].type != ARGT_STOP; i++) {
3401 if (args[i].type == ARGT_STR)
3402 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003403 else if (args[i].type == ARGT_REG)
3404 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003405 }
3406 WILL_LJMP(lua_error(L));
3407 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003408}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003409
3410/*
3411 *
3412 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003413 * Class Converters
3414 *
3415 *
3416 */
3417
3418/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003419 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003420 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003421__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003422{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003423 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003424}
3425
3426/* This function creates and push in the stack a Converters object
3427 * according with a current TXN.
3428 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003429static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003430{
Willy Tarreau7073c472015-04-06 11:15:40 +02003431 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003432
3433 /* Check stack size. */
3434 if (!lua_checkstack(L, 3))
3435 return 0;
3436
3437 /* Create the object: obj[0] = userdata.
3438 * Note that the base of the Converters object is the
3439 * same than the TXN object.
3440 */
3441 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003442 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003443 lua_rawseti(L, -2, 0);
3444
Willy Tarreau7073c472015-04-06 11:15:40 +02003445 hsmp->s = txn->s;
3446 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003447 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003448 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003449
Willy Tarreau87b09662015-04-03 00:22:06 +02003450 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003451 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3452 lua_setmetatable(L, -2);
3453
3454 return 1;
3455}
3456
3457/* This function is an LUA binding. It is called with each converter.
3458 * It uses closure argument to store the associated converter. It
3459 * returns only one argument or throws an error. An error is thrown
3460 * only if an error is encountered during the argument parsing. If
3461 * the converter function function fails, nil is returned.
3462 */
3463__LJMP static int hlua_run_sample_conv(lua_State *L)
3464{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003465 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003466 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003467 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003468 int i;
3469 struct sample smp;
3470
3471 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003472 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003473
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003474 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003475 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003476
3477 /* Get extra arguments. */
3478 for (i = 0; i < lua_gettop(L) - 2; i++) {
3479 if (i >= ARGM_NBARGS)
3480 break;
3481 hlua_lua2arg(L, i + 3, &args[i]);
3482 }
3483 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003484 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003485
3486 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003487 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003488
3489 /* Run the special args checker. */
3490 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3491 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003492 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003493 }
3494
3495 /* Initialise the sample. */
3496 if (!hlua_lua2smp(L, 2, &smp)) {
3497 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003498 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003499 }
3500
Willy Tarreau1777ea62016-03-10 16:15:46 +01003501 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3502
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003503 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003504 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003505 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003506 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003507 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003508 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003509 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3510 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003511 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003512 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003513 }
3514
3515 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003516 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003517 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003518 lua_pushstring(L, "");
3519 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003520 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003521 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003522 }
3523
3524 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003525 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003526 hlua_smp2lua_str(L, &smp);
3527 else
3528 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003529 end:
3530 for (i = 0; args[i].type != ARGT_STOP; i++) {
3531 if (args[i].type == ARGT_STR)
3532 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003533 else if (args[i].type == ARGT_REG)
3534 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003535 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003536 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003537
3538 error:
3539 for (i = 0; args[i].type != ARGT_STOP; i++) {
3540 if (args[i].type == ARGT_STR)
3541 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003542 else if (args[i].type == ARGT_REG)
3543 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003544 }
3545 WILL_LJMP(lua_error(L));
3546 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003547}
3548
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003549/*
3550 *
3551 *
3552 * Class AppletTCP
3553 *
3554 *
3555 */
3556
3557/* Returns a struct hlua_txn if the stack entry "ud" is
3558 * a class stream, otherwise it throws an error.
3559 */
3560__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3561{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003562 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003563}
3564
3565/* This function creates and push in the stack an Applet object
3566 * according with a current TXN.
3567 */
3568static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3569{
3570 struct hlua_appctx *appctx;
3571 struct stream_interface *si = ctx->owner;
3572 struct stream *s = si_strm(si);
3573 struct proxy *p = s->be;
3574
3575 /* Check stack size. */
3576 if (!lua_checkstack(L, 3))
3577 return 0;
3578
3579 /* Create the object: obj[0] = userdata.
3580 * Note that the base of the Converters object is the
3581 * same than the TXN object.
3582 */
3583 lua_newtable(L);
3584 appctx = lua_newuserdata(L, sizeof(*appctx));
3585 lua_rawseti(L, -2, 0);
3586 appctx->appctx = ctx;
3587 appctx->htxn.s = s;
3588 appctx->htxn.p = p;
3589
3590 /* Create the "f" field that contains a list of fetches. */
3591 lua_pushstring(L, "f");
3592 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3593 return 0;
3594 lua_settable(L, -3);
3595
3596 /* Create the "sf" field that contains a list of stringsafe fetches. */
3597 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003598 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003599 return 0;
3600 lua_settable(L, -3);
3601
3602 /* Create the "c" field that contains a list of converters. */
3603 lua_pushstring(L, "c");
3604 if (!hlua_converters_new(L, &appctx->htxn, 0))
3605 return 0;
3606 lua_settable(L, -3);
3607
3608 /* Create the "sc" field that contains a list of stringsafe converters. */
3609 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003610 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003611 return 0;
3612 lua_settable(L, -3);
3613
3614 /* Pop a class stream metatable and affect it to the table. */
3615 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3616 lua_setmetatable(L, -2);
3617
3618 return 1;
3619}
3620
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003621__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3622{
3623 struct hlua_appctx *appctx;
3624 struct stream *s;
3625 const char *name;
3626 size_t len;
3627 struct sample smp;
3628
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003629 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3630 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003631
3632 /* It is useles to retrieve the stream, but this function
3633 * runs only in a stream context.
3634 */
3635 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3636 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3637 s = appctx->htxn.s;
3638
3639 /* Converts the third argument in a sample. */
3640 hlua_lua2smp(L, 3, &smp);
3641
3642 /* Store the sample in a variable. */
3643 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003644
3645 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3646 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3647 else
3648 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3649
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003650 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003651}
3652
3653__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3654{
3655 struct hlua_appctx *appctx;
3656 struct stream *s;
3657 const char *name;
3658 size_t len;
3659 struct sample smp;
3660
3661 MAY_LJMP(check_args(L, 2, "unset_var"));
3662
3663 /* It is useles to retrieve the stream, but this function
3664 * runs only in a stream context.
3665 */
3666 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3667 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3668 s = appctx->htxn.s;
3669
3670 /* Unset the variable. */
3671 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003672 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3673 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003674}
3675
3676__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3677{
3678 struct hlua_appctx *appctx;
3679 struct stream *s;
3680 const char *name;
3681 size_t len;
3682 struct sample smp;
3683
3684 MAY_LJMP(check_args(L, 2, "get_var"));
3685
3686 /* It is useles to retrieve the stream, but this function
3687 * runs only in a stream context.
3688 */
3689 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3690 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3691 s = appctx->htxn.s;
3692
3693 smp_set_owner(&smp, s->be, s->sess, s, 0);
3694 if (!vars_get_by_name(name, len, &smp)) {
3695 lua_pushnil(L);
3696 return 1;
3697 }
3698
3699 return hlua_smp2lua(L, &smp);
3700}
3701
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003702__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3703{
3704 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3705 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003706 struct hlua *hlua;
3707
3708 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003709 if (!s->hlua)
3710 return 0;
3711 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003712
3713 MAY_LJMP(check_args(L, 2, "set_priv"));
3714
3715 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003716 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003717
3718 /* Get and store new value. */
3719 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3720 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3721
3722 return 0;
3723}
3724
3725__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3726{
3727 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3728 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003729 struct hlua *hlua;
3730
3731 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003732 if (!s->hlua) {
3733 lua_pushnil(L);
3734 return 1;
3735 }
3736 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003737
3738 /* Push configuration index in the stack. */
3739 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3740
3741 return 1;
3742}
3743
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003744/* If expected data not yet available, it returns a yield. This function
3745 * consumes the data in the buffer. It returns a string containing the
3746 * data. This string can be empty.
3747 */
3748__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3749{
3750 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3751 struct stream_interface *si = appctx->appctx->owner;
3752 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003753 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003754 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003755 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003756 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003757
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003758 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003759 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003760
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003761 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003762 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003763 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003764 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003765 }
3766
3767 /* End of data: commit the total strings and return. */
3768 if (ret < 0) {
3769 luaL_pushresult(&appctx->b);
3770 return 1;
3771 }
3772
3773 /* Ensure that the block 2 length is usable. */
3774 if (ret == 1)
3775 len2 = 0;
3776
3777 /* dont check the max length read and dont check. */
3778 luaL_addlstring(&appctx->b, blk1, len1);
3779 luaL_addlstring(&appctx->b, blk2, len2);
3780
3781 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003782 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003783 luaL_pushresult(&appctx->b);
3784 return 1;
3785}
3786
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003787/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003788__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3789{
3790 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3791
3792 /* Initialise the string catenation. */
3793 luaL_buffinit(L, &appctx->b);
3794
3795 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3796}
3797
3798/* If expected data not yet available, it returns a yield. This function
3799 * consumes the data in the buffer. It returns a string containing the
3800 * data. This string can be empty.
3801 */
3802__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3803{
3804 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3805 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003806 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003807 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003808 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003809 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003810 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003811 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003812
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003813 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003814 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003815
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003816 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003817 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003818 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003819 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003820 }
3821
3822 /* End of data: commit the total strings and return. */
3823 if (ret < 0) {
3824 luaL_pushresult(&appctx->b);
3825 return 1;
3826 }
3827
3828 /* Ensure that the block 2 length is usable. */
3829 if (ret == 1)
3830 len2 = 0;
3831
3832 if (len == -1) {
3833
3834 /* If len == -1, catenate all the data avalaile and
3835 * yield because we want to get all the data until
3836 * the end of data stream.
3837 */
3838 luaL_addlstring(&appctx->b, blk1, len1);
3839 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003840 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003841 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003842 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003843
3844 } else {
3845
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003846 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003847 if (len1 > len)
3848 len1 = len;
3849 luaL_addlstring(&appctx->b, blk1, len1);
3850 len -= len1;
3851
3852 /* Copy the second block. */
3853 if (len2 > len)
3854 len2 = len;
3855 luaL_addlstring(&appctx->b, blk2, len2);
3856 len -= len2;
3857
3858 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003859 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003860
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003861 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003862 if (len > 0) {
3863 lua_pushinteger(L, len);
3864 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003865 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003866 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003867 }
3868
3869 /* return the result. */
3870 luaL_pushresult(&appctx->b);
3871 return 1;
3872 }
3873
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003874 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003875 hlua_pusherror(L, "Lua: internal error");
3876 WILL_LJMP(lua_error(L));
3877 return 0;
3878}
3879
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003880/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003881__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3882{
3883 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3884 int len = -1;
3885
3886 if (lua_gettop(L) > 2)
3887 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3888 if (lua_gettop(L) >= 2) {
3889 len = MAY_LJMP(luaL_checkinteger(L, 2));
3890 lua_pop(L, 1);
3891 }
3892
3893 /* Confirm or set the required length */
3894 lua_pushinteger(L, len);
3895
3896 /* Initialise the string catenation. */
3897 luaL_buffinit(L, &appctx->b);
3898
3899 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3900}
3901
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003902/* Append data in the output side of the buffer. This data is immediately
3903 * sent. The function returns the amount of data written. If the buffer
3904 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003905 * if the channel is closed.
3906 */
3907__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3908{
3909 size_t len;
3910 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3911 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3912 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3913 struct stream_interface *si = appctx->appctx->owner;
3914 struct channel *chn = si_ic(si);
3915 int max;
3916
3917 /* Get the max amount of data which can write as input in the channel. */
3918 max = channel_recv_max(chn);
3919 if (max > (len - l))
3920 max = len - l;
3921
3922 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003923 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003924
3925 /* update counters. */
3926 l += max;
3927 lua_pop(L, 1);
3928 lua_pushinteger(L, l);
3929
3930 /* If some data is not send, declares the situation to the
3931 * applet, and returns a yield.
3932 */
3933 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003934 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003935 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003936 }
3937
3938 return 1;
3939}
3940
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003941/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003942 * yield the LUA process, and resume it without checking the
3943 * input arguments.
3944 */
3945__LJMP static int hlua_applet_tcp_send(lua_State *L)
3946{
3947 MAY_LJMP(check_args(L, 2, "send"));
3948 lua_pushinteger(L, 0);
3949
3950 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3951}
3952
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003953/*
3954 *
3955 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003956 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003957 *
3958 *
3959 */
3960
3961/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003962 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003963 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003964__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003965{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003966 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003967}
3968
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003969/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003970 * according with a current TXN.
3971 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003972static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003973{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003974 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003975 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003976 struct stream_interface *si = ctx->owner;
3977 struct stream *s = si_strm(si);
3978 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003979 struct htx *htx;
3980 struct htx_blk *blk;
3981 struct htx_sl *sl;
3982 struct ist path;
3983 unsigned long long len = 0;
3984 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003985
3986 /* Check stack size. */
3987 if (!lua_checkstack(L, 3))
3988 return 0;
3989
3990 /* Create the object: obj[0] = userdata.
3991 * Note that the base of the Converters object is the
3992 * same than the TXN object.
3993 */
3994 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003995 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003996 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003997 appctx->appctx = ctx;
3998 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003999 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004000 appctx->htxn.s = s;
4001 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004002
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004003 /* Create the "f" field that contains a list of fetches. */
4004 lua_pushstring(L, "f");
4005 if (!hlua_fetches_new(L, &appctx->htxn, 0))
4006 return 0;
4007 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004008
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004009 /* Create the "sf" field that contains a list of stringsafe fetches. */
4010 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004011 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004012 return 0;
4013 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004014
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004015 /* Create the "c" field that contains a list of converters. */
4016 lua_pushstring(L, "c");
4017 if (!hlua_converters_new(L, &appctx->htxn, 0))
4018 return 0;
4019 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004020
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004021 /* Create the "sc" field that contains a list of stringsafe converters. */
4022 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004023 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004024 return 0;
4025 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004026
Christopher Fauleta2097962019-07-15 16:25:33 +02004027 htx = htxbuf(&s->req.buf);
4028 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004029 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004030 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004031
Christopher Fauleta2097962019-07-15 16:25:33 +02004032 /* Stores the request method. */
4033 lua_pushstring(L, "method");
4034 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4035 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004036
Christopher Fauleta2097962019-07-15 16:25:33 +02004037 /* Stores the http version. */
4038 lua_pushstring(L, "version");
4039 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4040 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004041
Christopher Fauleta2097962019-07-15 16:25:33 +02004042 /* creates an array of headers. hlua_http_get_headers() crates and push
4043 * the array on the top of the stack.
4044 */
4045 lua_pushstring(L, "headers");
4046 htxn.s = s;
4047 htxn.p = px;
4048 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004049 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004050 return 0;
4051 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004052
Christopher Fauleta2097962019-07-15 16:25:33 +02004053 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004054 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004055 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004056
Christopher Fauleta2097962019-07-15 16:25:33 +02004057 p = path.ptr;
4058 end = path.ptr + path.len;
4059 q = p;
4060 while (q < end && *q != '?')
4061 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004062
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004063 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004064 lua_pushstring(L, "path");
4065 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004066 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004067
Christopher Fauleta2097962019-07-15 16:25:33 +02004068 /* Stores the query string. */
4069 lua_pushstring(L, "qs");
4070 if (*q == '?')
4071 q++;
4072 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004073 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004074 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004075
Christopher Fauleta2097962019-07-15 16:25:33 +02004076 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4077 struct htx_blk *blk = htx_get_blk(htx, pos);
4078 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004079
Christopher Fauleta2097962019-07-15 16:25:33 +02004080 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
4081 break;
4082 if (type == HTX_BLK_DATA)
4083 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004084 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004085 if (htx->extra != ULLONG_MAX)
4086 len += htx->extra;
4087
4088 /* Stores the request path. */
4089 lua_pushstring(L, "length");
4090 lua_pushinteger(L, len);
4091 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004092
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004093 /* Create an empty array of HTTP request headers. */
4094 lua_pushstring(L, "response");
4095 lua_newtable(L);
4096 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004097
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004098 /* Pop a class stream metatable and affect it to the table. */
4099 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4100 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004101
4102 return 1;
4103}
4104
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004105__LJMP static int hlua_applet_http_set_var(lua_State *L)
4106{
4107 struct hlua_appctx *appctx;
4108 struct stream *s;
4109 const char *name;
4110 size_t len;
4111 struct sample smp;
4112
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004113 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4114 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004115
4116 /* It is useles to retrieve the stream, but this function
4117 * runs only in a stream context.
4118 */
4119 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4120 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4121 s = appctx->htxn.s;
4122
4123 /* Converts the third argument in a sample. */
4124 hlua_lua2smp(L, 3, &smp);
4125
4126 /* Store the sample in a variable. */
4127 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004128
4129 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4130 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4131 else
4132 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4133
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004134 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004135}
4136
4137__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4138{
4139 struct hlua_appctx *appctx;
4140 struct stream *s;
4141 const char *name;
4142 size_t len;
4143 struct sample smp;
4144
4145 MAY_LJMP(check_args(L, 2, "unset_var"));
4146
4147 /* It is useles to retrieve the stream, but this function
4148 * runs only in a stream context.
4149 */
4150 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4151 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4152 s = appctx->htxn.s;
4153
4154 /* Unset the variable. */
4155 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004156 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4157 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004158}
4159
4160__LJMP static int hlua_applet_http_get_var(lua_State *L)
4161{
4162 struct hlua_appctx *appctx;
4163 struct stream *s;
4164 const char *name;
4165 size_t len;
4166 struct sample smp;
4167
4168 MAY_LJMP(check_args(L, 2, "get_var"));
4169
4170 /* It is useles to retrieve the stream, but this function
4171 * runs only in a stream context.
4172 */
4173 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4174 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4175 s = appctx->htxn.s;
4176
4177 smp_set_owner(&smp, s->be, s->sess, s, 0);
4178 if (!vars_get_by_name(name, len, &smp)) {
4179 lua_pushnil(L);
4180 return 1;
4181 }
4182
4183 return hlua_smp2lua(L, &smp);
4184}
4185
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004186__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4187{
4188 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4189 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004190 struct hlua *hlua;
4191
4192 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004193 if (!s->hlua)
4194 return 0;
4195 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004196
4197 MAY_LJMP(check_args(L, 2, "set_priv"));
4198
4199 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004200 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004201
4202 /* Get and store new value. */
4203 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4204 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4205
4206 return 0;
4207}
4208
4209__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4210{
4211 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4212 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004213 struct hlua *hlua;
4214
4215 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004216 if (!s->hlua) {
4217 lua_pushnil(L);
4218 return 1;
4219 }
4220 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004221
4222 /* Push configuration index in the stack. */
4223 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4224
4225 return 1;
4226}
4227
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004228/* If expected data not yet available, it returns a yield. This function
4229 * consumes the data in the buffer. It returns a string containing the
4230 * data. This string can be empty.
4231 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004232__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004233{
4234 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4235 struct stream_interface *si = appctx->appctx->owner;
4236 struct channel *req = si_oc(si);
4237 struct htx *htx;
4238 struct htx_blk *blk;
4239 size_t count;
4240 int stop = 0;
4241
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004242 htx = htx_from_buf(&req->buf);
4243 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004244 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004245
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004246 while (count && !stop && blk) {
4247 enum htx_blk_type type = htx_get_blk_type(blk);
4248 uint32_t sz = htx_get_blksz(blk);
4249 struct ist v;
4250 uint32_t vlen;
4251 char *nl;
4252
Christopher Faulete461e342018-12-18 16:43:35 +01004253 if (type == HTX_BLK_EOM) {
4254 stop = 1;
4255 break;
4256 }
4257
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004258 vlen = sz;
4259 if (vlen > count) {
4260 if (type != HTX_BLK_DATA)
4261 break;
4262 vlen = count;
4263 }
4264
4265 switch (type) {
4266 case HTX_BLK_UNUSED:
4267 break;
4268
4269 case HTX_BLK_DATA:
4270 v = htx_get_blk_value(htx, blk);
4271 v.len = vlen;
4272 nl = istchr(v, '\n');
4273 if (nl != NULL) {
4274 stop = 1;
4275 vlen = nl - v.ptr + 1;
4276 }
4277 luaL_addlstring(&appctx->b, v.ptr, vlen);
4278 break;
4279
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004280 case HTX_BLK_TLR:
4281 case HTX_BLK_EOM:
4282 stop = 1;
4283 break;
4284
4285 default:
4286 break;
4287 }
4288
4289 co_set_data(req, co_data(req) - vlen);
4290 count -= vlen;
4291 if (sz == vlen)
4292 blk = htx_remove_blk(htx, blk);
4293 else {
4294 htx_cut_data_blk(htx, blk, vlen);
4295 break;
4296 }
4297 }
4298
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004299 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004300 if (!stop) {
4301 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004302 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004303 }
4304
4305 /* return the result. */
4306 luaL_pushresult(&appctx->b);
4307 return 1;
4308}
4309
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004310
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004311/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004312__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004313{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004314 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004315
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004316 /* Initialise the string catenation. */
4317 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004318
Christopher Fauleta2097962019-07-15 16:25:33 +02004319 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004320}
4321
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004322/* If expected data not yet available, it returns a yield. This function
4323 * consumes the data in the buffer. It returns a string containing the
4324 * data. This string can be empty.
4325 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004326__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004327{
4328 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4329 struct stream_interface *si = appctx->appctx->owner;
4330 struct channel *req = si_oc(si);
4331 struct htx *htx;
4332 struct htx_blk *blk;
4333 size_t count;
4334 int len;
4335
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004336 htx = htx_from_buf(&req->buf);
4337 len = MAY_LJMP(luaL_checkinteger(L, 2));
4338 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004339 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004340 while (count && len && blk) {
4341 enum htx_blk_type type = htx_get_blk_type(blk);
4342 uint32_t sz = htx_get_blksz(blk);
4343 struct ist v;
4344 uint32_t vlen;
4345
Christopher Faulete461e342018-12-18 16:43:35 +01004346 if (type == HTX_BLK_EOM) {
4347 len = 0;
4348 break;
4349 }
4350
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004351 vlen = sz;
4352 if (len > 0 && vlen > len)
4353 vlen = len;
4354 if (vlen > count) {
4355 if (type != HTX_BLK_DATA)
4356 break;
4357 vlen = count;
4358 }
4359
4360 switch (type) {
4361 case HTX_BLK_UNUSED:
4362 break;
4363
4364 case HTX_BLK_DATA:
4365 v = htx_get_blk_value(htx, blk);
4366 luaL_addlstring(&appctx->b, v.ptr, vlen);
4367 break;
4368
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004369 case HTX_BLK_TLR:
4370 case HTX_BLK_EOM:
4371 len = 0;
4372 break;
4373
4374 default:
4375 break;
4376 }
4377
4378 co_set_data(req, co_data(req) - vlen);
4379 count -= vlen;
4380 if (len > 0)
4381 len -= vlen;
4382 if (sz == vlen)
4383 blk = htx_remove_blk(htx, blk);
4384 else {
4385 htx_cut_data_blk(htx, blk, vlen);
4386 break;
4387 }
4388 }
4389
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004390 htx_to_buf(htx, &req->buf);
4391
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004392 /* If we are no other data available, yield waiting for new data. */
4393 if (len) {
4394 if (len > 0) {
4395 lua_pushinteger(L, len);
4396 lua_replace(L, 2);
4397 }
4398 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004399 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004400 }
4401
4402 /* return the result. */
4403 luaL_pushresult(&appctx->b);
4404 return 1;
4405}
4406
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004407/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004408__LJMP static int hlua_applet_http_recv(lua_State *L)
4409{
4410 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4411 int len = -1;
4412
4413 /* Check arguments. */
4414 if (lua_gettop(L) > 2)
4415 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4416 if (lua_gettop(L) >= 2) {
4417 len = MAY_LJMP(luaL_checkinteger(L, 2));
4418 lua_pop(L, 1);
4419 }
4420
Christopher Fauleta2097962019-07-15 16:25:33 +02004421 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004422
Christopher Fauleta2097962019-07-15 16:25:33 +02004423 /* Initialise the string catenation. */
4424 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004425
Christopher Fauleta2097962019-07-15 16:25:33 +02004426 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004427}
4428
4429/* Append data in the output side of the buffer. This data is immediately
4430 * sent. The function returns the amount of data written. If the buffer
4431 * cannot contain the data, the function yields. The function returns -1
4432 * if the channel is closed.
4433 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004434__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004435{
4436 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4437 struct stream_interface *si = appctx->appctx->owner;
4438 struct channel *res = si_ic(si);
4439 struct htx *htx = htx_from_buf(&res->buf);
4440 const char *data;
4441 size_t len;
4442 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4443 int max;
4444
Christopher Faulet9060fc02019-07-03 11:39:30 +02004445 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004446 if (!max)
4447 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004448
4449 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4450
4451 /* Get the max amount of data which can write as input in the channel. */
4452 if (max > (len - l))
4453 max = len - l;
4454
4455 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004456 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004457 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004458
4459 /* update counters. */
4460 l += max;
4461 lua_pop(L, 1);
4462 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004463
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004464 /* If some data is not send, declares the situation to the
4465 * applet, and returns a yield.
4466 */
4467 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004468 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004469 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004470 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004471 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004472 }
4473
Christopher Fauleta2097962019-07-15 16:25:33 +02004474 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004475 return 1;
4476}
4477
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004478/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004479 * yield the LUA process, and resume it without checking the
4480 * input arguments.
4481 */
4482__LJMP static int hlua_applet_http_send(lua_State *L)
4483{
4484 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004485
4486 /* We want to send some data. Headers must be sent. */
4487 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4488 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4489 WILL_LJMP(lua_error(L));
4490 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004491
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004492 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004493 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004494
Christopher Fauleta2097962019-07-15 16:25:33 +02004495 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004496}
4497
4498__LJMP static int hlua_applet_http_addheader(lua_State *L)
4499{
4500 const char *name;
4501 int ret;
4502
4503 MAY_LJMP(hlua_checkapplet_http(L, 1));
4504 name = MAY_LJMP(luaL_checkstring(L, 2));
4505 MAY_LJMP(luaL_checkstring(L, 3));
4506
4507 /* Push in the stack the "response" entry. */
4508 ret = lua_getfield(L, 1, "response");
4509 if (ret != LUA_TTABLE) {
4510 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4511 "is expected as an array. %s found", lua_typename(L, ret));
4512 WILL_LJMP(lua_error(L));
4513 }
4514
4515 /* check if the header is already registered if it is not
4516 * the case, register it.
4517 */
4518 ret = lua_getfield(L, -1, name);
4519 if (ret == LUA_TNIL) {
4520
4521 /* Entry not found. */
4522 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4523
4524 /* Insert the new header name in the array in the top of the stack.
4525 * It left the new array in the top of the stack.
4526 */
4527 lua_newtable(L);
4528 lua_pushvalue(L, 2);
4529 lua_pushvalue(L, -2);
4530 lua_settable(L, -4);
4531
4532 } else if (ret != LUA_TTABLE) {
4533
4534 /* corruption error. */
4535 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4536 "is expected as an array. %s found", name, lua_typename(L, ret));
4537 WILL_LJMP(lua_error(L));
4538 }
4539
4540 /* Now the top od thestack is an array of values. We push
4541 * the header value as new entry.
4542 */
4543 lua_pushvalue(L, 3);
4544 ret = lua_rawlen(L, -2);
4545 lua_rawseti(L, -2, ret + 1);
4546 lua_pushboolean(L, 1);
4547 return 1;
4548}
4549
4550__LJMP static int hlua_applet_http_status(lua_State *L)
4551{
4552 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4553 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004554 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004555
4556 if (status < 100 || status > 599) {
4557 lua_pushboolean(L, 0);
4558 return 1;
4559 }
4560
4561 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004562 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004563 lua_pushboolean(L, 1);
4564 return 1;
4565}
4566
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004567
Christopher Fauleta2097962019-07-15 16:25:33 +02004568__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004569{
4570 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4571 struct stream_interface *si = appctx->appctx->owner;
4572 struct channel *res = si_ic(si);
4573 struct htx *htx;
4574 struct htx_sl *sl;
4575 struct h1m h1m;
4576 const char *status, *reason;
4577 const char *name, *value;
4578 size_t nlen, vlen;
4579 unsigned int flags;
4580
4581 /* Send the message at once. */
4582 htx = htx_from_buf(&res->buf);
4583 h1m_init_res(&h1m);
4584
4585 /* Use the same http version than the request. */
4586 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4587 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4588 if (reason == NULL)
4589 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4590 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4591 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4592 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4593 }
4594 else {
4595 flags = HTX_SL_F_IS_RESP;
4596 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4597 }
4598 if (!sl) {
4599 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4600 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4601 WILL_LJMP(lua_error(L));
4602 }
4603 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4604
4605 /* Get the array associated to the field "response" in the object AppletHTTP. */
4606 lua_pushvalue(L, 0);
4607 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4608 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4609 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4610 WILL_LJMP(lua_error(L));
4611 }
4612
4613 /* Browse the list of headers. */
4614 lua_pushnil(L);
4615 while(lua_next(L, -2) != 0) {
4616 /* We expect a string as -2. */
4617 if (lua_type(L, -2) != LUA_TSTRING) {
4618 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4619 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4620 lua_typename(L, lua_type(L, -2)));
4621 WILL_LJMP(lua_error(L));
4622 }
4623 name = lua_tolstring(L, -2, &nlen);
4624
4625 /* We expect an array as -1. */
4626 if (lua_type(L, -1) != LUA_TTABLE) {
4627 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4628 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4629 name,
4630 lua_typename(L, lua_type(L, -1)));
4631 WILL_LJMP(lua_error(L));
4632 }
4633
4634 /* Browse the table who is on the top of the stack. */
4635 lua_pushnil(L);
4636 while(lua_next(L, -2) != 0) {
4637 int id;
4638
4639 /* We expect a number as -2. */
4640 if (lua_type(L, -2) != LUA_TNUMBER) {
4641 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4642 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4643 name,
4644 lua_typename(L, lua_type(L, -2)));
4645 WILL_LJMP(lua_error(L));
4646 }
4647 id = lua_tointeger(L, -2);
4648
4649 /* We expect a string as -2. */
4650 if (lua_type(L, -1) != LUA_TSTRING) {
4651 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4652 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4653 name, id,
4654 lua_typename(L, lua_type(L, -1)));
4655 WILL_LJMP(lua_error(L));
4656 }
4657 value = lua_tolstring(L, -1, &vlen);
4658
4659 /* Simple Protocol checks. */
4660 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004661 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004662 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4663 struct ist v = ist2(value, vlen);
4664 int ret;
4665
4666 ret = h1_parse_cont_len_header(&h1m, &v);
4667 if (ret < 0) {
4668 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4669 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4670 name);
4671 WILL_LJMP(lua_error(L));
4672 }
4673 else if (ret == 0)
4674 goto next; /* Skip it */
4675 }
4676
4677 /* Add a new header */
4678 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4679 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4680 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4681 name);
4682 WILL_LJMP(lua_error(L));
4683 }
4684 next:
4685 /* Remove the array from the stack, and get next element with a remaining string. */
4686 lua_pop(L, 1);
4687 }
4688
4689 /* Remove the array from the stack, and get next element with a remaining string. */
4690 lua_pop(L, 1);
4691 }
4692
4693 if (h1m.flags & H1_MF_CHNK)
4694 h1m.flags &= ~H1_MF_CLEN;
4695 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4696 h1m.flags |= H1_MF_XFER_LEN;
4697
4698 /* Uset HTX start-line flags */
4699 if (h1m.flags & H1_MF_XFER_ENC)
4700 flags |= HTX_SL_F_XFER_ENC;
4701 if (h1m.flags & H1_MF_XFER_LEN) {
4702 flags |= HTX_SL_F_XFER_LEN;
4703 if (h1m.flags & H1_MF_CHNK)
4704 flags |= HTX_SL_F_CHNK;
4705 else if (h1m.flags & H1_MF_CLEN)
4706 flags |= HTX_SL_F_CLEN;
4707 if (h1m.body_len == 0)
4708 flags |= HTX_SL_F_BODYLESS;
4709 }
4710 sl->flags |= flags;
4711
4712 /* If we dont have a content-length set, and the HTTP version is 1.1
4713 * and the status code implies the presence of a message body, we must
4714 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004715 * for the keepalive compliance. If the applet announces a transfer-encoding
4716 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004717 */
4718 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4719 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4720 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4721 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4722 /* Add a new header */
4723 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4724 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4725 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4726 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4727 WILL_LJMP(lua_error(L));
4728 }
4729 }
4730
4731 /* Finalize headers. */
4732 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4733 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4734 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4735 WILL_LJMP(lua_error(L));
4736 }
4737
4738 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4739 b_reset(&res->buf);
4740 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4741 WILL_LJMP(lua_error(L));
4742 }
4743
4744 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004745 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004746
4747 /* Headers sent, set the flag. */
4748 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4749 return 0;
4750
4751}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004752/* We will build the status line and the headers of the HTTP response.
4753 * We will try send at once if its not possible, we give back the hand
4754 * waiting for more room.
4755 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004756__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004757{
4758 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4759 struct stream_interface *si = appctx->appctx->owner;
4760 struct channel *res = si_ic(si);
4761
4762 if (co_data(res)) {
4763 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004764 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004765 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004766 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004767}
4768
4769
Christopher Fauleta2097962019-07-15 16:25:33 +02004770__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004771{
Christopher Fauleta2097962019-07-15 16:25:33 +02004772 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004773}
4774
Christopher Fauleta2097962019-07-15 16:25:33 +02004775/*
4776 *
4777 *
4778 * Class HTTP
4779 *
4780 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004781 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004782
4783/* Returns a struct hlua_txn if the stack entry "ud" is
4784 * a class stream, otherwise it throws an error.
4785 */
4786__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004787{
Christopher Fauleta2097962019-07-15 16:25:33 +02004788 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4789}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004790
Christopher Fauleta2097962019-07-15 16:25:33 +02004791/* This function creates and push in the stack a HTTP object
4792 * according with a current TXN.
4793 */
4794static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4795{
4796 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004797
Christopher Fauleta2097962019-07-15 16:25:33 +02004798 /* Check stack size. */
4799 if (!lua_checkstack(L, 3))
4800 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004801
Christopher Fauleta2097962019-07-15 16:25:33 +02004802 /* Create the object: obj[0] = userdata.
4803 * Note that the base of the Converters object is the
4804 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004805 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004806 lua_newtable(L);
4807 htxn = lua_newuserdata(L, sizeof(*htxn));
4808 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004809
4810 htxn->s = txn->s;
4811 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004812 htxn->dir = txn->dir;
4813 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004814
4815 /* Pop a class stream metatable and affect it to the table. */
4816 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4817 lua_setmetatable(L, -2);
4818
4819 return 1;
4820}
4821
4822/* This function creates ans returns an array of HTTP headers.
4823 * This function does not fails. It is used as wrapper with the
4824 * 2 following functions.
4825 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004826__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004827{
Christopher Fauleta2097962019-07-15 16:25:33 +02004828 struct htx *htx;
4829 int32_t pos;
4830
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004831 /* Create the table. */
4832 lua_newtable(L);
4833
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004834
Christopher Fauleta2097962019-07-15 16:25:33 +02004835 htx = htxbuf(&msg->chn->buf);
4836 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4837 struct htx_blk *blk = htx_get_blk(htx, pos);
4838 enum htx_blk_type type = htx_get_blk_type(blk);
4839 struct ist n, v;
4840 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004841
Christopher Fauleta2097962019-07-15 16:25:33 +02004842 if (type == HTX_BLK_HDR) {
4843 n = htx_get_blk_name(htx,blk);
4844 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004845 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004846 else if (type == HTX_BLK_EOH)
4847 break;
4848 else
4849 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004850
Christopher Fauleta2097962019-07-15 16:25:33 +02004851 /* Check for existing entry:
4852 * assume that the table is on the top of the stack, and
4853 * push the key in the stack, the function lua_gettable()
4854 * perform the lookup.
4855 */
4856 lua_pushlstring(L, n.ptr, n.len);
4857 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004858
Christopher Fauleta2097962019-07-15 16:25:33 +02004859 switch (lua_type(L, -1)) {
4860 case LUA_TNIL:
4861 /* Table not found, create it. */
4862 lua_pop(L, 1); /* remove the nil value. */
4863 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4864 lua_newtable(L); /* create and push empty table. */
4865 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4866 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4867 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004868 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004869
Christopher Fauleta2097962019-07-15 16:25:33 +02004870 case LUA_TTABLE:
4871 /* Entry found: push the value in the table. */
4872 len = lua_rawlen(L, -1);
4873 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4874 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4875 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4876 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004877
Christopher Fauleta2097962019-07-15 16:25:33 +02004878 default:
4879 /* Other cases are errors. */
4880 hlua_pusherror(L, "internal error during the parsing of headers.");
4881 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004882 }
4883 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004884 return 1;
4885}
4886
4887__LJMP static int hlua_http_req_get_headers(lua_State *L)
4888{
4889 struct hlua_txn *htxn;
4890
4891 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4892 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4893
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004894 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004895 WILL_LJMP(lua_error(L));
4896
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004897 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004898}
4899
4900__LJMP static int hlua_http_res_get_headers(lua_State *L)
4901{
4902 struct hlua_txn *htxn;
4903
4904 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4905 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4906
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004907 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004908 WILL_LJMP(lua_error(L));
4909
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004910 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004911}
4912
4913/* This function replace full header, or just a value in
4914 * the request or in the response. It is a wrapper fir the
4915 * 4 following functions.
4916 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004917__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004918{
4919 size_t name_len;
4920 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4921 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4922 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004923 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004924 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004925
Dragan Dosen26743032019-04-30 15:54:36 +02004926 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004927 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4928
Christopher Fauleta2097962019-07-15 16:25:33 +02004929 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004930 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004931 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004932 return 0;
4933}
4934
4935__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4936{
4937 struct hlua_txn *htxn;
4938
4939 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4940 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4941
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004942 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004943 WILL_LJMP(lua_error(L));
4944
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004945 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004946}
4947
4948__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4949{
4950 struct hlua_txn *htxn;
4951
4952 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4953 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4954
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004955 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004956 WILL_LJMP(lua_error(L));
4957
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004958 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004959}
4960
4961__LJMP static int hlua_http_req_rep_val(lua_State *L)
4962{
4963 struct hlua_txn *htxn;
4964
4965 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4966 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4967
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004968 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004969 WILL_LJMP(lua_error(L));
4970
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004971 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004972}
4973
4974__LJMP static int hlua_http_res_rep_val(lua_State *L)
4975{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004976 struct hlua_txn *htxn;
4977
4978 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4979 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4980
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004981 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004982 WILL_LJMP(lua_error(L));
4983
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004984 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004985}
4986
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004987/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004988 * It is a wrapper for the 2 following functions.
4989 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004990__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004991{
4992 size_t len;
4993 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004994 struct htx *htx = htxbuf(&msg->chn->buf);
4995 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004996
Christopher Fauleta2097962019-07-15 16:25:33 +02004997 ctx.blk = NULL;
4998 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4999 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005000 return 0;
5001}
5002
5003__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5004{
5005 struct hlua_txn *htxn;
5006
5007 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5008 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5009
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005010 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005011 WILL_LJMP(lua_error(L));
5012
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005013 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005014}
5015
5016__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5017{
5018 struct hlua_txn *htxn;
5019
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005020 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005021 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5022
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005023 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005024 WILL_LJMP(lua_error(L));
5025
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005026 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005027}
5028
5029/* This function adds an header. It is a wrapper used by
5030 * the 2 following functions.
5031 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005032__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005033{
5034 size_t name_len;
5035 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5036 size_t value_len;
5037 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005038 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005039
Christopher Fauleta2097962019-07-15 16:25:33 +02005040 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5041 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005042 return 0;
5043}
5044
5045__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5046{
5047 struct hlua_txn *htxn;
5048
5049 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5050 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5051
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005052 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005053 WILL_LJMP(lua_error(L));
5054
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005055 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005056}
5057
5058__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5059{
5060 struct hlua_txn *htxn;
5061
5062 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5063 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5064
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005065 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005066 WILL_LJMP(lua_error(L));
5067
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005068 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005069}
5070
5071static int hlua_http_req_set_hdr(lua_State *L)
5072{
5073 struct hlua_txn *htxn;
5074
5075 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5076 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5077
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005078 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005079 WILL_LJMP(lua_error(L));
5080
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005081 hlua_http_del_hdr(L, &htxn->s->txn->req);
5082 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005083}
5084
5085static int hlua_http_res_set_hdr(lua_State *L)
5086{
5087 struct hlua_txn *htxn;
5088
5089 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5090 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5091
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005092 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005093 WILL_LJMP(lua_error(L));
5094
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005095 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5096 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005097}
5098
5099/* This function set the method. */
5100static int hlua_http_req_set_meth(lua_State *L)
5101{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005102 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005103 size_t name_len;
5104 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005105
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005106 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005107 WILL_LJMP(lua_error(L));
5108
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005109 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005110 return 1;
5111}
5112
5113/* This function set the method. */
5114static int hlua_http_req_set_path(lua_State *L)
5115{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005116 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005117 size_t name_len;
5118 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005119
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005120 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005121 WILL_LJMP(lua_error(L));
5122
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005123 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005124 return 1;
5125}
5126
5127/* This function set the query-string. */
5128static int hlua_http_req_set_query(lua_State *L)
5129{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005130 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005131 size_t name_len;
5132 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005133
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005134 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005135 WILL_LJMP(lua_error(L));
5136
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005137 /* Check length. */
5138 if (name_len > trash.size - 1) {
5139 lua_pushboolean(L, 0);
5140 return 1;
5141 }
5142
5143 /* Add the mark question as prefix. */
5144 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005145 trash.area[trash.data++] = '?';
5146 memcpy(trash.area + trash.data, name, name_len);
5147 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005148
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005149 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005150 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005151 return 1;
5152}
5153
5154/* This function set the uri. */
5155static int hlua_http_req_set_uri(lua_State *L)
5156{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005157 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005158 size_t name_len;
5159 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005160
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005161 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005162 WILL_LJMP(lua_error(L));
5163
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005164 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005165 return 1;
5166}
5167
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005168/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005169static int hlua_http_res_set_status(lua_State *L)
5170{
5171 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5172 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005173 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5174 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005175
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005176 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005177 WILL_LJMP(lua_error(L));
5178
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005179 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005180 return 0;
5181}
5182
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005183/*
5184 *
5185 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005186 * Class TXN
5187 *
5188 *
5189 */
5190
5191/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005192 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005193 */
5194__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5195{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005196 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005197}
5198
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005199__LJMP static int hlua_set_var(lua_State *L)
5200{
5201 struct hlua_txn *htxn;
5202 const char *name;
5203 size_t len;
5204 struct sample smp;
5205
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005206 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5207 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005208
5209 /* It is useles to retrieve the stream, but this function
5210 * runs only in a stream context.
5211 */
5212 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5213 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5214
5215 /* Converts the third argument in a sample. */
5216 hlua_lua2smp(L, 3, &smp);
5217
5218 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005219 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005220
5221 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5222 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5223 else
5224 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5225
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005226 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005227}
5228
Christopher Faulet85d79c92016-11-09 16:54:56 +01005229__LJMP static int hlua_unset_var(lua_State *L)
5230{
5231 struct hlua_txn *htxn;
5232 const char *name;
5233 size_t len;
5234 struct sample smp;
5235
5236 MAY_LJMP(check_args(L, 2, "unset_var"));
5237
5238 /* It is useles to retrieve the stream, but this function
5239 * runs only in a stream context.
5240 */
5241 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5242 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5243
5244 /* Unset the variable. */
5245 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005246 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5247 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005248}
5249
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005250__LJMP static int hlua_get_var(lua_State *L)
5251{
5252 struct hlua_txn *htxn;
5253 const char *name;
5254 size_t len;
5255 struct sample smp;
5256
5257 MAY_LJMP(check_args(L, 2, "get_var"));
5258
5259 /* It is useles to retrieve the stream, but this function
5260 * runs only in a stream context.
5261 */
5262 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5263 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5264
Willy Tarreau7560dd42016-03-10 16:28:58 +01005265 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005266 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005267 lua_pushnil(L);
5268 return 1;
5269 }
5270
5271 return hlua_smp2lua(L, &smp);
5272}
5273
Willy Tarreau59551662015-03-10 14:23:13 +01005274__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005275{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005276 struct hlua *hlua;
5277
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005278 MAY_LJMP(check_args(L, 2, "set_priv"));
5279
Willy Tarreau87b09662015-04-03 00:22:06 +02005280 /* It is useles to retrieve the stream, but this function
5281 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005282 */
5283 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005284 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005285
5286 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005287 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005288
5289 /* Get and store new value. */
5290 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5291 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5292
5293 return 0;
5294}
5295
Willy Tarreau59551662015-03-10 14:23:13 +01005296__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005297{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005298 struct hlua *hlua;
5299
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005300 MAY_LJMP(check_args(L, 1, "get_priv"));
5301
Willy Tarreau87b09662015-04-03 00:22:06 +02005302 /* It is useles to retrieve the stream, but this function
5303 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005304 */
5305 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005306 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005307
5308 /* Push configuration index in the stack. */
5309 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5310
5311 return 1;
5312}
5313
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005314/* Create stack entry containing a class TXN. This function
5315 * return 0 if the stack does not contains free slots,
5316 * otherwise it returns 1.
5317 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005318static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005319{
Willy Tarreaude491382015-04-06 11:04:28 +02005320 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005321
5322 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005323 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005324 return 0;
5325
5326 /* NOTE: The allocation never fails. The failure
5327 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005328 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005329 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005330 /* Create the object: obj[0] = userdata. */
5331 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005332 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005333 lua_rawseti(L, -2, 0);
5334
Willy Tarreaude491382015-04-06 11:04:28 +02005335 htxn->s = s;
5336 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005337 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005338 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005339
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005340 /* Create the "f" field that contains a list of fetches. */
5341 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005342 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005343 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005344 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005345
5346 /* Create the "sf" field that contains a list of stringsafe fetches. */
5347 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005348 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005349 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005350 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005351
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005352 /* Create the "c" field that contains a list of converters. */
5353 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005354 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005355 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005356 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005357
5358 /* Create the "sc" field that contains a list of stringsafe converters. */
5359 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005360 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005361 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005362 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005363
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005364 /* Create the "req" field that contains the request channel object. */
5365 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005366 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005367 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005368 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005369
5370 /* Create the "res" field that contains the response channel object. */
5371 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005372 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005373 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005374 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005375
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005376 /* Creates the HTTP object is the current proxy allows http. */
5377 lua_pushstring(L, "http");
5378 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005379 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005380 return 0;
5381 }
5382 else
5383 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005384 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005385
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005386 /* Pop a class sesison metatable and affect it to the userdata. */
5387 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5388 lua_setmetatable(L, -2);
5389
5390 return 1;
5391}
5392
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005393__LJMP static int hlua_txn_deflog(lua_State *L)
5394{
5395 const char *msg;
5396 struct hlua_txn *htxn;
5397
5398 MAY_LJMP(check_args(L, 2, "deflog"));
5399 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5400 msg = MAY_LJMP(luaL_checkstring(L, 2));
5401
5402 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5403 return 0;
5404}
5405
5406__LJMP static int hlua_txn_log(lua_State *L)
5407{
5408 int level;
5409 const char *msg;
5410 struct hlua_txn *htxn;
5411
5412 MAY_LJMP(check_args(L, 3, "log"));
5413 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5414 level = MAY_LJMP(luaL_checkinteger(L, 2));
5415 msg = MAY_LJMP(luaL_checkstring(L, 3));
5416
5417 if (level < 0 || level >= NB_LOG_LEVELS)
5418 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5419
5420 hlua_sendlog(htxn->s->be, level, msg);
5421 return 0;
5422}
5423
5424__LJMP static int hlua_txn_log_debug(lua_State *L)
5425{
5426 const char *msg;
5427 struct hlua_txn *htxn;
5428
5429 MAY_LJMP(check_args(L, 2, "Debug"));
5430 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5431 msg = MAY_LJMP(luaL_checkstring(L, 2));
5432 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5433 return 0;
5434}
5435
5436__LJMP static int hlua_txn_log_info(lua_State *L)
5437{
5438 const char *msg;
5439 struct hlua_txn *htxn;
5440
5441 MAY_LJMP(check_args(L, 2, "Info"));
5442 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5443 msg = MAY_LJMP(luaL_checkstring(L, 2));
5444 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5445 return 0;
5446}
5447
5448__LJMP static int hlua_txn_log_warning(lua_State *L)
5449{
5450 const char *msg;
5451 struct hlua_txn *htxn;
5452
5453 MAY_LJMP(check_args(L, 2, "Warning"));
5454 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5455 msg = MAY_LJMP(luaL_checkstring(L, 2));
5456 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5457 return 0;
5458}
5459
5460__LJMP static int hlua_txn_log_alert(lua_State *L)
5461{
5462 const char *msg;
5463 struct hlua_txn *htxn;
5464
5465 MAY_LJMP(check_args(L, 2, "Alert"));
5466 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5467 msg = MAY_LJMP(luaL_checkstring(L, 2));
5468 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5469 return 0;
5470}
5471
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005472__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5473{
5474 struct hlua_txn *htxn;
5475 int ll;
5476
5477 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5478 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5479 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5480
5481 if (ll < 0 || ll > 7)
5482 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5483
5484 htxn->s->logs.level = ll;
5485 return 0;
5486}
5487
5488__LJMP static int hlua_txn_set_tos(lua_State *L)
5489{
5490 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005491 int tos;
5492
5493 MAY_LJMP(check_args(L, 2, "set_tos"));
5494 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5495 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5496
Willy Tarreau1a18b542018-12-11 16:37:42 +01005497 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005498 return 0;
5499}
5500
5501__LJMP static int hlua_txn_set_mark(lua_State *L)
5502{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005503 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005504 int mark;
5505
5506 MAY_LJMP(check_args(L, 2, "set_mark"));
5507 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5508 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5509
Lukas Tribus579e3e32019-08-11 18:03:45 +02005510 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005511 return 0;
5512}
5513
Patrick Hemmer268a7072018-05-11 12:52:31 -04005514__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5515{
5516 struct hlua_txn *htxn;
5517
5518 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5519 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5520 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5521 return 0;
5522}
5523
5524__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5525{
5526 struct hlua_txn *htxn;
5527
5528 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5529 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5530 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5531 return 0;
5532}
5533
Christopher Faulet700d9e82020-01-31 12:21:52 +01005534/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005535 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005536 * message and terminate the transaction. It returns 1 on success and 0 on
5537 * error. The Reply must be on top of the stack.
5538 */
5539__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5540{
5541 struct htx *htx;
5542 struct htx_sl *sl;
5543 struct h1m h1m;
5544 const char *status, *reason, *body;
5545 size_t status_len, reason_len, body_len;
5546 int ret, code, flags;
5547
5548 code = 200;
5549 status = "200";
5550 status_len = 3;
5551 ret = lua_getfield(L, -1, "status");
5552 if (ret == LUA_TNUMBER) {
5553 code = lua_tointeger(L, -1);
5554 status = lua_tolstring(L, -1, &status_len);
5555 }
5556 lua_pop(L, 1);
5557
5558 reason = http_get_reason(code);
5559 reason_len = strlen(reason);
5560 ret = lua_getfield(L, -1, "reason");
5561 if (ret == LUA_TSTRING)
5562 reason = lua_tolstring(L, -1, &reason_len);
5563 lua_pop(L, 1);
5564
5565 body = NULL;
5566 body_len = 0;
5567 ret = lua_getfield(L, -1, "body");
5568 if (ret == LUA_TSTRING)
5569 body = lua_tolstring(L, -1, &body_len);
5570 lua_pop(L, 1);
5571
5572 /* Prepare the response before inserting the headers */
5573 h1m_init_res(&h1m);
5574 htx = htx_from_buf(&s->res.buf);
5575 channel_htx_truncate(&s->res, htx);
5576 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5577 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5578 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5579 ist2(status, status_len), ist2(reason, reason_len));
5580 }
5581 else {
5582 flags = HTX_SL_F_IS_RESP;
5583 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5584 ist2(status, status_len), ist2(reason, reason_len));
5585 }
5586 if (!sl)
5587 goto fail;
5588 sl->info.res.status = code;
5589
5590 /* Push in the stack the "headers" entry. */
5591 ret = lua_getfield(L, -1, "headers");
5592 if (ret != LUA_TTABLE)
5593 goto skip_headers;
5594
5595 lua_pushnil(L);
5596 while (lua_next(L, -2) != 0) {
5597 struct ist name, value;
5598 const char *n, *v;
5599 size_t nlen, vlen;
5600
5601 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5602 /* Skip element if the key is not a string or if the value is not a table */
5603 goto next_hdr;
5604 }
5605
5606 n = lua_tolstring(L, -2, &nlen);
5607 name = ist2(n, nlen);
5608 if (isteqi(name, ist("content-length"))) {
5609 /* Always skip content-length header. It will be added
5610 * later with the correct len
5611 */
5612 goto next_hdr;
5613 }
5614
5615 /* Loop on header's values */
5616 lua_pushnil(L);
5617 while (lua_next(L, -2)) {
5618 if (!lua_isstring(L, -1)) {
5619 /* Skip the value if it is not a string */
5620 goto next_value;
5621 }
5622
5623 v = lua_tolstring(L, -1, &vlen);
5624 value = ist2(v, vlen);
5625
5626 if (isteqi(name, ist("transfer-encoding")))
5627 h1_parse_xfer_enc_header(&h1m, value);
5628 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5629 goto fail;
5630
5631 next_value:
5632 lua_pop(L, 1);
5633 }
5634
5635 next_hdr:
5636 lua_pop(L, 1);
5637 }
5638 skip_headers:
5639 lua_pop(L, 1);
5640
5641 /* Update h1m flags: CLEN is set if CHNK is not present */
5642 if (!(h1m.flags & H1_MF_CHNK)) {
5643 const char *clen = ultoa(body_len);
5644
5645 h1m.flags |= H1_MF_CLEN;
5646 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5647 goto fail;
5648 }
5649 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5650 h1m.flags |= H1_MF_XFER_LEN;
5651
5652 /* Update HTX start-line flags */
5653 if (h1m.flags & H1_MF_XFER_ENC)
5654 flags |= HTX_SL_F_XFER_ENC;
5655 if (h1m.flags & H1_MF_XFER_LEN) {
5656 flags |= HTX_SL_F_XFER_LEN;
5657 if (h1m.flags & H1_MF_CHNK)
5658 flags |= HTX_SL_F_CHNK;
5659 else if (h1m.flags & H1_MF_CLEN)
5660 flags |= HTX_SL_F_CLEN;
5661 if (h1m.body_len == 0)
5662 flags |= HTX_SL_F_BODYLESS;
5663 }
5664 sl->flags |= flags;
5665
5666
5667 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5668 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5669 !htx_add_endof(htx, HTX_BLK_EOM))
5670 goto fail;
5671
5672 /* Now, forward the response and terminate the transaction */
5673 s->txn->status = code;
5674 htx_to_buf(htx, &s->res.buf);
5675 if (!http_forward_proxy_resp(s, 1))
5676 goto fail;
5677
5678 return 1;
5679
5680 fail:
5681 channel_htx_truncate(&s->res, htx);
5682 return 0;
5683}
5684
5685/* Terminate a transaction if called from a lua action. For TCP streams,
5686 * processing is just aborted. Nothing is returned to the client and all
5687 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5688 * is forwarded to the client before terminating the transaction. On success,
5689 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5690 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5691 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005692 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005693__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005694{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005695 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005696 struct stream *s;
5697 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005698
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005699 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005700
Christopher Faulet700d9e82020-01-31 12:21:52 +01005701 /* If the flags NOTERM is set, we cannot terminate the session, so we
5702 * just end the execution of the current lua code. */
5703 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005704 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005705
Christopher Faulet700d9e82020-01-31 12:21:52 +01005706 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005707 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005708 struct channel *req = &s->req;
5709 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005710
Christopher Faulet700d9e82020-01-31 12:21:52 +01005711 channel_auto_read(req);
5712 channel_abort(req);
5713 channel_auto_close(req);
5714 channel_erase(req);
5715
5716 res->wex = tick_add_ifset(now_ms, res->wto);
5717 channel_auto_read(res);
5718 channel_auto_close(res);
5719 channel_shutr_now(res);
5720
5721 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5722 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005723 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005724
Christopher Faulet700d9e82020-01-31 12:21:52 +01005725 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5726 /* No reply or invalid reply */
5727 s->txn->status = 0;
5728 http_reply_and_close(s, 0, NULL);
5729 }
5730 else {
5731 /* Remove extra args to have the reply on top of the stack */
5732 if (lua_gettop(L) > 2)
5733 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005734
Christopher Faulet700d9e82020-01-31 12:21:52 +01005735 if (!hlua_txn_forward_reply(L, s)) {
5736 if (!(s->flags & SF_ERR_MASK))
5737 s->flags |= SF_ERR_PRXCOND;
5738 lua_pushinteger(L, ACT_RET_ERR);
5739 WILL_LJMP(hlua_done(L));
5740 return 0; /* Never reached */
5741 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005742 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005743
Christopher Faulet700d9e82020-01-31 12:21:52 +01005744 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5745 if (htxn->dir == SMP_OPT_DIR_REQ) {
5746 /* let's log the request time */
5747 s->logs.tv_request = now;
5748 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5749 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5750 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005751
Christopher Faulet700d9e82020-01-31 12:21:52 +01005752 done:
5753 if (!(s->flags & SF_ERR_MASK))
5754 s->flags |= SF_ERR_LOCAL;
5755 if (!(s->flags & SF_FINST_MASK))
5756 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005757
Christopher Faulet4ad73102020-03-05 11:07:31 +01005758 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005759 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005760 return 0;
5761}
5762
Christopher Faulet700d9e82020-01-31 12:21:52 +01005763/*
5764 *
5765 *
5766 * Class REPLY
5767 *
5768 *
5769 */
5770
5771/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5772 * free slots, the function fails and returns 0;
5773 */
5774static int hlua_txn_reply_new(lua_State *L)
5775{
5776 struct hlua_txn *htxn;
5777 const char *reason, *body = NULL;
5778 int ret, status;
5779
5780 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005781 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005782 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5783 WILL_LJMP(lua_error(L));
5784 }
5785
5786 /* Default value */
5787 status = 200;
5788 reason = http_get_reason(status);
5789
5790 if (lua_istable(L, 2)) {
5791 /* load status and reason from the table argument at index 2 */
5792 ret = lua_getfield(L, 2, "status");
5793 if (ret == LUA_TNIL)
5794 goto reason;
5795 else if (ret != LUA_TNUMBER) {
5796 /* invalid status: ignore the reason */
5797 goto body;
5798 }
5799 status = lua_tointeger(L, -1);
5800
5801 reason:
5802 lua_pop(L, 1); /* restore the stack: remove status */
5803 ret = lua_getfield(L, 2, "reason");
5804 if (ret == LUA_TSTRING)
5805 reason = lua_tostring(L, -1);
5806
5807 body:
5808 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5809 ret = lua_getfield(L, 2, "body");
5810 if (ret == LUA_TSTRING)
5811 body = lua_tostring(L, -1);
5812 lua_pop(L, 1); /* restore the stack: remove body */
5813 }
5814
5815 /* Create the Reply table */
5816 lua_newtable(L);
5817
5818 /* Add status element */
5819 lua_pushstring(L, "status");
5820 lua_pushinteger(L, status);
5821 lua_settable(L, -3);
5822
5823 /* Add reason element */
5824 reason = http_get_reason(status);
5825 lua_pushstring(L, "reason");
5826 lua_pushstring(L, reason);
5827 lua_settable(L, -3);
5828
5829 /* Add body element, nil if undefined */
5830 lua_pushstring(L, "body");
5831 if (body)
5832 lua_pushstring(L, body);
5833 else
5834 lua_pushnil(L);
5835 lua_settable(L, -3);
5836
5837 /* Add headers element */
5838 lua_pushstring(L, "headers");
5839 lua_newtable(L);
5840
5841 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5842 if (lua_istable(L, 2)) {
5843 /* load headers from the table argument at index 2. If it is a table, copy it. */
5844 ret = lua_getfield(L, 2, "headers");
5845 if (ret == LUA_TTABLE) {
5846 /* stack: [ ... <headers:table>, <table> ] */
5847 lua_pushnil(L);
5848 while (lua_next(L, -2) != 0) {
5849 /* stack: [ ... <headers:table>, <table>, k, v] */
5850 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5851 /* invalid value type, skip it */
5852 lua_pop(L, 1);
5853 continue;
5854 }
5855
5856
5857 /* Duplicate the key and swap it with the value. */
5858 lua_pushvalue(L, -2);
5859 lua_insert(L, -2);
5860 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5861
5862 lua_newtable(L);
5863 lua_insert(L, -2);
5864 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5865
5866 if (lua_isstring(L, -1)) {
5867 /* push the value in the inner table */
5868 lua_rawseti(L, -2, 1);
5869 }
5870 else { /* table */
5871 lua_pushnil(L);
5872 while (lua_next(L, -2) != 0) {
5873 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5874 if (!lua_isstring(L, -1)) {
5875 /* invalid value type, skip it*/
5876 lua_pop(L, 1);
5877 continue;
5878 }
5879 /* push the value in the inner table */
5880 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5881 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5882 }
5883 lua_pop(L, 1);
5884 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5885 }
5886
5887 /* push (k,v) on the stack in the headers table:
5888 * stack: [ ... <headers:table>, <table>, k, k, v ]
5889 */
5890 lua_settable(L, -5);
5891 /* stack: [ ... <headers:table>, <table>, k ] */
5892 }
5893 }
5894 lua_pop(L, 1);
5895 }
5896 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5897 lua_settable(L, -3);
5898 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5899
5900 /* Pop a class sesison metatable and affect it to the userdata. */
5901 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5902 lua_setmetatable(L, -2);
5903 return 1;
5904}
5905
5906/* Set the reply status code, and optionally the reason. If no reason is
5907 * provided, the default one corresponding to the status code is used.
5908 */
5909__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5910{
5911 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5912 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5913
5914 /* First argument (self) must be a table */
5915 luaL_checktype(L, 1, LUA_TTABLE);
5916
5917 if (status < 100 || status > 599) {
5918 lua_pushboolean(L, 0);
5919 return 1;
5920 }
5921 if (!reason)
5922 reason = http_get_reason(status);
5923
5924 lua_pushinteger(L, status);
5925 lua_setfield(L, 1, "status");
5926
5927 lua_pushstring(L, reason);
5928 lua_setfield(L, 1, "reason");
5929
5930 lua_pushboolean(L, 1);
5931 return 1;
5932}
5933
5934/* Add a header into the reply object. Each header name is associated to an
5935 * array of values in the "headers" table. If the header name is not found, a
5936 * new entry is created.
5937 */
5938__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5939{
5940 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5941 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5942 int ret;
5943
5944 /* First argument (self) must be a table */
5945 luaL_checktype(L, 1, LUA_TTABLE);
5946
5947 /* Push in the stack the "headers" entry. */
5948 ret = lua_getfield(L, 1, "headers");
5949 if (ret != LUA_TTABLE) {
5950 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5951 WILL_LJMP(lua_error(L));
5952 }
5953
5954 /* check if the header is already registered. If not, register it. */
5955 ret = lua_getfield(L, -1, name);
5956 if (ret == LUA_TNIL) {
5957 /* Entry not found. */
5958 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5959
5960 /* Insert the new header name in the array in the top of the stack.
5961 * It left the new array in the top of the stack.
5962 */
5963 lua_newtable(L);
5964 lua_pushstring(L, name);
5965 lua_pushvalue(L, -2);
5966 lua_settable(L, -4);
5967 }
5968 else if (ret != LUA_TTABLE) {
5969 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5970 WILL_LJMP(lua_error(L));
5971 }
5972
5973 /* Now the top od thestack is an array of values. We push
5974 * the header value as new entry.
5975 */
5976 lua_pushstring(L, value);
5977 ret = lua_rawlen(L, -2);
5978 lua_rawseti(L, -2, ret + 1);
5979
5980 lua_pushboolean(L, 1);
5981 return 1;
5982}
5983
5984/* Remove all occurrences of a given header name. */
5985__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5986{
5987 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5988 int ret;
5989
5990 /* First argument (self) must be a table */
5991 luaL_checktype(L, 1, LUA_TTABLE);
5992
5993 /* Push in the stack the "headers" entry. */
5994 ret = lua_getfield(L, 1, "headers");
5995 if (ret != LUA_TTABLE) {
5996 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5997 WILL_LJMP(lua_error(L));
5998 }
5999
6000 lua_pushstring(L, name);
6001 lua_pushnil(L);
6002 lua_settable(L, -3);
6003
6004 lua_pushboolean(L, 1);
6005 return 1;
6006}
6007
6008/* Set the reply's body. Overwrite any existing entry. */
6009__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6010{
6011 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6012
6013 /* First argument (self) must be a table */
6014 luaL_checktype(L, 1, LUA_TTABLE);
6015
6016 lua_pushstring(L, payload);
6017 lua_setfield(L, 1, "body");
6018
6019 lua_pushboolean(L, 1);
6020 return 1;
6021}
6022
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006023__LJMP static int hlua_log(lua_State *L)
6024{
6025 int level;
6026 const char *msg;
6027
6028 MAY_LJMP(check_args(L, 2, "log"));
6029 level = MAY_LJMP(luaL_checkinteger(L, 1));
6030 msg = MAY_LJMP(luaL_checkstring(L, 2));
6031
6032 if (level < 0 || level >= NB_LOG_LEVELS)
6033 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6034
6035 hlua_sendlog(NULL, level, msg);
6036 return 0;
6037}
6038
6039__LJMP static int hlua_log_debug(lua_State *L)
6040{
6041 const char *msg;
6042
6043 MAY_LJMP(check_args(L, 1, "debug"));
6044 msg = MAY_LJMP(luaL_checkstring(L, 1));
6045 hlua_sendlog(NULL, LOG_DEBUG, msg);
6046 return 0;
6047}
6048
6049__LJMP static int hlua_log_info(lua_State *L)
6050{
6051 const char *msg;
6052
6053 MAY_LJMP(check_args(L, 1, "info"));
6054 msg = MAY_LJMP(luaL_checkstring(L, 1));
6055 hlua_sendlog(NULL, LOG_INFO, msg);
6056 return 0;
6057}
6058
6059__LJMP static int hlua_log_warning(lua_State *L)
6060{
6061 const char *msg;
6062
6063 MAY_LJMP(check_args(L, 1, "warning"));
6064 msg = MAY_LJMP(luaL_checkstring(L, 1));
6065 hlua_sendlog(NULL, LOG_WARNING, msg);
6066 return 0;
6067}
6068
6069__LJMP static int hlua_log_alert(lua_State *L)
6070{
6071 const char *msg;
6072
6073 MAY_LJMP(check_args(L, 1, "alert"));
6074 msg = MAY_LJMP(luaL_checkstring(L, 1));
6075 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006076 return 0;
6077}
6078
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006079__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006080{
6081 int wakeup_ms = lua_tointeger(L, -1);
6082 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006083 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006084 return 0;
6085}
6086
6087__LJMP static int hlua_sleep(lua_State *L)
6088{
6089 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006090 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006091
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006092 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006093
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006094 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006095 wakeup_ms = tick_add(now_ms, delay);
6096 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006097
Willy Tarreau9635e032018-10-16 17:52:55 +02006098 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006099 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006100}
6101
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006102__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006103{
6104 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006105 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006106
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006107 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006108
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006109 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006110 wakeup_ms = tick_add(now_ms, delay);
6111 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006112
Willy Tarreau9635e032018-10-16 17:52:55 +02006113 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006114 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006115}
6116
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006117/* This functionis an LUA binding. it permits to give back
6118 * the hand at the HAProxy scheduler. It is used when the
6119 * LUA processing consumes a lot of time.
6120 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006121__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006122{
6123 return 0;
6124}
6125
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006126__LJMP static int hlua_yield(lua_State *L)
6127{
Willy Tarreau9635e032018-10-16 17:52:55 +02006128 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006129 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006130}
6131
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006132/* This function change the nice of the currently executed
6133 * task. It is used set low or high priority at the current
6134 * task.
6135 */
Willy Tarreau59551662015-03-10 14:23:13 +01006136__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006137{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006138 struct hlua *hlua;
6139 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006140
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006141 MAY_LJMP(check_args(L, 1, "set_nice"));
6142 hlua = hlua_gethlua(L);
6143 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006144
6145 /* If he task is not set, I'm in a start mode. */
6146 if (!hlua || !hlua->task)
6147 return 0;
6148
6149 if (nice < -1024)
6150 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006151 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006152 nice = 1024;
6153
6154 hlua->task->nice = nice;
6155 return 0;
6156}
6157
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006158/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006159 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6160 * return an E_AGAIN signal, the emmiter of this signal must set a
6161 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006162 *
6163 * Task wrapper are longjmp safe because the only one Lua code
6164 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006165 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006166struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006167{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006168 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006169 enum hlua_exec status;
6170
Christopher Faulet5bc99722018-04-25 10:34:45 +02006171 if (task->thread_mask == MAX_THREADS_MASK)
6172 task_set_affinity(task, tid_bit);
6173
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006174 /* If it is the first call to the task, we must initialize the
6175 * execution timeouts.
6176 */
6177 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006178 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006179
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006180 /* Execute the Lua code. */
6181 status = hlua_ctx_resume(hlua, 1);
6182
6183 switch (status) {
6184 /* finished or yield */
6185 case HLUA_E_OK:
6186 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006187 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006188 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006189 break;
6190
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006191 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006192 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006193 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006194 break;
6195
6196 /* finished with error. */
6197 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006198 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006199 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006200 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006201 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006202 break;
6203
6204 case HLUA_E_ERR:
6205 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006206 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006207 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006208 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006209 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006210 break;
6211 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006212 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006213}
6214
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006215/* This function is an LUA binding that register LUA function to be
6216 * executed after the HAProxy configuration parsing and before the
6217 * HAProxy scheduler starts. This function expect only one LUA
6218 * argument that is a function. This function returns nothing, but
6219 * throws if an error is encountered.
6220 */
6221__LJMP static int hlua_register_init(lua_State *L)
6222{
6223 struct hlua_init_function *init;
6224 int ref;
6225
6226 MAY_LJMP(check_args(L, 1, "register_init"));
6227
6228 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6229
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006230 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006231 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006232 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006233
6234 init->function_ref = ref;
6235 LIST_ADDQ(&hlua_init_functions, &init->l);
6236 return 0;
6237}
6238
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006239/* This functio is an LUA binding. It permits to register a task
6240 * executed in parallel of the main HAroxy activity. The task is
6241 * created and it is set in the HAProxy scheduler. It can be called
6242 * from the "init" section, "post init" or during the runtime.
6243 *
6244 * Lua prototype:
6245 *
6246 * <none> core.register_task(<function>)
6247 */
6248static int hlua_register_task(lua_State *L)
6249{
6250 struct hlua *hlua;
6251 struct task *task;
6252 int ref;
6253
6254 MAY_LJMP(check_args(L, 1, "register_task"));
6255
6256 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6257
Willy Tarreaubafbe012017-11-24 17:34:44 +01006258 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006259 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006260 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006261
Emeric Brunc60def82017-09-27 14:59:38 +02006262 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006263 if (!task)
6264 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6265
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006266 task->context = hlua;
6267 task->process = hlua_process_task;
6268
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006269 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006270 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006271
6272 /* Restore the function in the stack. */
6273 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6274 hlua->nargs = 0;
6275
6276 /* Schedule task. */
6277 task_schedule(task, now_ms);
6278
6279 return 0;
6280}
6281
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006282/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6283 * doesn't allow "yield" functions because the HAProxy engine cannot
6284 * resume converters.
6285 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006286static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006287{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006288 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006289 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006290 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006291
Willy Tarreaube508f12016-03-10 11:47:01 +01006292 if (!stream)
6293 return 0;
6294
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 converter '%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 converter '%s': can't initialize Lua context.\n", fcn->name);
6308 return 0;
6309 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006310 }
6311
Thierry FOURNIER9be813f2015-02-16 20:21:12 +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(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006322 return 0;
6323 }
6324
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006325 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006326 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006327 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006328 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +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 FOURNIER9be813f2015-02-16 20:21:12 +01006334
6335 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006336 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006337 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006338 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006339 return 0;
6340 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006341 hlua_smp2lua(stream->hlua->T, smp);
6342 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006343
6344 /* push keywords in the stack. */
6345 if (arg_p) {
6346 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006347 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006348 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006349 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006350 return 0;
6351 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006352 hlua_arg2lua(stream->hlua->T, arg_p);
6353 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006354 }
6355 }
6356
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006357 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006358 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006359
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006360 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006361 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006362 }
6363
6364 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006365 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006366 /* finished. */
6367 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006368 /* If the stack is empty, the function fails. */
6369 if (lua_gettop(stream->hlua->T) <= 0)
6370 return 0;
6371
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006372 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006373 hlua_lua2smp(stream->hlua->T, -1, smp);
6374 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006375 return 1;
6376
6377 /* yield. */
6378 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006379 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006380 return 0;
6381
6382 /* finished with error. */
6383 case HLUA_E_ERRMSG:
6384 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006385 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006386 fcn->name, lua_tostring(stream->hlua->T, -1));
6387 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006388 return 0;
6389
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006390 case HLUA_E_ETMOUT:
6391 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6392 return 0;
6393
6394 case HLUA_E_NOMEM:
6395 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6396 return 0;
6397
6398 case HLUA_E_YIELD:
6399 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6400 return 0;
6401
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006402 case HLUA_E_ERR:
6403 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006404 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006405 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006406
6407 default:
6408 return 0;
6409 }
6410}
6411
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006412/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6413 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006414 * resume sample-fetches. This function will be called by the sample
6415 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006416 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006417static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6418 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006419{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006420 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006421 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006422 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006423 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006424
Willy Tarreaube508f12016-03-10 11:47:01 +01006425 if (!stream)
6426 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006427
Willy Tarreau87b09662015-04-03 00:22:06 +02006428 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006429 * Lua context can be not initialized. This behavior
6430 * permits to save performances because a systematic
6431 * Lua initialization cause 5% performances loss.
6432 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006433 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006434 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006435 if (!stream->hlua) {
6436 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6437 return 0;
6438 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006439 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006440 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6441 return 0;
6442 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006443 }
6444
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006445 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006446 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006447
6448 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006449 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6450 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6451 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006452 else
6453 error = "critical error";
6454 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006455 return 0;
6456 }
6457
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006458 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006459 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006460 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006461 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006462 return 0;
6463 }
6464
6465 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006466 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006467
6468 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006469 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006470 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006471 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006472 return 0;
6473 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006474 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006475
6476 /* push keywords in the stack. */
6477 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6478 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006479 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006480 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006481 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006482 return 0;
6483 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006484 hlua_arg2lua(stream->hlua->T, arg_p);
6485 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006486 }
6487
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006488 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006489 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006490
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006491 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006492 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006493 }
6494
6495 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006496 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006497 /* finished. */
6498 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006499 /* If the stack is empty, the function fails. */
6500 if (lua_gettop(stream->hlua->T) <= 0)
6501 return 0;
6502
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006503 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006504 hlua_lua2smp(stream->hlua->T, -1, smp);
6505 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006506
6507 /* Set the end of execution flag. */
6508 smp->flags &= ~SMP_F_MAY_CHANGE;
6509 return 1;
6510
6511 /* yield. */
6512 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006513 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006514 return 0;
6515
6516 /* finished with error. */
6517 case HLUA_E_ERRMSG:
6518 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006519 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006520 fcn->name, lua_tostring(stream->hlua->T, -1));
6521 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006522 return 0;
6523
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006524 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006525 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6526 return 0;
6527
6528 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006529 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6530 return 0;
6531
6532 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006533 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6534 return 0;
6535
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006536 case HLUA_E_ERR:
6537 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006538 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006539 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006540
6541 default:
6542 return 0;
6543 }
6544}
6545
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006546/* This function is an LUA binding used for registering
6547 * "sample-conv" functions. It expects a converter name used
6548 * in the haproxy configuration file, and an LUA function.
6549 */
6550__LJMP static int hlua_register_converters(lua_State *L)
6551{
6552 struct sample_conv_kw_list *sck;
6553 const char *name;
6554 int ref;
6555 int len;
6556 struct hlua_function *fcn;
6557
6558 MAY_LJMP(check_args(L, 2, "register_converters"));
6559
6560 /* First argument : converter name. */
6561 name = MAY_LJMP(luaL_checkstring(L, 1));
6562
6563 /* Second argument : lua function. */
6564 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6565
6566 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006567 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006568 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006569 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006570 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006571 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006572 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006573
6574 /* Fill fcn. */
6575 fcn->name = strdup(name);
6576 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006577 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006578 fcn->function_ref = ref;
6579
6580 /* List head */
6581 sck->list.n = sck->list.p = NULL;
6582
6583 /* converter keyword. */
6584 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006585 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006586 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006587 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006588
6589 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6590 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006591 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 +01006592 sck->kw[0].val_args = NULL;
6593 sck->kw[0].in_type = SMP_T_STR;
6594 sck->kw[0].out_type = SMP_T_STR;
6595 sck->kw[0].private = fcn;
6596
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006597 /* Register this new converter */
6598 sample_register_convs(sck);
6599
6600 return 0;
6601}
6602
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006603/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006604 * "sample-fetch" functions. It expects a converter name used
6605 * in the haproxy configuration file, and an LUA function.
6606 */
6607__LJMP static int hlua_register_fetches(lua_State *L)
6608{
6609 const char *name;
6610 int ref;
6611 int len;
6612 struct sample_fetch_kw_list *sfk;
6613 struct hlua_function *fcn;
6614
6615 MAY_LJMP(check_args(L, 2, "register_fetches"));
6616
6617 /* First argument : sample-fetch name. */
6618 name = MAY_LJMP(luaL_checkstring(L, 1));
6619
6620 /* Second argument : lua function. */
6621 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6622
6623 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006624 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006625 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006626 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006627 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006628 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006629 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006630
6631 /* Fill fcn. */
6632 fcn->name = strdup(name);
6633 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006634 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006635 fcn->function_ref = ref;
6636
6637 /* List head */
6638 sfk->list.n = sfk->list.p = NULL;
6639
6640 /* sample-fetch keyword. */
6641 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006642 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006643 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006644 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006645
6646 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6647 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006648 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 +01006649 sfk->kw[0].val_args = NULL;
6650 sfk->kw[0].out_type = SMP_T_STR;
6651 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6652 sfk->kw[0].val = 0;
6653 sfk->kw[0].private = fcn;
6654
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006655 /* Register this new fetch. */
6656 sample_register_fetches(sfk);
6657
6658 return 0;
6659}
6660
Christopher Faulet501465d2020-02-26 14:54:16 +01006661/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006662 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006663__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006664{
6665 struct hlua *hlua = hlua_gethlua(L);
6666 unsigned int delay;
6667 unsigned int wakeup_ms;
6668
6669 MAY_LJMP(check_args(L, 1, "wake_time"));
6670
6671 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6672 wakeup_ms = tick_add(now_ms, delay);
6673 hlua->wake_time = wakeup_ms;
6674 return 0;
6675}
6676
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006677/* This function is a wrapper to execute each LUA function declared as an action
6678 * wrapper during the initialisation period. This function may return any
6679 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6680 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6681 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006682 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006683static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006684 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006685{
6686 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006687 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006688 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006689 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006690
6691 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006692 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6693 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6694 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6695 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006696 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006697 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006698 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006699 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006700
Willy Tarreau87b09662015-04-03 00:22:06 +02006701 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006702 * Lua context can be not initialized. This behavior
6703 * permits to save performances because a systematic
6704 * Lua initialization cause 5% performances loss.
6705 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006706 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006707 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006708 if (!s->hlua) {
6709 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6710 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006711 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006712 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006713 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006714 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6715 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006716 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006717 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006718 }
6719
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006720 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006721 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006722
6723 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006724 if (!SET_SAFE_LJMP(s->hlua->T)) {
6725 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6726 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006727 else
6728 error = "critical error";
6729 SEND_ERR(px, "Lua function '%s': %s.\n",
6730 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006731 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006732 }
6733
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006734 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006735 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006736 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006737 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006738 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006739 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006740 }
6741
6742 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006743 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006744
Willy Tarreau87b09662015-04-03 00:22:06 +02006745 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006746 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006747 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006748 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006749 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006750 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006751 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006752 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006753
6754 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006755 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006756 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006757 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006758 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006759 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006760 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006761 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006762 lua_pushstring(s->hlua->T, *arg);
6763 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006764 }
6765
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006766 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006767 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006768
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006769 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006770 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006771 }
6772
6773 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006774 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006775 /* finished. */
6776 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006777 /* Catch the return value */
6778 if (lua_gettop(s->hlua->T) > 0)
6779 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006780
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006781 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006782 if (act_ret == ACT_RET_YIELD) {
6783 if (flags & ACT_OPT_FINAL)
6784 goto err_yield;
6785
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006786 if (dir == SMP_OPT_DIR_REQ)
6787 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6788 s->hlua->wake_time);
6789 else
6790 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6791 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006792 }
6793 goto end;
6794
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006795 /* yield. */
6796 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006797 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006798 if (dir == SMP_OPT_DIR_REQ)
6799 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6800 s->hlua->wake_time);
6801 else
6802 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6803 s->hlua->wake_time);
6804
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006805 /* Some actions can be wake up when a "write" event
6806 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006807 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006808 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006809 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006810 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006811 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006812 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006813 act_ret = ACT_RET_YIELD;
6814 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006815
6816 /* finished with error. */
6817 case HLUA_E_ERRMSG:
6818 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006819 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006820 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6821 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006822 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006823
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006824 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006825 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006826 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006827
6828 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006829 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006830 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006831
6832 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02006833 err_yield:
6834 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006835 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6836 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006837 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006838
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006839 case HLUA_E_ERR:
6840 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006841 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006842 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006843
6844 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006845 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006846 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006847
6848 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02006849 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006850 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006851 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006852}
6853
Olivier Houchard9f6af332018-05-25 14:04:04 +02006854struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006855{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006856 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006857
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006858 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006859 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006860 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006861}
6862
6863static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6864{
6865 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006866 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006867 struct task *task;
6868 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006869 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006870
Willy Tarreaubafbe012017-11-24 17:34:44 +01006871 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006872 if (!hlua) {
6873 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6874 ctx->rule->arg.hlua_rule->fcn.name);
6875 return 0;
6876 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006877 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006878 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006879 ctx->ctx.hlua_apptcp.flags = 0;
6880
6881 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006882 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006883 if (!task) {
6884 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6885 ctx->rule->arg.hlua_rule->fcn.name);
6886 return 0;
6887 }
6888 task->nice = 0;
6889 task->context = ctx;
6890 task->process = hlua_applet_wakeup;
6891 ctx->ctx.hlua_apptcp.task = task;
6892
6893 /* In the execution wrappers linked with a stream, the
6894 * Lua context can be not initialized. This behavior
6895 * permits to save performances because a systematic
6896 * Lua initialization cause 5% performances loss.
6897 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006898 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006899 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6900 ctx->rule->arg.hlua_rule->fcn.name);
6901 return 0;
6902 }
6903
6904 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006905 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006906
6907 /* The following Lua calls can fail. */
6908 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006909 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6910 error = lua_tostring(hlua->T, -1);
6911 else
6912 error = "critical error";
6913 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6914 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006915 return 0;
6916 }
6917
6918 /* Check stack available size. */
6919 if (!lua_checkstack(hlua->T, 1)) {
6920 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6921 ctx->rule->arg.hlua_rule->fcn.name);
6922 RESET_SAFE_LJMP(hlua->T);
6923 return 0;
6924 }
6925
6926 /* Restore the function in the stack. */
6927 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6928
6929 /* Create and and push object stream in the stack. */
6930 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6931 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6932 ctx->rule->arg.hlua_rule->fcn.name);
6933 RESET_SAFE_LJMP(hlua->T);
6934 return 0;
6935 }
6936 hlua->nargs = 1;
6937
6938 /* push keywords in the stack. */
6939 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6940 if (!lua_checkstack(hlua->T, 1)) {
6941 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6942 ctx->rule->arg.hlua_rule->fcn.name);
6943 RESET_SAFE_LJMP(hlua->T);
6944 return 0;
6945 }
6946 lua_pushstring(hlua->T, *arg);
6947 hlua->nargs++;
6948 }
6949
6950 RESET_SAFE_LJMP(hlua->T);
6951
6952 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006953 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006954 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006955
6956 return 1;
6957}
6958
Willy Tarreau60409db2019-08-21 14:14:50 +02006959void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006960{
6961 struct stream_interface *si = ctx->owner;
6962 struct stream *strm = si_strm(si);
6963 struct channel *res = si_ic(si);
6964 struct act_rule *rule = ctx->rule;
6965 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006966 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006967
6968 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006969 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6970 /* eat the whole request */
6971 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006972 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006973 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006974
6975 /* If the stream is disconnect or closed, ldo nothing. */
6976 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6977 return;
6978
6979 /* Execute the function. */
6980 switch (hlua_ctx_resume(hlua, 1)) {
6981 /* finished. */
6982 case HLUA_E_OK:
6983 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6984
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006985 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006986 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006987 res->flags |= CF_READ_NULL;
6988 si_shutr(si);
6989 return;
6990
6991 /* yield. */
6992 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006993 if (hlua->wake_time != TICK_ETERNITY)
6994 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006995 return;
6996
6997 /* finished with error. */
6998 case HLUA_E_ERRMSG:
6999 /* Display log. */
7000 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7001 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7002 lua_pop(hlua->T, 1);
7003 goto error;
7004
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007005 case HLUA_E_ETMOUT:
7006 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
7007 rule->arg.hlua_rule->fcn.name);
7008 goto error;
7009
7010 case HLUA_E_NOMEM:
7011 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
7012 rule->arg.hlua_rule->fcn.name);
7013 goto error;
7014
7015 case HLUA_E_YIELD: /* unexpected */
7016 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
7017 rule->arg.hlua_rule->fcn.name);
7018 goto error;
7019
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007020 case HLUA_E_ERR:
7021 /* Display log. */
7022 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
7023 rule->arg.hlua_rule->fcn.name);
7024 goto error;
7025
7026 default:
7027 goto error;
7028 }
7029
7030error:
7031
7032 /* For all other cases, just close the stream. */
7033 si_shutw(si);
7034 si_shutr(si);
7035 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7036}
7037
7038static void hlua_applet_tcp_release(struct appctx *ctx)
7039{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007040 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007041 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007042 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007043 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007044}
7045
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007046/* The function returns 1 if the initialisation is complete, 0 if
7047 * an errors occurs and -1 if more data are required for initializing
7048 * the applet.
7049 */
7050static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7051{
7052 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007053 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007054 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007055 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007056 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007057 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007058
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007059 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007060 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007061 if (!hlua) {
7062 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7063 ctx->rule->arg.hlua_rule->fcn.name);
7064 return 0;
7065 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007066 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007067 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007068 ctx->ctx.hlua_apphttp.left_bytes = -1;
7069 ctx->ctx.hlua_apphttp.flags = 0;
7070
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007071 if (txn->req.flags & HTTP_MSGF_VER_11)
7072 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7073
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007074 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007075 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007076 if (!task) {
7077 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7078 ctx->rule->arg.hlua_rule->fcn.name);
7079 return 0;
7080 }
7081 task->nice = 0;
7082 task->context = ctx;
7083 task->process = hlua_applet_wakeup;
7084 ctx->ctx.hlua_apphttp.task = task;
7085
7086 /* In the execution wrappers linked with a stream, the
7087 * Lua context can be not initialized. This behavior
7088 * permits to save performances because a systematic
7089 * Lua initialization cause 5% performances loss.
7090 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007091 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007092 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
7093 ctx->rule->arg.hlua_rule->fcn.name);
7094 return 0;
7095 }
7096
7097 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007098 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007099
7100 /* The following Lua calls can fail. */
7101 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007102 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7103 error = lua_tostring(hlua->T, -1);
7104 else
7105 error = "critical error";
7106 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7107 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007108 return 0;
7109 }
7110
7111 /* Check stack available size. */
7112 if (!lua_checkstack(hlua->T, 1)) {
7113 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7114 ctx->rule->arg.hlua_rule->fcn.name);
7115 RESET_SAFE_LJMP(hlua->T);
7116 return 0;
7117 }
7118
7119 /* Restore the function in the stack. */
7120 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7121
7122 /* Create and and push object stream in the stack. */
7123 if (!hlua_applet_http_new(hlua->T, ctx)) {
7124 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7125 ctx->rule->arg.hlua_rule->fcn.name);
7126 RESET_SAFE_LJMP(hlua->T);
7127 return 0;
7128 }
7129 hlua->nargs = 1;
7130
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007131 /* push keywords in the stack. */
7132 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7133 if (!lua_checkstack(hlua->T, 1)) {
7134 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7135 ctx->rule->arg.hlua_rule->fcn.name);
7136 RESET_SAFE_LJMP(hlua->T);
7137 return 0;
7138 }
7139 lua_pushstring(hlua->T, *arg);
7140 hlua->nargs++;
7141 }
7142
7143 RESET_SAFE_LJMP(hlua->T);
7144
7145 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007146 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007147
7148 return 1;
7149}
7150
Willy Tarreau60409db2019-08-21 14:14:50 +02007151void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007152{
7153 struct stream_interface *si = ctx->owner;
7154 struct stream *strm = si_strm(si);
7155 struct channel *req = si_oc(si);
7156 struct channel *res = si_ic(si);
7157 struct act_rule *rule = ctx->rule;
7158 struct proxy *px = strm->be;
7159 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7160 struct htx *req_htx, *res_htx;
7161
7162 res_htx = htx_from_buf(&res->buf);
7163
7164 /* If the stream is disconnect or closed, ldo nothing. */
7165 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7166 goto out;
7167
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007168 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007169 if (!b_size(&res->buf)) {
7170 si_rx_room_blk(si);
7171 goto out;
7172 }
7173 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007174 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007175 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7176
7177 /* Set the currently running flag. */
7178 if (!HLUA_IS_RUNNING(hlua) &&
7179 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7180 struct htx_blk *blk;
7181 size_t count = co_data(req);
7182
7183 if (!count) {
7184 si_cant_get(si);
7185 goto out;
7186 }
7187
7188 /* We need to flush the request header. This left the body for
7189 * the Lua.
7190 */
7191 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007192 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007193 while (count && blk) {
7194 enum htx_blk_type type = htx_get_blk_type(blk);
7195 uint32_t sz = htx_get_blksz(blk);
7196
7197 if (sz > count) {
7198 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007199 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007200 goto out;
7201 }
7202
7203 count -= sz;
7204 co_set_data(req, co_data(req) - sz);
7205 blk = htx_remove_blk(req_htx, blk);
7206
7207 if (type == HTX_BLK_EOH)
7208 break;
7209 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007210 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007211 }
7212
7213 /* Executes The applet if it is not done. */
7214 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7215
7216 /* Execute the function. */
7217 switch (hlua_ctx_resume(hlua, 1)) {
7218 /* finished. */
7219 case HLUA_E_OK:
7220 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7221 break;
7222
7223 /* yield. */
7224 case HLUA_E_AGAIN:
7225 if (hlua->wake_time != TICK_ETERNITY)
7226 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007227 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007228
7229 /* finished with error. */
7230 case HLUA_E_ERRMSG:
7231 /* Display log. */
7232 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7233 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7234 lua_pop(hlua->T, 1);
7235 goto error;
7236
7237 case HLUA_E_ETMOUT:
7238 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7239 rule->arg.hlua_rule->fcn.name);
7240 goto error;
7241
7242 case HLUA_E_NOMEM:
7243 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7244 rule->arg.hlua_rule->fcn.name);
7245 goto error;
7246
7247 case HLUA_E_YIELD: /* unexpected */
7248 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7249 rule->arg.hlua_rule->fcn.name);
7250 goto error;
7251
7252 case HLUA_E_ERR:
7253 /* Display log. */
7254 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7255 rule->arg.hlua_rule->fcn.name);
7256 goto error;
7257
7258 default:
7259 goto error;
7260 }
7261 }
7262
7263 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007264 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7265 goto done;
7266
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007267 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7268 goto error;
7269
Christopher Faulet54b5e212019-06-04 10:08:28 +02007270 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02007271 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007272 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7273 si_rx_room_blk(si);
7274 goto out;
7275 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007276 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007277 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7278 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007279 }
7280
7281 done:
7282 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007283 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007284 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007285 si_shutr(si);
7286 }
7287
7288 /* eat the whole request */
7289 if (co_data(req)) {
7290 req_htx = htx_from_buf(&req->buf);
7291 co_htx_skip(req, req_htx, co_data(req));
7292 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007293 }
7294 }
7295
7296 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007297 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007298 return;
7299
7300 error:
7301
7302 /* If we are in HTTP mode, and we are not send any
7303 * data, return a 500 server error in best effort:
7304 * if there is no room available in the buffer,
7305 * just close the connection.
7306 */
7307 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007308 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007309
7310 channel_erase(res);
7311 res->buf.data = b_data(err);
7312 memcpy(res->buf.area, b_head(err), b_data(err));
7313 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007314 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007315 }
7316 if (!(strm->flags & SF_ERR_MASK))
7317 strm->flags |= SF_ERR_RESOURCE;
7318 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7319 goto done;
7320}
7321
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007322static void hlua_applet_http_release(struct appctx *ctx)
7323{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007324 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007325 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007326 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007327 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007328}
7329
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007330/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007331 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007332 *
7333 * This function can fail with an abort() due to an Lua critical error.
7334 * We are in the configuration parsing process of HAProxy, this abort() is
7335 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007336 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007337static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7338 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007339{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007340 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007341 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007342
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007343 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007344 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007345 if (!rule->arg.hlua_rule) {
7346 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007347 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007348 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007349
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007350 /* Memory for arguments. */
7351 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7352 if (!rule->arg.hlua_rule->args) {
7353 memprintf(err, "out of memory error");
7354 return ACT_RET_PRS_ERR;
7355 }
7356
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007357 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007358 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007359
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007360 /* Expect some arguments */
7361 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007362 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007363 memprintf(err, "expect %d arguments", fcn->nargs);
7364 return ACT_RET_PRS_ERR;
7365 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007366 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007367 if (!rule->arg.hlua_rule->args[i]) {
7368 memprintf(err, "out of memory error");
7369 return ACT_RET_PRS_ERR;
7370 }
7371 (*cur_arg)++;
7372 }
7373 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007374
Thierry FOURNIER42148732015-09-02 17:17:33 +02007375 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007376 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007377 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007378}
7379
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007380static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7381 struct act_rule *rule, char **err)
7382{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007383 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007384
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007385 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007386 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007387 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007388 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007389 * the call of this analyzer.
7390 */
7391 if (rule->from != ACT_F_HTTP_REQ) {
7392 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7393 return ACT_RET_PRS_ERR;
7394 }
7395
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007396 /* Memory for the rule. */
7397 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7398 if (!rule->arg.hlua_rule) {
7399 memprintf(err, "out of memory error");
7400 return ACT_RET_PRS_ERR;
7401 }
7402
7403 /* Reference the Lua function and store the reference. */
7404 rule->arg.hlua_rule->fcn = *fcn;
7405
7406 /* TODO: later accept arguments. */
7407 rule->arg.hlua_rule->args = NULL;
7408
7409 /* Add applet pointer in the rule. */
7410 rule->applet.obj_type = OBJ_TYPE_APPLET;
7411 rule->applet.name = fcn->name;
7412 rule->applet.init = hlua_applet_http_init;
7413 rule->applet.fct = hlua_applet_http_fct;
7414 rule->applet.release = hlua_applet_http_release;
7415 rule->applet.timeout = hlua_timeout_applet;
7416
7417 return ACT_RET_PRS_OK;
7418}
7419
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007420/* This function is an LUA binding used for registering
7421 * "sample-conv" functions. It expects a converter name used
7422 * in the haproxy configuration file, and an LUA function.
7423 */
7424__LJMP static int hlua_register_action(lua_State *L)
7425{
7426 struct action_kw_list *akl;
7427 const char *name;
7428 int ref;
7429 int len;
7430 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007431 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007432
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007433 /* Initialise the number of expected arguments at 0. */
7434 nargs = 0;
7435
7436 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7437 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007438
7439 /* First argument : converter name. */
7440 name = MAY_LJMP(luaL_checkstring(L, 1));
7441
7442 /* Second argument : environment. */
7443 if (lua_type(L, 2) != LUA_TTABLE)
7444 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7445
7446 /* Third argument : lua function. */
7447 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7448
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007449 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007450 if (lua_gettop(L) >= 4)
7451 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7452
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007453 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007454 lua_pushnil(L);
7455 while (lua_next(L, 2) != 0) {
7456 if (lua_type(L, -1) != LUA_TSTRING)
7457 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7458
7459 /* Check required environment. Only accepted "http" or "tcp". */
7460 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007461 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007462 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007463 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007464 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007465 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007466 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007467
7468 /* Fill fcn. */
7469 fcn->name = strdup(name);
7470 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007471 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007472 fcn->function_ref = ref;
7473
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007474 /* Set the expected number od arguments. */
7475 fcn->nargs = nargs;
7476
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007477 /* List head */
7478 akl->list.n = akl->list.p = NULL;
7479
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007480 /* action keyword. */
7481 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007482 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007483 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007484 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007485
7486 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7487
7488 akl->kw[0].match_pfx = 0;
7489 akl->kw[0].private = fcn;
7490 akl->kw[0].parse = action_register_lua;
7491
7492 /* select the action registering point. */
7493 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7494 tcp_req_cont_keywords_register(akl);
7495 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7496 tcp_res_cont_keywords_register(akl);
7497 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7498 http_req_keywords_register(akl);
7499 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7500 http_res_keywords_register(akl);
7501 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007502 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007503 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7504 "are expected.", lua_tostring(L, -1)));
7505
7506 /* pop the environment string. */
7507 lua_pop(L, 1);
7508 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007509 return ACT_RET_PRS_OK;
7510}
7511
7512static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7513 struct act_rule *rule, char **err)
7514{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007515 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007516
Christopher Faulet280f85b2019-07-15 15:02:04 +02007517 if (px->mode == PR_MODE_HTTP) {
7518 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007519 return ACT_RET_PRS_ERR;
7520 }
7521
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007522 /* Memory for the rule. */
7523 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7524 if (!rule->arg.hlua_rule) {
7525 memprintf(err, "out of memory error");
7526 return ACT_RET_PRS_ERR;
7527 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007528
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007529 /* Reference the Lua function and store the reference. */
7530 rule->arg.hlua_rule->fcn = *fcn;
7531
7532 /* TODO: later accept arguments. */
7533 rule->arg.hlua_rule->args = NULL;
7534
7535 /* Add applet pointer in the rule. */
7536 rule->applet.obj_type = OBJ_TYPE_APPLET;
7537 rule->applet.name = fcn->name;
7538 rule->applet.init = hlua_applet_tcp_init;
7539 rule->applet.fct = hlua_applet_tcp_fct;
7540 rule->applet.release = hlua_applet_tcp_release;
7541 rule->applet.timeout = hlua_timeout_applet;
7542
7543 return 0;
7544}
7545
7546/* This function is an LUA binding used for registering
7547 * "sample-conv" functions. It expects a converter name used
7548 * in the haproxy configuration file, and an LUA function.
7549 */
7550__LJMP static int hlua_register_service(lua_State *L)
7551{
7552 struct action_kw_list *akl;
7553 const char *name;
7554 const char *env;
7555 int ref;
7556 int len;
7557 struct hlua_function *fcn;
7558
7559 MAY_LJMP(check_args(L, 3, "register_service"));
7560
7561 /* First argument : converter name. */
7562 name = MAY_LJMP(luaL_checkstring(L, 1));
7563
7564 /* Second argument : environment. */
7565 env = MAY_LJMP(luaL_checkstring(L, 2));
7566
7567 /* Third argument : lua function. */
7568 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7569
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007570 /* Allocate and fill the sample fetch keyword struct. */
7571 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7572 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007573 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007574 fcn = calloc(1, sizeof(*fcn));
7575 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007576 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007577
7578 /* Fill fcn. */
7579 len = strlen("<lua.>") + strlen(name) + 1;
7580 fcn->name = calloc(1, len);
7581 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007582 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007583 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7584 fcn->function_ref = ref;
7585
7586 /* List head */
7587 akl->list.n = akl->list.p = NULL;
7588
7589 /* converter keyword. */
7590 len = strlen("lua.") + strlen(name) + 1;
7591 akl->kw[0].kw = calloc(1, len);
7592 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007593 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007594
7595 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7596
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007597 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007598 if (strcmp(env, "tcp") == 0)
7599 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007600 else if (strcmp(env, "http") == 0)
7601 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007602 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007603 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007604 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007605
7606 akl->kw[0].match_pfx = 0;
7607 akl->kw[0].private = fcn;
7608
7609 /* End of array. */
7610 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7611
7612 /* Register this new converter */
7613 service_keywords_register(akl);
7614
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007615 return 0;
7616}
7617
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007618/* This function initialises Lua cli handler. It copies the
7619 * arguments in the Lua stack and create channel IO objects.
7620 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007621static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007622{
7623 struct hlua *hlua;
7624 struct hlua_function *fcn;
7625 int i;
7626 const char *error;
7627
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007628 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007629 appctx->ctx.hlua_cli.fcn = private;
7630
Willy Tarreaubafbe012017-11-24 17:34:44 +01007631 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007632 if (!hlua) {
7633 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007634 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007635 }
7636 HLUA_INIT(hlua);
7637 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007638
7639 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007640 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007641 * applet_http. It is absolutely compatible.
7642 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007643 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007644 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007645 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007646 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007647 }
7648 appctx->ctx.hlua_cli.task->nice = 0;
7649 appctx->ctx.hlua_cli.task->context = appctx;
7650 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7651
7652 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007653 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007654 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007655 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007656 }
7657
7658 /* The following Lua calls can fail. */
7659 if (!SET_SAFE_LJMP(hlua->T)) {
7660 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7661 error = lua_tostring(hlua->T, -1);
7662 else
7663 error = "critical error";
7664 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7665 goto error;
7666 }
7667
7668 /* Check stack available size. */
7669 if (!lua_checkstack(hlua->T, 2)) {
7670 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7671 goto error;
7672 }
7673
7674 /* Restore the function in the stack. */
7675 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7676
7677 /* Once the arguments parsed, the CLI is like an AppletTCP,
7678 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007679 */
7680 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7681 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7682 goto error;
7683 }
7684 hlua->nargs = 1;
7685
7686 /* push keywords in the stack. */
7687 for (i = 0; *args[i]; i++) {
7688 /* Check stack available size. */
7689 if (!lua_checkstack(hlua->T, 1)) {
7690 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7691 goto error;
7692 }
7693 lua_pushstring(hlua->T, args[i]);
7694 hlua->nargs++;
7695 }
7696
7697 /* We must initialize the execution timeouts. */
7698 hlua->max_time = hlua_timeout_session;
7699
7700 /* At this point the execution is safe. */
7701 RESET_SAFE_LJMP(hlua->T);
7702
7703 /* It's ok */
7704 return 0;
7705
7706 /* It's not ok. */
7707error:
7708 RESET_SAFE_LJMP(hlua->T);
7709 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007710 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007711 return 1;
7712}
7713
7714static int hlua_cli_io_handler_fct(struct appctx *appctx)
7715{
7716 struct hlua *hlua;
7717 struct stream_interface *si;
7718 struct hlua_function *fcn;
7719
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007720 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007721 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007722 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007723
7724 /* If the stream is disconnect or closed, ldo nothing. */
7725 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7726 return 1;
7727
7728 /* Execute the function. */
7729 switch (hlua_ctx_resume(hlua, 1)) {
7730
7731 /* finished. */
7732 case HLUA_E_OK:
7733 return 1;
7734
7735 /* yield. */
7736 case HLUA_E_AGAIN:
7737 /* We want write. */
7738 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007739 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007740 /* Set the timeout. */
7741 if (hlua->wake_time != TICK_ETERNITY)
7742 task_schedule(hlua->task, hlua->wake_time);
7743 return 0;
7744
7745 /* finished with error. */
7746 case HLUA_E_ERRMSG:
7747 /* Display log. */
7748 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7749 fcn->name, lua_tostring(hlua->T, -1));
7750 lua_pop(hlua->T, 1);
7751 return 1;
7752
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007753 case HLUA_E_ETMOUT:
7754 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7755 fcn->name);
7756 return 1;
7757
7758 case HLUA_E_NOMEM:
7759 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7760 fcn->name);
7761 return 1;
7762
7763 case HLUA_E_YIELD: /* unexpected */
7764 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7765 fcn->name);
7766 return 1;
7767
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007768 case HLUA_E_ERR:
7769 /* Display log. */
7770 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7771 fcn->name);
7772 return 1;
7773
7774 default:
7775 return 1;
7776 }
7777
7778 return 1;
7779}
7780
7781static void hlua_cli_io_release_fct(struct appctx *appctx)
7782{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007783 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007784 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007785}
7786
7787/* This function is an LUA binding used for registering
7788 * new keywords in the cli. It expects a list of keywords
7789 * which are the "path". It is limited to 5 keywords. A
7790 * description of the command, a function to be executed
7791 * for the parsing and a function for io handlers.
7792 */
7793__LJMP static int hlua_register_cli(lua_State *L)
7794{
7795 struct cli_kw_list *cli_kws;
7796 const char *message;
7797 int ref_io;
7798 int len;
7799 struct hlua_function *fcn;
7800 int index;
7801 int i;
7802
7803 MAY_LJMP(check_args(L, 3, "register_cli"));
7804
7805 /* First argument : an array of maximum 5 keywords. */
7806 if (!lua_istable(L, 1))
7807 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7808
7809 /* Second argument : string with contextual message. */
7810 message = MAY_LJMP(luaL_checkstring(L, 2));
7811
7812 /* Third and fourth argument : lua function. */
7813 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7814
7815 /* Allocate and fill the sample fetch keyword struct. */
7816 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7817 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007818 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007819 fcn = calloc(1, sizeof(*fcn));
7820 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007821 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007822
7823 /* Fill path. */
7824 index = 0;
7825 lua_pushnil(L);
7826 while(lua_next(L, 1) != 0) {
7827 if (index >= 5)
7828 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7829 if (lua_type(L, -1) != LUA_TSTRING)
7830 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7831 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7832 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007833 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007834 index++;
7835 lua_pop(L, 1);
7836 }
7837
7838 /* Copy help message. */
7839 cli_kws->kw[0].usage = strdup(message);
7840 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007841 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007842
7843 /* Fill fcn io handler. */
7844 len = strlen("<lua.cli>") + 1;
7845 for (i = 0; i < index; i++)
7846 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7847 fcn->name = calloc(1, len);
7848 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007849 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007850 strncat((char *)fcn->name, "<lua.cli", len);
7851 for (i = 0; i < index; i++) {
7852 strncat((char *)fcn->name, ".", len);
7853 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7854 }
7855 strncat((char *)fcn->name, ">", len);
7856 fcn->function_ref = ref_io;
7857
7858 /* Fill last entries. */
7859 cli_kws->kw[0].private = fcn;
7860 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7861 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7862 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7863
7864 /* Register this new converter */
7865 cli_register_kw(cli_kws);
7866
7867 return 0;
7868}
7869
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007870static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7871 struct proxy *defpx, const char *file, int line,
7872 char **err, unsigned int *timeout)
7873{
7874 const char *error;
7875
7876 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007877 if (error == PARSE_TIME_OVER) {
7878 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7879 args[1], args[0]);
7880 return -1;
7881 }
7882 else if (error == PARSE_TIME_UNDER) {
7883 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7884 args[1], args[0]);
7885 return -1;
7886 }
7887 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007888 memprintf(err, "%s: invalid timeout", args[0]);
7889 return -1;
7890 }
7891 return 0;
7892}
7893
7894static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7895 struct proxy *defpx, const char *file, int line,
7896 char **err)
7897{
7898 return hlua_read_timeout(args, section_type, curpx, defpx,
7899 file, line, err, &hlua_timeout_session);
7900}
7901
7902static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7903 struct proxy *defpx, const char *file, int line,
7904 char **err)
7905{
7906 return hlua_read_timeout(args, section_type, curpx, defpx,
7907 file, line, err, &hlua_timeout_task);
7908}
7909
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007910static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7911 struct proxy *defpx, const char *file, int line,
7912 char **err)
7913{
7914 return hlua_read_timeout(args, section_type, curpx, defpx,
7915 file, line, err, &hlua_timeout_applet);
7916}
7917
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007918static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7919 struct proxy *defpx, const char *file, int line,
7920 char **err)
7921{
7922 char *error;
7923
7924 hlua_nb_instruction = strtoll(args[1], &error, 10);
7925 if (*error != '\0') {
7926 memprintf(err, "%s: invalid number", args[0]);
7927 return -1;
7928 }
7929 return 0;
7930}
7931
Willy Tarreau32f61e22015-03-18 17:54:59 +01007932static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7933 struct proxy *defpx, const char *file, int line,
7934 char **err)
7935{
7936 char *error;
7937
7938 if (*(args[1]) == 0) {
7939 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7940 return -1;
7941 }
7942 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7943 if (*error != '\0') {
7944 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7945 return -1;
7946 }
7947 return 0;
7948}
7949
7950
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007951/* This function is called by the main configuration key "lua-load". It loads and
7952 * execute an lua file during the parsing of the HAProxy configuration file. It is
7953 * the main lua entry point.
7954 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007955 * This function runs with the HAProxy keywords API. It returns -1 if an error
7956 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007957 *
7958 * In some error case, LUA set an error message in top of the stack. This function
7959 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007960 *
7961 * This function can fail with an abort() due to an Lua critical error.
7962 * We are in the configuration parsing process of HAProxy, this abort() is
7963 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007964 */
7965static int hlua_load(char **args, int section_type, struct proxy *curpx,
7966 struct proxy *defpx, const char *file, int line,
7967 char **err)
7968{
7969 int error;
7970
7971 /* Just load and compile the file. */
7972 error = luaL_loadfile(gL.T, args[1]);
7973 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007974 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007975 lua_pop(gL.T, 1);
7976 return -1;
7977 }
7978
7979 /* If no syntax error where detected, execute the code. */
7980 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7981 switch (error) {
7982 case LUA_OK:
7983 break;
7984 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007985 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007986 lua_pop(gL.T, 1);
7987 return -1;
7988 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007989 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007990 return -1;
7991 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007992 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007993 lua_pop(gL.T, 1);
7994 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02007995#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007996 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007997 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007998 lua_pop(gL.T, 1);
7999 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008000#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008001 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05008002 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008003 lua_pop(gL.T, 1);
8004 return -1;
8005 }
8006
8007 return 0;
8008}
8009
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008010/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8011 * in the given <ctx>.
8012 */
8013static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
8014{
8015 lua_getglobal(ctx.T, "package"); /* push package variable */
8016 lua_pushstring(ctx.T, path); /* push given path */
8017 lua_pushstring(ctx.T, ";"); /* push semicolon */
8018 lua_getfield(ctx.T, -3, type); /* push old path */
8019 lua_concat(ctx.T, 3); /* concatenate to new path */
8020 lua_setfield(ctx.T, -2, type); /* store new path */
8021 lua_pop(ctx.T, 1); /* pop package variable */
8022
8023 return 0;
8024}
8025
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008026static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
8027 struct proxy *defpx, const char *file, int line,
8028 char **err)
8029{
8030 char *path;
8031 char *type = "path";
8032 if (too_many_args(2, args, err, NULL)) {
8033 return -1;
8034 }
8035
8036 if (!(*args[1])) {
8037 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
8038 return -1;
8039 }
8040 path = args[1];
8041
8042 if (*args[2]) {
8043 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8044 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
8045 return -1;
8046 }
8047 type = args[2];
8048 }
8049
8050 return hlua_prepend_path(gL, type, path);
8051}
8052
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008053/* configuration keywords declaration */
8054static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008055 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008056 { CFG_GLOBAL, "lua-load", hlua_load },
8057 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8058 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008059 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008060 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008061 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008062 { 0, NULL, NULL },
8063}};
8064
Willy Tarreau0108d902018-11-25 19:14:37 +01008065INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8066
Christopher Fauletafd8f102018-11-08 11:34:21 +01008067
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008068/* This function can fail with an abort() due to an Lua critical error.
8069 * We are in the initialisation process of HAProxy, this abort() is
8070 * tolerated.
8071 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008072int hlua_post_init()
8073{
8074 struct hlua_init_function *init;
8075 const char *msg;
8076 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008077 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008078
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008079 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008080 if (!SET_SAFE_LJMP(gL.T)) {
8081 if (lua_type(gL.T, -1) == LUA_TSTRING)
8082 error = lua_tostring(gL.T, -1);
8083 else
8084 error = "critical error";
8085 fprintf(stderr, "Lua post-init: %s.\n", error);
8086 exit(1);
8087 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008088
8089#if USE_OPENSSL
8090 /* Initialize SSL server. */
8091 if (socket_ssl.xprt->prepare_srv) {
8092 int saved_used_backed = global.ssl_used_backend;
8093 // don't affect maxconn automatic computation
8094 socket_ssl.xprt->prepare_srv(&socket_ssl);
8095 global.ssl_used_backend = saved_used_backed;
8096 }
8097#endif
8098
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008099 hlua_fcn_post_init(gL.T);
8100 RESET_SAFE_LJMP(gL.T);
8101
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008102 list_for_each_entry(init, &hlua_init_functions, l) {
8103 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
8104 ret = hlua_ctx_resume(&gL, 0);
8105 switch (ret) {
8106 case HLUA_E_OK:
8107 lua_pop(gL.T, -1);
8108 return 1;
8109 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008110 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008111 return 0;
8112 case HLUA_E_ERRMSG:
8113 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008114 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008115 return 0;
8116 case HLUA_E_ERR:
8117 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008118 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008119 return 0;
8120 }
8121 }
8122 return 1;
8123}
8124
Willy Tarreau32f61e22015-03-18 17:54:59 +01008125/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8126 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8127 * is the previously allocated size or the kind of object in case of a new
8128 * allocation. <nsize> is the requested new size.
8129 */
8130static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8131{
8132 struct hlua_mem_allocator *zone = ud;
8133
8134 if (nsize == 0) {
8135 /* it's a free */
8136 if (ptr)
8137 zone->allocated -= osize;
8138 free(ptr);
8139 return NULL;
8140 }
8141
8142 if (!ptr) {
8143 /* it's a new allocation */
8144 if (zone->limit && zone->allocated + nsize > zone->limit)
8145 return NULL;
8146
8147 ptr = malloc(nsize);
8148 if (ptr)
8149 zone->allocated += nsize;
8150 return ptr;
8151 }
8152
8153 /* it's a realloc */
8154 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8155 return NULL;
8156
8157 ptr = realloc(ptr, nsize);
8158 if (ptr)
8159 zone->allocated += nsize - osize;
8160 return ptr;
8161}
8162
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008163/* Ithis function can fail with an abort() due to an Lua critical error.
8164 * We are in the initialisation process of HAProxy, this abort() is
8165 * tolerated.
8166 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008167void hlua_init(void)
8168{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008169 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008170 int idx;
8171 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008172 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008173 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008174 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008175#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008176 struct srv_kw *kw;
8177 int tmp_error;
8178 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008179 char *args[] = { /* SSL client configuration. */
8180 "ssl",
8181 "verify",
8182 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008183 NULL
8184 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008185#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008186
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008187 /* Init main lua stack. */
8188 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008189 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008190 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008191 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008192 hlua_sethlua(&gL);
8193 gL.Tref = LUA_REFNIL;
8194 gL.task = NULL;
8195
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008196 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008197 * the Lua function can fail with an abort. We are in the initialisation
8198 * process of HAProxy, this abort() is tolerated.
8199 */
8200
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008201 /* Initialise lua. */
8202 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008203#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8204#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8205#ifdef HLUA_PREPEND_PATH
8206 hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
8207#endif
8208#ifdef HLUA_PREPEND_CPATH
8209 hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
8210#endif
8211#undef HLUA_PREPEND_PATH_TOSTRING
8212#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008213
Thierry Fournier75933d42016-01-21 09:30:18 +01008214 /* Set safe environment for the initialisation. */
8215 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008216 if (lua_type(gL.T, -1) == LUA_TSTRING)
8217 error_msg = lua_tostring(gL.T, -1);
8218 else
8219 error_msg = "critical error";
8220 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008221 exit(1);
8222 }
8223
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008224 /*
8225 *
8226 * Create "core" object.
8227 *
8228 */
8229
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008230 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008231 lua_newtable(gL.T);
8232
8233 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008234 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008235 hlua_class_const_int(gL.T, log_levels[i], i);
8236
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008237 /* Register special functions. */
8238 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008239 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008240 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008241 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008242 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008243 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008244 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008245 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008246 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008247 hlua_class_function(gL.T, "sleep", hlua_sleep);
8248 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008249 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8250 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8251 hlua_class_function(gL.T, "set_map", hlua_set_map);
8252 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008253 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008254 hlua_class_function(gL.T, "log", hlua_log);
8255 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8256 hlua_class_function(gL.T, "Info", hlua_log_info);
8257 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8258 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008259 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008260 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008261
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008262 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008263
8264 /*
8265 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008266 * Create "act" object.
8267 *
8268 */
8269
8270 /* This table entry is the object "act" base. */
8271 lua_newtable(gL.T);
8272
8273 /* push action return constants */
8274 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8275 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8276 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8277 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8278 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8279 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8280 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8281 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8282
Christopher Faulet501465d2020-02-26 14:54:16 +01008283 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008284
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008285 lua_setglobal(gL.T, "act");
8286
8287 /*
8288 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008289 * Register class Map
8290 *
8291 */
8292
8293 /* This table entry is the object "Map" base. */
8294 lua_newtable(gL.T);
8295
8296 /* register pattern types. */
8297 for (i=0; i<PAT_MATCH_NUM; i++)
8298 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008299 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008300 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8301 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008302 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008303
8304 /* register constructor. */
8305 hlua_class_function(gL.T, "new", hlua_map_new);
8306
8307 /* Create and fill the metatable. */
8308 lua_newtable(gL.T);
8309
Ilya Shipitsind4259502020-04-08 01:07:56 +05008310 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008311 lua_pushstring(gL.T, "__index");
8312 lua_newtable(gL.T);
8313
8314 /* Register . */
8315 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8316 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8317
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008318 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008319
Thierry Fournier45e78d72016-02-19 18:34:46 +01008320 /* Register previous table in the registry with reference and named entry.
8321 * The function hlua_register_metatable() pops the stack, so we
8322 * previously create a copy of the table.
8323 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008324 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008325 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008326
8327 /* Assign the metatable to the mai Map object. */
8328 lua_setmetatable(gL.T, -2);
8329
8330 /* Set a name to the table. */
8331 lua_setglobal(gL.T, "Map");
8332
8333 /*
8334 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008335 * Register class Channel
8336 *
8337 */
8338
8339 /* Create and fill the metatable. */
8340 lua_newtable(gL.T);
8341
Ilya Shipitsind4259502020-04-08 01:07:56 +05008342 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008343 lua_pushstring(gL.T, "__index");
8344 lua_newtable(gL.T);
8345
8346 /* Register . */
8347 hlua_class_function(gL.T, "get", hlua_channel_get);
8348 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8349 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8350 hlua_class_function(gL.T, "set", hlua_channel_set);
8351 hlua_class_function(gL.T, "append", hlua_channel_append);
8352 hlua_class_function(gL.T, "send", hlua_channel_send);
8353 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8354 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8355 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008356 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008357 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008358
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008359 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008360
8361 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008362 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008363
8364 /*
8365 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008366 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008367 *
8368 */
8369
8370 /* Create and fill the metatable. */
8371 lua_newtable(gL.T);
8372
Ilya Shipitsind4259502020-04-08 01:07:56 +05008373 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008374 lua_pushstring(gL.T, "__index");
8375 lua_newtable(gL.T);
8376
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008377 /* Browse existing fetches and create the associated
8378 * object method.
8379 */
8380 sf = NULL;
8381 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008382 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8383 * by an underscore.
8384 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008385 strncpy(trash.area, sf->kw, trash.size);
8386 trash.area[trash.size - 1] = '\0';
8387 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008388 if (*p == '.' || *p == '-' || *p == '+')
8389 *p = '_';
8390
8391 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008392 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008393 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008394 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008395 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008396 }
8397
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008398 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008399
8400 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008401 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008402
8403 /*
8404 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008405 * Register class Converters
8406 *
8407 */
8408
8409 /* Create and fill the metatable. */
8410 lua_newtable(gL.T);
8411
8412 /* Create and fill the __index entry. */
8413 lua_pushstring(gL.T, "__index");
8414 lua_newtable(gL.T);
8415
8416 /* Browse existing converters and create the associated
8417 * object method.
8418 */
8419 sc = NULL;
8420 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008421 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8422 * by an underscore.
8423 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008424 strncpy(trash.area, sc->kw, trash.size);
8425 trash.area[trash.size - 1] = '\0';
8426 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008427 if (*p == '.' || *p == '-' || *p == '+')
8428 *p = '_';
8429
8430 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008431 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008432 lua_pushlightuserdata(gL.T, sc);
8433 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008434 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008435 }
8436
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008437 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008438
8439 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008440 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008441
8442 /*
8443 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008444 * Register class HTTP
8445 *
8446 */
8447
8448 /* Create and fill the metatable. */
8449 lua_newtable(gL.T);
8450
Ilya Shipitsind4259502020-04-08 01:07:56 +05008451 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008452 lua_pushstring(gL.T, "__index");
8453 lua_newtable(gL.T);
8454
8455 /* Register Lua functions. */
8456 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8457 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8458 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8459 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8460 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8461 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8462 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8463 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8464 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8465 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8466
8467 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8468 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8469 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8470 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8471 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8472 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008473 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008474
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008475 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008476
8477 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008478 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008479
8480 /*
8481 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008482 * Register class AppletTCP
8483 *
8484 */
8485
8486 /* Create and fill the metatable. */
8487 lua_newtable(gL.T);
8488
Ilya Shipitsind4259502020-04-08 01:07:56 +05008489 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008490 lua_pushstring(gL.T, "__index");
8491 lua_newtable(gL.T);
8492
8493 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008494 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8495 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8496 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8497 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8498 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008499 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8500 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8501 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008502
8503 lua_settable(gL.T, -3);
8504
8505 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008506 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008507
8508 /*
8509 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008510 * Register class AppletHTTP
8511 *
8512 */
8513
8514 /* Create and fill the metatable. */
8515 lua_newtable(gL.T);
8516
Ilya Shipitsind4259502020-04-08 01:07:56 +05008517 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008518 lua_pushstring(gL.T, "__index");
8519 lua_newtable(gL.T);
8520
8521 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008522 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8523 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008524 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8525 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8526 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008527 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8528 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8529 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8530 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8531 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8532 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8533
8534 lua_settable(gL.T, -3);
8535
8536 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008537 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008538
8539 /*
8540 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008541 * Register class TXN
8542 *
8543 */
8544
8545 /* Create and fill the metatable. */
8546 lua_newtable(gL.T);
8547
Ilya Shipitsind4259502020-04-08 01:07:56 +05008548 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008549 lua_pushstring(gL.T, "__index");
8550 lua_newtable(gL.T);
8551
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008552 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008553 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8554 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8555 hlua_class_function(gL.T, "set_var", hlua_set_var);
8556 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8557 hlua_class_function(gL.T, "get_var", hlua_get_var);
8558 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008559 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008560 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8561 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8562 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8563 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8564 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8565 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8566 hlua_class_function(gL.T, "log", hlua_txn_log);
8567 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8568 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8569 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8570 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008571
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008572 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008573
8574 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008575 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008576
8577 /*
8578 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008579 * Register class reply
8580 *
8581 */
8582 lua_newtable(gL.T);
8583 lua_pushstring(gL.T, "__index");
8584 lua_newtable(gL.T);
8585 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8586 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8587 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8588 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8589 lua_settable(gL.T, -3); /* Sets the __index entry. */
8590 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8591
8592
8593 /*
8594 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008595 * Register class Socket
8596 *
8597 */
8598
8599 /* Create and fill the metatable. */
8600 lua_newtable(gL.T);
8601
Ilya Shipitsind4259502020-04-08 01:07:56 +05008602 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008603 lua_pushstring(gL.T, "__index");
8604 lua_newtable(gL.T);
8605
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008606#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008607 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008608#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008609 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8610 hlua_class_function(gL.T, "send", hlua_socket_send);
8611 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8612 hlua_class_function(gL.T, "close", hlua_socket_close);
8613 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8614 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8615 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8616 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8617
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008618 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008619
8620 /* Register the garbage collector entry. */
8621 lua_pushstring(gL.T, "__gc");
8622 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008623 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008624
8625 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008626 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008627
8628 /* Proxy and server configuration initialisation. */
8629 memset(&socket_proxy, 0, sizeof(socket_proxy));
8630 init_new_proxy(&socket_proxy);
8631 socket_proxy.parent = NULL;
8632 socket_proxy.last_change = now.tv_sec;
8633 socket_proxy.id = "LUA-SOCKET";
8634 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8635 socket_proxy.maxconn = 0;
8636 socket_proxy.accept = NULL;
8637 socket_proxy.options2 |= PR_O2_INDEPSTR;
8638 socket_proxy.srv = NULL;
8639 socket_proxy.conn_retries = 0;
8640 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8641
8642 /* Init TCP server: unchanged parameters */
8643 memset(&socket_tcp, 0, sizeof(socket_tcp));
8644 socket_tcp.next = NULL;
8645 socket_tcp.proxy = &socket_proxy;
8646 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8647 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008648 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008649 socket_tcp.idle_conns = NULL;
8650 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008651 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008652 socket_tcp.last_change = 0;
8653 socket_tcp.id = "LUA-TCP-CONN";
8654 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8655 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8656 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8657
8658 /* XXX: Copy default parameter from default server,
8659 * but the default server is not initialized.
8660 */
8661 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8662 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8663 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8664 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8665 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8666 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8667 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8668 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8669 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8670 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8671
8672 socket_tcp.check.status = HCHK_STATUS_INI;
8673 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8674 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8675 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8676 socket_tcp.check.server = &socket_tcp;
8677
8678 socket_tcp.agent.status = HCHK_STATUS_INI;
8679 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8680 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8681 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8682 socket_tcp.agent.server = &socket_tcp;
8683
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008684 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008685
8686#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008687 /* Init TCP server: unchanged parameters */
8688 memset(&socket_ssl, 0, sizeof(socket_ssl));
8689 socket_ssl.next = NULL;
8690 socket_ssl.proxy = &socket_proxy;
8691 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8692 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008693 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008694 socket_ssl.idle_conns = NULL;
8695 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008696 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008697 socket_ssl.last_change = 0;
8698 socket_ssl.id = "LUA-SSL-CONN";
8699 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8700 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8701 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8702
8703 /* XXX: Copy default parameter from default server,
8704 * but the default server is not initialized.
8705 */
8706 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8707 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8708 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8709 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8710 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8711 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8712 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8713 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8714 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8715 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8716
8717 socket_ssl.check.status = HCHK_STATUS_INI;
8718 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8719 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8720 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8721 socket_ssl.check.server = &socket_ssl;
8722
8723 socket_ssl.agent.status = HCHK_STATUS_INI;
8724 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8725 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8726 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8727 socket_ssl.agent.server = &socket_ssl;
8728
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008729 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008730 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008731
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008732 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008733 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8734 /*
8735 *
8736 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008737 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008738 * features like client certificates and ssl_verify.
8739 *
8740 */
8741 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8742 if (tmp_error != 0) {
8743 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8744 abort(); /* This must be never arrives because the command line
8745 not editable by the user. */
8746 }
8747 idx += kw->skip;
8748 }
8749 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008750#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008751
8752 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008753}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008754
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02008755static void hlua_deinit()
8756{
8757 lua_close(gL.T);
8758}
8759
8760REGISTER_POST_DEINIT(hlua_deinit);
8761
Willy Tarreau80713382018-11-26 10:19:54 +01008762static void hlua_register_build_options(void)
8763{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008764 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008765
Willy Tarreaubb57d942016-12-21 19:04:56 +01008766 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8767 hap_register_build_opts(ptr, 1);
8768}
Willy Tarreau80713382018-11-26 10:19:54 +01008769
8770INITCALL0(STG_REGISTER, hlua_register_build_options);