blob: 2c70a69bb3d0cc9812e8aa43c95cbb358f8315a3 [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. */
Thierry Fournier92689e62020-11-28 11:21:25 +0100160struct {
161 lua_State *T;
162} gL;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100163
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100164/* This is the memory pool containing struct lua for applets
165 * (including cli).
166 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100167DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100168
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100169/* Used for Socket connection. */
170static struct proxy socket_proxy;
171static struct server socket_tcp;
172#ifdef USE_OPENSSL
173static struct server socket_ssl;
174#endif
175
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100176/* List head of the function called at the initialisation time. */
177struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
178
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100179/* The following variables contains the reference of the different
180 * Lua classes. These references are useful for identify metadata
181 * associated with an object.
182 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100183static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100184static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100185static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100186static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100187static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100188static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200189static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200190static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200191static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100192static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100193
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100194/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200195 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100196 * short timeout. Lua linked with tasks doesn't have a timeout
197 * because a task may remain alive during all the haproxy execution.
198 */
199static unsigned int hlua_timeout_session = 4000; /* session timeout. */
200static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200201static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100202
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100203/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
204 * it is used for preventing infinite loops.
205 *
206 * I test the scheer with an infinite loop containing one incrementation
207 * and one test. I run this loop between 10 seconds, I raise a ceil of
208 * 710M loops from one interrupt each 9000 instructions, so I fix the value
209 * to one interrupt each 10 000 instructions.
210 *
211 * configured | Number of
212 * instructions | loops executed
213 * between two | in milions
214 * forced yields |
215 * ---------------+---------------
216 * 10 | 160
217 * 500 | 670
218 * 1000 | 680
219 * 5000 | 700
220 * 7000 | 700
221 * 8000 | 700
222 * 9000 | 710 <- ceil
223 * 10000 | 710
224 * 100000 | 710
225 * 1000000 | 710
226 *
227 */
228static unsigned int hlua_nb_instruction = 10000;
229
Willy Tarreaucdb53462020-12-02 12:12:00 +0100230/* Descriptor for the memory allocation state. The limit is pre-initialised to
231 * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it
232 * is replaced with ~0 during post_init after everything was loaded. This way
233 * it is guaranteed that if limit is ~0 the boot is complete and that if it's
234 * zero it's not yet limited and proper accounting is required.
Willy Tarreau32f61e22015-03-18 17:54:59 +0100235 */
236struct hlua_mem_allocator {
237 size_t allocated;
238 size_t limit;
239};
240
Willy Tarreaucdb53462020-12-02 12:12:00 +0100241static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64);
Willy Tarreau32f61e22015-03-18 17:54:59 +0100242
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100243/* These functions converts types between HAProxy internal args or
244 * sample and LUA types. Another function permits to check if the
245 * LUA stack contains arguments according with an required ARG_T
246 * format.
247 */
248static int hlua_arg2lua(lua_State *L, const struct arg *arg);
249static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100250__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100251 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100252static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100253static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100254static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
255
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100256__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200257
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200258#define SEND_ERR(__be, __fmt, __args...) \
259 do { \
260 send_log(__be, LOG_ERR, __fmt, ## __args); \
261 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100262 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200263 } while (0)
264
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100265/* Used to check an Lua function type in the stack. It creates and
266 * returns a reference of the function. This function throws an
267 * error if the rgument is not a "function".
268 */
269__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
270{
271 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100272 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100273 WILL_LJMP(luaL_argerror(L, argno, msg));
274 }
275 lua_pushvalue(L, argno);
276 return luaL_ref(L, LUA_REGISTRYINDEX);
277}
278
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200279/* Return the string that is of the top of the stack. */
280const char *hlua_get_top_error_string(lua_State *L)
281{
282 if (lua_gettop(L) < 1)
283 return "unknown error";
284 if (lua_type(L, -1) != LUA_TSTRING)
285 return "unknown error";
286 return lua_tostring(L, -1);
287}
288
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200289__LJMP static const char *hlua_traceback(lua_State *L)
290{
291 lua_Debug ar;
292 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200293 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200294 int filled = 0;
295
296 while (lua_getstack(L, level++, &ar)) {
297
298 /* Add separator */
299 if (filled)
300 chunk_appendf(msg, ", ");
301 filled = 1;
302
303 /* Fill fields:
304 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
305 * 'l': fills in the field currentline;
306 * 'n': fills in the field name and namewhat;
307 * 't': fills in the field istailcall;
308 */
309 lua_getinfo(L, "Slnt", &ar);
310
311 /* Append code localisation */
312 if (ar.currentline > 0)
313 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
314 else
315 chunk_appendf(msg, "%s ", ar.short_src);
316
317 /*
318 * Get function name
319 *
320 * if namewhat is no empty, name is defined.
321 * what contains "Lua" for Lua function, "C" for C function,
322 * or "main" for main code.
323 */
324 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
325 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
326
327 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
328 chunk_appendf(msg, "main chunk");
329
330 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
331 chunk_appendf(msg, "C function line %d", ar.linedefined);
332
333 else /* nothing left... */
334 chunk_appendf(msg, "?");
335
336
337 /* Display tailed call */
338 if (ar.istailcall)
339 chunk_appendf(msg, " ...");
340 }
341
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200342 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200343}
344
345
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100346/* This function check the number of arguments available in the
347 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500348 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100349 */
350__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
351{
352 if (lua_gettop(L) == nb)
353 return;
354 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
355}
356
Mark Lakes22154b42018-01-29 14:38:40 -0800357/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100358 * and the line number where the error is encountered.
359 */
360static int hlua_pusherror(lua_State *L, const char *fmt, ...)
361{
362 va_list argp;
363 va_start(argp, fmt);
364 luaL_where(L, 1);
365 lua_pushvfstring(L, fmt, argp);
366 va_end(argp);
367 lua_concat(L, 2);
368 return 1;
369}
370
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100371/* This functions is used with sample fetch and converters. It
372 * converts the HAProxy configuration argument in a lua stack
373 * values.
374 *
375 * It takes an array of "arg", and each entry of the array is
376 * converted and pushed in the LUA stack.
377 */
378static int hlua_arg2lua(lua_State *L, const struct arg *arg)
379{
380 switch (arg->type) {
381 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100382 case ARGT_TIME:
383 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100384 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100385 break;
386
387 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200388 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100389 break;
390
391 case ARGT_IPV4:
392 case ARGT_IPV6:
393 case ARGT_MSK4:
394 case ARGT_MSK6:
395 case ARGT_FE:
396 case ARGT_BE:
397 case ARGT_TAB:
398 case ARGT_SRV:
399 case ARGT_USR:
400 case ARGT_MAP:
401 default:
402 lua_pushnil(L);
403 break;
404 }
405 return 1;
406}
407
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500408/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100409 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500410 * with sample fetch wrappers. The input arguments are given to the
411 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100412 */
413static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
414{
415 switch (lua_type(L, ud)) {
416
417 case LUA_TNUMBER:
418 case LUA_TBOOLEAN:
419 arg->type = ARGT_SINT;
420 arg->data.sint = lua_tointeger(L, ud);
421 break;
422
423 case LUA_TSTRING:
424 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200425 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200426 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200427 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200428 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100429 break;
430
431 case LUA_TUSERDATA:
432 case LUA_TNIL:
433 case LUA_TTABLE:
434 case LUA_TFUNCTION:
435 case LUA_TTHREAD:
436 case LUA_TLIGHTUSERDATA:
437 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200438 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100439 break;
440 }
441 return 1;
442}
443
444/* the following functions are used to convert a struct sample
445 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500446 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100448static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200450 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100451 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200453 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100454 break;
455
456 case SMP_T_BIN:
457 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200458 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100459 break;
460
461 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200462 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100463 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
464 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
465 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
466 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
467 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
468 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
469 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
470 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
471 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200472 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100473 break;
474 default:
475 lua_pushnil(L);
476 break;
477 }
478 break;
479
480 case SMP_T_IPV4:
481 case SMP_T_IPV6:
482 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200483 if (sample_casts[smp->data.type][SMP_T_STR] &&
484 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200485 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100486 else
487 lua_pushnil(L);
488 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100489 default:
490 lua_pushnil(L);
491 break;
492 }
493 return 1;
494}
495
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100496/* the following functions are used to convert a struct sample
497 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500498 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100499 */
500static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
501{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200502 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100503
504 case SMP_T_BIN:
505 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200506 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100507 break;
508
509 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200510 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100511 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
512 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
513 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
514 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
515 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
516 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
517 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
518 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
519 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200520 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100521 break;
522 default:
523 lua_pushstring(L, "");
524 break;
525 }
526 break;
527
528 case SMP_T_SINT:
529 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100530 case SMP_T_IPV4:
531 case SMP_T_IPV6:
532 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200533 if (sample_casts[smp->data.type][SMP_T_STR] &&
534 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200535 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100536 else
537 lua_pushstring(L, "");
538 break;
539 default:
540 lua_pushstring(L, "");
541 break;
542 }
543 return 1;
544}
545
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100546/* the following functions are used to convert an Lua type in a
547 * struct sample. This is useful to provide data from a converter
548 * to the LUA code.
549 */
550static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
551{
552 switch (lua_type(L, ud)) {
553
554 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200555 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100557 break;
558
559
560 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200561 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200562 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100563 break;
564
565 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200566 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100567 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200568 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200569 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200570 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200571 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100572 break;
573
574 case LUA_TUSERDATA:
575 case LUA_TNIL:
576 case LUA_TTABLE:
577 case LUA_TFUNCTION:
578 case LUA_TTHREAD:
579 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200580 case LUA_TNONE:
581 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200582 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200583 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100584 break;
585 }
586 return 1;
587}
588
Ilya Shipitsind4259502020-04-08 01:07:56 +0500589/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800590 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100591 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100592 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500593 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100594 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100595 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100596__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100597 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100598{
599 int min_arg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200600 int i, idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100601 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200602 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200603 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200604 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200605 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100606
607 idx = 0;
608 min_arg = ARGM(mask);
609 mask >>= ARGM_BITS;
610
611 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200612 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100613
614 /* Check oversize. */
615 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200616 msg = "Malformed argument mask";
617 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100618 }
619
620 /* Check for mandatory arguments. */
621 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100622 if (idx < min_arg) {
623
624 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200625 if (idx > 0) {
626 msg = "Mandatory argument expected";
627 goto error;
628 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100629
630 /* If first argument have a certain type, some default values
631 * may be used. See the function smp_resolve_args().
632 */
633 switch (mask & ARGT_MASK) {
634
635 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200636 if (!(p->cap & PR_CAP_FE)) {
637 msg = "Mandatory argument expected";
638 goto error;
639 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100640 argp[idx].data.prx = p;
641 argp[idx].type = ARGT_FE;
642 argp[idx+1].type = ARGT_STOP;
643 break;
644
645 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200646 if (!(p->cap & PR_CAP_BE)) {
647 msg = "Mandatory argument expected";
648 goto error;
649 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100650 argp[idx].data.prx = p;
651 argp[idx].type = ARGT_BE;
652 argp[idx+1].type = ARGT_STOP;
653 break;
654
655 case ARGT_TAB:
656 argp[idx].data.prx = p;
657 argp[idx].type = ARGT_TAB;
658 argp[idx+1].type = ARGT_STOP;
659 break;
660
661 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200662 msg = "Mandatory argument expected";
663 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100664 break;
665 }
666 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200667 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100668 }
669
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500670 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100671 if ((mask & ARGT_MASK) == ARGT_STOP &&
672 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200673 msg = "Last argument expected";
674 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100675 }
676
677 if ((mask & ARGT_MASK) == ARGT_STOP &&
678 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200679 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100680 }
681
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200682 /* Convert some argument types. All string in argp[] are for not
683 * duplicated yet.
684 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100685 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100686 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200687 if (argp[idx].type != ARGT_SINT) {
688 msg = "integer expected";
689 goto error;
690 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100691 argp[idx].type = ARGT_SINT;
692 break;
693
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100694 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200695 if (argp[idx].type != ARGT_SINT) {
696 msg = "integer expected";
697 goto error;
698 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200699 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100700 break;
701
702 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200703 if (argp[idx].type != ARGT_SINT) {
704 msg = "integer expected";
705 goto error;
706 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200707 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100708 break;
709
710 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200711 if (argp[idx].type != ARGT_STR) {
712 msg = "string expected";
713 goto error;
714 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200715 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200716 if (!argp[idx].data.prx) {
717 msg = "frontend doesn't exist";
718 goto error;
719 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100720 argp[idx].type = ARGT_FE;
721 break;
722
723 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200724 if (argp[idx].type != ARGT_STR) {
725 msg = "string expected";
726 goto error;
727 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200728 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200729 if (!argp[idx].data.prx) {
730 msg = "backend doesn't exist";
731 goto error;
732 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100733 argp[idx].type = ARGT_BE;
734 break;
735
736 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200737 if (argp[idx].type != ARGT_STR) {
738 msg = "string expected";
739 goto error;
740 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200741 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200742 if (!argp[idx].data.t) {
743 msg = "table doesn't exist";
744 goto error;
745 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100746 argp[idx].type = ARGT_TAB;
747 break;
748
749 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200750 if (argp[idx].type != ARGT_STR) {
751 msg = "string expected";
752 goto error;
753 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200754 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100755 if (sname) {
756 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200757 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200758 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200759 if (!px) {
760 msg = "backend doesn't exist";
761 goto error;
762 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100763 }
764 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200765 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100766 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100767 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100768 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200769 if (!argp[idx].data.srv) {
770 msg = "server doesn't exist";
771 goto error;
772 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100773 argp[idx].type = ARGT_SRV;
774 break;
775
776 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200777 if (argp[idx].type != ARGT_STR) {
778 msg = "string expected";
779 goto error;
780 }
781 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
782 msg = "invalid IPv4 address";
783 goto error;
784 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100785 argp[idx].type = ARGT_IPV4;
786 break;
787
788 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200789 if (argp[idx].type == ARGT_SINT)
790 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
791 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200792 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
793 msg = "invalid IPv4 mask";
794 goto error;
795 }
796 }
797 else {
798 msg = "integer or string expected";
799 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200800 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100801 argp[idx].type = ARGT_MSK4;
802 break;
803
804 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200805 if (argp[idx].type != ARGT_STR) {
806 msg = "string expected";
807 goto error;
808 }
809 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
810 msg = "invalid IPv6 address";
811 goto error;
812 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100813 argp[idx].type = ARGT_IPV6;
814 break;
815
816 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200817 if (argp[idx].type == ARGT_SINT)
818 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
819 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200820 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
821 msg = "invalid IPv6 mask";
822 goto error;
823 }
824 }
825 else {
826 msg = "integer or string expected";
827 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200828 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100829 argp[idx].type = ARGT_MSK6;
830 break;
831
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200832 case ARGT_REG:
833 if (argp[idx].type != ARGT_STR) {
834 msg = "string expected";
835 goto error;
836 }
837 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
838 if (!reg) {
839 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
840 argp[idx].data.str.area, err);
841 free(err);
842 goto error;
843 }
844 argp[idx].type = ARGT_REG;
845 argp[idx].data.reg = reg;
846 break;
847
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100848 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200849 if (argp[idx].type != ARGT_STR) {
850 msg = "string expected";
851 goto error;
852 }
853 if (p->uri_auth && p->uri_auth->userlist &&
854 !strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area))
855 ul = p->uri_auth->userlist;
856 else
857 ul = auth_find_userlist(argp[idx].data.str.area);
858
859 if (!ul) {
860 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
861 goto error;
862 }
863 argp[idx].type = ARGT_USR;
864 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200865 break;
866
867 case ARGT_STR:
868 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
869 msg = "unable to duplicate string arg";
870 goto error;
871 }
872 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100873 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200874
Christopher Fauletd25d9262020-08-06 11:04:46 +0200875 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200876 msg = "type not yet supported";
877 goto error;
878 break;
879
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100880 }
881
882 /* Check for type of argument. */
883 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200884 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
885 arg_type_names[(mask & ARGT_MASK)],
886 arg_type_names[argp[idx].type & ARGT_MASK]);
887 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100888 }
889
890 /* Next argument. */
891 mask >>= ARGT_BITS;
892 idx++;
893 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200894 return 0;
895
896 error:
897 for (i = 0; i < idx; i++) {
898 if (argp[i].type == ARGT_STR)
899 chunk_destroy(&argp[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200900 else if (argp[i].type == ARGT_REG)
901 regex_free(argp[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200902 }
903 WILL_LJMP(luaL_argerror(L, first + idx, msg));
904 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100905}
906
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500908 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100909 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100910 *
911 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100912 * - hlua_sethlua : create the association between hlua context and lua_state.
913 */
914static inline struct hlua *hlua_gethlua(lua_State *L)
915{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100916 struct hlua **hlua = lua_getextraspace(L);
917 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100918}
919static inline void hlua_sethlua(struct hlua *hlua)
920{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100921 struct hlua **hlua_store = lua_getextraspace(hlua->T);
922 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100923}
924
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100925/* This function is used to send logs. It try to send on screen (stderr)
926 * and on the default syslog server.
927 */
928static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
929{
930 struct tm tm;
931 char *p;
932
933 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200934 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100935 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200936 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200937 /* Break the message if exceed the buffer size. */
938 *(p-4) = ' ';
939 *(p-3) = '.';
940 *(p-2) = '.';
941 *(p-1) = '.';
942 break;
943 }
Willy Tarreau90807112020-02-25 08:16:33 +0100944 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100945 *p = *msg;
946 else
947 *p = '.';
948 }
949 *p = '\0';
950
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200951 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100952 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +0200953 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
954 return;
955
Willy Tarreaua678b432015-08-28 10:14:59 +0200956 get_localtime(date.tv_sec, &tm);
957 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100958 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200959 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100960 fflush(stderr);
961 }
962}
963
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100964/* This function just ensure that the yield will be always
965 * returned with a timeout and permit to set some flags
966 */
967__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100968 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100969{
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100970 struct hlua *hlua;
971
972 /* Get hlua struct, or NULL if we execute from main lua state */
973 hlua = hlua_gethlua(L);
974 if (!hlua) {
975 return;
976 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100977
978 /* Set the wake timeout. If timeout is required, we set
979 * the expiration time.
980 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200981 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100982
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100983 hlua->flags |= flags;
984
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100985 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200986 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100987}
988
Willy Tarreau87b09662015-04-03 00:22:06 +0200989/* This function initialises the Lua environment stored in the stream.
990 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100991 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200992 *
993 * This function is particular. it initialises a new Lua thread. If the
994 * initialisation fails (example: out of memory error), the lua function
995 * throws an error (longjmp).
996 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100997 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500998 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100999 * threads appear, the safe environment set a lock to ensure only one
1000 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001001 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001002 *
1003 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001004 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001005 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001006 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001007 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001008 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001009 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001010 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001011int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001012{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001013 if (!already_safe) {
1014 if (!SET_SAFE_LJMP(gL.T)) {
1015 lua->Tref = LUA_REFNIL;
1016 return 0;
1017 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001018 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001019 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001020 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001021 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001022 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001023 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001024 lua->T = lua_newthread(gL.T);
1025 if (!lua->T) {
1026 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001027 if (!already_safe)
1028 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001029 return 0;
1030 }
1031 hlua_sethlua(lua);
1032 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1033 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001034 if (!already_safe)
1035 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001036 return 1;
1037}
1038
Willy Tarreau87b09662015-04-03 00:22:06 +02001039/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001040 * is destroyed. The destroy also the memory context. The struct "lua"
1041 * is not freed.
1042 */
1043void hlua_ctx_destroy(struct hlua *lua)
1044{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001045 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001046 return;
1047
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001048 if (!lua->T)
1049 goto end;
1050
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001051 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001052 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001053
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001054 if (!SET_SAFE_LJMP(lua->T))
1055 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001056 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001057 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001058
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001059 if (!SET_SAFE_LJMP(gL.T))
1060 return;
1061 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1062 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001063 /* Forces a garbage collecting process. If the Lua program is finished
1064 * without error, we run the GC on the thread pointer. Its freed all
1065 * the unused memory.
1066 * If the thread is finnish with an error or is currently yielded,
1067 * it seems that the GC applied on the thread doesn't clean anything,
1068 * so e run the GC on the main thread.
1069 * NOTE: maybe this action locks all the Lua threads untiml the en of
1070 * the garbage collection.
1071 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001072 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +02001073 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001074 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +02001075 lua_gc(gL.T, LUA_GCCOLLECT, 0);
1076 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001077 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001078
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001079 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001080
1081end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001082 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001083}
1084
1085/* This function is used to restore the Lua context when a coroutine
1086 * fails. This function copy the common memory between old coroutine
1087 * and the new coroutine. The old coroutine is destroyed, and its
1088 * replaced by the new coroutine.
1089 * If the flag "keep_msg" is set, the last entry of the old is assumed
1090 * as string error message and it is copied in the new stack.
1091 */
1092static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1093{
1094 lua_State *T;
1095 int new_ref;
1096
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001097 /* New Lua coroutine. */
1098 T = lua_newthread(gL.T);
1099 if (!T)
1100 return 0;
1101
1102 /* Copy last error message. */
1103 if (keep_msg)
1104 lua_xmove(lua->T, T, 1);
1105
1106 /* Copy data between the coroutines. */
1107 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1108 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001109 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001110
1111 /* Destroy old data. */
1112 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1113
1114 /* The thread is garbage collected by Lua. */
1115 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1116
1117 /* Fill the struct with the new coroutine values. */
1118 lua->Mref = new_ref;
1119 lua->T = T;
1120 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1121
1122 /* Set context. */
1123 hlua_sethlua(lua);
1124
1125 return 1;
1126}
1127
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001128void hlua_hook(lua_State *L, lua_Debug *ar)
1129{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001130 struct hlua *hlua;
1131
1132 /* Get hlua struct, or NULL if we execute from main lua state */
1133 hlua = hlua_gethlua(L);
1134 if (!hlua)
1135 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001136
1137 /* Lua cannot yield when its returning from a function,
1138 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001139 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001140 */
1141 if (lua_gethookmask(L) & LUA_MASKRET) {
1142 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1143 return;
1144 }
1145
1146 /* restore the interrupt condition. */
1147 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1148
1149 /* If we interrupt the Lua processing in yieldable state, we yield.
1150 * If the state is not yieldable, trying yield causes an error.
1151 */
1152 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001153 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001154
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001155 /* If we cannot yield, update the clock and check the timeout. */
1156 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001157 hlua->run_time += now_ms - hlua->start_time;
1158 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001159 lua_pushfstring(L, "execution timeout");
1160 WILL_LJMP(lua_error(L));
1161 }
1162
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001163 /* Update the start time. */
1164 hlua->start_time = now_ms;
1165
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001166 /* Try to interrupt the process at the end of the current
1167 * unyieldable function.
1168 */
1169 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001170}
1171
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001172/* This function start or resumes the Lua stack execution. If the flag
1173 * "yield_allowed" if no set and the LUA stack execution returns a yield
1174 * The function return an error.
1175 *
1176 * The function can returns 4 values:
1177 * - HLUA_E_OK : The execution is terminated without any errors.
1178 * - HLUA_E_AGAIN : The execution must continue at the next associated
1179 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001180 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001181 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001182 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001183 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001184 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001185 * LUA code.
1186 */
1187static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1188{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001189#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1190 int nres;
1191#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001192 int ret;
1193 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001194 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001195
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001196 /* Initialise run time counter. */
1197 if (!HLUA_IS_RUNNING(lua))
1198 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001199
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001200 /* Lock the whole Lua execution. This lock must be before the
1201 * label "resume_execution".
1202 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001203 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001204
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001205resume_execution:
1206
1207 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1208 * instructions. it is used for preventing infinite loops.
1209 */
1210 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1211
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001212 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001213 HLUA_SET_RUN(lua);
1214 HLUA_CLR_CTRLYIELD(lua);
1215 HLUA_CLR_WAKERESWR(lua);
1216 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001217
Christopher Fauletbc275a92020-02-26 14:55:16 +01001218 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001219 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001220 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001221
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001222 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001223#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1224 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1225#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001226 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001227#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001228 switch (ret) {
1229
1230 case LUA_OK:
1231 ret = HLUA_E_OK;
1232 break;
1233
1234 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001235 /* Check if the execution timeout is expired. It it is the case, we
1236 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001237 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001238 tv_update_date(0, 1);
1239 lua->run_time += now_ms - lua->start_time;
1240 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001241 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001242 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001243 break;
1244 }
1245 /* Process the forced yield. if the general yield is not allowed or
1246 * if no task were associated this the current Lua execution
1247 * coroutine, we resume the execution. Else we want to return in the
1248 * scheduler and we want to be waked up again, to continue the
1249 * current Lua execution. So we schedule our own task.
1250 */
1251 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001252 if (!yield_allowed || !lua->task)
1253 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001254 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001255 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001256 if (!yield_allowed) {
1257 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001258 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001259 break;
1260 }
1261 ret = HLUA_E_AGAIN;
1262 break;
1263
1264 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001265
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001266 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001267 * because the errors ares the only one mean to return immediately
1268 * from and lua execution.
1269 */
1270 if (lua->flags & HLUA_EXIT) {
1271 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001272 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001273 break;
1274 }
1275
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001276 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001277 if (!lua_checkstack(lua->T, 1)) {
1278 ret = HLUA_E_ERR;
1279 break;
1280 }
1281 msg = lua_tostring(lua->T, -1);
1282 lua_settop(lua->T, 0); /* Empty the stack. */
1283 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001284 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001285 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001286 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001287 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001288 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001289 ret = HLUA_E_ERRMSG;
1290 break;
1291
1292 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001293 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001294 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001295 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001296 break;
1297
1298 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001299 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001300 if (!lua_checkstack(lua->T, 1)) {
1301 ret = HLUA_E_ERR;
1302 break;
1303 }
1304 msg = lua_tostring(lua->T, -1);
1305 lua_settop(lua->T, 0); /* Empty the stack. */
1306 lua_pop(lua->T, 1);
1307 if (msg)
1308 lua_pushfstring(lua->T, "message handler error: %s", msg);
1309 else
1310 lua_pushfstring(lua->T, "message handler error");
1311 ret = HLUA_E_ERRMSG;
1312 break;
1313
1314 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001315 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001316 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001317 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001318 break;
1319 }
1320
1321 switch (ret) {
1322 case HLUA_E_AGAIN:
1323 break;
1324
1325 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001326 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001327 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001328 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001329 break;
1330
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001331 case HLUA_E_ETMOUT:
1332 case HLUA_E_NOMEM:
1333 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001334 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001335 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001336 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001337 hlua_ctx_renew(lua, 0);
1338 break;
1339
1340 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001341 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001342 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001343 break;
1344 }
1345
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001346 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001347 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001348
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001349 return ret;
1350}
1351
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001352/* This function exit the current code. */
1353__LJMP static int hlua_done(lua_State *L)
1354{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001355 struct hlua *hlua;
1356
1357 /* Get hlua struct, or NULL if we execute from main lua state */
1358 hlua = hlua_gethlua(L);
1359 if (!hlua)
1360 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001361
1362 hlua->flags |= HLUA_EXIT;
1363 WILL_LJMP(lua_error(L));
1364
1365 return 0;
1366}
1367
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001368/* This function is an LUA binding. It provides a function
1369 * for deleting ACL from a referenced ACL file.
1370 */
1371__LJMP static int hlua_del_acl(lua_State *L)
1372{
1373 const char *name;
1374 const char *key;
1375 struct pat_ref *ref;
1376
1377 MAY_LJMP(check_args(L, 2, "del_acl"));
1378
1379 name = MAY_LJMP(luaL_checkstring(L, 1));
1380 key = MAY_LJMP(luaL_checkstring(L, 2));
1381
1382 ref = pat_ref_lookup(name);
1383 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001384 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001385
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001386 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001387 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001388 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001389 return 0;
1390}
1391
1392/* This function is an LUA binding. It provides a function
1393 * for deleting map entry from a referenced map file.
1394 */
1395static int hlua_del_map(lua_State *L)
1396{
1397 const char *name;
1398 const char *key;
1399 struct pat_ref *ref;
1400
1401 MAY_LJMP(check_args(L, 2, "del_map"));
1402
1403 name = MAY_LJMP(luaL_checkstring(L, 1));
1404 key = MAY_LJMP(luaL_checkstring(L, 2));
1405
1406 ref = pat_ref_lookup(name);
1407 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001408 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001409
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001410 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001411 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001412 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001413 return 0;
1414}
1415
1416/* This function is an LUA binding. It provides a function
1417 * for adding ACL pattern from a referenced ACL file.
1418 */
1419static int hlua_add_acl(lua_State *L)
1420{
1421 const char *name;
1422 const char *key;
1423 struct pat_ref *ref;
1424
1425 MAY_LJMP(check_args(L, 2, "add_acl"));
1426
1427 name = MAY_LJMP(luaL_checkstring(L, 1));
1428 key = MAY_LJMP(luaL_checkstring(L, 2));
1429
1430 ref = pat_ref_lookup(name);
1431 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001432 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001433
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001434 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001435 if (pat_ref_find_elt(ref, key) == NULL)
1436 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001437 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001438 return 0;
1439}
1440
1441/* This function is an LUA binding. It provides a function
1442 * for setting map pattern and sample from a referenced map
1443 * file.
1444 */
1445static int hlua_set_map(lua_State *L)
1446{
1447 const char *name;
1448 const char *key;
1449 const char *value;
1450 struct pat_ref *ref;
1451
1452 MAY_LJMP(check_args(L, 3, "set_map"));
1453
1454 name = MAY_LJMP(luaL_checkstring(L, 1));
1455 key = MAY_LJMP(luaL_checkstring(L, 2));
1456 value = MAY_LJMP(luaL_checkstring(L, 3));
1457
1458 ref = pat_ref_lookup(name);
1459 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001460 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001461
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001462 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001463 if (pat_ref_find_elt(ref, key) != NULL)
1464 pat_ref_set(ref, key, value, NULL);
1465 else
1466 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001467 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001468 return 0;
1469}
1470
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001471/* A class is a lot of memory that contain data. This data can be a table,
1472 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001473 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001474 * the name of the object (_G[<name>] = <metable> ).
1475 *
1476 * A metable is a table that modify the standard behavior of a standard
1477 * access to the associated data. The entries of this new metatable are
1478 * defined as is:
1479 *
1480 * http://lua-users.org/wiki/MetatableEvents
1481 *
1482 * __index
1483 *
1484 * we access an absent field in a table, the result is nil. This is
1485 * true, but it is not the whole truth. Actually, such access triggers
1486 * the interpreter to look for an __index metamethod: If there is no
1487 * such method, as usually happens, then the access results in nil;
1488 * otherwise, the metamethod will provide the result.
1489 *
1490 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1491 * the key does not appear in the table, but the metatable has an __index
1492 * property:
1493 *
1494 * - if the value is a function, the function is called, passing in the
1495 * table and the key; the return value of that function is returned as
1496 * the result.
1497 *
1498 * - if the value is another table, the value of the key in that table is
1499 * asked for and returned (and if it doesn't exist in that table, but that
1500 * table's metatable has an __index property, then it continues on up)
1501 *
1502 * - Use "rawget(myTable,key)" to skip this metamethod.
1503 *
1504 * http://www.lua.org/pil/13.4.1.html
1505 *
1506 * __newindex
1507 *
1508 * Like __index, but control property assignment.
1509 *
1510 * __mode - Control weak references. A string value with one or both
1511 * of the characters 'k' and 'v' which specifies that the the
1512 * keys and/or values in the table are weak references.
1513 *
1514 * __call - Treat a table like a function. When a table is followed by
1515 * parenthesis such as "myTable( 'foo' )" and the metatable has
1516 * a __call key pointing to a function, that function is invoked
1517 * (passing any specified arguments) and the return value is
1518 * returned.
1519 *
1520 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1521 * called, if the metatable for myTable has a __metatable
1522 * key, the value of that key is returned instead of the
1523 * actual metatable.
1524 *
1525 * __tostring - Control string representation. When the builtin
1526 * "tostring( myTable )" function is called, if the metatable
1527 * for myTable has a __tostring property set to a function,
1528 * that function is invoked (passing myTable to it) and the
1529 * return value is used as the string representation.
1530 *
1531 * __len - Control table length. When the table length is requested using
1532 * the length operator ( '#' ), if the metatable for myTable has
1533 * a __len key pointing to a function, that function is invoked
1534 * (passing myTable to it) and the return value used as the value
1535 * of "#myTable".
1536 *
1537 * __gc - Userdata finalizer code. When userdata is set to be garbage
1538 * collected, if the metatable has a __gc field pointing to a
1539 * function, that function is first invoked, passing the userdata
1540 * to it. The __gc metamethod is not called for tables.
1541 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1542 *
1543 * Special metamethods for redefining standard operators:
1544 * http://www.lua.org/pil/13.1.html
1545 *
1546 * __add "+"
1547 * __sub "-"
1548 * __mul "*"
1549 * __div "/"
1550 * __unm "!"
1551 * __pow "^"
1552 * __concat ".."
1553 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001554 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001555 * http://www.lua.org/pil/13.2.html
1556 *
1557 * __eq "=="
1558 * __lt "<"
1559 * __le "<="
1560 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001561
1562/*
1563 *
1564 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001565 * Class Map
1566 *
1567 *
1568 */
1569
1570/* Returns a struct hlua_map if the stack entry "ud" is
1571 * a class session, otherwise it throws an error.
1572 */
1573__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1574{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001575 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001576}
1577
1578/* This function is the map constructor. It don't need
1579 * the class Map object. It creates and return a new Map
1580 * object. It must be called only during "body" or "init"
1581 * context because it process some filesystem accesses.
1582 */
1583__LJMP static int hlua_map_new(struct lua_State *L)
1584{
1585 const char *fn;
1586 int match = PAT_MATCH_STR;
1587 struct sample_conv conv;
1588 const char *file = "";
1589 int line = 0;
1590 lua_Debug ar;
1591 char *err = NULL;
1592 struct arg args[2];
1593
1594 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1595 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1596
1597 fn = MAY_LJMP(luaL_checkstring(L, 1));
1598
1599 if (lua_gettop(L) >= 2) {
1600 match = MAY_LJMP(luaL_checkinteger(L, 2));
1601 if (match < 0 || match >= PAT_MATCH_NUM)
1602 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1603 }
1604
1605 /* Get Lua filename and line number. */
1606 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1607 lua_getinfo(L, "Sl", &ar); /* get info about it */
1608 if (ar.currentline > 0) { /* is there info? */
1609 file = ar.short_src;
1610 line = ar.currentline;
1611 }
1612 }
1613
1614 /* fill fake sample_conv struct. */
1615 conv.kw = ""; /* unused. */
1616 conv.process = NULL; /* unused. */
1617 conv.arg_mask = 0; /* unused. */
1618 conv.val_args = NULL; /* unused. */
1619 conv.out_type = SMP_T_STR;
1620 conv.private = (void *)(long)match;
1621 switch (match) {
1622 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1623 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1624 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1625 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1626 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1627 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1628 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001629 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001630 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1631 default:
1632 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1633 }
1634
1635 /* fill fake args. */
1636 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001637 args[0].data.str.area = strdup(fn);
1638 args[0].data.str.data = strlen(fn);
1639 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001640 args[1].type = ARGT_STOP;
1641
1642 /* load the map. */
1643 if (!sample_load_map(args, &conv, file, line, &err)) {
1644 /* error case: we cant use luaL_error because we must
1645 * free the err variable.
1646 */
1647 luaL_where(L, 1);
1648 lua_pushfstring(L, "'new': %s.", err);
1649 lua_concat(L, 2);
1650 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001651 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001652 WILL_LJMP(lua_error(L));
1653 }
1654
1655 /* create the lua object. */
1656 lua_newtable(L);
1657 lua_pushlightuserdata(L, args[0].data.map);
1658 lua_rawseti(L, -2, 0);
1659
1660 /* Pop a class Map metatable and affect it to the userdata. */
1661 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1662 lua_setmetatable(L, -2);
1663
1664
1665 return 1;
1666}
1667
1668__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1669{
1670 struct map_descriptor *desc;
1671 struct pattern *pat;
1672 struct sample smp;
1673
1674 MAY_LJMP(check_args(L, 2, "lookup"));
1675 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001676 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001677 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001678 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001679 }
1680 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001681 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001682 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001683 smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data));
Thierry Fournier91dc0c02020-11-10 20:38:20 +01001684 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001685 }
1686
1687 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001688 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001689 if (str)
1690 lua_pushstring(L, "");
1691 else
1692 lua_pushnil(L);
1693 return 1;
1694 }
1695
1696 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001697 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001698 return 1;
1699}
1700
1701__LJMP static int hlua_map_lookup(struct lua_State *L)
1702{
1703 return _hlua_map_lookup(L, 0);
1704}
1705
1706__LJMP static int hlua_map_slookup(struct lua_State *L)
1707{
1708 return _hlua_map_lookup(L, 1);
1709}
1710
1711/*
1712 *
1713 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001714 * Class Socket
1715 *
1716 *
1717 */
1718
1719__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1720{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001721 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001722}
1723
1724/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001725 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001726 * received.
1727 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001728static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001729{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001730 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001731
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001732 if (appctx->ctx.hlua_cosocket.die) {
1733 si_shutw(si);
1734 si_shutr(si);
1735 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001736 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1737 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001738 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1739 }
1740
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001741 /* If we cant write, wakeup the pending write signals. */
1742 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001743 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001744
1745 /* If we cant read, wakeup the pending read signals. */
1746 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001747 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001748
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001749 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001750 * to be notified whenever the connection completes.
1751 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001752 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001753 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001754 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001755 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001756 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001757 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001758
1759 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001760 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001761
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001762 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001763 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001764 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001765
1766 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001767 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001768 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001769
1770 /* Some data were injected in the buffer, notify the stream
1771 * interface.
1772 */
1773 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001774 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001775
1776 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001777 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001778 */
1779 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001780 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001781}
1782
Willy Tarreau87b09662015-04-03 00:22:06 +02001783/* This function is called when the "struct stream" is destroyed.
1784 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001785 * Wake all the pending signals.
1786 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001787static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001788{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001789 struct xref *peer;
1790
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001791 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001792 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1793 if (peer)
1794 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001795
1796 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001797 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1798 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001799}
1800
1801/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001802 * uses this object. If the stream does not exists, just quit.
1803 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001804 * pending signal can rest in the read and write lists. destroy
1805 * it.
1806 */
1807__LJMP static int hlua_socket_gc(lua_State *L)
1808{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001809 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001810 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001811 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001812
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001813 MAY_LJMP(check_args(L, 1, "__gc"));
1814
1815 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001816 peer = xref_get_peer_and_lock(&socket->xref);
1817 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001819 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001820
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001821 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001822 appctx->ctx.hlua_cosocket.die = 1;
1823 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001824
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001825 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001826 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001827 return 0;
1828}
1829
1830/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001831 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832 */
sada05ed3302018-05-11 11:48:18 -07001833__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001834{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001835 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001836 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001837 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001838 struct hlua *hlua;
1839
1840 /* Get hlua struct, or NULL if we execute from main lua state */
1841 hlua = hlua_gethlua(L);
1842 if (!hlua)
1843 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001844
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001845 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001846
1847 /* Check if we run on the same thread than the xreator thread.
1848 * We cannot access to the socket if the thread is different.
1849 */
1850 if (socket->tid != tid)
1851 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1852
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001853 peer = xref_get_peer_and_lock(&socket->xref);
1854 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001855 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001856
1857 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001858 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001859
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001860 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001861 appctx->ctx.hlua_cosocket.die = 1;
1862 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001863
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001864 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001865 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001866 return 0;
1867}
1868
sada05ed3302018-05-11 11:48:18 -07001869/* The close function calls close_helper.
1870 */
1871__LJMP static int hlua_socket_close(lua_State *L)
1872{
1873 MAY_LJMP(check_args(L, 1, "close"));
1874 return hlua_socket_close_helper(L);
1875}
1876
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001877/* This Lua function assumes that the stack contain three parameters.
1878 * 1 - USERDATA containing a struct socket
1879 * 2 - INTEGER with values of the macro defined below
1880 * If the integer is -1, we must read at most one line.
1881 * If the integer is -2, we ust read all the data until the
1882 * end of the stream.
1883 * If the integer is positive value, we must read a number of
1884 * bytes corresponding to this value.
1885 */
1886#define HLSR_READ_LINE (-1)
1887#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001888__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001889{
1890 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1891 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001892 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001893 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001894 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001895 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001896 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001897 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001898 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001899 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001900 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001901 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001902 struct stream_interface *si;
1903 struct stream *s;
1904 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001905 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001907 /* Get hlua struct, or NULL if we execute from main lua state */
1908 hlua = hlua_gethlua(L);
1909
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001910 /* Check if this lua stack is schedulable. */
1911 if (!hlua || !hlua->task)
1912 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1913 "'frontend', 'backend' or 'task'"));
1914
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001915 /* Check if we run on the same thread than the xreator thread.
1916 * We cannot access to the socket if the thread is different.
1917 */
1918 if (socket->tid != tid)
1919 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1920
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001921 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001922 peer = xref_get_peer_and_lock(&socket->xref);
1923 if (!peer)
1924 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001925 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1926 si = appctx->owner;
1927 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001928
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001929 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001930 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001931 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001932 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001933 if (nblk < 0) /* Connection close. */
1934 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001935 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001936 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001937
1938 /* remove final \r\n. */
1939 if (nblk == 1) {
1940 if (blk1[len1-1] == '\n') {
1941 len1--;
1942 skip_at_end++;
1943 if (blk1[len1-1] == '\r') {
1944 len1--;
1945 skip_at_end++;
1946 }
1947 }
1948 }
1949 else {
1950 if (blk2[len2-1] == '\n') {
1951 len2--;
1952 skip_at_end++;
1953 if (blk2[len2-1] == '\r') {
1954 len2--;
1955 skip_at_end++;
1956 }
1957 }
1958 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001959 }
1960
1961 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001963 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001964 if (nblk < 0) /* Connection close. */
1965 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001966 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001967 goto connection_empty;
1968 }
1969
1970 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001971 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001972 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001973 if (nblk < 0) /* Connection close. */
1974 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001975 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001976 goto connection_empty;
1977
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001978 missing_bytes = wanted - socket->b.n;
1979 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001980 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001981 len1 = missing_bytes;
1982 } if (nblk == 2 && len1 + len2 > missing_bytes)
1983 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001984 }
1985
1986 len = len1;
1987
1988 luaL_addlstring(&socket->b, blk1, len1);
1989 if (nblk == 2) {
1990 len += len2;
1991 luaL_addlstring(&socket->b, blk2, len2);
1992 }
1993
1994 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001995 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001996
1997 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001998 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001999
2000 /* If the pattern reclaim to read all the data
2001 * in the connection, got out.
2002 */
2003 if (wanted == HLSR_READ_ALL)
2004 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002005 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002006 goto connection_empty;
2007
2008 /* Return result. */
2009 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002010 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002011 return 1;
2012
2013connection_closed:
2014
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002015 xref_unlock(&socket->xref, peer);
2016
2017no_peer:
2018
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002019 /* If the buffer containds data. */
2020 if (socket->b.n > 0) {
2021 luaL_pushresult(&socket->b);
2022 return 1;
2023 }
2024 lua_pushnil(L);
2025 lua_pushstring(L, "connection closed.");
2026 return 2;
2027
2028connection_empty:
2029
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002030 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2031 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002032 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002033 }
2034 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002035 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002036 return 0;
2037}
2038
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002039/* This Lua function gets two parameters. The first one can be string
2040 * or a number. If the string is "*l", the user requires one line. If
2041 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002042 * If the value is a number, the user require a number of bytes equal
2043 * to the value. The default value is "*l" (a line).
2044 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002045 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002046 * integer takes this values:
2047 * -1 : read a line
2048 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002049 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002050 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002051 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002052 * concatenated with the read data.
2053 */
2054__LJMP static int hlua_socket_receive(struct lua_State *L)
2055{
2056 int wanted = HLSR_READ_LINE;
2057 const char *pattern;
2058 int type;
2059 char *error;
2060 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002061 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002062
2063 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2064 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2065
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002066 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002067
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002068 /* Check if we run on the same thread than the xreator thread.
2069 * We cannot access to the socket if the thread is different.
2070 */
2071 if (socket->tid != tid)
2072 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2073
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002074 /* check for pattern. */
2075 if (lua_gettop(L) >= 2) {
2076 type = lua_type(L, 2);
2077 if (type == LUA_TSTRING) {
2078 pattern = lua_tostring(L, 2);
2079 if (strcmp(pattern, "*a") == 0)
2080 wanted = HLSR_READ_ALL;
2081 else if (strcmp(pattern, "*l") == 0)
2082 wanted = HLSR_READ_LINE;
2083 else {
2084 wanted = strtoll(pattern, &error, 10);
2085 if (*error != '\0')
2086 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2087 }
2088 }
2089 else if (type == LUA_TNUMBER) {
2090 wanted = lua_tointeger(L, 2);
2091 if (wanted < 0)
2092 WILL_LJMP(luaL_error(L, "Unsupported size."));
2093 }
2094 }
2095
2096 /* Set pattern. */
2097 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002098
2099 /* Check if we would replace the top by itself. */
2100 if (lua_gettop(L) != 2)
2101 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002102
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002103 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002104 luaL_buffinit(L, &socket->b);
2105
2106 /* Check prefix. */
2107 if (lua_gettop(L) >= 3) {
2108 if (lua_type(L, 3) != LUA_TSTRING)
2109 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2110 pattern = lua_tolstring(L, 3, &len);
2111 luaL_addlstring(&socket->b, pattern, len);
2112 }
2113
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002114 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002115}
2116
2117/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002118 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002119 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002120static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002121{
2122 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002123 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002124 struct appctx *appctx;
2125 size_t buf_len;
2126 const char *buf;
2127 int len;
2128 int send_len;
2129 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002130 struct xref *peer;
2131 struct stream_interface *si;
2132 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002133
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002134 /* Get hlua struct, or NULL if we execute from main lua state */
2135 hlua = hlua_gethlua(L);
2136
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002137 /* Check if this lua stack is schedulable. */
2138 if (!hlua || !hlua->task)
2139 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2140 "'frontend', 'backend' or 'task'"));
2141
2142 /* Get object */
2143 socket = MAY_LJMP(hlua_checksocket(L, 1));
2144 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002145 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002146
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002147 /* Check if we run on the same thread than the xreator thread.
2148 * We cannot access to the socket if the thread is different.
2149 */
2150 if (socket->tid != tid)
2151 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2152
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002153 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002154 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002155 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002156 lua_pushinteger(L, -1);
2157 return 1;
2158 }
2159 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2160 si = appctx->owner;
2161 s = si_strm(si);
2162
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002163 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002164 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002165 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002166 lua_pushinteger(L, -1);
2167 return 1;
2168 }
2169
2170 /* Update the input buffer data. */
2171 buf += sent;
2172 send_len = buf_len - sent;
2173
2174 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002175 if (sent >= buf_len) {
2176 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002177 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002178 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002179
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002180 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002181 * the request buffer if its not required.
2182 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002183 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002184 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002185 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002186 }
2187
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002188 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002189 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002190 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002191 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002192 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002193
2194 /* send data */
2195 if (len < send_len)
2196 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002197 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002198
2199 /* "Not enough space" (-1), "Buffer too little to contain
2200 * the data" (-2) are not expected because the available length
2201 * is tested.
2202 * Other unknown error are also not expected.
2203 */
2204 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002205 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002206 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002207
sada05ed3302018-05-11 11:48:18 -07002208 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002209 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002210 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002211 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002212 return 1;
2213 }
2214
2215 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002216 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002217
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002218 s->req.rex = TICK_ETERNITY;
2219 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002220
2221 /* Update length sent. */
2222 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002223 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002224
2225 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002226 if (sent + len >= buf_len) {
2227 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002228 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002229 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002230
2231hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002232 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2233 xref_unlock(&socket->xref, peer);
2234 WILL_LJMP(luaL_error(L, "out of memory"));
2235 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002236 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002237 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002238 return 0;
2239}
2240
2241/* This function initiate the send of data. It just check the input
2242 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002243 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002244 * "hlua_socket_write_yield" that can yield.
2245 *
2246 * The Lua function gets between 3 and 4 parameters. The first one is
2247 * the associated object. The second is a string buffer. The third is
2248 * a facultative integer that represents where is the buffer position
2249 * of the start of the data that can send. The first byte is the
2250 * position "1". The default value is "1". The fourth argument is a
2251 * facultative integer that represents where is the buffer position
2252 * of the end of the data that can send. The default is the last byte.
2253 */
2254static int hlua_socket_send(struct lua_State *L)
2255{
2256 int i;
2257 int j;
2258 const char *buf;
2259 size_t buf_len;
2260
2261 /* Check number of arguments. */
2262 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2263 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2264
2265 /* Get the string. */
2266 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2267
2268 /* Get and check j. */
2269 if (lua_gettop(L) == 4) {
2270 j = MAY_LJMP(luaL_checkinteger(L, 4));
2271 if (j < 0)
2272 j = buf_len + j + 1;
2273 if (j > buf_len)
2274 j = buf_len + 1;
2275 lua_pop(L, 1);
2276 }
2277 else
2278 j = buf_len;
2279
2280 /* Get and check i. */
2281 if (lua_gettop(L) == 3) {
2282 i = MAY_LJMP(luaL_checkinteger(L, 3));
2283 if (i < 0)
2284 i = buf_len + i + 1;
2285 if (i > buf_len)
2286 i = buf_len + 1;
2287 lua_pop(L, 1);
2288 } else
2289 i = 1;
2290
2291 /* Check bth i and j. */
2292 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002293 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294 return 1;
2295 }
2296 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002297 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002298 return 1;
2299 }
2300 if (i == 0)
2301 i = 1;
2302 if (j == 0)
2303 j = 1;
2304
2305 /* Pop the string. */
2306 lua_pop(L, 1);
2307
2308 /* Update the buffer length. */
2309 buf += i - 1;
2310 buf_len = j - i + 1;
2311 lua_pushlstring(L, buf, buf_len);
2312
2313 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002314 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002315
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002316 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002317}
2318
Willy Tarreau22b0a682015-06-17 19:43:49 +02002319#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002320__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2321{
2322 static char buffer[SOCKET_INFO_MAX_LEN];
2323 int ret;
2324 int len;
2325 char *p;
2326
2327 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2328 if (ret <= 0) {
2329 lua_pushnil(L);
2330 return 1;
2331 }
2332
2333 if (ret == AF_UNIX) {
2334 lua_pushstring(L, buffer+1);
2335 return 1;
2336 }
2337 else if (ret == AF_INET6) {
2338 buffer[0] = '[';
2339 len = strlen(buffer);
2340 buffer[len] = ']';
2341 len++;
2342 buffer[len] = ':';
2343 len++;
2344 p = buffer;
2345 }
2346 else if (ret == AF_INET) {
2347 p = buffer + 1;
2348 len = strlen(p);
2349 p[len] = ':';
2350 len++;
2351 }
2352 else {
2353 lua_pushnil(L);
2354 return 1;
2355 }
2356
2357 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2358 lua_pushnil(L);
2359 return 1;
2360 }
2361
2362 lua_pushstring(L, p);
2363 return 1;
2364}
2365
2366/* Returns information about the peer of the connection. */
2367__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2368{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002369 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002370 struct xref *peer;
2371 struct appctx *appctx;
2372 struct stream_interface *si;
2373 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002374 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002375
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002376 MAY_LJMP(check_args(L, 1, "getpeername"));
2377
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002378 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002379
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002380 /* Check if we run on the same thread than the xreator thread.
2381 * We cannot access to the socket if the thread is different.
2382 */
2383 if (socket->tid != tid)
2384 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2385
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002386 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002387 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002388 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389 lua_pushnil(L);
2390 return 1;
2391 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002392 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2393 si = appctx->owner;
2394 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002395
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002396 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002397 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002398 lua_pushnil(L);
2399 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002400 }
2401
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002402 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002403 xref_unlock(&socket->xref, peer);
2404 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002405}
2406
2407/* Returns information about my connection side. */
2408static int hlua_socket_getsockname(struct lua_State *L)
2409{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002410 struct hlua_socket *socket;
2411 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002412 struct appctx *appctx;
2413 struct xref *peer;
2414 struct stream_interface *si;
2415 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002416 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002417
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002418 MAY_LJMP(check_args(L, 1, "getsockname"));
2419
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002420 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002421
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002422 /* Check if we run on the same thread than the xreator thread.
2423 * We cannot access to the socket if the thread is different.
2424 */
2425 if (socket->tid != tid)
2426 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2427
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002428 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002429 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002430 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002431 lua_pushnil(L);
2432 return 1;
2433 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002434 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2435 si = appctx->owner;
2436 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002437
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002438 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002439 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002440 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002441 lua_pushnil(L);
2442 return 1;
2443 }
2444
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002445 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002446 xref_unlock(&socket->xref, peer);
2447 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002448}
2449
2450/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002451static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002452 .obj_type = OBJ_TYPE_APPLET,
2453 .name = "<LUA_TCP>",
2454 .fct = hlua_socket_handler,
2455 .release = hlua_socket_release,
2456};
2457
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002458__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002459{
2460 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002461 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002462 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002463 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002464 struct stream_interface *si;
2465 struct stream *s;
2466
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002467 /* Get hlua struct, or NULL if we execute from main lua state */
2468 hlua = hlua_gethlua(L);
2469 if (!hlua)
2470 return 0;
2471
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002472 /* Check if we run on the same thread than the xreator thread.
2473 * We cannot access to the socket if the thread is different.
2474 */
2475 if (socket->tid != tid)
2476 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2477
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002478 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002479 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002480 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002481 lua_pushnil(L);
2482 lua_pushstring(L, "Can't connect");
2483 return 2;
2484 }
2485 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2486 si = appctx->owner;
2487 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002488
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002489 /* Check if we run on the same thread than the xreator thread.
2490 * We cannot access to the socket if the thread is different.
2491 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002492 if (socket->tid != tid) {
2493 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002494 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002495 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002496
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002497 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002498 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002499 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002500 lua_pushnil(L);
2501 lua_pushstring(L, "Can't connect");
2502 return 2;
2503 }
2504
Willy Tarreaue09101e2018-10-16 17:37:12 +02002505 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002506
2507 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002508 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002509 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002510 lua_pushinteger(L, 1);
2511 return 1;
2512 }
2513
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002514 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2515 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002516 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002517 }
2518 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002519 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002520 return 0;
2521}
2522
2523/* This function fail or initite the connection. */
2524__LJMP static int hlua_socket_connect(struct lua_State *L)
2525{
2526 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002527 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002528 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002529 struct hlua *hlua;
2530 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002531 int low, high;
2532 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002533 struct xref *peer;
2534 struct stream_interface *si;
2535 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002536
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002537 if (lua_gettop(L) < 2)
2538 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002539
2540 /* Get args. */
2541 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002542
2543 /* Check if we run on the same thread than the xreator thread.
2544 * We cannot access to the socket if the thread is different.
2545 */
2546 if (socket->tid != tid)
2547 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2548
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002549 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002550 if (lua_gettop(L) >= 3) {
2551 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002552 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002553
Tim Duesterhus6edab862018-01-06 19:04:45 +01002554 /* Force the ip to end with a colon, to support IPv6 addresses
2555 * that are not enclosed within square brackets.
2556 */
2557 if (port > 0) {
2558 luaL_buffinit(L, &b);
2559 luaL_addstring(&b, ip);
2560 luaL_addchar(&b, ':');
2561 luaL_pushresult(&b);
2562 ip = lua_tolstring(L, lua_gettop(L), NULL);
2563 }
2564 }
2565
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002566 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002567 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002568 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002569 lua_pushnil(L);
2570 return 1;
2571 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002572
2573 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002574 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, NULL, NULL, PA_O_PORT_OK | PA_O_STREAM);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002575 if (!addr) {
2576 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002577 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002578 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002579
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002580 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002581 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002582 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002583 if (port == -1) {
2584 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002585 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002586 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002587 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2588 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002589 if (port == -1) {
2590 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002591 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002592 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002593 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002594 }
2595 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002596
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002597 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2598 si = appctx->owner;
2599 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002600
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002601 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002602 xref_unlock(&socket->xref, peer);
2603 WILL_LJMP(luaL_error(L, "connect: internal error"));
2604 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002605 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002607 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002608 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002609 if (!hlua)
2610 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002611
2612 /* inform the stream that we want to be notified whenever the
2613 * connection completes.
2614 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002615 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002616 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002617 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002618
Willy Tarreauf31af932020-01-14 09:59:38 +01002619 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002620
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002621 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2622 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002623 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002624 }
2625 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002626
2627 task_wakeup(s->task, TASK_WOKEN_INIT);
2628 /* Return yield waiting for connection. */
2629
Willy Tarreau9635e032018-10-16 17:52:55 +02002630 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002631
2632 return 0;
2633}
2634
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002635#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002636__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2637{
2638 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002639 struct xref *peer;
2640 struct appctx *appctx;
2641 struct stream_interface *si;
2642 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002643
2644 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2645 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002646
2647 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002648 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002649 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002650 lua_pushnil(L);
2651 return 1;
2652 }
2653 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2654 si = appctx->owner;
2655 s = si_strm(si);
2656
2657 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002658 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002659 return MAY_LJMP(hlua_socket_connect(L));
2660}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002661#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002662
2663__LJMP static int hlua_socket_setoption(struct lua_State *L)
2664{
2665 return 0;
2666}
2667
2668__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2669{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002670 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002671 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002672 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002673 struct xref *peer;
2674 struct appctx *appctx;
2675 struct stream_interface *si;
2676 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002677
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002678 MAY_LJMP(check_args(L, 2, "settimeout"));
2679
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002680 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002681
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002682 /* convert the timeout to millis */
2683 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002684
Thierry Fournier17a921b2018-03-08 09:59:02 +01002685 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002686 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002687 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2688
Mark Lakes56cc1252018-03-27 09:48:06 +02002689 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002690 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002691
2692 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002693 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002694 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002695
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002696 /* Check if we run on the same thread than the xreator thread.
2697 * We cannot access to the socket if the thread is different.
2698 */
2699 if (socket->tid != tid)
2700 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2701
Mark Lakes56cc1252018-03-27 09:48:06 +02002702 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002703 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002704 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002705 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2706 WILL_LJMP(lua_error(L));
2707 return 0;
2708 }
2709 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2710 si = appctx->owner;
2711 s = si_strm(si);
2712
Cyril Bonté7bb63452018-08-17 23:51:02 +02002713 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002714 s->req.rto = tmout;
2715 s->req.wto = tmout;
2716 s->res.rto = tmout;
2717 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002718 s->req.rex = tick_add_ifset(now_ms, tmout);
2719 s->req.wex = tick_add_ifset(now_ms, tmout);
2720 s->res.rex = tick_add_ifset(now_ms, tmout);
2721 s->res.wex = tick_add_ifset(now_ms, tmout);
2722
2723 s->task->expire = tick_add_ifset(now_ms, tmout);
2724 task_queue(s->task);
2725
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002726 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002727
Thierry Fourniere9636f12018-03-08 09:54:32 +01002728 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002729 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002730}
2731
2732__LJMP static int hlua_socket_new(lua_State *L)
2733{
2734 struct hlua_socket *socket;
2735 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002736 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002737 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002738
2739 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002740 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002741 hlua_pusherror(L, "socket: full stack");
2742 goto out_fail_conf;
2743 }
2744
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002745 /* Create the object: obj[0] = userdata. */
2746 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002747 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002748 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002749 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002750 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002751
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002752 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002753 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002754 hlua_pusherror(L, "socket: uninitialized pools.");
2755 goto out_fail_conf;
2756 }
2757
Willy Tarreau87b09662015-04-03 00:22:06 +02002758 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002759 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2760 lua_setmetatable(L, -2);
2761
Willy Tarreaud420a972015-04-06 00:39:18 +02002762 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002763 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002764 if (!appctx) {
2765 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002766 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002767 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002768
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002769 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002770 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002771 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2772 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002773
Willy Tarreaud420a972015-04-06 00:39:18 +02002774 /* Now create a session, task and stream for this applet */
2775 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2776 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002777 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002778 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002779 }
2780
Willy Tarreau87787ac2017-08-28 16:22:54 +02002781 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002782 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002783 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002784 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002785 }
2786
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002787 /* Initialise cross reference between stream and Lua socket object. */
2788 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002789
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002790 /* Configure "right" stream interface. this "si" is used to connect
2791 * and retrieve data from the server. The connection is initialized
2792 * with the "struct server".
2793 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002794 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002795
2796 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002797 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002798 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002799
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002800 return 1;
2801
Willy Tarreaud420a972015-04-06 00:39:18 +02002802 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002803 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002804 out_fail_sess:
2805 appctx_free(appctx);
2806 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002807 WILL_LJMP(lua_error(L));
2808 return 0;
2809}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002810
2811/*
2812 *
2813 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002814 * Class Channel
2815 *
2816 *
2817 */
2818
2819/* Returns the struct hlua_channel join to the class channel in the
2820 * stack entry "ud" or throws an argument error.
2821 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002822__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002823{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002824 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002825}
2826
Willy Tarreau47860ed2015-03-10 14:07:50 +01002827/* Pushes the channel onto the top of the stack. If the stask does not have a
2828 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002829 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002830static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002831{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002832 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002833 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002834 return 0;
2835
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002836 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002837 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002838 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002839
2840 /* Pop a class sesison metatable and affect it to the userdata. */
2841 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2842 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002843 return 1;
2844}
2845
2846/* Duplicate all the data present in the input channel and put it
2847 * in a string LUA variables. Returns -1 and push a nil value in
2848 * the stack if the channel is closed and all the data are consumed,
2849 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002850 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002851 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002852static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002853{
2854 char *blk1;
2855 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002856 size_t len1;
2857 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002858 int ret;
2859 luaL_Buffer b;
2860
Willy Tarreau06d80a92017-10-19 14:32:15 +02002861 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002862 if (unlikely(ret == 0))
2863 return 0;
2864
2865 if (unlikely(ret < 0)) {
2866 lua_pushnil(L);
2867 return -1;
2868 }
2869
2870 luaL_buffinit(L, &b);
2871 luaL_addlstring(&b, blk1, len1);
2872 if (unlikely(ret == 2))
2873 luaL_addlstring(&b, blk2, len2);
2874 luaL_pushresult(&b);
2875
2876 if (unlikely(ret == 2))
2877 return len1 + len2;
2878 return len1;
2879}
2880
2881/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2882 * a yield. This function keep the data in the buffer.
2883 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002884__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002885{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002886 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002887
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002888 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2889
Thierry Fournier77016da2020-08-15 14:35:51 +02002890 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2891 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002892 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002893 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002894
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002895 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002896 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897 return 1;
2898}
2899
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002900/* Check arguments for the function "hlua_channel_dup_yield". */
2901__LJMP static int hlua_channel_dup(lua_State *L)
2902{
2903 MAY_LJMP(check_args(L, 1, "dup"));
2904 MAY_LJMP(hlua_checkchannel(L, 1));
2905 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2906}
2907
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002908/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2909 * a yield. This function consumes the data in the buffer. It returns
2910 * a string containing the data or a nil pointer if no data are available
2911 * and the channel is closed.
2912 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002913__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002914{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002915 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002916 int ret;
2917
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002918 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002919
Thierry Fournier77016da2020-08-15 14:35:51 +02002920 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2921 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002922 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002923 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002924
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002925 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002926 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002927 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002928
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002929 if (unlikely(ret == -1))
2930 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002931
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002932 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002933 return 1;
2934}
2935
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002936/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002937__LJMP static int hlua_channel_get(lua_State *L)
2938{
2939 MAY_LJMP(check_args(L, 1, "get"));
2940 MAY_LJMP(hlua_checkchannel(L, 1));
2941 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2942}
2943
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002944/* This functions consumes and returns one line. If the channel is closed,
2945 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002946 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002947 * value.
2948 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002949__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002950{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002951 char *blk1;
2952 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002953 size_t len1;
2954 size_t len2;
2955 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002956 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002957 int ret;
2958 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002959
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002960 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2961
Thierry Fournier77016da2020-08-15 14:35:51 +02002962 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2963 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002964 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002965 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002966
Willy Tarreau06d80a92017-10-19 14:32:15 +02002967 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002968 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002969 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002970
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971 if (ret == -1) {
2972 lua_pushnil(L);
2973 return 1;
2974 }
2975
2976 luaL_buffinit(L, &b);
2977 luaL_addlstring(&b, blk1, len1);
2978 len = len1;
2979 if (unlikely(ret == 2)) {
2980 luaL_addlstring(&b, blk2, len2);
2981 len += len2;
2982 }
2983 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002984 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002985 return 1;
2986}
2987
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002988/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002989__LJMP static int hlua_channel_getline(lua_State *L)
2990{
2991 MAY_LJMP(check_args(L, 1, "getline"));
2992 MAY_LJMP(hlua_checkchannel(L, 1));
2993 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2994}
2995
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002996/* This function takes a string as input, and append it at the
2997 * input side of channel. If the data is too big, but a space
2998 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002999 * yields. If the data is bigger than the buffer, or if the
3000 * channel is closed, it returns -1. Otherwise, it returns the
3001 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003002 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003003__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003004{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003005 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003006 size_t len;
3007 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3008 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3009 int ret;
3010 int max;
3011
Thierry Fournier77016da2020-08-15 14:35:51 +02003012 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3013 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003014 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003015 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003016
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003017 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003018 * the request buffer if its not required.
3019 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003020 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003021 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003022 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003023 }
3024
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003025 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003026 if (max > len - l)
3027 max = len - l;
3028
Willy Tarreau06d80a92017-10-19 14:32:15 +02003029 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003030 if (ret == -2 || ret == -3) {
3031 lua_pushinteger(L, -1);
3032 return 1;
3033 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01003034 if (ret == -1) {
3035 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02003036 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01003037 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003038 l += ret;
3039 lua_pop(L, 1);
3040 lua_pushinteger(L, l);
3041
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003042 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003043 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003044 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003045 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003046 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003047 */
3048 return 1;
3049 }
3050 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003051 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003052 return 1;
3053}
3054
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003055/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3056 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003057 * buffer size is too little for the data.
3058 */
3059__LJMP static int hlua_channel_append(lua_State *L)
3060{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003061 size_t len;
3062
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003063 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003064 MAY_LJMP(hlua_checkchannel(L, 1));
3065 MAY_LJMP(luaL_checklstring(L, 2, &len));
3066 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003067 lua_pushinteger(L, 0);
3068
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003069 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003070}
3071
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003072/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003073 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003074 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003075 * or -1 if the channel is closed or if the buffer size is too
3076 * little for the data.
3077 */
3078__LJMP static int hlua_channel_set(lua_State *L)
3079{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003080 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003081
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003082 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003083 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003084 lua_pushinteger(L, 0);
3085
Thierry Fournier77016da2020-08-15 14:35:51 +02003086 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3087 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003088 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003089 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003090
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003091 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003092
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003093 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003094}
3095
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003096/* Append data in the output side of the buffer. This data is immediately
3097 * sent. The function returns the amount of data written. If the buffer
3098 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003099 * if the channel is closed.
3100 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003101__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003102{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003103 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003104 size_t len;
3105 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3106 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3107 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003108 struct hlua *hlua;
3109
3110 /* Get hlua struct, or NULL if we execute from main lua state */
3111 hlua = hlua_gethlua(L);
3112 if (!hlua) {
3113 lua_pushnil(L);
3114 return 1;
3115 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003116
Thierry Fournier77016da2020-08-15 14:35:51 +02003117 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3118 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003119 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003120 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003121
Willy Tarreau47860ed2015-03-10 14:07:50 +01003122 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003123 lua_pushinteger(L, -1);
3124 return 1;
3125 }
3126
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003127 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003128 * the request buffer if its not required.
3129 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003130 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003131 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003132 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003133 }
3134
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003135 /* The written data will be immediately sent, so we can check
3136 * the available space without taking in account the reserve.
3137 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003138 * data, because the buffer will be flushed.
3139 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003140 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003141
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003142 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003143 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003144 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003145 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003146 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003147 return 1;
3148
3149 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003150 if (max > len - l)
3151 max = len - l;
3152
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003153 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003154 * detects a non contiguous buffer and realign it.
3155 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003156 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003157 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003158
3159 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003160 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003161
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003162 /* buffer replace considers that the input part is filled.
3163 * so, I must forward these new data in the output part.
3164 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003165 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003166
3167 l += max;
3168 lua_pop(L, 1);
3169 lua_pushinteger(L, l);
3170
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003171 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003172 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003173 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003174 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003175 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003176 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003177 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003178
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003179 if (l < len) {
3180 /* If we are waiting for space in the response buffer, we
3181 * must set the flag WAKERESWR. This flag required the task
3182 * wake up if any activity is detected on the response buffer.
3183 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003184 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003185 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003186 else
3187 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003188 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003189 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003190
3191 return 1;
3192}
3193
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003194/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003195 * yield the LUA process, and resume it without checking the
3196 * input arguments.
3197 */
3198__LJMP static int hlua_channel_send(lua_State *L)
3199{
3200 MAY_LJMP(check_args(L, 2, "send"));
3201 lua_pushinteger(L, 0);
3202
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003203 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003204}
3205
3206/* This function forward and amount of butes. The data pass from
3207 * the input side of the buffer to the output side, and can be
3208 * forwarded. This function never fails.
3209 *
3210 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003211 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003212 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003213__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003214{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003215 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003216 int len;
3217 int l;
3218 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003219 struct hlua *hlua;
3220
3221 /* Get hlua struct, or NULL if we execute from main lua state */
3222 hlua = hlua_gethlua(L);
3223 if (!hlua)
3224 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003225
3226 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003227
Thierry Fournier77016da2020-08-15 14:35:51 +02003228 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3229 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003230 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003231 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003232
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003233 len = MAY_LJMP(luaL_checkinteger(L, 2));
3234 l = MAY_LJMP(luaL_checkinteger(L, -1));
3235
3236 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003237 if (max > ci_data(chn))
3238 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003239 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003240 l += max;
3241
3242 lua_pop(L, 1);
3243 lua_pushinteger(L, l);
3244
3245 /* Check if it miss bytes to forward. */
3246 if (l < len) {
3247 /* The the input channel or the output channel are closed, we
3248 * must return the amount of data forwarded.
3249 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003250 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003251 return 1;
3252
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003253 /* If we are waiting for space data in the response buffer, we
3254 * must set the flag WAKERESWR. This flag required the task
3255 * wake up if any activity is detected on the response buffer.
3256 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003257 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003258 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003259 else
3260 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003261
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003262 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003263 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003264 }
3265
3266 return 1;
3267}
3268
3269/* Just check the input and prepare the stack for the previous
3270 * function "hlua_channel_forward_yield"
3271 */
3272__LJMP static int hlua_channel_forward(lua_State *L)
3273{
3274 MAY_LJMP(check_args(L, 2, "forward"));
3275 MAY_LJMP(hlua_checkchannel(L, 1));
3276 MAY_LJMP(luaL_checkinteger(L, 2));
3277
3278 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003279 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003280}
3281
3282/* Just returns the number of bytes available in the input
3283 * side of the buffer. This function never fails.
3284 */
3285__LJMP static int hlua_channel_get_in_len(lua_State *L)
3286{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003287 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003288
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003289 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003290 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003291 if (IS_HTX_STRM(chn_strm(chn))) {
3292 struct htx *htx = htxbuf(&chn->buf);
3293 lua_pushinteger(L, htx->data - co_data(chn));
3294 }
3295 else
3296 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003297 return 1;
3298}
3299
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003300/* Returns true if the channel is full. */
3301__LJMP static int hlua_channel_is_full(lua_State *L)
3302{
3303 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003304
3305 MAY_LJMP(check_args(L, 1, "is_full"));
3306 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003307 /* ignore the reserve, we are not on a producer side (ie in an
3308 * applet).
3309 */
3310 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003311 return 1;
3312}
3313
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003314/* Returns true if the channel is the response channel. */
3315__LJMP static int hlua_channel_is_resp(lua_State *L)
3316{
3317 struct channel *chn;
3318
3319 MAY_LJMP(check_args(L, 1, "is_resp"));
3320 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3321
3322 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3323 return 1;
3324}
3325
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003326/* Just returns the number of bytes available in the output
3327 * side of the buffer. This function never fails.
3328 */
3329__LJMP static int hlua_channel_get_out_len(lua_State *L)
3330{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003331 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003332
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003333 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003334 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003335 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003336 return 1;
3337}
3338
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003339/*
3340 *
3341 *
3342 * Class Fetches
3343 *
3344 *
3345 */
3346
3347/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003348 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003349 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003350__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003351{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003352 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003353}
3354
3355/* This function creates and push in the stack a fetch object according
3356 * with a current TXN.
3357 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003358static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003359{
Willy Tarreau7073c472015-04-06 11:15:40 +02003360 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003361
3362 /* Check stack size. */
3363 if (!lua_checkstack(L, 3))
3364 return 0;
3365
3366 /* Create the object: obj[0] = userdata.
3367 * Note that the base of the Fetches object is the
3368 * transaction object.
3369 */
3370 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003371 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003372 lua_rawseti(L, -2, 0);
3373
Willy Tarreau7073c472015-04-06 11:15:40 +02003374 hsmp->s = txn->s;
3375 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003376 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003377 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003378
3379 /* Pop a class sesison metatable and affect it to the userdata. */
3380 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3381 lua_setmetatable(L, -2);
3382
3383 return 1;
3384}
3385
3386/* This function is an LUA binding. It is called with each sample-fetch.
3387 * It uses closure argument to store the associated sample-fetch. It
3388 * returns only one argument or throws an error. An error is thrown
3389 * only if an error is encountered during the argument parsing. If
3390 * the "sample-fetch" function fails, nil is returned.
3391 */
3392__LJMP static int hlua_run_sample_fetch(lua_State *L)
3393{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003394 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003395 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003396 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003397 int i;
3398 struct sample smp;
3399
3400 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003401 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003402
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003403 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003404 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003405
Thierry FOURNIERca988662015-12-20 18:43:03 +01003406 /* Check execution authorization. */
3407 if (f->use & SMP_USE_HTTP_ANY &&
3408 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3409 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3410 "is not available in Lua services", f->kw);
3411 WILL_LJMP(lua_error(L));
3412 }
3413
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003414 /* Get extra arguments. */
3415 for (i = 0; i < lua_gettop(L) - 1; i++) {
3416 if (i >= ARGM_NBARGS)
3417 break;
3418 hlua_lua2arg(L, i + 2, &args[i]);
3419 }
3420 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003421 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003422
3423 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003424 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003425
3426 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003427 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003428 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003429 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003430 }
3431
3432 /* Initialise the sample. */
3433 memset(&smp, 0, sizeof(smp));
3434
3435 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003436 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003437 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003438 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003439 lua_pushstring(L, "");
3440 else
3441 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003442 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003443 }
3444
3445 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003446 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003447 hlua_smp2lua_str(L, &smp);
3448 else
3449 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003450
3451 end:
3452 for (i = 0; args[i].type != ARGT_STOP; i++) {
3453 if (args[i].type == ARGT_STR)
3454 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003455 else if (args[i].type == ARGT_REG)
3456 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003457 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003458 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003459
3460 error:
3461 for (i = 0; args[i].type != ARGT_STOP; i++) {
3462 if (args[i].type == ARGT_STR)
3463 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003464 else if (args[i].type == ARGT_REG)
3465 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003466 }
3467 WILL_LJMP(lua_error(L));
3468 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003469}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003470
3471/*
3472 *
3473 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003474 * Class Converters
3475 *
3476 *
3477 */
3478
3479/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003480 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003481 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003482__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003483{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003484 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003485}
3486
3487/* This function creates and push in the stack a Converters object
3488 * according with a current TXN.
3489 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003490static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003491{
Willy Tarreau7073c472015-04-06 11:15:40 +02003492 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003493
3494 /* Check stack size. */
3495 if (!lua_checkstack(L, 3))
3496 return 0;
3497
3498 /* Create the object: obj[0] = userdata.
3499 * Note that the base of the Converters object is the
3500 * same than the TXN object.
3501 */
3502 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003503 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003504 lua_rawseti(L, -2, 0);
3505
Willy Tarreau7073c472015-04-06 11:15:40 +02003506 hsmp->s = txn->s;
3507 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003508 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003509 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003510
Willy Tarreau87b09662015-04-03 00:22:06 +02003511 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003512 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3513 lua_setmetatable(L, -2);
3514
3515 return 1;
3516}
3517
3518/* This function is an LUA binding. It is called with each converter.
3519 * It uses closure argument to store the associated converter. It
3520 * returns only one argument or throws an error. An error is thrown
3521 * only if an error is encountered during the argument parsing. If
3522 * the converter function function fails, nil is returned.
3523 */
3524__LJMP static int hlua_run_sample_conv(lua_State *L)
3525{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003526 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003527 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003528 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003529 int i;
3530 struct sample smp;
3531
3532 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003533 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003534
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003535 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003536 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003537
3538 /* Get extra arguments. */
3539 for (i = 0; i < lua_gettop(L) - 2; i++) {
3540 if (i >= ARGM_NBARGS)
3541 break;
3542 hlua_lua2arg(L, i + 3, &args[i]);
3543 }
3544 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003545 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003546
3547 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003548 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003549
3550 /* Run the special args checker. */
3551 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3552 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003553 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003554 }
3555
3556 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003557 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003558 if (!hlua_lua2smp(L, 2, &smp)) {
3559 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003560 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003561 }
3562
Willy Tarreau1777ea62016-03-10 16:15:46 +01003563 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3564
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003565 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003566 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003567 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003568 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003569 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003570 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003571 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3572 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003573 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003574 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003575 }
3576
3577 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003578 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003579 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003580 lua_pushstring(L, "");
3581 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003582 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003583 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003584 }
3585
3586 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003587 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003588 hlua_smp2lua_str(L, &smp);
3589 else
3590 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003591 end:
3592 for (i = 0; args[i].type != ARGT_STOP; i++) {
3593 if (args[i].type == ARGT_STR)
3594 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003595 else if (args[i].type == ARGT_REG)
3596 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003597 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003598 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003599
3600 error:
3601 for (i = 0; args[i].type != ARGT_STOP; i++) {
3602 if (args[i].type == ARGT_STR)
3603 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003604 else if (args[i].type == ARGT_REG)
3605 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003606 }
3607 WILL_LJMP(lua_error(L));
3608 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003609}
3610
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003611/*
3612 *
3613 *
3614 * Class AppletTCP
3615 *
3616 *
3617 */
3618
3619/* Returns a struct hlua_txn if the stack entry "ud" is
3620 * a class stream, otherwise it throws an error.
3621 */
3622__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3623{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003624 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003625}
3626
3627/* This function creates and push in the stack an Applet object
3628 * according with a current TXN.
3629 */
3630static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3631{
3632 struct hlua_appctx *appctx;
3633 struct stream_interface *si = ctx->owner;
3634 struct stream *s = si_strm(si);
3635 struct proxy *p = s->be;
3636
3637 /* Check stack size. */
3638 if (!lua_checkstack(L, 3))
3639 return 0;
3640
3641 /* Create the object: obj[0] = userdata.
3642 * Note that the base of the Converters object is the
3643 * same than the TXN object.
3644 */
3645 lua_newtable(L);
3646 appctx = lua_newuserdata(L, sizeof(*appctx));
3647 lua_rawseti(L, -2, 0);
3648 appctx->appctx = ctx;
3649 appctx->htxn.s = s;
3650 appctx->htxn.p = p;
3651
3652 /* Create the "f" field that contains a list of fetches. */
3653 lua_pushstring(L, "f");
3654 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3655 return 0;
3656 lua_settable(L, -3);
3657
3658 /* Create the "sf" field that contains a list of stringsafe fetches. */
3659 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003660 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003661 return 0;
3662 lua_settable(L, -3);
3663
3664 /* Create the "c" field that contains a list of converters. */
3665 lua_pushstring(L, "c");
3666 if (!hlua_converters_new(L, &appctx->htxn, 0))
3667 return 0;
3668 lua_settable(L, -3);
3669
3670 /* Create the "sc" field that contains a list of stringsafe converters. */
3671 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003672 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003673 return 0;
3674 lua_settable(L, -3);
3675
3676 /* Pop a class stream metatable and affect it to the table. */
3677 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3678 lua_setmetatable(L, -2);
3679
3680 return 1;
3681}
3682
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003683__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3684{
3685 struct hlua_appctx *appctx;
3686 struct stream *s;
3687 const char *name;
3688 size_t len;
3689 struct sample smp;
3690
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003691 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3692 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003693
3694 /* It is useles to retrieve the stream, but this function
3695 * runs only in a stream context.
3696 */
3697 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3698 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3699 s = appctx->htxn.s;
3700
3701 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003702 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003703 hlua_lua2smp(L, 3, &smp);
3704
3705 /* Store the sample in a variable. */
3706 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003707
3708 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3709 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3710 else
3711 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3712
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003713 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003714}
3715
3716__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3717{
3718 struct hlua_appctx *appctx;
3719 struct stream *s;
3720 const char *name;
3721 size_t len;
3722 struct sample smp;
3723
3724 MAY_LJMP(check_args(L, 2, "unset_var"));
3725
3726 /* It is useles to retrieve the stream, but this function
3727 * runs only in a stream context.
3728 */
3729 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3730 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3731 s = appctx->htxn.s;
3732
3733 /* Unset the variable. */
3734 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003735 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3736 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003737}
3738
3739__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3740{
3741 struct hlua_appctx *appctx;
3742 struct stream *s;
3743 const char *name;
3744 size_t len;
3745 struct sample smp;
3746
3747 MAY_LJMP(check_args(L, 2, "get_var"));
3748
3749 /* It is useles to retrieve the stream, but this function
3750 * runs only in a stream context.
3751 */
3752 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3753 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3754 s = appctx->htxn.s;
3755
3756 smp_set_owner(&smp, s->be, s->sess, s, 0);
3757 if (!vars_get_by_name(name, len, &smp)) {
3758 lua_pushnil(L);
3759 return 1;
3760 }
3761
3762 return hlua_smp2lua(L, &smp);
3763}
3764
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003765__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3766{
3767 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3768 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003769 struct hlua *hlua;
3770
3771 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003772 if (!s->hlua)
3773 return 0;
3774 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003775
3776 MAY_LJMP(check_args(L, 2, "set_priv"));
3777
3778 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003779 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003780
3781 /* Get and store new value. */
3782 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3783 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3784
3785 return 0;
3786}
3787
3788__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3789{
3790 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3791 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003792 struct hlua *hlua;
3793
3794 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003795 if (!s->hlua) {
3796 lua_pushnil(L);
3797 return 1;
3798 }
3799 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003800
3801 /* Push configuration index in the stack. */
3802 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3803
3804 return 1;
3805}
3806
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003807/* If expected data not yet available, it returns a yield. This function
3808 * consumes the data in the buffer. It returns a string containing the
3809 * data. This string can be empty.
3810 */
3811__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3812{
3813 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3814 struct stream_interface *si = appctx->appctx->owner;
3815 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003816 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003817 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003818 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003819 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003820
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003821 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003822 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003823
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003824 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003825 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003826 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003827 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003828 }
3829
3830 /* End of data: commit the total strings and return. */
3831 if (ret < 0) {
3832 luaL_pushresult(&appctx->b);
3833 return 1;
3834 }
3835
3836 /* Ensure that the block 2 length is usable. */
3837 if (ret == 1)
3838 len2 = 0;
3839
3840 /* dont check the max length read and dont check. */
3841 luaL_addlstring(&appctx->b, blk1, len1);
3842 luaL_addlstring(&appctx->b, blk2, len2);
3843
3844 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003845 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003846 luaL_pushresult(&appctx->b);
3847 return 1;
3848}
3849
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003850/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003851__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3852{
3853 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3854
3855 /* Initialise the string catenation. */
3856 luaL_buffinit(L, &appctx->b);
3857
3858 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3859}
3860
3861/* If expected data not yet available, it returns a yield. This function
3862 * consumes the data in the buffer. It returns a string containing the
3863 * data. This string can be empty.
3864 */
3865__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3866{
3867 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3868 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003869 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003870 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003871 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003872 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003873 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003874 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003875
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003876 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003877 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003878
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003879 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003880 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003881 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003882 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003883 }
3884
3885 /* End of data: commit the total strings and return. */
3886 if (ret < 0) {
3887 luaL_pushresult(&appctx->b);
3888 return 1;
3889 }
3890
3891 /* Ensure that the block 2 length is usable. */
3892 if (ret == 1)
3893 len2 = 0;
3894
3895 if (len == -1) {
3896
3897 /* If len == -1, catenate all the data avalaile and
3898 * yield because we want to get all the data until
3899 * the end of data stream.
3900 */
3901 luaL_addlstring(&appctx->b, blk1, len1);
3902 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003903 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003904 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003905 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003906
3907 } else {
3908
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003909 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003910 if (len1 > len)
3911 len1 = len;
3912 luaL_addlstring(&appctx->b, blk1, len1);
3913 len -= len1;
3914
3915 /* Copy the second block. */
3916 if (len2 > len)
3917 len2 = len;
3918 luaL_addlstring(&appctx->b, blk2, len2);
3919 len -= len2;
3920
3921 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003922 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003923
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003924 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003925 if (len > 0) {
3926 lua_pushinteger(L, len);
3927 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003928 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003929 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003930 }
3931
3932 /* return the result. */
3933 luaL_pushresult(&appctx->b);
3934 return 1;
3935 }
3936
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003937 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003938 hlua_pusherror(L, "Lua: internal error");
3939 WILL_LJMP(lua_error(L));
3940 return 0;
3941}
3942
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003943/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003944__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3945{
3946 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3947 int len = -1;
3948
3949 if (lua_gettop(L) > 2)
3950 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3951 if (lua_gettop(L) >= 2) {
3952 len = MAY_LJMP(luaL_checkinteger(L, 2));
3953 lua_pop(L, 1);
3954 }
3955
3956 /* Confirm or set the required length */
3957 lua_pushinteger(L, len);
3958
3959 /* Initialise the string catenation. */
3960 luaL_buffinit(L, &appctx->b);
3961
3962 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3963}
3964
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003965/* Append data in the output side of the buffer. This data is immediately
3966 * sent. The function returns the amount of data written. If the buffer
3967 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003968 * if the channel is closed.
3969 */
3970__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3971{
3972 size_t len;
3973 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3974 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3975 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3976 struct stream_interface *si = appctx->appctx->owner;
3977 struct channel *chn = si_ic(si);
3978 int max;
3979
3980 /* Get the max amount of data which can write as input in the channel. */
3981 max = channel_recv_max(chn);
3982 if (max > (len - l))
3983 max = len - l;
3984
3985 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003986 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003987
3988 /* update counters. */
3989 l += max;
3990 lua_pop(L, 1);
3991 lua_pushinteger(L, l);
3992
3993 /* If some data is not send, declares the situation to the
3994 * applet, and returns a yield.
3995 */
3996 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003997 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003998 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003999 }
4000
4001 return 1;
4002}
4003
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004004/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004005 * yield the LUA process, and resume it without checking the
4006 * input arguments.
4007 */
4008__LJMP static int hlua_applet_tcp_send(lua_State *L)
4009{
4010 MAY_LJMP(check_args(L, 2, "send"));
4011 lua_pushinteger(L, 0);
4012
4013 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4014}
4015
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004016/*
4017 *
4018 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004019 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004020 *
4021 *
4022 */
4023
4024/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004025 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004026 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004027__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004028{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004029 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004030}
4031
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004032/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004033 * according with a current TXN.
4034 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004035static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004036{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004037 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004038 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004039 struct stream_interface *si = ctx->owner;
4040 struct stream *s = si_strm(si);
4041 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004042 struct htx *htx;
4043 struct htx_blk *blk;
4044 struct htx_sl *sl;
4045 struct ist path;
4046 unsigned long long len = 0;
4047 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004048
4049 /* Check stack size. */
4050 if (!lua_checkstack(L, 3))
4051 return 0;
4052
4053 /* Create the object: obj[0] = userdata.
4054 * Note that the base of the Converters object is the
4055 * same than the TXN object.
4056 */
4057 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004058 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004059 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004060 appctx->appctx = ctx;
4061 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004062 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004063 appctx->htxn.s = s;
4064 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004065
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004066 /* Create the "f" field that contains a list of fetches. */
4067 lua_pushstring(L, "f");
4068 if (!hlua_fetches_new(L, &appctx->htxn, 0))
4069 return 0;
4070 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004071
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004072 /* Create the "sf" field that contains a list of stringsafe fetches. */
4073 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004074 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004075 return 0;
4076 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004077
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004078 /* Create the "c" field that contains a list of converters. */
4079 lua_pushstring(L, "c");
4080 if (!hlua_converters_new(L, &appctx->htxn, 0))
4081 return 0;
4082 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004083
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004084 /* Create the "sc" field that contains a list of stringsafe converters. */
4085 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004086 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004087 return 0;
4088 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004089
Christopher Fauleta2097962019-07-15 16:25:33 +02004090 htx = htxbuf(&s->req.buf);
4091 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004092 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004093 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004094
Christopher Fauleta2097962019-07-15 16:25:33 +02004095 /* Stores the request method. */
4096 lua_pushstring(L, "method");
4097 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4098 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004099
Christopher Fauleta2097962019-07-15 16:25:33 +02004100 /* Stores the http version. */
4101 lua_pushstring(L, "version");
4102 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4103 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004104
Christopher Fauleta2097962019-07-15 16:25:33 +02004105 /* creates an array of headers. hlua_http_get_headers() crates and push
4106 * the array on the top of the stack.
4107 */
4108 lua_pushstring(L, "headers");
4109 htxn.s = s;
4110 htxn.p = px;
4111 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004112 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004113 return 0;
4114 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004115
Christopher Fauleta2097962019-07-15 16:25:33 +02004116 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004117 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004118 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004119
Christopher Fauleta2097962019-07-15 16:25:33 +02004120 p = path.ptr;
4121 end = path.ptr + path.len;
4122 q = p;
4123 while (q < end && *q != '?')
4124 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004125
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004126 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004127 lua_pushstring(L, "path");
4128 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004129 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004130
Christopher Fauleta2097962019-07-15 16:25:33 +02004131 /* Stores the query string. */
4132 lua_pushstring(L, "qs");
4133 if (*q == '?')
4134 q++;
4135 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004136 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004137 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004138
Christopher Fauleta2097962019-07-15 16:25:33 +02004139 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4140 struct htx_blk *blk = htx_get_blk(htx, pos);
4141 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004142
Christopher Fauleta2097962019-07-15 16:25:33 +02004143 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
4144 break;
4145 if (type == HTX_BLK_DATA)
4146 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004147 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004148 if (htx->extra != ULLONG_MAX)
4149 len += htx->extra;
4150
4151 /* Stores the request path. */
4152 lua_pushstring(L, "length");
4153 lua_pushinteger(L, len);
4154 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004155
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004156 /* Create an empty array of HTTP request headers. */
4157 lua_pushstring(L, "response");
4158 lua_newtable(L);
4159 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004160
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004161 /* Pop a class stream metatable and affect it to the table. */
4162 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4163 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004164
4165 return 1;
4166}
4167
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004168__LJMP static int hlua_applet_http_set_var(lua_State *L)
4169{
4170 struct hlua_appctx *appctx;
4171 struct stream *s;
4172 const char *name;
4173 size_t len;
4174 struct sample smp;
4175
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004176 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4177 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004178
4179 /* It is useles to retrieve the stream, but this function
4180 * runs only in a stream context.
4181 */
4182 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4183 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4184 s = appctx->htxn.s;
4185
4186 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004187 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004188 hlua_lua2smp(L, 3, &smp);
4189
4190 /* Store the sample in a variable. */
4191 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004192
4193 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4194 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4195 else
4196 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4197
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004198 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004199}
4200
4201__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4202{
4203 struct hlua_appctx *appctx;
4204 struct stream *s;
4205 const char *name;
4206 size_t len;
4207 struct sample smp;
4208
4209 MAY_LJMP(check_args(L, 2, "unset_var"));
4210
4211 /* It is useles to retrieve the stream, but this function
4212 * runs only in a stream context.
4213 */
4214 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4215 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4216 s = appctx->htxn.s;
4217
4218 /* Unset the variable. */
4219 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004220 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4221 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004222}
4223
4224__LJMP static int hlua_applet_http_get_var(lua_State *L)
4225{
4226 struct hlua_appctx *appctx;
4227 struct stream *s;
4228 const char *name;
4229 size_t len;
4230 struct sample smp;
4231
4232 MAY_LJMP(check_args(L, 2, "get_var"));
4233
4234 /* It is useles to retrieve the stream, but this function
4235 * runs only in a stream context.
4236 */
4237 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4238 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4239 s = appctx->htxn.s;
4240
4241 smp_set_owner(&smp, s->be, s->sess, s, 0);
4242 if (!vars_get_by_name(name, len, &smp)) {
4243 lua_pushnil(L);
4244 return 1;
4245 }
4246
4247 return hlua_smp2lua(L, &smp);
4248}
4249
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004250__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4251{
4252 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4253 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004254 struct hlua *hlua;
4255
4256 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004257 if (!s->hlua)
4258 return 0;
4259 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004260
4261 MAY_LJMP(check_args(L, 2, "set_priv"));
4262
4263 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004264 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004265
4266 /* Get and store new value. */
4267 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4268 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4269
4270 return 0;
4271}
4272
4273__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4274{
4275 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4276 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004277 struct hlua *hlua;
4278
4279 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004280 if (!s->hlua) {
4281 lua_pushnil(L);
4282 return 1;
4283 }
4284 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004285
4286 /* Push configuration index in the stack. */
4287 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4288
4289 return 1;
4290}
4291
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004292/* If expected data not yet available, it returns a yield. This function
4293 * consumes the data in the buffer. It returns a string containing the
4294 * data. This string can be empty.
4295 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004296__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004297{
4298 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4299 struct stream_interface *si = appctx->appctx->owner;
4300 struct channel *req = si_oc(si);
4301 struct htx *htx;
4302 struct htx_blk *blk;
4303 size_t count;
4304 int stop = 0;
4305
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004306 htx = htx_from_buf(&req->buf);
4307 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004308 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004309
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004310 while (count && !stop && blk) {
4311 enum htx_blk_type type = htx_get_blk_type(blk);
4312 uint32_t sz = htx_get_blksz(blk);
4313 struct ist v;
4314 uint32_t vlen;
4315 char *nl;
4316
Christopher Faulete461e342018-12-18 16:43:35 +01004317 if (type == HTX_BLK_EOM) {
4318 stop = 1;
4319 break;
4320 }
4321
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004322 vlen = sz;
4323 if (vlen > count) {
4324 if (type != HTX_BLK_DATA)
4325 break;
4326 vlen = count;
4327 }
4328
4329 switch (type) {
4330 case HTX_BLK_UNUSED:
4331 break;
4332
4333 case HTX_BLK_DATA:
4334 v = htx_get_blk_value(htx, blk);
4335 v.len = vlen;
4336 nl = istchr(v, '\n');
4337 if (nl != NULL) {
4338 stop = 1;
4339 vlen = nl - v.ptr + 1;
4340 }
4341 luaL_addlstring(&appctx->b, v.ptr, vlen);
4342 break;
4343
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004344 case HTX_BLK_TLR:
4345 case HTX_BLK_EOM:
4346 stop = 1;
4347 break;
4348
4349 default:
4350 break;
4351 }
4352
4353 co_set_data(req, co_data(req) - vlen);
4354 count -= vlen;
4355 if (sz == vlen)
4356 blk = htx_remove_blk(htx, blk);
4357 else {
4358 htx_cut_data_blk(htx, blk, vlen);
4359 break;
4360 }
4361 }
4362
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004363 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004364 if (!stop) {
4365 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004366 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004367 }
4368
4369 /* return the result. */
4370 luaL_pushresult(&appctx->b);
4371 return 1;
4372}
4373
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004374
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004375/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004376__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004377{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004378 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004379
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004380 /* Initialise the string catenation. */
4381 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004382
Christopher Fauleta2097962019-07-15 16:25:33 +02004383 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004384}
4385
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004386/* If expected data not yet available, it returns a yield. This function
4387 * consumes the data in the buffer. It returns a string containing the
4388 * data. This string can be empty.
4389 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004390__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004391{
4392 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4393 struct stream_interface *si = appctx->appctx->owner;
4394 struct channel *req = si_oc(si);
4395 struct htx *htx;
4396 struct htx_blk *blk;
4397 size_t count;
4398 int len;
4399
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004400 htx = htx_from_buf(&req->buf);
4401 len = MAY_LJMP(luaL_checkinteger(L, 2));
4402 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004403 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004404 while (count && len && blk) {
4405 enum htx_blk_type type = htx_get_blk_type(blk);
4406 uint32_t sz = htx_get_blksz(blk);
4407 struct ist v;
4408 uint32_t vlen;
4409
Christopher Faulete461e342018-12-18 16:43:35 +01004410 if (type == HTX_BLK_EOM) {
4411 len = 0;
4412 break;
4413 }
4414
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004415 vlen = sz;
4416 if (len > 0 && vlen > len)
4417 vlen = len;
4418 if (vlen > count) {
4419 if (type != HTX_BLK_DATA)
4420 break;
4421 vlen = count;
4422 }
4423
4424 switch (type) {
4425 case HTX_BLK_UNUSED:
4426 break;
4427
4428 case HTX_BLK_DATA:
4429 v = htx_get_blk_value(htx, blk);
4430 luaL_addlstring(&appctx->b, v.ptr, vlen);
4431 break;
4432
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004433 case HTX_BLK_TLR:
4434 case HTX_BLK_EOM:
4435 len = 0;
4436 break;
4437
4438 default:
4439 break;
4440 }
4441
4442 co_set_data(req, co_data(req) - vlen);
4443 count -= vlen;
4444 if (len > 0)
4445 len -= vlen;
4446 if (sz == vlen)
4447 blk = htx_remove_blk(htx, blk);
4448 else {
4449 htx_cut_data_blk(htx, blk, vlen);
4450 break;
4451 }
4452 }
4453
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004454 htx_to_buf(htx, &req->buf);
4455
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004456 /* If we are no other data available, yield waiting for new data. */
4457 if (len) {
4458 if (len > 0) {
4459 lua_pushinteger(L, len);
4460 lua_replace(L, 2);
4461 }
4462 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004463 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004464 }
4465
4466 /* return the result. */
4467 luaL_pushresult(&appctx->b);
4468 return 1;
4469}
4470
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004471/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004472__LJMP static int hlua_applet_http_recv(lua_State *L)
4473{
4474 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4475 int len = -1;
4476
4477 /* Check arguments. */
4478 if (lua_gettop(L) > 2)
4479 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4480 if (lua_gettop(L) >= 2) {
4481 len = MAY_LJMP(luaL_checkinteger(L, 2));
4482 lua_pop(L, 1);
4483 }
4484
Christopher Fauleta2097962019-07-15 16:25:33 +02004485 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004486
Christopher Fauleta2097962019-07-15 16:25:33 +02004487 /* Initialise the string catenation. */
4488 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004489
Christopher Fauleta2097962019-07-15 16:25:33 +02004490 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004491}
4492
4493/* Append data in the output side of the buffer. This data is immediately
4494 * sent. The function returns the amount of data written. If the buffer
4495 * cannot contain the data, the function yields. The function returns -1
4496 * if the channel is closed.
4497 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004498__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004499{
4500 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4501 struct stream_interface *si = appctx->appctx->owner;
4502 struct channel *res = si_ic(si);
4503 struct htx *htx = htx_from_buf(&res->buf);
4504 const char *data;
4505 size_t len;
4506 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4507 int max;
4508
Christopher Faulet9060fc02019-07-03 11:39:30 +02004509 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004510 if (!max)
4511 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004512
4513 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4514
4515 /* Get the max amount of data which can write as input in the channel. */
4516 if (max > (len - l))
4517 max = len - l;
4518
4519 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004520 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004521 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004522
4523 /* update counters. */
4524 l += max;
4525 lua_pop(L, 1);
4526 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004527
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004528 /* If some data is not send, declares the situation to the
4529 * applet, and returns a yield.
4530 */
4531 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004532 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004533 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004534 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004535 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004536 }
4537
Christopher Fauleta2097962019-07-15 16:25:33 +02004538 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004539 return 1;
4540}
4541
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004542/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004543 * yield the LUA process, and resume it without checking the
4544 * input arguments.
4545 */
4546__LJMP static int hlua_applet_http_send(lua_State *L)
4547{
4548 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004549
4550 /* We want to send some data. Headers must be sent. */
4551 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4552 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4553 WILL_LJMP(lua_error(L));
4554 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004555
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004556 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004557 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004558
Christopher Fauleta2097962019-07-15 16:25:33 +02004559 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004560}
4561
4562__LJMP static int hlua_applet_http_addheader(lua_State *L)
4563{
4564 const char *name;
4565 int ret;
4566
4567 MAY_LJMP(hlua_checkapplet_http(L, 1));
4568 name = MAY_LJMP(luaL_checkstring(L, 2));
4569 MAY_LJMP(luaL_checkstring(L, 3));
4570
4571 /* Push in the stack the "response" entry. */
4572 ret = lua_getfield(L, 1, "response");
4573 if (ret != LUA_TTABLE) {
4574 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4575 "is expected as an array. %s found", lua_typename(L, ret));
4576 WILL_LJMP(lua_error(L));
4577 }
4578
4579 /* check if the header is already registered if it is not
4580 * the case, register it.
4581 */
4582 ret = lua_getfield(L, -1, name);
4583 if (ret == LUA_TNIL) {
4584
4585 /* Entry not found. */
4586 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4587
4588 /* Insert the new header name in the array in the top of the stack.
4589 * It left the new array in the top of the stack.
4590 */
4591 lua_newtable(L);
4592 lua_pushvalue(L, 2);
4593 lua_pushvalue(L, -2);
4594 lua_settable(L, -4);
4595
4596 } else if (ret != LUA_TTABLE) {
4597
4598 /* corruption error. */
4599 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4600 "is expected as an array. %s found", name, lua_typename(L, ret));
4601 WILL_LJMP(lua_error(L));
4602 }
4603
4604 /* Now the top od thestack is an array of values. We push
4605 * the header value as new entry.
4606 */
4607 lua_pushvalue(L, 3);
4608 ret = lua_rawlen(L, -2);
4609 lua_rawseti(L, -2, ret + 1);
4610 lua_pushboolean(L, 1);
4611 return 1;
4612}
4613
4614__LJMP static int hlua_applet_http_status(lua_State *L)
4615{
4616 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4617 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004618 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004619
4620 if (status < 100 || status > 599) {
4621 lua_pushboolean(L, 0);
4622 return 1;
4623 }
4624
4625 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004626 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004627 lua_pushboolean(L, 1);
4628 return 1;
4629}
4630
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004631
Christopher Fauleta2097962019-07-15 16:25:33 +02004632__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004633{
4634 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4635 struct stream_interface *si = appctx->appctx->owner;
4636 struct channel *res = si_ic(si);
4637 struct htx *htx;
4638 struct htx_sl *sl;
4639 struct h1m h1m;
4640 const char *status, *reason;
4641 const char *name, *value;
4642 size_t nlen, vlen;
4643 unsigned int flags;
4644
4645 /* Send the message at once. */
4646 htx = htx_from_buf(&res->buf);
4647 h1m_init_res(&h1m);
4648
4649 /* Use the same http version than the request. */
4650 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4651 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4652 if (reason == NULL)
4653 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4654 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4655 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4656 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4657 }
4658 else {
4659 flags = HTX_SL_F_IS_RESP;
4660 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4661 }
4662 if (!sl) {
4663 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004664 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004665 WILL_LJMP(lua_error(L));
4666 }
4667 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4668
4669 /* Get the array associated to the field "response" in the object AppletHTTP. */
4670 lua_pushvalue(L, 0);
4671 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4672 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004673 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004674 WILL_LJMP(lua_error(L));
4675 }
4676
4677 /* Browse the list of headers. */
4678 lua_pushnil(L);
4679 while(lua_next(L, -2) != 0) {
4680 /* We expect a string as -2. */
4681 if (lua_type(L, -2) != LUA_TSTRING) {
4682 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004683 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004684 lua_typename(L, lua_type(L, -2)));
4685 WILL_LJMP(lua_error(L));
4686 }
4687 name = lua_tolstring(L, -2, &nlen);
4688
4689 /* We expect an array as -1. */
4690 if (lua_type(L, -1) != LUA_TTABLE) {
4691 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004692 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004693 name,
4694 lua_typename(L, lua_type(L, -1)));
4695 WILL_LJMP(lua_error(L));
4696 }
4697
4698 /* Browse the table who is on the top of the stack. */
4699 lua_pushnil(L);
4700 while(lua_next(L, -2) != 0) {
4701 int id;
4702
4703 /* We expect a number as -2. */
4704 if (lua_type(L, -2) != LUA_TNUMBER) {
4705 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004706 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004707 name,
4708 lua_typename(L, lua_type(L, -2)));
4709 WILL_LJMP(lua_error(L));
4710 }
4711 id = lua_tointeger(L, -2);
4712
4713 /* We expect a string as -2. */
4714 if (lua_type(L, -1) != LUA_TSTRING) {
4715 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004716 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004717 name, id,
4718 lua_typename(L, lua_type(L, -1)));
4719 WILL_LJMP(lua_error(L));
4720 }
4721 value = lua_tolstring(L, -1, &vlen);
4722
4723 /* Simple Protocol checks. */
4724 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004725 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004726 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4727 struct ist v = ist2(value, vlen);
4728 int ret;
4729
4730 ret = h1_parse_cont_len_header(&h1m, &v);
4731 if (ret < 0) {
4732 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004733 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004734 name);
4735 WILL_LJMP(lua_error(L));
4736 }
4737 else if (ret == 0)
4738 goto next; /* Skip it */
4739 }
4740
4741 /* Add a new header */
4742 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4743 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004744 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004745 name);
4746 WILL_LJMP(lua_error(L));
4747 }
4748 next:
4749 /* Remove the array from the stack, and get next element with a remaining string. */
4750 lua_pop(L, 1);
4751 }
4752
4753 /* Remove the array from the stack, and get next element with a remaining string. */
4754 lua_pop(L, 1);
4755 }
4756
4757 if (h1m.flags & H1_MF_CHNK)
4758 h1m.flags &= ~H1_MF_CLEN;
4759 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4760 h1m.flags |= H1_MF_XFER_LEN;
4761
4762 /* Uset HTX start-line flags */
4763 if (h1m.flags & H1_MF_XFER_ENC)
4764 flags |= HTX_SL_F_XFER_ENC;
4765 if (h1m.flags & H1_MF_XFER_LEN) {
4766 flags |= HTX_SL_F_XFER_LEN;
4767 if (h1m.flags & H1_MF_CHNK)
4768 flags |= HTX_SL_F_CHNK;
4769 else if (h1m.flags & H1_MF_CLEN)
4770 flags |= HTX_SL_F_CLEN;
4771 if (h1m.body_len == 0)
4772 flags |= HTX_SL_F_BODYLESS;
4773 }
4774 sl->flags |= flags;
4775
4776 /* If we dont have a content-length set, and the HTTP version is 1.1
4777 * and the status code implies the presence of a message body, we must
4778 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004779 * for the keepalive compliance. If the applet announces a transfer-encoding
4780 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004781 */
4782 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4783 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4784 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4785 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4786 /* Add a new header */
4787 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4788 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4789 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004790 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004791 WILL_LJMP(lua_error(L));
4792 }
4793 }
4794
4795 /* Finalize headers. */
4796 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4797 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004798 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004799 WILL_LJMP(lua_error(L));
4800 }
4801
4802 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4803 b_reset(&res->buf);
4804 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4805 WILL_LJMP(lua_error(L));
4806 }
4807
4808 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004809 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004810
4811 /* Headers sent, set the flag. */
4812 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4813 return 0;
4814
4815}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004816/* We will build the status line and the headers of the HTTP response.
4817 * We will try send at once if its not possible, we give back the hand
4818 * waiting for more room.
4819 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004820__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004821{
4822 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4823 struct stream_interface *si = appctx->appctx->owner;
4824 struct channel *res = si_ic(si);
4825
4826 if (co_data(res)) {
4827 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004828 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004829 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004830 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004831}
4832
4833
Christopher Fauleta2097962019-07-15 16:25:33 +02004834__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004835{
Christopher Fauleta2097962019-07-15 16:25:33 +02004836 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004837}
4838
Christopher Fauleta2097962019-07-15 16:25:33 +02004839/*
4840 *
4841 *
4842 * Class HTTP
4843 *
4844 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004845 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004846
4847/* Returns a struct hlua_txn if the stack entry "ud" is
4848 * a class stream, otherwise it throws an error.
4849 */
4850__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004851{
Christopher Fauleta2097962019-07-15 16:25:33 +02004852 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4853}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004854
Christopher Fauleta2097962019-07-15 16:25:33 +02004855/* This function creates and push in the stack a HTTP object
4856 * according with a current TXN.
4857 */
4858static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4859{
4860 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004861
Christopher Fauleta2097962019-07-15 16:25:33 +02004862 /* Check stack size. */
4863 if (!lua_checkstack(L, 3))
4864 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004865
Christopher Fauleta2097962019-07-15 16:25:33 +02004866 /* Create the object: obj[0] = userdata.
4867 * Note that the base of the Converters object is the
4868 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004869 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004870 lua_newtable(L);
4871 htxn = lua_newuserdata(L, sizeof(*htxn));
4872 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004873
4874 htxn->s = txn->s;
4875 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004876 htxn->dir = txn->dir;
4877 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004878
4879 /* Pop a class stream metatable and affect it to the table. */
4880 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4881 lua_setmetatable(L, -2);
4882
4883 return 1;
4884}
4885
4886/* This function creates ans returns an array of HTTP headers.
4887 * This function does not fails. It is used as wrapper with the
4888 * 2 following functions.
4889 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004890__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004891{
Christopher Fauleta2097962019-07-15 16:25:33 +02004892 struct htx *htx;
4893 int32_t pos;
4894
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004895 /* Create the table. */
4896 lua_newtable(L);
4897
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004898
Christopher Fauleta2097962019-07-15 16:25:33 +02004899 htx = htxbuf(&msg->chn->buf);
4900 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4901 struct htx_blk *blk = htx_get_blk(htx, pos);
4902 enum htx_blk_type type = htx_get_blk_type(blk);
4903 struct ist n, v;
4904 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004905
Christopher Fauleta2097962019-07-15 16:25:33 +02004906 if (type == HTX_BLK_HDR) {
4907 n = htx_get_blk_name(htx,blk);
4908 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004909 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004910 else if (type == HTX_BLK_EOH)
4911 break;
4912 else
4913 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004914
Christopher Fauleta2097962019-07-15 16:25:33 +02004915 /* Check for existing entry:
4916 * assume that the table is on the top of the stack, and
4917 * push the key in the stack, the function lua_gettable()
4918 * perform the lookup.
4919 */
4920 lua_pushlstring(L, n.ptr, n.len);
4921 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004922
Christopher Fauleta2097962019-07-15 16:25:33 +02004923 switch (lua_type(L, -1)) {
4924 case LUA_TNIL:
4925 /* Table not found, create it. */
4926 lua_pop(L, 1); /* remove the nil value. */
4927 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4928 lua_newtable(L); /* create and push empty table. */
4929 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4930 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4931 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004932 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004933
Christopher Fauleta2097962019-07-15 16:25:33 +02004934 case LUA_TTABLE:
4935 /* Entry found: push the value in the table. */
4936 len = lua_rawlen(L, -1);
4937 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4938 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4939 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4940 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004941
Christopher Fauleta2097962019-07-15 16:25:33 +02004942 default:
4943 /* Other cases are errors. */
4944 hlua_pusherror(L, "internal error during the parsing of headers.");
4945 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004946 }
4947 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004948 return 1;
4949}
4950
4951__LJMP static int hlua_http_req_get_headers(lua_State *L)
4952{
4953 struct hlua_txn *htxn;
4954
4955 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4956 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4957
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004958 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004959 WILL_LJMP(lua_error(L));
4960
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004961 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004962}
4963
4964__LJMP static int hlua_http_res_get_headers(lua_State *L)
4965{
4966 struct hlua_txn *htxn;
4967
4968 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4969 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4970
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004971 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004972 WILL_LJMP(lua_error(L));
4973
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004974 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004975}
4976
4977/* This function replace full header, or just a value in
4978 * the request or in the response. It is a wrapper fir the
4979 * 4 following functions.
4980 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004981__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004982{
4983 size_t name_len;
4984 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4985 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4986 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004987 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004988 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004989
Dragan Dosen26743032019-04-30 15:54:36 +02004990 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004991 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4992
Christopher Fauleta2097962019-07-15 16:25:33 +02004993 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004994 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004995 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004996 return 0;
4997}
4998
4999__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5000{
5001 struct hlua_txn *htxn;
5002
5003 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5004 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5005
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005006 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005007 WILL_LJMP(lua_error(L));
5008
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005009 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005010}
5011
5012__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5013{
5014 struct hlua_txn *htxn;
5015
5016 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5017 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5018
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005019 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005020 WILL_LJMP(lua_error(L));
5021
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005022 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005023}
5024
5025__LJMP static int hlua_http_req_rep_val(lua_State *L)
5026{
5027 struct hlua_txn *htxn;
5028
5029 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5030 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5031
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005032 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005033 WILL_LJMP(lua_error(L));
5034
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005035 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005036}
5037
5038__LJMP static int hlua_http_res_rep_val(lua_State *L)
5039{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005040 struct hlua_txn *htxn;
5041
5042 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5043 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5044
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005045 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005046 WILL_LJMP(lua_error(L));
5047
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005048 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005049}
5050
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005051/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005052 * It is a wrapper for the 2 following functions.
5053 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005054__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005055{
5056 size_t len;
5057 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005058 struct htx *htx = htxbuf(&msg->chn->buf);
5059 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005060
Christopher Fauleta2097962019-07-15 16:25:33 +02005061 ctx.blk = NULL;
5062 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5063 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005064 return 0;
5065}
5066
5067__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5068{
5069 struct hlua_txn *htxn;
5070
5071 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5072 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5073
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005074 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005075 WILL_LJMP(lua_error(L));
5076
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005077 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005078}
5079
5080__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5081{
5082 struct hlua_txn *htxn;
5083
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005084 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005085 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5086
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005087 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005088 WILL_LJMP(lua_error(L));
5089
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005090 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005091}
5092
5093/* This function adds an header. It is a wrapper used by
5094 * the 2 following functions.
5095 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005096__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005097{
5098 size_t name_len;
5099 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5100 size_t value_len;
5101 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005102 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005103
Christopher Fauleta2097962019-07-15 16:25:33 +02005104 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5105 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005106 return 0;
5107}
5108
5109__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5110{
5111 struct hlua_txn *htxn;
5112
5113 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5114 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5115
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005116 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005117 WILL_LJMP(lua_error(L));
5118
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005119 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005120}
5121
5122__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5123{
5124 struct hlua_txn *htxn;
5125
5126 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5127 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5128
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005129 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005130 WILL_LJMP(lua_error(L));
5131
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005132 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005133}
5134
5135static int hlua_http_req_set_hdr(lua_State *L)
5136{
5137 struct hlua_txn *htxn;
5138
5139 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5140 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5141
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005142 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005143 WILL_LJMP(lua_error(L));
5144
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005145 hlua_http_del_hdr(L, &htxn->s->txn->req);
5146 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005147}
5148
5149static int hlua_http_res_set_hdr(lua_State *L)
5150{
5151 struct hlua_txn *htxn;
5152
5153 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5154 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5155
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005156 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005157 WILL_LJMP(lua_error(L));
5158
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005159 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5160 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005161}
5162
5163/* This function set the method. */
5164static int hlua_http_req_set_meth(lua_State *L)
5165{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005166 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005167 size_t name_len;
5168 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005169
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005170 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005171 WILL_LJMP(lua_error(L));
5172
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005173 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005174 return 1;
5175}
5176
5177/* This function set the method. */
5178static int hlua_http_req_set_path(lua_State *L)
5179{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005180 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005181 size_t name_len;
5182 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005183
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005184 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005185 WILL_LJMP(lua_error(L));
5186
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005187 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005188 return 1;
5189}
5190
5191/* This function set the query-string. */
5192static int hlua_http_req_set_query(lua_State *L)
5193{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005194 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005195 size_t name_len;
5196 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005197
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005198 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005199 WILL_LJMP(lua_error(L));
5200
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005201 /* Check length. */
5202 if (name_len > trash.size - 1) {
5203 lua_pushboolean(L, 0);
5204 return 1;
5205 }
5206
5207 /* Add the mark question as prefix. */
5208 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005209 trash.area[trash.data++] = '?';
5210 memcpy(trash.area + trash.data, name, name_len);
5211 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005212
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005213 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005214 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005215 return 1;
5216}
5217
5218/* This function set the uri. */
5219static int hlua_http_req_set_uri(lua_State *L)
5220{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005221 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005222 size_t name_len;
5223 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005224
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005225 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005226 WILL_LJMP(lua_error(L));
5227
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005228 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005229 return 1;
5230}
5231
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005232/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005233static int hlua_http_res_set_status(lua_State *L)
5234{
5235 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5236 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005237 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5238 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005239
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005240 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005241 WILL_LJMP(lua_error(L));
5242
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005243 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005244 return 0;
5245}
5246
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005247/*
5248 *
5249 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005250 * Class TXN
5251 *
5252 *
5253 */
5254
5255/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005256 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005257 */
5258__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5259{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005260 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005261}
5262
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005263__LJMP static int hlua_set_var(lua_State *L)
5264{
5265 struct hlua_txn *htxn;
5266 const char *name;
5267 size_t len;
5268 struct sample smp;
5269
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005270 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5271 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005272
5273 /* It is useles to retrieve the stream, but this function
5274 * runs only in a stream context.
5275 */
5276 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5277 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5278
5279 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005280 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005281 hlua_lua2smp(L, 3, &smp);
5282
5283 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005284 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005285
5286 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5287 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5288 else
5289 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5290
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005291 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005292}
5293
Christopher Faulet85d79c92016-11-09 16:54:56 +01005294__LJMP static int hlua_unset_var(lua_State *L)
5295{
5296 struct hlua_txn *htxn;
5297 const char *name;
5298 size_t len;
5299 struct sample smp;
5300
5301 MAY_LJMP(check_args(L, 2, "unset_var"));
5302
5303 /* It is useles to retrieve the stream, but this function
5304 * runs only in a stream context.
5305 */
5306 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5307 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5308
5309 /* Unset the variable. */
5310 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005311 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5312 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005313}
5314
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005315__LJMP static int hlua_get_var(lua_State *L)
5316{
5317 struct hlua_txn *htxn;
5318 const char *name;
5319 size_t len;
5320 struct sample smp;
5321
5322 MAY_LJMP(check_args(L, 2, "get_var"));
5323
5324 /* It is useles to retrieve the stream, but this function
5325 * runs only in a stream context.
5326 */
5327 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5328 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5329
Willy Tarreau7560dd42016-03-10 16:28:58 +01005330 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005331 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005332 lua_pushnil(L);
5333 return 1;
5334 }
5335
5336 return hlua_smp2lua(L, &smp);
5337}
5338
Willy Tarreau59551662015-03-10 14:23:13 +01005339__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005340{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005341 struct hlua *hlua;
5342
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005343 MAY_LJMP(check_args(L, 2, "set_priv"));
5344
Willy Tarreau87b09662015-04-03 00:22:06 +02005345 /* It is useles to retrieve the stream, but this function
5346 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005347 */
5348 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005349
5350 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005351 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005352 if (!hlua)
5353 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005354
5355 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005356 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005357
5358 /* Get and store new value. */
5359 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5360 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5361
5362 return 0;
5363}
5364
Willy Tarreau59551662015-03-10 14:23:13 +01005365__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005366{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005367 struct hlua *hlua;
5368
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005369 MAY_LJMP(check_args(L, 1, "get_priv"));
5370
Willy Tarreau87b09662015-04-03 00:22:06 +02005371 /* It is useles to retrieve the stream, but this function
5372 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005373 */
5374 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005375
5376 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005377 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005378 if (!hlua) {
5379 lua_pushnil(L);
5380 return 1;
5381 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005382
5383 /* Push configuration index in the stack. */
5384 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5385
5386 return 1;
5387}
5388
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005389/* Create stack entry containing a class TXN. This function
5390 * return 0 if the stack does not contains free slots,
5391 * otherwise it returns 1.
5392 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005393static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005394{
Willy Tarreaude491382015-04-06 11:04:28 +02005395 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005396
5397 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005398 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005399 return 0;
5400
5401 /* NOTE: The allocation never fails. The failure
5402 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005403 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005404 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005405 /* Create the object: obj[0] = userdata. */
5406 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005407 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005408 lua_rawseti(L, -2, 0);
5409
Willy Tarreaude491382015-04-06 11:04:28 +02005410 htxn->s = s;
5411 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005412 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005413 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005414
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005415 /* Create the "f" field that contains a list of fetches. */
5416 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005417 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005418 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005419 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005420
5421 /* Create the "sf" field that contains a list of stringsafe fetches. */
5422 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005423 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005424 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005425 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005426
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005427 /* Create the "c" field that contains a list of converters. */
5428 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005429 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005430 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005431 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005432
5433 /* Create the "sc" field that contains a list of stringsafe converters. */
5434 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005435 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005436 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005437 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005438
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005439 /* Create the "req" field that contains the request channel object. */
5440 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005441 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005442 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005443 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005444
5445 /* Create the "res" field that contains the response channel object. */
5446 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005447 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005448 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005449 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005450
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005451 /* Creates the HTTP object is the current proxy allows http. */
5452 lua_pushstring(L, "http");
5453 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005454 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005455 return 0;
5456 }
5457 else
5458 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005459 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005460
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005461 /* Pop a class sesison metatable and affect it to the userdata. */
5462 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5463 lua_setmetatable(L, -2);
5464
5465 return 1;
5466}
5467
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005468__LJMP static int hlua_txn_deflog(lua_State *L)
5469{
5470 const char *msg;
5471 struct hlua_txn *htxn;
5472
5473 MAY_LJMP(check_args(L, 2, "deflog"));
5474 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5475 msg = MAY_LJMP(luaL_checkstring(L, 2));
5476
5477 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5478 return 0;
5479}
5480
5481__LJMP static int hlua_txn_log(lua_State *L)
5482{
5483 int level;
5484 const char *msg;
5485 struct hlua_txn *htxn;
5486
5487 MAY_LJMP(check_args(L, 3, "log"));
5488 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5489 level = MAY_LJMP(luaL_checkinteger(L, 2));
5490 msg = MAY_LJMP(luaL_checkstring(L, 3));
5491
5492 if (level < 0 || level >= NB_LOG_LEVELS)
5493 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5494
5495 hlua_sendlog(htxn->s->be, level, msg);
5496 return 0;
5497}
5498
5499__LJMP static int hlua_txn_log_debug(lua_State *L)
5500{
5501 const char *msg;
5502 struct hlua_txn *htxn;
5503
5504 MAY_LJMP(check_args(L, 2, "Debug"));
5505 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5506 msg = MAY_LJMP(luaL_checkstring(L, 2));
5507 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5508 return 0;
5509}
5510
5511__LJMP static int hlua_txn_log_info(lua_State *L)
5512{
5513 const char *msg;
5514 struct hlua_txn *htxn;
5515
5516 MAY_LJMP(check_args(L, 2, "Info"));
5517 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5518 msg = MAY_LJMP(luaL_checkstring(L, 2));
5519 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5520 return 0;
5521}
5522
5523__LJMP static int hlua_txn_log_warning(lua_State *L)
5524{
5525 const char *msg;
5526 struct hlua_txn *htxn;
5527
5528 MAY_LJMP(check_args(L, 2, "Warning"));
5529 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5530 msg = MAY_LJMP(luaL_checkstring(L, 2));
5531 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5532 return 0;
5533}
5534
5535__LJMP static int hlua_txn_log_alert(lua_State *L)
5536{
5537 const char *msg;
5538 struct hlua_txn *htxn;
5539
5540 MAY_LJMP(check_args(L, 2, "Alert"));
5541 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5542 msg = MAY_LJMP(luaL_checkstring(L, 2));
5543 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5544 return 0;
5545}
5546
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005547__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5548{
5549 struct hlua_txn *htxn;
5550 int ll;
5551
5552 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5553 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5554 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5555
5556 if (ll < 0 || ll > 7)
5557 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5558
5559 htxn->s->logs.level = ll;
5560 return 0;
5561}
5562
5563__LJMP static int hlua_txn_set_tos(lua_State *L)
5564{
5565 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005566 int tos;
5567
5568 MAY_LJMP(check_args(L, 2, "set_tos"));
5569 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5570 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5571
Willy Tarreau1a18b542018-12-11 16:37:42 +01005572 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005573 return 0;
5574}
5575
5576__LJMP static int hlua_txn_set_mark(lua_State *L)
5577{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005578 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005579 int mark;
5580
5581 MAY_LJMP(check_args(L, 2, "set_mark"));
5582 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5583 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5584
Lukas Tribus579e3e32019-08-11 18:03:45 +02005585 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005586 return 0;
5587}
5588
Patrick Hemmer268a7072018-05-11 12:52:31 -04005589__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5590{
5591 struct hlua_txn *htxn;
5592
5593 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5594 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5595 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5596 return 0;
5597}
5598
5599__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5600{
5601 struct hlua_txn *htxn;
5602
5603 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5604 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5605 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5606 return 0;
5607}
5608
Christopher Faulet700d9e82020-01-31 12:21:52 +01005609/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005610 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005611 * message and terminate the transaction. It returns 1 on success and 0 on
5612 * error. The Reply must be on top of the stack.
5613 */
5614__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5615{
5616 struct htx *htx;
5617 struct htx_sl *sl;
5618 struct h1m h1m;
5619 const char *status, *reason, *body;
5620 size_t status_len, reason_len, body_len;
5621 int ret, code, flags;
5622
5623 code = 200;
5624 status = "200";
5625 status_len = 3;
5626 ret = lua_getfield(L, -1, "status");
5627 if (ret == LUA_TNUMBER) {
5628 code = lua_tointeger(L, -1);
5629 status = lua_tolstring(L, -1, &status_len);
5630 }
5631 lua_pop(L, 1);
5632
5633 reason = http_get_reason(code);
5634 reason_len = strlen(reason);
5635 ret = lua_getfield(L, -1, "reason");
5636 if (ret == LUA_TSTRING)
5637 reason = lua_tolstring(L, -1, &reason_len);
5638 lua_pop(L, 1);
5639
5640 body = NULL;
5641 body_len = 0;
5642 ret = lua_getfield(L, -1, "body");
5643 if (ret == LUA_TSTRING)
5644 body = lua_tolstring(L, -1, &body_len);
5645 lua_pop(L, 1);
5646
5647 /* Prepare the response before inserting the headers */
5648 h1m_init_res(&h1m);
5649 htx = htx_from_buf(&s->res.buf);
5650 channel_htx_truncate(&s->res, htx);
5651 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5652 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5653 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5654 ist2(status, status_len), ist2(reason, reason_len));
5655 }
5656 else {
5657 flags = HTX_SL_F_IS_RESP;
5658 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5659 ist2(status, status_len), ist2(reason, reason_len));
5660 }
5661 if (!sl)
5662 goto fail;
5663 sl->info.res.status = code;
5664
5665 /* Push in the stack the "headers" entry. */
5666 ret = lua_getfield(L, -1, "headers");
5667 if (ret != LUA_TTABLE)
5668 goto skip_headers;
5669
5670 lua_pushnil(L);
5671 while (lua_next(L, -2) != 0) {
5672 struct ist name, value;
5673 const char *n, *v;
5674 size_t nlen, vlen;
5675
5676 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5677 /* Skip element if the key is not a string or if the value is not a table */
5678 goto next_hdr;
5679 }
5680
5681 n = lua_tolstring(L, -2, &nlen);
5682 name = ist2(n, nlen);
5683 if (isteqi(name, ist("content-length"))) {
5684 /* Always skip content-length header. It will be added
5685 * later with the correct len
5686 */
5687 goto next_hdr;
5688 }
5689
5690 /* Loop on header's values */
5691 lua_pushnil(L);
5692 while (lua_next(L, -2)) {
5693 if (!lua_isstring(L, -1)) {
5694 /* Skip the value if it is not a string */
5695 goto next_value;
5696 }
5697
5698 v = lua_tolstring(L, -1, &vlen);
5699 value = ist2(v, vlen);
5700
5701 if (isteqi(name, ist("transfer-encoding")))
5702 h1_parse_xfer_enc_header(&h1m, value);
5703 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5704 goto fail;
5705
5706 next_value:
5707 lua_pop(L, 1);
5708 }
5709
5710 next_hdr:
5711 lua_pop(L, 1);
5712 }
5713 skip_headers:
5714 lua_pop(L, 1);
5715
5716 /* Update h1m flags: CLEN is set if CHNK is not present */
5717 if (!(h1m.flags & H1_MF_CHNK)) {
5718 const char *clen = ultoa(body_len);
5719
5720 h1m.flags |= H1_MF_CLEN;
5721 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5722 goto fail;
5723 }
5724 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5725 h1m.flags |= H1_MF_XFER_LEN;
5726
5727 /* Update HTX start-line flags */
5728 if (h1m.flags & H1_MF_XFER_ENC)
5729 flags |= HTX_SL_F_XFER_ENC;
5730 if (h1m.flags & H1_MF_XFER_LEN) {
5731 flags |= HTX_SL_F_XFER_LEN;
5732 if (h1m.flags & H1_MF_CHNK)
5733 flags |= HTX_SL_F_CHNK;
5734 else if (h1m.flags & H1_MF_CLEN)
5735 flags |= HTX_SL_F_CLEN;
5736 if (h1m.body_len == 0)
5737 flags |= HTX_SL_F_BODYLESS;
5738 }
5739 sl->flags |= flags;
5740
5741
5742 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5743 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5744 !htx_add_endof(htx, HTX_BLK_EOM))
5745 goto fail;
5746
5747 /* Now, forward the response and terminate the transaction */
5748 s->txn->status = code;
5749 htx_to_buf(htx, &s->res.buf);
5750 if (!http_forward_proxy_resp(s, 1))
5751 goto fail;
5752
5753 return 1;
5754
5755 fail:
5756 channel_htx_truncate(&s->res, htx);
5757 return 0;
5758}
5759
5760/* Terminate a transaction if called from a lua action. For TCP streams,
5761 * processing is just aborted. Nothing is returned to the client and all
5762 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5763 * is forwarded to the client before terminating the transaction. On success,
5764 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5765 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5766 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005767 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005768__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005769{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005770 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005771 struct stream *s;
5772 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005773
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005774 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005775
Christopher Faulet700d9e82020-01-31 12:21:52 +01005776 /* If the flags NOTERM is set, we cannot terminate the session, so we
5777 * just end the execution of the current lua code. */
5778 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005779 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005780
Christopher Faulet700d9e82020-01-31 12:21:52 +01005781 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005782 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005783 struct channel *req = &s->req;
5784 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005785
Christopher Faulet700d9e82020-01-31 12:21:52 +01005786 channel_auto_read(req);
5787 channel_abort(req);
5788 channel_auto_close(req);
5789 channel_erase(req);
5790
5791 res->wex = tick_add_ifset(now_ms, res->wto);
5792 channel_auto_read(res);
5793 channel_auto_close(res);
5794 channel_shutr_now(res);
5795
5796 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5797 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005798 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005799
Christopher Faulet700d9e82020-01-31 12:21:52 +01005800 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5801 /* No reply or invalid reply */
5802 s->txn->status = 0;
5803 http_reply_and_close(s, 0, NULL);
5804 }
5805 else {
5806 /* Remove extra args to have the reply on top of the stack */
5807 if (lua_gettop(L) > 2)
5808 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005809
Christopher Faulet700d9e82020-01-31 12:21:52 +01005810 if (!hlua_txn_forward_reply(L, s)) {
5811 if (!(s->flags & SF_ERR_MASK))
5812 s->flags |= SF_ERR_PRXCOND;
5813 lua_pushinteger(L, ACT_RET_ERR);
5814 WILL_LJMP(hlua_done(L));
5815 return 0; /* Never reached */
5816 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005817 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005818
Christopher Faulet700d9e82020-01-31 12:21:52 +01005819 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5820 if (htxn->dir == SMP_OPT_DIR_REQ) {
5821 /* let's log the request time */
5822 s->logs.tv_request = now;
5823 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5824 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5825 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005826
Christopher Faulet700d9e82020-01-31 12:21:52 +01005827 done:
5828 if (!(s->flags & SF_ERR_MASK))
5829 s->flags |= SF_ERR_LOCAL;
5830 if (!(s->flags & SF_FINST_MASK))
5831 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005832
Christopher Faulet4ad73102020-03-05 11:07:31 +01005833 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005834 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005835 return 0;
5836}
5837
Christopher Faulet700d9e82020-01-31 12:21:52 +01005838/*
5839 *
5840 *
5841 * Class REPLY
5842 *
5843 *
5844 */
5845
5846/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5847 * free slots, the function fails and returns 0;
5848 */
5849static int hlua_txn_reply_new(lua_State *L)
5850{
5851 struct hlua_txn *htxn;
5852 const char *reason, *body = NULL;
5853 int ret, status;
5854
5855 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005856 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005857 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5858 WILL_LJMP(lua_error(L));
5859 }
5860
5861 /* Default value */
5862 status = 200;
5863 reason = http_get_reason(status);
5864
5865 if (lua_istable(L, 2)) {
5866 /* load status and reason from the table argument at index 2 */
5867 ret = lua_getfield(L, 2, "status");
5868 if (ret == LUA_TNIL)
5869 goto reason;
5870 else if (ret != LUA_TNUMBER) {
5871 /* invalid status: ignore the reason */
5872 goto body;
5873 }
5874 status = lua_tointeger(L, -1);
5875
5876 reason:
5877 lua_pop(L, 1); /* restore the stack: remove status */
5878 ret = lua_getfield(L, 2, "reason");
5879 if (ret == LUA_TSTRING)
5880 reason = lua_tostring(L, -1);
5881
5882 body:
5883 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5884 ret = lua_getfield(L, 2, "body");
5885 if (ret == LUA_TSTRING)
5886 body = lua_tostring(L, -1);
5887 lua_pop(L, 1); /* restore the stack: remove body */
5888 }
5889
5890 /* Create the Reply table */
5891 lua_newtable(L);
5892
5893 /* Add status element */
5894 lua_pushstring(L, "status");
5895 lua_pushinteger(L, status);
5896 lua_settable(L, -3);
5897
5898 /* Add reason element */
5899 reason = http_get_reason(status);
5900 lua_pushstring(L, "reason");
5901 lua_pushstring(L, reason);
5902 lua_settable(L, -3);
5903
5904 /* Add body element, nil if undefined */
5905 lua_pushstring(L, "body");
5906 if (body)
5907 lua_pushstring(L, body);
5908 else
5909 lua_pushnil(L);
5910 lua_settable(L, -3);
5911
5912 /* Add headers element */
5913 lua_pushstring(L, "headers");
5914 lua_newtable(L);
5915
5916 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5917 if (lua_istable(L, 2)) {
5918 /* load headers from the table argument at index 2. If it is a table, copy it. */
5919 ret = lua_getfield(L, 2, "headers");
5920 if (ret == LUA_TTABLE) {
5921 /* stack: [ ... <headers:table>, <table> ] */
5922 lua_pushnil(L);
5923 while (lua_next(L, -2) != 0) {
5924 /* stack: [ ... <headers:table>, <table>, k, v] */
5925 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5926 /* invalid value type, skip it */
5927 lua_pop(L, 1);
5928 continue;
5929 }
5930
5931
5932 /* Duplicate the key and swap it with the value. */
5933 lua_pushvalue(L, -2);
5934 lua_insert(L, -2);
5935 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5936
5937 lua_newtable(L);
5938 lua_insert(L, -2);
5939 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5940
5941 if (lua_isstring(L, -1)) {
5942 /* push the value in the inner table */
5943 lua_rawseti(L, -2, 1);
5944 }
5945 else { /* table */
5946 lua_pushnil(L);
5947 while (lua_next(L, -2) != 0) {
5948 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5949 if (!lua_isstring(L, -1)) {
5950 /* invalid value type, skip it*/
5951 lua_pop(L, 1);
5952 continue;
5953 }
5954 /* push the value in the inner table */
5955 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5956 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5957 }
5958 lua_pop(L, 1);
5959 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5960 }
5961
5962 /* push (k,v) on the stack in the headers table:
5963 * stack: [ ... <headers:table>, <table>, k, k, v ]
5964 */
5965 lua_settable(L, -5);
5966 /* stack: [ ... <headers:table>, <table>, k ] */
5967 }
5968 }
5969 lua_pop(L, 1);
5970 }
5971 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5972 lua_settable(L, -3);
5973 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5974
5975 /* Pop a class sesison metatable and affect it to the userdata. */
5976 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5977 lua_setmetatable(L, -2);
5978 return 1;
5979}
5980
5981/* Set the reply status code, and optionally the reason. If no reason is
5982 * provided, the default one corresponding to the status code is used.
5983 */
5984__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5985{
5986 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5987 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5988
5989 /* First argument (self) must be a table */
5990 luaL_checktype(L, 1, LUA_TTABLE);
5991
5992 if (status < 100 || status > 599) {
5993 lua_pushboolean(L, 0);
5994 return 1;
5995 }
5996 if (!reason)
5997 reason = http_get_reason(status);
5998
5999 lua_pushinteger(L, status);
6000 lua_setfield(L, 1, "status");
6001
6002 lua_pushstring(L, reason);
6003 lua_setfield(L, 1, "reason");
6004
6005 lua_pushboolean(L, 1);
6006 return 1;
6007}
6008
6009/* Add a header into the reply object. Each header name is associated to an
6010 * array of values in the "headers" table. If the header name is not found, a
6011 * new entry is created.
6012 */
6013__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6014{
6015 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6016 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6017 int ret;
6018
6019 /* First argument (self) must be a table */
6020 luaL_checktype(L, 1, LUA_TTABLE);
6021
6022 /* Push in the stack the "headers" entry. */
6023 ret = lua_getfield(L, 1, "headers");
6024 if (ret != LUA_TTABLE) {
6025 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6026 WILL_LJMP(lua_error(L));
6027 }
6028
6029 /* check if the header is already registered. If not, register it. */
6030 ret = lua_getfield(L, -1, name);
6031 if (ret == LUA_TNIL) {
6032 /* Entry not found. */
6033 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6034
6035 /* Insert the new header name in the array in the top of the stack.
6036 * It left the new array in the top of the stack.
6037 */
6038 lua_newtable(L);
6039 lua_pushstring(L, name);
6040 lua_pushvalue(L, -2);
6041 lua_settable(L, -4);
6042 }
6043 else if (ret != LUA_TTABLE) {
6044 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6045 WILL_LJMP(lua_error(L));
6046 }
6047
6048 /* Now the top od thestack is an array of values. We push
6049 * the header value as new entry.
6050 */
6051 lua_pushstring(L, value);
6052 ret = lua_rawlen(L, -2);
6053 lua_rawseti(L, -2, ret + 1);
6054
6055 lua_pushboolean(L, 1);
6056 return 1;
6057}
6058
6059/* Remove all occurrences of a given header name. */
6060__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6061{
6062 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6063 int ret;
6064
6065 /* First argument (self) must be a table */
6066 luaL_checktype(L, 1, LUA_TTABLE);
6067
6068 /* Push in the stack the "headers" entry. */
6069 ret = lua_getfield(L, 1, "headers");
6070 if (ret != LUA_TTABLE) {
6071 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6072 WILL_LJMP(lua_error(L));
6073 }
6074
6075 lua_pushstring(L, name);
6076 lua_pushnil(L);
6077 lua_settable(L, -3);
6078
6079 lua_pushboolean(L, 1);
6080 return 1;
6081}
6082
6083/* Set the reply's body. Overwrite any existing entry. */
6084__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6085{
6086 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6087
6088 /* First argument (self) must be a table */
6089 luaL_checktype(L, 1, LUA_TTABLE);
6090
6091 lua_pushstring(L, payload);
6092 lua_setfield(L, 1, "body");
6093
6094 lua_pushboolean(L, 1);
6095 return 1;
6096}
6097
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006098__LJMP static int hlua_log(lua_State *L)
6099{
6100 int level;
6101 const char *msg;
6102
6103 MAY_LJMP(check_args(L, 2, "log"));
6104 level = MAY_LJMP(luaL_checkinteger(L, 1));
6105 msg = MAY_LJMP(luaL_checkstring(L, 2));
6106
6107 if (level < 0 || level >= NB_LOG_LEVELS)
6108 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6109
6110 hlua_sendlog(NULL, level, msg);
6111 return 0;
6112}
6113
6114__LJMP static int hlua_log_debug(lua_State *L)
6115{
6116 const char *msg;
6117
6118 MAY_LJMP(check_args(L, 1, "debug"));
6119 msg = MAY_LJMP(luaL_checkstring(L, 1));
6120 hlua_sendlog(NULL, LOG_DEBUG, msg);
6121 return 0;
6122}
6123
6124__LJMP static int hlua_log_info(lua_State *L)
6125{
6126 const char *msg;
6127
6128 MAY_LJMP(check_args(L, 1, "info"));
6129 msg = MAY_LJMP(luaL_checkstring(L, 1));
6130 hlua_sendlog(NULL, LOG_INFO, msg);
6131 return 0;
6132}
6133
6134__LJMP static int hlua_log_warning(lua_State *L)
6135{
6136 const char *msg;
6137
6138 MAY_LJMP(check_args(L, 1, "warning"));
6139 msg = MAY_LJMP(luaL_checkstring(L, 1));
6140 hlua_sendlog(NULL, LOG_WARNING, msg);
6141 return 0;
6142}
6143
6144__LJMP static int hlua_log_alert(lua_State *L)
6145{
6146 const char *msg;
6147
6148 MAY_LJMP(check_args(L, 1, "alert"));
6149 msg = MAY_LJMP(luaL_checkstring(L, 1));
6150 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006151 return 0;
6152}
6153
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006154__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006155{
6156 int wakeup_ms = lua_tointeger(L, -1);
6157 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006158 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006159 return 0;
6160}
6161
6162__LJMP static int hlua_sleep(lua_State *L)
6163{
6164 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006165 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006166
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006167 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006168
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006169 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006170 wakeup_ms = tick_add(now_ms, delay);
6171 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006172
Willy Tarreau9635e032018-10-16 17:52:55 +02006173 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006174 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006175}
6176
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006177__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006178{
6179 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006180 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006181
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006182 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006183
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006184 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006185 wakeup_ms = tick_add(now_ms, delay);
6186 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006187
Willy Tarreau9635e032018-10-16 17:52:55 +02006188 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006189 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006190}
6191
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006192/* This functionis an LUA binding. it permits to give back
6193 * the hand at the HAProxy scheduler. It is used when the
6194 * LUA processing consumes a lot of time.
6195 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006196__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006197{
6198 return 0;
6199}
6200
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006201__LJMP static int hlua_yield(lua_State *L)
6202{
Willy Tarreau9635e032018-10-16 17:52:55 +02006203 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006204 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006205}
6206
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006207/* This function change the nice of the currently executed
6208 * task. It is used set low or high priority at the current
6209 * task.
6210 */
Willy Tarreau59551662015-03-10 14:23:13 +01006211__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006212{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006213 struct hlua *hlua;
6214 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006215
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006216 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006217 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006218
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006219 /* Get hlua struct, or NULL if we execute from main lua state */
6220 hlua = hlua_gethlua(L);
6221
6222 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006223 if (!hlua || !hlua->task)
6224 return 0;
6225
6226 if (nice < -1024)
6227 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006228 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006229 nice = 1024;
6230
6231 hlua->task->nice = nice;
6232 return 0;
6233}
6234
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006235/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006236 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6237 * return an E_AGAIN signal, the emmiter of this signal must set a
6238 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006239 *
6240 * Task wrapper are longjmp safe because the only one Lua code
6241 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006242 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006243struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006244{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006245 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006246 enum hlua_exec status;
6247
Christopher Faulet5bc99722018-04-25 10:34:45 +02006248 if (task->thread_mask == MAX_THREADS_MASK)
6249 task_set_affinity(task, tid_bit);
6250
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006251 /* If it is the first call to the task, we must initialize the
6252 * execution timeouts.
6253 */
6254 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006255 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006256
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006257 /* Execute the Lua code. */
6258 status = hlua_ctx_resume(hlua, 1);
6259
6260 switch (status) {
6261 /* finished or yield */
6262 case HLUA_E_OK:
6263 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006264 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006265 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006266 break;
6267
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006268 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006269 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006270 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006271 break;
6272
6273 /* finished with error. */
6274 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006275 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006276 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006277 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006278 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006279 break;
6280
6281 case HLUA_E_ERR:
6282 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006283 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006284 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006285 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006286 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006287 break;
6288 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006289 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006290}
6291
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006292/* This function is an LUA binding that register LUA function to be
6293 * executed after the HAProxy configuration parsing and before the
6294 * HAProxy scheduler starts. This function expect only one LUA
6295 * argument that is a function. This function returns nothing, but
6296 * throws if an error is encountered.
6297 */
6298__LJMP static int hlua_register_init(lua_State *L)
6299{
6300 struct hlua_init_function *init;
6301 int ref;
6302
6303 MAY_LJMP(check_args(L, 1, "register_init"));
6304
6305 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6306
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006307 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006308 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006309 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006310
6311 init->function_ref = ref;
6312 LIST_ADDQ(&hlua_init_functions, &init->l);
6313 return 0;
6314}
6315
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006316/* This functio is an LUA binding. It permits to register a task
6317 * executed in parallel of the main HAroxy activity. The task is
6318 * created and it is set in the HAProxy scheduler. It can be called
6319 * from the "init" section, "post init" or during the runtime.
6320 *
6321 * Lua prototype:
6322 *
6323 * <none> core.register_task(<function>)
6324 */
6325static int hlua_register_task(lua_State *L)
6326{
6327 struct hlua *hlua;
6328 struct task *task;
6329 int ref;
6330
6331 MAY_LJMP(check_args(L, 1, "register_task"));
6332
6333 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6334
Willy Tarreaubafbe012017-11-24 17:34:44 +01006335 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006336 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006337 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006338
Emeric Brunc60def82017-09-27 14:59:38 +02006339 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006340 if (!task)
6341 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6342
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006343 task->context = hlua;
6344 task->process = hlua_process_task;
6345
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006346 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006347 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006348
6349 /* Restore the function in the stack. */
6350 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6351 hlua->nargs = 0;
6352
6353 /* Schedule task. */
6354 task_schedule(task, now_ms);
6355
6356 return 0;
6357}
6358
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006359/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6360 * doesn't allow "yield" functions because the HAProxy engine cannot
6361 * resume converters.
6362 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006363static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006364{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006365 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006366 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006367 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006368
Willy Tarreaube508f12016-03-10 11:47:01 +01006369 if (!stream)
6370 return 0;
6371
Willy Tarreau87b09662015-04-03 00:22:06 +02006372 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006373 * Lua context can be not initialized. This behavior
6374 * permits to save performances because a systematic
6375 * Lua initialization cause 5% performances loss.
6376 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006377 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006378 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006379 if (!stream->hlua) {
6380 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6381 return 0;
6382 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006383 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006384 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6385 return 0;
6386 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006387 }
6388
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006389 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006390 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006391
6392 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006393 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6394 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6395 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006396 else
6397 error = "critical error";
6398 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006399 return 0;
6400 }
6401
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006402 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006403 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006404 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006405 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006406 return 0;
6407 }
6408
6409 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006410 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006411
6412 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006413 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006414 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006415 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006416 return 0;
6417 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006418 hlua_smp2lua(stream->hlua->T, smp);
6419 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006420
6421 /* push keywords in the stack. */
6422 if (arg_p) {
6423 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006424 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006425 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006426 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006427 return 0;
6428 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006429 hlua_arg2lua(stream->hlua->T, arg_p);
6430 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006431 }
6432 }
6433
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006434 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006435 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006436
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006437 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006438 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006439 }
6440
6441 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006442 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006443 /* finished. */
6444 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006445 /* If the stack is empty, the function fails. */
6446 if (lua_gettop(stream->hlua->T) <= 0)
6447 return 0;
6448
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006449 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006450 hlua_lua2smp(stream->hlua->T, -1, smp);
6451 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006452 return 1;
6453
6454 /* yield. */
6455 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006456 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006457 return 0;
6458
6459 /* finished with error. */
6460 case HLUA_E_ERRMSG:
6461 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006462 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006463 fcn->name, lua_tostring(stream->hlua->T, -1));
6464 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006465 return 0;
6466
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006467 case HLUA_E_ETMOUT:
6468 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6469 return 0;
6470
6471 case HLUA_E_NOMEM:
6472 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6473 return 0;
6474
6475 case HLUA_E_YIELD:
6476 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6477 return 0;
6478
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006479 case HLUA_E_ERR:
6480 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006481 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006482 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006483
6484 default:
6485 return 0;
6486 }
6487}
6488
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006489/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6490 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006491 * resume sample-fetches. This function will be called by the sample
6492 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006493 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006494static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6495 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006496{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006497 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006498 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006499 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006500 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006501
Willy Tarreaube508f12016-03-10 11:47:01 +01006502 if (!stream)
6503 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006504
Willy Tarreau87b09662015-04-03 00:22:06 +02006505 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006506 * Lua context can be not initialized. This behavior
6507 * permits to save performances because a systematic
6508 * Lua initialization cause 5% performances loss.
6509 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006510 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006511 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006512 if (!stream->hlua) {
6513 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6514 return 0;
6515 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006516 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006517 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6518 return 0;
6519 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006520 }
6521
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006522 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006523 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006524
6525 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006526 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6527 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6528 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006529 else
6530 error = "critical error";
6531 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006532 return 0;
6533 }
6534
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006535 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006536 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006537 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006538 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006539 return 0;
6540 }
6541
6542 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006543 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006544
6545 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006546 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006547 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006548 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006549 return 0;
6550 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006551 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006552
6553 /* push keywords in the stack. */
6554 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6555 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006556 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006557 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006558 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006559 return 0;
6560 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006561 hlua_arg2lua(stream->hlua->T, arg_p);
6562 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006563 }
6564
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006565 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006566 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006567
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006568 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006569 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006570 }
6571
6572 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006573 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006574 /* finished. */
6575 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006576 /* If the stack is empty, the function fails. */
6577 if (lua_gettop(stream->hlua->T) <= 0)
6578 return 0;
6579
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006580 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006581 hlua_lua2smp(stream->hlua->T, -1, smp);
6582 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006583
6584 /* Set the end of execution flag. */
6585 smp->flags &= ~SMP_F_MAY_CHANGE;
6586 return 1;
6587
6588 /* yield. */
6589 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006590 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006591 return 0;
6592
6593 /* finished with error. */
6594 case HLUA_E_ERRMSG:
6595 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006596 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006597 fcn->name, lua_tostring(stream->hlua->T, -1));
6598 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006599 return 0;
6600
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006601 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006602 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6603 return 0;
6604
6605 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006606 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6607 return 0;
6608
6609 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006610 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6611 return 0;
6612
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006613 case HLUA_E_ERR:
6614 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006615 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006616 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006617
6618 default:
6619 return 0;
6620 }
6621}
6622
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006623/* This function is an LUA binding used for registering
6624 * "sample-conv" functions. It expects a converter name used
6625 * in the haproxy configuration file, and an LUA function.
6626 */
6627__LJMP static int hlua_register_converters(lua_State *L)
6628{
6629 struct sample_conv_kw_list *sck;
6630 const char *name;
6631 int ref;
6632 int len;
6633 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006634 struct sample_conv *sc;
6635 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006636
6637 MAY_LJMP(check_args(L, 2, "register_converters"));
6638
6639 /* First argument : converter name. */
6640 name = MAY_LJMP(luaL_checkstring(L, 1));
6641
6642 /* Second argument : lua function. */
6643 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6644
Thierry Fournierf67442e2020-11-28 20:41:07 +01006645 /* Check if the converter is already registered */
6646 trash = get_trash_chunk();
6647 chunk_printf(trash, "lua.%s", name);
6648 sc = find_sample_conv(trash->area, trash->data);
6649 if (sc != NULL) {
6650 ha_warning("Trying to register converter 'lua.%s' more than once. "
6651 "This will become a hard error in version 2.5.\n", name);
6652 }
6653
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006654 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006655 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006656 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006657 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006658 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006659 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006660 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006661
6662 /* Fill fcn. */
6663 fcn->name = strdup(name);
6664 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006665 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006666 fcn->function_ref = ref;
6667
6668 /* List head */
6669 sck->list.n = sck->list.p = NULL;
6670
6671 /* converter keyword. */
6672 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006673 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006674 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006675 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006676
6677 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6678 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006679 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 +01006680 sck->kw[0].val_args = NULL;
6681 sck->kw[0].in_type = SMP_T_STR;
6682 sck->kw[0].out_type = SMP_T_STR;
6683 sck->kw[0].private = fcn;
6684
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006685 /* Register this new converter */
6686 sample_register_convs(sck);
6687
6688 return 0;
6689}
6690
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006691/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006692 * "sample-fetch" functions. It expects a converter name used
6693 * in the haproxy configuration file, and an LUA function.
6694 */
6695__LJMP static int hlua_register_fetches(lua_State *L)
6696{
6697 const char *name;
6698 int ref;
6699 int len;
6700 struct sample_fetch_kw_list *sfk;
6701 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006702 struct sample_fetch *sf;
6703 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006704
6705 MAY_LJMP(check_args(L, 2, "register_fetches"));
6706
6707 /* First argument : sample-fetch name. */
6708 name = MAY_LJMP(luaL_checkstring(L, 1));
6709
6710 /* Second argument : lua function. */
6711 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6712
Thierry Fournierf67442e2020-11-28 20:41:07 +01006713 /* Check if the sample-fetch is already registered */
6714 trash = get_trash_chunk();
6715 chunk_printf(trash, "lua.%s", name);
6716 sf = find_sample_fetch(trash->area, trash->data);
6717 if (sf != NULL) {
6718 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6719 "This will become a hard error in version 2.5.\n", name);
6720 }
6721
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006722 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006723 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006724 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006725 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006726 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006727 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006728 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006729
6730 /* Fill fcn. */
6731 fcn->name = strdup(name);
6732 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006733 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006734 fcn->function_ref = ref;
6735
6736 /* List head */
6737 sfk->list.n = sfk->list.p = NULL;
6738
6739 /* sample-fetch keyword. */
6740 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006741 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006742 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006743 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006744
6745 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6746 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006747 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 +01006748 sfk->kw[0].val_args = NULL;
6749 sfk->kw[0].out_type = SMP_T_STR;
6750 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6751 sfk->kw[0].val = 0;
6752 sfk->kw[0].private = fcn;
6753
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006754 /* Register this new fetch. */
6755 sample_register_fetches(sfk);
6756
6757 return 0;
6758}
6759
Christopher Faulet501465d2020-02-26 14:54:16 +01006760/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006761 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006762__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006763{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006764 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006765 unsigned int delay;
6766 unsigned int wakeup_ms;
6767
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006768 /* Get hlua struct, or NULL if we execute from main lua state */
6769 hlua = hlua_gethlua(L);
6770 if (!hlua) {
6771 return 0;
6772 }
6773
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006774 MAY_LJMP(check_args(L, 1, "wake_time"));
6775
6776 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6777 wakeup_ms = tick_add(now_ms, delay);
6778 hlua->wake_time = wakeup_ms;
6779 return 0;
6780}
6781
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006782/* This function is a wrapper to execute each LUA function declared as an action
6783 * wrapper during the initialisation period. This function may return any
6784 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6785 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6786 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006787 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006788static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006789 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006790{
6791 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006792 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006793 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006794 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006795
6796 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006797 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6798 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6799 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6800 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006801 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006802 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006803 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006804 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006805
Willy Tarreau87b09662015-04-03 00:22:06 +02006806 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006807 * Lua context can be not initialized. This behavior
6808 * permits to save performances because a systematic
6809 * Lua initialization cause 5% performances loss.
6810 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006811 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006812 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006813 if (!s->hlua) {
6814 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006815 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006816 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006817 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006818 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006819 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006820 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006821 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006822 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006823 }
6824
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006825 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006826 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006827
6828 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006829 if (!SET_SAFE_LJMP(s->hlua->T)) {
6830 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6831 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006832 else
6833 error = "critical error";
6834 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006835 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006836 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006837 }
6838
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006839 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006840 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006841 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006842 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006843 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006844 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006845 }
6846
6847 /* Restore the function in the stack. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01006848 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn->function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006849
Willy Tarreau87b09662015-04-03 00:22:06 +02006850 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006851 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006852 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006853 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006854 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006855 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006856 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006857 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006858
6859 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006860 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006861 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006862 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006863 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006864 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006865 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006866 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006867 lua_pushstring(s->hlua->T, *arg);
6868 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006869 }
6870
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006871 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006872 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006873
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006874 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006875 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006876 }
6877
6878 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006879 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006880 /* finished. */
6881 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006882 /* Catch the return value */
6883 if (lua_gettop(s->hlua->T) > 0)
6884 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006885
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006886 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006887 if (act_ret == ACT_RET_YIELD) {
6888 if (flags & ACT_OPT_FINAL)
6889 goto err_yield;
6890
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006891 if (dir == SMP_OPT_DIR_REQ)
6892 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6893 s->hlua->wake_time);
6894 else
6895 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6896 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006897 }
6898 goto end;
6899
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006900 /* yield. */
6901 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006902 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006903 if (dir == SMP_OPT_DIR_REQ)
6904 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6905 s->hlua->wake_time);
6906 else
6907 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6908 s->hlua->wake_time);
6909
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006910 /* Some actions can be wake up when a "write" event
6911 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006912 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006913 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006914 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006915 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006916 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006917 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006918 act_ret = ACT_RET_YIELD;
6919 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006920
6921 /* finished with error. */
6922 case HLUA_E_ERRMSG:
6923 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006924 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006925 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006926 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006927 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006928
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006929 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01006930 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006931 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006932
6933 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01006934 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006935 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006936
6937 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02006938 err_yield:
6939 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006940 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006941 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006942 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006943
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006944 case HLUA_E_ERR:
6945 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006946 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006947 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006948
6949 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006950 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006951 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006952
6953 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02006954 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006955 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006956 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006957}
6958
Olivier Houchard9f6af332018-05-25 14:04:04 +02006959struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006960{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006961 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006962
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006963 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006964 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006965 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006966}
6967
6968static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6969{
6970 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006971 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006972 struct task *task;
6973 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006974 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006975
Willy Tarreaubafbe012017-11-24 17:34:44 +01006976 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006977 if (!hlua) {
6978 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006979 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006980 return 0;
6981 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006982 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006983 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006984 ctx->ctx.hlua_apptcp.flags = 0;
6985
6986 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006987 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006988 if (!task) {
6989 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006990 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006991 return 0;
6992 }
6993 task->nice = 0;
6994 task->context = ctx;
6995 task->process = hlua_applet_wakeup;
6996 ctx->ctx.hlua_apptcp.task = task;
6997
6998 /* In the execution wrappers linked with a stream, the
6999 * Lua context can be not initialized. This behavior
7000 * permits to save performances because a systematic
7001 * Lua initialization cause 5% performances loss.
7002 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007003 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007004 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007005 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007006 return 0;
7007 }
7008
7009 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007010 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007011
7012 /* The following Lua calls can fail. */
7013 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007014 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7015 error = lua_tostring(hlua->T, -1);
7016 else
7017 error = "critical error";
7018 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007019 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007020 return 0;
7021 }
7022
7023 /* Check stack available size. */
7024 if (!lua_checkstack(hlua->T, 1)) {
7025 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007026 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007027 RESET_SAFE_LJMP(hlua->T);
7028 return 0;
7029 }
7030
7031 /* Restore the function in the stack. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007032 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007033
7034 /* Create and and push object stream in the stack. */
7035 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7036 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007037 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007038 RESET_SAFE_LJMP(hlua->T);
7039 return 0;
7040 }
7041 hlua->nargs = 1;
7042
7043 /* push keywords in the stack. */
7044 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7045 if (!lua_checkstack(hlua->T, 1)) {
7046 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007047 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007048 RESET_SAFE_LJMP(hlua->T);
7049 return 0;
7050 }
7051 lua_pushstring(hlua->T, *arg);
7052 hlua->nargs++;
7053 }
7054
7055 RESET_SAFE_LJMP(hlua->T);
7056
7057 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007058 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007059 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007060
7061 return 1;
7062}
7063
Willy Tarreau60409db2019-08-21 14:14:50 +02007064void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007065{
7066 struct stream_interface *si = ctx->owner;
7067 struct stream *strm = si_strm(si);
7068 struct channel *res = si_ic(si);
7069 struct act_rule *rule = ctx->rule;
7070 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007071 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007072
7073 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007074 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7075 /* eat the whole request */
7076 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007077 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007078 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007079
7080 /* If the stream is disconnect or closed, ldo nothing. */
7081 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7082 return;
7083
7084 /* Execute the function. */
7085 switch (hlua_ctx_resume(hlua, 1)) {
7086 /* finished. */
7087 case HLUA_E_OK:
7088 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7089
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007090 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007091 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007092 res->flags |= CF_READ_NULL;
7093 si_shutr(si);
7094 return;
7095
7096 /* yield. */
7097 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007098 if (hlua->wake_time != TICK_ETERNITY)
7099 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007100 return;
7101
7102 /* finished with error. */
7103 case HLUA_E_ERRMSG:
7104 /* Display log. */
7105 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007106 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007107 lua_pop(hlua->T, 1);
7108 goto error;
7109
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007110 case HLUA_E_ETMOUT:
7111 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007112 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007113 goto error;
7114
7115 case HLUA_E_NOMEM:
7116 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007117 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007118 goto error;
7119
7120 case HLUA_E_YIELD: /* unexpected */
7121 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007122 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007123 goto error;
7124
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007125 case HLUA_E_ERR:
7126 /* Display log. */
7127 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007128 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007129 goto error;
7130
7131 default:
7132 goto error;
7133 }
7134
7135error:
7136
7137 /* For all other cases, just close the stream. */
7138 si_shutw(si);
7139 si_shutr(si);
7140 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7141}
7142
7143static void hlua_applet_tcp_release(struct appctx *ctx)
7144{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007145 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007146 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007147 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007148 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007149}
7150
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007151/* The function returns 1 if the initialisation is complete, 0 if
7152 * an errors occurs and -1 if more data are required for initializing
7153 * the applet.
7154 */
7155static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7156{
7157 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007158 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007159 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007160 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007161 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007162 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007163
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007164 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007165 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007166 if (!hlua) {
7167 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007168 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007169 return 0;
7170 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007171 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007172 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007173 ctx->ctx.hlua_apphttp.left_bytes = -1;
7174 ctx->ctx.hlua_apphttp.flags = 0;
7175
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007176 if (txn->req.flags & HTTP_MSGF_VER_11)
7177 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7178
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007179 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007180 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007181 if (!task) {
7182 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007183 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007184 return 0;
7185 }
7186 task->nice = 0;
7187 task->context = ctx;
7188 task->process = hlua_applet_wakeup;
7189 ctx->ctx.hlua_apphttp.task = task;
7190
7191 /* In the execution wrappers linked with a stream, the
7192 * Lua context can be not initialized. This behavior
7193 * permits to save performances because a systematic
7194 * Lua initialization cause 5% performances loss.
7195 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007196 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007197 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007198 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007199 return 0;
7200 }
7201
7202 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007203 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007204
7205 /* The following Lua calls can fail. */
7206 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007207 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7208 error = lua_tostring(hlua->T, -1);
7209 else
7210 error = "critical error";
7211 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007212 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007213 return 0;
7214 }
7215
7216 /* Check stack available size. */
7217 if (!lua_checkstack(hlua->T, 1)) {
7218 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007219 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007220 RESET_SAFE_LJMP(hlua->T);
7221 return 0;
7222 }
7223
7224 /* Restore the function in the stack. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007225 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007226
7227 /* Create and and push object stream in the stack. */
7228 if (!hlua_applet_http_new(hlua->T, ctx)) {
7229 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007230 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007231 RESET_SAFE_LJMP(hlua->T);
7232 return 0;
7233 }
7234 hlua->nargs = 1;
7235
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007236 /* push keywords in the stack. */
7237 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7238 if (!lua_checkstack(hlua->T, 1)) {
7239 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007240 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007241 RESET_SAFE_LJMP(hlua->T);
7242 return 0;
7243 }
7244 lua_pushstring(hlua->T, *arg);
7245 hlua->nargs++;
7246 }
7247
7248 RESET_SAFE_LJMP(hlua->T);
7249
7250 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007251 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007252
7253 return 1;
7254}
7255
Willy Tarreau60409db2019-08-21 14:14:50 +02007256void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007257{
7258 struct stream_interface *si = ctx->owner;
7259 struct stream *strm = si_strm(si);
7260 struct channel *req = si_oc(si);
7261 struct channel *res = si_ic(si);
7262 struct act_rule *rule = ctx->rule;
7263 struct proxy *px = strm->be;
7264 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7265 struct htx *req_htx, *res_htx;
7266
7267 res_htx = htx_from_buf(&res->buf);
7268
7269 /* If the stream is disconnect or closed, ldo nothing. */
7270 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7271 goto out;
7272
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007273 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007274 if (!b_size(&res->buf)) {
7275 si_rx_room_blk(si);
7276 goto out;
7277 }
7278 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007279 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007280 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7281
7282 /* Set the currently running flag. */
7283 if (!HLUA_IS_RUNNING(hlua) &&
7284 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7285 struct htx_blk *blk;
7286 size_t count = co_data(req);
7287
7288 if (!count) {
7289 si_cant_get(si);
7290 goto out;
7291 }
7292
7293 /* We need to flush the request header. This left the body for
7294 * the Lua.
7295 */
7296 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007297 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007298 while (count && blk) {
7299 enum htx_blk_type type = htx_get_blk_type(blk);
7300 uint32_t sz = htx_get_blksz(blk);
7301
7302 if (sz > count) {
7303 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007304 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007305 goto out;
7306 }
7307
7308 count -= sz;
7309 co_set_data(req, co_data(req) - sz);
7310 blk = htx_remove_blk(req_htx, blk);
7311
7312 if (type == HTX_BLK_EOH)
7313 break;
7314 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007315 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007316 }
7317
7318 /* Executes The applet if it is not done. */
7319 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7320
7321 /* Execute the function. */
7322 switch (hlua_ctx_resume(hlua, 1)) {
7323 /* finished. */
7324 case HLUA_E_OK:
7325 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7326 break;
7327
7328 /* yield. */
7329 case HLUA_E_AGAIN:
7330 if (hlua->wake_time != TICK_ETERNITY)
7331 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007332 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007333
7334 /* finished with error. */
7335 case HLUA_E_ERRMSG:
7336 /* Display log. */
7337 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007338 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007339 lua_pop(hlua->T, 1);
7340 goto error;
7341
7342 case HLUA_E_ETMOUT:
7343 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007344 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007345 goto error;
7346
7347 case HLUA_E_NOMEM:
7348 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007349 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007350 goto error;
7351
7352 case HLUA_E_YIELD: /* unexpected */
7353 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007354 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007355 goto error;
7356
7357 case HLUA_E_ERR:
7358 /* Display log. */
7359 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007360 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007361 goto error;
7362
7363 default:
7364 goto error;
7365 }
7366 }
7367
7368 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007369 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7370 goto done;
7371
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007372 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7373 goto error;
7374
Christopher Faulet54b5e212019-06-04 10:08:28 +02007375 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02007376 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007377 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7378 si_rx_room_blk(si);
7379 goto out;
7380 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007381 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007382 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7383 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007384 }
7385
7386 done:
7387 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007388 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007389 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007390 si_shutr(si);
7391 }
7392
7393 /* eat the whole request */
7394 if (co_data(req)) {
7395 req_htx = htx_from_buf(&req->buf);
7396 co_htx_skip(req, req_htx, co_data(req));
7397 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007398 }
7399 }
7400
7401 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007402 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007403 return;
7404
7405 error:
7406
7407 /* If we are in HTTP mode, and we are not send any
7408 * data, return a 500 server error in best effort:
7409 * if there is no room available in the buffer,
7410 * just close the connection.
7411 */
7412 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007413 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007414
7415 channel_erase(res);
7416 res->buf.data = b_data(err);
7417 memcpy(res->buf.area, b_head(err), b_data(err));
7418 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007419 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007420 }
7421 if (!(strm->flags & SF_ERR_MASK))
7422 strm->flags |= SF_ERR_RESOURCE;
7423 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7424 goto done;
7425}
7426
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007427static void hlua_applet_http_release(struct appctx *ctx)
7428{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007429 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007430 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007431 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007432 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007433}
7434
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007435/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007436 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007437 *
7438 * This function can fail with an abort() due to an Lua critical error.
7439 * We are in the configuration parsing process of HAProxy, this abort() is
7440 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007441 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007442static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7443 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007444{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007445 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007446 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007447
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007448 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007449 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007450 if (!rule->arg.hlua_rule) {
7451 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007452 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007453 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007454
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007455 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007456 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7457 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007458 if (!rule->arg.hlua_rule->args) {
7459 memprintf(err, "out of memory error");
7460 return ACT_RET_PRS_ERR;
7461 }
7462
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007463 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007464 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007465
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007466 /* Expect some arguments */
7467 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007468 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007469 memprintf(err, "expect %d arguments", fcn->nargs);
7470 return ACT_RET_PRS_ERR;
7471 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007472 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007473 if (!rule->arg.hlua_rule->args[i]) {
7474 memprintf(err, "out of memory error");
7475 return ACT_RET_PRS_ERR;
7476 }
7477 (*cur_arg)++;
7478 }
7479 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007480
Thierry FOURNIER42148732015-09-02 17:17:33 +02007481 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007482 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007483 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007484}
7485
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007486static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7487 struct act_rule *rule, char **err)
7488{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007489 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007490
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007491 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007492 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007493 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007494 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007495 * the call of this analyzer.
7496 */
7497 if (rule->from != ACT_F_HTTP_REQ) {
7498 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7499 return ACT_RET_PRS_ERR;
7500 }
7501
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007502 /* Memory for the rule. */
7503 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7504 if (!rule->arg.hlua_rule) {
7505 memprintf(err, "out of memory error");
7506 return ACT_RET_PRS_ERR;
7507 }
7508
7509 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007510 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007511
7512 /* TODO: later accept arguments. */
7513 rule->arg.hlua_rule->args = NULL;
7514
7515 /* Add applet pointer in the rule. */
7516 rule->applet.obj_type = OBJ_TYPE_APPLET;
7517 rule->applet.name = fcn->name;
7518 rule->applet.init = hlua_applet_http_init;
7519 rule->applet.fct = hlua_applet_http_fct;
7520 rule->applet.release = hlua_applet_http_release;
7521 rule->applet.timeout = hlua_timeout_applet;
7522
7523 return ACT_RET_PRS_OK;
7524}
7525
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007526/* This function is an LUA binding used for registering
7527 * "sample-conv" functions. It expects a converter name used
7528 * in the haproxy configuration file, and an LUA function.
7529 */
7530__LJMP static int hlua_register_action(lua_State *L)
7531{
7532 struct action_kw_list *akl;
7533 const char *name;
7534 int ref;
7535 int len;
7536 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007537 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007538 struct buffer *trash;
7539 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007540
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007541 /* Initialise the number of expected arguments at 0. */
7542 nargs = 0;
7543
7544 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7545 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007546
7547 /* First argument : converter name. */
7548 name = MAY_LJMP(luaL_checkstring(L, 1));
7549
7550 /* Second argument : environment. */
7551 if (lua_type(L, 2) != LUA_TTABLE)
7552 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7553
7554 /* Third argument : lua function. */
7555 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7556
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007557 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007558 if (lua_gettop(L) >= 4)
7559 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7560
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007561 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007562 lua_pushnil(L);
7563 while (lua_next(L, 2) != 0) {
7564 if (lua_type(L, -1) != LUA_TSTRING)
7565 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7566
Thierry Fournierf67442e2020-11-28 20:41:07 +01007567 /* Check if action exists */
7568 trash = get_trash_chunk();
7569 chunk_printf(trash, "lua.%s", name);
7570 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7571 akw = tcp_req_cont_action(trash->area);
7572 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7573 akw = tcp_res_cont_action(trash->area);
7574 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7575 akw = action_http_req_custom(trash->area);
7576 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7577 akw = action_http_res_custom(trash->area);
7578 } else {
7579 akw = NULL;
7580 }
7581 if (akw != NULL) {
7582 ha_warning("Trying to register action 'lua.%s' more than once. "
7583 "This will become a hard error in version 2.5.\n", name);
7584 }
7585
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007586 /* Check required environment. Only accepted "http" or "tcp". */
7587 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007588 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007589 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007590 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007591 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007592 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007593 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007594
7595 /* Fill fcn. */
7596 fcn->name = strdup(name);
7597 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007598 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007599 fcn->function_ref = ref;
7600
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007601 /* Set the expected number od arguments. */
7602 fcn->nargs = nargs;
7603
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007604 /* List head */
7605 akl->list.n = akl->list.p = NULL;
7606
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007607 /* action keyword. */
7608 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007609 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007610 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007611 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007612
7613 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7614
7615 akl->kw[0].match_pfx = 0;
7616 akl->kw[0].private = fcn;
7617 akl->kw[0].parse = action_register_lua;
7618
7619 /* select the action registering point. */
7620 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7621 tcp_req_cont_keywords_register(akl);
7622 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7623 tcp_res_cont_keywords_register(akl);
7624 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7625 http_req_keywords_register(akl);
7626 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7627 http_res_keywords_register(akl);
7628 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007629 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007630 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7631 "are expected.", lua_tostring(L, -1)));
7632
7633 /* pop the environment string. */
7634 lua_pop(L, 1);
7635 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007636 return ACT_RET_PRS_OK;
7637}
7638
7639static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7640 struct act_rule *rule, char **err)
7641{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007642 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007643
Christopher Faulet280f85b2019-07-15 15:02:04 +02007644 if (px->mode == PR_MODE_HTTP) {
7645 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007646 return ACT_RET_PRS_ERR;
7647 }
7648
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007649 /* Memory for the rule. */
7650 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7651 if (!rule->arg.hlua_rule) {
7652 memprintf(err, "out of memory error");
7653 return ACT_RET_PRS_ERR;
7654 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007655
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007656 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007657 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007658
7659 /* TODO: later accept arguments. */
7660 rule->arg.hlua_rule->args = NULL;
7661
7662 /* Add applet pointer in the rule. */
7663 rule->applet.obj_type = OBJ_TYPE_APPLET;
7664 rule->applet.name = fcn->name;
7665 rule->applet.init = hlua_applet_tcp_init;
7666 rule->applet.fct = hlua_applet_tcp_fct;
7667 rule->applet.release = hlua_applet_tcp_release;
7668 rule->applet.timeout = hlua_timeout_applet;
7669
7670 return 0;
7671}
7672
7673/* This function is an LUA binding used for registering
7674 * "sample-conv" functions. It expects a converter name used
7675 * in the haproxy configuration file, and an LUA function.
7676 */
7677__LJMP static int hlua_register_service(lua_State *L)
7678{
7679 struct action_kw_list *akl;
7680 const char *name;
7681 const char *env;
7682 int ref;
7683 int len;
7684 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007685 struct buffer *trash;
7686 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007687
7688 MAY_LJMP(check_args(L, 3, "register_service"));
7689
7690 /* First argument : converter name. */
7691 name = MAY_LJMP(luaL_checkstring(L, 1));
7692
7693 /* Second argument : environment. */
7694 env = MAY_LJMP(luaL_checkstring(L, 2));
7695
7696 /* Third argument : lua function. */
7697 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7698
Thierry Fournierf67442e2020-11-28 20:41:07 +01007699 /* Check for service already registered */
7700 trash = get_trash_chunk();
7701 chunk_printf(trash, "lua.%s", name);
7702 akw = service_find(trash->area);
7703 if (akw != NULL) {
7704 ha_warning("Trying to register service 'lua.%s' more than once. "
7705 "This will become a hard error in version 2.5.\n", name);
7706 }
7707
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007708 /* Allocate and fill the sample fetch keyword struct. */
7709 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7710 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007711 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007712 fcn = calloc(1, sizeof(*fcn));
7713 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007714 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007715
7716 /* Fill fcn. */
7717 len = strlen("<lua.>") + strlen(name) + 1;
7718 fcn->name = calloc(1, len);
7719 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007720 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007721 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7722 fcn->function_ref = ref;
7723
7724 /* List head */
7725 akl->list.n = akl->list.p = NULL;
7726
7727 /* converter keyword. */
7728 len = strlen("lua.") + strlen(name) + 1;
7729 akl->kw[0].kw = calloc(1, len);
7730 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007731 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007732
7733 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7734
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007735 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007736 if (strcmp(env, "tcp") == 0)
7737 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007738 else if (strcmp(env, "http") == 0)
7739 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007740 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007741 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007742 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007743
7744 akl->kw[0].match_pfx = 0;
7745 akl->kw[0].private = fcn;
7746
7747 /* End of array. */
7748 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7749
7750 /* Register this new converter */
7751 service_keywords_register(akl);
7752
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007753 return 0;
7754}
7755
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007756/* This function initialises Lua cli handler. It copies the
7757 * arguments in the Lua stack and create channel IO objects.
7758 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007759static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007760{
7761 struct hlua *hlua;
7762 struct hlua_function *fcn;
7763 int i;
7764 const char *error;
7765
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007766 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007767 appctx->ctx.hlua_cli.fcn = private;
7768
Willy Tarreaubafbe012017-11-24 17:34:44 +01007769 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007770 if (!hlua) {
7771 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007772 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007773 }
7774 HLUA_INIT(hlua);
7775 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007776
7777 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007778 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007779 * applet_http. It is absolutely compatible.
7780 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007781 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007782 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007783 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007784 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007785 }
7786 appctx->ctx.hlua_cli.task->nice = 0;
7787 appctx->ctx.hlua_cli.task->context = appctx;
7788 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7789
7790 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007791 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007792 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007793 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007794 }
7795
7796 /* The following Lua calls can fail. */
7797 if (!SET_SAFE_LJMP(hlua->T)) {
7798 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7799 error = lua_tostring(hlua->T, -1);
7800 else
7801 error = "critical error";
7802 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7803 goto error;
7804 }
7805
7806 /* Check stack available size. */
7807 if (!lua_checkstack(hlua->T, 2)) {
7808 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7809 goto error;
7810 }
7811
7812 /* Restore the function in the stack. */
7813 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7814
7815 /* Once the arguments parsed, the CLI is like an AppletTCP,
7816 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007817 */
7818 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7819 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7820 goto error;
7821 }
7822 hlua->nargs = 1;
7823
7824 /* push keywords in the stack. */
7825 for (i = 0; *args[i]; i++) {
7826 /* Check stack available size. */
7827 if (!lua_checkstack(hlua->T, 1)) {
7828 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7829 goto error;
7830 }
7831 lua_pushstring(hlua->T, args[i]);
7832 hlua->nargs++;
7833 }
7834
7835 /* We must initialize the execution timeouts. */
7836 hlua->max_time = hlua_timeout_session;
7837
7838 /* At this point the execution is safe. */
7839 RESET_SAFE_LJMP(hlua->T);
7840
7841 /* It's ok */
7842 return 0;
7843
7844 /* It's not ok. */
7845error:
7846 RESET_SAFE_LJMP(hlua->T);
7847 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007848 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007849 return 1;
7850}
7851
7852static int hlua_cli_io_handler_fct(struct appctx *appctx)
7853{
7854 struct hlua *hlua;
7855 struct stream_interface *si;
7856 struct hlua_function *fcn;
7857
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007858 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007859 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007860 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007861
7862 /* If the stream is disconnect or closed, ldo nothing. */
7863 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7864 return 1;
7865
7866 /* Execute the function. */
7867 switch (hlua_ctx_resume(hlua, 1)) {
7868
7869 /* finished. */
7870 case HLUA_E_OK:
7871 return 1;
7872
7873 /* yield. */
7874 case HLUA_E_AGAIN:
7875 /* We want write. */
7876 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007877 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007878 /* Set the timeout. */
7879 if (hlua->wake_time != TICK_ETERNITY)
7880 task_schedule(hlua->task, hlua->wake_time);
7881 return 0;
7882
7883 /* finished with error. */
7884 case HLUA_E_ERRMSG:
7885 /* Display log. */
7886 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7887 fcn->name, lua_tostring(hlua->T, -1));
7888 lua_pop(hlua->T, 1);
7889 return 1;
7890
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007891 case HLUA_E_ETMOUT:
7892 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7893 fcn->name);
7894 return 1;
7895
7896 case HLUA_E_NOMEM:
7897 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7898 fcn->name);
7899 return 1;
7900
7901 case HLUA_E_YIELD: /* unexpected */
7902 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7903 fcn->name);
7904 return 1;
7905
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007906 case HLUA_E_ERR:
7907 /* Display log. */
7908 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7909 fcn->name);
7910 return 1;
7911
7912 default:
7913 return 1;
7914 }
7915
7916 return 1;
7917}
7918
7919static void hlua_cli_io_release_fct(struct appctx *appctx)
7920{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007921 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007922 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007923}
7924
7925/* This function is an LUA binding used for registering
7926 * new keywords in the cli. It expects a list of keywords
7927 * which are the "path". It is limited to 5 keywords. A
7928 * description of the command, a function to be executed
7929 * for the parsing and a function for io handlers.
7930 */
7931__LJMP static int hlua_register_cli(lua_State *L)
7932{
7933 struct cli_kw_list *cli_kws;
7934 const char *message;
7935 int ref_io;
7936 int len;
7937 struct hlua_function *fcn;
7938 int index;
7939 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007940 struct buffer *trash;
7941 const char *kw[5];
7942 struct cli_kw *cli_kw;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007943
7944 MAY_LJMP(check_args(L, 3, "register_cli"));
7945
7946 /* First argument : an array of maximum 5 keywords. */
7947 if (!lua_istable(L, 1))
7948 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7949
7950 /* Second argument : string with contextual message. */
7951 message = MAY_LJMP(luaL_checkstring(L, 2));
7952
7953 /* Third and fourth argument : lua function. */
7954 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7955
Thierry Fournierf67442e2020-11-28 20:41:07 +01007956 /* Check for CLI service already registered */
7957 trash = get_trash_chunk();
7958 index = 0;
7959 lua_pushnil(L);
7960 memset(kw, 0, sizeof(kw));
7961 while (lua_next(L, 1) != 0) {
7962 if (index >= CLI_PREFIX_KW_NB)
7963 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7964 if (lua_type(L, -1) != LUA_TSTRING)
7965 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7966 kw[index] = lua_tostring(L, -1);
7967 if (index == 0)
7968 chunk_printf(trash, "%s", kw[index]);
7969 else
7970 chunk_appendf(trash, " %s", kw[index]);
7971 index++;
7972 lua_pop(L, 1);
7973 }
7974 cli_kw = cli_find_kw_exact((char **)kw);
7975 if (cli_kw != NULL) {
7976 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
7977 "This will become a hard error in version 2.5.\n", trash->area);
7978 }
7979
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007980 /* Allocate and fill the sample fetch keyword struct. */
7981 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7982 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007983 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007984 fcn = calloc(1, sizeof(*fcn));
7985 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007986 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007987
7988 /* Fill path. */
7989 index = 0;
7990 lua_pushnil(L);
7991 while(lua_next(L, 1) != 0) {
7992 if (index >= 5)
7993 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7994 if (lua_type(L, -1) != LUA_TSTRING)
7995 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7996 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7997 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007998 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007999 index++;
8000 lua_pop(L, 1);
8001 }
8002
8003 /* Copy help message. */
8004 cli_kws->kw[0].usage = strdup(message);
8005 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008006 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008007
8008 /* Fill fcn io handler. */
8009 len = strlen("<lua.cli>") + 1;
8010 for (i = 0; i < index; i++)
8011 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8012 fcn->name = calloc(1, len);
8013 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008014 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008015 strncat((char *)fcn->name, "<lua.cli", len);
8016 for (i = 0; i < index; i++) {
8017 strncat((char *)fcn->name, ".", len);
8018 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8019 }
8020 strncat((char *)fcn->name, ">", len);
8021 fcn->function_ref = ref_io;
8022
8023 /* Fill last entries. */
8024 cli_kws->kw[0].private = fcn;
8025 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8026 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8027 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8028
8029 /* Register this new converter */
8030 cli_register_kw(cli_kws);
8031
8032 return 0;
8033}
8034
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008035static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
8036 struct proxy *defpx, const char *file, int line,
8037 char **err, unsigned int *timeout)
8038{
8039 const char *error;
8040
8041 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008042 if (error == PARSE_TIME_OVER) {
8043 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8044 args[1], args[0]);
8045 return -1;
8046 }
8047 else if (error == PARSE_TIME_UNDER) {
8048 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8049 args[1], args[0]);
8050 return -1;
8051 }
8052 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008053 memprintf(err, "%s: invalid timeout", args[0]);
8054 return -1;
8055 }
8056 return 0;
8057}
8058
8059static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
8060 struct proxy *defpx, const char *file, int line,
8061 char **err)
8062{
8063 return hlua_read_timeout(args, section_type, curpx, defpx,
8064 file, line, err, &hlua_timeout_session);
8065}
8066
8067static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8068 struct proxy *defpx, const char *file, int line,
8069 char **err)
8070{
8071 return hlua_read_timeout(args, section_type, curpx, defpx,
8072 file, line, err, &hlua_timeout_task);
8073}
8074
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008075static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8076 struct proxy *defpx, const char *file, int line,
8077 char **err)
8078{
8079 return hlua_read_timeout(args, section_type, curpx, defpx,
8080 file, line, err, &hlua_timeout_applet);
8081}
8082
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008083static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
8084 struct proxy *defpx, const char *file, int line,
8085 char **err)
8086{
8087 char *error;
8088
8089 hlua_nb_instruction = strtoll(args[1], &error, 10);
8090 if (*error != '\0') {
8091 memprintf(err, "%s: invalid number", args[0]);
8092 return -1;
8093 }
8094 return 0;
8095}
8096
Willy Tarreau32f61e22015-03-18 17:54:59 +01008097static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8098 struct proxy *defpx, const char *file, int line,
8099 char **err)
8100{
8101 char *error;
8102
8103 if (*(args[1]) == 0) {
8104 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8105 return -1;
8106 }
8107 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8108 if (*error != '\0') {
8109 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8110 return -1;
8111 }
8112 return 0;
8113}
8114
8115
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008116/* This function is called by the main configuration key "lua-load". It loads and
8117 * execute an lua file during the parsing of the HAProxy configuration file. It is
8118 * the main lua entry point.
8119 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008120 * This function runs with the HAProxy keywords API. It returns -1 if an error
8121 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008122 *
8123 * In some error case, LUA set an error message in top of the stack. This function
8124 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008125 *
8126 * This function can fail with an abort() due to an Lua critical error.
8127 * We are in the configuration parsing process of HAProxy, this abort() is
8128 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008129 */
8130static int hlua_load(char **args, int section_type, struct proxy *curpx,
8131 struct proxy *defpx, const char *file, int line,
8132 char **err)
8133{
8134 int error;
8135
Thierry Fournier77a88942020-11-29 01:06:24 +01008136 if (*(args[1]) == 0) {
8137 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8138 return -1;
8139 }
8140
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008141 /* Just load and compile the file. */
8142 error = luaL_loadfile(gL.T, args[1]);
8143 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008144 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008145 lua_pop(gL.T, 1);
8146 return -1;
8147 }
8148
8149 /* If no syntax error where detected, execute the code. */
8150 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
8151 switch (error) {
8152 case LUA_OK:
8153 break;
8154 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008155 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008156 lua_pop(gL.T, 1);
8157 return -1;
8158 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008159 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008160 return -1;
8161 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008162 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008163 lua_pop(gL.T, 1);
8164 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008165#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008166 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008167 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008168 lua_pop(gL.T, 1);
8169 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008170#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008171 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05008172 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008173 lua_pop(gL.T, 1);
8174 return -1;
8175 }
8176
8177 return 0;
8178}
8179
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008180/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8181 * in the given <ctx>.
8182 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008183static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008184{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008185 lua_getglobal(L, "package"); /* push package variable */
8186 lua_pushstring(L, path); /* push given path */
8187 lua_pushstring(L, ";"); /* push semicolon */
8188 lua_getfield(L, -3, type); /* push old path */
8189 lua_concat(L, 3); /* concatenate to new path */
8190 lua_setfield(L, -2, type); /* store new path */
8191 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008192
8193 return 0;
8194}
8195
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008196static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
8197 struct proxy *defpx, const char *file, int line,
8198 char **err)
8199{
8200 char *path;
8201 char *type = "path";
8202 if (too_many_args(2, args, err, NULL)) {
8203 return -1;
8204 }
8205
8206 if (!(*args[1])) {
8207 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
8208 return -1;
8209 }
8210 path = args[1];
8211
8212 if (*args[2]) {
8213 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8214 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
8215 return -1;
8216 }
8217 type = args[2];
8218 }
8219
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008220 return hlua_prepend_path(gL.T, type, path);
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008221}
8222
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008223/* configuration keywords declaration */
8224static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008225 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008226 { CFG_GLOBAL, "lua-load", hlua_load },
8227 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8228 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008229 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008230 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008231 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008232 { 0, NULL, NULL },
8233}};
8234
Willy Tarreau0108d902018-11-25 19:14:37 +01008235INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8236
Christopher Fauletafd8f102018-11-08 11:34:21 +01008237
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008238/* This function can fail with an abort() due to an Lua critical error.
8239 * We are in the initialisation process of HAProxy, this abort() is
8240 * tolerated.
8241 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008242int hlua_post_init()
8243{
8244 struct hlua_init_function *init;
8245 const char *msg;
8246 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008247 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008248 const char *kind;
8249 const char *trace;
8250 int return_status = 1;
8251#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8252 int nres;
8253#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008254
Willy Tarreaucdb53462020-12-02 12:12:00 +01008255 /* disable memory limit checks if limit is not set */
8256 if (!hlua_global_allocator.limit)
8257 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8258
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008259 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008260 if (!SET_SAFE_LJMP(gL.T)) {
8261 if (lua_type(gL.T, -1) == LUA_TSTRING)
8262 error = lua_tostring(gL.T, -1);
8263 else
8264 error = "critical error";
8265 fprintf(stderr, "Lua post-init: %s.\n", error);
8266 exit(1);
8267 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008268
8269#if USE_OPENSSL
8270 /* Initialize SSL server. */
8271 if (socket_ssl.xprt->prepare_srv) {
8272 int saved_used_backed = global.ssl_used_backend;
8273 // don't affect maxconn automatic computation
8274 socket_ssl.xprt->prepare_srv(&socket_ssl);
8275 global.ssl_used_backend = saved_used_backed;
8276 }
8277#endif
8278
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008279 hlua_fcn_post_init(gL.T);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008280
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008281 list_for_each_entry(init, &hlua_init_functions, l) {
8282 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008283
8284#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8285 ret = lua_resume(gL.T, gL.T, 0, &nres);
8286#else
8287 ret = lua_resume(gL.T, gL.T, 0);
8288#endif
8289 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008290 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008291
8292 case LUA_OK:
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008293 lua_pop(gL.T, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008294 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008295
8296 case LUA_ERRERR:
8297 kind = "message handler error";
8298 /* Fall thru */
8299 case LUA_ERRRUN:
8300 if (!kind)
8301 kind = "runtime error";
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008302 msg = lua_tostring(gL.T, -1);
Thierry Fournier670db242020-11-28 10:49:59 +01008303 lua_settop(gL.T, 0); /* Empty the stack. */
8304 lua_pop(gL.T, 1);
8305 trace = hlua_traceback(gL.T);
8306 if (msg)
8307 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8308 else
8309 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8310 return_status = 0;
8311 break;
8312
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008313 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008314 /* Unknown error */
8315 kind = "Unknown error";
8316 /* Fall thru */
8317 case LUA_YIELD:
8318 /* yield is not configured at this step, this state doesn't happen */
8319 if (!kind)
8320 kind = "yield not allowed";
8321 /* Fall thru */
8322 case LUA_ERRMEM:
8323 if (!kind)
8324 kind = "out of memory error";
8325 lua_settop(gL.T, 0);
8326 lua_pop(gL.T, 1);
8327 trace = hlua_traceback(gL.T);
8328 ha_alert("Lua init: %s: %s\n", kind, trace);
8329 return_status = 0;
8330 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008331 }
Thierry Fournier670db242020-11-28 10:49:59 +01008332 if (!return_status)
8333 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008334 }
Thierry Fournier670db242020-11-28 10:49:59 +01008335 RESET_SAFE_LJMP(gL.T);
8336 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008337}
8338
Willy Tarreau32f61e22015-03-18 17:54:59 +01008339/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8340 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8341 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008342 * allocation. <nsize> is the requested new size. A new allocation is
8343 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008344 * zero. This one verifies that the limits are respected but is optimized
8345 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008346 */
8347static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8348{
8349 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008350 size_t limit, old, new;
8351
8352 /* a limit of ~0 means unlimited and boot complete, so there's no need
8353 * for accounting anymore.
8354 */
8355 if (likely(~zone->limit == 0))
8356 return realloc(ptr, nsize);
Willy Tarreau32f61e22015-03-18 17:54:59 +01008357
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008358 if (!ptr)
8359 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008360
Willy Tarreaucdb53462020-12-02 12:12:00 +01008361 /* enforce strict limits across all threads */
8362 limit = zone->limit;
8363 old = _HA_ATOMIC_LOAD(&zone->allocated);
8364 do {
8365 new = old + nsize - osize;
8366 if (unlikely(nsize && limit && new > limit))
8367 return NULL;
8368 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008369
8370 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008371
8372 if (unlikely(!ptr && nsize)) // failed
8373 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8374
8375 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008376 return ptr;
8377}
8378
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008379/* Ithis function can fail with an abort() due to an Lua critical error.
8380 * We are in the initialisation process of HAProxy, this abort() is
8381 * tolerated.
8382 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008383lua_State *hlua_init_state(void)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008384{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008385 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008386 int idx;
8387 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008388 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008389 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008390 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008391 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008392 lua_State *L;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008393#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008394 struct srv_kw *kw;
8395 int tmp_error;
8396 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008397 char *args[] = { /* SSL client configuration. */
8398 "ssl",
8399 "verify",
8400 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008401 NULL
8402 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008403#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008404
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008405 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008406 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008407
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008408 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008409 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008410 *context = NULL;
8411
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008412 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008413 * the Lua function can fail with an abort. We are in the initialisation
8414 * process of HAProxy, this abort() is tolerated.
8415 */
8416
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008417 /* Set safe environment for the initialisation. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008418 if (!SET_SAFE_LJMP(L)) {
8419 if (lua_type(L, -1) == LUA_TSTRING)
8420 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008421 else
8422 error_msg = "critical error";
8423 fprintf(stderr, "Lua init: %s.\n", error_msg);
8424 exit(1);
8425 }
8426
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008427
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008428 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008429 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008430#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8431#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8432#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008433 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008434#endif
8435#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008436 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008437#endif
8438#undef HLUA_PREPEND_PATH_TOSTRING
8439#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008440
8441 /*
8442 *
8443 * Create "core" object.
8444 *
8445 */
8446
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008447 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008448 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008449
8450 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008451 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008452 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008453
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008454 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008455 hlua_class_function(L, "register_init", hlua_register_init);
8456 hlua_class_function(L, "register_task", hlua_register_task);
8457 hlua_class_function(L, "register_fetches", hlua_register_fetches);
8458 hlua_class_function(L, "register_converters", hlua_register_converters);
8459 hlua_class_function(L, "register_action", hlua_register_action);
8460 hlua_class_function(L, "register_service", hlua_register_service);
8461 hlua_class_function(L, "register_cli", hlua_register_cli);
8462 hlua_class_function(L, "yield", hlua_yield);
8463 hlua_class_function(L, "set_nice", hlua_set_nice);
8464 hlua_class_function(L, "sleep", hlua_sleep);
8465 hlua_class_function(L, "msleep", hlua_msleep);
8466 hlua_class_function(L, "add_acl", hlua_add_acl);
8467 hlua_class_function(L, "del_acl", hlua_del_acl);
8468 hlua_class_function(L, "set_map", hlua_set_map);
8469 hlua_class_function(L, "del_map", hlua_del_map);
8470 hlua_class_function(L, "tcp", hlua_socket_new);
8471 hlua_class_function(L, "log", hlua_log);
8472 hlua_class_function(L, "Debug", hlua_log_debug);
8473 hlua_class_function(L, "Info", hlua_log_info);
8474 hlua_class_function(L, "Warning", hlua_log_warning);
8475 hlua_class_function(L, "Alert", hlua_log_alert);
8476 hlua_class_function(L, "done", hlua_done);
8477 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008478
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008479 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008480
8481 /*
8482 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008483 * Create "act" object.
8484 *
8485 */
8486
8487 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008488 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008489
8490 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008491 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
8492 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
8493 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
8494 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
8495 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
8496 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
8497 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
8498 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008499
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008500 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008501
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008502 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008503
8504 /*
8505 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008506 * Register class Map
8507 *
8508 */
8509
8510 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008511 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008512
8513 /* register pattern types. */
8514 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008515 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008516 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008517 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008518 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008519 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008520
8521 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008522 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008523
8524 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008525 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008526
Ilya Shipitsind4259502020-04-08 01:07:56 +05008527 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008528 lua_pushstring(L, "__index");
8529 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008530
8531 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008532 hlua_class_function(L, "lookup", hlua_map_lookup);
8533 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008534
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008535 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008536
Thierry Fournier45e78d72016-02-19 18:34:46 +01008537 /* Register previous table in the registry with reference and named entry.
8538 * The function hlua_register_metatable() pops the stack, so we
8539 * previously create a copy of the table.
8540 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008541 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
8542 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008543
8544 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008545 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008546
8547 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008548 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008549
8550 /*
8551 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008552 * Register class Channel
8553 *
8554 */
8555
8556 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008557 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008558
Ilya Shipitsind4259502020-04-08 01:07:56 +05008559 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008560 lua_pushstring(L, "__index");
8561 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008562
8563 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008564 hlua_class_function(L, "get", hlua_channel_get);
8565 hlua_class_function(L, "dup", hlua_channel_dup);
8566 hlua_class_function(L, "getline", hlua_channel_getline);
8567 hlua_class_function(L, "set", hlua_channel_set);
8568 hlua_class_function(L, "append", hlua_channel_append);
8569 hlua_class_function(L, "send", hlua_channel_send);
8570 hlua_class_function(L, "forward", hlua_channel_forward);
8571 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
8572 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
8573 hlua_class_function(L, "is_full", hlua_channel_is_full);
8574 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008575
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008576 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008577
8578 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008579 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008580
8581 /*
8582 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008583 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008584 *
8585 */
8586
8587 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008588 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008589
Ilya Shipitsind4259502020-04-08 01:07:56 +05008590 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008591 lua_pushstring(L, "__index");
8592 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008593
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008594 /* Browse existing fetches and create the associated
8595 * object method.
8596 */
8597 sf = NULL;
8598 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008599 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8600 * by an underscore.
8601 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008602 strncpy(trash.area, sf->kw, trash.size);
8603 trash.area[trash.size - 1] = '\0';
8604 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008605 if (*p == '.' || *p == '-' || *p == '+')
8606 *p = '_';
8607
8608 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008609 lua_pushstring(L, trash.area);
8610 lua_pushlightuserdata(L, sf);
8611 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
8612 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008613 }
8614
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008615 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008616
8617 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008618 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008619
8620 /*
8621 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008622 * Register class Converters
8623 *
8624 */
8625
8626 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008627 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008628
8629 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008630 lua_pushstring(L, "__index");
8631 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008632
8633 /* Browse existing converters and create the associated
8634 * object method.
8635 */
8636 sc = NULL;
8637 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008638 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8639 * by an underscore.
8640 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008641 strncpy(trash.area, sc->kw, trash.size);
8642 trash.area[trash.size - 1] = '\0';
8643 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008644 if (*p == '.' || *p == '-' || *p == '+')
8645 *p = '_';
8646
8647 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008648 lua_pushstring(L, trash.area);
8649 lua_pushlightuserdata(L, sc);
8650 lua_pushcclosure(L, hlua_run_sample_conv, 1);
8651 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008652 }
8653
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008654 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008655
8656 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008657 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008658
8659 /*
8660 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008661 * Register class HTTP
8662 *
8663 */
8664
8665 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008666 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008667
Ilya Shipitsind4259502020-04-08 01:07:56 +05008668 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008669 lua_pushstring(L, "__index");
8670 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008671
8672 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008673 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
8674 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
8675 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
8676 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
8677 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
8678 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
8679 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
8680 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
8681 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
8682 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008683
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008684 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
8685 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
8686 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
8687 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
8688 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
8689 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
8690 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008691
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008692 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008693
8694 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008695 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008696
8697 /*
8698 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008699 * Register class AppletTCP
8700 *
8701 */
8702
8703 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008704 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008705
Ilya Shipitsind4259502020-04-08 01:07:56 +05008706 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008707 lua_pushstring(L, "__index");
8708 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008709
8710 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008711 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
8712 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
8713 hlua_class_function(L, "send", hlua_applet_tcp_send);
8714 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
8715 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
8716 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
8717 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
8718 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008719
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008720 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008721
8722 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008723 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008724
8725 /*
8726 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008727 * Register class AppletHTTP
8728 *
8729 */
8730
8731 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008732 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008733
Ilya Shipitsind4259502020-04-08 01:07:56 +05008734 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008735 lua_pushstring(L, "__index");
8736 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008737
8738 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008739 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
8740 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
8741 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
8742 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
8743 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
8744 hlua_class_function(L, "getline", hlua_applet_http_getline);
8745 hlua_class_function(L, "receive", hlua_applet_http_recv);
8746 hlua_class_function(L, "send", hlua_applet_http_send);
8747 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
8748 hlua_class_function(L, "set_status", hlua_applet_http_status);
8749 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008750
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008751 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008752
8753 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008754 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008755
8756 /*
8757 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008758 * Register class TXN
8759 *
8760 */
8761
8762 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008763 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008764
Ilya Shipitsind4259502020-04-08 01:07:56 +05008765 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008766 lua_pushstring(L, "__index");
8767 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008768
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008769 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008770 hlua_class_function(L, "set_priv", hlua_set_priv);
8771 hlua_class_function(L, "get_priv", hlua_get_priv);
8772 hlua_class_function(L, "set_var", hlua_set_var);
8773 hlua_class_function(L, "unset_var", hlua_unset_var);
8774 hlua_class_function(L, "get_var", hlua_get_var);
8775 hlua_class_function(L, "done", hlua_txn_done);
8776 hlua_class_function(L, "reply", hlua_txn_reply_new);
8777 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
8778 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
8779 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
8780 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
8781 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
8782 hlua_class_function(L, "deflog", hlua_txn_deflog);
8783 hlua_class_function(L, "log", hlua_txn_log);
8784 hlua_class_function(L, "Debug", hlua_txn_log_debug);
8785 hlua_class_function(L, "Info", hlua_txn_log_info);
8786 hlua_class_function(L, "Warning", hlua_txn_log_warning);
8787 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008788
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008789 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008790
8791 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008792 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008793
8794 /*
8795 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008796 * Register class reply
8797 *
8798 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008799 lua_newtable(L);
8800 lua_pushstring(L, "__index");
8801 lua_newtable(L);
8802 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
8803 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
8804 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
8805 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
8806 lua_settable(L, -3); /* Sets the __index entry. */
8807 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008808
8809
8810 /*
8811 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008812 * Register class Socket
8813 *
8814 */
8815
8816 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008817 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008818
Ilya Shipitsind4259502020-04-08 01:07:56 +05008819 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008820 lua_pushstring(L, "__index");
8821 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008822
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008823#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008824 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008825#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008826 hlua_class_function(L, "connect", hlua_socket_connect);
8827 hlua_class_function(L, "send", hlua_socket_send);
8828 hlua_class_function(L, "receive", hlua_socket_receive);
8829 hlua_class_function(L, "close", hlua_socket_close);
8830 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
8831 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
8832 hlua_class_function(L, "setoption", hlua_socket_setoption);
8833 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008834
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008835 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008836
8837 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008838 lua_pushstring(L, "__gc");
8839 lua_pushcclosure(L, hlua_socket_gc, 0);
8840 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008841
8842 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008843 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008844
8845 /* Proxy and server configuration initialisation. */
8846 memset(&socket_proxy, 0, sizeof(socket_proxy));
8847 init_new_proxy(&socket_proxy);
8848 socket_proxy.parent = NULL;
8849 socket_proxy.last_change = now.tv_sec;
8850 socket_proxy.id = "LUA-SOCKET";
8851 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8852 socket_proxy.maxconn = 0;
8853 socket_proxy.accept = NULL;
8854 socket_proxy.options2 |= PR_O2_INDEPSTR;
8855 socket_proxy.srv = NULL;
8856 socket_proxy.conn_retries = 0;
8857 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8858
8859 /* Init TCP server: unchanged parameters */
8860 memset(&socket_tcp, 0, sizeof(socket_tcp));
8861 socket_tcp.next = NULL;
8862 socket_tcp.proxy = &socket_proxy;
8863 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8864 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008865 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008866 socket_tcp.idle_conns = NULL;
8867 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008868 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008869 socket_tcp.last_change = 0;
8870 socket_tcp.id = "LUA-TCP-CONN";
8871 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8872 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8873 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8874
8875 /* XXX: Copy default parameter from default server,
8876 * but the default server is not initialized.
8877 */
8878 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8879 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8880 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8881 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8882 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8883 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8884 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8885 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8886 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8887 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8888
8889 socket_tcp.check.status = HCHK_STATUS_INI;
8890 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8891 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8892 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8893 socket_tcp.check.server = &socket_tcp;
8894
8895 socket_tcp.agent.status = HCHK_STATUS_INI;
8896 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8897 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8898 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8899 socket_tcp.agent.server = &socket_tcp;
8900
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008901 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008902
8903#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008904 /* Init TCP server: unchanged parameters */
8905 memset(&socket_ssl, 0, sizeof(socket_ssl));
8906 socket_ssl.next = NULL;
8907 socket_ssl.proxy = &socket_proxy;
8908 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8909 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008910 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008911 socket_ssl.idle_conns = NULL;
8912 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008913 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008914 socket_ssl.last_change = 0;
8915 socket_ssl.id = "LUA-SSL-CONN";
8916 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8917 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8918 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8919
8920 /* XXX: Copy default parameter from default server,
8921 * but the default server is not initialized.
8922 */
8923 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8924 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8925 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8926 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8927 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8928 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8929 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8930 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8931 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8932 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8933
8934 socket_ssl.check.status = HCHK_STATUS_INI;
8935 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8936 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8937 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8938 socket_ssl.check.server = &socket_ssl;
8939
8940 socket_ssl.agent.status = HCHK_STATUS_INI;
8941 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8942 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8943 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8944 socket_ssl.agent.server = &socket_ssl;
8945
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008946 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008947 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008948
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008949 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008950 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8951 /*
8952 *
8953 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008954 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008955 * features like client certificates and ssl_verify.
8956 *
8957 */
8958 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8959 if (tmp_error != 0) {
8960 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8961 abort(); /* This must be never arrives because the command line
8962 not editable by the user. */
8963 }
8964 idx += kw->skip;
8965 }
8966 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008967#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008968
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008969 RESET_SAFE_LJMP(L);
8970
8971 return L;
8972}
8973
8974void hlua_init(void) {
8975 gL.T = hlua_init_state();
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008976}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008977
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02008978static void hlua_deinit()
8979{
8980 lua_close(gL.T);
8981}
8982
8983REGISTER_POST_DEINIT(hlua_deinit);
8984
Willy Tarreau80713382018-11-26 10:19:54 +01008985static void hlua_register_build_options(void)
8986{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008987 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008988
Willy Tarreaubb57d942016-12-21 19:04:56 +01008989 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8990 hap_register_build_opts(ptr, 1);
8991}
Willy Tarreau80713382018-11-26 10:19:54 +01008992
8993INITCALL0(STG_REGISTER, hlua_register_build_options);