blob: 6530a39cfab1076677ce3a87657e922b1d4c6a9e [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua unsafe core engine
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010013#include <ctype.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020014#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010015
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010016#include <lauxlib.h>
17#include <lua.h>
18#include <lualib.h>
19
Thierry FOURNIER463119c2015-03-10 00:35:36 +010020#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
21#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010022#endif
23
Willy Tarreau8d2b7772020-05-27 10:58:19 +020024#include <import/ebpttree.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010025
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
27#include <haproxy/applet.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020028#include <haproxy/arg.h>
Christopher Fauletd25d9262020-08-06 11:04:46 +020029#include <haproxy/auth.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020030#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020031#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020032#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020033#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020034#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020035#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020036#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020037#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020038#include <haproxy/http_fetch.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020040#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020041#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020042#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020043#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020044#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020045#include <haproxy/payload.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020046#include <haproxy/proxy-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020047#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020048#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020049#include <haproxy/server-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020050#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020051#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020052#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020053#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020054#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020055#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020056#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020057#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020058#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020059#include <haproxy/xref.h>
60
Thierry FOURNIER380d0932015-01-23 14:27:52 +010061
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010062/* Lua uses longjmp to perform yield or throwing errors. This
63 * macro is used only for identifying the function that can
64 * not return because a longjmp is executed.
65 * __LJMP marks a prototype of hlua file that can use longjmp.
66 * WILL_LJMP() marks an lua function that will use longjmp.
67 * MAY_LJMP() marks an lua function that may use longjmp.
68 */
69#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020070#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010071#define MAY_LJMP(func) func
72
Thierry FOURNIERbabae282015-09-17 11:36:37 +020073/* This couple of function executes securely some Lua calls outside of
74 * the lua runtime environment. Each Lua call can return a longjmp
75 * if it encounter a memory error.
76 *
77 * Lua documentation extract:
78 *
79 * If an error happens outside any protected environment, Lua calls
80 * a panic function (see lua_atpanic) and then calls abort, thus
81 * exiting the host application. Your panic function can avoid this
82 * exit by never returning (e.g., doing a long jump to your own
83 * recovery point outside Lua).
84 *
85 * The panic function runs as if it were a message handler (see
86 * §2.3); in particular, the error message is at the top of the
87 * stack. However, there is no guarantee about stack space. To push
88 * anything on the stack, the panic function must first check the
89 * available space (see §4.2).
90 *
91 * We must check all the Lua entry point. This includes:
92 * - The include/proto/hlua.h exported functions
93 * - the task wrapper function
94 * - The action wrapper function
95 * - The converters wrapper function
96 * - The sample-fetch wrapper functions
97 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +050098 * It is tolerated that the initialisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080099 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200100 *
101 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
102 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
103 * because they must be exists in the program stack when the longjmp
104 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200105 *
106 * Note that the Lua processing is not really thread safe. It provides
107 * heavy system which consists to add our own lock function in the Lua
108 * code and recompile the library. This system will probably not accepted
109 * by maintainers of various distribs.
110 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500111 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200112 * quick looking on the Lua sources displays a lua_lock() a the start
113 * of function and a lua_unlock() at the end of the function. So I
114 * conclude that the Lua thread safe mode just perform a mutex around
115 * all execution. So I prefer to do this in the HAProxy code, it will be
116 * easier for distro maintainers.
117 *
118 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
119 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
120 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200121 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100122__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200123THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200124static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100125static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200126
127#define SET_SAFE_LJMP(__L) \
128 ({ \
129 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100130 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200131 if (setjmp(safe_ljmp_env) != 0) { \
132 lua_atpanic(__L, hlua_panic_safe); \
133 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100134 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200135 } else { \
136 lua_atpanic(__L, hlua_panic_ljmp); \
137 ret = 1; \
138 } \
139 ret; \
140 })
141
142/* If we are the last function catching Lua errors, we
143 * must reset the panic function.
144 */
145#define RESET_SAFE_LJMP(__L) \
146 do { \
147 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100148 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200149 } while(0)
150
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200151/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200152#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100153/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200154#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200155/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100156#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100157#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200158
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100159/* The main Lua execution context. */
160struct hlua gL;
161
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100162/* This is the memory pool containing struct lua for applets
163 * (including cli).
164 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100165DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100166
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100167/* Used for Socket connection. */
168static struct proxy socket_proxy;
169static struct server socket_tcp;
170#ifdef USE_OPENSSL
171static struct server socket_ssl;
172#endif
173
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100174/* List head of the function called at the initialisation time. */
175struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
176
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100177/* The following variables contains the reference of the different
178 * Lua classes. These references are useful for identify metadata
179 * associated with an object.
180 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100181static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100182static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100183static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100184static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100185static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100186static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200187static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200188static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200189static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100190static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100191
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100192/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200193 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100194 * short timeout. Lua linked with tasks doesn't have a timeout
195 * because a task may remain alive during all the haproxy execution.
196 */
197static unsigned int hlua_timeout_session = 4000; /* session timeout. */
198static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200199static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100200
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100201/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
202 * it is used for preventing infinite loops.
203 *
204 * I test the scheer with an infinite loop containing one incrementation
205 * and one test. I run this loop between 10 seconds, I raise a ceil of
206 * 710M loops from one interrupt each 9000 instructions, so I fix the value
207 * to one interrupt each 10 000 instructions.
208 *
209 * configured | Number of
210 * instructions | loops executed
211 * between two | in milions
212 * forced yields |
213 * ---------------+---------------
214 * 10 | 160
215 * 500 | 670
216 * 1000 | 680
217 * 5000 | 700
218 * 7000 | 700
219 * 8000 | 700
220 * 9000 | 710 <- ceil
221 * 10000 | 710
222 * 100000 | 710
223 * 1000000 | 710
224 *
225 */
226static unsigned int hlua_nb_instruction = 10000;
227
Willy Tarreaucdb53462020-12-02 12:12:00 +0100228/* Descriptor for the memory allocation state. The limit is pre-initialised to
229 * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it
230 * is replaced with ~0 during post_init after everything was loaded. This way
231 * it is guaranteed that if limit is ~0 the boot is complete and that if it's
232 * zero it's not yet limited and proper accounting is required.
Willy Tarreau32f61e22015-03-18 17:54:59 +0100233 */
234struct hlua_mem_allocator {
235 size_t allocated;
236 size_t limit;
237};
238
Willy Tarreaucdb53462020-12-02 12:12:00 +0100239static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64);
Willy Tarreau32f61e22015-03-18 17:54:59 +0100240
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100241/* These functions converts types between HAProxy internal args or
242 * sample and LUA types. Another function permits to check if the
243 * LUA stack contains arguments according with an required ARG_T
244 * format.
245 */
246static int hlua_arg2lua(lua_State *L, const struct arg *arg);
247static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100248__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100249 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100250static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100251static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100252static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
253
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100254__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200255
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200256#define SEND_ERR(__be, __fmt, __args...) \
257 do { \
258 send_log(__be, LOG_ERR, __fmt, ## __args); \
259 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100260 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200261 } while (0)
262
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100263/* Used to check an Lua function type in the stack. It creates and
264 * returns a reference of the function. This function throws an
265 * error if the rgument is not a "function".
266 */
267__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
268{
269 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100270 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100271 WILL_LJMP(luaL_argerror(L, argno, msg));
272 }
273 lua_pushvalue(L, argno);
274 return luaL_ref(L, LUA_REGISTRYINDEX);
275}
276
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200277/* Return the string that is of the top of the stack. */
278const char *hlua_get_top_error_string(lua_State *L)
279{
280 if (lua_gettop(L) < 1)
281 return "unknown error";
282 if (lua_type(L, -1) != LUA_TSTRING)
283 return "unknown error";
284 return lua_tostring(L, -1);
285}
286
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200287__LJMP static const char *hlua_traceback(lua_State *L)
288{
289 lua_Debug ar;
290 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200291 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200292 int filled = 0;
293
294 while (lua_getstack(L, level++, &ar)) {
295
296 /* Add separator */
297 if (filled)
298 chunk_appendf(msg, ", ");
299 filled = 1;
300
301 /* Fill fields:
302 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
303 * 'l': fills in the field currentline;
304 * 'n': fills in the field name and namewhat;
305 * 't': fills in the field istailcall;
306 */
307 lua_getinfo(L, "Slnt", &ar);
308
309 /* Append code localisation */
310 if (ar.currentline > 0)
311 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
312 else
313 chunk_appendf(msg, "%s ", ar.short_src);
314
315 /*
316 * Get function name
317 *
318 * if namewhat is no empty, name is defined.
319 * what contains "Lua" for Lua function, "C" for C function,
320 * or "main" for main code.
321 */
322 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
323 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
324
325 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
326 chunk_appendf(msg, "main chunk");
327
328 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
329 chunk_appendf(msg, "C function line %d", ar.linedefined);
330
331 else /* nothing left... */
332 chunk_appendf(msg, "?");
333
334
335 /* Display tailed call */
336 if (ar.istailcall)
337 chunk_appendf(msg, " ...");
338 }
339
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200340 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200341}
342
343
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100344/* This function check the number of arguments available in the
345 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500346 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100347 */
348__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
349{
350 if (lua_gettop(L) == nb)
351 return;
352 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
353}
354
Mark Lakes22154b42018-01-29 14:38:40 -0800355/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100356 * and the line number where the error is encountered.
357 */
358static int hlua_pusherror(lua_State *L, const char *fmt, ...)
359{
360 va_list argp;
361 va_start(argp, fmt);
362 luaL_where(L, 1);
363 lua_pushvfstring(L, fmt, argp);
364 va_end(argp);
365 lua_concat(L, 2);
366 return 1;
367}
368
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100369/* This functions is used with sample fetch and converters. It
370 * converts the HAProxy configuration argument in a lua stack
371 * values.
372 *
373 * It takes an array of "arg", and each entry of the array is
374 * converted and pushed in the LUA stack.
375 */
376static int hlua_arg2lua(lua_State *L, const struct arg *arg)
377{
378 switch (arg->type) {
379 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100380 case ARGT_TIME:
381 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100382 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100383 break;
384
385 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200386 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100387 break;
388
389 case ARGT_IPV4:
390 case ARGT_IPV6:
391 case ARGT_MSK4:
392 case ARGT_MSK6:
393 case ARGT_FE:
394 case ARGT_BE:
395 case ARGT_TAB:
396 case ARGT_SRV:
397 case ARGT_USR:
398 case ARGT_MAP:
399 default:
400 lua_pushnil(L);
401 break;
402 }
403 return 1;
404}
405
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500406/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100407 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500408 * with sample fetch wrappers. The input arguments are given to the
409 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100410 */
411static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
412{
413 switch (lua_type(L, ud)) {
414
415 case LUA_TNUMBER:
416 case LUA_TBOOLEAN:
417 arg->type = ARGT_SINT;
418 arg->data.sint = lua_tointeger(L, ud);
419 break;
420
421 case LUA_TSTRING:
422 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200423 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200424 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200425 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200426 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100427 break;
428
429 case LUA_TUSERDATA:
430 case LUA_TNIL:
431 case LUA_TTABLE:
432 case LUA_TFUNCTION:
433 case LUA_TTHREAD:
434 case LUA_TLIGHTUSERDATA:
435 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200436 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100437 break;
438 }
439 return 1;
440}
441
442/* the following functions are used to convert a struct sample
443 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500444 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100445 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100446static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200448 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100450 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200451 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452 break;
453
454 case SMP_T_BIN:
455 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200456 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 break;
458
459 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200460 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100461 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
462 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
463 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
464 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
465 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
466 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
467 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
468 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
469 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200470 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100471 break;
472 default:
473 lua_pushnil(L);
474 break;
475 }
476 break;
477
478 case SMP_T_IPV4:
479 case SMP_T_IPV6:
480 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200481 if (sample_casts[smp->data.type][SMP_T_STR] &&
482 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200483 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100484 else
485 lua_pushnil(L);
486 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100487 default:
488 lua_pushnil(L);
489 break;
490 }
491 return 1;
492}
493
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100494/* the following functions are used to convert a struct sample
495 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500496 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100497 */
498static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
499{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200500 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100501
502 case SMP_T_BIN:
503 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200504 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505 break;
506
507 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200508 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100509 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
510 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
511 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
512 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
513 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
514 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
515 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
516 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
517 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100519 break;
520 default:
521 lua_pushstring(L, "");
522 break;
523 }
524 break;
525
526 case SMP_T_SINT:
527 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100528 case SMP_T_IPV4:
529 case SMP_T_IPV6:
530 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200531 if (sample_casts[smp->data.type][SMP_T_STR] &&
532 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200533 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100534 else
535 lua_pushstring(L, "");
536 break;
537 default:
538 lua_pushstring(L, "");
539 break;
540 }
541 return 1;
542}
543
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100544/* the following functions are used to convert an Lua type in a
545 * struct sample. This is useful to provide data from a converter
546 * to the LUA code.
547 */
548static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
549{
550 switch (lua_type(L, ud)) {
551
552 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200553 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200554 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100555 break;
556
557
558 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200559 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200560 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 break;
562
563 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200564 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100565 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200566 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200567 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200568 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200569 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 break;
571
572 case LUA_TUSERDATA:
573 case LUA_TNIL:
574 case LUA_TTABLE:
575 case LUA_TFUNCTION:
576 case LUA_TTHREAD:
577 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200578 case LUA_TNONE:
579 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200580 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200581 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100582 break;
583 }
584 return 1;
585}
586
Ilya Shipitsind4259502020-04-08 01:07:56 +0500587/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800588 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100589 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100590 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500591 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100592 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100593 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100594__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100595 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100596{
597 int min_arg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200598 int i, idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100599 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200600 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200601 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200602 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200603 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100604
605 idx = 0;
606 min_arg = ARGM(mask);
607 mask >>= ARGM_BITS;
608
609 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200610 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100611
612 /* Check oversize. */
613 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200614 msg = "Malformed argument mask";
615 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100616 }
617
618 /* Check for mandatory arguments. */
619 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100620 if (idx < min_arg) {
621
622 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200623 if (idx > 0) {
624 msg = "Mandatory argument expected";
625 goto error;
626 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100627
628 /* If first argument have a certain type, some default values
629 * may be used. See the function smp_resolve_args().
630 */
631 switch (mask & ARGT_MASK) {
632
633 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200634 if (!(p->cap & PR_CAP_FE)) {
635 msg = "Mandatory argument expected";
636 goto error;
637 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100638 argp[idx].data.prx = p;
639 argp[idx].type = ARGT_FE;
640 argp[idx+1].type = ARGT_STOP;
641 break;
642
643 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200644 if (!(p->cap & PR_CAP_BE)) {
645 msg = "Mandatory argument expected";
646 goto error;
647 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100648 argp[idx].data.prx = p;
649 argp[idx].type = ARGT_BE;
650 argp[idx+1].type = ARGT_STOP;
651 break;
652
653 case ARGT_TAB:
654 argp[idx].data.prx = p;
655 argp[idx].type = ARGT_TAB;
656 argp[idx+1].type = ARGT_STOP;
657 break;
658
659 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200660 msg = "Mandatory argument expected";
661 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100662 break;
663 }
664 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200665 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100666 }
667
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500668 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100669 if ((mask & ARGT_MASK) == ARGT_STOP &&
670 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200671 msg = "Last argument expected";
672 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100673 }
674
675 if ((mask & ARGT_MASK) == ARGT_STOP &&
676 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200677 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100678 }
679
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200680 /* Convert some argument types. All string in argp[] are for not
681 * duplicated yet.
682 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100683 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100684 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200685 if (argp[idx].type != ARGT_SINT) {
686 msg = "integer expected";
687 goto error;
688 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100689 argp[idx].type = ARGT_SINT;
690 break;
691
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100692 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200693 if (argp[idx].type != ARGT_SINT) {
694 msg = "integer expected";
695 goto error;
696 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200697 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100698 break;
699
700 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200701 if (argp[idx].type != ARGT_SINT) {
702 msg = "integer expected";
703 goto error;
704 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200705 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100706 break;
707
708 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200709 if (argp[idx].type != ARGT_STR) {
710 msg = "string expected";
711 goto error;
712 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200713 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200714 if (!argp[idx].data.prx) {
715 msg = "frontend doesn't exist";
716 goto error;
717 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100718 argp[idx].type = ARGT_FE;
719 break;
720
721 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200722 if (argp[idx].type != ARGT_STR) {
723 msg = "string expected";
724 goto error;
725 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200726 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200727 if (!argp[idx].data.prx) {
728 msg = "backend doesn't exist";
729 goto error;
730 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100731 argp[idx].type = ARGT_BE;
732 break;
733
734 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200735 if (argp[idx].type != ARGT_STR) {
736 msg = "string expected";
737 goto error;
738 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200739 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200740 if (!argp[idx].data.t) {
741 msg = "table doesn't exist";
742 goto error;
743 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100744 argp[idx].type = ARGT_TAB;
745 break;
746
747 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200748 if (argp[idx].type != ARGT_STR) {
749 msg = "string expected";
750 goto error;
751 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200752 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100753 if (sname) {
754 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200755 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200756 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200757 if (!px) {
758 msg = "backend doesn't exist";
759 goto error;
760 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100761 }
762 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200763 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100764 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100765 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100766 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200767 if (!argp[idx].data.srv) {
768 msg = "server doesn't exist";
769 goto error;
770 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100771 argp[idx].type = ARGT_SRV;
772 break;
773
774 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200775 if (argp[idx].type != ARGT_STR) {
776 msg = "string expected";
777 goto error;
778 }
779 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
780 msg = "invalid IPv4 address";
781 goto error;
782 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100783 argp[idx].type = ARGT_IPV4;
784 break;
785
786 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200787 if (argp[idx].type == ARGT_SINT)
788 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
789 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200790 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
791 msg = "invalid IPv4 mask";
792 goto error;
793 }
794 }
795 else {
796 msg = "integer or string expected";
797 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200798 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100799 argp[idx].type = ARGT_MSK4;
800 break;
801
802 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200803 if (argp[idx].type != ARGT_STR) {
804 msg = "string expected";
805 goto error;
806 }
807 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
808 msg = "invalid IPv6 address";
809 goto error;
810 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100811 argp[idx].type = ARGT_IPV6;
812 break;
813
814 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200815 if (argp[idx].type == ARGT_SINT)
816 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
817 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200818 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
819 msg = "invalid IPv6 mask";
820 goto error;
821 }
822 }
823 else {
824 msg = "integer or string expected";
825 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200826 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100827 argp[idx].type = ARGT_MSK6;
828 break;
829
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200830 case ARGT_REG:
831 if (argp[idx].type != ARGT_STR) {
832 msg = "string expected";
833 goto error;
834 }
835 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
836 if (!reg) {
837 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
838 argp[idx].data.str.area, err);
839 free(err);
840 goto error;
841 }
842 argp[idx].type = ARGT_REG;
843 argp[idx].data.reg = reg;
844 break;
845
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100846 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200847 if (argp[idx].type != ARGT_STR) {
848 msg = "string expected";
849 goto error;
850 }
851 if (p->uri_auth && p->uri_auth->userlist &&
852 !strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area))
853 ul = p->uri_auth->userlist;
854 else
855 ul = auth_find_userlist(argp[idx].data.str.area);
856
857 if (!ul) {
858 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
859 goto error;
860 }
861 argp[idx].type = ARGT_USR;
862 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200863 break;
864
865 case ARGT_STR:
866 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
867 msg = "unable to duplicate string arg";
868 goto error;
869 }
870 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100871 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200872
Christopher Fauletd25d9262020-08-06 11:04:46 +0200873 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200874 msg = "type not yet supported";
875 goto error;
876 break;
877
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100878 }
879
880 /* Check for type of argument. */
881 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200882 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
883 arg_type_names[(mask & ARGT_MASK)],
884 arg_type_names[argp[idx].type & ARGT_MASK]);
885 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100886 }
887
888 /* Next argument. */
889 mask >>= ARGT_BITS;
890 idx++;
891 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200892 return 0;
893
894 error:
895 for (i = 0; i < idx; i++) {
896 if (argp[i].type == ARGT_STR)
897 chunk_destroy(&argp[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200898 else if (argp[i].type == ARGT_REG)
899 regex_free(argp[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200900 }
901 WILL_LJMP(luaL_argerror(L, first + idx, msg));
902 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100903}
904
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100905/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500906 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100908 *
909 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100910 * - hlua_sethlua : create the association between hlua context and lua_state.
911 */
912static inline struct hlua *hlua_gethlua(lua_State *L)
913{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100914 struct hlua **hlua = lua_getextraspace(L);
915 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100916}
917static inline void hlua_sethlua(struct hlua *hlua)
918{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100919 struct hlua **hlua_store = lua_getextraspace(hlua->T);
920 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100921}
922
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100923/* This function is used to send logs. It try to send on screen (stderr)
924 * and on the default syslog server.
925 */
926static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
927{
928 struct tm tm;
929 char *p;
930
931 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200932 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100933 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200934 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200935 /* Break the message if exceed the buffer size. */
936 *(p-4) = ' ';
937 *(p-3) = '.';
938 *(p-2) = '.';
939 *(p-1) = '.';
940 break;
941 }
Willy Tarreau90807112020-02-25 08:16:33 +0100942 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100943 *p = *msg;
944 else
945 *p = '.';
946 }
947 *p = '\0';
948
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200949 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100950 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +0200951 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
952 return;
953
Willy Tarreaua678b432015-08-28 10:14:59 +0200954 get_localtime(date.tv_sec, &tm);
955 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100956 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200957 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100958 fflush(stderr);
959 }
960}
961
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100962/* This function just ensure that the yield will be always
963 * returned with a timeout and permit to set some flags
964 */
965__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100966 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100967{
968 struct hlua *hlua = hlua_gethlua(L);
969
970 /* Set the wake timeout. If timeout is required, we set
971 * the expiration time.
972 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200973 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100974
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100975 hlua->flags |= flags;
976
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100977 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200978 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100979}
980
Willy Tarreau87b09662015-04-03 00:22:06 +0200981/* This function initialises the Lua environment stored in the stream.
982 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100983 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200984 *
985 * This function is particular. it initialises a new Lua thread. If the
986 * initialisation fails (example: out of memory error), the lua function
987 * throws an error (longjmp).
988 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100989 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500990 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100991 * threads appear, the safe environment set a lock to ensure only one
992 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500993 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100994 *
995 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500996 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100997 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800998 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200999 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001000 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001001 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001002 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001003int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001004{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001005 if (!already_safe) {
1006 if (!SET_SAFE_LJMP(gL.T)) {
1007 lua->Tref = LUA_REFNIL;
1008 return 0;
1009 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001010 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001011 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001012 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001013 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001014 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001015 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001016 lua->T = lua_newthread(gL.T);
1017 if (!lua->T) {
1018 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001019 if (!already_safe)
1020 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001021 return 0;
1022 }
1023 hlua_sethlua(lua);
1024 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1025 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001026 if (!already_safe)
1027 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001028 return 1;
1029}
1030
Willy Tarreau87b09662015-04-03 00:22:06 +02001031/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001032 * is destroyed. The destroy also the memory context. The struct "lua"
1033 * is not freed.
1034 */
1035void hlua_ctx_destroy(struct hlua *lua)
1036{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001037 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001038 return;
1039
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001040 if (!lua->T)
1041 goto end;
1042
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001043 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001044 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001045
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001046 if (!SET_SAFE_LJMP(lua->T))
1047 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001048 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001049 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001050
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001051 if (!SET_SAFE_LJMP(gL.T))
1052 return;
1053 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1054 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001055 /* Forces a garbage collecting process. If the Lua program is finished
1056 * without error, we run the GC on the thread pointer. Its freed all
1057 * the unused memory.
1058 * If the thread is finnish with an error or is currently yielded,
1059 * it seems that the GC applied on the thread doesn't clean anything,
1060 * so e run the GC on the main thread.
1061 * NOTE: maybe this action locks all the Lua threads untiml the en of
1062 * the garbage collection.
1063 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001064 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +02001065 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001066 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +02001067 lua_gc(gL.T, LUA_GCCOLLECT, 0);
1068 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001069 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001070
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001071 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001072
1073end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001074 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001075}
1076
1077/* This function is used to restore the Lua context when a coroutine
1078 * fails. This function copy the common memory between old coroutine
1079 * and the new coroutine. The old coroutine is destroyed, and its
1080 * replaced by the new coroutine.
1081 * If the flag "keep_msg" is set, the last entry of the old is assumed
1082 * as string error message and it is copied in the new stack.
1083 */
1084static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1085{
1086 lua_State *T;
1087 int new_ref;
1088
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001089 /* New Lua coroutine. */
1090 T = lua_newthread(gL.T);
1091 if (!T)
1092 return 0;
1093
1094 /* Copy last error message. */
1095 if (keep_msg)
1096 lua_xmove(lua->T, T, 1);
1097
1098 /* Copy data between the coroutines. */
1099 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1100 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001101 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001102
1103 /* Destroy old data. */
1104 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1105
1106 /* The thread is garbage collected by Lua. */
1107 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1108
1109 /* Fill the struct with the new coroutine values. */
1110 lua->Mref = new_ref;
1111 lua->T = T;
1112 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1113
1114 /* Set context. */
1115 hlua_sethlua(lua);
1116
1117 return 1;
1118}
1119
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001120void hlua_hook(lua_State *L, lua_Debug *ar)
1121{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001122 struct hlua *hlua = hlua_gethlua(L);
1123
1124 /* Lua cannot yield when its returning from a function,
1125 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001126 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001127 */
1128 if (lua_gethookmask(L) & LUA_MASKRET) {
1129 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1130 return;
1131 }
1132
1133 /* restore the interrupt condition. */
1134 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1135
1136 /* If we interrupt the Lua processing in yieldable state, we yield.
1137 * If the state is not yieldable, trying yield causes an error.
1138 */
1139 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001140 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001141
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001142 /* If we cannot yield, update the clock and check the timeout. */
1143 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001144 hlua->run_time += now_ms - hlua->start_time;
1145 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001146 lua_pushfstring(L, "execution timeout");
1147 WILL_LJMP(lua_error(L));
1148 }
1149
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001150 /* Update the start time. */
1151 hlua->start_time = now_ms;
1152
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001153 /* Try to interrupt the process at the end of the current
1154 * unyieldable function.
1155 */
1156 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001157}
1158
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001159/* This function start or resumes the Lua stack execution. If the flag
1160 * "yield_allowed" if no set and the LUA stack execution returns a yield
1161 * The function return an error.
1162 *
1163 * The function can returns 4 values:
1164 * - HLUA_E_OK : The execution is terminated without any errors.
1165 * - HLUA_E_AGAIN : The execution must continue at the next associated
1166 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001167 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001168 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001169 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001170 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001171 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001172 * LUA code.
1173 */
1174static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1175{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001176#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1177 int nres;
1178#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001179 int ret;
1180 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001181 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001182
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001183 /* Initialise run time counter. */
1184 if (!HLUA_IS_RUNNING(lua))
1185 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001186
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001187 /* Lock the whole Lua execution. This lock must be before the
1188 * label "resume_execution".
1189 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001190 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001191
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001192resume_execution:
1193
1194 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1195 * instructions. it is used for preventing infinite loops.
1196 */
1197 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1198
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001199 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001200 HLUA_SET_RUN(lua);
1201 HLUA_CLR_CTRLYIELD(lua);
1202 HLUA_CLR_WAKERESWR(lua);
1203 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001204
Christopher Fauletbc275a92020-02-26 14:55:16 +01001205 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001206 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001207 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001208
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001209 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001210#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1211 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1212#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001213 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001214#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001215 switch (ret) {
1216
1217 case LUA_OK:
1218 ret = HLUA_E_OK;
1219 break;
1220
1221 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001222 /* Check if the execution timeout is expired. It it is the case, we
1223 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001224 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001225 tv_update_date(0, 1);
1226 lua->run_time += now_ms - lua->start_time;
1227 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001228 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001229 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001230 break;
1231 }
1232 /* Process the forced yield. if the general yield is not allowed or
1233 * if no task were associated this the current Lua execution
1234 * coroutine, we resume the execution. Else we want to return in the
1235 * scheduler and we want to be waked up again, to continue the
1236 * current Lua execution. So we schedule our own task.
1237 */
1238 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001239 if (!yield_allowed || !lua->task)
1240 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001241 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001242 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001243 if (!yield_allowed) {
1244 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001245 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001246 break;
1247 }
1248 ret = HLUA_E_AGAIN;
1249 break;
1250
1251 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001252
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001253 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001254 * because the errors ares the only one mean to return immediately
1255 * from and lua execution.
1256 */
1257 if (lua->flags & HLUA_EXIT) {
1258 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001259 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001260 break;
1261 }
1262
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001263 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001264 if (!lua_checkstack(lua->T, 1)) {
1265 ret = HLUA_E_ERR;
1266 break;
1267 }
1268 msg = lua_tostring(lua->T, -1);
1269 lua_settop(lua->T, 0); /* Empty the stack. */
1270 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001271 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001272 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001273 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001274 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001275 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001276 ret = HLUA_E_ERRMSG;
1277 break;
1278
1279 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001280 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001281 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001282 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001283 break;
1284
1285 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001286 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001287 if (!lua_checkstack(lua->T, 1)) {
1288 ret = HLUA_E_ERR;
1289 break;
1290 }
1291 msg = lua_tostring(lua->T, -1);
1292 lua_settop(lua->T, 0); /* Empty the stack. */
1293 lua_pop(lua->T, 1);
1294 if (msg)
1295 lua_pushfstring(lua->T, "message handler error: %s", msg);
1296 else
1297 lua_pushfstring(lua->T, "message handler error");
1298 ret = HLUA_E_ERRMSG;
1299 break;
1300
1301 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001302 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001303 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001304 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001305 break;
1306 }
1307
1308 switch (ret) {
1309 case HLUA_E_AGAIN:
1310 break;
1311
1312 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001313 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001314 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001315 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001316 break;
1317
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001318 case HLUA_E_ETMOUT:
1319 case HLUA_E_NOMEM:
1320 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001321 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001322 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001323 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001324 hlua_ctx_renew(lua, 0);
1325 break;
1326
1327 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001328 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001329 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001330 break;
1331 }
1332
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001333 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001334 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001335
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001336 return ret;
1337}
1338
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001339/* This function exit the current code. */
1340__LJMP static int hlua_done(lua_State *L)
1341{
1342 struct hlua *hlua = hlua_gethlua(L);
1343
1344 hlua->flags |= HLUA_EXIT;
1345 WILL_LJMP(lua_error(L));
1346
1347 return 0;
1348}
1349
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001350/* This function is an LUA binding. It provides a function
1351 * for deleting ACL from a referenced ACL file.
1352 */
1353__LJMP static int hlua_del_acl(lua_State *L)
1354{
1355 const char *name;
1356 const char *key;
1357 struct pat_ref *ref;
1358
1359 MAY_LJMP(check_args(L, 2, "del_acl"));
1360
1361 name = MAY_LJMP(luaL_checkstring(L, 1));
1362 key = MAY_LJMP(luaL_checkstring(L, 2));
1363
1364 ref = pat_ref_lookup(name);
1365 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001366 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001367
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001368 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001369 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001370 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001371 return 0;
1372}
1373
1374/* This function is an LUA binding. It provides a function
1375 * for deleting map entry from a referenced map file.
1376 */
1377static int hlua_del_map(lua_State *L)
1378{
1379 const char *name;
1380 const char *key;
1381 struct pat_ref *ref;
1382
1383 MAY_LJMP(check_args(L, 2, "del_map"));
1384
1385 name = MAY_LJMP(luaL_checkstring(L, 1));
1386 key = MAY_LJMP(luaL_checkstring(L, 2));
1387
1388 ref = pat_ref_lookup(name);
1389 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001390 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001391
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001392 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001393 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001394 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001395 return 0;
1396}
1397
1398/* This function is an LUA binding. It provides a function
1399 * for adding ACL pattern from a referenced ACL file.
1400 */
1401static int hlua_add_acl(lua_State *L)
1402{
1403 const char *name;
1404 const char *key;
1405 struct pat_ref *ref;
1406
1407 MAY_LJMP(check_args(L, 2, "add_acl"));
1408
1409 name = MAY_LJMP(luaL_checkstring(L, 1));
1410 key = MAY_LJMP(luaL_checkstring(L, 2));
1411
1412 ref = pat_ref_lookup(name);
1413 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001414 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001415
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001416 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001417 if (pat_ref_find_elt(ref, key) == NULL)
1418 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001419 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001420 return 0;
1421}
1422
1423/* This function is an LUA binding. It provides a function
1424 * for setting map pattern and sample from a referenced map
1425 * file.
1426 */
1427static int hlua_set_map(lua_State *L)
1428{
1429 const char *name;
1430 const char *key;
1431 const char *value;
1432 struct pat_ref *ref;
1433
1434 MAY_LJMP(check_args(L, 3, "set_map"));
1435
1436 name = MAY_LJMP(luaL_checkstring(L, 1));
1437 key = MAY_LJMP(luaL_checkstring(L, 2));
1438 value = MAY_LJMP(luaL_checkstring(L, 3));
1439
1440 ref = pat_ref_lookup(name);
1441 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001442 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001443
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001444 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001445 if (pat_ref_find_elt(ref, key) != NULL)
1446 pat_ref_set(ref, key, value, NULL);
1447 else
1448 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001449 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001450 return 0;
1451}
1452
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001453/* A class is a lot of memory that contain data. This data can be a table,
1454 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001455 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001456 * the name of the object (_G[<name>] = <metable> ).
1457 *
1458 * A metable is a table that modify the standard behavior of a standard
1459 * access to the associated data. The entries of this new metatable are
1460 * defined as is:
1461 *
1462 * http://lua-users.org/wiki/MetatableEvents
1463 *
1464 * __index
1465 *
1466 * we access an absent field in a table, the result is nil. This is
1467 * true, but it is not the whole truth. Actually, such access triggers
1468 * the interpreter to look for an __index metamethod: If there is no
1469 * such method, as usually happens, then the access results in nil;
1470 * otherwise, the metamethod will provide the result.
1471 *
1472 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1473 * the key does not appear in the table, but the metatable has an __index
1474 * property:
1475 *
1476 * - if the value is a function, the function is called, passing in the
1477 * table and the key; the return value of that function is returned as
1478 * the result.
1479 *
1480 * - if the value is another table, the value of the key in that table is
1481 * asked for and returned (and if it doesn't exist in that table, but that
1482 * table's metatable has an __index property, then it continues on up)
1483 *
1484 * - Use "rawget(myTable,key)" to skip this metamethod.
1485 *
1486 * http://www.lua.org/pil/13.4.1.html
1487 *
1488 * __newindex
1489 *
1490 * Like __index, but control property assignment.
1491 *
1492 * __mode - Control weak references. A string value with one or both
1493 * of the characters 'k' and 'v' which specifies that the the
1494 * keys and/or values in the table are weak references.
1495 *
1496 * __call - Treat a table like a function. When a table is followed by
1497 * parenthesis such as "myTable( 'foo' )" and the metatable has
1498 * a __call key pointing to a function, that function is invoked
1499 * (passing any specified arguments) and the return value is
1500 * returned.
1501 *
1502 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1503 * called, if the metatable for myTable has a __metatable
1504 * key, the value of that key is returned instead of the
1505 * actual metatable.
1506 *
1507 * __tostring - Control string representation. When the builtin
1508 * "tostring( myTable )" function is called, if the metatable
1509 * for myTable has a __tostring property set to a function,
1510 * that function is invoked (passing myTable to it) and the
1511 * return value is used as the string representation.
1512 *
1513 * __len - Control table length. When the table length is requested using
1514 * the length operator ( '#' ), if the metatable for myTable has
1515 * a __len key pointing to a function, that function is invoked
1516 * (passing myTable to it) and the return value used as the value
1517 * of "#myTable".
1518 *
1519 * __gc - Userdata finalizer code. When userdata is set to be garbage
1520 * collected, if the metatable has a __gc field pointing to a
1521 * function, that function is first invoked, passing the userdata
1522 * to it. The __gc metamethod is not called for tables.
1523 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1524 *
1525 * Special metamethods for redefining standard operators:
1526 * http://www.lua.org/pil/13.1.html
1527 *
1528 * __add "+"
1529 * __sub "-"
1530 * __mul "*"
1531 * __div "/"
1532 * __unm "!"
1533 * __pow "^"
1534 * __concat ".."
1535 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001536 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001537 * http://www.lua.org/pil/13.2.html
1538 *
1539 * __eq "=="
1540 * __lt "<"
1541 * __le "<="
1542 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001543
1544/*
1545 *
1546 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001547 * Class Map
1548 *
1549 *
1550 */
1551
1552/* Returns a struct hlua_map if the stack entry "ud" is
1553 * a class session, otherwise it throws an error.
1554 */
1555__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1556{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001557 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001558}
1559
1560/* This function is the map constructor. It don't need
1561 * the class Map object. It creates and return a new Map
1562 * object. It must be called only during "body" or "init"
1563 * context because it process some filesystem accesses.
1564 */
1565__LJMP static int hlua_map_new(struct lua_State *L)
1566{
1567 const char *fn;
1568 int match = PAT_MATCH_STR;
1569 struct sample_conv conv;
1570 const char *file = "";
1571 int line = 0;
1572 lua_Debug ar;
1573 char *err = NULL;
1574 struct arg args[2];
1575
1576 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1577 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1578
1579 fn = MAY_LJMP(luaL_checkstring(L, 1));
1580
1581 if (lua_gettop(L) >= 2) {
1582 match = MAY_LJMP(luaL_checkinteger(L, 2));
1583 if (match < 0 || match >= PAT_MATCH_NUM)
1584 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1585 }
1586
1587 /* Get Lua filename and line number. */
1588 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1589 lua_getinfo(L, "Sl", &ar); /* get info about it */
1590 if (ar.currentline > 0) { /* is there info? */
1591 file = ar.short_src;
1592 line = ar.currentline;
1593 }
1594 }
1595
1596 /* fill fake sample_conv struct. */
1597 conv.kw = ""; /* unused. */
1598 conv.process = NULL; /* unused. */
1599 conv.arg_mask = 0; /* unused. */
1600 conv.val_args = NULL; /* unused. */
1601 conv.out_type = SMP_T_STR;
1602 conv.private = (void *)(long)match;
1603 switch (match) {
1604 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1605 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1606 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1607 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1608 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1609 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1610 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001611 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001612 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1613 default:
1614 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1615 }
1616
1617 /* fill fake args. */
1618 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001619 args[0].data.str.area = strdup(fn);
1620 args[0].data.str.data = strlen(fn);
1621 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001622 args[1].type = ARGT_STOP;
1623
1624 /* load the map. */
1625 if (!sample_load_map(args, &conv, file, line, &err)) {
1626 /* error case: we cant use luaL_error because we must
1627 * free the err variable.
1628 */
1629 luaL_where(L, 1);
1630 lua_pushfstring(L, "'new': %s.", err);
1631 lua_concat(L, 2);
1632 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001633 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001634 WILL_LJMP(lua_error(L));
1635 }
1636
1637 /* create the lua object. */
1638 lua_newtable(L);
1639 lua_pushlightuserdata(L, args[0].data.map);
1640 lua_rawseti(L, -2, 0);
1641
1642 /* Pop a class Map metatable and affect it to the userdata. */
1643 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1644 lua_setmetatable(L, -2);
1645
1646
1647 return 1;
1648}
1649
1650__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1651{
1652 struct map_descriptor *desc;
1653 struct pattern *pat;
1654 struct sample smp;
1655
1656 MAY_LJMP(check_args(L, 2, "lookup"));
1657 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001658 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001659 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001660 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001661 }
1662 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001663 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001664 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001665 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 +01001666 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001667 }
1668
1669 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001670 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001671 if (str)
1672 lua_pushstring(L, "");
1673 else
1674 lua_pushnil(L);
1675 return 1;
1676 }
1677
1678 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001679 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001680 return 1;
1681}
1682
1683__LJMP static int hlua_map_lookup(struct lua_State *L)
1684{
1685 return _hlua_map_lookup(L, 0);
1686}
1687
1688__LJMP static int hlua_map_slookup(struct lua_State *L)
1689{
1690 return _hlua_map_lookup(L, 1);
1691}
1692
1693/*
1694 *
1695 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001696 * Class Socket
1697 *
1698 *
1699 */
1700
1701__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1702{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001703 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001704}
1705
1706/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001707 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001708 * received.
1709 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001710static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001711{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001712 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001713
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001714 if (appctx->ctx.hlua_cosocket.die) {
1715 si_shutw(si);
1716 si_shutr(si);
1717 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001718 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1719 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001720 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1721 }
1722
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001723 /* If we cant write, wakeup the pending write signals. */
1724 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001725 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001726
1727 /* If we cant read, wakeup the pending read signals. */
1728 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001729 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001730
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001731 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001732 * to be notified whenever the connection completes.
1733 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001734 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001735 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001736 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001737 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001738 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001739 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001740
1741 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001742 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001743
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001744 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001745 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001746 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001747
1748 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001749 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001750 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001751
1752 /* Some data were injected in the buffer, notify the stream
1753 * interface.
1754 */
1755 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001756 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001757
1758 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001759 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001760 */
1761 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001762 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001763}
1764
Willy Tarreau87b09662015-04-03 00:22:06 +02001765/* This function is called when the "struct stream" is destroyed.
1766 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001767 * Wake all the pending signals.
1768 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001769static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001770{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001771 struct xref *peer;
1772
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001773 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001774 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1775 if (peer)
1776 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001777
1778 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001779 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1780 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001781}
1782
1783/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001784 * uses this object. If the stream does not exists, just quit.
1785 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001786 * pending signal can rest in the read and write lists. destroy
1787 * it.
1788 */
1789__LJMP static int hlua_socket_gc(lua_State *L)
1790{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001791 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001792 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001793 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001794
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001795 MAY_LJMP(check_args(L, 1, "__gc"));
1796
1797 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001798 peer = xref_get_peer_and_lock(&socket->xref);
1799 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001800 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001801 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001802
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001803 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001804 appctx->ctx.hlua_cosocket.die = 1;
1805 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001806
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001807 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001808 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001809 return 0;
1810}
1811
1812/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001813 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001814 */
sada05ed3302018-05-11 11:48:18 -07001815__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001816{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001817 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001819 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001820 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001821
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001822 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001823
1824 /* Check if we run on the same thread than the xreator thread.
1825 * We cannot access to the socket if the thread is different.
1826 */
1827 if (socket->tid != tid)
1828 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1829
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001830 peer = xref_get_peer_and_lock(&socket->xref);
1831 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001833
1834 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001835 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001836
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001837 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001838 appctx->ctx.hlua_cosocket.die = 1;
1839 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001840
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001841 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001842 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001843 return 0;
1844}
1845
sada05ed3302018-05-11 11:48:18 -07001846/* The close function calls close_helper.
1847 */
1848__LJMP static int hlua_socket_close(lua_State *L)
1849{
1850 MAY_LJMP(check_args(L, 1, "close"));
1851 return hlua_socket_close_helper(L);
1852}
1853
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001854/* This Lua function assumes that the stack contain three parameters.
1855 * 1 - USERDATA containing a struct socket
1856 * 2 - INTEGER with values of the macro defined below
1857 * If the integer is -1, we must read at most one line.
1858 * If the integer is -2, we ust read all the data until the
1859 * end of the stream.
1860 * If the integer is positive value, we must read a number of
1861 * bytes corresponding to this value.
1862 */
1863#define HLSR_READ_LINE (-1)
1864#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001865__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001866{
1867 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1868 int wanted = lua_tointeger(L, 2);
1869 struct hlua *hlua = hlua_gethlua(L);
1870 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001871 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001872 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001873 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001874 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001875 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001876 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001877 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001878 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001879 struct stream_interface *si;
1880 struct stream *s;
1881 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001882 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001883
1884 /* Check if this lua stack is schedulable. */
1885 if (!hlua || !hlua->task)
1886 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1887 "'frontend', 'backend' or 'task'"));
1888
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001889 /* Check if we run on the same thread than the xreator thread.
1890 * We cannot access to the socket if the thread is different.
1891 */
1892 if (socket->tid != tid)
1893 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1894
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001895 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001896 peer = xref_get_peer_and_lock(&socket->xref);
1897 if (!peer)
1898 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001899 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1900 si = appctx->owner;
1901 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001902
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001903 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001905 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001906 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001907 if (nblk < 0) /* Connection close. */
1908 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001909 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001910 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001911
1912 /* remove final \r\n. */
1913 if (nblk == 1) {
1914 if (blk1[len1-1] == '\n') {
1915 len1--;
1916 skip_at_end++;
1917 if (blk1[len1-1] == '\r') {
1918 len1--;
1919 skip_at_end++;
1920 }
1921 }
1922 }
1923 else {
1924 if (blk2[len2-1] == '\n') {
1925 len2--;
1926 skip_at_end++;
1927 if (blk2[len2-1] == '\r') {
1928 len2--;
1929 skip_at_end++;
1930 }
1931 }
1932 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001933 }
1934
1935 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001936 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001937 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001938 if (nblk < 0) /* Connection close. */
1939 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001940 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001941 goto connection_empty;
1942 }
1943
1944 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001945 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001946 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001947 if (nblk < 0) /* Connection close. */
1948 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001949 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001950 goto connection_empty;
1951
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001952 missing_bytes = wanted - socket->b.n;
1953 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001954 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001955 len1 = missing_bytes;
1956 } if (nblk == 2 && len1 + len2 > missing_bytes)
1957 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001958 }
1959
1960 len = len1;
1961
1962 luaL_addlstring(&socket->b, blk1, len1);
1963 if (nblk == 2) {
1964 len += len2;
1965 luaL_addlstring(&socket->b, blk2, len2);
1966 }
1967
1968 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001969 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001970
1971 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001972 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001973
1974 /* If the pattern reclaim to read all the data
1975 * in the connection, got out.
1976 */
1977 if (wanted == HLSR_READ_ALL)
1978 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001979 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001980 goto connection_empty;
1981
1982 /* Return result. */
1983 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001984 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001985 return 1;
1986
1987connection_closed:
1988
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001989 xref_unlock(&socket->xref, peer);
1990
1991no_peer:
1992
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001993 /* If the buffer containds data. */
1994 if (socket->b.n > 0) {
1995 luaL_pushresult(&socket->b);
1996 return 1;
1997 }
1998 lua_pushnil(L);
1999 lua_pushstring(L, "connection closed.");
2000 return 2;
2001
2002connection_empty:
2003
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002004 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2005 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002006 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002007 }
2008 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002009 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002010 return 0;
2011}
2012
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002013/* This Lua function gets two parameters. The first one can be string
2014 * or a number. If the string is "*l", the user requires one line. If
2015 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002016 * If the value is a number, the user require a number of bytes equal
2017 * to the value. The default value is "*l" (a line).
2018 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002019 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002020 * integer takes this values:
2021 * -1 : read a line
2022 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002023 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002024 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002025 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002026 * concatenated with the read data.
2027 */
2028__LJMP static int hlua_socket_receive(struct lua_State *L)
2029{
2030 int wanted = HLSR_READ_LINE;
2031 const char *pattern;
2032 int type;
2033 char *error;
2034 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002035 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002036
2037 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2038 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2039
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002040 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002041
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002042 /* Check if we run on the same thread than the xreator thread.
2043 * We cannot access to the socket if the thread is different.
2044 */
2045 if (socket->tid != tid)
2046 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2047
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048 /* check for pattern. */
2049 if (lua_gettop(L) >= 2) {
2050 type = lua_type(L, 2);
2051 if (type == LUA_TSTRING) {
2052 pattern = lua_tostring(L, 2);
2053 if (strcmp(pattern, "*a") == 0)
2054 wanted = HLSR_READ_ALL;
2055 else if (strcmp(pattern, "*l") == 0)
2056 wanted = HLSR_READ_LINE;
2057 else {
2058 wanted = strtoll(pattern, &error, 10);
2059 if (*error != '\0')
2060 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2061 }
2062 }
2063 else if (type == LUA_TNUMBER) {
2064 wanted = lua_tointeger(L, 2);
2065 if (wanted < 0)
2066 WILL_LJMP(luaL_error(L, "Unsupported size."));
2067 }
2068 }
2069
2070 /* Set pattern. */
2071 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002072
2073 /* Check if we would replace the top by itself. */
2074 if (lua_gettop(L) != 2)
2075 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002076
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002077 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002078 luaL_buffinit(L, &socket->b);
2079
2080 /* Check prefix. */
2081 if (lua_gettop(L) >= 3) {
2082 if (lua_type(L, 3) != LUA_TSTRING)
2083 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2084 pattern = lua_tolstring(L, 3, &len);
2085 luaL_addlstring(&socket->b, pattern, len);
2086 }
2087
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002088 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002089}
2090
2091/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002092 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002093 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002094static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002095{
2096 struct hlua_socket *socket;
2097 struct hlua *hlua = hlua_gethlua(L);
2098 struct appctx *appctx;
2099 size_t buf_len;
2100 const char *buf;
2101 int len;
2102 int send_len;
2103 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002104 struct xref *peer;
2105 struct stream_interface *si;
2106 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002107
2108 /* Check if this lua stack is schedulable. */
2109 if (!hlua || !hlua->task)
2110 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2111 "'frontend', 'backend' or 'task'"));
2112
2113 /* Get object */
2114 socket = MAY_LJMP(hlua_checksocket(L, 1));
2115 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002116 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002117
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002118 /* Check if we run on the same thread than the xreator thread.
2119 * We cannot access to the socket if the thread is different.
2120 */
2121 if (socket->tid != tid)
2122 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2123
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002124 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002125 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002126 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002127 lua_pushinteger(L, -1);
2128 return 1;
2129 }
2130 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2131 si = appctx->owner;
2132 s = si_strm(si);
2133
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002134 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002135 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002136 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002137 lua_pushinteger(L, -1);
2138 return 1;
2139 }
2140
2141 /* Update the input buffer data. */
2142 buf += sent;
2143 send_len = buf_len - sent;
2144
2145 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002146 if (sent >= buf_len) {
2147 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002148 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002149 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002150
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002151 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002152 * the request buffer if its not required.
2153 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002154 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002155 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002156 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002157 }
2158
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002159 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002160 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002161 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002162 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002163 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002164
2165 /* send data */
2166 if (len < send_len)
2167 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002168 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002169
2170 /* "Not enough space" (-1), "Buffer too little to contain
2171 * the data" (-2) are not expected because the available length
2172 * is tested.
2173 * Other unknown error are also not expected.
2174 */
2175 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002176 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002177 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002178
sada05ed3302018-05-11 11:48:18 -07002179 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002180 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002181 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002182 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002183 return 1;
2184 }
2185
2186 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002187 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002188
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002189 s->req.rex = TICK_ETERNITY;
2190 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002191
2192 /* Update length sent. */
2193 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002194 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002195
2196 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002197 if (sent + len >= buf_len) {
2198 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002199 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002200 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002201
2202hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002203 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2204 xref_unlock(&socket->xref, peer);
2205 WILL_LJMP(luaL_error(L, "out of memory"));
2206 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002207 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002208 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002209 return 0;
2210}
2211
2212/* This function initiate the send of data. It just check the input
2213 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002214 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002215 * "hlua_socket_write_yield" that can yield.
2216 *
2217 * The Lua function gets between 3 and 4 parameters. The first one is
2218 * the associated object. The second is a string buffer. The third is
2219 * a facultative integer that represents where is the buffer position
2220 * of the start of the data that can send. The first byte is the
2221 * position "1". The default value is "1". The fourth argument is a
2222 * facultative integer that represents where is the buffer position
2223 * of the end of the data that can send. The default is the last byte.
2224 */
2225static int hlua_socket_send(struct lua_State *L)
2226{
2227 int i;
2228 int j;
2229 const char *buf;
2230 size_t buf_len;
2231
2232 /* Check number of arguments. */
2233 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2234 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2235
2236 /* Get the string. */
2237 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2238
2239 /* Get and check j. */
2240 if (lua_gettop(L) == 4) {
2241 j = MAY_LJMP(luaL_checkinteger(L, 4));
2242 if (j < 0)
2243 j = buf_len + j + 1;
2244 if (j > buf_len)
2245 j = buf_len + 1;
2246 lua_pop(L, 1);
2247 }
2248 else
2249 j = buf_len;
2250
2251 /* Get and check i. */
2252 if (lua_gettop(L) == 3) {
2253 i = MAY_LJMP(luaL_checkinteger(L, 3));
2254 if (i < 0)
2255 i = buf_len + i + 1;
2256 if (i > buf_len)
2257 i = buf_len + 1;
2258 lua_pop(L, 1);
2259 } else
2260 i = 1;
2261
2262 /* Check bth i and j. */
2263 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002264 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002265 return 1;
2266 }
2267 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002268 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002269 return 1;
2270 }
2271 if (i == 0)
2272 i = 1;
2273 if (j == 0)
2274 j = 1;
2275
2276 /* Pop the string. */
2277 lua_pop(L, 1);
2278
2279 /* Update the buffer length. */
2280 buf += i - 1;
2281 buf_len = j - i + 1;
2282 lua_pushlstring(L, buf, buf_len);
2283
2284 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002285 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002286
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002287 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002288}
2289
Willy Tarreau22b0a682015-06-17 19:43:49 +02002290#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002291__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2292{
2293 static char buffer[SOCKET_INFO_MAX_LEN];
2294 int ret;
2295 int len;
2296 char *p;
2297
2298 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2299 if (ret <= 0) {
2300 lua_pushnil(L);
2301 return 1;
2302 }
2303
2304 if (ret == AF_UNIX) {
2305 lua_pushstring(L, buffer+1);
2306 return 1;
2307 }
2308 else if (ret == AF_INET6) {
2309 buffer[0] = '[';
2310 len = strlen(buffer);
2311 buffer[len] = ']';
2312 len++;
2313 buffer[len] = ':';
2314 len++;
2315 p = buffer;
2316 }
2317 else if (ret == AF_INET) {
2318 p = buffer + 1;
2319 len = strlen(p);
2320 p[len] = ':';
2321 len++;
2322 }
2323 else {
2324 lua_pushnil(L);
2325 return 1;
2326 }
2327
2328 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2329 lua_pushnil(L);
2330 return 1;
2331 }
2332
2333 lua_pushstring(L, p);
2334 return 1;
2335}
2336
2337/* Returns information about the peer of the connection. */
2338__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2339{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002340 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002341 struct xref *peer;
2342 struct appctx *appctx;
2343 struct stream_interface *si;
2344 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002345 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002346
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002347 MAY_LJMP(check_args(L, 1, "getpeername"));
2348
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002349 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002351 /* Check if we run on the same thread than the xreator thread.
2352 * We cannot access to the socket if the thread is different.
2353 */
2354 if (socket->tid != tid)
2355 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2356
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002357 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002358 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002359 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002360 lua_pushnil(L);
2361 return 1;
2362 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002363 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2364 si = appctx->owner;
2365 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002366
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002367 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002368 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002369 lua_pushnil(L);
2370 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002371 }
2372
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002373 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002374 xref_unlock(&socket->xref, peer);
2375 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002376}
2377
2378/* Returns information about my connection side. */
2379static int hlua_socket_getsockname(struct lua_State *L)
2380{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002381 struct hlua_socket *socket;
2382 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002383 struct appctx *appctx;
2384 struct xref *peer;
2385 struct stream_interface *si;
2386 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002387 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002388
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389 MAY_LJMP(check_args(L, 1, "getsockname"));
2390
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002391 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002392
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002393 /* Check if we run on the same thread than the xreator thread.
2394 * We cannot access to the socket if the thread is different.
2395 */
2396 if (socket->tid != tid)
2397 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2398
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002399 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002400 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002401 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002402 lua_pushnil(L);
2403 return 1;
2404 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002405 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2406 si = appctx->owner;
2407 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002408
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002409 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002410 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002411 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002412 lua_pushnil(L);
2413 return 1;
2414 }
2415
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002416 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002417 xref_unlock(&socket->xref, peer);
2418 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002419}
2420
2421/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002422static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002423 .obj_type = OBJ_TYPE_APPLET,
2424 .name = "<LUA_TCP>",
2425 .fct = hlua_socket_handler,
2426 .release = hlua_socket_release,
2427};
2428
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002429__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002430{
2431 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2432 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002433 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002434 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002435 struct stream_interface *si;
2436 struct stream *s;
2437
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002438 /* Check if we run on the same thread than the xreator thread.
2439 * We cannot access to the socket if the thread is different.
2440 */
2441 if (socket->tid != tid)
2442 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2443
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002444 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002445 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002446 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002447 lua_pushnil(L);
2448 lua_pushstring(L, "Can't connect");
2449 return 2;
2450 }
2451 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2452 si = appctx->owner;
2453 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002454
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002455 /* Check if we run on the same thread than the xreator thread.
2456 * We cannot access to the socket if the thread is different.
2457 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002458 if (socket->tid != tid) {
2459 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002460 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002461 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002462
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002463 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002464 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002465 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002466 lua_pushnil(L);
2467 lua_pushstring(L, "Can't connect");
2468 return 2;
2469 }
2470
Willy Tarreaue09101e2018-10-16 17:37:12 +02002471 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002472
2473 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002474 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002475 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002476 lua_pushinteger(L, 1);
2477 return 1;
2478 }
2479
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002480 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2481 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002482 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002483 }
2484 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002485 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002486 return 0;
2487}
2488
2489/* This function fail or initite the connection. */
2490__LJMP static int hlua_socket_connect(struct lua_State *L)
2491{
2492 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002493 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002494 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002495 struct hlua *hlua;
2496 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002497 int low, high;
2498 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002499 struct xref *peer;
2500 struct stream_interface *si;
2501 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002502
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002503 if (lua_gettop(L) < 2)
2504 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002505
2506 /* Get args. */
2507 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002508
2509 /* Check if we run on the same thread than the xreator thread.
2510 * We cannot access to the socket if the thread is different.
2511 */
2512 if (socket->tid != tid)
2513 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2514
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002515 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002516 if (lua_gettop(L) >= 3) {
2517 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002518 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002519
Tim Duesterhus6edab862018-01-06 19:04:45 +01002520 /* Force the ip to end with a colon, to support IPv6 addresses
2521 * that are not enclosed within square brackets.
2522 */
2523 if (port > 0) {
2524 luaL_buffinit(L, &b);
2525 luaL_addstring(&b, ip);
2526 luaL_addchar(&b, ':');
2527 luaL_pushresult(&b);
2528 ip = lua_tolstring(L, lua_gettop(L), NULL);
2529 }
2530 }
2531
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002532 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002533 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002534 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002535 lua_pushnil(L);
2536 return 1;
2537 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002538
2539 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002540 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 +02002541 if (!addr) {
2542 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002543 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002544 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002545
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002546 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002547 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002548 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002549 if (port == -1) {
2550 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002551 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002552 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002553 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2554 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002555 if (port == -1) {
2556 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002557 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002558 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002559 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002560 }
2561 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002562
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002563 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2564 si = appctx->owner;
2565 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002566
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002567 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002568 xref_unlock(&socket->xref, peer);
2569 WILL_LJMP(luaL_error(L, "connect: internal error"));
2570 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002571 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002572
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002573 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002574
2575 /* inform the stream that we want to be notified whenever the
2576 * connection completes.
2577 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002578 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002579 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002580 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002581
Willy Tarreauf31af932020-01-14 09:59:38 +01002582 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002583
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002584 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2585 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002586 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002587 }
2588 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002589
2590 task_wakeup(s->task, TASK_WOKEN_INIT);
2591 /* Return yield waiting for connection. */
2592
Willy Tarreau9635e032018-10-16 17:52:55 +02002593 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002594
2595 return 0;
2596}
2597
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002598#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002599__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2600{
2601 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002602 struct xref *peer;
2603 struct appctx *appctx;
2604 struct stream_interface *si;
2605 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606
2607 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2608 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002609
2610 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002611 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002612 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002613 lua_pushnil(L);
2614 return 1;
2615 }
2616 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2617 si = appctx->owner;
2618 s = si_strm(si);
2619
2620 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002621 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002622 return MAY_LJMP(hlua_socket_connect(L));
2623}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002624#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002625
2626__LJMP static int hlua_socket_setoption(struct lua_State *L)
2627{
2628 return 0;
2629}
2630
2631__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2632{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002633 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002634 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002635 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002636 struct xref *peer;
2637 struct appctx *appctx;
2638 struct stream_interface *si;
2639 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002640
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002641 MAY_LJMP(check_args(L, 2, "settimeout"));
2642
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002643 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002644
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002645 /* convert the timeout to millis */
2646 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002647
Thierry Fournier17a921b2018-03-08 09:59:02 +01002648 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002649 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002650 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2651
Mark Lakes56cc1252018-03-27 09:48:06 +02002652 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002653 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002654
2655 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002656 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002657 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002658
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002659 /* Check if we run on the same thread than the xreator thread.
2660 * We cannot access to the socket if the thread is different.
2661 */
2662 if (socket->tid != tid)
2663 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2664
Mark Lakes56cc1252018-03-27 09:48:06 +02002665 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002666 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002667 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002668 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2669 WILL_LJMP(lua_error(L));
2670 return 0;
2671 }
2672 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2673 si = appctx->owner;
2674 s = si_strm(si);
2675
Cyril Bonté7bb63452018-08-17 23:51:02 +02002676 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002677 s->req.rto = tmout;
2678 s->req.wto = tmout;
2679 s->res.rto = tmout;
2680 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002681 s->req.rex = tick_add_ifset(now_ms, tmout);
2682 s->req.wex = tick_add_ifset(now_ms, tmout);
2683 s->res.rex = tick_add_ifset(now_ms, tmout);
2684 s->res.wex = tick_add_ifset(now_ms, tmout);
2685
2686 s->task->expire = tick_add_ifset(now_ms, tmout);
2687 task_queue(s->task);
2688
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002689 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002690
Thierry Fourniere9636f12018-03-08 09:54:32 +01002691 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002692 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002693}
2694
2695__LJMP static int hlua_socket_new(lua_State *L)
2696{
2697 struct hlua_socket *socket;
2698 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002699 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002700 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002701
2702 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002703 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002704 hlua_pusherror(L, "socket: full stack");
2705 goto out_fail_conf;
2706 }
2707
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002708 /* Create the object: obj[0] = userdata. */
2709 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002710 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002711 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002712 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002713 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002714
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002715 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002716 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002717 hlua_pusherror(L, "socket: uninitialized pools.");
2718 goto out_fail_conf;
2719 }
2720
Willy Tarreau87b09662015-04-03 00:22:06 +02002721 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002722 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2723 lua_setmetatable(L, -2);
2724
Willy Tarreaud420a972015-04-06 00:39:18 +02002725 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002726 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002727 if (!appctx) {
2728 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002729 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002730 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002731
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002732 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002733 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002734 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2735 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002736
Willy Tarreaud420a972015-04-06 00:39:18 +02002737 /* Now create a session, task and stream for this applet */
2738 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2739 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002740 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002741 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002742 }
2743
Willy Tarreau87787ac2017-08-28 16:22:54 +02002744 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002745 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002746 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002747 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002748 }
2749
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002750 /* Initialise cross reference between stream and Lua socket object. */
2751 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002752
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002753 /* Configure "right" stream interface. this "si" is used to connect
2754 * and retrieve data from the server. The connection is initialized
2755 * with the "struct server".
2756 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002757 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002758
2759 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002760 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002761 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002762
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002763 return 1;
2764
Willy Tarreaud420a972015-04-06 00:39:18 +02002765 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002766 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002767 out_fail_sess:
2768 appctx_free(appctx);
2769 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002770 WILL_LJMP(lua_error(L));
2771 return 0;
2772}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002773
2774/*
2775 *
2776 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002777 * Class Channel
2778 *
2779 *
2780 */
2781
2782/* Returns the struct hlua_channel join to the class channel in the
2783 * stack entry "ud" or throws an argument error.
2784 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002785__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002786{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002787 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002788}
2789
Willy Tarreau47860ed2015-03-10 14:07:50 +01002790/* Pushes the channel onto the top of the stack. If the stask does not have a
2791 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002792 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002793static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002794{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002795 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002796 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002797 return 0;
2798
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002799 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002800 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002801 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002802
2803 /* Pop a class sesison metatable and affect it to the userdata. */
2804 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2805 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002806 return 1;
2807}
2808
2809/* Duplicate all the data present in the input channel and put it
2810 * in a string LUA variables. Returns -1 and push a nil value in
2811 * the stack if the channel is closed and all the data are consumed,
2812 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002813 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002814 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002815static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002816{
2817 char *blk1;
2818 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002819 size_t len1;
2820 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002821 int ret;
2822 luaL_Buffer b;
2823
Willy Tarreau06d80a92017-10-19 14:32:15 +02002824 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002825 if (unlikely(ret == 0))
2826 return 0;
2827
2828 if (unlikely(ret < 0)) {
2829 lua_pushnil(L);
2830 return -1;
2831 }
2832
2833 luaL_buffinit(L, &b);
2834 luaL_addlstring(&b, blk1, len1);
2835 if (unlikely(ret == 2))
2836 luaL_addlstring(&b, blk2, len2);
2837 luaL_pushresult(&b);
2838
2839 if (unlikely(ret == 2))
2840 return len1 + len2;
2841 return len1;
2842}
2843
2844/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2845 * a yield. This function keep the data in the buffer.
2846 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002847__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002848{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002849 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002850
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002851 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2852
Thierry Fournier77016da2020-08-15 14:35:51 +02002853 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2854 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002855 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002856 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002857
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002858 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002859 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002860 return 1;
2861}
2862
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002863/* Check arguments for the function "hlua_channel_dup_yield". */
2864__LJMP static int hlua_channel_dup(lua_State *L)
2865{
2866 MAY_LJMP(check_args(L, 1, "dup"));
2867 MAY_LJMP(hlua_checkchannel(L, 1));
2868 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2869}
2870
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002871/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2872 * a yield. This function consumes the data in the buffer. It returns
2873 * a string containing the data or a nil pointer if no data are available
2874 * and the channel is closed.
2875 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002876__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002877{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002878 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002879 int ret;
2880
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002881 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002882
Thierry Fournier77016da2020-08-15 14:35:51 +02002883 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2884 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002885 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002886 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002887
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002888 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002889 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002890 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002891
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002892 if (unlikely(ret == -1))
2893 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002894
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002895 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002896 return 1;
2897}
2898
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002899/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002900__LJMP static int hlua_channel_get(lua_State *L)
2901{
2902 MAY_LJMP(check_args(L, 1, "get"));
2903 MAY_LJMP(hlua_checkchannel(L, 1));
2904 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2905}
2906
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002907/* This functions consumes and returns one line. If the channel is closed,
2908 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002909 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002910 * value.
2911 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002912__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002913{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002914 char *blk1;
2915 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002916 size_t len1;
2917 size_t len2;
2918 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002919 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002920 int ret;
2921 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002922
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002923 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2924
Thierry Fournier77016da2020-08-15 14:35:51 +02002925 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2926 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002927 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002928 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002929
Willy Tarreau06d80a92017-10-19 14:32:15 +02002930 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002931 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002932 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002933
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002934 if (ret == -1) {
2935 lua_pushnil(L);
2936 return 1;
2937 }
2938
2939 luaL_buffinit(L, &b);
2940 luaL_addlstring(&b, blk1, len1);
2941 len = len1;
2942 if (unlikely(ret == 2)) {
2943 luaL_addlstring(&b, blk2, len2);
2944 len += len2;
2945 }
2946 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002947 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002948 return 1;
2949}
2950
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002951/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002952__LJMP static int hlua_channel_getline(lua_State *L)
2953{
2954 MAY_LJMP(check_args(L, 1, "getline"));
2955 MAY_LJMP(hlua_checkchannel(L, 1));
2956 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2957}
2958
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002959/* This function takes a string as input, and append it at the
2960 * input side of channel. If the data is too big, but a space
2961 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002962 * yields. If the data is bigger than the buffer, or if the
2963 * channel is closed, it returns -1. Otherwise, it returns the
2964 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002965 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002966__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002967{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002968 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002969 size_t len;
2970 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2971 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2972 int ret;
2973 int max;
2974
Thierry Fournier77016da2020-08-15 14:35:51 +02002975 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2976 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002977 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002978 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002979
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002980 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002981 * the request buffer if its not required.
2982 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002983 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002984 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002985 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002986 }
2987
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002988 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002989 if (max > len - l)
2990 max = len - l;
2991
Willy Tarreau06d80a92017-10-19 14:32:15 +02002992 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002993 if (ret == -2 || ret == -3) {
2994 lua_pushinteger(L, -1);
2995 return 1;
2996 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002997 if (ret == -1) {
2998 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002999 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01003000 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003001 l += ret;
3002 lua_pop(L, 1);
3003 lua_pushinteger(L, l);
3004
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003005 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003006 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003007 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003008 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003009 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010 */
3011 return 1;
3012 }
3013 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003014 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003015 return 1;
3016}
3017
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003018/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3019 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003020 * buffer size is too little for the data.
3021 */
3022__LJMP static int hlua_channel_append(lua_State *L)
3023{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003024 size_t len;
3025
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003026 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003027 MAY_LJMP(hlua_checkchannel(L, 1));
3028 MAY_LJMP(luaL_checklstring(L, 2, &len));
3029 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003030 lua_pushinteger(L, 0);
3031
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003032 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003033}
3034
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003035/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003036 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003037 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003038 * or -1 if the channel is closed or if the buffer size is too
3039 * little for the data.
3040 */
3041__LJMP static int hlua_channel_set(lua_State *L)
3042{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003043 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003044
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003045 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003046 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003047 lua_pushinteger(L, 0);
3048
Thierry Fournier77016da2020-08-15 14:35:51 +02003049 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3050 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003051 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003052 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003053
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003054 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003055
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003056 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003057}
3058
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003059/* Append data in the output side of the buffer. This data is immediately
3060 * sent. The function returns the amount of data written. If the buffer
3061 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003062 * if the channel is closed.
3063 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003064__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003065{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003066 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003067 size_t len;
3068 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3069 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3070 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003071 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003072
Thierry Fournier77016da2020-08-15 14:35:51 +02003073 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3074 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003075 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003076 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003077
Willy Tarreau47860ed2015-03-10 14:07:50 +01003078 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003079 lua_pushinteger(L, -1);
3080 return 1;
3081 }
3082
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003083 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003084 * the request buffer if its not required.
3085 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003086 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003087 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003088 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003089 }
3090
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003091 /* The written data will be immediately sent, so we can check
3092 * the available space without taking in account the reserve.
3093 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003094 * data, because the buffer will be flushed.
3095 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003096 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003097
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003098 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003099 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003100 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003101 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003102 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003103 return 1;
3104
3105 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003106 if (max > len - l)
3107 max = len - l;
3108
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003109 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003110 * detects a non contiguous buffer and realign it.
3111 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003112 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003113 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003114
3115 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003116 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003117
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003118 /* buffer replace considers that the input part is filled.
3119 * so, I must forward these new data in the output part.
3120 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003121 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003122
3123 l += max;
3124 lua_pop(L, 1);
3125 lua_pushinteger(L, l);
3126
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003127 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003128 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003129 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003130 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003131 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003132 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003133 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003134
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003135 if (l < len) {
3136 /* If we are waiting for space in the response buffer, we
3137 * must set the flag WAKERESWR. This flag required the task
3138 * wake up if any activity is detected on the response buffer.
3139 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003140 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003141 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003142 else
3143 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003144 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003145 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003146
3147 return 1;
3148}
3149
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003150/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003151 * yield the LUA process, and resume it without checking the
3152 * input arguments.
3153 */
3154__LJMP static int hlua_channel_send(lua_State *L)
3155{
3156 MAY_LJMP(check_args(L, 2, "send"));
3157 lua_pushinteger(L, 0);
3158
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003159 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003160}
3161
3162/* This function forward and amount of butes. The data pass from
3163 * the input side of the buffer to the output side, and can be
3164 * forwarded. This function never fails.
3165 *
3166 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003167 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003168 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003169__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003170{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003171 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003172 int len;
3173 int l;
3174 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003175 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003176
3177 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003178
Thierry Fournier77016da2020-08-15 14:35:51 +02003179 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3180 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003181 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003182 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003183
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003184 len = MAY_LJMP(luaL_checkinteger(L, 2));
3185 l = MAY_LJMP(luaL_checkinteger(L, -1));
3186
3187 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003188 if (max > ci_data(chn))
3189 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003190 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003191 l += max;
3192
3193 lua_pop(L, 1);
3194 lua_pushinteger(L, l);
3195
3196 /* Check if it miss bytes to forward. */
3197 if (l < len) {
3198 /* The the input channel or the output channel are closed, we
3199 * must return the amount of data forwarded.
3200 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003201 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003202 return 1;
3203
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003204 /* If we are waiting for space data in the response buffer, we
3205 * must set the flag WAKERESWR. This flag required the task
3206 * wake up if any activity is detected on the response buffer.
3207 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003208 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003209 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003210 else
3211 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003212
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003213 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003214 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003215 }
3216
3217 return 1;
3218}
3219
3220/* Just check the input and prepare the stack for the previous
3221 * function "hlua_channel_forward_yield"
3222 */
3223__LJMP static int hlua_channel_forward(lua_State *L)
3224{
3225 MAY_LJMP(check_args(L, 2, "forward"));
3226 MAY_LJMP(hlua_checkchannel(L, 1));
3227 MAY_LJMP(luaL_checkinteger(L, 2));
3228
3229 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003230 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003231}
3232
3233/* Just returns the number of bytes available in the input
3234 * side of the buffer. This function never fails.
3235 */
3236__LJMP static int hlua_channel_get_in_len(lua_State *L)
3237{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003238 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003239
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003240 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003241 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003242 if (IS_HTX_STRM(chn_strm(chn))) {
3243 struct htx *htx = htxbuf(&chn->buf);
3244 lua_pushinteger(L, htx->data - co_data(chn));
3245 }
3246 else
3247 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003248 return 1;
3249}
3250
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003251/* Returns true if the channel is full. */
3252__LJMP static int hlua_channel_is_full(lua_State *L)
3253{
3254 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003255
3256 MAY_LJMP(check_args(L, 1, "is_full"));
3257 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003258 /* ignore the reserve, we are not on a producer side (ie in an
3259 * applet).
3260 */
3261 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003262 return 1;
3263}
3264
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003265/* Returns true if the channel is the response channel. */
3266__LJMP static int hlua_channel_is_resp(lua_State *L)
3267{
3268 struct channel *chn;
3269
3270 MAY_LJMP(check_args(L, 1, "is_resp"));
3271 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3272
3273 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3274 return 1;
3275}
3276
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003277/* Just returns the number of bytes available in the output
3278 * side of the buffer. This function never fails.
3279 */
3280__LJMP static int hlua_channel_get_out_len(lua_State *L)
3281{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003282 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003283
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003284 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003285 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003286 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003287 return 1;
3288}
3289
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003290/*
3291 *
3292 *
3293 * Class Fetches
3294 *
3295 *
3296 */
3297
3298/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003299 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003300 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003301__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003302{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003303 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003304}
3305
3306/* This function creates and push in the stack a fetch object according
3307 * with a current TXN.
3308 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003309static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003310{
Willy Tarreau7073c472015-04-06 11:15:40 +02003311 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003312
3313 /* Check stack size. */
3314 if (!lua_checkstack(L, 3))
3315 return 0;
3316
3317 /* Create the object: obj[0] = userdata.
3318 * Note that the base of the Fetches object is the
3319 * transaction object.
3320 */
3321 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003322 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003323 lua_rawseti(L, -2, 0);
3324
Willy Tarreau7073c472015-04-06 11:15:40 +02003325 hsmp->s = txn->s;
3326 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003327 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003328 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003329
3330 /* Pop a class sesison metatable and affect it to the userdata. */
3331 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3332 lua_setmetatable(L, -2);
3333
3334 return 1;
3335}
3336
3337/* This function is an LUA binding. It is called with each sample-fetch.
3338 * It uses closure argument to store the associated sample-fetch. It
3339 * returns only one argument or throws an error. An error is thrown
3340 * only if an error is encountered during the argument parsing. If
3341 * the "sample-fetch" function fails, nil is returned.
3342 */
3343__LJMP static int hlua_run_sample_fetch(lua_State *L)
3344{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003345 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003346 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003347 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003348 int i;
3349 struct sample smp;
3350
3351 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003352 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003353
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003354 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003355 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003356
Thierry FOURNIERca988662015-12-20 18:43:03 +01003357 /* Check execution authorization. */
3358 if (f->use & SMP_USE_HTTP_ANY &&
3359 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3360 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3361 "is not available in Lua services", f->kw);
3362 WILL_LJMP(lua_error(L));
3363 }
3364
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003365 /* Get extra arguments. */
3366 for (i = 0; i < lua_gettop(L) - 1; i++) {
3367 if (i >= ARGM_NBARGS)
3368 break;
3369 hlua_lua2arg(L, i + 2, &args[i]);
3370 }
3371 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003372 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003373
3374 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003375 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003376
3377 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003378 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003379 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003380 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003381 }
3382
3383 /* Initialise the sample. */
3384 memset(&smp, 0, sizeof(smp));
3385
3386 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003387 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003388 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003389 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003390 lua_pushstring(L, "");
3391 else
3392 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003393 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003394 }
3395
3396 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003397 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003398 hlua_smp2lua_str(L, &smp);
3399 else
3400 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003401
3402 end:
3403 for (i = 0; args[i].type != ARGT_STOP; i++) {
3404 if (args[i].type == ARGT_STR)
3405 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003406 else if (args[i].type == ARGT_REG)
3407 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003408 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003409 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003410
3411 error:
3412 for (i = 0; args[i].type != ARGT_STOP; i++) {
3413 if (args[i].type == ARGT_STR)
3414 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003415 else if (args[i].type == ARGT_REG)
3416 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003417 }
3418 WILL_LJMP(lua_error(L));
3419 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003420}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003421
3422/*
3423 *
3424 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003425 * Class Converters
3426 *
3427 *
3428 */
3429
3430/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003431 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003432 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003433__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003434{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003435 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003436}
3437
3438/* This function creates and push in the stack a Converters object
3439 * according with a current TXN.
3440 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003441static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003442{
Willy Tarreau7073c472015-04-06 11:15:40 +02003443 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003444
3445 /* Check stack size. */
3446 if (!lua_checkstack(L, 3))
3447 return 0;
3448
3449 /* Create the object: obj[0] = userdata.
3450 * Note that the base of the Converters object is the
3451 * same than the TXN object.
3452 */
3453 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003454 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003455 lua_rawseti(L, -2, 0);
3456
Willy Tarreau7073c472015-04-06 11:15:40 +02003457 hsmp->s = txn->s;
3458 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003459 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003460 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003461
Willy Tarreau87b09662015-04-03 00:22:06 +02003462 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003463 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3464 lua_setmetatable(L, -2);
3465
3466 return 1;
3467}
3468
3469/* This function is an LUA binding. It is called with each converter.
3470 * It uses closure argument to store the associated converter. It
3471 * returns only one argument or throws an error. An error is thrown
3472 * only if an error is encountered during the argument parsing. If
3473 * the converter function function fails, nil is returned.
3474 */
3475__LJMP static int hlua_run_sample_conv(lua_State *L)
3476{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003477 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003478 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003479 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003480 int i;
3481 struct sample smp;
3482
3483 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003484 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003485
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003486 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003487 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003488
3489 /* Get extra arguments. */
3490 for (i = 0; i < lua_gettop(L) - 2; i++) {
3491 if (i >= ARGM_NBARGS)
3492 break;
3493 hlua_lua2arg(L, i + 3, &args[i]);
3494 }
3495 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003496 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003497
3498 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003499 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003500
3501 /* Run the special args checker. */
3502 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3503 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003504 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003505 }
3506
3507 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003508 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003509 if (!hlua_lua2smp(L, 2, &smp)) {
3510 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003511 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003512 }
3513
Willy Tarreau1777ea62016-03-10 16:15:46 +01003514 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3515
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003516 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003517 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003518 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003519 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003520 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003521 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003522 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3523 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003524 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003525 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003526 }
3527
3528 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003529 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003530 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003531 lua_pushstring(L, "");
3532 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003533 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003534 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003535 }
3536
3537 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003538 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003539 hlua_smp2lua_str(L, &smp);
3540 else
3541 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003542 end:
3543 for (i = 0; args[i].type != ARGT_STOP; i++) {
3544 if (args[i].type == ARGT_STR)
3545 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003546 else if (args[i].type == ARGT_REG)
3547 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003548 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003549 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003550
3551 error:
3552 for (i = 0; args[i].type != ARGT_STOP; i++) {
3553 if (args[i].type == ARGT_STR)
3554 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003555 else if (args[i].type == ARGT_REG)
3556 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003557 }
3558 WILL_LJMP(lua_error(L));
3559 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003560}
3561
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003562/*
3563 *
3564 *
3565 * Class AppletTCP
3566 *
3567 *
3568 */
3569
3570/* Returns a struct hlua_txn if the stack entry "ud" is
3571 * a class stream, otherwise it throws an error.
3572 */
3573__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3574{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003575 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003576}
3577
3578/* This function creates and push in the stack an Applet object
3579 * according with a current TXN.
3580 */
3581static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3582{
3583 struct hlua_appctx *appctx;
3584 struct stream_interface *si = ctx->owner;
3585 struct stream *s = si_strm(si);
3586 struct proxy *p = s->be;
3587
3588 /* Check stack size. */
3589 if (!lua_checkstack(L, 3))
3590 return 0;
3591
3592 /* Create the object: obj[0] = userdata.
3593 * Note that the base of the Converters object is the
3594 * same than the TXN object.
3595 */
3596 lua_newtable(L);
3597 appctx = lua_newuserdata(L, sizeof(*appctx));
3598 lua_rawseti(L, -2, 0);
3599 appctx->appctx = ctx;
3600 appctx->htxn.s = s;
3601 appctx->htxn.p = p;
3602
3603 /* Create the "f" field that contains a list of fetches. */
3604 lua_pushstring(L, "f");
3605 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3606 return 0;
3607 lua_settable(L, -3);
3608
3609 /* Create the "sf" field that contains a list of stringsafe fetches. */
3610 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003611 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003612 return 0;
3613 lua_settable(L, -3);
3614
3615 /* Create the "c" field that contains a list of converters. */
3616 lua_pushstring(L, "c");
3617 if (!hlua_converters_new(L, &appctx->htxn, 0))
3618 return 0;
3619 lua_settable(L, -3);
3620
3621 /* Create the "sc" field that contains a list of stringsafe converters. */
3622 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003623 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003624 return 0;
3625 lua_settable(L, -3);
3626
3627 /* Pop a class stream metatable and affect it to the table. */
3628 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3629 lua_setmetatable(L, -2);
3630
3631 return 1;
3632}
3633
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003634__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3635{
3636 struct hlua_appctx *appctx;
3637 struct stream *s;
3638 const char *name;
3639 size_t len;
3640 struct sample smp;
3641
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003642 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3643 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003644
3645 /* It is useles to retrieve the stream, but this function
3646 * runs only in a stream context.
3647 */
3648 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3649 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3650 s = appctx->htxn.s;
3651
3652 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003653 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003654 hlua_lua2smp(L, 3, &smp);
3655
3656 /* Store the sample in a variable. */
3657 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003658
3659 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3660 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3661 else
3662 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3663
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003664 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003665}
3666
3667__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3668{
3669 struct hlua_appctx *appctx;
3670 struct stream *s;
3671 const char *name;
3672 size_t len;
3673 struct sample smp;
3674
3675 MAY_LJMP(check_args(L, 2, "unset_var"));
3676
3677 /* It is useles to retrieve the stream, but this function
3678 * runs only in a stream context.
3679 */
3680 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3681 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3682 s = appctx->htxn.s;
3683
3684 /* Unset the variable. */
3685 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003686 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3687 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003688}
3689
3690__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3691{
3692 struct hlua_appctx *appctx;
3693 struct stream *s;
3694 const char *name;
3695 size_t len;
3696 struct sample smp;
3697
3698 MAY_LJMP(check_args(L, 2, "get_var"));
3699
3700 /* It is useles to retrieve the stream, but this function
3701 * runs only in a stream context.
3702 */
3703 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3704 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3705 s = appctx->htxn.s;
3706
3707 smp_set_owner(&smp, s->be, s->sess, s, 0);
3708 if (!vars_get_by_name(name, len, &smp)) {
3709 lua_pushnil(L);
3710 return 1;
3711 }
3712
3713 return hlua_smp2lua(L, &smp);
3714}
3715
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003716__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3717{
3718 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3719 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003720 struct hlua *hlua;
3721
3722 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003723 if (!s->hlua)
3724 return 0;
3725 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003726
3727 MAY_LJMP(check_args(L, 2, "set_priv"));
3728
3729 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003730 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003731
3732 /* Get and store new value. */
3733 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3734 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3735
3736 return 0;
3737}
3738
3739__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3740{
3741 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3742 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003743 struct hlua *hlua;
3744
3745 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003746 if (!s->hlua) {
3747 lua_pushnil(L);
3748 return 1;
3749 }
3750 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003751
3752 /* Push configuration index in the stack. */
3753 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3754
3755 return 1;
3756}
3757
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003758/* If expected data not yet available, it returns a yield. This function
3759 * consumes the data in the buffer. It returns a string containing the
3760 * data. This string can be empty.
3761 */
3762__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3763{
3764 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3765 struct stream_interface *si = appctx->appctx->owner;
3766 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003767 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003768 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003769 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003770 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003771
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003772 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003773 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003774
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003775 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003776 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003777 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003778 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003779 }
3780
3781 /* End of data: commit the total strings and return. */
3782 if (ret < 0) {
3783 luaL_pushresult(&appctx->b);
3784 return 1;
3785 }
3786
3787 /* Ensure that the block 2 length is usable. */
3788 if (ret == 1)
3789 len2 = 0;
3790
3791 /* dont check the max length read and dont check. */
3792 luaL_addlstring(&appctx->b, blk1, len1);
3793 luaL_addlstring(&appctx->b, blk2, len2);
3794
3795 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003796 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003797 luaL_pushresult(&appctx->b);
3798 return 1;
3799}
3800
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003801/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003802__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3803{
3804 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3805
3806 /* Initialise the string catenation. */
3807 luaL_buffinit(L, &appctx->b);
3808
3809 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3810}
3811
3812/* If expected data not yet available, it returns a yield. This function
3813 * consumes the data in the buffer. It returns a string containing the
3814 * data. This string can be empty.
3815 */
3816__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3817{
3818 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3819 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003820 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003821 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003822 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003823 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003824 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003825 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003826
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003827 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003828 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003829
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003830 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003831 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003832 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003833 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003834 }
3835
3836 /* End of data: commit the total strings and return. */
3837 if (ret < 0) {
3838 luaL_pushresult(&appctx->b);
3839 return 1;
3840 }
3841
3842 /* Ensure that the block 2 length is usable. */
3843 if (ret == 1)
3844 len2 = 0;
3845
3846 if (len == -1) {
3847
3848 /* If len == -1, catenate all the data avalaile and
3849 * yield because we want to get all the data until
3850 * the end of data stream.
3851 */
3852 luaL_addlstring(&appctx->b, blk1, len1);
3853 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003854 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003855 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003856 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003857
3858 } else {
3859
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003860 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003861 if (len1 > len)
3862 len1 = len;
3863 luaL_addlstring(&appctx->b, blk1, len1);
3864 len -= len1;
3865
3866 /* Copy the second block. */
3867 if (len2 > len)
3868 len2 = len;
3869 luaL_addlstring(&appctx->b, blk2, len2);
3870 len -= len2;
3871
3872 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003873 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003874
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003875 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003876 if (len > 0) {
3877 lua_pushinteger(L, len);
3878 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003879 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003880 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003881 }
3882
3883 /* return the result. */
3884 luaL_pushresult(&appctx->b);
3885 return 1;
3886 }
3887
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003888 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003889 hlua_pusherror(L, "Lua: internal error");
3890 WILL_LJMP(lua_error(L));
3891 return 0;
3892}
3893
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003894/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003895__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3896{
3897 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3898 int len = -1;
3899
3900 if (lua_gettop(L) > 2)
3901 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3902 if (lua_gettop(L) >= 2) {
3903 len = MAY_LJMP(luaL_checkinteger(L, 2));
3904 lua_pop(L, 1);
3905 }
3906
3907 /* Confirm or set the required length */
3908 lua_pushinteger(L, len);
3909
3910 /* Initialise the string catenation. */
3911 luaL_buffinit(L, &appctx->b);
3912
3913 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3914}
3915
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003916/* Append data in the output side of the buffer. This data is immediately
3917 * sent. The function returns the amount of data written. If the buffer
3918 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003919 * if the channel is closed.
3920 */
3921__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3922{
3923 size_t len;
3924 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3925 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3926 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3927 struct stream_interface *si = appctx->appctx->owner;
3928 struct channel *chn = si_ic(si);
3929 int max;
3930
3931 /* Get the max amount of data which can write as input in the channel. */
3932 max = channel_recv_max(chn);
3933 if (max > (len - l))
3934 max = len - l;
3935
3936 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003937 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003938
3939 /* update counters. */
3940 l += max;
3941 lua_pop(L, 1);
3942 lua_pushinteger(L, l);
3943
3944 /* If some data is not send, declares the situation to the
3945 * applet, and returns a yield.
3946 */
3947 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003948 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003949 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003950 }
3951
3952 return 1;
3953}
3954
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003955/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003956 * yield the LUA process, and resume it without checking the
3957 * input arguments.
3958 */
3959__LJMP static int hlua_applet_tcp_send(lua_State *L)
3960{
3961 MAY_LJMP(check_args(L, 2, "send"));
3962 lua_pushinteger(L, 0);
3963
3964 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3965}
3966
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003967/*
3968 *
3969 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003970 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003971 *
3972 *
3973 */
3974
3975/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003976 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003977 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003978__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003979{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003980 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003981}
3982
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003983/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003984 * according with a current TXN.
3985 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003986static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003987{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003988 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003989 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003990 struct stream_interface *si = ctx->owner;
3991 struct stream *s = si_strm(si);
3992 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003993 struct htx *htx;
3994 struct htx_blk *blk;
3995 struct htx_sl *sl;
3996 struct ist path;
3997 unsigned long long len = 0;
3998 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003999
4000 /* Check stack size. */
4001 if (!lua_checkstack(L, 3))
4002 return 0;
4003
4004 /* Create the object: obj[0] = userdata.
4005 * Note that the base of the Converters object is the
4006 * same than the TXN object.
4007 */
4008 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004009 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004010 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004011 appctx->appctx = ctx;
4012 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004013 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004014 appctx->htxn.s = s;
4015 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004016
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004017 /* Create the "f" field that contains a list of fetches. */
4018 lua_pushstring(L, "f");
4019 if (!hlua_fetches_new(L, &appctx->htxn, 0))
4020 return 0;
4021 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004022
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004023 /* Create the "sf" field that contains a list of stringsafe fetches. */
4024 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004025 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004026 return 0;
4027 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004028
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004029 /* Create the "c" field that contains a list of converters. */
4030 lua_pushstring(L, "c");
4031 if (!hlua_converters_new(L, &appctx->htxn, 0))
4032 return 0;
4033 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004034
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004035 /* Create the "sc" field that contains a list of stringsafe converters. */
4036 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004037 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004038 return 0;
4039 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004040
Christopher Fauleta2097962019-07-15 16:25:33 +02004041 htx = htxbuf(&s->req.buf);
4042 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004043 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004044 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004045
Christopher Fauleta2097962019-07-15 16:25:33 +02004046 /* Stores the request method. */
4047 lua_pushstring(L, "method");
4048 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4049 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004050
Christopher Fauleta2097962019-07-15 16:25:33 +02004051 /* Stores the http version. */
4052 lua_pushstring(L, "version");
4053 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4054 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004055
Christopher Fauleta2097962019-07-15 16:25:33 +02004056 /* creates an array of headers. hlua_http_get_headers() crates and push
4057 * the array on the top of the stack.
4058 */
4059 lua_pushstring(L, "headers");
4060 htxn.s = s;
4061 htxn.p = px;
4062 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004063 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004064 return 0;
4065 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004066
Christopher Fauleta2097962019-07-15 16:25:33 +02004067 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004068 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004069 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004070
Christopher Fauleta2097962019-07-15 16:25:33 +02004071 p = path.ptr;
4072 end = path.ptr + path.len;
4073 q = p;
4074 while (q < end && *q != '?')
4075 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004076
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004077 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004078 lua_pushstring(L, "path");
4079 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004080 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004081
Christopher Fauleta2097962019-07-15 16:25:33 +02004082 /* Stores the query string. */
4083 lua_pushstring(L, "qs");
4084 if (*q == '?')
4085 q++;
4086 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004087 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004088 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004089
Christopher Fauleta2097962019-07-15 16:25:33 +02004090 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4091 struct htx_blk *blk = htx_get_blk(htx, pos);
4092 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004093
Christopher Fauleta2097962019-07-15 16:25:33 +02004094 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
4095 break;
4096 if (type == HTX_BLK_DATA)
4097 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004098 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004099 if (htx->extra != ULLONG_MAX)
4100 len += htx->extra;
4101
4102 /* Stores the request path. */
4103 lua_pushstring(L, "length");
4104 lua_pushinteger(L, len);
4105 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004106
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004107 /* Create an empty array of HTTP request headers. */
4108 lua_pushstring(L, "response");
4109 lua_newtable(L);
4110 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004111
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004112 /* Pop a class stream metatable and affect it to the table. */
4113 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4114 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004115
4116 return 1;
4117}
4118
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004119__LJMP static int hlua_applet_http_set_var(lua_State *L)
4120{
4121 struct hlua_appctx *appctx;
4122 struct stream *s;
4123 const char *name;
4124 size_t len;
4125 struct sample smp;
4126
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004127 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4128 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004129
4130 /* It is useles to retrieve the stream, but this function
4131 * runs only in a stream context.
4132 */
4133 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4134 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4135 s = appctx->htxn.s;
4136
4137 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004138 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004139 hlua_lua2smp(L, 3, &smp);
4140
4141 /* Store the sample in a variable. */
4142 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004143
4144 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4145 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4146 else
4147 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4148
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004149 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004150}
4151
4152__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4153{
4154 struct hlua_appctx *appctx;
4155 struct stream *s;
4156 const char *name;
4157 size_t len;
4158 struct sample smp;
4159
4160 MAY_LJMP(check_args(L, 2, "unset_var"));
4161
4162 /* It is useles to retrieve the stream, but this function
4163 * runs only in a stream context.
4164 */
4165 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4166 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4167 s = appctx->htxn.s;
4168
4169 /* Unset the variable. */
4170 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004171 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4172 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004173}
4174
4175__LJMP static int hlua_applet_http_get_var(lua_State *L)
4176{
4177 struct hlua_appctx *appctx;
4178 struct stream *s;
4179 const char *name;
4180 size_t len;
4181 struct sample smp;
4182
4183 MAY_LJMP(check_args(L, 2, "get_var"));
4184
4185 /* It is useles to retrieve the stream, but this function
4186 * runs only in a stream context.
4187 */
4188 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4189 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4190 s = appctx->htxn.s;
4191
4192 smp_set_owner(&smp, s->be, s->sess, s, 0);
4193 if (!vars_get_by_name(name, len, &smp)) {
4194 lua_pushnil(L);
4195 return 1;
4196 }
4197
4198 return hlua_smp2lua(L, &smp);
4199}
4200
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004201__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4202{
4203 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4204 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004205 struct hlua *hlua;
4206
4207 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004208 if (!s->hlua)
4209 return 0;
4210 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004211
4212 MAY_LJMP(check_args(L, 2, "set_priv"));
4213
4214 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004215 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004216
4217 /* Get and store new value. */
4218 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4219 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4220
4221 return 0;
4222}
4223
4224__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4225{
4226 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4227 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004228 struct hlua *hlua;
4229
4230 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004231 if (!s->hlua) {
4232 lua_pushnil(L);
4233 return 1;
4234 }
4235 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004236
4237 /* Push configuration index in the stack. */
4238 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4239
4240 return 1;
4241}
4242
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004243/* If expected data not yet available, it returns a yield. This function
4244 * consumes the data in the buffer. It returns a string containing the
4245 * data. This string can be empty.
4246 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004247__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004248{
4249 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4250 struct stream_interface *si = appctx->appctx->owner;
4251 struct channel *req = si_oc(si);
4252 struct htx *htx;
4253 struct htx_blk *blk;
4254 size_t count;
4255 int stop = 0;
4256
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004257 htx = htx_from_buf(&req->buf);
4258 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004259 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004260
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004261 while (count && !stop && blk) {
4262 enum htx_blk_type type = htx_get_blk_type(blk);
4263 uint32_t sz = htx_get_blksz(blk);
4264 struct ist v;
4265 uint32_t vlen;
4266 char *nl;
4267
Christopher Faulete461e342018-12-18 16:43:35 +01004268 if (type == HTX_BLK_EOM) {
4269 stop = 1;
4270 break;
4271 }
4272
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004273 vlen = sz;
4274 if (vlen > count) {
4275 if (type != HTX_BLK_DATA)
4276 break;
4277 vlen = count;
4278 }
4279
4280 switch (type) {
4281 case HTX_BLK_UNUSED:
4282 break;
4283
4284 case HTX_BLK_DATA:
4285 v = htx_get_blk_value(htx, blk);
4286 v.len = vlen;
4287 nl = istchr(v, '\n');
4288 if (nl != NULL) {
4289 stop = 1;
4290 vlen = nl - v.ptr + 1;
4291 }
4292 luaL_addlstring(&appctx->b, v.ptr, vlen);
4293 break;
4294
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004295 case HTX_BLK_TLR:
4296 case HTX_BLK_EOM:
4297 stop = 1;
4298 break;
4299
4300 default:
4301 break;
4302 }
4303
4304 co_set_data(req, co_data(req) - vlen);
4305 count -= vlen;
4306 if (sz == vlen)
4307 blk = htx_remove_blk(htx, blk);
4308 else {
4309 htx_cut_data_blk(htx, blk, vlen);
4310 break;
4311 }
4312 }
4313
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004314 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004315 if (!stop) {
4316 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004317 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004318 }
4319
4320 /* return the result. */
4321 luaL_pushresult(&appctx->b);
4322 return 1;
4323}
4324
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004325
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004326/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004327__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004328{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004329 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004330
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004331 /* Initialise the string catenation. */
4332 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004333
Christopher Fauleta2097962019-07-15 16:25:33 +02004334 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004335}
4336
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004337/* If expected data not yet available, it returns a yield. This function
4338 * consumes the data in the buffer. It returns a string containing the
4339 * data. This string can be empty.
4340 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004341__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004342{
4343 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4344 struct stream_interface *si = appctx->appctx->owner;
4345 struct channel *req = si_oc(si);
4346 struct htx *htx;
4347 struct htx_blk *blk;
4348 size_t count;
4349 int len;
4350
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004351 htx = htx_from_buf(&req->buf);
4352 len = MAY_LJMP(luaL_checkinteger(L, 2));
4353 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004354 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004355 while (count && len && blk) {
4356 enum htx_blk_type type = htx_get_blk_type(blk);
4357 uint32_t sz = htx_get_blksz(blk);
4358 struct ist v;
4359 uint32_t vlen;
4360
Christopher Faulete461e342018-12-18 16:43:35 +01004361 if (type == HTX_BLK_EOM) {
4362 len = 0;
4363 break;
4364 }
4365
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004366 vlen = sz;
4367 if (len > 0 && vlen > len)
4368 vlen = len;
4369 if (vlen > count) {
4370 if (type != HTX_BLK_DATA)
4371 break;
4372 vlen = count;
4373 }
4374
4375 switch (type) {
4376 case HTX_BLK_UNUSED:
4377 break;
4378
4379 case HTX_BLK_DATA:
4380 v = htx_get_blk_value(htx, blk);
4381 luaL_addlstring(&appctx->b, v.ptr, vlen);
4382 break;
4383
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004384 case HTX_BLK_TLR:
4385 case HTX_BLK_EOM:
4386 len = 0;
4387 break;
4388
4389 default:
4390 break;
4391 }
4392
4393 co_set_data(req, co_data(req) - vlen);
4394 count -= vlen;
4395 if (len > 0)
4396 len -= vlen;
4397 if (sz == vlen)
4398 blk = htx_remove_blk(htx, blk);
4399 else {
4400 htx_cut_data_blk(htx, blk, vlen);
4401 break;
4402 }
4403 }
4404
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004405 htx_to_buf(htx, &req->buf);
4406
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004407 /* If we are no other data available, yield waiting for new data. */
4408 if (len) {
4409 if (len > 0) {
4410 lua_pushinteger(L, len);
4411 lua_replace(L, 2);
4412 }
4413 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004414 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004415 }
4416
4417 /* return the result. */
4418 luaL_pushresult(&appctx->b);
4419 return 1;
4420}
4421
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004422/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004423__LJMP static int hlua_applet_http_recv(lua_State *L)
4424{
4425 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4426 int len = -1;
4427
4428 /* Check arguments. */
4429 if (lua_gettop(L) > 2)
4430 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4431 if (lua_gettop(L) >= 2) {
4432 len = MAY_LJMP(luaL_checkinteger(L, 2));
4433 lua_pop(L, 1);
4434 }
4435
Christopher Fauleta2097962019-07-15 16:25:33 +02004436 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004437
Christopher Fauleta2097962019-07-15 16:25:33 +02004438 /* Initialise the string catenation. */
4439 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004440
Christopher Fauleta2097962019-07-15 16:25:33 +02004441 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004442}
4443
4444/* Append data in the output side of the buffer. This data is immediately
4445 * sent. The function returns the amount of data written. If the buffer
4446 * cannot contain the data, the function yields. The function returns -1
4447 * if the channel is closed.
4448 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004449__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004450{
4451 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4452 struct stream_interface *si = appctx->appctx->owner;
4453 struct channel *res = si_ic(si);
4454 struct htx *htx = htx_from_buf(&res->buf);
4455 const char *data;
4456 size_t len;
4457 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4458 int max;
4459
Christopher Faulet9060fc02019-07-03 11:39:30 +02004460 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004461 if (!max)
4462 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004463
4464 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4465
4466 /* Get the max amount of data which can write as input in the channel. */
4467 if (max > (len - l))
4468 max = len - l;
4469
4470 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004471 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004472 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004473
4474 /* update counters. */
4475 l += max;
4476 lua_pop(L, 1);
4477 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004478
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004479 /* If some data is not send, declares the situation to the
4480 * applet, and returns a yield.
4481 */
4482 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004483 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004484 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004485 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004486 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004487 }
4488
Christopher Fauleta2097962019-07-15 16:25:33 +02004489 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004490 return 1;
4491}
4492
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004493/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004494 * yield the LUA process, and resume it without checking the
4495 * input arguments.
4496 */
4497__LJMP static int hlua_applet_http_send(lua_State *L)
4498{
4499 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004500
4501 /* We want to send some data. Headers must be sent. */
4502 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4503 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4504 WILL_LJMP(lua_error(L));
4505 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004506
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004507 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004508 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004509
Christopher Fauleta2097962019-07-15 16:25:33 +02004510 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004511}
4512
4513__LJMP static int hlua_applet_http_addheader(lua_State *L)
4514{
4515 const char *name;
4516 int ret;
4517
4518 MAY_LJMP(hlua_checkapplet_http(L, 1));
4519 name = MAY_LJMP(luaL_checkstring(L, 2));
4520 MAY_LJMP(luaL_checkstring(L, 3));
4521
4522 /* Push in the stack the "response" entry. */
4523 ret = lua_getfield(L, 1, "response");
4524 if (ret != LUA_TTABLE) {
4525 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4526 "is expected as an array. %s found", lua_typename(L, ret));
4527 WILL_LJMP(lua_error(L));
4528 }
4529
4530 /* check if the header is already registered if it is not
4531 * the case, register it.
4532 */
4533 ret = lua_getfield(L, -1, name);
4534 if (ret == LUA_TNIL) {
4535
4536 /* Entry not found. */
4537 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4538
4539 /* Insert the new header name in the array in the top of the stack.
4540 * It left the new array in the top of the stack.
4541 */
4542 lua_newtable(L);
4543 lua_pushvalue(L, 2);
4544 lua_pushvalue(L, -2);
4545 lua_settable(L, -4);
4546
4547 } else if (ret != LUA_TTABLE) {
4548
4549 /* corruption error. */
4550 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4551 "is expected as an array. %s found", name, lua_typename(L, ret));
4552 WILL_LJMP(lua_error(L));
4553 }
4554
4555 /* Now the top od thestack is an array of values. We push
4556 * the header value as new entry.
4557 */
4558 lua_pushvalue(L, 3);
4559 ret = lua_rawlen(L, -2);
4560 lua_rawseti(L, -2, ret + 1);
4561 lua_pushboolean(L, 1);
4562 return 1;
4563}
4564
4565__LJMP static int hlua_applet_http_status(lua_State *L)
4566{
4567 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4568 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004569 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004570
4571 if (status < 100 || status > 599) {
4572 lua_pushboolean(L, 0);
4573 return 1;
4574 }
4575
4576 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004577 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004578 lua_pushboolean(L, 1);
4579 return 1;
4580}
4581
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004582
Christopher Fauleta2097962019-07-15 16:25:33 +02004583__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004584{
4585 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4586 struct stream_interface *si = appctx->appctx->owner;
4587 struct channel *res = si_ic(si);
4588 struct htx *htx;
4589 struct htx_sl *sl;
4590 struct h1m h1m;
4591 const char *status, *reason;
4592 const char *name, *value;
4593 size_t nlen, vlen;
4594 unsigned int flags;
4595
4596 /* Send the message at once. */
4597 htx = htx_from_buf(&res->buf);
4598 h1m_init_res(&h1m);
4599
4600 /* Use the same http version than the request. */
4601 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4602 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4603 if (reason == NULL)
4604 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4605 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4606 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4607 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4608 }
4609 else {
4610 flags = HTX_SL_F_IS_RESP;
4611 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4612 }
4613 if (!sl) {
4614 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4615 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4616 WILL_LJMP(lua_error(L));
4617 }
4618 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4619
4620 /* Get the array associated to the field "response" in the object AppletHTTP. */
4621 lua_pushvalue(L, 0);
4622 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4623 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4624 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4625 WILL_LJMP(lua_error(L));
4626 }
4627
4628 /* Browse the list of headers. */
4629 lua_pushnil(L);
4630 while(lua_next(L, -2) != 0) {
4631 /* We expect a string as -2. */
4632 if (lua_type(L, -2) != LUA_TSTRING) {
4633 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4634 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4635 lua_typename(L, lua_type(L, -2)));
4636 WILL_LJMP(lua_error(L));
4637 }
4638 name = lua_tolstring(L, -2, &nlen);
4639
4640 /* We expect an array as -1. */
4641 if (lua_type(L, -1) != LUA_TTABLE) {
4642 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4643 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4644 name,
4645 lua_typename(L, lua_type(L, -1)));
4646 WILL_LJMP(lua_error(L));
4647 }
4648
4649 /* Browse the table who is on the top of the stack. */
4650 lua_pushnil(L);
4651 while(lua_next(L, -2) != 0) {
4652 int id;
4653
4654 /* We expect a number as -2. */
4655 if (lua_type(L, -2) != LUA_TNUMBER) {
4656 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4657 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4658 name,
4659 lua_typename(L, lua_type(L, -2)));
4660 WILL_LJMP(lua_error(L));
4661 }
4662 id = lua_tointeger(L, -2);
4663
4664 /* We expect a string as -2. */
4665 if (lua_type(L, -1) != LUA_TSTRING) {
4666 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4667 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4668 name, id,
4669 lua_typename(L, lua_type(L, -1)));
4670 WILL_LJMP(lua_error(L));
4671 }
4672 value = lua_tolstring(L, -1, &vlen);
4673
4674 /* Simple Protocol checks. */
4675 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004676 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004677 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4678 struct ist v = ist2(value, vlen);
4679 int ret;
4680
4681 ret = h1_parse_cont_len_header(&h1m, &v);
4682 if (ret < 0) {
4683 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4684 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4685 name);
4686 WILL_LJMP(lua_error(L));
4687 }
4688 else if (ret == 0)
4689 goto next; /* Skip it */
4690 }
4691
4692 /* Add a new header */
4693 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4694 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4695 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4696 name);
4697 WILL_LJMP(lua_error(L));
4698 }
4699 next:
4700 /* Remove the array from the stack, and get next element with a remaining string. */
4701 lua_pop(L, 1);
4702 }
4703
4704 /* Remove the array from the stack, and get next element with a remaining string. */
4705 lua_pop(L, 1);
4706 }
4707
4708 if (h1m.flags & H1_MF_CHNK)
4709 h1m.flags &= ~H1_MF_CLEN;
4710 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4711 h1m.flags |= H1_MF_XFER_LEN;
4712
4713 /* Uset HTX start-line flags */
4714 if (h1m.flags & H1_MF_XFER_ENC)
4715 flags |= HTX_SL_F_XFER_ENC;
4716 if (h1m.flags & H1_MF_XFER_LEN) {
4717 flags |= HTX_SL_F_XFER_LEN;
4718 if (h1m.flags & H1_MF_CHNK)
4719 flags |= HTX_SL_F_CHNK;
4720 else if (h1m.flags & H1_MF_CLEN)
4721 flags |= HTX_SL_F_CLEN;
4722 if (h1m.body_len == 0)
4723 flags |= HTX_SL_F_BODYLESS;
4724 }
4725 sl->flags |= flags;
4726
4727 /* If we dont have a content-length set, and the HTTP version is 1.1
4728 * and the status code implies the presence of a message body, we must
4729 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004730 * for the keepalive compliance. If the applet announces a transfer-encoding
4731 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004732 */
4733 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4734 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4735 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4736 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4737 /* Add a new header */
4738 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4739 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4740 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4741 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4742 WILL_LJMP(lua_error(L));
4743 }
4744 }
4745
4746 /* Finalize headers. */
4747 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4748 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4749 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4750 WILL_LJMP(lua_error(L));
4751 }
4752
4753 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4754 b_reset(&res->buf);
4755 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4756 WILL_LJMP(lua_error(L));
4757 }
4758
4759 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004760 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004761
4762 /* Headers sent, set the flag. */
4763 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4764 return 0;
4765
4766}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004767/* We will build the status line and the headers of the HTTP response.
4768 * We will try send at once if its not possible, we give back the hand
4769 * waiting for more room.
4770 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004771__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004772{
4773 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4774 struct stream_interface *si = appctx->appctx->owner;
4775 struct channel *res = si_ic(si);
4776
4777 if (co_data(res)) {
4778 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004779 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004780 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004781 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004782}
4783
4784
Christopher Fauleta2097962019-07-15 16:25:33 +02004785__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004786{
Christopher Fauleta2097962019-07-15 16:25:33 +02004787 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004788}
4789
Christopher Fauleta2097962019-07-15 16:25:33 +02004790/*
4791 *
4792 *
4793 * Class HTTP
4794 *
4795 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004796 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004797
4798/* Returns a struct hlua_txn if the stack entry "ud" is
4799 * a class stream, otherwise it throws an error.
4800 */
4801__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004802{
Christopher Fauleta2097962019-07-15 16:25:33 +02004803 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4804}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004805
Christopher Fauleta2097962019-07-15 16:25:33 +02004806/* This function creates and push in the stack a HTTP object
4807 * according with a current TXN.
4808 */
4809static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4810{
4811 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004812
Christopher Fauleta2097962019-07-15 16:25:33 +02004813 /* Check stack size. */
4814 if (!lua_checkstack(L, 3))
4815 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004816
Christopher Fauleta2097962019-07-15 16:25:33 +02004817 /* Create the object: obj[0] = userdata.
4818 * Note that the base of the Converters object is the
4819 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004820 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004821 lua_newtable(L);
4822 htxn = lua_newuserdata(L, sizeof(*htxn));
4823 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004824
4825 htxn->s = txn->s;
4826 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004827 htxn->dir = txn->dir;
4828 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004829
4830 /* Pop a class stream metatable and affect it to the table. */
4831 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4832 lua_setmetatable(L, -2);
4833
4834 return 1;
4835}
4836
4837/* This function creates ans returns an array of HTTP headers.
4838 * This function does not fails. It is used as wrapper with the
4839 * 2 following functions.
4840 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004841__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004842{
Christopher Fauleta2097962019-07-15 16:25:33 +02004843 struct htx *htx;
4844 int32_t pos;
4845
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004846 /* Create the table. */
4847 lua_newtable(L);
4848
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004849
Christopher Fauleta2097962019-07-15 16:25:33 +02004850 htx = htxbuf(&msg->chn->buf);
4851 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4852 struct htx_blk *blk = htx_get_blk(htx, pos);
4853 enum htx_blk_type type = htx_get_blk_type(blk);
4854 struct ist n, v;
4855 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004856
Christopher Fauleta2097962019-07-15 16:25:33 +02004857 if (type == HTX_BLK_HDR) {
4858 n = htx_get_blk_name(htx,blk);
4859 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004860 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004861 else if (type == HTX_BLK_EOH)
4862 break;
4863 else
4864 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004865
Christopher Fauleta2097962019-07-15 16:25:33 +02004866 /* Check for existing entry:
4867 * assume that the table is on the top of the stack, and
4868 * push the key in the stack, the function lua_gettable()
4869 * perform the lookup.
4870 */
4871 lua_pushlstring(L, n.ptr, n.len);
4872 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004873
Christopher Fauleta2097962019-07-15 16:25:33 +02004874 switch (lua_type(L, -1)) {
4875 case LUA_TNIL:
4876 /* Table not found, create it. */
4877 lua_pop(L, 1); /* remove the nil value. */
4878 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4879 lua_newtable(L); /* create and push empty table. */
4880 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4881 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4882 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004883 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004884
Christopher Fauleta2097962019-07-15 16:25:33 +02004885 case LUA_TTABLE:
4886 /* Entry found: push the value in the table. */
4887 len = lua_rawlen(L, -1);
4888 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4889 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4890 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4891 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004892
Christopher Fauleta2097962019-07-15 16:25:33 +02004893 default:
4894 /* Other cases are errors. */
4895 hlua_pusherror(L, "internal error during the parsing of headers.");
4896 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004897 }
4898 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004899 return 1;
4900}
4901
4902__LJMP static int hlua_http_req_get_headers(lua_State *L)
4903{
4904 struct hlua_txn *htxn;
4905
4906 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4907 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4908
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004909 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004910 WILL_LJMP(lua_error(L));
4911
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004912 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004913}
4914
4915__LJMP static int hlua_http_res_get_headers(lua_State *L)
4916{
4917 struct hlua_txn *htxn;
4918
4919 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4920 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4921
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004922 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004923 WILL_LJMP(lua_error(L));
4924
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004925 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004926}
4927
4928/* This function replace full header, or just a value in
4929 * the request or in the response. It is a wrapper fir the
4930 * 4 following functions.
4931 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004932__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004933{
4934 size_t name_len;
4935 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4936 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4937 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004938 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004939 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004940
Dragan Dosen26743032019-04-30 15:54:36 +02004941 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004942 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4943
Christopher Fauleta2097962019-07-15 16:25:33 +02004944 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004945 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004946 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004947 return 0;
4948}
4949
4950__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4951{
4952 struct hlua_txn *htxn;
4953
4954 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4955 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4956
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004957 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004958 WILL_LJMP(lua_error(L));
4959
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004960 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004961}
4962
4963__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4964{
4965 struct hlua_txn *htxn;
4966
4967 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4968 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4969
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004970 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004971 WILL_LJMP(lua_error(L));
4972
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004973 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004974}
4975
4976__LJMP static int hlua_http_req_rep_val(lua_State *L)
4977{
4978 struct hlua_txn *htxn;
4979
4980 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4981 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4982
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004983 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004984 WILL_LJMP(lua_error(L));
4985
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004986 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004987}
4988
4989__LJMP static int hlua_http_res_rep_val(lua_State *L)
4990{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004991 struct hlua_txn *htxn;
4992
4993 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4994 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4995
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004996 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004997 WILL_LJMP(lua_error(L));
4998
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004999 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005000}
5001
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005002/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005003 * It is a wrapper for the 2 following functions.
5004 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005005__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005006{
5007 size_t len;
5008 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005009 struct htx *htx = htxbuf(&msg->chn->buf);
5010 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005011
Christopher Fauleta2097962019-07-15 16:25:33 +02005012 ctx.blk = NULL;
5013 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5014 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005015 return 0;
5016}
5017
5018__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5019{
5020 struct hlua_txn *htxn;
5021
5022 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5023 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5024
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005025 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005026 WILL_LJMP(lua_error(L));
5027
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005028 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005029}
5030
5031__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5032{
5033 struct hlua_txn *htxn;
5034
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005035 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005036 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5037
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005038 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005039 WILL_LJMP(lua_error(L));
5040
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005041 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005042}
5043
5044/* This function adds an header. It is a wrapper used by
5045 * the 2 following functions.
5046 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005047__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005048{
5049 size_t name_len;
5050 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5051 size_t value_len;
5052 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005053 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005054
Christopher Fauleta2097962019-07-15 16:25:33 +02005055 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5056 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005057 return 0;
5058}
5059
5060__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5061{
5062 struct hlua_txn *htxn;
5063
5064 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5065 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5066
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005067 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005068 WILL_LJMP(lua_error(L));
5069
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005070 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005071}
5072
5073__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5074{
5075 struct hlua_txn *htxn;
5076
5077 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5078 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5079
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005080 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005081 WILL_LJMP(lua_error(L));
5082
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005083 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005084}
5085
5086static int hlua_http_req_set_hdr(lua_State *L)
5087{
5088 struct hlua_txn *htxn;
5089
5090 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5091 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5092
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005093 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005094 WILL_LJMP(lua_error(L));
5095
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005096 hlua_http_del_hdr(L, &htxn->s->txn->req);
5097 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005098}
5099
5100static int hlua_http_res_set_hdr(lua_State *L)
5101{
5102 struct hlua_txn *htxn;
5103
5104 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5105 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5106
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005107 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005108 WILL_LJMP(lua_error(L));
5109
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005110 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5111 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005112}
5113
5114/* This function set the method. */
5115static int hlua_http_req_set_meth(lua_State *L)
5116{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005117 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005118 size_t name_len;
5119 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005120
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005121 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005122 WILL_LJMP(lua_error(L));
5123
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005124 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005125 return 1;
5126}
5127
5128/* This function set the method. */
5129static int hlua_http_req_set_path(lua_State *L)
5130{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005131 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005132 size_t name_len;
5133 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005134
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005135 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005136 WILL_LJMP(lua_error(L));
5137
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005138 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005139 return 1;
5140}
5141
5142/* This function set the query-string. */
5143static int hlua_http_req_set_query(lua_State *L)
5144{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005145 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005146 size_t name_len;
5147 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005148
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005149 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005150 WILL_LJMP(lua_error(L));
5151
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005152 /* Check length. */
5153 if (name_len > trash.size - 1) {
5154 lua_pushboolean(L, 0);
5155 return 1;
5156 }
5157
5158 /* Add the mark question as prefix. */
5159 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005160 trash.area[trash.data++] = '?';
5161 memcpy(trash.area + trash.data, name, name_len);
5162 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005163
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005164 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005165 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005166 return 1;
5167}
5168
5169/* This function set the uri. */
5170static int hlua_http_req_set_uri(lua_State *L)
5171{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005172 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005173 size_t name_len;
5174 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005175
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005176 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005177 WILL_LJMP(lua_error(L));
5178
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005179 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005180 return 1;
5181}
5182
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005183/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005184static int hlua_http_res_set_status(lua_State *L)
5185{
5186 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5187 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005188 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5189 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005190
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005191 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005192 WILL_LJMP(lua_error(L));
5193
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005194 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005195 return 0;
5196}
5197
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005198/*
5199 *
5200 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005201 * Class TXN
5202 *
5203 *
5204 */
5205
5206/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005207 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005208 */
5209__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5210{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005211 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005212}
5213
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005214__LJMP static int hlua_set_var(lua_State *L)
5215{
5216 struct hlua_txn *htxn;
5217 const char *name;
5218 size_t len;
5219 struct sample smp;
5220
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005221 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5222 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005223
5224 /* It is useles to retrieve the stream, but this function
5225 * runs only in a stream context.
5226 */
5227 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5228 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5229
5230 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005231 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005232 hlua_lua2smp(L, 3, &smp);
5233
5234 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005235 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005236
5237 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5238 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5239 else
5240 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5241
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005242 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005243}
5244
Christopher Faulet85d79c92016-11-09 16:54:56 +01005245__LJMP static int hlua_unset_var(lua_State *L)
5246{
5247 struct hlua_txn *htxn;
5248 const char *name;
5249 size_t len;
5250 struct sample smp;
5251
5252 MAY_LJMP(check_args(L, 2, "unset_var"));
5253
5254 /* It is useles to retrieve the stream, but this function
5255 * runs only in a stream context.
5256 */
5257 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5258 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5259
5260 /* Unset the variable. */
5261 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005262 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5263 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005264}
5265
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005266__LJMP static int hlua_get_var(lua_State *L)
5267{
5268 struct hlua_txn *htxn;
5269 const char *name;
5270 size_t len;
5271 struct sample smp;
5272
5273 MAY_LJMP(check_args(L, 2, "get_var"));
5274
5275 /* It is useles to retrieve the stream, but this function
5276 * runs only in a stream context.
5277 */
5278 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5279 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5280
Willy Tarreau7560dd42016-03-10 16:28:58 +01005281 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005282 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005283 lua_pushnil(L);
5284 return 1;
5285 }
5286
5287 return hlua_smp2lua(L, &smp);
5288}
5289
Willy Tarreau59551662015-03-10 14:23:13 +01005290__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005291{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005292 struct hlua *hlua;
5293
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005294 MAY_LJMP(check_args(L, 2, "set_priv"));
5295
Willy Tarreau87b09662015-04-03 00:22:06 +02005296 /* It is useles to retrieve the stream, but this function
5297 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005298 */
5299 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005300 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005301
5302 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005303 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005304
5305 /* Get and store new value. */
5306 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5307 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5308
5309 return 0;
5310}
5311
Willy Tarreau59551662015-03-10 14:23:13 +01005312__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005313{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005314 struct hlua *hlua;
5315
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005316 MAY_LJMP(check_args(L, 1, "get_priv"));
5317
Willy Tarreau87b09662015-04-03 00:22:06 +02005318 /* It is useles to retrieve the stream, but this function
5319 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005320 */
5321 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005322 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005323
5324 /* Push configuration index in the stack. */
5325 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5326
5327 return 1;
5328}
5329
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005330/* Create stack entry containing a class TXN. This function
5331 * return 0 if the stack does not contains free slots,
5332 * otherwise it returns 1.
5333 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005334static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005335{
Willy Tarreaude491382015-04-06 11:04:28 +02005336 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005337
5338 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005339 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005340 return 0;
5341
5342 /* NOTE: The allocation never fails. The failure
5343 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005344 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005345 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005346 /* Create the object: obj[0] = userdata. */
5347 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005348 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005349 lua_rawseti(L, -2, 0);
5350
Willy Tarreaude491382015-04-06 11:04:28 +02005351 htxn->s = s;
5352 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005353 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005354 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005355
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005356 /* Create the "f" field that contains a list of fetches. */
5357 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005358 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005359 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005360 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005361
5362 /* Create the "sf" field that contains a list of stringsafe fetches. */
5363 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005364 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005365 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005366 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005367
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005368 /* Create the "c" field that contains a list of converters. */
5369 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005370 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005371 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005372 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005373
5374 /* Create the "sc" field that contains a list of stringsafe converters. */
5375 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005376 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005377 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005378 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005379
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005380 /* Create the "req" field that contains the request channel object. */
5381 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005382 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005383 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005384 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005385
5386 /* Create the "res" field that contains the response channel object. */
5387 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005388 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005389 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005390 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005391
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005392 /* Creates the HTTP object is the current proxy allows http. */
5393 lua_pushstring(L, "http");
5394 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005395 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005396 return 0;
5397 }
5398 else
5399 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005400 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005401
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005402 /* Pop a class sesison metatable and affect it to the userdata. */
5403 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5404 lua_setmetatable(L, -2);
5405
5406 return 1;
5407}
5408
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005409__LJMP static int hlua_txn_deflog(lua_State *L)
5410{
5411 const char *msg;
5412 struct hlua_txn *htxn;
5413
5414 MAY_LJMP(check_args(L, 2, "deflog"));
5415 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5416 msg = MAY_LJMP(luaL_checkstring(L, 2));
5417
5418 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5419 return 0;
5420}
5421
5422__LJMP static int hlua_txn_log(lua_State *L)
5423{
5424 int level;
5425 const char *msg;
5426 struct hlua_txn *htxn;
5427
5428 MAY_LJMP(check_args(L, 3, "log"));
5429 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5430 level = MAY_LJMP(luaL_checkinteger(L, 2));
5431 msg = MAY_LJMP(luaL_checkstring(L, 3));
5432
5433 if (level < 0 || level >= NB_LOG_LEVELS)
5434 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5435
5436 hlua_sendlog(htxn->s->be, level, msg);
5437 return 0;
5438}
5439
5440__LJMP static int hlua_txn_log_debug(lua_State *L)
5441{
5442 const char *msg;
5443 struct hlua_txn *htxn;
5444
5445 MAY_LJMP(check_args(L, 2, "Debug"));
5446 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5447 msg = MAY_LJMP(luaL_checkstring(L, 2));
5448 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5449 return 0;
5450}
5451
5452__LJMP static int hlua_txn_log_info(lua_State *L)
5453{
5454 const char *msg;
5455 struct hlua_txn *htxn;
5456
5457 MAY_LJMP(check_args(L, 2, "Info"));
5458 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5459 msg = MAY_LJMP(luaL_checkstring(L, 2));
5460 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5461 return 0;
5462}
5463
5464__LJMP static int hlua_txn_log_warning(lua_State *L)
5465{
5466 const char *msg;
5467 struct hlua_txn *htxn;
5468
5469 MAY_LJMP(check_args(L, 2, "Warning"));
5470 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5471 msg = MAY_LJMP(luaL_checkstring(L, 2));
5472 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5473 return 0;
5474}
5475
5476__LJMP static int hlua_txn_log_alert(lua_State *L)
5477{
5478 const char *msg;
5479 struct hlua_txn *htxn;
5480
5481 MAY_LJMP(check_args(L, 2, "Alert"));
5482 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5483 msg = MAY_LJMP(luaL_checkstring(L, 2));
5484 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5485 return 0;
5486}
5487
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005488__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5489{
5490 struct hlua_txn *htxn;
5491 int ll;
5492
5493 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5494 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5495 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5496
5497 if (ll < 0 || ll > 7)
5498 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5499
5500 htxn->s->logs.level = ll;
5501 return 0;
5502}
5503
5504__LJMP static int hlua_txn_set_tos(lua_State *L)
5505{
5506 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005507 int tos;
5508
5509 MAY_LJMP(check_args(L, 2, "set_tos"));
5510 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5511 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5512
Willy Tarreau1a18b542018-12-11 16:37:42 +01005513 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005514 return 0;
5515}
5516
5517__LJMP static int hlua_txn_set_mark(lua_State *L)
5518{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005519 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005520 int mark;
5521
5522 MAY_LJMP(check_args(L, 2, "set_mark"));
5523 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5524 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5525
Lukas Tribus579e3e32019-08-11 18:03:45 +02005526 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005527 return 0;
5528}
5529
Patrick Hemmer268a7072018-05-11 12:52:31 -04005530__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5531{
5532 struct hlua_txn *htxn;
5533
5534 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5535 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5536 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5537 return 0;
5538}
5539
5540__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5541{
5542 struct hlua_txn *htxn;
5543
5544 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5545 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5546 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5547 return 0;
5548}
5549
Christopher Faulet700d9e82020-01-31 12:21:52 +01005550/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005551 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005552 * message and terminate the transaction. It returns 1 on success and 0 on
5553 * error. The Reply must be on top of the stack.
5554 */
5555__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5556{
5557 struct htx *htx;
5558 struct htx_sl *sl;
5559 struct h1m h1m;
5560 const char *status, *reason, *body;
5561 size_t status_len, reason_len, body_len;
5562 int ret, code, flags;
5563
5564 code = 200;
5565 status = "200";
5566 status_len = 3;
5567 ret = lua_getfield(L, -1, "status");
5568 if (ret == LUA_TNUMBER) {
5569 code = lua_tointeger(L, -1);
5570 status = lua_tolstring(L, -1, &status_len);
5571 }
5572 lua_pop(L, 1);
5573
5574 reason = http_get_reason(code);
5575 reason_len = strlen(reason);
5576 ret = lua_getfield(L, -1, "reason");
5577 if (ret == LUA_TSTRING)
5578 reason = lua_tolstring(L, -1, &reason_len);
5579 lua_pop(L, 1);
5580
5581 body = NULL;
5582 body_len = 0;
5583 ret = lua_getfield(L, -1, "body");
5584 if (ret == LUA_TSTRING)
5585 body = lua_tolstring(L, -1, &body_len);
5586 lua_pop(L, 1);
5587
5588 /* Prepare the response before inserting the headers */
5589 h1m_init_res(&h1m);
5590 htx = htx_from_buf(&s->res.buf);
5591 channel_htx_truncate(&s->res, htx);
5592 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5593 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5594 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5595 ist2(status, status_len), ist2(reason, reason_len));
5596 }
5597 else {
5598 flags = HTX_SL_F_IS_RESP;
5599 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5600 ist2(status, status_len), ist2(reason, reason_len));
5601 }
5602 if (!sl)
5603 goto fail;
5604 sl->info.res.status = code;
5605
5606 /* Push in the stack the "headers" entry. */
5607 ret = lua_getfield(L, -1, "headers");
5608 if (ret != LUA_TTABLE)
5609 goto skip_headers;
5610
5611 lua_pushnil(L);
5612 while (lua_next(L, -2) != 0) {
5613 struct ist name, value;
5614 const char *n, *v;
5615 size_t nlen, vlen;
5616
5617 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5618 /* Skip element if the key is not a string or if the value is not a table */
5619 goto next_hdr;
5620 }
5621
5622 n = lua_tolstring(L, -2, &nlen);
5623 name = ist2(n, nlen);
5624 if (isteqi(name, ist("content-length"))) {
5625 /* Always skip content-length header. It will be added
5626 * later with the correct len
5627 */
5628 goto next_hdr;
5629 }
5630
5631 /* Loop on header's values */
5632 lua_pushnil(L);
5633 while (lua_next(L, -2)) {
5634 if (!lua_isstring(L, -1)) {
5635 /* Skip the value if it is not a string */
5636 goto next_value;
5637 }
5638
5639 v = lua_tolstring(L, -1, &vlen);
5640 value = ist2(v, vlen);
5641
5642 if (isteqi(name, ist("transfer-encoding")))
5643 h1_parse_xfer_enc_header(&h1m, value);
5644 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5645 goto fail;
5646
5647 next_value:
5648 lua_pop(L, 1);
5649 }
5650
5651 next_hdr:
5652 lua_pop(L, 1);
5653 }
5654 skip_headers:
5655 lua_pop(L, 1);
5656
5657 /* Update h1m flags: CLEN is set if CHNK is not present */
5658 if (!(h1m.flags & H1_MF_CHNK)) {
5659 const char *clen = ultoa(body_len);
5660
5661 h1m.flags |= H1_MF_CLEN;
5662 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5663 goto fail;
5664 }
5665 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5666 h1m.flags |= H1_MF_XFER_LEN;
5667
5668 /* Update HTX start-line flags */
5669 if (h1m.flags & H1_MF_XFER_ENC)
5670 flags |= HTX_SL_F_XFER_ENC;
5671 if (h1m.flags & H1_MF_XFER_LEN) {
5672 flags |= HTX_SL_F_XFER_LEN;
5673 if (h1m.flags & H1_MF_CHNK)
5674 flags |= HTX_SL_F_CHNK;
5675 else if (h1m.flags & H1_MF_CLEN)
5676 flags |= HTX_SL_F_CLEN;
5677 if (h1m.body_len == 0)
5678 flags |= HTX_SL_F_BODYLESS;
5679 }
5680 sl->flags |= flags;
5681
5682
5683 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5684 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5685 !htx_add_endof(htx, HTX_BLK_EOM))
5686 goto fail;
5687
5688 /* Now, forward the response and terminate the transaction */
5689 s->txn->status = code;
5690 htx_to_buf(htx, &s->res.buf);
5691 if (!http_forward_proxy_resp(s, 1))
5692 goto fail;
5693
5694 return 1;
5695
5696 fail:
5697 channel_htx_truncate(&s->res, htx);
5698 return 0;
5699}
5700
5701/* Terminate a transaction if called from a lua action. For TCP streams,
5702 * processing is just aborted. Nothing is returned to the client and all
5703 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5704 * is forwarded to the client before terminating the transaction. On success,
5705 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5706 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5707 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005708 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005709__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005710{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005711 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005712 struct stream *s;
5713 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005714
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005715 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005716
Christopher Faulet700d9e82020-01-31 12:21:52 +01005717 /* If the flags NOTERM is set, we cannot terminate the session, so we
5718 * just end the execution of the current lua code. */
5719 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005720 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005721
Christopher Faulet700d9e82020-01-31 12:21:52 +01005722 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005723 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005724 struct channel *req = &s->req;
5725 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005726
Christopher Faulet700d9e82020-01-31 12:21:52 +01005727 channel_auto_read(req);
5728 channel_abort(req);
5729 channel_auto_close(req);
5730 channel_erase(req);
5731
5732 res->wex = tick_add_ifset(now_ms, res->wto);
5733 channel_auto_read(res);
5734 channel_auto_close(res);
5735 channel_shutr_now(res);
5736
5737 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5738 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005739 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005740
Christopher Faulet700d9e82020-01-31 12:21:52 +01005741 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5742 /* No reply or invalid reply */
5743 s->txn->status = 0;
5744 http_reply_and_close(s, 0, NULL);
5745 }
5746 else {
5747 /* Remove extra args to have the reply on top of the stack */
5748 if (lua_gettop(L) > 2)
5749 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005750
Christopher Faulet700d9e82020-01-31 12:21:52 +01005751 if (!hlua_txn_forward_reply(L, s)) {
5752 if (!(s->flags & SF_ERR_MASK))
5753 s->flags |= SF_ERR_PRXCOND;
5754 lua_pushinteger(L, ACT_RET_ERR);
5755 WILL_LJMP(hlua_done(L));
5756 return 0; /* Never reached */
5757 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005758 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005759
Christopher Faulet700d9e82020-01-31 12:21:52 +01005760 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5761 if (htxn->dir == SMP_OPT_DIR_REQ) {
5762 /* let's log the request time */
5763 s->logs.tv_request = now;
5764 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5765 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5766 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005767
Christopher Faulet700d9e82020-01-31 12:21:52 +01005768 done:
5769 if (!(s->flags & SF_ERR_MASK))
5770 s->flags |= SF_ERR_LOCAL;
5771 if (!(s->flags & SF_FINST_MASK))
5772 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005773
Christopher Faulet4ad73102020-03-05 11:07:31 +01005774 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005775 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005776 return 0;
5777}
5778
Christopher Faulet700d9e82020-01-31 12:21:52 +01005779/*
5780 *
5781 *
5782 * Class REPLY
5783 *
5784 *
5785 */
5786
5787/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5788 * free slots, the function fails and returns 0;
5789 */
5790static int hlua_txn_reply_new(lua_State *L)
5791{
5792 struct hlua_txn *htxn;
5793 const char *reason, *body = NULL;
5794 int ret, status;
5795
5796 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005797 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005798 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5799 WILL_LJMP(lua_error(L));
5800 }
5801
5802 /* Default value */
5803 status = 200;
5804 reason = http_get_reason(status);
5805
5806 if (lua_istable(L, 2)) {
5807 /* load status and reason from the table argument at index 2 */
5808 ret = lua_getfield(L, 2, "status");
5809 if (ret == LUA_TNIL)
5810 goto reason;
5811 else if (ret != LUA_TNUMBER) {
5812 /* invalid status: ignore the reason */
5813 goto body;
5814 }
5815 status = lua_tointeger(L, -1);
5816
5817 reason:
5818 lua_pop(L, 1); /* restore the stack: remove status */
5819 ret = lua_getfield(L, 2, "reason");
5820 if (ret == LUA_TSTRING)
5821 reason = lua_tostring(L, -1);
5822
5823 body:
5824 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5825 ret = lua_getfield(L, 2, "body");
5826 if (ret == LUA_TSTRING)
5827 body = lua_tostring(L, -1);
5828 lua_pop(L, 1); /* restore the stack: remove body */
5829 }
5830
5831 /* Create the Reply table */
5832 lua_newtable(L);
5833
5834 /* Add status element */
5835 lua_pushstring(L, "status");
5836 lua_pushinteger(L, status);
5837 lua_settable(L, -3);
5838
5839 /* Add reason element */
5840 reason = http_get_reason(status);
5841 lua_pushstring(L, "reason");
5842 lua_pushstring(L, reason);
5843 lua_settable(L, -3);
5844
5845 /* Add body element, nil if undefined */
5846 lua_pushstring(L, "body");
5847 if (body)
5848 lua_pushstring(L, body);
5849 else
5850 lua_pushnil(L);
5851 lua_settable(L, -3);
5852
5853 /* Add headers element */
5854 lua_pushstring(L, "headers");
5855 lua_newtable(L);
5856
5857 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5858 if (lua_istable(L, 2)) {
5859 /* load headers from the table argument at index 2. If it is a table, copy it. */
5860 ret = lua_getfield(L, 2, "headers");
5861 if (ret == LUA_TTABLE) {
5862 /* stack: [ ... <headers:table>, <table> ] */
5863 lua_pushnil(L);
5864 while (lua_next(L, -2) != 0) {
5865 /* stack: [ ... <headers:table>, <table>, k, v] */
5866 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5867 /* invalid value type, skip it */
5868 lua_pop(L, 1);
5869 continue;
5870 }
5871
5872
5873 /* Duplicate the key and swap it with the value. */
5874 lua_pushvalue(L, -2);
5875 lua_insert(L, -2);
5876 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5877
5878 lua_newtable(L);
5879 lua_insert(L, -2);
5880 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5881
5882 if (lua_isstring(L, -1)) {
5883 /* push the value in the inner table */
5884 lua_rawseti(L, -2, 1);
5885 }
5886 else { /* table */
5887 lua_pushnil(L);
5888 while (lua_next(L, -2) != 0) {
5889 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5890 if (!lua_isstring(L, -1)) {
5891 /* invalid value type, skip it*/
5892 lua_pop(L, 1);
5893 continue;
5894 }
5895 /* push the value in the inner table */
5896 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5897 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5898 }
5899 lua_pop(L, 1);
5900 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5901 }
5902
5903 /* push (k,v) on the stack in the headers table:
5904 * stack: [ ... <headers:table>, <table>, k, k, v ]
5905 */
5906 lua_settable(L, -5);
5907 /* stack: [ ... <headers:table>, <table>, k ] */
5908 }
5909 }
5910 lua_pop(L, 1);
5911 }
5912 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5913 lua_settable(L, -3);
5914 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5915
5916 /* Pop a class sesison metatable and affect it to the userdata. */
5917 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5918 lua_setmetatable(L, -2);
5919 return 1;
5920}
5921
5922/* Set the reply status code, and optionally the reason. If no reason is
5923 * provided, the default one corresponding to the status code is used.
5924 */
5925__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5926{
5927 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5928 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5929
5930 /* First argument (self) must be a table */
5931 luaL_checktype(L, 1, LUA_TTABLE);
5932
5933 if (status < 100 || status > 599) {
5934 lua_pushboolean(L, 0);
5935 return 1;
5936 }
5937 if (!reason)
5938 reason = http_get_reason(status);
5939
5940 lua_pushinteger(L, status);
5941 lua_setfield(L, 1, "status");
5942
5943 lua_pushstring(L, reason);
5944 lua_setfield(L, 1, "reason");
5945
5946 lua_pushboolean(L, 1);
5947 return 1;
5948}
5949
5950/* Add a header into the reply object. Each header name is associated to an
5951 * array of values in the "headers" table. If the header name is not found, a
5952 * new entry is created.
5953 */
5954__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5955{
5956 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5957 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5958 int ret;
5959
5960 /* First argument (self) must be a table */
5961 luaL_checktype(L, 1, LUA_TTABLE);
5962
5963 /* Push in the stack the "headers" entry. */
5964 ret = lua_getfield(L, 1, "headers");
5965 if (ret != LUA_TTABLE) {
5966 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5967 WILL_LJMP(lua_error(L));
5968 }
5969
5970 /* check if the header is already registered. If not, register it. */
5971 ret = lua_getfield(L, -1, name);
5972 if (ret == LUA_TNIL) {
5973 /* Entry not found. */
5974 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5975
5976 /* Insert the new header name in the array in the top of the stack.
5977 * It left the new array in the top of the stack.
5978 */
5979 lua_newtable(L);
5980 lua_pushstring(L, name);
5981 lua_pushvalue(L, -2);
5982 lua_settable(L, -4);
5983 }
5984 else if (ret != LUA_TTABLE) {
5985 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5986 WILL_LJMP(lua_error(L));
5987 }
5988
5989 /* Now the top od thestack is an array of values. We push
5990 * the header value as new entry.
5991 */
5992 lua_pushstring(L, value);
5993 ret = lua_rawlen(L, -2);
5994 lua_rawseti(L, -2, ret + 1);
5995
5996 lua_pushboolean(L, 1);
5997 return 1;
5998}
5999
6000/* Remove all occurrences of a given header name. */
6001__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6002{
6003 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6004 int ret;
6005
6006 /* First argument (self) must be a table */
6007 luaL_checktype(L, 1, LUA_TTABLE);
6008
6009 /* Push in the stack the "headers" entry. */
6010 ret = lua_getfield(L, 1, "headers");
6011 if (ret != LUA_TTABLE) {
6012 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6013 WILL_LJMP(lua_error(L));
6014 }
6015
6016 lua_pushstring(L, name);
6017 lua_pushnil(L);
6018 lua_settable(L, -3);
6019
6020 lua_pushboolean(L, 1);
6021 return 1;
6022}
6023
6024/* Set the reply's body. Overwrite any existing entry. */
6025__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6026{
6027 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6028
6029 /* First argument (self) must be a table */
6030 luaL_checktype(L, 1, LUA_TTABLE);
6031
6032 lua_pushstring(L, payload);
6033 lua_setfield(L, 1, "body");
6034
6035 lua_pushboolean(L, 1);
6036 return 1;
6037}
6038
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006039__LJMP static int hlua_log(lua_State *L)
6040{
6041 int level;
6042 const char *msg;
6043
6044 MAY_LJMP(check_args(L, 2, "log"));
6045 level = MAY_LJMP(luaL_checkinteger(L, 1));
6046 msg = MAY_LJMP(luaL_checkstring(L, 2));
6047
6048 if (level < 0 || level >= NB_LOG_LEVELS)
6049 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6050
6051 hlua_sendlog(NULL, level, msg);
6052 return 0;
6053}
6054
6055__LJMP static int hlua_log_debug(lua_State *L)
6056{
6057 const char *msg;
6058
6059 MAY_LJMP(check_args(L, 1, "debug"));
6060 msg = MAY_LJMP(luaL_checkstring(L, 1));
6061 hlua_sendlog(NULL, LOG_DEBUG, msg);
6062 return 0;
6063}
6064
6065__LJMP static int hlua_log_info(lua_State *L)
6066{
6067 const char *msg;
6068
6069 MAY_LJMP(check_args(L, 1, "info"));
6070 msg = MAY_LJMP(luaL_checkstring(L, 1));
6071 hlua_sendlog(NULL, LOG_INFO, msg);
6072 return 0;
6073}
6074
6075__LJMP static int hlua_log_warning(lua_State *L)
6076{
6077 const char *msg;
6078
6079 MAY_LJMP(check_args(L, 1, "warning"));
6080 msg = MAY_LJMP(luaL_checkstring(L, 1));
6081 hlua_sendlog(NULL, LOG_WARNING, msg);
6082 return 0;
6083}
6084
6085__LJMP static int hlua_log_alert(lua_State *L)
6086{
6087 const char *msg;
6088
6089 MAY_LJMP(check_args(L, 1, "alert"));
6090 msg = MAY_LJMP(luaL_checkstring(L, 1));
6091 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006092 return 0;
6093}
6094
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006095__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006096{
6097 int wakeup_ms = lua_tointeger(L, -1);
6098 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006099 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006100 return 0;
6101}
6102
6103__LJMP static int hlua_sleep(lua_State *L)
6104{
6105 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006106 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006107
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006108 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006109
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006110 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006111 wakeup_ms = tick_add(now_ms, delay);
6112 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006113
Willy Tarreau9635e032018-10-16 17:52:55 +02006114 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006115 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006116}
6117
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006118__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006119{
6120 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006121 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006122
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006123 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006124
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006125 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006126 wakeup_ms = tick_add(now_ms, delay);
6127 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006128
Willy Tarreau9635e032018-10-16 17:52:55 +02006129 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006130 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006131}
6132
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006133/* This functionis an LUA binding. it permits to give back
6134 * the hand at the HAProxy scheduler. It is used when the
6135 * LUA processing consumes a lot of time.
6136 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006137__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006138{
6139 return 0;
6140}
6141
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006142__LJMP static int hlua_yield(lua_State *L)
6143{
Willy Tarreau9635e032018-10-16 17:52:55 +02006144 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006145 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006146}
6147
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006148/* This function change the nice of the currently executed
6149 * task. It is used set low or high priority at the current
6150 * task.
6151 */
Willy Tarreau59551662015-03-10 14:23:13 +01006152__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006153{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006154 struct hlua *hlua;
6155 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006156
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006157 MAY_LJMP(check_args(L, 1, "set_nice"));
6158 hlua = hlua_gethlua(L);
6159 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006160
6161 /* If he task is not set, I'm in a start mode. */
6162 if (!hlua || !hlua->task)
6163 return 0;
6164
6165 if (nice < -1024)
6166 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006167 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006168 nice = 1024;
6169
6170 hlua->task->nice = nice;
6171 return 0;
6172}
6173
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006174/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006175 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6176 * return an E_AGAIN signal, the emmiter of this signal must set a
6177 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006178 *
6179 * Task wrapper are longjmp safe because the only one Lua code
6180 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006181 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006182struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006183{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006184 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006185 enum hlua_exec status;
6186
Christopher Faulet5bc99722018-04-25 10:34:45 +02006187 if (task->thread_mask == MAX_THREADS_MASK)
6188 task_set_affinity(task, tid_bit);
6189
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006190 /* If it is the first call to the task, we must initialize the
6191 * execution timeouts.
6192 */
6193 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006194 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006195
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006196 /* Execute the Lua code. */
6197 status = hlua_ctx_resume(hlua, 1);
6198
6199 switch (status) {
6200 /* finished or yield */
6201 case HLUA_E_OK:
6202 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006203 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006204 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006205 break;
6206
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006207 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006208 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006209 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006210 break;
6211
6212 /* finished with error. */
6213 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006214 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006215 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006216 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006217 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006218 break;
6219
6220 case HLUA_E_ERR:
6221 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006222 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006223 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006224 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006225 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006226 break;
6227 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006228 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006229}
6230
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006231/* This function is an LUA binding that register LUA function to be
6232 * executed after the HAProxy configuration parsing and before the
6233 * HAProxy scheduler starts. This function expect only one LUA
6234 * argument that is a function. This function returns nothing, but
6235 * throws if an error is encountered.
6236 */
6237__LJMP static int hlua_register_init(lua_State *L)
6238{
6239 struct hlua_init_function *init;
6240 int ref;
6241
6242 MAY_LJMP(check_args(L, 1, "register_init"));
6243
6244 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6245
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006246 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006247 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006248 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006249
6250 init->function_ref = ref;
6251 LIST_ADDQ(&hlua_init_functions, &init->l);
6252 return 0;
6253}
6254
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006255/* This functio is an LUA binding. It permits to register a task
6256 * executed in parallel of the main HAroxy activity. The task is
6257 * created and it is set in the HAProxy scheduler. It can be called
6258 * from the "init" section, "post init" or during the runtime.
6259 *
6260 * Lua prototype:
6261 *
6262 * <none> core.register_task(<function>)
6263 */
6264static int hlua_register_task(lua_State *L)
6265{
6266 struct hlua *hlua;
6267 struct task *task;
6268 int ref;
6269
6270 MAY_LJMP(check_args(L, 1, "register_task"));
6271
6272 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6273
Willy Tarreaubafbe012017-11-24 17:34:44 +01006274 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006275 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006276 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006277
Emeric Brunc60def82017-09-27 14:59:38 +02006278 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006279 if (!task)
6280 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6281
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006282 task->context = hlua;
6283 task->process = hlua_process_task;
6284
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006285 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006286 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006287
6288 /* Restore the function in the stack. */
6289 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6290 hlua->nargs = 0;
6291
6292 /* Schedule task. */
6293 task_schedule(task, now_ms);
6294
6295 return 0;
6296}
6297
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006298/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6299 * doesn't allow "yield" functions because the HAProxy engine cannot
6300 * resume converters.
6301 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006302static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006303{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006304 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006305 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006306 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006307
Willy Tarreaube508f12016-03-10 11:47:01 +01006308 if (!stream)
6309 return 0;
6310
Willy Tarreau87b09662015-04-03 00:22:06 +02006311 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006312 * Lua context can be not initialized. This behavior
6313 * permits to save performances because a systematic
6314 * Lua initialization cause 5% performances loss.
6315 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006316 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006317 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006318 if (!stream->hlua) {
6319 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6320 return 0;
6321 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006322 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006323 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6324 return 0;
6325 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006326 }
6327
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006328 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006329 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006330
6331 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006332 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6333 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6334 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006335 else
6336 error = "critical error";
6337 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006338 return 0;
6339 }
6340
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006341 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006342 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006343 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006344 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006345 return 0;
6346 }
6347
6348 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006349 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006350
6351 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006352 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006353 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006354 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006355 return 0;
6356 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006357 hlua_smp2lua(stream->hlua->T, smp);
6358 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006359
6360 /* push keywords in the stack. */
6361 if (arg_p) {
6362 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006363 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006364 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006365 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006366 return 0;
6367 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006368 hlua_arg2lua(stream->hlua->T, arg_p);
6369 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006370 }
6371 }
6372
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006373 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006374 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006375
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006376 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006377 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006378 }
6379
6380 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006381 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006382 /* finished. */
6383 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006384 /* If the stack is empty, the function fails. */
6385 if (lua_gettop(stream->hlua->T) <= 0)
6386 return 0;
6387
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006388 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006389 hlua_lua2smp(stream->hlua->T, -1, smp);
6390 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006391 return 1;
6392
6393 /* yield. */
6394 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006395 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006396 return 0;
6397
6398 /* finished with error. */
6399 case HLUA_E_ERRMSG:
6400 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006401 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006402 fcn->name, lua_tostring(stream->hlua->T, -1));
6403 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006404 return 0;
6405
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006406 case HLUA_E_ETMOUT:
6407 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6408 return 0;
6409
6410 case HLUA_E_NOMEM:
6411 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6412 return 0;
6413
6414 case HLUA_E_YIELD:
6415 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6416 return 0;
6417
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006418 case HLUA_E_ERR:
6419 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006420 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006421 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006422
6423 default:
6424 return 0;
6425 }
6426}
6427
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006428/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6429 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006430 * resume sample-fetches. This function will be called by the sample
6431 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006432 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006433static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6434 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006435{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006436 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006437 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006438 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006439 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006440
Willy Tarreaube508f12016-03-10 11:47:01 +01006441 if (!stream)
6442 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006443
Willy Tarreau87b09662015-04-03 00:22:06 +02006444 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006445 * Lua context can be not initialized. This behavior
6446 * permits to save performances because a systematic
6447 * Lua initialization cause 5% performances loss.
6448 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006449 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006450 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006451 if (!stream->hlua) {
6452 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6453 return 0;
6454 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006455 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006456 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6457 return 0;
6458 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006459 }
6460
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006461 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006462 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006463
6464 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006465 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6466 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6467 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006468 else
6469 error = "critical error";
6470 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006471 return 0;
6472 }
6473
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006474 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006475 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006476 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006477 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006478 return 0;
6479 }
6480
6481 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006482 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006483
6484 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006485 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006486 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006487 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006488 return 0;
6489 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006490 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006491
6492 /* push keywords in the stack. */
6493 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6494 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006495 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006496 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006497 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006498 return 0;
6499 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006500 hlua_arg2lua(stream->hlua->T, arg_p);
6501 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006502 }
6503
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006504 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006505 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006506
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006507 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006508 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006509 }
6510
6511 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006512 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006513 /* finished. */
6514 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006515 /* If the stack is empty, the function fails. */
6516 if (lua_gettop(stream->hlua->T) <= 0)
6517 return 0;
6518
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006519 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006520 hlua_lua2smp(stream->hlua->T, -1, smp);
6521 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006522
6523 /* Set the end of execution flag. */
6524 smp->flags &= ~SMP_F_MAY_CHANGE;
6525 return 1;
6526
6527 /* yield. */
6528 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006529 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006530 return 0;
6531
6532 /* finished with error. */
6533 case HLUA_E_ERRMSG:
6534 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006535 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006536 fcn->name, lua_tostring(stream->hlua->T, -1));
6537 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006538 return 0;
6539
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006540 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006541 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6542 return 0;
6543
6544 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006545 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6546 return 0;
6547
6548 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006549 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6550 return 0;
6551
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006552 case HLUA_E_ERR:
6553 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006554 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006555 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006556
6557 default:
6558 return 0;
6559 }
6560}
6561
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006562/* This function is an LUA binding used for registering
6563 * "sample-conv" functions. It expects a converter name used
6564 * in the haproxy configuration file, and an LUA function.
6565 */
6566__LJMP static int hlua_register_converters(lua_State *L)
6567{
6568 struct sample_conv_kw_list *sck;
6569 const char *name;
6570 int ref;
6571 int len;
6572 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006573 struct sample_conv *sc;
6574 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006575
6576 MAY_LJMP(check_args(L, 2, "register_converters"));
6577
6578 /* First argument : converter name. */
6579 name = MAY_LJMP(luaL_checkstring(L, 1));
6580
6581 /* Second argument : lua function. */
6582 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6583
Thierry Fournierf67442e2020-11-28 20:41:07 +01006584 /* Check if the converter is already registered */
6585 trash = get_trash_chunk();
6586 chunk_printf(trash, "lua.%s", name);
6587 sc = find_sample_conv(trash->area, trash->data);
6588 if (sc != NULL) {
6589 ha_warning("Trying to register converter 'lua.%s' more than once. "
6590 "This will become a hard error in version 2.5.\n", name);
6591 }
6592
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006593 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006594 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006595 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006596 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006597 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006598 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006599 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006600
6601 /* Fill fcn. */
6602 fcn->name = strdup(name);
6603 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006604 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006605 fcn->function_ref = ref;
6606
6607 /* List head */
6608 sck->list.n = sck->list.p = NULL;
6609
6610 /* converter keyword. */
6611 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006612 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006613 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006614 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006615
6616 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6617 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006618 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 +01006619 sck->kw[0].val_args = NULL;
6620 sck->kw[0].in_type = SMP_T_STR;
6621 sck->kw[0].out_type = SMP_T_STR;
6622 sck->kw[0].private = fcn;
6623
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006624 /* Register this new converter */
6625 sample_register_convs(sck);
6626
6627 return 0;
6628}
6629
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006630/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006631 * "sample-fetch" functions. It expects a converter name used
6632 * in the haproxy configuration file, and an LUA function.
6633 */
6634__LJMP static int hlua_register_fetches(lua_State *L)
6635{
6636 const char *name;
6637 int ref;
6638 int len;
6639 struct sample_fetch_kw_list *sfk;
6640 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006641 struct sample_fetch *sf;
6642 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006643
6644 MAY_LJMP(check_args(L, 2, "register_fetches"));
6645
6646 /* First argument : sample-fetch name. */
6647 name = MAY_LJMP(luaL_checkstring(L, 1));
6648
6649 /* Second argument : lua function. */
6650 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6651
Thierry Fournierf67442e2020-11-28 20:41:07 +01006652 /* Check if the sample-fetch is already registered */
6653 trash = get_trash_chunk();
6654 chunk_printf(trash, "lua.%s", name);
6655 sf = find_sample_fetch(trash->area, trash->data);
6656 if (sf != NULL) {
6657 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6658 "This will become a hard error in version 2.5.\n", name);
6659 }
6660
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006661 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006662 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006663 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006664 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006665 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006666 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006667 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006668
6669 /* Fill fcn. */
6670 fcn->name = strdup(name);
6671 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006672 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006673 fcn->function_ref = ref;
6674
6675 /* List head */
6676 sfk->list.n = sfk->list.p = NULL;
6677
6678 /* sample-fetch keyword. */
6679 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006680 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006681 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006682 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006683
6684 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6685 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006686 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 +01006687 sfk->kw[0].val_args = NULL;
6688 sfk->kw[0].out_type = SMP_T_STR;
6689 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6690 sfk->kw[0].val = 0;
6691 sfk->kw[0].private = fcn;
6692
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006693 /* Register this new fetch. */
6694 sample_register_fetches(sfk);
6695
6696 return 0;
6697}
6698
Christopher Faulet501465d2020-02-26 14:54:16 +01006699/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006700 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006701__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006702{
6703 struct hlua *hlua = hlua_gethlua(L);
6704 unsigned int delay;
6705 unsigned int wakeup_ms;
6706
6707 MAY_LJMP(check_args(L, 1, "wake_time"));
6708
6709 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6710 wakeup_ms = tick_add(now_ms, delay);
6711 hlua->wake_time = wakeup_ms;
6712 return 0;
6713}
6714
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006715/* This function is a wrapper to execute each LUA function declared as an action
6716 * wrapper during the initialisation period. This function may return any
6717 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6718 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6719 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006720 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006721static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006722 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006723{
6724 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006725 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006726 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006727 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006728
6729 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006730 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6731 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6732 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6733 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006734 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006735 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006736 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006737 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006738
Willy Tarreau87b09662015-04-03 00:22:06 +02006739 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006740 * Lua context can be not initialized. This behavior
6741 * permits to save performances because a systematic
6742 * Lua initialization cause 5% performances loss.
6743 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006744 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006745 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006746 if (!s->hlua) {
6747 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6748 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006749 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006750 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006751 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006752 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6753 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006754 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006755 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006756 }
6757
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006758 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006759 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006760
6761 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006762 if (!SET_SAFE_LJMP(s->hlua->T)) {
6763 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6764 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006765 else
6766 error = "critical error";
6767 SEND_ERR(px, "Lua function '%s': %s.\n",
6768 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006769 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006770 }
6771
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006772 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006773 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006774 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006775 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006776 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006777 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006778 }
6779
6780 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006781 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006782
Willy Tarreau87b09662015-04-03 00:22:06 +02006783 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006784 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006785 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006786 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006787 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006788 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006789 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006790 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006791
6792 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006793 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006794 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006795 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006796 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006797 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006798 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006799 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006800 lua_pushstring(s->hlua->T, *arg);
6801 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006802 }
6803
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006804 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006805 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006806
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006807 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006808 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006809 }
6810
6811 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006812 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006813 /* finished. */
6814 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006815 /* Catch the return value */
6816 if (lua_gettop(s->hlua->T) > 0)
6817 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006818
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006819 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006820 if (act_ret == ACT_RET_YIELD) {
6821 if (flags & ACT_OPT_FINAL)
6822 goto err_yield;
6823
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006824 if (dir == SMP_OPT_DIR_REQ)
6825 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6826 s->hlua->wake_time);
6827 else
6828 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6829 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006830 }
6831 goto end;
6832
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006833 /* yield. */
6834 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006835 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006836 if (dir == SMP_OPT_DIR_REQ)
6837 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6838 s->hlua->wake_time);
6839 else
6840 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6841 s->hlua->wake_time);
6842
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006843 /* Some actions can be wake up when a "write" event
6844 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006845 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006846 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006847 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006848 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006849 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006850 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006851 act_ret = ACT_RET_YIELD;
6852 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006853
6854 /* finished with error. */
6855 case HLUA_E_ERRMSG:
6856 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006857 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006858 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6859 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006860 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006861
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006862 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006863 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006864 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006865
6866 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006867 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006868 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006869
6870 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02006871 err_yield:
6872 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006873 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6874 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006875 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006876
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006877 case HLUA_E_ERR:
6878 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006879 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006880 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006881
6882 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006883 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006884 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006885
6886 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02006887 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006888 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006889 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006890}
6891
Olivier Houchard9f6af332018-05-25 14:04:04 +02006892struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006893{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006894 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006895
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006896 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006897 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006898 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006899}
6900
6901static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6902{
6903 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006904 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006905 struct task *task;
6906 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006907 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006908
Willy Tarreaubafbe012017-11-24 17:34:44 +01006909 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006910 if (!hlua) {
6911 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6912 ctx->rule->arg.hlua_rule->fcn.name);
6913 return 0;
6914 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006915 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006916 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006917 ctx->ctx.hlua_apptcp.flags = 0;
6918
6919 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006920 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006921 if (!task) {
6922 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6923 ctx->rule->arg.hlua_rule->fcn.name);
6924 return 0;
6925 }
6926 task->nice = 0;
6927 task->context = ctx;
6928 task->process = hlua_applet_wakeup;
6929 ctx->ctx.hlua_apptcp.task = task;
6930
6931 /* In the execution wrappers linked with a stream, the
6932 * Lua context can be not initialized. This behavior
6933 * permits to save performances because a systematic
6934 * Lua initialization cause 5% performances loss.
6935 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006936 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006937 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6938 ctx->rule->arg.hlua_rule->fcn.name);
6939 return 0;
6940 }
6941
6942 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006943 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006944
6945 /* The following Lua calls can fail. */
6946 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006947 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6948 error = lua_tostring(hlua->T, -1);
6949 else
6950 error = "critical error";
6951 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6952 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006953 return 0;
6954 }
6955
6956 /* Check stack available size. */
6957 if (!lua_checkstack(hlua->T, 1)) {
6958 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6959 ctx->rule->arg.hlua_rule->fcn.name);
6960 RESET_SAFE_LJMP(hlua->T);
6961 return 0;
6962 }
6963
6964 /* Restore the function in the stack. */
6965 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6966
6967 /* Create and and push object stream in the stack. */
6968 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6969 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6970 ctx->rule->arg.hlua_rule->fcn.name);
6971 RESET_SAFE_LJMP(hlua->T);
6972 return 0;
6973 }
6974 hlua->nargs = 1;
6975
6976 /* push keywords in the stack. */
6977 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6978 if (!lua_checkstack(hlua->T, 1)) {
6979 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6980 ctx->rule->arg.hlua_rule->fcn.name);
6981 RESET_SAFE_LJMP(hlua->T);
6982 return 0;
6983 }
6984 lua_pushstring(hlua->T, *arg);
6985 hlua->nargs++;
6986 }
6987
6988 RESET_SAFE_LJMP(hlua->T);
6989
6990 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006991 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006992 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006993
6994 return 1;
6995}
6996
Willy Tarreau60409db2019-08-21 14:14:50 +02006997void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006998{
6999 struct stream_interface *si = ctx->owner;
7000 struct stream *strm = si_strm(si);
7001 struct channel *res = si_ic(si);
7002 struct act_rule *rule = ctx->rule;
7003 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007004 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007005
7006 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007007 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7008 /* eat the whole request */
7009 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007010 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007011 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007012
7013 /* If the stream is disconnect or closed, ldo nothing. */
7014 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7015 return;
7016
7017 /* Execute the function. */
7018 switch (hlua_ctx_resume(hlua, 1)) {
7019 /* finished. */
7020 case HLUA_E_OK:
7021 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7022
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007023 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007024 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007025 res->flags |= CF_READ_NULL;
7026 si_shutr(si);
7027 return;
7028
7029 /* yield. */
7030 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007031 if (hlua->wake_time != TICK_ETERNITY)
7032 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007033 return;
7034
7035 /* finished with error. */
7036 case HLUA_E_ERRMSG:
7037 /* Display log. */
7038 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7039 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7040 lua_pop(hlua->T, 1);
7041 goto error;
7042
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007043 case HLUA_E_ETMOUT:
7044 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
7045 rule->arg.hlua_rule->fcn.name);
7046 goto error;
7047
7048 case HLUA_E_NOMEM:
7049 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
7050 rule->arg.hlua_rule->fcn.name);
7051 goto error;
7052
7053 case HLUA_E_YIELD: /* unexpected */
7054 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
7055 rule->arg.hlua_rule->fcn.name);
7056 goto error;
7057
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007058 case HLUA_E_ERR:
7059 /* Display log. */
7060 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
7061 rule->arg.hlua_rule->fcn.name);
7062 goto error;
7063
7064 default:
7065 goto error;
7066 }
7067
7068error:
7069
7070 /* For all other cases, just close the stream. */
7071 si_shutw(si);
7072 si_shutr(si);
7073 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7074}
7075
7076static void hlua_applet_tcp_release(struct appctx *ctx)
7077{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007078 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007079 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007080 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007081 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007082}
7083
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007084/* The function returns 1 if the initialisation is complete, 0 if
7085 * an errors occurs and -1 if more data are required for initializing
7086 * the applet.
7087 */
7088static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7089{
7090 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007091 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007092 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007093 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007094 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007095 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007096
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007097 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007098 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007099 if (!hlua) {
7100 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7101 ctx->rule->arg.hlua_rule->fcn.name);
7102 return 0;
7103 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007104 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007105 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007106 ctx->ctx.hlua_apphttp.left_bytes = -1;
7107 ctx->ctx.hlua_apphttp.flags = 0;
7108
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007109 if (txn->req.flags & HTTP_MSGF_VER_11)
7110 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7111
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007112 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007113 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007114 if (!task) {
7115 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7116 ctx->rule->arg.hlua_rule->fcn.name);
7117 return 0;
7118 }
7119 task->nice = 0;
7120 task->context = ctx;
7121 task->process = hlua_applet_wakeup;
7122 ctx->ctx.hlua_apphttp.task = task;
7123
7124 /* In the execution wrappers linked with a stream, the
7125 * Lua context can be not initialized. This behavior
7126 * permits to save performances because a systematic
7127 * Lua initialization cause 5% performances loss.
7128 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007129 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007130 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
7131 ctx->rule->arg.hlua_rule->fcn.name);
7132 return 0;
7133 }
7134
7135 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007136 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007137
7138 /* The following Lua calls can fail. */
7139 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007140 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7141 error = lua_tostring(hlua->T, -1);
7142 else
7143 error = "critical error";
7144 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7145 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007146 return 0;
7147 }
7148
7149 /* Check stack available size. */
7150 if (!lua_checkstack(hlua->T, 1)) {
7151 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7152 ctx->rule->arg.hlua_rule->fcn.name);
7153 RESET_SAFE_LJMP(hlua->T);
7154 return 0;
7155 }
7156
7157 /* Restore the function in the stack. */
7158 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7159
7160 /* Create and and push object stream in the stack. */
7161 if (!hlua_applet_http_new(hlua->T, ctx)) {
7162 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7163 ctx->rule->arg.hlua_rule->fcn.name);
7164 RESET_SAFE_LJMP(hlua->T);
7165 return 0;
7166 }
7167 hlua->nargs = 1;
7168
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007169 /* push keywords in the stack. */
7170 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7171 if (!lua_checkstack(hlua->T, 1)) {
7172 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7173 ctx->rule->arg.hlua_rule->fcn.name);
7174 RESET_SAFE_LJMP(hlua->T);
7175 return 0;
7176 }
7177 lua_pushstring(hlua->T, *arg);
7178 hlua->nargs++;
7179 }
7180
7181 RESET_SAFE_LJMP(hlua->T);
7182
7183 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007184 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007185
7186 return 1;
7187}
7188
Willy Tarreau60409db2019-08-21 14:14:50 +02007189void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007190{
7191 struct stream_interface *si = ctx->owner;
7192 struct stream *strm = si_strm(si);
7193 struct channel *req = si_oc(si);
7194 struct channel *res = si_ic(si);
7195 struct act_rule *rule = ctx->rule;
7196 struct proxy *px = strm->be;
7197 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7198 struct htx *req_htx, *res_htx;
7199
7200 res_htx = htx_from_buf(&res->buf);
7201
7202 /* If the stream is disconnect or closed, ldo nothing. */
7203 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7204 goto out;
7205
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007206 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007207 if (!b_size(&res->buf)) {
7208 si_rx_room_blk(si);
7209 goto out;
7210 }
7211 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007212 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007213 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7214
7215 /* Set the currently running flag. */
7216 if (!HLUA_IS_RUNNING(hlua) &&
7217 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7218 struct htx_blk *blk;
7219 size_t count = co_data(req);
7220
7221 if (!count) {
7222 si_cant_get(si);
7223 goto out;
7224 }
7225
7226 /* We need to flush the request header. This left the body for
7227 * the Lua.
7228 */
7229 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007230 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007231 while (count && blk) {
7232 enum htx_blk_type type = htx_get_blk_type(blk);
7233 uint32_t sz = htx_get_blksz(blk);
7234
7235 if (sz > count) {
7236 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007237 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007238 goto out;
7239 }
7240
7241 count -= sz;
7242 co_set_data(req, co_data(req) - sz);
7243 blk = htx_remove_blk(req_htx, blk);
7244
7245 if (type == HTX_BLK_EOH)
7246 break;
7247 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007248 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007249 }
7250
7251 /* Executes The applet if it is not done. */
7252 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7253
7254 /* Execute the function. */
7255 switch (hlua_ctx_resume(hlua, 1)) {
7256 /* finished. */
7257 case HLUA_E_OK:
7258 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7259 break;
7260
7261 /* yield. */
7262 case HLUA_E_AGAIN:
7263 if (hlua->wake_time != TICK_ETERNITY)
7264 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007265 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007266
7267 /* finished with error. */
7268 case HLUA_E_ERRMSG:
7269 /* Display log. */
7270 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7271 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7272 lua_pop(hlua->T, 1);
7273 goto error;
7274
7275 case HLUA_E_ETMOUT:
7276 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7277 rule->arg.hlua_rule->fcn.name);
7278 goto error;
7279
7280 case HLUA_E_NOMEM:
7281 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7282 rule->arg.hlua_rule->fcn.name);
7283 goto error;
7284
7285 case HLUA_E_YIELD: /* unexpected */
7286 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7287 rule->arg.hlua_rule->fcn.name);
7288 goto error;
7289
7290 case HLUA_E_ERR:
7291 /* Display log. */
7292 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7293 rule->arg.hlua_rule->fcn.name);
7294 goto error;
7295
7296 default:
7297 goto error;
7298 }
7299 }
7300
7301 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007302 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7303 goto done;
7304
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007305 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7306 goto error;
7307
Christopher Faulet54b5e212019-06-04 10:08:28 +02007308 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02007309 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007310 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7311 si_rx_room_blk(si);
7312 goto out;
7313 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007314 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007315 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7316 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007317 }
7318
7319 done:
7320 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007321 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007322 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007323 si_shutr(si);
7324 }
7325
7326 /* eat the whole request */
7327 if (co_data(req)) {
7328 req_htx = htx_from_buf(&req->buf);
7329 co_htx_skip(req, req_htx, co_data(req));
7330 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007331 }
7332 }
7333
7334 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007335 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007336 return;
7337
7338 error:
7339
7340 /* If we are in HTTP mode, and we are not send any
7341 * data, return a 500 server error in best effort:
7342 * if there is no room available in the buffer,
7343 * just close the connection.
7344 */
7345 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007346 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007347
7348 channel_erase(res);
7349 res->buf.data = b_data(err);
7350 memcpy(res->buf.area, b_head(err), b_data(err));
7351 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007352 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007353 }
7354 if (!(strm->flags & SF_ERR_MASK))
7355 strm->flags |= SF_ERR_RESOURCE;
7356 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7357 goto done;
7358}
7359
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007360static void hlua_applet_http_release(struct appctx *ctx)
7361{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007362 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007363 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007364 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007365 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007366}
7367
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007368/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007369 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007370 *
7371 * This function can fail with an abort() due to an Lua critical error.
7372 * We are in the configuration parsing process of HAProxy, this abort() is
7373 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007374 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007375static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7376 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007377{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007378 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007379 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007380
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007381 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007382 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007383 if (!rule->arg.hlua_rule) {
7384 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007385 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007386 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007387
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007388 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007389 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7390 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007391 if (!rule->arg.hlua_rule->args) {
7392 memprintf(err, "out of memory error");
7393 return ACT_RET_PRS_ERR;
7394 }
7395
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007396 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007397 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007398
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007399 /* Expect some arguments */
7400 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007401 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007402 memprintf(err, "expect %d arguments", fcn->nargs);
7403 return ACT_RET_PRS_ERR;
7404 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007405 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007406 if (!rule->arg.hlua_rule->args[i]) {
7407 memprintf(err, "out of memory error");
7408 return ACT_RET_PRS_ERR;
7409 }
7410 (*cur_arg)++;
7411 }
7412 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007413
Thierry FOURNIER42148732015-09-02 17:17:33 +02007414 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007415 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007416 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007417}
7418
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007419static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7420 struct act_rule *rule, char **err)
7421{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007422 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007423
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007424 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007425 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007426 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007427 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007428 * the call of this analyzer.
7429 */
7430 if (rule->from != ACT_F_HTTP_REQ) {
7431 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7432 return ACT_RET_PRS_ERR;
7433 }
7434
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007435 /* Memory for the rule. */
7436 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7437 if (!rule->arg.hlua_rule) {
7438 memprintf(err, "out of memory error");
7439 return ACT_RET_PRS_ERR;
7440 }
7441
7442 /* Reference the Lua function and store the reference. */
7443 rule->arg.hlua_rule->fcn = *fcn;
7444
7445 /* TODO: later accept arguments. */
7446 rule->arg.hlua_rule->args = NULL;
7447
7448 /* Add applet pointer in the rule. */
7449 rule->applet.obj_type = OBJ_TYPE_APPLET;
7450 rule->applet.name = fcn->name;
7451 rule->applet.init = hlua_applet_http_init;
7452 rule->applet.fct = hlua_applet_http_fct;
7453 rule->applet.release = hlua_applet_http_release;
7454 rule->applet.timeout = hlua_timeout_applet;
7455
7456 return ACT_RET_PRS_OK;
7457}
7458
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007459/* This function is an LUA binding used for registering
7460 * "sample-conv" functions. It expects a converter name used
7461 * in the haproxy configuration file, and an LUA function.
7462 */
7463__LJMP static int hlua_register_action(lua_State *L)
7464{
7465 struct action_kw_list *akl;
7466 const char *name;
7467 int ref;
7468 int len;
7469 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007470 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007471 struct buffer *trash;
7472 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007473
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007474 /* Initialise the number of expected arguments at 0. */
7475 nargs = 0;
7476
7477 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7478 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007479
7480 /* First argument : converter name. */
7481 name = MAY_LJMP(luaL_checkstring(L, 1));
7482
7483 /* Second argument : environment. */
7484 if (lua_type(L, 2) != LUA_TTABLE)
7485 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7486
7487 /* Third argument : lua function. */
7488 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7489
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007490 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007491 if (lua_gettop(L) >= 4)
7492 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7493
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007494 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007495 lua_pushnil(L);
7496 while (lua_next(L, 2) != 0) {
7497 if (lua_type(L, -1) != LUA_TSTRING)
7498 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7499
Thierry Fournierf67442e2020-11-28 20:41:07 +01007500 /* Check if action exists */
7501 trash = get_trash_chunk();
7502 chunk_printf(trash, "lua.%s", name);
7503 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7504 akw = tcp_req_cont_action(trash->area);
7505 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7506 akw = tcp_res_cont_action(trash->area);
7507 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7508 akw = action_http_req_custom(trash->area);
7509 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7510 akw = action_http_res_custom(trash->area);
7511 } else {
7512 akw = NULL;
7513 }
7514 if (akw != NULL) {
7515 ha_warning("Trying to register action 'lua.%s' more than once. "
7516 "This will become a hard error in version 2.5.\n", name);
7517 }
7518
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007519 /* Check required environment. Only accepted "http" or "tcp". */
7520 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007521 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007522 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007523 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007524 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007525 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007526 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007527
7528 /* Fill fcn. */
7529 fcn->name = strdup(name);
7530 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007531 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007532 fcn->function_ref = ref;
7533
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007534 /* Set the expected number od arguments. */
7535 fcn->nargs = nargs;
7536
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007537 /* List head */
7538 akl->list.n = akl->list.p = NULL;
7539
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007540 /* action keyword. */
7541 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007542 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007543 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007544 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007545
7546 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7547
7548 akl->kw[0].match_pfx = 0;
7549 akl->kw[0].private = fcn;
7550 akl->kw[0].parse = action_register_lua;
7551
7552 /* select the action registering point. */
7553 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7554 tcp_req_cont_keywords_register(akl);
7555 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7556 tcp_res_cont_keywords_register(akl);
7557 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7558 http_req_keywords_register(akl);
7559 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7560 http_res_keywords_register(akl);
7561 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007562 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007563 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7564 "are expected.", lua_tostring(L, -1)));
7565
7566 /* pop the environment string. */
7567 lua_pop(L, 1);
7568 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007569 return ACT_RET_PRS_OK;
7570}
7571
7572static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7573 struct act_rule *rule, char **err)
7574{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007575 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007576
Christopher Faulet280f85b2019-07-15 15:02:04 +02007577 if (px->mode == PR_MODE_HTTP) {
7578 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007579 return ACT_RET_PRS_ERR;
7580 }
7581
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007582 /* Memory for the rule. */
7583 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7584 if (!rule->arg.hlua_rule) {
7585 memprintf(err, "out of memory error");
7586 return ACT_RET_PRS_ERR;
7587 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007588
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007589 /* Reference the Lua function and store the reference. */
7590 rule->arg.hlua_rule->fcn = *fcn;
7591
7592 /* TODO: later accept arguments. */
7593 rule->arg.hlua_rule->args = NULL;
7594
7595 /* Add applet pointer in the rule. */
7596 rule->applet.obj_type = OBJ_TYPE_APPLET;
7597 rule->applet.name = fcn->name;
7598 rule->applet.init = hlua_applet_tcp_init;
7599 rule->applet.fct = hlua_applet_tcp_fct;
7600 rule->applet.release = hlua_applet_tcp_release;
7601 rule->applet.timeout = hlua_timeout_applet;
7602
7603 return 0;
7604}
7605
7606/* This function is an LUA binding used for registering
7607 * "sample-conv" functions. It expects a converter name used
7608 * in the haproxy configuration file, and an LUA function.
7609 */
7610__LJMP static int hlua_register_service(lua_State *L)
7611{
7612 struct action_kw_list *akl;
7613 const char *name;
7614 const char *env;
7615 int ref;
7616 int len;
7617 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007618 struct buffer *trash;
7619 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007620
7621 MAY_LJMP(check_args(L, 3, "register_service"));
7622
7623 /* First argument : converter name. */
7624 name = MAY_LJMP(luaL_checkstring(L, 1));
7625
7626 /* Second argument : environment. */
7627 env = MAY_LJMP(luaL_checkstring(L, 2));
7628
7629 /* Third argument : lua function. */
7630 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7631
Thierry Fournierf67442e2020-11-28 20:41:07 +01007632 /* Check for service already registered */
7633 trash = get_trash_chunk();
7634 chunk_printf(trash, "lua.%s", name);
7635 akw = service_find(trash->area);
7636 if (akw != NULL) {
7637 ha_warning("Trying to register service 'lua.%s' more than once. "
7638 "This will become a hard error in version 2.5.\n", name);
7639 }
7640
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007641 /* Allocate and fill the sample fetch keyword struct. */
7642 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7643 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007644 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007645 fcn = calloc(1, sizeof(*fcn));
7646 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007647 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007648
7649 /* Fill fcn. */
7650 len = strlen("<lua.>") + strlen(name) + 1;
7651 fcn->name = calloc(1, len);
7652 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007653 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007654 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7655 fcn->function_ref = ref;
7656
7657 /* List head */
7658 akl->list.n = akl->list.p = NULL;
7659
7660 /* converter keyword. */
7661 len = strlen("lua.") + strlen(name) + 1;
7662 akl->kw[0].kw = calloc(1, len);
7663 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007664 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007665
7666 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7667
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007668 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007669 if (strcmp(env, "tcp") == 0)
7670 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007671 else if (strcmp(env, "http") == 0)
7672 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007673 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007674 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007675 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007676
7677 akl->kw[0].match_pfx = 0;
7678 akl->kw[0].private = fcn;
7679
7680 /* End of array. */
7681 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7682
7683 /* Register this new converter */
7684 service_keywords_register(akl);
7685
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007686 return 0;
7687}
7688
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007689/* This function initialises Lua cli handler. It copies the
7690 * arguments in the Lua stack and create channel IO objects.
7691 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007692static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007693{
7694 struct hlua *hlua;
7695 struct hlua_function *fcn;
7696 int i;
7697 const char *error;
7698
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007699 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007700 appctx->ctx.hlua_cli.fcn = private;
7701
Willy Tarreaubafbe012017-11-24 17:34:44 +01007702 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007703 if (!hlua) {
7704 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007705 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007706 }
7707 HLUA_INIT(hlua);
7708 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007709
7710 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007711 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007712 * applet_http. It is absolutely compatible.
7713 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007714 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007715 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007716 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007717 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007718 }
7719 appctx->ctx.hlua_cli.task->nice = 0;
7720 appctx->ctx.hlua_cli.task->context = appctx;
7721 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7722
7723 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007724 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007725 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007726 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007727 }
7728
7729 /* The following Lua calls can fail. */
7730 if (!SET_SAFE_LJMP(hlua->T)) {
7731 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7732 error = lua_tostring(hlua->T, -1);
7733 else
7734 error = "critical error";
7735 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7736 goto error;
7737 }
7738
7739 /* Check stack available size. */
7740 if (!lua_checkstack(hlua->T, 2)) {
7741 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7742 goto error;
7743 }
7744
7745 /* Restore the function in the stack. */
7746 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7747
7748 /* Once the arguments parsed, the CLI is like an AppletTCP,
7749 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007750 */
7751 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7752 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7753 goto error;
7754 }
7755 hlua->nargs = 1;
7756
7757 /* push keywords in the stack. */
7758 for (i = 0; *args[i]; i++) {
7759 /* Check stack available size. */
7760 if (!lua_checkstack(hlua->T, 1)) {
7761 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7762 goto error;
7763 }
7764 lua_pushstring(hlua->T, args[i]);
7765 hlua->nargs++;
7766 }
7767
7768 /* We must initialize the execution timeouts. */
7769 hlua->max_time = hlua_timeout_session;
7770
7771 /* At this point the execution is safe. */
7772 RESET_SAFE_LJMP(hlua->T);
7773
7774 /* It's ok */
7775 return 0;
7776
7777 /* It's not ok. */
7778error:
7779 RESET_SAFE_LJMP(hlua->T);
7780 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007781 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007782 return 1;
7783}
7784
7785static int hlua_cli_io_handler_fct(struct appctx *appctx)
7786{
7787 struct hlua *hlua;
7788 struct stream_interface *si;
7789 struct hlua_function *fcn;
7790
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007791 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007792 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007793 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007794
7795 /* If the stream is disconnect or closed, ldo nothing. */
7796 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7797 return 1;
7798
7799 /* Execute the function. */
7800 switch (hlua_ctx_resume(hlua, 1)) {
7801
7802 /* finished. */
7803 case HLUA_E_OK:
7804 return 1;
7805
7806 /* yield. */
7807 case HLUA_E_AGAIN:
7808 /* We want write. */
7809 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007810 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007811 /* Set the timeout. */
7812 if (hlua->wake_time != TICK_ETERNITY)
7813 task_schedule(hlua->task, hlua->wake_time);
7814 return 0;
7815
7816 /* finished with error. */
7817 case HLUA_E_ERRMSG:
7818 /* Display log. */
7819 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7820 fcn->name, lua_tostring(hlua->T, -1));
7821 lua_pop(hlua->T, 1);
7822 return 1;
7823
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007824 case HLUA_E_ETMOUT:
7825 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7826 fcn->name);
7827 return 1;
7828
7829 case HLUA_E_NOMEM:
7830 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7831 fcn->name);
7832 return 1;
7833
7834 case HLUA_E_YIELD: /* unexpected */
7835 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7836 fcn->name);
7837 return 1;
7838
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007839 case HLUA_E_ERR:
7840 /* Display log. */
7841 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7842 fcn->name);
7843 return 1;
7844
7845 default:
7846 return 1;
7847 }
7848
7849 return 1;
7850}
7851
7852static void hlua_cli_io_release_fct(struct appctx *appctx)
7853{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007854 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007855 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007856}
7857
7858/* This function is an LUA binding used for registering
7859 * new keywords in the cli. It expects a list of keywords
7860 * which are the "path". It is limited to 5 keywords. A
7861 * description of the command, a function to be executed
7862 * for the parsing and a function for io handlers.
7863 */
7864__LJMP static int hlua_register_cli(lua_State *L)
7865{
7866 struct cli_kw_list *cli_kws;
7867 const char *message;
7868 int ref_io;
7869 int len;
7870 struct hlua_function *fcn;
7871 int index;
7872 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007873 struct buffer *trash;
7874 const char *kw[5];
7875 struct cli_kw *cli_kw;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007876
7877 MAY_LJMP(check_args(L, 3, "register_cli"));
7878
7879 /* First argument : an array of maximum 5 keywords. */
7880 if (!lua_istable(L, 1))
7881 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7882
7883 /* Second argument : string with contextual message. */
7884 message = MAY_LJMP(luaL_checkstring(L, 2));
7885
7886 /* Third and fourth argument : lua function. */
7887 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7888
Thierry Fournierf67442e2020-11-28 20:41:07 +01007889 /* Check for CLI service already registered */
7890 trash = get_trash_chunk();
7891 index = 0;
7892 lua_pushnil(L);
7893 memset(kw, 0, sizeof(kw));
7894 while (lua_next(L, 1) != 0) {
7895 if (index >= CLI_PREFIX_KW_NB)
7896 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7897 if (lua_type(L, -1) != LUA_TSTRING)
7898 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7899 kw[index] = lua_tostring(L, -1);
7900 if (index == 0)
7901 chunk_printf(trash, "%s", kw[index]);
7902 else
7903 chunk_appendf(trash, " %s", kw[index]);
7904 index++;
7905 lua_pop(L, 1);
7906 }
7907 cli_kw = cli_find_kw_exact((char **)kw);
7908 if (cli_kw != NULL) {
7909 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
7910 "This will become a hard error in version 2.5.\n", trash->area);
7911 }
7912
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007913 /* Allocate and fill the sample fetch keyword struct. */
7914 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7915 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007916 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007917 fcn = calloc(1, sizeof(*fcn));
7918 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007919 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007920
7921 /* Fill path. */
7922 index = 0;
7923 lua_pushnil(L);
7924 while(lua_next(L, 1) != 0) {
7925 if (index >= 5)
7926 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7927 if (lua_type(L, -1) != LUA_TSTRING)
7928 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7929 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7930 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007931 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007932 index++;
7933 lua_pop(L, 1);
7934 }
7935
7936 /* Copy help message. */
7937 cli_kws->kw[0].usage = strdup(message);
7938 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007939 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007940
7941 /* Fill fcn io handler. */
7942 len = strlen("<lua.cli>") + 1;
7943 for (i = 0; i < index; i++)
7944 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7945 fcn->name = calloc(1, len);
7946 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007947 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007948 strncat((char *)fcn->name, "<lua.cli", len);
7949 for (i = 0; i < index; i++) {
7950 strncat((char *)fcn->name, ".", len);
7951 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7952 }
7953 strncat((char *)fcn->name, ">", len);
7954 fcn->function_ref = ref_io;
7955
7956 /* Fill last entries. */
7957 cli_kws->kw[0].private = fcn;
7958 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7959 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7960 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7961
7962 /* Register this new converter */
7963 cli_register_kw(cli_kws);
7964
7965 return 0;
7966}
7967
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007968static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7969 struct proxy *defpx, const char *file, int line,
7970 char **err, unsigned int *timeout)
7971{
7972 const char *error;
7973
7974 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007975 if (error == PARSE_TIME_OVER) {
7976 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7977 args[1], args[0]);
7978 return -1;
7979 }
7980 else if (error == PARSE_TIME_UNDER) {
7981 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7982 args[1], args[0]);
7983 return -1;
7984 }
7985 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007986 memprintf(err, "%s: invalid timeout", args[0]);
7987 return -1;
7988 }
7989 return 0;
7990}
7991
7992static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7993 struct proxy *defpx, const char *file, int line,
7994 char **err)
7995{
7996 return hlua_read_timeout(args, section_type, curpx, defpx,
7997 file, line, err, &hlua_timeout_session);
7998}
7999
8000static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8001 struct proxy *defpx, const char *file, int line,
8002 char **err)
8003{
8004 return hlua_read_timeout(args, section_type, curpx, defpx,
8005 file, line, err, &hlua_timeout_task);
8006}
8007
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008008static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8009 struct proxy *defpx, const char *file, int line,
8010 char **err)
8011{
8012 return hlua_read_timeout(args, section_type, curpx, defpx,
8013 file, line, err, &hlua_timeout_applet);
8014}
8015
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008016static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
8017 struct proxy *defpx, const char *file, int line,
8018 char **err)
8019{
8020 char *error;
8021
8022 hlua_nb_instruction = strtoll(args[1], &error, 10);
8023 if (*error != '\0') {
8024 memprintf(err, "%s: invalid number", args[0]);
8025 return -1;
8026 }
8027 return 0;
8028}
8029
Willy Tarreau32f61e22015-03-18 17:54:59 +01008030static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8031 struct proxy *defpx, const char *file, int line,
8032 char **err)
8033{
8034 char *error;
8035
8036 if (*(args[1]) == 0) {
8037 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8038 return -1;
8039 }
8040 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8041 if (*error != '\0') {
8042 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8043 return -1;
8044 }
8045 return 0;
8046}
8047
8048
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008049/* This function is called by the main configuration key "lua-load". It loads and
8050 * execute an lua file during the parsing of the HAProxy configuration file. It is
8051 * the main lua entry point.
8052 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008053 * This function runs with the HAProxy keywords API. It returns -1 if an error
8054 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008055 *
8056 * In some error case, LUA set an error message in top of the stack. This function
8057 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008058 *
8059 * This function can fail with an abort() due to an Lua critical error.
8060 * We are in the configuration parsing process of HAProxy, this abort() is
8061 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008062 */
8063static int hlua_load(char **args, int section_type, struct proxy *curpx,
8064 struct proxy *defpx, const char *file, int line,
8065 char **err)
8066{
8067 int error;
8068
Thierry Fournier77a88942020-11-29 01:06:24 +01008069 if (*(args[1]) == 0) {
8070 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8071 return -1;
8072 }
8073
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008074 /* Just load and compile the file. */
8075 error = luaL_loadfile(gL.T, args[1]);
8076 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008077 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008078 lua_pop(gL.T, 1);
8079 return -1;
8080 }
8081
8082 /* If no syntax error where detected, execute the code. */
8083 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
8084 switch (error) {
8085 case LUA_OK:
8086 break;
8087 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008088 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008089 lua_pop(gL.T, 1);
8090 return -1;
8091 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008092 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008093 return -1;
8094 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008095 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008096 lua_pop(gL.T, 1);
8097 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008098#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008099 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008100 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008101 lua_pop(gL.T, 1);
8102 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008103#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008104 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05008105 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008106 lua_pop(gL.T, 1);
8107 return -1;
8108 }
8109
8110 return 0;
8111}
8112
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008113/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8114 * in the given <ctx>.
8115 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008116static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008117{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008118 lua_getglobal(L, "package"); /* push package variable */
8119 lua_pushstring(L, path); /* push given path */
8120 lua_pushstring(L, ";"); /* push semicolon */
8121 lua_getfield(L, -3, type); /* push old path */
8122 lua_concat(L, 3); /* concatenate to new path */
8123 lua_setfield(L, -2, type); /* store new path */
8124 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008125
8126 return 0;
8127}
8128
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008129static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
8130 struct proxy *defpx, const char *file, int line,
8131 char **err)
8132{
8133 char *path;
8134 char *type = "path";
8135 if (too_many_args(2, args, err, NULL)) {
8136 return -1;
8137 }
8138
8139 if (!(*args[1])) {
8140 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
8141 return -1;
8142 }
8143 path = args[1];
8144
8145 if (*args[2]) {
8146 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8147 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
8148 return -1;
8149 }
8150 type = args[2];
8151 }
8152
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008153 return hlua_prepend_path(gL.T, type, path);
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008154}
8155
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008156/* configuration keywords declaration */
8157static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008158 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008159 { CFG_GLOBAL, "lua-load", hlua_load },
8160 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8161 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008162 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008163 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008164 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008165 { 0, NULL, NULL },
8166}};
8167
Willy Tarreau0108d902018-11-25 19:14:37 +01008168INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8169
Christopher Fauletafd8f102018-11-08 11:34:21 +01008170
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008171/* This function can fail with an abort() due to an Lua critical error.
8172 * We are in the initialisation process of HAProxy, this abort() is
8173 * tolerated.
8174 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008175int hlua_post_init()
8176{
8177 struct hlua_init_function *init;
8178 const char *msg;
8179 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008180 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008181 const char *kind;
8182 const char *trace;
8183 int return_status = 1;
8184#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8185 int nres;
8186#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008187
Willy Tarreaucdb53462020-12-02 12:12:00 +01008188 /* disable memory limit checks if limit is not set */
8189 if (!hlua_global_allocator.limit)
8190 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8191
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008192 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008193 if (!SET_SAFE_LJMP(gL.T)) {
8194 if (lua_type(gL.T, -1) == LUA_TSTRING)
8195 error = lua_tostring(gL.T, -1);
8196 else
8197 error = "critical error";
8198 fprintf(stderr, "Lua post-init: %s.\n", error);
8199 exit(1);
8200 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008201
8202#if USE_OPENSSL
8203 /* Initialize SSL server. */
8204 if (socket_ssl.xprt->prepare_srv) {
8205 int saved_used_backed = global.ssl_used_backend;
8206 // don't affect maxconn automatic computation
8207 socket_ssl.xprt->prepare_srv(&socket_ssl);
8208 global.ssl_used_backend = saved_used_backed;
8209 }
8210#endif
8211
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008212 hlua_fcn_post_init(gL.T);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008213
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008214 list_for_each_entry(init, &hlua_init_functions, l) {
8215 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008216
8217#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8218 ret = lua_resume(gL.T, gL.T, 0, &nres);
8219#else
8220 ret = lua_resume(gL.T, gL.T, 0);
8221#endif
8222 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008223 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008224
8225 case LUA_OK:
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008226 lua_pop(gL.T, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008227 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008228
8229 case LUA_ERRERR:
8230 kind = "message handler error";
8231 /* Fall thru */
8232 case LUA_ERRRUN:
8233 if (!kind)
8234 kind = "runtime error";
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008235 msg = lua_tostring(gL.T, -1);
Thierry Fournier670db242020-11-28 10:49:59 +01008236 lua_settop(gL.T, 0); /* Empty the stack. */
8237 lua_pop(gL.T, 1);
8238 trace = hlua_traceback(gL.T);
8239 if (msg)
8240 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8241 else
8242 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8243 return_status = 0;
8244 break;
8245
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008246 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008247 /* Unknown error */
8248 kind = "Unknown error";
8249 /* Fall thru */
8250 case LUA_YIELD:
8251 /* yield is not configured at this step, this state doesn't happen */
8252 if (!kind)
8253 kind = "yield not allowed";
8254 /* Fall thru */
8255 case LUA_ERRMEM:
8256 if (!kind)
8257 kind = "out of memory error";
8258 lua_settop(gL.T, 0);
8259 lua_pop(gL.T, 1);
8260 trace = hlua_traceback(gL.T);
8261 ha_alert("Lua init: %s: %s\n", kind, trace);
8262 return_status = 0;
8263 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008264 }
Thierry Fournier670db242020-11-28 10:49:59 +01008265 if (!return_status)
8266 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008267 }
Thierry Fournier670db242020-11-28 10:49:59 +01008268 RESET_SAFE_LJMP(gL.T);
8269 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008270}
8271
Willy Tarreau32f61e22015-03-18 17:54:59 +01008272/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8273 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8274 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008275 * allocation. <nsize> is the requested new size. A new allocation is
8276 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008277 * zero. This one verifies that the limits are respected but is optimized
8278 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008279 */
8280static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8281{
8282 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008283 size_t limit, old, new;
8284
8285 /* a limit of ~0 means unlimited and boot complete, so there's no need
8286 * for accounting anymore.
8287 */
8288 if (likely(~zone->limit == 0))
8289 return realloc(ptr, nsize);
Willy Tarreau32f61e22015-03-18 17:54:59 +01008290
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008291 if (!ptr)
8292 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008293
Willy Tarreaucdb53462020-12-02 12:12:00 +01008294 /* enforce strict limits across all threads */
8295 limit = zone->limit;
8296 old = _HA_ATOMIC_LOAD(&zone->allocated);
8297 do {
8298 new = old + nsize - osize;
8299 if (unlikely(nsize && limit && new > limit))
8300 return NULL;
8301 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008302
8303 ptr = realloc(ptr, nsize);
Willy Tarreaucdb53462020-12-02 12:12:00 +01008304
8305 if (unlikely(!ptr && nsize)) // failed
8306 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8307
8308 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008309 return ptr;
8310}
8311
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008312/* Ithis function can fail with an abort() due to an Lua critical error.
8313 * We are in the initialisation process of HAProxy, this abort() is
8314 * tolerated.
8315 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008316void hlua_init(void)
8317{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008318 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008319 int idx;
8320 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008321 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008322 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008323 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008324#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008325 struct srv_kw *kw;
8326 int tmp_error;
8327 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008328 char *args[] = { /* SSL client configuration. */
8329 "ssl",
8330 "verify",
8331 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008332 NULL
8333 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008334#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008335
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008336 /* Init main lua stack. */
8337 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008338 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008339 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008340 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008341 hlua_sethlua(&gL);
8342 gL.Tref = LUA_REFNIL;
8343 gL.task = NULL;
8344
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008345 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008346 * the Lua function can fail with an abort. We are in the initialisation
8347 * process of HAProxy, this abort() is tolerated.
8348 */
8349
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008350 /* Set safe environment for the initialisation. */
8351 if (!SET_SAFE_LJMP(gL.T)) {
8352 if (lua_type(gL.T, -1) == LUA_TSTRING)
8353 error_msg = lua_tostring(gL.T, -1);
8354 else
8355 error_msg = "critical error";
8356 fprintf(stderr, "Lua init: %s.\n", error_msg);
8357 exit(1);
8358 }
8359
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008360 /* Initialise lua. */
8361 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008362#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8363#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8364#ifdef HLUA_PREPEND_PATH
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008365 hlua_prepend_path(gL.T, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008366#endif
8367#ifdef HLUA_PREPEND_CPATH
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008368 hlua_prepend_path(gL.T, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008369#endif
8370#undef HLUA_PREPEND_PATH_TOSTRING
8371#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008372
8373 /*
8374 *
8375 * Create "core" object.
8376 *
8377 */
8378
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008379 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008380 lua_newtable(gL.T);
8381
8382 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008383 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008384 hlua_class_const_int(gL.T, log_levels[i], i);
8385
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008386 /* Register special functions. */
8387 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008388 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008389 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008390 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008391 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008392 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008393 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008394 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008395 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008396 hlua_class_function(gL.T, "sleep", hlua_sleep);
8397 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008398 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8399 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8400 hlua_class_function(gL.T, "set_map", hlua_set_map);
8401 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008402 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008403 hlua_class_function(gL.T, "log", hlua_log);
8404 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8405 hlua_class_function(gL.T, "Info", hlua_log_info);
8406 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8407 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008408 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008409 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008410
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008411 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008412
8413 /*
8414 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008415 * Create "act" object.
8416 *
8417 */
8418
8419 /* This table entry is the object "act" base. */
8420 lua_newtable(gL.T);
8421
8422 /* push action return constants */
8423 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8424 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8425 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8426 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8427 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8428 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8429 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8430 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8431
Christopher Faulet501465d2020-02-26 14:54:16 +01008432 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008433
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008434 lua_setglobal(gL.T, "act");
8435
8436 /*
8437 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008438 * Register class Map
8439 *
8440 */
8441
8442 /* This table entry is the object "Map" base. */
8443 lua_newtable(gL.T);
8444
8445 /* register pattern types. */
8446 for (i=0; i<PAT_MATCH_NUM; i++)
8447 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008448 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008449 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8450 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008451 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008452
8453 /* register constructor. */
8454 hlua_class_function(gL.T, "new", hlua_map_new);
8455
8456 /* Create and fill the metatable. */
8457 lua_newtable(gL.T);
8458
Ilya Shipitsind4259502020-04-08 01:07:56 +05008459 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008460 lua_pushstring(gL.T, "__index");
8461 lua_newtable(gL.T);
8462
8463 /* Register . */
8464 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8465 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8466
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008467 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008468
Thierry Fournier45e78d72016-02-19 18:34:46 +01008469 /* Register previous table in the registry with reference and named entry.
8470 * The function hlua_register_metatable() pops the stack, so we
8471 * previously create a copy of the table.
8472 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008473 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008474 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008475
8476 /* Assign the metatable to the mai Map object. */
8477 lua_setmetatable(gL.T, -2);
8478
8479 /* Set a name to the table. */
8480 lua_setglobal(gL.T, "Map");
8481
8482 /*
8483 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008484 * Register class Channel
8485 *
8486 */
8487
8488 /* Create and fill the metatable. */
8489 lua_newtable(gL.T);
8490
Ilya Shipitsind4259502020-04-08 01:07:56 +05008491 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008492 lua_pushstring(gL.T, "__index");
8493 lua_newtable(gL.T);
8494
8495 /* Register . */
8496 hlua_class_function(gL.T, "get", hlua_channel_get);
8497 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8498 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8499 hlua_class_function(gL.T, "set", hlua_channel_set);
8500 hlua_class_function(gL.T, "append", hlua_channel_append);
8501 hlua_class_function(gL.T, "send", hlua_channel_send);
8502 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8503 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8504 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008505 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008506 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008507
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008508 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008509
8510 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008511 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008512
8513 /*
8514 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008515 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008516 *
8517 */
8518
8519 /* Create and fill the metatable. */
8520 lua_newtable(gL.T);
8521
Ilya Shipitsind4259502020-04-08 01:07:56 +05008522 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008523 lua_pushstring(gL.T, "__index");
8524 lua_newtable(gL.T);
8525
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008526 /* Browse existing fetches and create the associated
8527 * object method.
8528 */
8529 sf = NULL;
8530 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008531 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8532 * by an underscore.
8533 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008534 strncpy(trash.area, sf->kw, trash.size);
8535 trash.area[trash.size - 1] = '\0';
8536 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008537 if (*p == '.' || *p == '-' || *p == '+')
8538 *p = '_';
8539
8540 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008541 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008542 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008543 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008544 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008545 }
8546
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008547 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008548
8549 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008550 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008551
8552 /*
8553 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008554 * Register class Converters
8555 *
8556 */
8557
8558 /* Create and fill the metatable. */
8559 lua_newtable(gL.T);
8560
8561 /* Create and fill the __index entry. */
8562 lua_pushstring(gL.T, "__index");
8563 lua_newtable(gL.T);
8564
8565 /* Browse existing converters and create the associated
8566 * object method.
8567 */
8568 sc = NULL;
8569 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008570 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8571 * by an underscore.
8572 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008573 strncpy(trash.area, sc->kw, trash.size);
8574 trash.area[trash.size - 1] = '\0';
8575 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008576 if (*p == '.' || *p == '-' || *p == '+')
8577 *p = '_';
8578
8579 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008580 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008581 lua_pushlightuserdata(gL.T, sc);
8582 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008583 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008584 }
8585
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008586 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008587
8588 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008589 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008590
8591 /*
8592 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008593 * Register class HTTP
8594 *
8595 */
8596
8597 /* Create and fill the metatable. */
8598 lua_newtable(gL.T);
8599
Ilya Shipitsind4259502020-04-08 01:07:56 +05008600 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008601 lua_pushstring(gL.T, "__index");
8602 lua_newtable(gL.T);
8603
8604 /* Register Lua functions. */
8605 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8606 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8607 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8608 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8609 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8610 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8611 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8612 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8613 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8614 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8615
8616 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8617 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8618 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8619 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8620 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8621 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008622 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008623
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008624 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008625
8626 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008627 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008628
8629 /*
8630 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008631 * Register class AppletTCP
8632 *
8633 */
8634
8635 /* Create and fill the metatable. */
8636 lua_newtable(gL.T);
8637
Ilya Shipitsind4259502020-04-08 01:07:56 +05008638 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008639 lua_pushstring(gL.T, "__index");
8640 lua_newtable(gL.T);
8641
8642 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008643 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8644 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8645 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8646 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8647 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008648 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8649 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8650 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008651
8652 lua_settable(gL.T, -3);
8653
8654 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008655 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008656
8657 /*
8658 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008659 * Register class AppletHTTP
8660 *
8661 */
8662
8663 /* Create and fill the metatable. */
8664 lua_newtable(gL.T);
8665
Ilya Shipitsind4259502020-04-08 01:07:56 +05008666 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008667 lua_pushstring(gL.T, "__index");
8668 lua_newtable(gL.T);
8669
8670 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008671 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8672 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008673 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8674 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8675 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008676 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8677 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8678 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8679 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8680 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8681 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8682
8683 lua_settable(gL.T, -3);
8684
8685 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008686 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008687
8688 /*
8689 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008690 * Register class TXN
8691 *
8692 */
8693
8694 /* Create and fill the metatable. */
8695 lua_newtable(gL.T);
8696
Ilya Shipitsind4259502020-04-08 01:07:56 +05008697 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008698 lua_pushstring(gL.T, "__index");
8699 lua_newtable(gL.T);
8700
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008701 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008702 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8703 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8704 hlua_class_function(gL.T, "set_var", hlua_set_var);
8705 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8706 hlua_class_function(gL.T, "get_var", hlua_get_var);
8707 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008708 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008709 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8710 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8711 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8712 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8713 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8714 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8715 hlua_class_function(gL.T, "log", hlua_txn_log);
8716 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8717 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8718 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8719 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008720
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008721 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008722
8723 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008724 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008725
8726 /*
8727 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008728 * Register class reply
8729 *
8730 */
8731 lua_newtable(gL.T);
8732 lua_pushstring(gL.T, "__index");
8733 lua_newtable(gL.T);
8734 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8735 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8736 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8737 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8738 lua_settable(gL.T, -3); /* Sets the __index entry. */
8739 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8740
8741
8742 /*
8743 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008744 * Register class Socket
8745 *
8746 */
8747
8748 /* Create and fill the metatable. */
8749 lua_newtable(gL.T);
8750
Ilya Shipitsind4259502020-04-08 01:07:56 +05008751 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008752 lua_pushstring(gL.T, "__index");
8753 lua_newtable(gL.T);
8754
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008755#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008756 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008757#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008758 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8759 hlua_class_function(gL.T, "send", hlua_socket_send);
8760 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8761 hlua_class_function(gL.T, "close", hlua_socket_close);
8762 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8763 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8764 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8765 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8766
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008767 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008768
8769 /* Register the garbage collector entry. */
8770 lua_pushstring(gL.T, "__gc");
8771 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008772 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008773
8774 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008775 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008776
8777 /* Proxy and server configuration initialisation. */
8778 memset(&socket_proxy, 0, sizeof(socket_proxy));
8779 init_new_proxy(&socket_proxy);
8780 socket_proxy.parent = NULL;
8781 socket_proxy.last_change = now.tv_sec;
8782 socket_proxy.id = "LUA-SOCKET";
8783 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8784 socket_proxy.maxconn = 0;
8785 socket_proxy.accept = NULL;
8786 socket_proxy.options2 |= PR_O2_INDEPSTR;
8787 socket_proxy.srv = NULL;
8788 socket_proxy.conn_retries = 0;
8789 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8790
8791 /* Init TCP server: unchanged parameters */
8792 memset(&socket_tcp, 0, sizeof(socket_tcp));
8793 socket_tcp.next = NULL;
8794 socket_tcp.proxy = &socket_proxy;
8795 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8796 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008797 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008798 socket_tcp.idle_conns = NULL;
8799 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008800 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008801 socket_tcp.last_change = 0;
8802 socket_tcp.id = "LUA-TCP-CONN";
8803 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8804 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8805 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8806
8807 /* XXX: Copy default parameter from default server,
8808 * but the default server is not initialized.
8809 */
8810 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8811 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8812 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8813 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8814 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8815 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8816 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8817 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8818 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8819 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8820
8821 socket_tcp.check.status = HCHK_STATUS_INI;
8822 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8823 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8824 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8825 socket_tcp.check.server = &socket_tcp;
8826
8827 socket_tcp.agent.status = HCHK_STATUS_INI;
8828 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8829 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8830 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8831 socket_tcp.agent.server = &socket_tcp;
8832
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008833 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008834
8835#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008836 /* Init TCP server: unchanged parameters */
8837 memset(&socket_ssl, 0, sizeof(socket_ssl));
8838 socket_ssl.next = NULL;
8839 socket_ssl.proxy = &socket_proxy;
8840 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8841 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008842 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008843 socket_ssl.idle_conns = NULL;
8844 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008845 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008846 socket_ssl.last_change = 0;
8847 socket_ssl.id = "LUA-SSL-CONN";
8848 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8849 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8850 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8851
8852 /* XXX: Copy default parameter from default server,
8853 * but the default server is not initialized.
8854 */
8855 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8856 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8857 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8858 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8859 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8860 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8861 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8862 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8863 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8864 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8865
8866 socket_ssl.check.status = HCHK_STATUS_INI;
8867 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8868 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8869 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8870 socket_ssl.check.server = &socket_ssl;
8871
8872 socket_ssl.agent.status = HCHK_STATUS_INI;
8873 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8874 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8875 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8876 socket_ssl.agent.server = &socket_ssl;
8877
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008878 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008879 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008880
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008881 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008882 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8883 /*
8884 *
8885 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008886 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008887 * features like client certificates and ssl_verify.
8888 *
8889 */
8890 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8891 if (tmp_error != 0) {
8892 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8893 abort(); /* This must be never arrives because the command line
8894 not editable by the user. */
8895 }
8896 idx += kw->skip;
8897 }
8898 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008899#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008900
8901 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008902}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008903
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02008904static void hlua_deinit()
8905{
8906 lua_close(gL.T);
8907}
8908
8909REGISTER_POST_DEINIT(hlua_deinit);
8910
Willy Tarreau80713382018-11-26 10:19:54 +01008911static void hlua_register_build_options(void)
8912{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008913 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008914
Willy Tarreaubb57d942016-12-21 19:04:56 +01008915 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8916 hap_register_build_opts(ptr, 1);
8917}
Willy Tarreau80713382018-11-26 10:19:54 +01008918
8919INITCALL0(STG_REGISTER, hlua_register_build_options);