blob: efbc90af3a5f6e3d666aab9db469687b4a6fae3a [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua unsafe core engine
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010013#include <ctype.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020014#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010015
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010016#include <lauxlib.h>
17#include <lua.h>
18#include <lualib.h>
19
Thierry FOURNIER463119c2015-03-10 00:35:36 +010020#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
21#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010022#endif
23
Willy Tarreau8d2b7772020-05-27 10:58:19 +020024#include <import/ebpttree.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010025
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/api.h>
27#include <haproxy/applet.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020028#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020029#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020030#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020031#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020032#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020033#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020034#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020035#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020036#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020037#include <haproxy/http_fetch.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020039#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020040#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020041#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020042#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020043#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020044#include <haproxy/payload.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020045#include <haproxy/proxy-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020047#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020048#include <haproxy/server-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020049#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020050#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020051#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020052#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020053#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020054#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020055#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020056#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020057#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020058#include <haproxy/xref.h>
59
Thierry FOURNIER380d0932015-01-23 14:27:52 +010060
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010061/* Lua uses longjmp to perform yield or throwing errors. This
62 * macro is used only for identifying the function that can
63 * not return because a longjmp is executed.
64 * __LJMP marks a prototype of hlua file that can use longjmp.
65 * WILL_LJMP() marks an lua function that will use longjmp.
66 * MAY_LJMP() marks an lua function that may use longjmp.
67 */
68#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020069#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010070#define MAY_LJMP(func) func
71
Thierry FOURNIERbabae282015-09-17 11:36:37 +020072/* This couple of function executes securely some Lua calls outside of
73 * the lua runtime environment. Each Lua call can return a longjmp
74 * if it encounter a memory error.
75 *
76 * Lua documentation extract:
77 *
78 * If an error happens outside any protected environment, Lua calls
79 * a panic function (see lua_atpanic) and then calls abort, thus
80 * exiting the host application. Your panic function can avoid this
81 * exit by never returning (e.g., doing a long jump to your own
82 * recovery point outside Lua).
83 *
84 * The panic function runs as if it were a message handler (see
85 * §2.3); in particular, the error message is at the top of the
86 * stack. However, there is no guarantee about stack space. To push
87 * anything on the stack, the panic function must first check the
88 * available space (see §4.2).
89 *
90 * We must check all the Lua entry point. This includes:
91 * - The include/proto/hlua.h exported functions
92 * - the task wrapper function
93 * - The action wrapper function
94 * - The converters wrapper function
95 * - The sample-fetch wrapper functions
96 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +050097 * It is tolerated that the initialisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080098 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +020099 *
100 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
101 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
102 * because they must be exists in the program stack when the longjmp
103 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200104 *
105 * Note that the Lua processing is not really thread safe. It provides
106 * heavy system which consists to add our own lock function in the Lua
107 * code and recompile the library. This system will probably not accepted
108 * by maintainers of various distribs.
109 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500110 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200111 * quick looking on the Lua sources displays a lua_lock() a the start
112 * of function and a lua_unlock() at the end of the function. So I
113 * conclude that the Lua thread safe mode just perform a mutex around
114 * all execution. So I prefer to do this in the HAProxy code, it will be
115 * easier for distro maintainers.
116 *
117 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
118 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
119 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200120 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100121__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200122THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100124static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200125
126#define SET_SAFE_LJMP(__L) \
127 ({ \
128 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100129 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200130 if (setjmp(safe_ljmp_env) != 0) { \
131 lua_atpanic(__L, hlua_panic_safe); \
132 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100133 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200134 } else { \
135 lua_atpanic(__L, hlua_panic_ljmp); \
136 ret = 1; \
137 } \
138 ret; \
139 })
140
141/* If we are the last function catching Lua errors, we
142 * must reset the panic function.
143 */
144#define RESET_SAFE_LJMP(__L) \
145 do { \
146 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100147 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200148 } while(0)
149
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200150/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200151#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100152/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200153#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200154/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100155#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100156#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200157
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100158/* The main Lua execution context. */
159struct hlua gL;
160
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100161/* This is the memory pool containing struct lua for applets
162 * (including cli).
163 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100164DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100165
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100166/* Used for Socket connection. */
167static struct proxy socket_proxy;
168static struct server socket_tcp;
169#ifdef USE_OPENSSL
170static struct server socket_ssl;
171#endif
172
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100173/* List head of the function called at the initialisation time. */
174struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
175
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100176/* The following variables contains the reference of the different
177 * Lua classes. These references are useful for identify metadata
178 * associated with an object.
179 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100180static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100181static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100182static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100183static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100184static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100185static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200186static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200187static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200188static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100189static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100190
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100191/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200192 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100193 * short timeout. Lua linked with tasks doesn't have a timeout
194 * because a task may remain alive during all the haproxy execution.
195 */
196static unsigned int hlua_timeout_session = 4000; /* session timeout. */
197static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200198static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100199
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100200/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
201 * it is used for preventing infinite loops.
202 *
203 * I test the scheer with an infinite loop containing one incrementation
204 * and one test. I run this loop between 10 seconds, I raise a ceil of
205 * 710M loops from one interrupt each 9000 instructions, so I fix the value
206 * to one interrupt each 10 000 instructions.
207 *
208 * configured | Number of
209 * instructions | loops executed
210 * between two | in milions
211 * forced yields |
212 * ---------------+---------------
213 * 10 | 160
214 * 500 | 670
215 * 1000 | 680
216 * 5000 | 700
217 * 7000 | 700
218 * 8000 | 700
219 * 9000 | 710 <- ceil
220 * 10000 | 710
221 * 100000 | 710
222 * 1000000 | 710
223 *
224 */
225static unsigned int hlua_nb_instruction = 10000;
226
Willy Tarreau32f61e22015-03-18 17:54:59 +0100227/* Descriptor for the memory allocation state. If limit is not null, it will
228 * be enforced on any memory allocation.
229 */
230struct hlua_mem_allocator {
231 size_t allocated;
232 size_t limit;
233};
234
235static struct hlua_mem_allocator hlua_global_allocator;
236
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100237/* These functions converts types between HAProxy internal args or
238 * sample and LUA types. Another function permits to check if the
239 * LUA stack contains arguments according with an required ARG_T
240 * format.
241 */
242static int hlua_arg2lua(lua_State *L, const struct arg *arg);
243static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100244__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100245 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100246static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100247static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100248static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
249
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100250__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200251
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200252#define SEND_ERR(__be, __fmt, __args...) \
253 do { \
254 send_log(__be, LOG_ERR, __fmt, ## __args); \
255 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100256 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200257 } while (0)
258
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100259/* Used to check an Lua function type in the stack. It creates and
260 * returns a reference of the function. This function throws an
261 * error if the rgument is not a "function".
262 */
263__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
264{
265 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100266 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100267 WILL_LJMP(luaL_argerror(L, argno, msg));
268 }
269 lua_pushvalue(L, argno);
270 return luaL_ref(L, LUA_REGISTRYINDEX);
271}
272
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200273/* Return the string that is of the top of the stack. */
274const char *hlua_get_top_error_string(lua_State *L)
275{
276 if (lua_gettop(L) < 1)
277 return "unknown error";
278 if (lua_type(L, -1) != LUA_TSTRING)
279 return "unknown error";
280 return lua_tostring(L, -1);
281}
282
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200283__LJMP static const char *hlua_traceback(lua_State *L)
284{
285 lua_Debug ar;
286 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200287 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200288 int filled = 0;
289
290 while (lua_getstack(L, level++, &ar)) {
291
292 /* Add separator */
293 if (filled)
294 chunk_appendf(msg, ", ");
295 filled = 1;
296
297 /* Fill fields:
298 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
299 * 'l': fills in the field currentline;
300 * 'n': fills in the field name and namewhat;
301 * 't': fills in the field istailcall;
302 */
303 lua_getinfo(L, "Slnt", &ar);
304
305 /* Append code localisation */
306 if (ar.currentline > 0)
307 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
308 else
309 chunk_appendf(msg, "%s ", ar.short_src);
310
311 /*
312 * Get function name
313 *
314 * if namewhat is no empty, name is defined.
315 * what contains "Lua" for Lua function, "C" for C function,
316 * or "main" for main code.
317 */
318 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
319 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
320
321 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
322 chunk_appendf(msg, "main chunk");
323
324 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
325 chunk_appendf(msg, "C function line %d", ar.linedefined);
326
327 else /* nothing left... */
328 chunk_appendf(msg, "?");
329
330
331 /* Display tailed call */
332 if (ar.istailcall)
333 chunk_appendf(msg, " ...");
334 }
335
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200336 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200337}
338
339
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100340/* This function check the number of arguments available in the
341 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500342 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100343 */
344__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
345{
346 if (lua_gettop(L) == nb)
347 return;
348 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
349}
350
Mark Lakes22154b42018-01-29 14:38:40 -0800351/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100352 * and the line number where the error is encountered.
353 */
354static int hlua_pusherror(lua_State *L, const char *fmt, ...)
355{
356 va_list argp;
357 va_start(argp, fmt);
358 luaL_where(L, 1);
359 lua_pushvfstring(L, fmt, argp);
360 va_end(argp);
361 lua_concat(L, 2);
362 return 1;
363}
364
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100365/* This functions is used with sample fetch and converters. It
366 * converts the HAProxy configuration argument in a lua stack
367 * values.
368 *
369 * It takes an array of "arg", and each entry of the array is
370 * converted and pushed in the LUA stack.
371 */
372static int hlua_arg2lua(lua_State *L, const struct arg *arg)
373{
374 switch (arg->type) {
375 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100376 case ARGT_TIME:
377 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100378 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100379 break;
380
381 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200382 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100383 break;
384
385 case ARGT_IPV4:
386 case ARGT_IPV6:
387 case ARGT_MSK4:
388 case ARGT_MSK6:
389 case ARGT_FE:
390 case ARGT_BE:
391 case ARGT_TAB:
392 case ARGT_SRV:
393 case ARGT_USR:
394 case ARGT_MAP:
395 default:
396 lua_pushnil(L);
397 break;
398 }
399 return 1;
400}
401
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500402/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100403 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500404 * with sample fetch wrappers. The input arguments are given to the
405 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100406 */
407static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
408{
409 switch (lua_type(L, ud)) {
410
411 case LUA_TNUMBER:
412 case LUA_TBOOLEAN:
413 arg->type = ARGT_SINT;
414 arg->data.sint = lua_tointeger(L, ud);
415 break;
416
417 case LUA_TSTRING:
418 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200419 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200420 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200421 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200422 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100423 break;
424
425 case LUA_TUSERDATA:
426 case LUA_TNIL:
427 case LUA_TTABLE:
428 case LUA_TFUNCTION:
429 case LUA_TTHREAD:
430 case LUA_TLIGHTUSERDATA:
431 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200432 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100433 break;
434 }
435 return 1;
436}
437
438/* the following functions are used to convert a struct sample
439 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500440 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100441 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100442static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100443{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200444 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100445 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200447 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100448 break;
449
450 case SMP_T_BIN:
451 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200452 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100453 break;
454
455 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200456 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
458 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
459 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
460 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
461 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
462 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
463 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
464 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
465 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200466 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100467 break;
468 default:
469 lua_pushnil(L);
470 break;
471 }
472 break;
473
474 case SMP_T_IPV4:
475 case SMP_T_IPV6:
476 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200477 if (sample_casts[smp->data.type][SMP_T_STR] &&
478 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100480 else
481 lua_pushnil(L);
482 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100483 default:
484 lua_pushnil(L);
485 break;
486 }
487 return 1;
488}
489
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100490/* the following functions are used to convert a struct sample
491 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500492 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100493 */
494static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
495{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200496 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100497
498 case SMP_T_BIN:
499 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100501 break;
502
503 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200504 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
506 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
507 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
508 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
509 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
510 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
511 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
512 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
513 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100515 break;
516 default:
517 lua_pushstring(L, "");
518 break;
519 }
520 break;
521
522 case SMP_T_SINT:
523 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100524 case SMP_T_IPV4:
525 case SMP_T_IPV6:
526 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200527 if (sample_casts[smp->data.type][SMP_T_STR] &&
528 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200529 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100530 else
531 lua_pushstring(L, "");
532 break;
533 default:
534 lua_pushstring(L, "");
535 break;
536 }
537 return 1;
538}
539
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100540/* the following functions are used to convert an Lua type in a
541 * struct sample. This is useful to provide data from a converter
542 * to the LUA code.
543 */
544static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
545{
546 switch (lua_type(L, ud)) {
547
548 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200549 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200550 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100551 break;
552
553
554 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200555 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100557 break;
558
559 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200560 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200562 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200563 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200564 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200565 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100566 break;
567
568 case LUA_TUSERDATA:
569 case LUA_TNIL:
570 case LUA_TTABLE:
571 case LUA_TFUNCTION:
572 case LUA_TTHREAD:
573 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200574 case LUA_TNONE:
575 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200576 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200577 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100578 break;
579 }
580 return 1;
581}
582
Ilya Shipitsind4259502020-04-08 01:07:56 +0500583/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800584 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100585 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100586 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500587 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100588 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100589 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100590__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100591 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100592{
593 int min_arg;
594 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100595 struct proxy *px;
596 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100597
598 idx = 0;
599 min_arg = ARGM(mask);
600 mask >>= ARGM_BITS;
601
602 while (1) {
603
604 /* Check oversize. */
605 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100606 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100607 }
608
609 /* Check for mandatory arguments. */
610 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100611 if (idx < min_arg) {
612
613 /* If miss other argument than the first one, we return an error. */
614 if (idx > 0)
615 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
616
617 /* If first argument have a certain type, some default values
618 * may be used. See the function smp_resolve_args().
619 */
620 switch (mask & ARGT_MASK) {
621
622 case ARGT_FE:
623 if (!(p->cap & PR_CAP_FE))
624 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
625 argp[idx].data.prx = p;
626 argp[idx].type = ARGT_FE;
627 argp[idx+1].type = ARGT_STOP;
628 break;
629
630 case ARGT_BE:
631 if (!(p->cap & PR_CAP_BE))
632 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
633 argp[idx].data.prx = p;
634 argp[idx].type = ARGT_BE;
635 argp[idx+1].type = ARGT_STOP;
636 break;
637
638 case ARGT_TAB:
639 argp[idx].data.prx = p;
640 argp[idx].type = ARGT_TAB;
641 argp[idx+1].type = ARGT_STOP;
642 break;
643
644 default:
645 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
646 break;
647 }
648 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100649 return 0;
650 }
651
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500652 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100653 if ((mask & ARGT_MASK) == ARGT_STOP &&
654 argp[idx].type != ARGT_STOP) {
655 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
656 }
657
658 if ((mask & ARGT_MASK) == ARGT_STOP &&
659 argp[idx].type == ARGT_STOP) {
660 return 0;
661 }
662
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100663 /* Convert some argument types. */
664 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100665 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100666 if (argp[idx].type != ARGT_SINT)
667 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
668 argp[idx].type = ARGT_SINT;
669 break;
670
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100671 case ARGT_TIME:
672 if (argp[idx].type != ARGT_SINT)
673 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200674 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100675 break;
676
677 case ARGT_SIZE:
678 if (argp[idx].type != ARGT_SINT)
679 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200680 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100681 break;
682
683 case ARGT_FE:
684 if (argp[idx].type != ARGT_STR)
685 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200686 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100687 if (!argp[idx].data.prx)
688 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
689 argp[idx].type = ARGT_FE;
690 break;
691
692 case ARGT_BE:
693 if (argp[idx].type != ARGT_STR)
694 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200695 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100696 if (!argp[idx].data.prx)
697 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
698 argp[idx].type = ARGT_BE;
699 break;
700
701 case ARGT_TAB:
702 if (argp[idx].type != ARGT_STR)
703 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200704 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Tim Duesterhus9fe7c632019-09-29 23:03:09 +0200705 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100706 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
707 argp[idx].type = ARGT_TAB;
708 break;
709
710 case ARGT_SRV:
711 if (argp[idx].type != ARGT_STR)
712 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200713 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100714 if (sname) {
715 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200716 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200717 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100718 if (!px)
719 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
720 }
721 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200722 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100723 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100724 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100725 argp[idx].data.srv = findserver(px, sname);
726 if (!argp[idx].data.srv)
727 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
728 argp[idx].type = ARGT_SRV;
729 break;
730
731 case ARGT_IPV4:
Christopher Faulet8e09ac82020-08-07 09:07:26 +0200732 if (argp[idx].type != ARGT_STR)
733 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200734 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100735 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
736 argp[idx].type = ARGT_IPV4;
737 break;
738
739 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200740 if (argp[idx].type == ARGT_SINT)
741 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
742 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200743 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4))
Christopher Faulete663a6e2020-08-07 09:11:22 +0200744 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
745 }
746 else
747 WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected"));
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100748 argp[idx].type = ARGT_MSK4;
749 break;
750
751 case ARGT_IPV6:
Christopher Faulet8e09ac82020-08-07 09:07:26 +0200752 if (argp[idx].type != ARGT_STR)
753 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200754 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100755 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
756 argp[idx].type = ARGT_IPV6;
757 break;
758
759 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200760 if (argp[idx].type == ARGT_SINT)
761 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
762 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200763 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6))
Christopher Faulete663a6e2020-08-07 09:11:22 +0200764 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
765 }
766 else
767 WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected"));
Tim Duesterhusb814da62018-01-25 16:24:50 +0100768 argp[idx].type = ARGT_MSK6;
769 break;
770
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100771 case ARGT_MAP:
772 case ARGT_REG:
773 case ARGT_USR:
774 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100775 break;
776 }
777
778 /* Check for type of argument. */
779 if ((mask & ARGT_MASK) != argp[idx].type) {
780 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
781 arg_type_names[(mask & ARGT_MASK)],
782 arg_type_names[argp[idx].type & ARGT_MASK]);
783 WILL_LJMP(luaL_argerror(L, first + idx, msg));
784 }
785
786 /* Next argument. */
787 mask >>= ARGT_BITS;
788 idx++;
789 }
790}
791
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100792/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500793 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100794 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100795 *
796 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100797 * - hlua_sethlua : create the association between hlua context and lua_state.
798 */
799static inline struct hlua *hlua_gethlua(lua_State *L)
800{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100801 struct hlua **hlua = lua_getextraspace(L);
802 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100803}
804static inline void hlua_sethlua(struct hlua *hlua)
805{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100806 struct hlua **hlua_store = lua_getextraspace(hlua->T);
807 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100808}
809
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100810/* This function is used to send logs. It try to send on screen (stderr)
811 * and on the default syslog server.
812 */
813static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
814{
815 struct tm tm;
816 char *p;
817
818 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200819 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100820 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200821 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200822 /* Break the message if exceed the buffer size. */
823 *(p-4) = ' ';
824 *(p-3) = '.';
825 *(p-2) = '.';
826 *(p-1) = '.';
827 break;
828 }
Willy Tarreau90807112020-02-25 08:16:33 +0100829 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100830 *p = *msg;
831 else
832 *p = '.';
833 }
834 *p = '\0';
835
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200836 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100837 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200838 get_localtime(date.tv_sec, &tm);
839 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100840 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200841 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100842 fflush(stderr);
843 }
844}
845
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100846/* This function just ensure that the yield will be always
847 * returned with a timeout and permit to set some flags
848 */
849__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100850 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100851{
852 struct hlua *hlua = hlua_gethlua(L);
853
854 /* Set the wake timeout. If timeout is required, we set
855 * the expiration time.
856 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200857 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100858
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100859 hlua->flags |= flags;
860
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100861 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200862 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100863}
864
Willy Tarreau87b09662015-04-03 00:22:06 +0200865/* This function initialises the Lua environment stored in the stream.
866 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100867 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200868 *
869 * This function is particular. it initialises a new Lua thread. If the
870 * initialisation fails (example: out of memory error), the lua function
871 * throws an error (longjmp).
872 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100873 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500874 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100875 * threads appear, the safe environment set a lock to ensure only one
876 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500877 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100878 *
879 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500880 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100881 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800882 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200883 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800884 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500885 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100886 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100887int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100888{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100889 if (!already_safe) {
890 if (!SET_SAFE_LJMP(gL.T)) {
891 lua->Tref = LUA_REFNIL;
892 return 0;
893 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200894 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100895 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100896 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +0100897 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +0100898 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100899 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100900 lua->T = lua_newthread(gL.T);
901 if (!lua->T) {
902 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100903 if (!already_safe)
904 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100905 return 0;
906 }
907 hlua_sethlua(lua);
908 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
909 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100910 if (!already_safe)
911 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100912 return 1;
913}
914
Willy Tarreau87b09662015-04-03 00:22:06 +0200915/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100916 * is destroyed. The destroy also the memory context. The struct "lua"
917 * is not freed.
918 */
919void hlua_ctx_destroy(struct hlua *lua)
920{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100921 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100922 return;
923
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100924 if (!lua->T)
925 goto end;
926
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100927 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200928 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100929
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200930 if (!SET_SAFE_LJMP(lua->T))
931 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100932 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200933 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200934
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200935 if (!SET_SAFE_LJMP(gL.T))
936 return;
937 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
938 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200939 /* Forces a garbage collecting process. If the Lua program is finished
940 * without error, we run the GC on the thread pointer. Its freed all
941 * the unused memory.
942 * If the thread is finnish with an error or is currently yielded,
943 * it seems that the GC applied on the thread doesn't clean anything,
944 * so e run the GC on the main thread.
945 * NOTE: maybe this action locks all the Lua threads untiml the en of
946 * the garbage collection.
947 */
Willy Tarreauf31af932020-01-14 09:59:38 +0100948 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200949 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200950 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200951 lua_gc(gL.T, LUA_GCCOLLECT, 0);
952 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200953 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200954
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200955 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100956
957end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100958 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100959}
960
961/* This function is used to restore the Lua context when a coroutine
962 * fails. This function copy the common memory between old coroutine
963 * and the new coroutine. The old coroutine is destroyed, and its
964 * replaced by the new coroutine.
965 * If the flag "keep_msg" is set, the last entry of the old is assumed
966 * as string error message and it is copied in the new stack.
967 */
968static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
969{
970 lua_State *T;
971 int new_ref;
972
973 /* Renew the main LUA stack doesn't have sense. */
974 if (lua == &gL)
975 return 0;
976
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100977 /* New Lua coroutine. */
978 T = lua_newthread(gL.T);
979 if (!T)
980 return 0;
981
982 /* Copy last error message. */
983 if (keep_msg)
984 lua_xmove(lua->T, T, 1);
985
986 /* Copy data between the coroutines. */
987 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
988 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500989 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100990
991 /* Destroy old data. */
992 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
993
994 /* The thread is garbage collected by Lua. */
995 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
996
997 /* Fill the struct with the new coroutine values. */
998 lua->Mref = new_ref;
999 lua->T = T;
1000 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1001
1002 /* Set context. */
1003 hlua_sethlua(lua);
1004
1005 return 1;
1006}
1007
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001008void hlua_hook(lua_State *L, lua_Debug *ar)
1009{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001010 struct hlua *hlua = hlua_gethlua(L);
1011
1012 /* Lua cannot yield when its returning from a function,
1013 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001014 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001015 */
1016 if (lua_gethookmask(L) & LUA_MASKRET) {
1017 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1018 return;
1019 }
1020
1021 /* restore the interrupt condition. */
1022 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1023
1024 /* If we interrupt the Lua processing in yieldable state, we yield.
1025 * If the state is not yieldable, trying yield causes an error.
1026 */
1027 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001028 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001029
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001030 /* If we cannot yield, update the clock and check the timeout. */
1031 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001032 hlua->run_time += now_ms - hlua->start_time;
1033 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001034 lua_pushfstring(L, "execution timeout");
1035 WILL_LJMP(lua_error(L));
1036 }
1037
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001038 /* Update the start time. */
1039 hlua->start_time = now_ms;
1040
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001041 /* Try to interrupt the process at the end of the current
1042 * unyieldable function.
1043 */
1044 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001045}
1046
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001047/* This function start or resumes the Lua stack execution. If the flag
1048 * "yield_allowed" if no set and the LUA stack execution returns a yield
1049 * The function return an error.
1050 *
1051 * The function can returns 4 values:
1052 * - HLUA_E_OK : The execution is terminated without any errors.
1053 * - HLUA_E_AGAIN : The execution must continue at the next associated
1054 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001055 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001056 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001057 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001058 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001059 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001060 * LUA code.
1061 */
1062static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1063{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001064#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1065 int nres;
1066#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001067 int ret;
1068 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001069 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001070
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001071 /* Initialise run time counter. */
1072 if (!HLUA_IS_RUNNING(lua))
1073 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001074
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001075 /* Lock the whole Lua execution. This lock must be before the
1076 * label "resume_execution".
1077 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001078 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001079
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001080resume_execution:
1081
1082 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1083 * instructions. it is used for preventing infinite loops.
1084 */
1085 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1086
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001087 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001088 HLUA_SET_RUN(lua);
1089 HLUA_CLR_CTRLYIELD(lua);
1090 HLUA_CLR_WAKERESWR(lua);
1091 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001092
Christopher Fauletbc275a92020-02-26 14:55:16 +01001093 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001094 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001095 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001096
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001097 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001098#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1099 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1100#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001101 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001102#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001103 switch (ret) {
1104
1105 case LUA_OK:
1106 ret = HLUA_E_OK;
1107 break;
1108
1109 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001110 /* Check if the execution timeout is expired. It it is the case, we
1111 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001112 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001113 tv_update_date(0, 1);
1114 lua->run_time += now_ms - lua->start_time;
1115 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001116 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001117 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001118 break;
1119 }
1120 /* Process the forced yield. if the general yield is not allowed or
1121 * if no task were associated this the current Lua execution
1122 * coroutine, we resume the execution. Else we want to return in the
1123 * scheduler and we want to be waked up again, to continue the
1124 * current Lua execution. So we schedule our own task.
1125 */
1126 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001127 if (!yield_allowed || !lua->task)
1128 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001129 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001130 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001131 if (!yield_allowed) {
1132 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001133 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001134 break;
1135 }
1136 ret = HLUA_E_AGAIN;
1137 break;
1138
1139 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001140
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001141 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001142 * because the errors ares the only one mean to return immediately
1143 * from and lua execution.
1144 */
1145 if (lua->flags & HLUA_EXIT) {
1146 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001147 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001148 break;
1149 }
1150
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001151 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001152 if (!lua_checkstack(lua->T, 1)) {
1153 ret = HLUA_E_ERR;
1154 break;
1155 }
1156 msg = lua_tostring(lua->T, -1);
1157 lua_settop(lua->T, 0); /* Empty the stack. */
1158 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001159 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001161 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001162 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001163 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001164 ret = HLUA_E_ERRMSG;
1165 break;
1166
1167 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001168 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001169 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001170 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001171 break;
1172
1173 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001174 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001175 if (!lua_checkstack(lua->T, 1)) {
1176 ret = HLUA_E_ERR;
1177 break;
1178 }
1179 msg = lua_tostring(lua->T, -1);
1180 lua_settop(lua->T, 0); /* Empty the stack. */
1181 lua_pop(lua->T, 1);
1182 if (msg)
1183 lua_pushfstring(lua->T, "message handler error: %s", msg);
1184 else
1185 lua_pushfstring(lua->T, "message handler error");
1186 ret = HLUA_E_ERRMSG;
1187 break;
1188
1189 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001190 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001191 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001192 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001193 break;
1194 }
1195
1196 switch (ret) {
1197 case HLUA_E_AGAIN:
1198 break;
1199
1200 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001201 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001202 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001203 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001204 break;
1205
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001206 case HLUA_E_ETMOUT:
1207 case HLUA_E_NOMEM:
1208 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001209 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001210 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001211 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001212 hlua_ctx_renew(lua, 0);
1213 break;
1214
1215 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001216 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001217 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001218 break;
1219 }
1220
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001221 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001222 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001223
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001224 return ret;
1225}
1226
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001227/* This function exit the current code. */
1228__LJMP static int hlua_done(lua_State *L)
1229{
1230 struct hlua *hlua = hlua_gethlua(L);
1231
1232 hlua->flags |= HLUA_EXIT;
1233 WILL_LJMP(lua_error(L));
1234
1235 return 0;
1236}
1237
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001238/* This function is an LUA binding. It provides a function
1239 * for deleting ACL from a referenced ACL file.
1240 */
1241__LJMP static int hlua_del_acl(lua_State *L)
1242{
1243 const char *name;
1244 const char *key;
1245 struct pat_ref *ref;
1246
1247 MAY_LJMP(check_args(L, 2, "del_acl"));
1248
1249 name = MAY_LJMP(luaL_checkstring(L, 1));
1250 key = MAY_LJMP(luaL_checkstring(L, 2));
1251
1252 ref = pat_ref_lookup(name);
1253 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001254 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001255
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001256 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001257 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001258 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001259 return 0;
1260}
1261
1262/* This function is an LUA binding. It provides a function
1263 * for deleting map entry from a referenced map file.
1264 */
1265static int hlua_del_map(lua_State *L)
1266{
1267 const char *name;
1268 const char *key;
1269 struct pat_ref *ref;
1270
1271 MAY_LJMP(check_args(L, 2, "del_map"));
1272
1273 name = MAY_LJMP(luaL_checkstring(L, 1));
1274 key = MAY_LJMP(luaL_checkstring(L, 2));
1275
1276 ref = pat_ref_lookup(name);
1277 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001278 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001279
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001280 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001281 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001282 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001283 return 0;
1284}
1285
1286/* This function is an LUA binding. It provides a function
1287 * for adding ACL pattern from a referenced ACL file.
1288 */
1289static int hlua_add_acl(lua_State *L)
1290{
1291 const char *name;
1292 const char *key;
1293 struct pat_ref *ref;
1294
1295 MAY_LJMP(check_args(L, 2, "add_acl"));
1296
1297 name = MAY_LJMP(luaL_checkstring(L, 1));
1298 key = MAY_LJMP(luaL_checkstring(L, 2));
1299
1300 ref = pat_ref_lookup(name);
1301 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001302 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001303
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001304 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001305 if (pat_ref_find_elt(ref, key) == NULL)
1306 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001307 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001308 return 0;
1309}
1310
1311/* This function is an LUA binding. It provides a function
1312 * for setting map pattern and sample from a referenced map
1313 * file.
1314 */
1315static int hlua_set_map(lua_State *L)
1316{
1317 const char *name;
1318 const char *key;
1319 const char *value;
1320 struct pat_ref *ref;
1321
1322 MAY_LJMP(check_args(L, 3, "set_map"));
1323
1324 name = MAY_LJMP(luaL_checkstring(L, 1));
1325 key = MAY_LJMP(luaL_checkstring(L, 2));
1326 value = MAY_LJMP(luaL_checkstring(L, 3));
1327
1328 ref = pat_ref_lookup(name);
1329 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001330 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001331
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001332 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001333 if (pat_ref_find_elt(ref, key) != NULL)
1334 pat_ref_set(ref, key, value, NULL);
1335 else
1336 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001337 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001338 return 0;
1339}
1340
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001341/* A class is a lot of memory that contain data. This data can be a table,
1342 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001343 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001344 * the name of the object (_G[<name>] = <metable> ).
1345 *
1346 * A metable is a table that modify the standard behavior of a standard
1347 * access to the associated data. The entries of this new metatable are
1348 * defined as is:
1349 *
1350 * http://lua-users.org/wiki/MetatableEvents
1351 *
1352 * __index
1353 *
1354 * we access an absent field in a table, the result is nil. This is
1355 * true, but it is not the whole truth. Actually, such access triggers
1356 * the interpreter to look for an __index metamethod: If there is no
1357 * such method, as usually happens, then the access results in nil;
1358 * otherwise, the metamethod will provide the result.
1359 *
1360 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1361 * the key does not appear in the table, but the metatable has an __index
1362 * property:
1363 *
1364 * - if the value is a function, the function is called, passing in the
1365 * table and the key; the return value of that function is returned as
1366 * the result.
1367 *
1368 * - if the value is another table, the value of the key in that table is
1369 * asked for and returned (and if it doesn't exist in that table, but that
1370 * table's metatable has an __index property, then it continues on up)
1371 *
1372 * - Use "rawget(myTable,key)" to skip this metamethod.
1373 *
1374 * http://www.lua.org/pil/13.4.1.html
1375 *
1376 * __newindex
1377 *
1378 * Like __index, but control property assignment.
1379 *
1380 * __mode - Control weak references. A string value with one or both
1381 * of the characters 'k' and 'v' which specifies that the the
1382 * keys and/or values in the table are weak references.
1383 *
1384 * __call - Treat a table like a function. When a table is followed by
1385 * parenthesis such as "myTable( 'foo' )" and the metatable has
1386 * a __call key pointing to a function, that function is invoked
1387 * (passing any specified arguments) and the return value is
1388 * returned.
1389 *
1390 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1391 * called, if the metatable for myTable has a __metatable
1392 * key, the value of that key is returned instead of the
1393 * actual metatable.
1394 *
1395 * __tostring - Control string representation. When the builtin
1396 * "tostring( myTable )" function is called, if the metatable
1397 * for myTable has a __tostring property set to a function,
1398 * that function is invoked (passing myTable to it) and the
1399 * return value is used as the string representation.
1400 *
1401 * __len - Control table length. When the table length is requested using
1402 * the length operator ( '#' ), if the metatable for myTable has
1403 * a __len key pointing to a function, that function is invoked
1404 * (passing myTable to it) and the return value used as the value
1405 * of "#myTable".
1406 *
1407 * __gc - Userdata finalizer code. When userdata is set to be garbage
1408 * collected, if the metatable has a __gc field pointing to a
1409 * function, that function is first invoked, passing the userdata
1410 * to it. The __gc metamethod is not called for tables.
1411 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1412 *
1413 * Special metamethods for redefining standard operators:
1414 * http://www.lua.org/pil/13.1.html
1415 *
1416 * __add "+"
1417 * __sub "-"
1418 * __mul "*"
1419 * __div "/"
1420 * __unm "!"
1421 * __pow "^"
1422 * __concat ".."
1423 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001424 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001425 * http://www.lua.org/pil/13.2.html
1426 *
1427 * __eq "=="
1428 * __lt "<"
1429 * __le "<="
1430 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001431
1432/*
1433 *
1434 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001435 * Class Map
1436 *
1437 *
1438 */
1439
1440/* Returns a struct hlua_map if the stack entry "ud" is
1441 * a class session, otherwise it throws an error.
1442 */
1443__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1444{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001445 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001446}
1447
1448/* This function is the map constructor. It don't need
1449 * the class Map object. It creates and return a new Map
1450 * object. It must be called only during "body" or "init"
1451 * context because it process some filesystem accesses.
1452 */
1453__LJMP static int hlua_map_new(struct lua_State *L)
1454{
1455 const char *fn;
1456 int match = PAT_MATCH_STR;
1457 struct sample_conv conv;
1458 const char *file = "";
1459 int line = 0;
1460 lua_Debug ar;
1461 char *err = NULL;
1462 struct arg args[2];
1463
1464 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1465 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1466
1467 fn = MAY_LJMP(luaL_checkstring(L, 1));
1468
1469 if (lua_gettop(L) >= 2) {
1470 match = MAY_LJMP(luaL_checkinteger(L, 2));
1471 if (match < 0 || match >= PAT_MATCH_NUM)
1472 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1473 }
1474
1475 /* Get Lua filename and line number. */
1476 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1477 lua_getinfo(L, "Sl", &ar); /* get info about it */
1478 if (ar.currentline > 0) { /* is there info? */
1479 file = ar.short_src;
1480 line = ar.currentline;
1481 }
1482 }
1483
1484 /* fill fake sample_conv struct. */
1485 conv.kw = ""; /* unused. */
1486 conv.process = NULL; /* unused. */
1487 conv.arg_mask = 0; /* unused. */
1488 conv.val_args = NULL; /* unused. */
1489 conv.out_type = SMP_T_STR;
1490 conv.private = (void *)(long)match;
1491 switch (match) {
1492 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1493 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1494 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1495 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1496 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1497 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1498 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001499 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001500 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1501 default:
1502 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1503 }
1504
1505 /* fill fake args. */
1506 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001507 args[0].data.str.area = strdup(fn);
1508 args[0].data.str.data = strlen(fn);
1509 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001510 args[1].type = ARGT_STOP;
1511
1512 /* load the map. */
1513 if (!sample_load_map(args, &conv, file, line, &err)) {
1514 /* error case: we cant use luaL_error because we must
1515 * free the err variable.
1516 */
1517 luaL_where(L, 1);
1518 lua_pushfstring(L, "'new': %s.", err);
1519 lua_concat(L, 2);
1520 free(err);
Christopher Faulet73292e92020-08-06 08:40:09 +02001521 free(args[0].data.str.area);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001522 WILL_LJMP(lua_error(L));
1523 }
1524
1525 /* create the lua object. */
1526 lua_newtable(L);
1527 lua_pushlightuserdata(L, args[0].data.map);
1528 lua_rawseti(L, -2, 0);
1529
1530 /* Pop a class Map metatable and affect it to the userdata. */
1531 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1532 lua_setmetatable(L, -2);
1533
1534
1535 return 1;
1536}
1537
1538__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1539{
1540 struct map_descriptor *desc;
1541 struct pattern *pat;
1542 struct sample smp;
1543
1544 MAY_LJMP(check_args(L, 2, "lookup"));
1545 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001546 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001547 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001548 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001549 }
1550 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001551 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001552 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001553 smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001554 }
1555
1556 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001557 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001558 if (str)
1559 lua_pushstring(L, "");
1560 else
1561 lua_pushnil(L);
1562 return 1;
1563 }
1564
1565 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001566 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001567 return 1;
1568}
1569
1570__LJMP static int hlua_map_lookup(struct lua_State *L)
1571{
1572 return _hlua_map_lookup(L, 0);
1573}
1574
1575__LJMP static int hlua_map_slookup(struct lua_State *L)
1576{
1577 return _hlua_map_lookup(L, 1);
1578}
1579
1580/*
1581 *
1582 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001583 * Class Socket
1584 *
1585 *
1586 */
1587
1588__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1589{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001590 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001591}
1592
1593/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001594 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001595 * received.
1596 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001597static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001598{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001599 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001600
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001601 if (appctx->ctx.hlua_cosocket.die) {
1602 si_shutw(si);
1603 si_shutr(si);
1604 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001605 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1606 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001607 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1608 }
1609
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001610 /* If we cant write, wakeup the pending write signals. */
1611 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001612 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001613
1614 /* If we cant read, wakeup the pending read signals. */
1615 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001616 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001617
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001618 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001619 * to be notified whenever the connection completes.
1620 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001621 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001622 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001623 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001624 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001625 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001626 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001627
1628 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001629 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001630
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001631 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001632 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001633 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001634
1635 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001636 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001637 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001638
1639 /* Some data were injected in the buffer, notify the stream
1640 * interface.
1641 */
1642 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001643 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001644
1645 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001646 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001647 */
1648 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001649 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001650}
1651
Willy Tarreau87b09662015-04-03 00:22:06 +02001652/* This function is called when the "struct stream" is destroyed.
1653 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001654 * Wake all the pending signals.
1655 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001656static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001657{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001658 struct xref *peer;
1659
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001660 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001661 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1662 if (peer)
1663 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001664
1665 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001666 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1667 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001668}
1669
1670/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001671 * uses this object. If the stream does not exists, just quit.
1672 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001673 * pending signal can rest in the read and write lists. destroy
1674 * it.
1675 */
1676__LJMP static int hlua_socket_gc(lua_State *L)
1677{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001678 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001679 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001680 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001681
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001682 MAY_LJMP(check_args(L, 1, "__gc"));
1683
1684 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001685 peer = xref_get_peer_and_lock(&socket->xref);
1686 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001687 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001688 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001689
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001690 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001691 appctx->ctx.hlua_cosocket.die = 1;
1692 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001693
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001694 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001695 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001696 return 0;
1697}
1698
1699/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001700 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701 */
sada05ed3302018-05-11 11:48:18 -07001702__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001704 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001706 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001707 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001708
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001709 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001710
1711 /* Check if we run on the same thread than the xreator thread.
1712 * We cannot access to the socket if the thread is different.
1713 */
1714 if (socket->tid != tid)
1715 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1716
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001717 peer = xref_get_peer_and_lock(&socket->xref);
1718 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001719 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001720
1721 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001722 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001723
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001724 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001725 appctx->ctx.hlua_cosocket.die = 1;
1726 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001727
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001728 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001729 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001730 return 0;
1731}
1732
sada05ed3302018-05-11 11:48:18 -07001733/* The close function calls close_helper.
1734 */
1735__LJMP static int hlua_socket_close(lua_State *L)
1736{
1737 MAY_LJMP(check_args(L, 1, "close"));
1738 return hlua_socket_close_helper(L);
1739}
1740
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001741/* This Lua function assumes that the stack contain three parameters.
1742 * 1 - USERDATA containing a struct socket
1743 * 2 - INTEGER with values of the macro defined below
1744 * If the integer is -1, we must read at most one line.
1745 * If the integer is -2, we ust read all the data until the
1746 * end of the stream.
1747 * If the integer is positive value, we must read a number of
1748 * bytes corresponding to this value.
1749 */
1750#define HLSR_READ_LINE (-1)
1751#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001752__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001753{
1754 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1755 int wanted = lua_tointeger(L, 2);
1756 struct hlua *hlua = hlua_gethlua(L);
1757 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001758 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001759 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001760 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001761 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001762 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001763 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001764 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001765 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001766 struct stream_interface *si;
1767 struct stream *s;
1768 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001769 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001770
1771 /* Check if this lua stack is schedulable. */
1772 if (!hlua || !hlua->task)
1773 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1774 "'frontend', 'backend' or 'task'"));
1775
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001776 /* Check if we run on the same thread than the xreator thread.
1777 * We cannot access to the socket if the thread is different.
1778 */
1779 if (socket->tid != tid)
1780 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1781
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001782 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001783 peer = xref_get_peer_and_lock(&socket->xref);
1784 if (!peer)
1785 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001786 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1787 si = appctx->owner;
1788 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001790 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001791 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001792 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001793 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001794 if (nblk < 0) /* Connection close. */
1795 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001796 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001797 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001798
1799 /* remove final \r\n. */
1800 if (nblk == 1) {
1801 if (blk1[len1-1] == '\n') {
1802 len1--;
1803 skip_at_end++;
1804 if (blk1[len1-1] == '\r') {
1805 len1--;
1806 skip_at_end++;
1807 }
1808 }
1809 }
1810 else {
1811 if (blk2[len2-1] == '\n') {
1812 len2--;
1813 skip_at_end++;
1814 if (blk2[len2-1] == '\r') {
1815 len2--;
1816 skip_at_end++;
1817 }
1818 }
1819 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001820 }
1821
1822 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001823 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001824 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825 if (nblk < 0) /* Connection close. */
1826 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001827 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001828 goto connection_empty;
1829 }
1830
1831 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001833 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001834 if (nblk < 0) /* Connection close. */
1835 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001836 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001837 goto connection_empty;
1838
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001839 missing_bytes = wanted - socket->b.n;
1840 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001841 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001842 len1 = missing_bytes;
1843 } if (nblk == 2 && len1 + len2 > missing_bytes)
1844 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001845 }
1846
1847 len = len1;
1848
1849 luaL_addlstring(&socket->b, blk1, len1);
1850 if (nblk == 2) {
1851 len += len2;
1852 luaL_addlstring(&socket->b, blk2, len2);
1853 }
1854
1855 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001856 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001857
1858 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001859 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001860
1861 /* If the pattern reclaim to read all the data
1862 * in the connection, got out.
1863 */
1864 if (wanted == HLSR_READ_ALL)
1865 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001866 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001867 goto connection_empty;
1868
1869 /* Return result. */
1870 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001871 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001872 return 1;
1873
1874connection_closed:
1875
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001876 xref_unlock(&socket->xref, peer);
1877
1878no_peer:
1879
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001880 /* If the buffer containds data. */
1881 if (socket->b.n > 0) {
1882 luaL_pushresult(&socket->b);
1883 return 1;
1884 }
1885 lua_pushnil(L);
1886 lua_pushstring(L, "connection closed.");
1887 return 2;
1888
1889connection_empty:
1890
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001891 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1892 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001893 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001894 }
1895 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001896 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001897 return 0;
1898}
1899
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001900/* This Lua function gets two parameters. The first one can be string
1901 * or a number. If the string is "*l", the user requires one line. If
1902 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001903 * If the value is a number, the user require a number of bytes equal
1904 * to the value. The default value is "*l" (a line).
1905 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001906 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001907 * integer takes this values:
1908 * -1 : read a line
1909 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001910 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001911 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001912 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001913 * concatenated with the read data.
1914 */
1915__LJMP static int hlua_socket_receive(struct lua_State *L)
1916{
1917 int wanted = HLSR_READ_LINE;
1918 const char *pattern;
1919 int type;
1920 char *error;
1921 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001922 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001923
1924 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1925 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1926
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001927 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001928
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001929 /* Check if we run on the same thread than the xreator thread.
1930 * We cannot access to the socket if the thread is different.
1931 */
1932 if (socket->tid != tid)
1933 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1934
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001935 /* check for pattern. */
1936 if (lua_gettop(L) >= 2) {
1937 type = lua_type(L, 2);
1938 if (type == LUA_TSTRING) {
1939 pattern = lua_tostring(L, 2);
1940 if (strcmp(pattern, "*a") == 0)
1941 wanted = HLSR_READ_ALL;
1942 else if (strcmp(pattern, "*l") == 0)
1943 wanted = HLSR_READ_LINE;
1944 else {
1945 wanted = strtoll(pattern, &error, 10);
1946 if (*error != '\0')
1947 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1948 }
1949 }
1950 else if (type == LUA_TNUMBER) {
1951 wanted = lua_tointeger(L, 2);
1952 if (wanted < 0)
1953 WILL_LJMP(luaL_error(L, "Unsupported size."));
1954 }
1955 }
1956
1957 /* Set pattern. */
1958 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001959
1960 /* Check if we would replace the top by itself. */
1961 if (lua_gettop(L) != 2)
1962 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001963
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001964 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001965 luaL_buffinit(L, &socket->b);
1966
1967 /* Check prefix. */
1968 if (lua_gettop(L) >= 3) {
1969 if (lua_type(L, 3) != LUA_TSTRING)
1970 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1971 pattern = lua_tolstring(L, 3, &len);
1972 luaL_addlstring(&socket->b, pattern, len);
1973 }
1974
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001975 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001976}
1977
1978/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001979 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001980 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001981static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001982{
1983 struct hlua_socket *socket;
1984 struct hlua *hlua = hlua_gethlua(L);
1985 struct appctx *appctx;
1986 size_t buf_len;
1987 const char *buf;
1988 int len;
1989 int send_len;
1990 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001991 struct xref *peer;
1992 struct stream_interface *si;
1993 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001994
1995 /* Check if this lua stack is schedulable. */
1996 if (!hlua || !hlua->task)
1997 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1998 "'frontend', 'backend' or 'task'"));
1999
2000 /* Get object */
2001 socket = MAY_LJMP(hlua_checksocket(L, 1));
2002 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002003 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002004
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002005 /* Check if we run on the same thread than the xreator thread.
2006 * We cannot access to the socket if the thread is different.
2007 */
2008 if (socket->tid != tid)
2009 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2010
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002011 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002012 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002013 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002014 lua_pushinteger(L, -1);
2015 return 1;
2016 }
2017 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2018 si = appctx->owner;
2019 s = si_strm(si);
2020
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002021 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002022 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002023 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002024 lua_pushinteger(L, -1);
2025 return 1;
2026 }
2027
2028 /* Update the input buffer data. */
2029 buf += sent;
2030 send_len = buf_len - sent;
2031
2032 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002033 if (sent >= buf_len) {
2034 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002035 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002036 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002038 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002039 * the request buffer if its not required.
2040 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002041 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002042 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002043 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002044 }
2045
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002046 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002047 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002048 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002049 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002050 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002051
2052 /* send data */
2053 if (len < send_len)
2054 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002055 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002056
2057 /* "Not enough space" (-1), "Buffer too little to contain
2058 * the data" (-2) are not expected because the available length
2059 * is tested.
2060 * Other unknown error are also not expected.
2061 */
2062 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002063 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002064 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002065
sada05ed3302018-05-11 11:48:18 -07002066 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002067 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002068 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002069 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002070 return 1;
2071 }
2072
2073 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002074 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002075
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002076 s->req.rex = TICK_ETERNITY;
2077 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002078
2079 /* Update length sent. */
2080 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002081 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002082
2083 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002084 if (sent + len >= buf_len) {
2085 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002086 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002087 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002088
2089hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002090 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2091 xref_unlock(&socket->xref, peer);
2092 WILL_LJMP(luaL_error(L, "out of memory"));
2093 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002094 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002095 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002096 return 0;
2097}
2098
2099/* This function initiate the send of data. It just check the input
2100 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002101 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002102 * "hlua_socket_write_yield" that can yield.
2103 *
2104 * The Lua function gets between 3 and 4 parameters. The first one is
2105 * the associated object. The second is a string buffer. The third is
2106 * a facultative integer that represents where is the buffer position
2107 * of the start of the data that can send. The first byte is the
2108 * position "1". The default value is "1". The fourth argument is a
2109 * facultative integer that represents where is the buffer position
2110 * of the end of the data that can send. The default is the last byte.
2111 */
2112static int hlua_socket_send(struct lua_State *L)
2113{
2114 int i;
2115 int j;
2116 const char *buf;
2117 size_t buf_len;
2118
2119 /* Check number of arguments. */
2120 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2121 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2122
2123 /* Get the string. */
2124 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2125
2126 /* Get and check j. */
2127 if (lua_gettop(L) == 4) {
2128 j = MAY_LJMP(luaL_checkinteger(L, 4));
2129 if (j < 0)
2130 j = buf_len + j + 1;
2131 if (j > buf_len)
2132 j = buf_len + 1;
2133 lua_pop(L, 1);
2134 }
2135 else
2136 j = buf_len;
2137
2138 /* Get and check i. */
2139 if (lua_gettop(L) == 3) {
2140 i = MAY_LJMP(luaL_checkinteger(L, 3));
2141 if (i < 0)
2142 i = buf_len + i + 1;
2143 if (i > buf_len)
2144 i = buf_len + 1;
2145 lua_pop(L, 1);
2146 } else
2147 i = 1;
2148
2149 /* Check bth i and j. */
2150 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002151 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002152 return 1;
2153 }
2154 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002155 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002156 return 1;
2157 }
2158 if (i == 0)
2159 i = 1;
2160 if (j == 0)
2161 j = 1;
2162
2163 /* Pop the string. */
2164 lua_pop(L, 1);
2165
2166 /* Update the buffer length. */
2167 buf += i - 1;
2168 buf_len = j - i + 1;
2169 lua_pushlstring(L, buf, buf_len);
2170
2171 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002172 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002173
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002174 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002175}
2176
Willy Tarreau22b0a682015-06-17 19:43:49 +02002177#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002178__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2179{
2180 static char buffer[SOCKET_INFO_MAX_LEN];
2181 int ret;
2182 int len;
2183 char *p;
2184
2185 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2186 if (ret <= 0) {
2187 lua_pushnil(L);
2188 return 1;
2189 }
2190
2191 if (ret == AF_UNIX) {
2192 lua_pushstring(L, buffer+1);
2193 return 1;
2194 }
2195 else if (ret == AF_INET6) {
2196 buffer[0] = '[';
2197 len = strlen(buffer);
2198 buffer[len] = ']';
2199 len++;
2200 buffer[len] = ':';
2201 len++;
2202 p = buffer;
2203 }
2204 else if (ret == AF_INET) {
2205 p = buffer + 1;
2206 len = strlen(p);
2207 p[len] = ':';
2208 len++;
2209 }
2210 else {
2211 lua_pushnil(L);
2212 return 1;
2213 }
2214
2215 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2216 lua_pushnil(L);
2217 return 1;
2218 }
2219
2220 lua_pushstring(L, p);
2221 return 1;
2222}
2223
2224/* Returns information about the peer of the connection. */
2225__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2226{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002227 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002228 struct xref *peer;
2229 struct appctx *appctx;
2230 struct stream_interface *si;
2231 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002232 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002233
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002234 MAY_LJMP(check_args(L, 1, "getpeername"));
2235
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002236 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002237
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002238 /* Check if we run on the same thread than the xreator thread.
2239 * We cannot access to the socket if the thread is different.
2240 */
2241 if (socket->tid != tid)
2242 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2243
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002244 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002245 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002246 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002247 lua_pushnil(L);
2248 return 1;
2249 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002250 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2251 si = appctx->owner;
2252 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002253
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002254 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002255 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002256 lua_pushnil(L);
2257 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002258 }
2259
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002260 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002261 xref_unlock(&socket->xref, peer);
2262 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002263}
2264
2265/* Returns information about my connection side. */
2266static int hlua_socket_getsockname(struct lua_State *L)
2267{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002268 struct hlua_socket *socket;
2269 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002270 struct appctx *appctx;
2271 struct xref *peer;
2272 struct stream_interface *si;
2273 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002274 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002275
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002276 MAY_LJMP(check_args(L, 1, "getsockname"));
2277
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002278 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002279
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002280 /* Check if we run on the same thread than the xreator thread.
2281 * We cannot access to the socket if the thread is different.
2282 */
2283 if (socket->tid != tid)
2284 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2285
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002286 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002287 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002288 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002289 lua_pushnil(L);
2290 return 1;
2291 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002292 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2293 si = appctx->owner;
2294 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002295
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002296 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002297 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002298 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002299 lua_pushnil(L);
2300 return 1;
2301 }
2302
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002303 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002304 xref_unlock(&socket->xref, peer);
2305 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002306}
2307
2308/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002309static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002310 .obj_type = OBJ_TYPE_APPLET,
2311 .name = "<LUA_TCP>",
2312 .fct = hlua_socket_handler,
2313 .release = hlua_socket_release,
2314};
2315
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002316__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002317{
2318 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2319 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002320 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002321 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002322 struct stream_interface *si;
2323 struct stream *s;
2324
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002325 /* Check if we run on the same thread than the xreator thread.
2326 * We cannot access to the socket if the thread is different.
2327 */
2328 if (socket->tid != tid)
2329 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2330
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002331 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002332 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002333 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002334 lua_pushnil(L);
2335 lua_pushstring(L, "Can't connect");
2336 return 2;
2337 }
2338 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2339 si = appctx->owner;
2340 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002341
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002342 /* Check if we run on the same thread than the xreator thread.
2343 * We cannot access to the socket if the thread is different.
2344 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002345 if (socket->tid != tid) {
2346 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002347 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002348 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002349
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002351 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002352 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002353 lua_pushnil(L);
2354 lua_pushstring(L, "Can't connect");
2355 return 2;
2356 }
2357
Willy Tarreaue09101e2018-10-16 17:37:12 +02002358 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002359
2360 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002361 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002362 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002363 lua_pushinteger(L, 1);
2364 return 1;
2365 }
2366
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002367 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2368 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002369 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002370 }
2371 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002372 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002373 return 0;
2374}
2375
2376/* This function fail or initite the connection. */
2377__LJMP static int hlua_socket_connect(struct lua_State *L)
2378{
2379 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002380 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002381 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002382 struct hlua *hlua;
2383 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002384 int low, high;
2385 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002386 struct xref *peer;
2387 struct stream_interface *si;
2388 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002390 if (lua_gettop(L) < 2)
2391 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002392
2393 /* Get args. */
2394 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002395
2396 /* Check if we run on the same thread than the xreator thread.
2397 * We cannot access to the socket if the thread is different.
2398 */
2399 if (socket->tid != tid)
2400 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2401
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002402 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002403 if (lua_gettop(L) >= 3) {
2404 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002405 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002406
Tim Duesterhus6edab862018-01-06 19:04:45 +01002407 /* Force the ip to end with a colon, to support IPv6 addresses
2408 * that are not enclosed within square brackets.
2409 */
2410 if (port > 0) {
2411 luaL_buffinit(L, &b);
2412 luaL_addstring(&b, ip);
2413 luaL_addchar(&b, ':');
2414 luaL_pushresult(&b);
2415 ip = lua_tolstring(L, lua_gettop(L), NULL);
2416 }
2417 }
2418
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002419 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002420 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002421 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002422 lua_pushnil(L);
2423 return 1;
2424 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002425
2426 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002427 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002428 if (!addr) {
2429 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002430 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002431 }
2432 if (low != high) {
2433 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002434 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002435 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002436
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002437 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002438 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002439 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002440 if (port == -1) {
2441 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002442 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002443 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002444 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2445 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002446 if (port == -1) {
2447 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002448 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002449 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002450 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002451 }
2452 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002453
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002454 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2455 si = appctx->owner;
2456 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002457
2458 if (!sockaddr_alloc(&s->target_addr)) {
2459 xref_unlock(&socket->xref, peer);
2460 WILL_LJMP(luaL_error(L, "connect: internal error"));
2461 }
2462 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002463 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002464
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002465 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002466
2467 /* inform the stream that we want to be notified whenever the
2468 * connection completes.
2469 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002470 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002471 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002472 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002473
Willy Tarreauf31af932020-01-14 09:59:38 +01002474 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002475
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002476 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2477 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002478 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002479 }
2480 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002481
2482 task_wakeup(s->task, TASK_WOKEN_INIT);
2483 /* Return yield waiting for connection. */
2484
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
2487 return 0;
2488}
2489
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002490#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002491__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2492{
2493 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002494 struct xref *peer;
2495 struct appctx *appctx;
2496 struct stream_interface *si;
2497 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002498
2499 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2500 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002501
2502 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002503 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002504 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002505 lua_pushnil(L);
2506 return 1;
2507 }
2508 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2509 si = appctx->owner;
2510 s = si_strm(si);
2511
2512 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002513 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002514 return MAY_LJMP(hlua_socket_connect(L));
2515}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002516#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002517
2518__LJMP static int hlua_socket_setoption(struct lua_State *L)
2519{
2520 return 0;
2521}
2522
2523__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2524{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002525 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002526 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002527 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002528 struct xref *peer;
2529 struct appctx *appctx;
2530 struct stream_interface *si;
2531 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002532
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002533 MAY_LJMP(check_args(L, 2, "settimeout"));
2534
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002535 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002536
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002537 /* convert the timeout to millis */
2538 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002539
Thierry Fournier17a921b2018-03-08 09:59:02 +01002540 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002541 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002542 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2543
Mark Lakes56cc1252018-03-27 09:48:06 +02002544 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002545 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002546
2547 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002548 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002549 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002550
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002551 /* Check if we run on the same thread than the xreator thread.
2552 * We cannot access to the socket if the thread is different.
2553 */
2554 if (socket->tid != tid)
2555 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2556
Mark Lakes56cc1252018-03-27 09:48:06 +02002557 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002558 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002559 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002560 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2561 WILL_LJMP(lua_error(L));
2562 return 0;
2563 }
2564 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2565 si = appctx->owner;
2566 s = si_strm(si);
2567
Cyril Bonté7bb63452018-08-17 23:51:02 +02002568 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002569 s->req.rto = tmout;
2570 s->req.wto = tmout;
2571 s->res.rto = tmout;
2572 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002573 s->req.rex = tick_add_ifset(now_ms, tmout);
2574 s->req.wex = tick_add_ifset(now_ms, tmout);
2575 s->res.rex = tick_add_ifset(now_ms, tmout);
2576 s->res.wex = tick_add_ifset(now_ms, tmout);
2577
2578 s->task->expire = tick_add_ifset(now_ms, tmout);
2579 task_queue(s->task);
2580
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002581 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002582
Thierry Fourniere9636f12018-03-08 09:54:32 +01002583 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002584 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002585}
2586
2587__LJMP static int hlua_socket_new(lua_State *L)
2588{
2589 struct hlua_socket *socket;
2590 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002591 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002592 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002593
2594 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002595 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002596 hlua_pusherror(L, "socket: full stack");
2597 goto out_fail_conf;
2598 }
2599
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002600 /* Create the object: obj[0] = userdata. */
2601 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002602 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002603 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002604 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002605 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002607 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002608 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002609 hlua_pusherror(L, "socket: uninitialized pools.");
2610 goto out_fail_conf;
2611 }
2612
Willy Tarreau87b09662015-04-03 00:22:06 +02002613 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002614 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2615 lua_setmetatable(L, -2);
2616
Willy Tarreaud420a972015-04-06 00:39:18 +02002617 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002618 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002619 if (!appctx) {
2620 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002621 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002622 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002623
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002624 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002625 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002626 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2627 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002628
Willy Tarreaud420a972015-04-06 00:39:18 +02002629 /* Now create a session, task and stream for this applet */
2630 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2631 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002632 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002633 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002634 }
2635
Willy Tarreau87787ac2017-08-28 16:22:54 +02002636 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002637 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002638 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002639 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002640 }
2641
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002642 /* Initialise cross reference between stream and Lua socket object. */
2643 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002644
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002645 /* Configure "right" stream interface. this "si" is used to connect
2646 * and retrieve data from the server. The connection is initialized
2647 * with the "struct server".
2648 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002649 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002650
2651 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002652 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002653 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002654
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002655 return 1;
2656
Willy Tarreaud420a972015-04-06 00:39:18 +02002657 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002658 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002659 out_fail_sess:
2660 appctx_free(appctx);
2661 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002662 WILL_LJMP(lua_error(L));
2663 return 0;
2664}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002665
2666/*
2667 *
2668 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002669 * Class Channel
2670 *
2671 *
2672 */
2673
2674/* Returns the struct hlua_channel join to the class channel in the
2675 * stack entry "ud" or throws an argument error.
2676 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002677__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002678{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002679 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002680}
2681
Willy Tarreau47860ed2015-03-10 14:07:50 +01002682/* Pushes the channel onto the top of the stack. If the stask does not have a
2683 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002684 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002685static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002686{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002687 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002688 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002689 return 0;
2690
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002691 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002692 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002693 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002694
2695 /* Pop a class sesison metatable and affect it to the userdata. */
2696 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2697 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002698 return 1;
2699}
2700
2701/* Duplicate all the data present in the input channel and put it
2702 * in a string LUA variables. Returns -1 and push a nil value in
2703 * the stack if the channel is closed and all the data are consumed,
2704 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002705 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002706 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002707static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002708{
2709 char *blk1;
2710 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002711 size_t len1;
2712 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002713 int ret;
2714 luaL_Buffer b;
2715
Willy Tarreau06d80a92017-10-19 14:32:15 +02002716 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002717 if (unlikely(ret == 0))
2718 return 0;
2719
2720 if (unlikely(ret < 0)) {
2721 lua_pushnil(L);
2722 return -1;
2723 }
2724
2725 luaL_buffinit(L, &b);
2726 luaL_addlstring(&b, blk1, len1);
2727 if (unlikely(ret == 2))
2728 luaL_addlstring(&b, blk2, len2);
2729 luaL_pushresult(&b);
2730
2731 if (unlikely(ret == 2))
2732 return len1 + len2;
2733 return len1;
2734}
2735
2736/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2737 * a yield. This function keep the data in the buffer.
2738 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002739__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002740{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002741 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002742
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002743 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2744
Christopher Faulet3f829a42018-12-13 21:56:45 +01002745 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2746 WILL_LJMP(lua_error(L));
2747
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002748 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002749 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002750 return 1;
2751}
2752
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002753/* Check arguments for the function "hlua_channel_dup_yield". */
2754__LJMP static int hlua_channel_dup(lua_State *L)
2755{
2756 MAY_LJMP(check_args(L, 1, "dup"));
2757 MAY_LJMP(hlua_checkchannel(L, 1));
2758 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2759}
2760
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002761/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2762 * a yield. This function consumes the data in the buffer. It returns
2763 * a string containing the data or a nil pointer if no data are available
2764 * and the channel is closed.
2765 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002766__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002767{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002768 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002769 int ret;
2770
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002771 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002772
Christopher Faulet3f829a42018-12-13 21:56:45 +01002773 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2774 WILL_LJMP(lua_error(L));
2775
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002776 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002777 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002778 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002779
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002780 if (unlikely(ret == -1))
2781 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002782
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002783 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002784 return 1;
2785}
2786
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002787/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002788__LJMP static int hlua_channel_get(lua_State *L)
2789{
2790 MAY_LJMP(check_args(L, 1, "get"));
2791 MAY_LJMP(hlua_checkchannel(L, 1));
2792 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2793}
2794
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002795/* This functions consumes and returns one line. If the channel is closed,
2796 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002797 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002798 * value.
2799 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002800__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002801{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002802 char *blk1;
2803 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002804 size_t len1;
2805 size_t len2;
2806 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002807 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002808 int ret;
2809 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002810
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002811 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2812
Christopher Faulet3f829a42018-12-13 21:56:45 +01002813 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2814 WILL_LJMP(lua_error(L));
2815
Willy Tarreau06d80a92017-10-19 14:32:15 +02002816 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002817 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002818 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002819
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002820 if (ret == -1) {
2821 lua_pushnil(L);
2822 return 1;
2823 }
2824
2825 luaL_buffinit(L, &b);
2826 luaL_addlstring(&b, blk1, len1);
2827 len = len1;
2828 if (unlikely(ret == 2)) {
2829 luaL_addlstring(&b, blk2, len2);
2830 len += len2;
2831 }
2832 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002833 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002834 return 1;
2835}
2836
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002837/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002838__LJMP static int hlua_channel_getline(lua_State *L)
2839{
2840 MAY_LJMP(check_args(L, 1, "getline"));
2841 MAY_LJMP(hlua_checkchannel(L, 1));
2842 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2843}
2844
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002845/* This function takes a string as input, and append it at the
2846 * input side of channel. If the data is too big, but a space
2847 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002848 * yields. If the data is bigger than the buffer, or if the
2849 * channel is closed, it returns -1. Otherwise, it returns the
2850 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002851 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002852__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002853{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002854 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002855 size_t len;
2856 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2857 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2858 int ret;
2859 int max;
2860
Christopher Faulet3f829a42018-12-13 21:56:45 +01002861 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2862 WILL_LJMP(lua_error(L));
2863
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002864 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002865 * the request buffer if its not required.
2866 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002867 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002868 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002869 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002870 }
2871
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002872 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002873 if (max > len - l)
2874 max = len - l;
2875
Willy Tarreau06d80a92017-10-19 14:32:15 +02002876 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002877 if (ret == -2 || ret == -3) {
2878 lua_pushinteger(L, -1);
2879 return 1;
2880 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002881 if (ret == -1) {
2882 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002883 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002884 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002885 l += ret;
2886 lua_pop(L, 1);
2887 lua_pushinteger(L, l);
2888
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002889 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002890 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002891 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002892 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002893 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002894 */
2895 return 1;
2896 }
2897 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002898 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002899 return 1;
2900}
2901
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002902/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2903 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002904 * buffer size is too little for the data.
2905 */
2906__LJMP static int hlua_channel_append(lua_State *L)
2907{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002908 size_t len;
2909
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002910 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002911 MAY_LJMP(hlua_checkchannel(L, 1));
2912 MAY_LJMP(luaL_checklstring(L, 2, &len));
2913 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002914 lua_pushinteger(L, 0);
2915
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002916 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002917}
2918
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002919/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002920 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002921 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002922 * or -1 if the channel is closed or if the buffer size is too
2923 * little for the data.
2924 */
2925__LJMP static int hlua_channel_set(lua_State *L)
2926{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002927 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002928
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002929 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002930 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002931 lua_pushinteger(L, 0);
2932
Christopher Faulet3f829a42018-12-13 21:56:45 +01002933 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2934 WILL_LJMP(lua_error(L));
2935
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002936 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002938 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002939}
2940
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002941/* Append data in the output side of the buffer. This data is immediately
2942 * sent. The function returns the amount of data written. If the buffer
2943 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002944 * if the channel is closed.
2945 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002946__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002947{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002948 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002949 size_t len;
2950 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2951 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2952 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002953 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002954
Christopher Faulet3f829a42018-12-13 21:56:45 +01002955 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2956 WILL_LJMP(lua_error(L));
2957
Willy Tarreau47860ed2015-03-10 14:07:50 +01002958 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002959 lua_pushinteger(L, -1);
2960 return 1;
2961 }
2962
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002963 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002964 * the request buffer if its not required.
2965 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002966 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002967 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002968 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002969 }
2970
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002971 /* The written data will be immediately sent, so we can check
2972 * the available space without taking in account the reserve.
2973 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002974 * data, because the buffer will be flushed.
2975 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002976 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002977
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002978 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002979 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002980 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002981 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02002982 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002983 return 1;
2984
2985 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002986 if (max > len - l)
2987 max = len - l;
2988
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002989 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002990 * detects a non contiguous buffer and realign it.
2991 */
Willy Tarreau3f679992018-06-15 15:06:42 +02002992 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002993 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002994
2995 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002996 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002997
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002998 /* buffer replace considers that the input part is filled.
2999 * so, I must forward these new data in the output part.
3000 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003001 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003002
3003 l += max;
3004 lua_pop(L, 1);
3005 lua_pushinteger(L, l);
3006
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003007 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +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 FOURNIERdeb5d732015-03-06 01:07:45 +01003010 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003011 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003012 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003013 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003014
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003015 if (l < len) {
3016 /* If we are waiting for space in the response buffer, we
3017 * must set the flag WAKERESWR. This flag required the task
3018 * wake up if any activity is detected on the response buffer.
3019 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003020 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003021 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003022 else
3023 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003024 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003025 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003026
3027 return 1;
3028}
3029
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003030/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031 * yield the LUA process, and resume it without checking the
3032 * input arguments.
3033 */
3034__LJMP static int hlua_channel_send(lua_State *L)
3035{
3036 MAY_LJMP(check_args(L, 2, "send"));
3037 lua_pushinteger(L, 0);
3038
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003039 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003040}
3041
3042/* This function forward and amount of butes. The data pass from
3043 * the input side of the buffer to the output side, and can be
3044 * forwarded. This function never fails.
3045 *
3046 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003047 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003048 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003049__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003050{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003051 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003052 int len;
3053 int l;
3054 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003055 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003056
3057 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003058
3059 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3060 WILL_LJMP(lua_error(L));
3061
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003062 len = MAY_LJMP(luaL_checkinteger(L, 2));
3063 l = MAY_LJMP(luaL_checkinteger(L, -1));
3064
3065 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003066 if (max > ci_data(chn))
3067 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003068 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003069 l += max;
3070
3071 lua_pop(L, 1);
3072 lua_pushinteger(L, l);
3073
3074 /* Check if it miss bytes to forward. */
3075 if (l < len) {
3076 /* The the input channel or the output channel are closed, we
3077 * must return the amount of data forwarded.
3078 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003079 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003080 return 1;
3081
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003082 /* If we are waiting for space data in the response buffer, we
3083 * must set the flag WAKERESWR. This flag required the task
3084 * wake up if any activity is detected on the response buffer.
3085 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003086 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003087 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003088 else
3089 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003090
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003091 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003092 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003093 }
3094
3095 return 1;
3096}
3097
3098/* Just check the input and prepare the stack for the previous
3099 * function "hlua_channel_forward_yield"
3100 */
3101__LJMP static int hlua_channel_forward(lua_State *L)
3102{
3103 MAY_LJMP(check_args(L, 2, "forward"));
3104 MAY_LJMP(hlua_checkchannel(L, 1));
3105 MAY_LJMP(luaL_checkinteger(L, 2));
3106
3107 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003108 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003109}
3110
3111/* Just returns the number of bytes available in the input
3112 * side of the buffer. This function never fails.
3113 */
3114__LJMP static int hlua_channel_get_in_len(lua_State *L)
3115{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003116 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003117
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003118 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003119 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003120 if (IS_HTX_STRM(chn_strm(chn))) {
3121 struct htx *htx = htxbuf(&chn->buf);
3122 lua_pushinteger(L, htx->data - co_data(chn));
3123 }
3124 else
3125 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003126 return 1;
3127}
3128
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003129/* Returns true if the channel is full. */
3130__LJMP static int hlua_channel_is_full(lua_State *L)
3131{
3132 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003133
3134 MAY_LJMP(check_args(L, 1, "is_full"));
3135 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003136 /* ignore the reserve, we are not on a producer side (ie in an
3137 * applet).
3138 */
3139 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003140 return 1;
3141}
3142
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003143/* Returns true if the channel is the response channel. */
3144__LJMP static int hlua_channel_is_resp(lua_State *L)
3145{
3146 struct channel *chn;
3147
3148 MAY_LJMP(check_args(L, 1, "is_resp"));
3149 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3150
3151 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3152 return 1;
3153}
3154
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003155/* Just returns the number of bytes available in the output
3156 * side of the buffer. This function never fails.
3157 */
3158__LJMP static int hlua_channel_get_out_len(lua_State *L)
3159{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003160 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003161
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003162 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003163 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003164 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003165 return 1;
3166}
3167
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003168/*
3169 *
3170 *
3171 * Class Fetches
3172 *
3173 *
3174 */
3175
3176/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003177 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003178 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003179__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003180{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003181 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003182}
3183
3184/* This function creates and push in the stack a fetch object according
3185 * with a current TXN.
3186 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003187static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003188{
Willy Tarreau7073c472015-04-06 11:15:40 +02003189 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003190
3191 /* Check stack size. */
3192 if (!lua_checkstack(L, 3))
3193 return 0;
3194
3195 /* Create the object: obj[0] = userdata.
3196 * Note that the base of the Fetches object is the
3197 * transaction object.
3198 */
3199 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003200 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003201 lua_rawseti(L, -2, 0);
3202
Willy Tarreau7073c472015-04-06 11:15:40 +02003203 hsmp->s = txn->s;
3204 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003205 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003206 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003207
3208 /* Pop a class sesison metatable and affect it to the userdata. */
3209 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3210 lua_setmetatable(L, -2);
3211
3212 return 1;
3213}
3214
3215/* This function is an LUA binding. It is called with each sample-fetch.
3216 * It uses closure argument to store the associated sample-fetch. It
3217 * returns only one argument or throws an error. An error is thrown
3218 * only if an error is encountered during the argument parsing. If
3219 * the "sample-fetch" function fails, nil is returned.
3220 */
3221__LJMP static int hlua_run_sample_fetch(lua_State *L)
3222{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003223 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003224 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003225 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003226 int i;
3227 struct sample smp;
3228
3229 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003230 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003231
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003232 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003233 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003234
Thierry FOURNIERca988662015-12-20 18:43:03 +01003235 /* Check execution authorization. */
3236 if (f->use & SMP_USE_HTTP_ANY &&
3237 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3238 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3239 "is not available in Lua services", f->kw);
3240 WILL_LJMP(lua_error(L));
3241 }
3242
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003243 /* Get extra arguments. */
3244 for (i = 0; i < lua_gettop(L) - 1; i++) {
3245 if (i >= ARGM_NBARGS)
3246 break;
3247 hlua_lua2arg(L, i + 2, &args[i]);
3248 }
3249 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003250 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003251
3252 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003253 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003254
3255 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003256 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003257 lua_pushfstring(L, "error in arguments");
3258 WILL_LJMP(lua_error(L));
3259 }
3260
3261 /* Initialise the sample. */
3262 memset(&smp, 0, sizeof(smp));
3263
3264 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003265 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003266 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003267 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003268 lua_pushstring(L, "");
3269 else
3270 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003271 return 1;
3272 }
3273
3274 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003275 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003276 hlua_smp2lua_str(L, &smp);
3277 else
3278 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003279 return 1;
3280}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003281
3282/*
3283 *
3284 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003285 * Class Converters
3286 *
3287 *
3288 */
3289
3290/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003291 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003292 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003293__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003294{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003295 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003296}
3297
3298/* This function creates and push in the stack a Converters object
3299 * according with a current TXN.
3300 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003301static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003302{
Willy Tarreau7073c472015-04-06 11:15:40 +02003303 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003304
3305 /* Check stack size. */
3306 if (!lua_checkstack(L, 3))
3307 return 0;
3308
3309 /* Create the object: obj[0] = userdata.
3310 * Note that the base of the Converters object is the
3311 * same than the TXN object.
3312 */
3313 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003314 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003315 lua_rawseti(L, -2, 0);
3316
Willy Tarreau7073c472015-04-06 11:15:40 +02003317 hsmp->s = txn->s;
3318 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003319 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003320 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003321
Willy Tarreau87b09662015-04-03 00:22:06 +02003322 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003323 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3324 lua_setmetatable(L, -2);
3325
3326 return 1;
3327}
3328
3329/* This function is an LUA binding. It is called with each converter.
3330 * It uses closure argument to store the associated converter. It
3331 * returns only one argument or throws an error. An error is thrown
3332 * only if an error is encountered during the argument parsing. If
3333 * the converter function function fails, nil is returned.
3334 */
3335__LJMP static int hlua_run_sample_conv(lua_State *L)
3336{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003337 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003338 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003339 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003340 int i;
3341 struct sample smp;
3342
3343 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003344 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003345
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003346 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003347 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003348
3349 /* Get extra arguments. */
3350 for (i = 0; i < lua_gettop(L) - 2; i++) {
3351 if (i >= ARGM_NBARGS)
3352 break;
3353 hlua_lua2arg(L, i + 3, &args[i]);
3354 }
3355 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003356 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003357
3358 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003359 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003360
3361 /* Run the special args checker. */
3362 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3363 hlua_pusherror(L, "error in arguments");
3364 WILL_LJMP(lua_error(L));
3365 }
3366
3367 /* Initialise the sample. */
3368 if (!hlua_lua2smp(L, 2, &smp)) {
3369 hlua_pusherror(L, "error in the input argument");
3370 WILL_LJMP(lua_error(L));
3371 }
3372
Willy Tarreau1777ea62016-03-10 16:15:46 +01003373 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3374
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003375 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003376 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003377 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003378 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003379 WILL_LJMP(lua_error(L));
3380 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003381 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3382 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003383 hlua_pusherror(L, "error during the input argument casting");
3384 WILL_LJMP(lua_error(L));
3385 }
3386
3387 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003388 if (!conv->process(args, &smp, conv->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
Willy Tarreaua678b432015-08-28 10:14:59 +02003392 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003393 return 1;
3394 }
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);
Willy Tarreaua678b432015-08-28 10:14:59 +02003401 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003402}
3403
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003404/*
3405 *
3406 *
3407 * Class AppletTCP
3408 *
3409 *
3410 */
3411
3412/* Returns a struct hlua_txn if the stack entry "ud" is
3413 * a class stream, otherwise it throws an error.
3414 */
3415__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3416{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003417 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003418}
3419
3420/* This function creates and push in the stack an Applet object
3421 * according with a current TXN.
3422 */
3423static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3424{
3425 struct hlua_appctx *appctx;
3426 struct stream_interface *si = ctx->owner;
3427 struct stream *s = si_strm(si);
3428 struct proxy *p = s->be;
3429
3430 /* Check stack size. */
3431 if (!lua_checkstack(L, 3))
3432 return 0;
3433
3434 /* Create the object: obj[0] = userdata.
3435 * Note that the base of the Converters object is the
3436 * same than the TXN object.
3437 */
3438 lua_newtable(L);
3439 appctx = lua_newuserdata(L, sizeof(*appctx));
3440 lua_rawseti(L, -2, 0);
3441 appctx->appctx = ctx;
3442 appctx->htxn.s = s;
3443 appctx->htxn.p = p;
3444
3445 /* Create the "f" field that contains a list of fetches. */
3446 lua_pushstring(L, "f");
3447 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3448 return 0;
3449 lua_settable(L, -3);
3450
3451 /* Create the "sf" field that contains a list of stringsafe fetches. */
3452 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003453 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003454 return 0;
3455 lua_settable(L, -3);
3456
3457 /* Create the "c" field that contains a list of converters. */
3458 lua_pushstring(L, "c");
3459 if (!hlua_converters_new(L, &appctx->htxn, 0))
3460 return 0;
3461 lua_settable(L, -3);
3462
3463 /* Create the "sc" field that contains a list of stringsafe converters. */
3464 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003465 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003466 return 0;
3467 lua_settable(L, -3);
3468
3469 /* Pop a class stream metatable and affect it to the table. */
3470 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3471 lua_setmetatable(L, -2);
3472
3473 return 1;
3474}
3475
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003476__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3477{
3478 struct hlua_appctx *appctx;
3479 struct stream *s;
3480 const char *name;
3481 size_t len;
3482 struct sample smp;
3483
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003484 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3485 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003486
3487 /* It is useles to retrieve the stream, but this function
3488 * runs only in a stream context.
3489 */
3490 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3491 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3492 s = appctx->htxn.s;
3493
3494 /* Converts the third argument in a sample. */
3495 hlua_lua2smp(L, 3, &smp);
3496
3497 /* Store the sample in a variable. */
3498 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003499
3500 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3501 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3502 else
3503 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3504
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003505 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003506}
3507
3508__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3509{
3510 struct hlua_appctx *appctx;
3511 struct stream *s;
3512 const char *name;
3513 size_t len;
3514 struct sample smp;
3515
3516 MAY_LJMP(check_args(L, 2, "unset_var"));
3517
3518 /* It is useles to retrieve the stream, but this function
3519 * runs only in a stream context.
3520 */
3521 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3522 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3523 s = appctx->htxn.s;
3524
3525 /* Unset the variable. */
3526 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003527 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3528 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003529}
3530
3531__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3532{
3533 struct hlua_appctx *appctx;
3534 struct stream *s;
3535 const char *name;
3536 size_t len;
3537 struct sample smp;
3538
3539 MAY_LJMP(check_args(L, 2, "get_var"));
3540
3541 /* It is useles to retrieve the stream, but this function
3542 * runs only in a stream context.
3543 */
3544 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3545 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3546 s = appctx->htxn.s;
3547
3548 smp_set_owner(&smp, s->be, s->sess, s, 0);
3549 if (!vars_get_by_name(name, len, &smp)) {
3550 lua_pushnil(L);
3551 return 1;
3552 }
3553
3554 return hlua_smp2lua(L, &smp);
3555}
3556
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003557__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3558{
3559 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3560 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003561 struct hlua *hlua;
3562
3563 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003564 if (!s->hlua)
3565 return 0;
3566 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003567
3568 MAY_LJMP(check_args(L, 2, "set_priv"));
3569
3570 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003571 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003572
3573 /* Get and store new value. */
3574 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3575 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3576
3577 return 0;
3578}
3579
3580__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3581{
3582 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3583 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003584 struct hlua *hlua;
3585
3586 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003587 if (!s->hlua) {
3588 lua_pushnil(L);
3589 return 1;
3590 }
3591 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003592
3593 /* Push configuration index in the stack. */
3594 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3595
3596 return 1;
3597}
3598
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003599/* If expected data not yet available, it returns a yield. This function
3600 * consumes the data in the buffer. It returns a string containing the
3601 * data. This string can be empty.
3602 */
3603__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3604{
3605 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3606 struct stream_interface *si = appctx->appctx->owner;
3607 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003608 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003609 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003610 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003611 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003612
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003613 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003614 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003615
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003616 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003617 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003618 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003619 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003620 }
3621
3622 /* End of data: commit the total strings and return. */
3623 if (ret < 0) {
3624 luaL_pushresult(&appctx->b);
3625 return 1;
3626 }
3627
3628 /* Ensure that the block 2 length is usable. */
3629 if (ret == 1)
3630 len2 = 0;
3631
3632 /* dont check the max length read and dont check. */
3633 luaL_addlstring(&appctx->b, blk1, len1);
3634 luaL_addlstring(&appctx->b, blk2, len2);
3635
3636 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003637 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003638 luaL_pushresult(&appctx->b);
3639 return 1;
3640}
3641
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003642/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003643__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3644{
3645 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3646
3647 /* Initialise the string catenation. */
3648 luaL_buffinit(L, &appctx->b);
3649
3650 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3651}
3652
3653/* If expected data not yet available, it returns a yield. This function
3654 * consumes the data in the buffer. It returns a string containing the
3655 * data. This string can be empty.
3656 */
3657__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3658{
3659 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3660 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003661 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003662 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003663 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003664 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003665 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003666 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003667
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003668 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003669 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003670
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003671 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003672 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003673 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003674 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003675 }
3676
3677 /* End of data: commit the total strings and return. */
3678 if (ret < 0) {
3679 luaL_pushresult(&appctx->b);
3680 return 1;
3681 }
3682
3683 /* Ensure that the block 2 length is usable. */
3684 if (ret == 1)
3685 len2 = 0;
3686
3687 if (len == -1) {
3688
3689 /* If len == -1, catenate all the data avalaile and
3690 * yield because we want to get all the data until
3691 * the end of data stream.
3692 */
3693 luaL_addlstring(&appctx->b, blk1, len1);
3694 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003695 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003696 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003697 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003698
3699 } else {
3700
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003701 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003702 if (len1 > len)
3703 len1 = len;
3704 luaL_addlstring(&appctx->b, blk1, len1);
3705 len -= len1;
3706
3707 /* Copy the second block. */
3708 if (len2 > len)
3709 len2 = len;
3710 luaL_addlstring(&appctx->b, blk2, len2);
3711 len -= len2;
3712
3713 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003714 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003715
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003716 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003717 if (len > 0) {
3718 lua_pushinteger(L, len);
3719 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003720 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003721 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003722 }
3723
3724 /* return the result. */
3725 luaL_pushresult(&appctx->b);
3726 return 1;
3727 }
3728
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003729 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003730 hlua_pusherror(L, "Lua: internal error");
3731 WILL_LJMP(lua_error(L));
3732 return 0;
3733}
3734
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003735/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003736__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3737{
3738 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3739 int len = -1;
3740
3741 if (lua_gettop(L) > 2)
3742 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3743 if (lua_gettop(L) >= 2) {
3744 len = MAY_LJMP(luaL_checkinteger(L, 2));
3745 lua_pop(L, 1);
3746 }
3747
3748 /* Confirm or set the required length */
3749 lua_pushinteger(L, len);
3750
3751 /* Initialise the string catenation. */
3752 luaL_buffinit(L, &appctx->b);
3753
3754 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3755}
3756
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003757/* Append data in the output side of the buffer. This data is immediately
3758 * sent. The function returns the amount of data written. If the buffer
3759 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003760 * if the channel is closed.
3761 */
3762__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3763{
3764 size_t len;
3765 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3766 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3767 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3768 struct stream_interface *si = appctx->appctx->owner;
3769 struct channel *chn = si_ic(si);
3770 int max;
3771
3772 /* Get the max amount of data which can write as input in the channel. */
3773 max = channel_recv_max(chn);
3774 if (max > (len - l))
3775 max = len - l;
3776
3777 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003778 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003779
3780 /* update counters. */
3781 l += max;
3782 lua_pop(L, 1);
3783 lua_pushinteger(L, l);
3784
3785 /* If some data is not send, declares the situation to the
3786 * applet, and returns a yield.
3787 */
3788 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003789 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003790 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003791 }
3792
3793 return 1;
3794}
3795
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003796/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003797 * yield the LUA process, and resume it without checking the
3798 * input arguments.
3799 */
3800__LJMP static int hlua_applet_tcp_send(lua_State *L)
3801{
3802 MAY_LJMP(check_args(L, 2, "send"));
3803 lua_pushinteger(L, 0);
3804
3805 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3806}
3807
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003808/*
3809 *
3810 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003811 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003812 *
3813 *
3814 */
3815
3816/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003817 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003818 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003819__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003820{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003821 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003822}
3823
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003824/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003825 * according with a current TXN.
3826 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003827static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003828{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003829 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003830 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003831 struct stream_interface *si = ctx->owner;
3832 struct stream *s = si_strm(si);
3833 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003834 struct htx *htx;
3835 struct htx_blk *blk;
3836 struct htx_sl *sl;
3837 struct ist path;
3838 unsigned long long len = 0;
3839 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003840
3841 /* Check stack size. */
3842 if (!lua_checkstack(L, 3))
3843 return 0;
3844
3845 /* Create the object: obj[0] = userdata.
3846 * Note that the base of the Converters object is the
3847 * same than the TXN object.
3848 */
3849 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003850 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003851 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003852 appctx->appctx = ctx;
3853 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003854 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003855 appctx->htxn.s = s;
3856 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003857
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003858 /* Create the "f" field that contains a list of fetches. */
3859 lua_pushstring(L, "f");
3860 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3861 return 0;
3862 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003863
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003864 /* Create the "sf" field that contains a list of stringsafe fetches. */
3865 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003866 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003867 return 0;
3868 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003869
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003870 /* Create the "c" field that contains a list of converters. */
3871 lua_pushstring(L, "c");
3872 if (!hlua_converters_new(L, &appctx->htxn, 0))
3873 return 0;
3874 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003875
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003876 /* Create the "sc" field that contains a list of stringsafe converters. */
3877 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003878 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003879 return 0;
3880 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003881
Christopher Fauleta2097962019-07-15 16:25:33 +02003882 htx = htxbuf(&s->req.buf);
3883 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003884 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003885 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003886
Christopher Fauleta2097962019-07-15 16:25:33 +02003887 /* Stores the request method. */
3888 lua_pushstring(L, "method");
3889 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3890 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003891
Christopher Fauleta2097962019-07-15 16:25:33 +02003892 /* Stores the http version. */
3893 lua_pushstring(L, "version");
3894 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3895 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003896
Christopher Fauleta2097962019-07-15 16:25:33 +02003897 /* creates an array of headers. hlua_http_get_headers() crates and push
3898 * the array on the top of the stack.
3899 */
3900 lua_pushstring(L, "headers");
3901 htxn.s = s;
3902 htxn.p = px;
3903 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01003904 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02003905 return 0;
3906 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003907
Christopher Fauleta2097962019-07-15 16:25:33 +02003908 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01003909 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02003910 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003911
Christopher Fauleta2097962019-07-15 16:25:33 +02003912 p = path.ptr;
3913 end = path.ptr + path.len;
3914 q = p;
3915 while (q < end && *q != '?')
3916 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003917
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003918 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003919 lua_pushstring(L, "path");
3920 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003921 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003922
Christopher Fauleta2097962019-07-15 16:25:33 +02003923 /* Stores the query string. */
3924 lua_pushstring(L, "qs");
3925 if (*q == '?')
3926 q++;
3927 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003928 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003929 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003930
Christopher Fauleta2097962019-07-15 16:25:33 +02003931 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3932 struct htx_blk *blk = htx_get_blk(htx, pos);
3933 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003934
Christopher Fauleta2097962019-07-15 16:25:33 +02003935 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3936 break;
3937 if (type == HTX_BLK_DATA)
3938 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003939 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003940 if (htx->extra != ULLONG_MAX)
3941 len += htx->extra;
3942
3943 /* Stores the request path. */
3944 lua_pushstring(L, "length");
3945 lua_pushinteger(L, len);
3946 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003947
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003948 /* Create an empty array of HTTP request headers. */
3949 lua_pushstring(L, "response");
3950 lua_newtable(L);
3951 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003952
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003953 /* Pop a class stream metatable and affect it to the table. */
3954 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3955 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003956
3957 return 1;
3958}
3959
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003960__LJMP static int hlua_applet_http_set_var(lua_State *L)
3961{
3962 struct hlua_appctx *appctx;
3963 struct stream *s;
3964 const char *name;
3965 size_t len;
3966 struct sample smp;
3967
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003968 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3969 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003970
3971 /* It is useles to retrieve the stream, but this function
3972 * runs only in a stream context.
3973 */
3974 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3975 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3976 s = appctx->htxn.s;
3977
3978 /* Converts the third argument in a sample. */
3979 hlua_lua2smp(L, 3, &smp);
3980
3981 /* Store the sample in a variable. */
3982 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003983
3984 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3985 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3986 else
3987 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3988
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003989 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003990}
3991
3992__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3993{
3994 struct hlua_appctx *appctx;
3995 struct stream *s;
3996 const char *name;
3997 size_t len;
3998 struct sample smp;
3999
4000 MAY_LJMP(check_args(L, 2, "unset_var"));
4001
4002 /* It is useles to retrieve the stream, but this function
4003 * runs only in a stream context.
4004 */
4005 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4006 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4007 s = appctx->htxn.s;
4008
4009 /* Unset the variable. */
4010 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004011 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4012 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004013}
4014
4015__LJMP static int hlua_applet_http_get_var(lua_State *L)
4016{
4017 struct hlua_appctx *appctx;
4018 struct stream *s;
4019 const char *name;
4020 size_t len;
4021 struct sample smp;
4022
4023 MAY_LJMP(check_args(L, 2, "get_var"));
4024
4025 /* It is useles to retrieve the stream, but this function
4026 * runs only in a stream context.
4027 */
4028 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4029 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4030 s = appctx->htxn.s;
4031
4032 smp_set_owner(&smp, s->be, s->sess, s, 0);
4033 if (!vars_get_by_name(name, len, &smp)) {
4034 lua_pushnil(L);
4035 return 1;
4036 }
4037
4038 return hlua_smp2lua(L, &smp);
4039}
4040
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004041__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4042{
4043 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4044 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004045 struct hlua *hlua;
4046
4047 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004048 if (!s->hlua)
4049 return 0;
4050 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004051
4052 MAY_LJMP(check_args(L, 2, "set_priv"));
4053
4054 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004055 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004056
4057 /* Get and store new value. */
4058 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4059 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4060
4061 return 0;
4062}
4063
4064__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4065{
4066 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4067 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004068 struct hlua *hlua;
4069
4070 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004071 if (!s->hlua) {
4072 lua_pushnil(L);
4073 return 1;
4074 }
4075 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004076
4077 /* Push configuration index in the stack. */
4078 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4079
4080 return 1;
4081}
4082
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004083/* If expected data not yet available, it returns a yield. This function
4084 * consumes the data in the buffer. It returns a string containing the
4085 * data. This string can be empty.
4086 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004087__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004088{
4089 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4090 struct stream_interface *si = appctx->appctx->owner;
4091 struct channel *req = si_oc(si);
4092 struct htx *htx;
4093 struct htx_blk *blk;
4094 size_t count;
4095 int stop = 0;
4096
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004097 htx = htx_from_buf(&req->buf);
4098 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004099 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004100
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004101 while (count && !stop && blk) {
4102 enum htx_blk_type type = htx_get_blk_type(blk);
4103 uint32_t sz = htx_get_blksz(blk);
4104 struct ist v;
4105 uint32_t vlen;
4106 char *nl;
4107
Christopher Faulete461e342018-12-18 16:43:35 +01004108 if (type == HTX_BLK_EOM) {
4109 stop = 1;
4110 break;
4111 }
4112
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004113 vlen = sz;
4114 if (vlen > count) {
4115 if (type != HTX_BLK_DATA)
4116 break;
4117 vlen = count;
4118 }
4119
4120 switch (type) {
4121 case HTX_BLK_UNUSED:
4122 break;
4123
4124 case HTX_BLK_DATA:
4125 v = htx_get_blk_value(htx, blk);
4126 v.len = vlen;
4127 nl = istchr(v, '\n');
4128 if (nl != NULL) {
4129 stop = 1;
4130 vlen = nl - v.ptr + 1;
4131 }
4132 luaL_addlstring(&appctx->b, v.ptr, vlen);
4133 break;
4134
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004135 case HTX_BLK_TLR:
4136 case HTX_BLK_EOM:
4137 stop = 1;
4138 break;
4139
4140 default:
4141 break;
4142 }
4143
4144 co_set_data(req, co_data(req) - vlen);
4145 count -= vlen;
4146 if (sz == vlen)
4147 blk = htx_remove_blk(htx, blk);
4148 else {
4149 htx_cut_data_blk(htx, blk, vlen);
4150 break;
4151 }
4152 }
4153
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004154 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004155 if (!stop) {
4156 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004157 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004158 }
4159
4160 /* return the result. */
4161 luaL_pushresult(&appctx->b);
4162 return 1;
4163}
4164
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004165
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004166/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004167__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004168{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004169 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004170
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004171 /* Initialise the string catenation. */
4172 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004173
Christopher Fauleta2097962019-07-15 16:25:33 +02004174 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004175}
4176
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004177/* If expected data not yet available, it returns a yield. This function
4178 * consumes the data in the buffer. It returns a string containing the
4179 * data. This string can be empty.
4180 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004181__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004182{
4183 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4184 struct stream_interface *si = appctx->appctx->owner;
4185 struct channel *req = si_oc(si);
4186 struct htx *htx;
4187 struct htx_blk *blk;
4188 size_t count;
4189 int len;
4190
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004191 htx = htx_from_buf(&req->buf);
4192 len = MAY_LJMP(luaL_checkinteger(L, 2));
4193 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004194 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004195 while (count && len && blk) {
4196 enum htx_blk_type type = htx_get_blk_type(blk);
4197 uint32_t sz = htx_get_blksz(blk);
4198 struct ist v;
4199 uint32_t vlen;
4200
Christopher Faulete461e342018-12-18 16:43:35 +01004201 if (type == HTX_BLK_EOM) {
4202 len = 0;
4203 break;
4204 }
4205
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004206 vlen = sz;
4207 if (len > 0 && vlen > len)
4208 vlen = len;
4209 if (vlen > count) {
4210 if (type != HTX_BLK_DATA)
4211 break;
4212 vlen = count;
4213 }
4214
4215 switch (type) {
4216 case HTX_BLK_UNUSED:
4217 break;
4218
4219 case HTX_BLK_DATA:
4220 v = htx_get_blk_value(htx, blk);
4221 luaL_addlstring(&appctx->b, v.ptr, vlen);
4222 break;
4223
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004224 case HTX_BLK_TLR:
4225 case HTX_BLK_EOM:
4226 len = 0;
4227 break;
4228
4229 default:
4230 break;
4231 }
4232
4233 co_set_data(req, co_data(req) - vlen);
4234 count -= vlen;
4235 if (len > 0)
4236 len -= vlen;
4237 if (sz == vlen)
4238 blk = htx_remove_blk(htx, blk);
4239 else {
4240 htx_cut_data_blk(htx, blk, vlen);
4241 break;
4242 }
4243 }
4244
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004245 htx_to_buf(htx, &req->buf);
4246
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004247 /* If we are no other data available, yield waiting for new data. */
4248 if (len) {
4249 if (len > 0) {
4250 lua_pushinteger(L, len);
4251 lua_replace(L, 2);
4252 }
4253 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004254 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004255 }
4256
4257 /* return the result. */
4258 luaL_pushresult(&appctx->b);
4259 return 1;
4260}
4261
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004262/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004263__LJMP static int hlua_applet_http_recv(lua_State *L)
4264{
4265 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4266 int len = -1;
4267
4268 /* Check arguments. */
4269 if (lua_gettop(L) > 2)
4270 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4271 if (lua_gettop(L) >= 2) {
4272 len = MAY_LJMP(luaL_checkinteger(L, 2));
4273 lua_pop(L, 1);
4274 }
4275
Christopher Fauleta2097962019-07-15 16:25:33 +02004276 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004277
Christopher Fauleta2097962019-07-15 16:25:33 +02004278 /* Initialise the string catenation. */
4279 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004280
Christopher Fauleta2097962019-07-15 16:25:33 +02004281 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004282}
4283
4284/* Append data in the output side of the buffer. This data is immediately
4285 * sent. The function returns the amount of data written. If the buffer
4286 * cannot contain the data, the function yields. The function returns -1
4287 * if the channel is closed.
4288 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004289__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004290{
4291 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4292 struct stream_interface *si = appctx->appctx->owner;
4293 struct channel *res = si_ic(si);
4294 struct htx *htx = htx_from_buf(&res->buf);
4295 const char *data;
4296 size_t len;
4297 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4298 int max;
4299
Christopher Faulet9060fc02019-07-03 11:39:30 +02004300 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004301 if (!max)
4302 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004303
4304 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4305
4306 /* Get the max amount of data which can write as input in the channel. */
4307 if (max > (len - l))
4308 max = len - l;
4309
4310 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004311 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004312 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004313
4314 /* update counters. */
4315 l += max;
4316 lua_pop(L, 1);
4317 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004318
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004319 /* If some data is not send, declares the situation to the
4320 * applet, and returns a yield.
4321 */
4322 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004323 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004324 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004325 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004326 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004327 }
4328
Christopher Fauleta2097962019-07-15 16:25:33 +02004329 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004330 return 1;
4331}
4332
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004333/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004334 * yield the LUA process, and resume it without checking the
4335 * input arguments.
4336 */
4337__LJMP static int hlua_applet_http_send(lua_State *L)
4338{
4339 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004340
4341 /* We want to send some data. Headers must be sent. */
4342 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4343 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4344 WILL_LJMP(lua_error(L));
4345 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004346
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004347 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004348 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004349
Christopher Fauleta2097962019-07-15 16:25:33 +02004350 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004351}
4352
4353__LJMP static int hlua_applet_http_addheader(lua_State *L)
4354{
4355 const char *name;
4356 int ret;
4357
4358 MAY_LJMP(hlua_checkapplet_http(L, 1));
4359 name = MAY_LJMP(luaL_checkstring(L, 2));
4360 MAY_LJMP(luaL_checkstring(L, 3));
4361
4362 /* Push in the stack the "response" entry. */
4363 ret = lua_getfield(L, 1, "response");
4364 if (ret != LUA_TTABLE) {
4365 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4366 "is expected as an array. %s found", lua_typename(L, ret));
4367 WILL_LJMP(lua_error(L));
4368 }
4369
4370 /* check if the header is already registered if it is not
4371 * the case, register it.
4372 */
4373 ret = lua_getfield(L, -1, name);
4374 if (ret == LUA_TNIL) {
4375
4376 /* Entry not found. */
4377 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4378
4379 /* Insert the new header name in the array in the top of the stack.
4380 * It left the new array in the top of the stack.
4381 */
4382 lua_newtable(L);
4383 lua_pushvalue(L, 2);
4384 lua_pushvalue(L, -2);
4385 lua_settable(L, -4);
4386
4387 } else if (ret != LUA_TTABLE) {
4388
4389 /* corruption error. */
4390 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4391 "is expected as an array. %s found", name, lua_typename(L, ret));
4392 WILL_LJMP(lua_error(L));
4393 }
4394
4395 /* Now the top od thestack is an array of values. We push
4396 * the header value as new entry.
4397 */
4398 lua_pushvalue(L, 3);
4399 ret = lua_rawlen(L, -2);
4400 lua_rawseti(L, -2, ret + 1);
4401 lua_pushboolean(L, 1);
4402 return 1;
4403}
4404
4405__LJMP static int hlua_applet_http_status(lua_State *L)
4406{
4407 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4408 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004409 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004410
4411 if (status < 100 || status > 599) {
4412 lua_pushboolean(L, 0);
4413 return 1;
4414 }
4415
4416 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004417 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004418 lua_pushboolean(L, 1);
4419 return 1;
4420}
4421
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004422
Christopher Fauleta2097962019-07-15 16:25:33 +02004423__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004424{
4425 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4426 struct stream_interface *si = appctx->appctx->owner;
4427 struct channel *res = si_ic(si);
4428 struct htx *htx;
4429 struct htx_sl *sl;
4430 struct h1m h1m;
4431 const char *status, *reason;
4432 const char *name, *value;
4433 size_t nlen, vlen;
4434 unsigned int flags;
4435
4436 /* Send the message at once. */
4437 htx = htx_from_buf(&res->buf);
4438 h1m_init_res(&h1m);
4439
4440 /* Use the same http version than the request. */
4441 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4442 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4443 if (reason == NULL)
4444 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4445 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4446 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4447 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4448 }
4449 else {
4450 flags = HTX_SL_F_IS_RESP;
4451 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4452 }
4453 if (!sl) {
4454 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4455 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4456 WILL_LJMP(lua_error(L));
4457 }
4458 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4459
4460 /* Get the array associated to the field "response" in the object AppletHTTP. */
4461 lua_pushvalue(L, 0);
4462 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4463 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4464 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4465 WILL_LJMP(lua_error(L));
4466 }
4467
4468 /* Browse the list of headers. */
4469 lua_pushnil(L);
4470 while(lua_next(L, -2) != 0) {
4471 /* We expect a string as -2. */
4472 if (lua_type(L, -2) != LUA_TSTRING) {
4473 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4474 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4475 lua_typename(L, lua_type(L, -2)));
4476 WILL_LJMP(lua_error(L));
4477 }
4478 name = lua_tolstring(L, -2, &nlen);
4479
4480 /* We expect an array as -1. */
4481 if (lua_type(L, -1) != LUA_TTABLE) {
4482 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4483 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4484 name,
4485 lua_typename(L, lua_type(L, -1)));
4486 WILL_LJMP(lua_error(L));
4487 }
4488
4489 /* Browse the table who is on the top of the stack. */
4490 lua_pushnil(L);
4491 while(lua_next(L, -2) != 0) {
4492 int id;
4493
4494 /* We expect a number as -2. */
4495 if (lua_type(L, -2) != LUA_TNUMBER) {
4496 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4497 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4498 name,
4499 lua_typename(L, lua_type(L, -2)));
4500 WILL_LJMP(lua_error(L));
4501 }
4502 id = lua_tointeger(L, -2);
4503
4504 /* We expect a string as -2. */
4505 if (lua_type(L, -1) != LUA_TSTRING) {
4506 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4507 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4508 name, id,
4509 lua_typename(L, lua_type(L, -1)));
4510 WILL_LJMP(lua_error(L));
4511 }
4512 value = lua_tolstring(L, -1, &vlen);
4513
4514 /* Simple Protocol checks. */
4515 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004516 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004517 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4518 struct ist v = ist2(value, vlen);
4519 int ret;
4520
4521 ret = h1_parse_cont_len_header(&h1m, &v);
4522 if (ret < 0) {
4523 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4524 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4525 name);
4526 WILL_LJMP(lua_error(L));
4527 }
4528 else if (ret == 0)
4529 goto next; /* Skip it */
4530 }
4531
4532 /* Add a new header */
4533 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4534 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4535 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4536 name);
4537 WILL_LJMP(lua_error(L));
4538 }
4539 next:
4540 /* Remove the array from the stack, and get next element with a remaining string. */
4541 lua_pop(L, 1);
4542 }
4543
4544 /* Remove the array from the stack, and get next element with a remaining string. */
4545 lua_pop(L, 1);
4546 }
4547
4548 if (h1m.flags & H1_MF_CHNK)
4549 h1m.flags &= ~H1_MF_CLEN;
4550 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4551 h1m.flags |= H1_MF_XFER_LEN;
4552
4553 /* Uset HTX start-line flags */
4554 if (h1m.flags & H1_MF_XFER_ENC)
4555 flags |= HTX_SL_F_XFER_ENC;
4556 if (h1m.flags & H1_MF_XFER_LEN) {
4557 flags |= HTX_SL_F_XFER_LEN;
4558 if (h1m.flags & H1_MF_CHNK)
4559 flags |= HTX_SL_F_CHNK;
4560 else if (h1m.flags & H1_MF_CLEN)
4561 flags |= HTX_SL_F_CLEN;
4562 if (h1m.body_len == 0)
4563 flags |= HTX_SL_F_BODYLESS;
4564 }
4565 sl->flags |= flags;
4566
4567 /* If we dont have a content-length set, and the HTTP version is 1.1
4568 * and the status code implies the presence of a message body, we must
4569 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004570 * for the keepalive compliance. If the applet announces a transfer-encoding
4571 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004572 */
4573 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4574 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4575 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4576 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4577 /* Add a new header */
4578 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4579 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4580 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4581 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4582 WILL_LJMP(lua_error(L));
4583 }
4584 }
4585
4586 /* Finalize headers. */
4587 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4588 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4589 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4590 WILL_LJMP(lua_error(L));
4591 }
4592
4593 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4594 b_reset(&res->buf);
4595 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4596 WILL_LJMP(lua_error(L));
4597 }
4598
4599 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004600 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004601
4602 /* Headers sent, set the flag. */
4603 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4604 return 0;
4605
4606}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004607/* We will build the status line and the headers of the HTTP response.
4608 * We will try send at once if its not possible, we give back the hand
4609 * waiting for more room.
4610 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004611__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004612{
4613 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4614 struct stream_interface *si = appctx->appctx->owner;
4615 struct channel *res = si_ic(si);
4616
4617 if (co_data(res)) {
4618 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004619 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004620 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004621 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004622}
4623
4624
Christopher Fauleta2097962019-07-15 16:25:33 +02004625__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004626{
Christopher Fauleta2097962019-07-15 16:25:33 +02004627 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004628}
4629
Christopher Fauleta2097962019-07-15 16:25:33 +02004630/*
4631 *
4632 *
4633 * Class HTTP
4634 *
4635 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004636 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004637
4638/* Returns a struct hlua_txn if the stack entry "ud" is
4639 * a class stream, otherwise it throws an error.
4640 */
4641__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004642{
Christopher Fauleta2097962019-07-15 16:25:33 +02004643 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4644}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004645
Christopher Fauleta2097962019-07-15 16:25:33 +02004646/* This function creates and push in the stack a HTTP object
4647 * according with a current TXN.
4648 */
4649static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4650{
4651 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004652
Christopher Fauleta2097962019-07-15 16:25:33 +02004653 /* Check stack size. */
4654 if (!lua_checkstack(L, 3))
4655 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004656
Christopher Fauleta2097962019-07-15 16:25:33 +02004657 /* Create the object: obj[0] = userdata.
4658 * Note that the base of the Converters object is the
4659 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004660 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004661 lua_newtable(L);
4662 htxn = lua_newuserdata(L, sizeof(*htxn));
4663 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004664
4665 htxn->s = txn->s;
4666 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004667 htxn->dir = txn->dir;
4668 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004669
4670 /* Pop a class stream metatable and affect it to the table. */
4671 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4672 lua_setmetatable(L, -2);
4673
4674 return 1;
4675}
4676
4677/* This function creates ans returns an array of HTTP headers.
4678 * This function does not fails. It is used as wrapper with the
4679 * 2 following functions.
4680 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004681__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004682{
Christopher Fauleta2097962019-07-15 16:25:33 +02004683 struct htx *htx;
4684 int32_t pos;
4685
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004686 /* Create the table. */
4687 lua_newtable(L);
4688
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004689
Christopher Fauleta2097962019-07-15 16:25:33 +02004690 htx = htxbuf(&msg->chn->buf);
4691 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4692 struct htx_blk *blk = htx_get_blk(htx, pos);
4693 enum htx_blk_type type = htx_get_blk_type(blk);
4694 struct ist n, v;
4695 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004696
Christopher Fauleta2097962019-07-15 16:25:33 +02004697 if (type == HTX_BLK_HDR) {
4698 n = htx_get_blk_name(htx,blk);
4699 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004700 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004701 else if (type == HTX_BLK_EOH)
4702 break;
4703 else
4704 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004705
Christopher Fauleta2097962019-07-15 16:25:33 +02004706 /* Check for existing entry:
4707 * assume that the table is on the top of the stack, and
4708 * push the key in the stack, the function lua_gettable()
4709 * perform the lookup.
4710 */
4711 lua_pushlstring(L, n.ptr, n.len);
4712 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004713
Christopher Fauleta2097962019-07-15 16:25:33 +02004714 switch (lua_type(L, -1)) {
4715 case LUA_TNIL:
4716 /* Table not found, create it. */
4717 lua_pop(L, 1); /* remove the nil value. */
4718 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4719 lua_newtable(L); /* create and push empty table. */
4720 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4721 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4722 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004723 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004724
Christopher Fauleta2097962019-07-15 16:25:33 +02004725 case LUA_TTABLE:
4726 /* Entry found: push the value in the table. */
4727 len = lua_rawlen(L, -1);
4728 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4729 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4730 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4731 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004732
Christopher Fauleta2097962019-07-15 16:25:33 +02004733 default:
4734 /* Other cases are errors. */
4735 hlua_pusherror(L, "internal error during the parsing of headers.");
4736 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004737 }
4738 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004739 return 1;
4740}
4741
4742__LJMP static int hlua_http_req_get_headers(lua_State *L)
4743{
4744 struct hlua_txn *htxn;
4745
4746 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4747 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4748
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004749 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004750 WILL_LJMP(lua_error(L));
4751
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004752 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004753}
4754
4755__LJMP static int hlua_http_res_get_headers(lua_State *L)
4756{
4757 struct hlua_txn *htxn;
4758
4759 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4760 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4761
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004762 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004763 WILL_LJMP(lua_error(L));
4764
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004765 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004766}
4767
4768/* This function replace full header, or just a value in
4769 * the request or in the response. It is a wrapper fir the
4770 * 4 following functions.
4771 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004772__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004773{
4774 size_t name_len;
4775 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4776 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4777 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004778 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004779 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004780
Dragan Dosen26743032019-04-30 15:54:36 +02004781 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004782 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4783
Christopher Fauleta2097962019-07-15 16:25:33 +02004784 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004785 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004786 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004787 return 0;
4788}
4789
4790__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4791{
4792 struct hlua_txn *htxn;
4793
4794 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4795 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4796
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004797 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004798 WILL_LJMP(lua_error(L));
4799
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004800 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004801}
4802
4803__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4804{
4805 struct hlua_txn *htxn;
4806
4807 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4808 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4809
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004810 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004811 WILL_LJMP(lua_error(L));
4812
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004813 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004814}
4815
4816__LJMP static int hlua_http_req_rep_val(lua_State *L)
4817{
4818 struct hlua_txn *htxn;
4819
4820 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4821 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4822
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004823 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004824 WILL_LJMP(lua_error(L));
4825
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004826 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004827}
4828
4829__LJMP static int hlua_http_res_rep_val(lua_State *L)
4830{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004831 struct hlua_txn *htxn;
4832
4833 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4834 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4835
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004836 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004837 WILL_LJMP(lua_error(L));
4838
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004839 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004840}
4841
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004842/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004843 * It is a wrapper for the 2 following functions.
4844 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004845__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004846{
4847 size_t len;
4848 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004849 struct htx *htx = htxbuf(&msg->chn->buf);
4850 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004851
Christopher Fauleta2097962019-07-15 16:25:33 +02004852 ctx.blk = NULL;
4853 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4854 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004855 return 0;
4856}
4857
4858__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4859{
4860 struct hlua_txn *htxn;
4861
4862 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4863 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4864
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004865 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004866 WILL_LJMP(lua_error(L));
4867
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004868 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004869}
4870
4871__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4872{
4873 struct hlua_txn *htxn;
4874
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004875 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004876 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4877
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004878 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004879 WILL_LJMP(lua_error(L));
4880
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004881 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004882}
4883
4884/* This function adds an header. It is a wrapper used by
4885 * the 2 following functions.
4886 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004887__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004888{
4889 size_t name_len;
4890 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4891 size_t value_len;
4892 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004893 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004894
Christopher Fauleta2097962019-07-15 16:25:33 +02004895 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4896 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004897 return 0;
4898}
4899
4900__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4901{
4902 struct hlua_txn *htxn;
4903
4904 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4905 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4906
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004907 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004908 WILL_LJMP(lua_error(L));
4909
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004910 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004911}
4912
4913__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4914{
4915 struct hlua_txn *htxn;
4916
4917 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4918 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4919
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004920 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004921 WILL_LJMP(lua_error(L));
4922
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004923 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004924}
4925
4926static int hlua_http_req_set_hdr(lua_State *L)
4927{
4928 struct hlua_txn *htxn;
4929
4930 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4931 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4932
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004933 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004934 WILL_LJMP(lua_error(L));
4935
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004936 hlua_http_del_hdr(L, &htxn->s->txn->req);
4937 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004938}
4939
4940static int hlua_http_res_set_hdr(lua_State *L)
4941{
4942 struct hlua_txn *htxn;
4943
4944 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4945 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4946
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004947 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004948 WILL_LJMP(lua_error(L));
4949
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004950 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
4951 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004952}
4953
4954/* This function set the method. */
4955static int hlua_http_req_set_meth(lua_State *L)
4956{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004957 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004958 size_t name_len;
4959 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004960
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004961 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004962 WILL_LJMP(lua_error(L));
4963
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004964 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004965 return 1;
4966}
4967
4968/* This function set the method. */
4969static int hlua_http_req_set_path(lua_State *L)
4970{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004971 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004972 size_t name_len;
4973 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004974
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004975 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004976 WILL_LJMP(lua_error(L));
4977
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004978 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004979 return 1;
4980}
4981
4982/* This function set the query-string. */
4983static int hlua_http_req_set_query(lua_State *L)
4984{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004985 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004986 size_t name_len;
4987 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004988
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004989 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004990 WILL_LJMP(lua_error(L));
4991
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004992 /* Check length. */
4993 if (name_len > trash.size - 1) {
4994 lua_pushboolean(L, 0);
4995 return 1;
4996 }
4997
4998 /* Add the mark question as prefix. */
4999 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005000 trash.area[trash.data++] = '?';
5001 memcpy(trash.area + trash.data, name, name_len);
5002 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005003
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005004 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005005 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005006 return 1;
5007}
5008
5009/* This function set the uri. */
5010static int hlua_http_req_set_uri(lua_State *L)
5011{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005012 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005013 size_t name_len;
5014 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005015
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005016 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005017 WILL_LJMP(lua_error(L));
5018
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005019 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005020 return 1;
5021}
5022
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005023/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005024static int hlua_http_res_set_status(lua_State *L)
5025{
5026 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5027 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005028 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5029 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005030
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005031 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005032 WILL_LJMP(lua_error(L));
5033
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005034 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005035 return 0;
5036}
5037
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005038/*
5039 *
5040 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005041 * Class TXN
5042 *
5043 *
5044 */
5045
5046/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005047 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005048 */
5049__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5050{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005051 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005052}
5053
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005054__LJMP static int hlua_set_var(lua_State *L)
5055{
5056 struct hlua_txn *htxn;
5057 const char *name;
5058 size_t len;
5059 struct sample smp;
5060
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005061 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5062 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005063
5064 /* It is useles to retrieve the stream, but this function
5065 * runs only in a stream context.
5066 */
5067 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5068 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5069
5070 /* Converts the third argument in a sample. */
5071 hlua_lua2smp(L, 3, &smp);
5072
5073 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005074 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005075
5076 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5077 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5078 else
5079 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5080
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005081 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005082}
5083
Christopher Faulet85d79c92016-11-09 16:54:56 +01005084__LJMP static int hlua_unset_var(lua_State *L)
5085{
5086 struct hlua_txn *htxn;
5087 const char *name;
5088 size_t len;
5089 struct sample smp;
5090
5091 MAY_LJMP(check_args(L, 2, "unset_var"));
5092
5093 /* It is useles to retrieve the stream, but this function
5094 * runs only in a stream context.
5095 */
5096 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5097 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5098
5099 /* Unset the variable. */
5100 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005101 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5102 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005103}
5104
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005105__LJMP static int hlua_get_var(lua_State *L)
5106{
5107 struct hlua_txn *htxn;
5108 const char *name;
5109 size_t len;
5110 struct sample smp;
5111
5112 MAY_LJMP(check_args(L, 2, "get_var"));
5113
5114 /* It is useles to retrieve the stream, but this function
5115 * runs only in a stream context.
5116 */
5117 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5118 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5119
Willy Tarreau7560dd42016-03-10 16:28:58 +01005120 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005121 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005122 lua_pushnil(L);
5123 return 1;
5124 }
5125
5126 return hlua_smp2lua(L, &smp);
5127}
5128
Willy Tarreau59551662015-03-10 14:23:13 +01005129__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005130{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005131 struct hlua *hlua;
5132
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005133 MAY_LJMP(check_args(L, 2, "set_priv"));
5134
Willy Tarreau87b09662015-04-03 00:22:06 +02005135 /* It is useles to retrieve the stream, but this function
5136 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005137 */
5138 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005139 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005140
5141 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005142 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005143
5144 /* Get and store new value. */
5145 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5146 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5147
5148 return 0;
5149}
5150
Willy Tarreau59551662015-03-10 14:23:13 +01005151__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005152{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005153 struct hlua *hlua;
5154
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005155 MAY_LJMP(check_args(L, 1, "get_priv"));
5156
Willy Tarreau87b09662015-04-03 00:22:06 +02005157 /* It is useles to retrieve the stream, but this function
5158 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005159 */
5160 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005161 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005162
5163 /* Push configuration index in the stack. */
5164 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5165
5166 return 1;
5167}
5168
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005169/* Create stack entry containing a class TXN. This function
5170 * return 0 if the stack does not contains free slots,
5171 * otherwise it returns 1.
5172 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005173static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005174{
Willy Tarreaude491382015-04-06 11:04:28 +02005175 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005176
5177 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005178 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005179 return 0;
5180
5181 /* NOTE: The allocation never fails. The failure
5182 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005183 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005184 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005185 /* Create the object: obj[0] = userdata. */
5186 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005187 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005188 lua_rawseti(L, -2, 0);
5189
Willy Tarreaude491382015-04-06 11:04:28 +02005190 htxn->s = s;
5191 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005192 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005193 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005194
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005195 /* Create the "f" field that contains a list of fetches. */
5196 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005197 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005198 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005199 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005200
5201 /* Create the "sf" field that contains a list of stringsafe fetches. */
5202 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005203 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005204 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005205 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005206
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005207 /* Create the "c" field that contains a list of converters. */
5208 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005209 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005210 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005211 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005212
5213 /* Create the "sc" field that contains a list of stringsafe converters. */
5214 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005215 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005216 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005217 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005218
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005219 /* Create the "req" field that contains the request channel object. */
5220 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005221 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005222 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005223 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005224
5225 /* Create the "res" field that contains the response channel object. */
5226 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005227 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005228 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005229 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005230
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005231 /* Creates the HTTP object is the current proxy allows http. */
5232 lua_pushstring(L, "http");
5233 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005234 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005235 return 0;
5236 }
5237 else
5238 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005239 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005240
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005241 /* Pop a class sesison metatable and affect it to the userdata. */
5242 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5243 lua_setmetatable(L, -2);
5244
5245 return 1;
5246}
5247
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005248__LJMP static int hlua_txn_deflog(lua_State *L)
5249{
5250 const char *msg;
5251 struct hlua_txn *htxn;
5252
5253 MAY_LJMP(check_args(L, 2, "deflog"));
5254 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5255 msg = MAY_LJMP(luaL_checkstring(L, 2));
5256
5257 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5258 return 0;
5259}
5260
5261__LJMP static int hlua_txn_log(lua_State *L)
5262{
5263 int level;
5264 const char *msg;
5265 struct hlua_txn *htxn;
5266
5267 MAY_LJMP(check_args(L, 3, "log"));
5268 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5269 level = MAY_LJMP(luaL_checkinteger(L, 2));
5270 msg = MAY_LJMP(luaL_checkstring(L, 3));
5271
5272 if (level < 0 || level >= NB_LOG_LEVELS)
5273 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5274
5275 hlua_sendlog(htxn->s->be, level, msg);
5276 return 0;
5277}
5278
5279__LJMP static int hlua_txn_log_debug(lua_State *L)
5280{
5281 const char *msg;
5282 struct hlua_txn *htxn;
5283
5284 MAY_LJMP(check_args(L, 2, "Debug"));
5285 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5286 msg = MAY_LJMP(luaL_checkstring(L, 2));
5287 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5288 return 0;
5289}
5290
5291__LJMP static int hlua_txn_log_info(lua_State *L)
5292{
5293 const char *msg;
5294 struct hlua_txn *htxn;
5295
5296 MAY_LJMP(check_args(L, 2, "Info"));
5297 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5298 msg = MAY_LJMP(luaL_checkstring(L, 2));
5299 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5300 return 0;
5301}
5302
5303__LJMP static int hlua_txn_log_warning(lua_State *L)
5304{
5305 const char *msg;
5306 struct hlua_txn *htxn;
5307
5308 MAY_LJMP(check_args(L, 2, "Warning"));
5309 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5310 msg = MAY_LJMP(luaL_checkstring(L, 2));
5311 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5312 return 0;
5313}
5314
5315__LJMP static int hlua_txn_log_alert(lua_State *L)
5316{
5317 const char *msg;
5318 struct hlua_txn *htxn;
5319
5320 MAY_LJMP(check_args(L, 2, "Alert"));
5321 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5322 msg = MAY_LJMP(luaL_checkstring(L, 2));
5323 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5324 return 0;
5325}
5326
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005327__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5328{
5329 struct hlua_txn *htxn;
5330 int ll;
5331
5332 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5333 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5334 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5335
5336 if (ll < 0 || ll > 7)
5337 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5338
5339 htxn->s->logs.level = ll;
5340 return 0;
5341}
5342
5343__LJMP static int hlua_txn_set_tos(lua_State *L)
5344{
5345 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005346 int tos;
5347
5348 MAY_LJMP(check_args(L, 2, "set_tos"));
5349 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5350 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5351
Willy Tarreau1a18b542018-12-11 16:37:42 +01005352 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005353 return 0;
5354}
5355
5356__LJMP static int hlua_txn_set_mark(lua_State *L)
5357{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005358 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005359 int mark;
5360
5361 MAY_LJMP(check_args(L, 2, "set_mark"));
5362 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5363 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5364
Lukas Tribus579e3e32019-08-11 18:03:45 +02005365 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005366 return 0;
5367}
5368
Patrick Hemmer268a7072018-05-11 12:52:31 -04005369__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5370{
5371 struct hlua_txn *htxn;
5372
5373 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5374 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5375 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5376 return 0;
5377}
5378
5379__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5380{
5381 struct hlua_txn *htxn;
5382
5383 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5384 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5385 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5386 return 0;
5387}
5388
Christopher Faulet700d9e82020-01-31 12:21:52 +01005389/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005390 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005391 * message and terminate the transaction. It returns 1 on success and 0 on
5392 * error. The Reply must be on top of the stack.
5393 */
5394__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5395{
5396 struct htx *htx;
5397 struct htx_sl *sl;
5398 struct h1m h1m;
5399 const char *status, *reason, *body;
5400 size_t status_len, reason_len, body_len;
5401 int ret, code, flags;
5402
5403 code = 200;
5404 status = "200";
5405 status_len = 3;
5406 ret = lua_getfield(L, -1, "status");
5407 if (ret == LUA_TNUMBER) {
5408 code = lua_tointeger(L, -1);
5409 status = lua_tolstring(L, -1, &status_len);
5410 }
5411 lua_pop(L, 1);
5412
5413 reason = http_get_reason(code);
5414 reason_len = strlen(reason);
5415 ret = lua_getfield(L, -1, "reason");
5416 if (ret == LUA_TSTRING)
5417 reason = lua_tolstring(L, -1, &reason_len);
5418 lua_pop(L, 1);
5419
5420 body = NULL;
5421 body_len = 0;
5422 ret = lua_getfield(L, -1, "body");
5423 if (ret == LUA_TSTRING)
5424 body = lua_tolstring(L, -1, &body_len);
5425 lua_pop(L, 1);
5426
5427 /* Prepare the response before inserting the headers */
5428 h1m_init_res(&h1m);
5429 htx = htx_from_buf(&s->res.buf);
5430 channel_htx_truncate(&s->res, htx);
5431 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5432 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5433 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5434 ist2(status, status_len), ist2(reason, reason_len));
5435 }
5436 else {
5437 flags = HTX_SL_F_IS_RESP;
5438 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5439 ist2(status, status_len), ist2(reason, reason_len));
5440 }
5441 if (!sl)
5442 goto fail;
5443 sl->info.res.status = code;
5444
5445 /* Push in the stack the "headers" entry. */
5446 ret = lua_getfield(L, -1, "headers");
5447 if (ret != LUA_TTABLE)
5448 goto skip_headers;
5449
5450 lua_pushnil(L);
5451 while (lua_next(L, -2) != 0) {
5452 struct ist name, value;
5453 const char *n, *v;
5454 size_t nlen, vlen;
5455
5456 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5457 /* Skip element if the key is not a string or if the value is not a table */
5458 goto next_hdr;
5459 }
5460
5461 n = lua_tolstring(L, -2, &nlen);
5462 name = ist2(n, nlen);
5463 if (isteqi(name, ist("content-length"))) {
5464 /* Always skip content-length header. It will be added
5465 * later with the correct len
5466 */
5467 goto next_hdr;
5468 }
5469
5470 /* Loop on header's values */
5471 lua_pushnil(L);
5472 while (lua_next(L, -2)) {
5473 if (!lua_isstring(L, -1)) {
5474 /* Skip the value if it is not a string */
5475 goto next_value;
5476 }
5477
5478 v = lua_tolstring(L, -1, &vlen);
5479 value = ist2(v, vlen);
5480
5481 if (isteqi(name, ist("transfer-encoding")))
5482 h1_parse_xfer_enc_header(&h1m, value);
5483 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5484 goto fail;
5485
5486 next_value:
5487 lua_pop(L, 1);
5488 }
5489
5490 next_hdr:
5491 lua_pop(L, 1);
5492 }
5493 skip_headers:
5494 lua_pop(L, 1);
5495
5496 /* Update h1m flags: CLEN is set if CHNK is not present */
5497 if (!(h1m.flags & H1_MF_CHNK)) {
5498 const char *clen = ultoa(body_len);
5499
5500 h1m.flags |= H1_MF_CLEN;
5501 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5502 goto fail;
5503 }
5504 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5505 h1m.flags |= H1_MF_XFER_LEN;
5506
5507 /* Update HTX start-line flags */
5508 if (h1m.flags & H1_MF_XFER_ENC)
5509 flags |= HTX_SL_F_XFER_ENC;
5510 if (h1m.flags & H1_MF_XFER_LEN) {
5511 flags |= HTX_SL_F_XFER_LEN;
5512 if (h1m.flags & H1_MF_CHNK)
5513 flags |= HTX_SL_F_CHNK;
5514 else if (h1m.flags & H1_MF_CLEN)
5515 flags |= HTX_SL_F_CLEN;
5516 if (h1m.body_len == 0)
5517 flags |= HTX_SL_F_BODYLESS;
5518 }
5519 sl->flags |= flags;
5520
5521
5522 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5523 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5524 !htx_add_endof(htx, HTX_BLK_EOM))
5525 goto fail;
5526
5527 /* Now, forward the response and terminate the transaction */
5528 s->txn->status = code;
5529 htx_to_buf(htx, &s->res.buf);
5530 if (!http_forward_proxy_resp(s, 1))
5531 goto fail;
5532
5533 return 1;
5534
5535 fail:
5536 channel_htx_truncate(&s->res, htx);
5537 return 0;
5538}
5539
5540/* Terminate a transaction if called from a lua action. For TCP streams,
5541 * processing is just aborted. Nothing is returned to the client and all
5542 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5543 * is forwarded to the client before terminating the transaction. On success,
5544 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5545 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5546 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005547 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005548__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005549{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005550 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005551 struct stream *s;
5552 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005553
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005554 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005555
Christopher Faulet700d9e82020-01-31 12:21:52 +01005556 /* If the flags NOTERM is set, we cannot terminate the session, so we
5557 * just end the execution of the current lua code. */
5558 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005559 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005560
Christopher Faulet700d9e82020-01-31 12:21:52 +01005561 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005562 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005563 struct channel *req = &s->req;
5564 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005565
Christopher Faulet700d9e82020-01-31 12:21:52 +01005566 channel_auto_read(req);
5567 channel_abort(req);
5568 channel_auto_close(req);
5569 channel_erase(req);
5570
5571 res->wex = tick_add_ifset(now_ms, res->wto);
5572 channel_auto_read(res);
5573 channel_auto_close(res);
5574 channel_shutr_now(res);
5575
5576 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5577 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005578 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005579
Christopher Faulet700d9e82020-01-31 12:21:52 +01005580 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5581 /* No reply or invalid reply */
5582 s->txn->status = 0;
5583 http_reply_and_close(s, 0, NULL);
5584 }
5585 else {
5586 /* Remove extra args to have the reply on top of the stack */
5587 if (lua_gettop(L) > 2)
5588 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005589
Christopher Faulet700d9e82020-01-31 12:21:52 +01005590 if (!hlua_txn_forward_reply(L, s)) {
5591 if (!(s->flags & SF_ERR_MASK))
5592 s->flags |= SF_ERR_PRXCOND;
5593 lua_pushinteger(L, ACT_RET_ERR);
5594 WILL_LJMP(hlua_done(L));
5595 return 0; /* Never reached */
5596 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005597 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005598
Christopher Faulet700d9e82020-01-31 12:21:52 +01005599 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5600 if (htxn->dir == SMP_OPT_DIR_REQ) {
5601 /* let's log the request time */
5602 s->logs.tv_request = now;
5603 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5604 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5605 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005606
Christopher Faulet700d9e82020-01-31 12:21:52 +01005607 done:
5608 if (!(s->flags & SF_ERR_MASK))
5609 s->flags |= SF_ERR_LOCAL;
5610 if (!(s->flags & SF_FINST_MASK))
5611 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005612
Christopher Faulet4ad73102020-03-05 11:07:31 +01005613 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005614 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005615 return 0;
5616}
5617
Christopher Faulet700d9e82020-01-31 12:21:52 +01005618/*
5619 *
5620 *
5621 * Class REPLY
5622 *
5623 *
5624 */
5625
5626/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5627 * free slots, the function fails and returns 0;
5628 */
5629static int hlua_txn_reply_new(lua_State *L)
5630{
5631 struct hlua_txn *htxn;
5632 const char *reason, *body = NULL;
5633 int ret, status;
5634
5635 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005636 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005637 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5638 WILL_LJMP(lua_error(L));
5639 }
5640
5641 /* Default value */
5642 status = 200;
5643 reason = http_get_reason(status);
5644
5645 if (lua_istable(L, 2)) {
5646 /* load status and reason from the table argument at index 2 */
5647 ret = lua_getfield(L, 2, "status");
5648 if (ret == LUA_TNIL)
5649 goto reason;
5650 else if (ret != LUA_TNUMBER) {
5651 /* invalid status: ignore the reason */
5652 goto body;
5653 }
5654 status = lua_tointeger(L, -1);
5655
5656 reason:
5657 lua_pop(L, 1); /* restore the stack: remove status */
5658 ret = lua_getfield(L, 2, "reason");
5659 if (ret == LUA_TSTRING)
5660 reason = lua_tostring(L, -1);
5661
5662 body:
5663 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5664 ret = lua_getfield(L, 2, "body");
5665 if (ret == LUA_TSTRING)
5666 body = lua_tostring(L, -1);
5667 lua_pop(L, 1); /* restore the stack: remove body */
5668 }
5669
5670 /* Create the Reply table */
5671 lua_newtable(L);
5672
5673 /* Add status element */
5674 lua_pushstring(L, "status");
5675 lua_pushinteger(L, status);
5676 lua_settable(L, -3);
5677
5678 /* Add reason element */
5679 reason = http_get_reason(status);
5680 lua_pushstring(L, "reason");
5681 lua_pushstring(L, reason);
5682 lua_settable(L, -3);
5683
5684 /* Add body element, nil if undefined */
5685 lua_pushstring(L, "body");
5686 if (body)
5687 lua_pushstring(L, body);
5688 else
5689 lua_pushnil(L);
5690 lua_settable(L, -3);
5691
5692 /* Add headers element */
5693 lua_pushstring(L, "headers");
5694 lua_newtable(L);
5695
5696 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5697 if (lua_istable(L, 2)) {
5698 /* load headers from the table argument at index 2. If it is a table, copy it. */
5699 ret = lua_getfield(L, 2, "headers");
5700 if (ret == LUA_TTABLE) {
5701 /* stack: [ ... <headers:table>, <table> ] */
5702 lua_pushnil(L);
5703 while (lua_next(L, -2) != 0) {
5704 /* stack: [ ... <headers:table>, <table>, k, v] */
5705 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5706 /* invalid value type, skip it */
5707 lua_pop(L, 1);
5708 continue;
5709 }
5710
5711
5712 /* Duplicate the key and swap it with the value. */
5713 lua_pushvalue(L, -2);
5714 lua_insert(L, -2);
5715 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5716
5717 lua_newtable(L);
5718 lua_insert(L, -2);
5719 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5720
5721 if (lua_isstring(L, -1)) {
5722 /* push the value in the inner table */
5723 lua_rawseti(L, -2, 1);
5724 }
5725 else { /* table */
5726 lua_pushnil(L);
5727 while (lua_next(L, -2) != 0) {
5728 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5729 if (!lua_isstring(L, -1)) {
5730 /* invalid value type, skip it*/
5731 lua_pop(L, 1);
5732 continue;
5733 }
5734 /* push the value in the inner table */
5735 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5736 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5737 }
5738 lua_pop(L, 1);
5739 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5740 }
5741
5742 /* push (k,v) on the stack in the headers table:
5743 * stack: [ ... <headers:table>, <table>, k, k, v ]
5744 */
5745 lua_settable(L, -5);
5746 /* stack: [ ... <headers:table>, <table>, k ] */
5747 }
5748 }
5749 lua_pop(L, 1);
5750 }
5751 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5752 lua_settable(L, -3);
5753 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5754
5755 /* Pop a class sesison metatable and affect it to the userdata. */
5756 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5757 lua_setmetatable(L, -2);
5758 return 1;
5759}
5760
5761/* Set the reply status code, and optionally the reason. If no reason is
5762 * provided, the default one corresponding to the status code is used.
5763 */
5764__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5765{
5766 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5767 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5768
5769 /* First argument (self) must be a table */
5770 luaL_checktype(L, 1, LUA_TTABLE);
5771
5772 if (status < 100 || status > 599) {
5773 lua_pushboolean(L, 0);
5774 return 1;
5775 }
5776 if (!reason)
5777 reason = http_get_reason(status);
5778
5779 lua_pushinteger(L, status);
5780 lua_setfield(L, 1, "status");
5781
5782 lua_pushstring(L, reason);
5783 lua_setfield(L, 1, "reason");
5784
5785 lua_pushboolean(L, 1);
5786 return 1;
5787}
5788
5789/* Add a header into the reply object. Each header name is associated to an
5790 * array of values in the "headers" table. If the header name is not found, a
5791 * new entry is created.
5792 */
5793__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5794{
5795 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5796 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5797 int ret;
5798
5799 /* First argument (self) must be a table */
5800 luaL_checktype(L, 1, LUA_TTABLE);
5801
5802 /* Push in the stack the "headers" entry. */
5803 ret = lua_getfield(L, 1, "headers");
5804 if (ret != LUA_TTABLE) {
5805 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5806 WILL_LJMP(lua_error(L));
5807 }
5808
5809 /* check if the header is already registered. If not, register it. */
5810 ret = lua_getfield(L, -1, name);
5811 if (ret == LUA_TNIL) {
5812 /* Entry not found. */
5813 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5814
5815 /* Insert the new header name in the array in the top of the stack.
5816 * It left the new array in the top of the stack.
5817 */
5818 lua_newtable(L);
5819 lua_pushstring(L, name);
5820 lua_pushvalue(L, -2);
5821 lua_settable(L, -4);
5822 }
5823 else if (ret != LUA_TTABLE) {
5824 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5825 WILL_LJMP(lua_error(L));
5826 }
5827
5828 /* Now the top od thestack is an array of values. We push
5829 * the header value as new entry.
5830 */
5831 lua_pushstring(L, value);
5832 ret = lua_rawlen(L, -2);
5833 lua_rawseti(L, -2, ret + 1);
5834
5835 lua_pushboolean(L, 1);
5836 return 1;
5837}
5838
5839/* Remove all occurrences of a given header name. */
5840__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5841{
5842 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5843 int ret;
5844
5845 /* First argument (self) must be a table */
5846 luaL_checktype(L, 1, LUA_TTABLE);
5847
5848 /* Push in the stack the "headers" entry. */
5849 ret = lua_getfield(L, 1, "headers");
5850 if (ret != LUA_TTABLE) {
5851 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5852 WILL_LJMP(lua_error(L));
5853 }
5854
5855 lua_pushstring(L, name);
5856 lua_pushnil(L);
5857 lua_settable(L, -3);
5858
5859 lua_pushboolean(L, 1);
5860 return 1;
5861}
5862
5863/* Set the reply's body. Overwrite any existing entry. */
5864__LJMP static int hlua_txn_reply_set_body(lua_State *L)
5865{
5866 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
5867
5868 /* First argument (self) must be a table */
5869 luaL_checktype(L, 1, LUA_TTABLE);
5870
5871 lua_pushstring(L, payload);
5872 lua_setfield(L, 1, "body");
5873
5874 lua_pushboolean(L, 1);
5875 return 1;
5876}
5877
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005878__LJMP static int hlua_log(lua_State *L)
5879{
5880 int level;
5881 const char *msg;
5882
5883 MAY_LJMP(check_args(L, 2, "log"));
5884 level = MAY_LJMP(luaL_checkinteger(L, 1));
5885 msg = MAY_LJMP(luaL_checkstring(L, 2));
5886
5887 if (level < 0 || level >= NB_LOG_LEVELS)
5888 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5889
5890 hlua_sendlog(NULL, level, msg);
5891 return 0;
5892}
5893
5894__LJMP static int hlua_log_debug(lua_State *L)
5895{
5896 const char *msg;
5897
5898 MAY_LJMP(check_args(L, 1, "debug"));
5899 msg = MAY_LJMP(luaL_checkstring(L, 1));
5900 hlua_sendlog(NULL, LOG_DEBUG, msg);
5901 return 0;
5902}
5903
5904__LJMP static int hlua_log_info(lua_State *L)
5905{
5906 const char *msg;
5907
5908 MAY_LJMP(check_args(L, 1, "info"));
5909 msg = MAY_LJMP(luaL_checkstring(L, 1));
5910 hlua_sendlog(NULL, LOG_INFO, msg);
5911 return 0;
5912}
5913
5914__LJMP static int hlua_log_warning(lua_State *L)
5915{
5916 const char *msg;
5917
5918 MAY_LJMP(check_args(L, 1, "warning"));
5919 msg = MAY_LJMP(luaL_checkstring(L, 1));
5920 hlua_sendlog(NULL, LOG_WARNING, msg);
5921 return 0;
5922}
5923
5924__LJMP static int hlua_log_alert(lua_State *L)
5925{
5926 const char *msg;
5927
5928 MAY_LJMP(check_args(L, 1, "alert"));
5929 msg = MAY_LJMP(luaL_checkstring(L, 1));
5930 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005931 return 0;
5932}
5933
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005934__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005935{
5936 int wakeup_ms = lua_tointeger(L, -1);
5937 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005938 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005939 return 0;
5940}
5941
5942__LJMP static int hlua_sleep(lua_State *L)
5943{
5944 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005945 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005946
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005947 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005948
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005949 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005950 wakeup_ms = tick_add(now_ms, delay);
5951 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005952
Willy Tarreau9635e032018-10-16 17:52:55 +02005953 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005954 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005955}
5956
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005957__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005958{
5959 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005960 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005961
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005962 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005963
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005964 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005965 wakeup_ms = tick_add(now_ms, delay);
5966 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005967
Willy Tarreau9635e032018-10-16 17:52:55 +02005968 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005969 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005970}
5971
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005972/* This functionis an LUA binding. it permits to give back
5973 * the hand at the HAProxy scheduler. It is used when the
5974 * LUA processing consumes a lot of time.
5975 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005976__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005977{
5978 return 0;
5979}
5980
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005981__LJMP static int hlua_yield(lua_State *L)
5982{
Willy Tarreau9635e032018-10-16 17:52:55 +02005983 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005984 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005985}
5986
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005987/* This function change the nice of the currently executed
5988 * task. It is used set low or high priority at the current
5989 * task.
5990 */
Willy Tarreau59551662015-03-10 14:23:13 +01005991__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005992{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005993 struct hlua *hlua;
5994 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005995
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005996 MAY_LJMP(check_args(L, 1, "set_nice"));
5997 hlua = hlua_gethlua(L);
5998 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005999
6000 /* If he task is not set, I'm in a start mode. */
6001 if (!hlua || !hlua->task)
6002 return 0;
6003
6004 if (nice < -1024)
6005 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006006 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006007 nice = 1024;
6008
6009 hlua->task->nice = nice;
6010 return 0;
6011}
6012
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006013/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006014 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6015 * return an E_AGAIN signal, the emmiter of this signal must set a
6016 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006017 *
6018 * Task wrapper are longjmp safe because the only one Lua code
6019 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006020 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006021struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006022{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006023 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006024 enum hlua_exec status;
6025
Christopher Faulet5bc99722018-04-25 10:34:45 +02006026 if (task->thread_mask == MAX_THREADS_MASK)
6027 task_set_affinity(task, tid_bit);
6028
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006029 /* If it is the first call to the task, we must initialize the
6030 * execution timeouts.
6031 */
6032 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006033 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006034
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006035 /* Execute the Lua code. */
6036 status = hlua_ctx_resume(hlua, 1);
6037
6038 switch (status) {
6039 /* finished or yield */
6040 case HLUA_E_OK:
6041 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006042 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006043 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006044 break;
6045
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006046 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006047 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006048 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006049 break;
6050
6051 /* finished with error. */
6052 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006053 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006054 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006055 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006056 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006057 break;
6058
6059 case HLUA_E_ERR:
6060 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006061 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006062 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006063 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006064 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006065 break;
6066 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006067 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006068}
6069
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006070/* This function is an LUA binding that register LUA function to be
6071 * executed after the HAProxy configuration parsing and before the
6072 * HAProxy scheduler starts. This function expect only one LUA
6073 * argument that is a function. This function returns nothing, but
6074 * throws if an error is encountered.
6075 */
6076__LJMP static int hlua_register_init(lua_State *L)
6077{
6078 struct hlua_init_function *init;
6079 int ref;
6080
6081 MAY_LJMP(check_args(L, 1, "register_init"));
6082
6083 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6084
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006085 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006086 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006087 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006088
6089 init->function_ref = ref;
6090 LIST_ADDQ(&hlua_init_functions, &init->l);
6091 return 0;
6092}
6093
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006094/* This functio is an LUA binding. It permits to register a task
6095 * executed in parallel of the main HAroxy activity. The task is
6096 * created and it is set in the HAProxy scheduler. It can be called
6097 * from the "init" section, "post init" or during the runtime.
6098 *
6099 * Lua prototype:
6100 *
6101 * <none> core.register_task(<function>)
6102 */
6103static int hlua_register_task(lua_State *L)
6104{
6105 struct hlua *hlua;
6106 struct task *task;
6107 int ref;
6108
6109 MAY_LJMP(check_args(L, 1, "register_task"));
6110
6111 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6112
Willy Tarreaubafbe012017-11-24 17:34:44 +01006113 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006114 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006115 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006116
Emeric Brunc60def82017-09-27 14:59:38 +02006117 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006118 if (!task)
6119 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6120
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006121 task->context = hlua;
6122 task->process = hlua_process_task;
6123
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006124 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006125 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006126
6127 /* Restore the function in the stack. */
6128 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6129 hlua->nargs = 0;
6130
6131 /* Schedule task. */
6132 task_schedule(task, now_ms);
6133
6134 return 0;
6135}
6136
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006137/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6138 * doesn't allow "yield" functions because the HAProxy engine cannot
6139 * resume converters.
6140 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006141static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006142{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006143 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006144 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006145 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006146
Willy Tarreaube508f12016-03-10 11:47:01 +01006147 if (!stream)
6148 return 0;
6149
Willy Tarreau87b09662015-04-03 00:22:06 +02006150 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006151 * Lua context can be not initialized. This behavior
6152 * permits to save performances because a systematic
6153 * Lua initialization cause 5% performances loss.
6154 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006155 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006156 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006157 if (!stream->hlua) {
6158 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6159 return 0;
6160 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006161 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006162 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6163 return 0;
6164 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006165 }
6166
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006167 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006168 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006169
6170 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006171 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6172 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6173 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006174 else
6175 error = "critical error";
6176 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006177 return 0;
6178 }
6179
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006180 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006181 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006182 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006183 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006184 return 0;
6185 }
6186
6187 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006188 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006189
6190 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006191 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006192 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006193 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006194 return 0;
6195 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006196 hlua_smp2lua(stream->hlua->T, smp);
6197 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006198
6199 /* push keywords in the stack. */
6200 if (arg_p) {
6201 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006202 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006203 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006204 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006205 return 0;
6206 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006207 hlua_arg2lua(stream->hlua->T, arg_p);
6208 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006209 }
6210 }
6211
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006212 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006213 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006214
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006215 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006216 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006217 }
6218
6219 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006220 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006221 /* finished. */
6222 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006223 /* If the stack is empty, the function fails. */
6224 if (lua_gettop(stream->hlua->T) <= 0)
6225 return 0;
6226
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006227 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006228 hlua_lua2smp(stream->hlua->T, -1, smp);
6229 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006230 return 1;
6231
6232 /* yield. */
6233 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006234 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006235 return 0;
6236
6237 /* finished with error. */
6238 case HLUA_E_ERRMSG:
6239 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006240 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006241 fcn->name, lua_tostring(stream->hlua->T, -1));
6242 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006243 return 0;
6244
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006245 case HLUA_E_ETMOUT:
6246 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6247 return 0;
6248
6249 case HLUA_E_NOMEM:
6250 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6251 return 0;
6252
6253 case HLUA_E_YIELD:
6254 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6255 return 0;
6256
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006257 case HLUA_E_ERR:
6258 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006259 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006260 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006261
6262 default:
6263 return 0;
6264 }
6265}
6266
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006267/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6268 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006269 * resume sample-fetches. This function will be called by the sample
6270 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006271 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006272static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6273 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006274{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006275 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006276 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006277 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006278 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006279
Willy Tarreaube508f12016-03-10 11:47:01 +01006280 if (!stream)
6281 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006282
Willy Tarreau87b09662015-04-03 00:22:06 +02006283 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006284 * Lua context can be not initialized. This behavior
6285 * permits to save performances because a systematic
6286 * Lua initialization cause 5% performances loss.
6287 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006288 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006289 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006290 if (!stream->hlua) {
6291 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6292 return 0;
6293 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006294 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006295 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6296 return 0;
6297 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006298 }
6299
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006300 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006301 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006302
6303 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006304 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6305 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6306 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006307 else
6308 error = "critical error";
6309 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006310 return 0;
6311 }
6312
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006313 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006314 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006315 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006316 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006317 return 0;
6318 }
6319
6320 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006321 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006322
6323 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006324 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006325 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006326 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006327 return 0;
6328 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006329 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006330
6331 /* push keywords in the stack. */
6332 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6333 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006334 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006335 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006336 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006337 return 0;
6338 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006339 hlua_arg2lua(stream->hlua->T, arg_p);
6340 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006341 }
6342
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006343 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006344 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006345
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006346 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006347 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006348 }
6349
6350 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006351 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006352 /* finished. */
6353 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006354 /* If the stack is empty, the function fails. */
6355 if (lua_gettop(stream->hlua->T) <= 0)
6356 return 0;
6357
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006358 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006359 hlua_lua2smp(stream->hlua->T, -1, smp);
6360 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006361
6362 /* Set the end of execution flag. */
6363 smp->flags &= ~SMP_F_MAY_CHANGE;
6364 return 1;
6365
6366 /* yield. */
6367 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006368 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006369 return 0;
6370
6371 /* finished with error. */
6372 case HLUA_E_ERRMSG:
6373 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006374 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006375 fcn->name, lua_tostring(stream->hlua->T, -1));
6376 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006377 return 0;
6378
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006379 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006380 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6381 return 0;
6382
6383 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006384 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6385 return 0;
6386
6387 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006388 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6389 return 0;
6390
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006391 case HLUA_E_ERR:
6392 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006393 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006394 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006395
6396 default:
6397 return 0;
6398 }
6399}
6400
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006401/* This function is an LUA binding used for registering
6402 * "sample-conv" functions. It expects a converter name used
6403 * in the haproxy configuration file, and an LUA function.
6404 */
6405__LJMP static int hlua_register_converters(lua_State *L)
6406{
6407 struct sample_conv_kw_list *sck;
6408 const char *name;
6409 int ref;
6410 int len;
6411 struct hlua_function *fcn;
6412
6413 MAY_LJMP(check_args(L, 2, "register_converters"));
6414
6415 /* First argument : converter name. */
6416 name = MAY_LJMP(luaL_checkstring(L, 1));
6417
6418 /* Second argument : lua function. */
6419 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6420
6421 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006422 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006423 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006424 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006425 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006426 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006427 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006428
6429 /* Fill fcn. */
6430 fcn->name = strdup(name);
6431 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006432 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006433 fcn->function_ref = ref;
6434
6435 /* List head */
6436 sck->list.n = sck->list.p = NULL;
6437
6438 /* converter keyword. */
6439 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006440 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006441 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006442 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006443
6444 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6445 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006446 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 +01006447 sck->kw[0].val_args = NULL;
6448 sck->kw[0].in_type = SMP_T_STR;
6449 sck->kw[0].out_type = SMP_T_STR;
6450 sck->kw[0].private = fcn;
6451
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006452 /* Register this new converter */
6453 sample_register_convs(sck);
6454
6455 return 0;
6456}
6457
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006458/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006459 * "sample-fetch" functions. It expects a converter name used
6460 * in the haproxy configuration file, and an LUA function.
6461 */
6462__LJMP static int hlua_register_fetches(lua_State *L)
6463{
6464 const char *name;
6465 int ref;
6466 int len;
6467 struct sample_fetch_kw_list *sfk;
6468 struct hlua_function *fcn;
6469
6470 MAY_LJMP(check_args(L, 2, "register_fetches"));
6471
6472 /* First argument : sample-fetch name. */
6473 name = MAY_LJMP(luaL_checkstring(L, 1));
6474
6475 /* Second argument : lua function. */
6476 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6477
6478 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006479 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006480 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006481 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006482 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006483 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006484 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006485
6486 /* Fill fcn. */
6487 fcn->name = strdup(name);
6488 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006489 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006490 fcn->function_ref = ref;
6491
6492 /* List head */
6493 sfk->list.n = sfk->list.p = NULL;
6494
6495 /* sample-fetch keyword. */
6496 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006497 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006498 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006499 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006500
6501 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6502 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006503 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 +01006504 sfk->kw[0].val_args = NULL;
6505 sfk->kw[0].out_type = SMP_T_STR;
6506 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6507 sfk->kw[0].val = 0;
6508 sfk->kw[0].private = fcn;
6509
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006510 /* Register this new fetch. */
6511 sample_register_fetches(sfk);
6512
6513 return 0;
6514}
6515
Christopher Faulet501465d2020-02-26 14:54:16 +01006516/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006517 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006518__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006519{
6520 struct hlua *hlua = hlua_gethlua(L);
6521 unsigned int delay;
6522 unsigned int wakeup_ms;
6523
6524 MAY_LJMP(check_args(L, 1, "wake_time"));
6525
6526 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6527 wakeup_ms = tick_add(now_ms, delay);
6528 hlua->wake_time = wakeup_ms;
6529 return 0;
6530}
6531
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006532/* This function is a wrapper to execute each LUA function declared as an action
6533 * wrapper during the initialisation period. This function may return any
6534 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6535 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6536 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006537 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006538static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006539 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006540{
6541 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006542 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006543 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006544 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006545
6546 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006547 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6548 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6549 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6550 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006551 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006552 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006553 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006554 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006555
Willy Tarreau87b09662015-04-03 00:22:06 +02006556 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006557 * Lua context can be not initialized. This behavior
6558 * permits to save performances because a systematic
6559 * Lua initialization cause 5% performances loss.
6560 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006561 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006562 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006563 if (!s->hlua) {
6564 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6565 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006566 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006567 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006568 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006569 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6570 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006571 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006572 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006573 }
6574
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006575 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006576 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006577
6578 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006579 if (!SET_SAFE_LJMP(s->hlua->T)) {
6580 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6581 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006582 else
6583 error = "critical error";
6584 SEND_ERR(px, "Lua function '%s': %s.\n",
6585 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006586 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006587 }
6588
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006589 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006590 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006591 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006592 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006593 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006594 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006595 }
6596
6597 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006598 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006599
Willy Tarreau87b09662015-04-03 00:22:06 +02006600 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006601 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006602 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006603 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006604 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006605 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006606 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006607 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006608
6609 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006610 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006611 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006612 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006613 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006614 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006615 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006616 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006617 lua_pushstring(s->hlua->T, *arg);
6618 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006619 }
6620
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006621 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006622 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006623
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006624 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006625 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006626 }
6627
6628 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006629 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006630 /* finished. */
6631 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006632 /* Catch the return value */
6633 if (lua_gettop(s->hlua->T) > 0)
6634 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006635
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006636 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006637 if (act_ret == ACT_RET_YIELD) {
6638 if (flags & ACT_OPT_FINAL)
6639 goto err_yield;
6640
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006641 if (dir == SMP_OPT_DIR_REQ)
6642 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6643 s->hlua->wake_time);
6644 else
6645 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6646 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006647 }
6648 goto end;
6649
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006650 /* yield. */
6651 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006652 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006653 if (dir == SMP_OPT_DIR_REQ)
6654 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6655 s->hlua->wake_time);
6656 else
6657 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6658 s->hlua->wake_time);
6659
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006660 /* Some actions can be wake up when a "write" event
6661 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006662 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006663 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006664 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006665 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006666 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006667 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006668 act_ret = ACT_RET_YIELD;
6669 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006670
6671 /* finished with error. */
6672 case HLUA_E_ERRMSG:
6673 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006674 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006675 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6676 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006677 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006678
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006679 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006680 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006681 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006682
6683 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006684 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006685 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006686
6687 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02006688 err_yield:
6689 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006690 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6691 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006692 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006693
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006694 case HLUA_E_ERR:
6695 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006696 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006697 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006698
6699 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006700 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006701 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006702
6703 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02006704 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006705 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006706 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006707}
6708
Olivier Houchard9f6af332018-05-25 14:04:04 +02006709struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006710{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006711 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006712
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006713 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006714 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006715 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006716}
6717
6718static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6719{
6720 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006721 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006722 struct task *task;
6723 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006724 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006725
Willy Tarreaubafbe012017-11-24 17:34:44 +01006726 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006727 if (!hlua) {
6728 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6729 ctx->rule->arg.hlua_rule->fcn.name);
6730 return 0;
6731 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006732 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006733 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006734 ctx->ctx.hlua_apptcp.flags = 0;
6735
6736 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006737 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006738 if (!task) {
6739 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6740 ctx->rule->arg.hlua_rule->fcn.name);
6741 return 0;
6742 }
6743 task->nice = 0;
6744 task->context = ctx;
6745 task->process = hlua_applet_wakeup;
6746 ctx->ctx.hlua_apptcp.task = task;
6747
6748 /* In the execution wrappers linked with a stream, the
6749 * Lua context can be not initialized. This behavior
6750 * permits to save performances because a systematic
6751 * Lua initialization cause 5% performances loss.
6752 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006753 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006754 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6755 ctx->rule->arg.hlua_rule->fcn.name);
6756 return 0;
6757 }
6758
6759 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006760 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006761
6762 /* The following Lua calls can fail. */
6763 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006764 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6765 error = lua_tostring(hlua->T, -1);
6766 else
6767 error = "critical error";
6768 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6769 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006770 return 0;
6771 }
6772
6773 /* Check stack available size. */
6774 if (!lua_checkstack(hlua->T, 1)) {
6775 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6776 ctx->rule->arg.hlua_rule->fcn.name);
6777 RESET_SAFE_LJMP(hlua->T);
6778 return 0;
6779 }
6780
6781 /* Restore the function in the stack. */
6782 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6783
6784 /* Create and and push object stream in the stack. */
6785 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6786 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6787 ctx->rule->arg.hlua_rule->fcn.name);
6788 RESET_SAFE_LJMP(hlua->T);
6789 return 0;
6790 }
6791 hlua->nargs = 1;
6792
6793 /* push keywords in the stack. */
6794 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6795 if (!lua_checkstack(hlua->T, 1)) {
6796 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6797 ctx->rule->arg.hlua_rule->fcn.name);
6798 RESET_SAFE_LJMP(hlua->T);
6799 return 0;
6800 }
6801 lua_pushstring(hlua->T, *arg);
6802 hlua->nargs++;
6803 }
6804
6805 RESET_SAFE_LJMP(hlua->T);
6806
6807 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006808 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006809 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006810
6811 return 1;
6812}
6813
Willy Tarreau60409db2019-08-21 14:14:50 +02006814void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006815{
6816 struct stream_interface *si = ctx->owner;
6817 struct stream *strm = si_strm(si);
6818 struct channel *res = si_ic(si);
6819 struct act_rule *rule = ctx->rule;
6820 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006821 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006822
6823 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006824 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6825 /* eat the whole request */
6826 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006827 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006828 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006829
6830 /* If the stream is disconnect or closed, ldo nothing. */
6831 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6832 return;
6833
6834 /* Execute the function. */
6835 switch (hlua_ctx_resume(hlua, 1)) {
6836 /* finished. */
6837 case HLUA_E_OK:
6838 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6839
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006840 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006841 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006842 res->flags |= CF_READ_NULL;
6843 si_shutr(si);
6844 return;
6845
6846 /* yield. */
6847 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006848 if (hlua->wake_time != TICK_ETERNITY)
6849 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006850 return;
6851
6852 /* finished with error. */
6853 case HLUA_E_ERRMSG:
6854 /* Display log. */
6855 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6856 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6857 lua_pop(hlua->T, 1);
6858 goto error;
6859
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006860 case HLUA_E_ETMOUT:
6861 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6862 rule->arg.hlua_rule->fcn.name);
6863 goto error;
6864
6865 case HLUA_E_NOMEM:
6866 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6867 rule->arg.hlua_rule->fcn.name);
6868 goto error;
6869
6870 case HLUA_E_YIELD: /* unexpected */
6871 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6872 rule->arg.hlua_rule->fcn.name);
6873 goto error;
6874
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006875 case HLUA_E_ERR:
6876 /* Display log. */
6877 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6878 rule->arg.hlua_rule->fcn.name);
6879 goto error;
6880
6881 default:
6882 goto error;
6883 }
6884
6885error:
6886
6887 /* For all other cases, just close the stream. */
6888 si_shutw(si);
6889 si_shutr(si);
6890 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6891}
6892
6893static void hlua_applet_tcp_release(struct appctx *ctx)
6894{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006895 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006896 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006897 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006898 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006899}
6900
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006901/* The function returns 1 if the initialisation is complete, 0 if
6902 * an errors occurs and -1 if more data are required for initializing
6903 * the applet.
6904 */
6905static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6906{
6907 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006908 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006909 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006910 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006911 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006912 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006913
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006914 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006915 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006916 if (!hlua) {
6917 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6918 ctx->rule->arg.hlua_rule->fcn.name);
6919 return 0;
6920 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006921 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006922 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006923 ctx->ctx.hlua_apphttp.left_bytes = -1;
6924 ctx->ctx.hlua_apphttp.flags = 0;
6925
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006926 if (txn->req.flags & HTTP_MSGF_VER_11)
6927 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6928
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006929 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006930 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006931 if (!task) {
6932 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6933 ctx->rule->arg.hlua_rule->fcn.name);
6934 return 0;
6935 }
6936 task->nice = 0;
6937 task->context = ctx;
6938 task->process = hlua_applet_wakeup;
6939 ctx->ctx.hlua_apphttp.task = task;
6940
6941 /* In the execution wrappers linked with a stream, the
6942 * Lua context can be not initialized. This behavior
6943 * permits to save performances because a systematic
6944 * Lua initialization cause 5% performances loss.
6945 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006946 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006947 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6948 ctx->rule->arg.hlua_rule->fcn.name);
6949 return 0;
6950 }
6951
6952 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006953 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006954
6955 /* The following Lua calls can fail. */
6956 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006957 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6958 error = lua_tostring(hlua->T, -1);
6959 else
6960 error = "critical error";
6961 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6962 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006963 return 0;
6964 }
6965
6966 /* Check stack available size. */
6967 if (!lua_checkstack(hlua->T, 1)) {
6968 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6969 ctx->rule->arg.hlua_rule->fcn.name);
6970 RESET_SAFE_LJMP(hlua->T);
6971 return 0;
6972 }
6973
6974 /* Restore the function in the stack. */
6975 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6976
6977 /* Create and and push object stream in the stack. */
6978 if (!hlua_applet_http_new(hlua->T, ctx)) {
6979 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6980 ctx->rule->arg.hlua_rule->fcn.name);
6981 RESET_SAFE_LJMP(hlua->T);
6982 return 0;
6983 }
6984 hlua->nargs = 1;
6985
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006986 /* push keywords in the stack. */
6987 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6988 if (!lua_checkstack(hlua->T, 1)) {
6989 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6990 ctx->rule->arg.hlua_rule->fcn.name);
6991 RESET_SAFE_LJMP(hlua->T);
6992 return 0;
6993 }
6994 lua_pushstring(hlua->T, *arg);
6995 hlua->nargs++;
6996 }
6997
6998 RESET_SAFE_LJMP(hlua->T);
6999
7000 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007001 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007002
7003 return 1;
7004}
7005
Willy Tarreau60409db2019-08-21 14:14:50 +02007006void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007007{
7008 struct stream_interface *si = ctx->owner;
7009 struct stream *strm = si_strm(si);
7010 struct channel *req = si_oc(si);
7011 struct channel *res = si_ic(si);
7012 struct act_rule *rule = ctx->rule;
7013 struct proxy *px = strm->be;
7014 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7015 struct htx *req_htx, *res_htx;
7016
7017 res_htx = htx_from_buf(&res->buf);
7018
7019 /* If the stream is disconnect or closed, ldo nothing. */
7020 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7021 goto out;
7022
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007023 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007024 if (!b_size(&res->buf)) {
7025 si_rx_room_blk(si);
7026 goto out;
7027 }
7028 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007029 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007030 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7031
7032 /* Set the currently running flag. */
7033 if (!HLUA_IS_RUNNING(hlua) &&
7034 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7035 struct htx_blk *blk;
7036 size_t count = co_data(req);
7037
7038 if (!count) {
7039 si_cant_get(si);
7040 goto out;
7041 }
7042
7043 /* We need to flush the request header. This left the body for
7044 * the Lua.
7045 */
7046 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007047 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007048 while (count && blk) {
7049 enum htx_blk_type type = htx_get_blk_type(blk);
7050 uint32_t sz = htx_get_blksz(blk);
7051
7052 if (sz > count) {
7053 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007054 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007055 goto out;
7056 }
7057
7058 count -= sz;
7059 co_set_data(req, co_data(req) - sz);
7060 blk = htx_remove_blk(req_htx, blk);
7061
7062 if (type == HTX_BLK_EOH)
7063 break;
7064 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007065 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007066 }
7067
7068 /* Executes The applet if it is not done. */
7069 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7070
7071 /* Execute the function. */
7072 switch (hlua_ctx_resume(hlua, 1)) {
7073 /* finished. */
7074 case HLUA_E_OK:
7075 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7076 break;
7077
7078 /* yield. */
7079 case HLUA_E_AGAIN:
7080 if (hlua->wake_time != TICK_ETERNITY)
7081 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007082 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007083
7084 /* finished with error. */
7085 case HLUA_E_ERRMSG:
7086 /* Display log. */
7087 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7088 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7089 lua_pop(hlua->T, 1);
7090 goto error;
7091
7092 case HLUA_E_ETMOUT:
7093 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7094 rule->arg.hlua_rule->fcn.name);
7095 goto error;
7096
7097 case HLUA_E_NOMEM:
7098 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7099 rule->arg.hlua_rule->fcn.name);
7100 goto error;
7101
7102 case HLUA_E_YIELD: /* unexpected */
7103 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7104 rule->arg.hlua_rule->fcn.name);
7105 goto error;
7106
7107 case HLUA_E_ERR:
7108 /* Display log. */
7109 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7110 rule->arg.hlua_rule->fcn.name);
7111 goto error;
7112
7113 default:
7114 goto error;
7115 }
7116 }
7117
7118 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007119 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7120 goto done;
7121
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007122 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7123 goto error;
7124
Christopher Faulet54b5e212019-06-04 10:08:28 +02007125 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02007126 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007127 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7128 si_rx_room_blk(si);
7129 goto out;
7130 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007131 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007132 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7133 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007134 }
7135
7136 done:
7137 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007138 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007139 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007140 si_shutr(si);
7141 }
7142
7143 /* eat the whole request */
7144 if (co_data(req)) {
7145 req_htx = htx_from_buf(&req->buf);
7146 co_htx_skip(req, req_htx, co_data(req));
7147 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007148 }
7149 }
7150
7151 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007152 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007153 return;
7154
7155 error:
7156
7157 /* If we are in HTTP mode, and we are not send any
7158 * data, return a 500 server error in best effort:
7159 * if there is no room available in the buffer,
7160 * just close the connection.
7161 */
7162 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007163 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007164
7165 channel_erase(res);
7166 res->buf.data = b_data(err);
7167 memcpy(res->buf.area, b_head(err), b_data(err));
7168 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007169 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007170 }
7171 if (!(strm->flags & SF_ERR_MASK))
7172 strm->flags |= SF_ERR_RESOURCE;
7173 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7174 goto done;
7175}
7176
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007177static void hlua_applet_http_release(struct appctx *ctx)
7178{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007179 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007180 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007181 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007182 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007183}
7184
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007185/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007186 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007187 *
7188 * This function can fail with an abort() due to an Lua critical error.
7189 * We are in the configuration parsing process of HAProxy, this abort() is
7190 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007191 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007192static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7193 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007194{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007195 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007196 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007197
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007198 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007199 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007200 if (!rule->arg.hlua_rule) {
7201 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007202 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007203 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007204
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007205 /* Memory for arguments. */
7206 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7207 if (!rule->arg.hlua_rule->args) {
7208 memprintf(err, "out of memory error");
7209 return ACT_RET_PRS_ERR;
7210 }
7211
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007212 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007213 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007214
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007215 /* Expect some arguments */
7216 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007217 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007218 memprintf(err, "expect %d arguments", fcn->nargs);
7219 return ACT_RET_PRS_ERR;
7220 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007221 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007222 if (!rule->arg.hlua_rule->args[i]) {
7223 memprintf(err, "out of memory error");
7224 return ACT_RET_PRS_ERR;
7225 }
7226 (*cur_arg)++;
7227 }
7228 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007229
Thierry FOURNIER42148732015-09-02 17:17:33 +02007230 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007231 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007232 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007233}
7234
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007235static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7236 struct act_rule *rule, char **err)
7237{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007238 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007239
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007240 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007241 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007242 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007243 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007244 * the call of this analyzer.
7245 */
7246 if (rule->from != ACT_F_HTTP_REQ) {
7247 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7248 return ACT_RET_PRS_ERR;
7249 }
7250
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007251 /* Memory for the rule. */
7252 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7253 if (!rule->arg.hlua_rule) {
7254 memprintf(err, "out of memory error");
7255 return ACT_RET_PRS_ERR;
7256 }
7257
7258 /* Reference the Lua function and store the reference. */
7259 rule->arg.hlua_rule->fcn = *fcn;
7260
7261 /* TODO: later accept arguments. */
7262 rule->arg.hlua_rule->args = NULL;
7263
7264 /* Add applet pointer in the rule. */
7265 rule->applet.obj_type = OBJ_TYPE_APPLET;
7266 rule->applet.name = fcn->name;
7267 rule->applet.init = hlua_applet_http_init;
7268 rule->applet.fct = hlua_applet_http_fct;
7269 rule->applet.release = hlua_applet_http_release;
7270 rule->applet.timeout = hlua_timeout_applet;
7271
7272 return ACT_RET_PRS_OK;
7273}
7274
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007275/* This function is an LUA binding used for registering
7276 * "sample-conv" functions. It expects a converter name used
7277 * in the haproxy configuration file, and an LUA function.
7278 */
7279__LJMP static int hlua_register_action(lua_State *L)
7280{
7281 struct action_kw_list *akl;
7282 const char *name;
7283 int ref;
7284 int len;
7285 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007286 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007287
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007288 /* Initialise the number of expected arguments at 0. */
7289 nargs = 0;
7290
7291 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7292 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007293
7294 /* First argument : converter name. */
7295 name = MAY_LJMP(luaL_checkstring(L, 1));
7296
7297 /* Second argument : environment. */
7298 if (lua_type(L, 2) != LUA_TTABLE)
7299 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7300
7301 /* Third argument : lua function. */
7302 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7303
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007304 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007305 if (lua_gettop(L) >= 4)
7306 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7307
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007308 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007309 lua_pushnil(L);
7310 while (lua_next(L, 2) != 0) {
7311 if (lua_type(L, -1) != LUA_TSTRING)
7312 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7313
7314 /* Check required environment. Only accepted "http" or "tcp". */
7315 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007316 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007317 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007318 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007319 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007320 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007321 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007322
7323 /* Fill fcn. */
7324 fcn->name = strdup(name);
7325 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007326 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007327 fcn->function_ref = ref;
7328
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007329 /* Set the expected number od arguments. */
7330 fcn->nargs = nargs;
7331
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007332 /* List head */
7333 akl->list.n = akl->list.p = NULL;
7334
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007335 /* action keyword. */
7336 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007337 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007338 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007339 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007340
7341 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7342
7343 akl->kw[0].match_pfx = 0;
7344 akl->kw[0].private = fcn;
7345 akl->kw[0].parse = action_register_lua;
7346
7347 /* select the action registering point. */
7348 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7349 tcp_req_cont_keywords_register(akl);
7350 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7351 tcp_res_cont_keywords_register(akl);
7352 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7353 http_req_keywords_register(akl);
7354 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7355 http_res_keywords_register(akl);
7356 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007357 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007358 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7359 "are expected.", lua_tostring(L, -1)));
7360
7361 /* pop the environment string. */
7362 lua_pop(L, 1);
7363 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007364 return ACT_RET_PRS_OK;
7365}
7366
7367static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7368 struct act_rule *rule, char **err)
7369{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007370 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007371
Christopher Faulet280f85b2019-07-15 15:02:04 +02007372 if (px->mode == PR_MODE_HTTP) {
7373 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007374 return ACT_RET_PRS_ERR;
7375 }
7376
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007377 /* Memory for the rule. */
7378 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7379 if (!rule->arg.hlua_rule) {
7380 memprintf(err, "out of memory error");
7381 return ACT_RET_PRS_ERR;
7382 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007383
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007384 /* Reference the Lua function and store the reference. */
7385 rule->arg.hlua_rule->fcn = *fcn;
7386
7387 /* TODO: later accept arguments. */
7388 rule->arg.hlua_rule->args = NULL;
7389
7390 /* Add applet pointer in the rule. */
7391 rule->applet.obj_type = OBJ_TYPE_APPLET;
7392 rule->applet.name = fcn->name;
7393 rule->applet.init = hlua_applet_tcp_init;
7394 rule->applet.fct = hlua_applet_tcp_fct;
7395 rule->applet.release = hlua_applet_tcp_release;
7396 rule->applet.timeout = hlua_timeout_applet;
7397
7398 return 0;
7399}
7400
7401/* This function is an LUA binding used for registering
7402 * "sample-conv" functions. It expects a converter name used
7403 * in the haproxy configuration file, and an LUA function.
7404 */
7405__LJMP static int hlua_register_service(lua_State *L)
7406{
7407 struct action_kw_list *akl;
7408 const char *name;
7409 const char *env;
7410 int ref;
7411 int len;
7412 struct hlua_function *fcn;
7413
7414 MAY_LJMP(check_args(L, 3, "register_service"));
7415
7416 /* First argument : converter name. */
7417 name = MAY_LJMP(luaL_checkstring(L, 1));
7418
7419 /* Second argument : environment. */
7420 env = MAY_LJMP(luaL_checkstring(L, 2));
7421
7422 /* Third argument : lua function. */
7423 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7424
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007425 /* Allocate and fill the sample fetch keyword struct. */
7426 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7427 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007428 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007429 fcn = calloc(1, sizeof(*fcn));
7430 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007431 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007432
7433 /* Fill fcn. */
7434 len = strlen("<lua.>") + strlen(name) + 1;
7435 fcn->name = calloc(1, len);
7436 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007437 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007438 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7439 fcn->function_ref = ref;
7440
7441 /* List head */
7442 akl->list.n = akl->list.p = NULL;
7443
7444 /* converter keyword. */
7445 len = strlen("lua.") + strlen(name) + 1;
7446 akl->kw[0].kw = calloc(1, len);
7447 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007448 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007449
7450 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7451
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007452 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007453 if (strcmp(env, "tcp") == 0)
7454 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007455 else if (strcmp(env, "http") == 0)
7456 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007457 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007458 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007459 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007460
7461 akl->kw[0].match_pfx = 0;
7462 akl->kw[0].private = fcn;
7463
7464 /* End of array. */
7465 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7466
7467 /* Register this new converter */
7468 service_keywords_register(akl);
7469
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007470 return 0;
7471}
7472
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007473/* This function initialises Lua cli handler. It copies the
7474 * arguments in the Lua stack and create channel IO objects.
7475 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007476static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007477{
7478 struct hlua *hlua;
7479 struct hlua_function *fcn;
7480 int i;
7481 const char *error;
7482
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007483 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007484 appctx->ctx.hlua_cli.fcn = private;
7485
Willy Tarreaubafbe012017-11-24 17:34:44 +01007486 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007487 if (!hlua) {
7488 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007489 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007490 }
7491 HLUA_INIT(hlua);
7492 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007493
7494 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007495 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007496 * applet_http. It is absolutely compatible.
7497 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007498 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007499 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007500 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007501 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007502 }
7503 appctx->ctx.hlua_cli.task->nice = 0;
7504 appctx->ctx.hlua_cli.task->context = appctx;
7505 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7506
7507 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007508 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007509 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007510 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007511 }
7512
7513 /* The following Lua calls can fail. */
7514 if (!SET_SAFE_LJMP(hlua->T)) {
7515 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7516 error = lua_tostring(hlua->T, -1);
7517 else
7518 error = "critical error";
7519 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7520 goto error;
7521 }
7522
7523 /* Check stack available size. */
7524 if (!lua_checkstack(hlua->T, 2)) {
7525 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7526 goto error;
7527 }
7528
7529 /* Restore the function in the stack. */
7530 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7531
7532 /* Once the arguments parsed, the CLI is like an AppletTCP,
7533 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007534 */
7535 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7536 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7537 goto error;
7538 }
7539 hlua->nargs = 1;
7540
7541 /* push keywords in the stack. */
7542 for (i = 0; *args[i]; i++) {
7543 /* Check stack available size. */
7544 if (!lua_checkstack(hlua->T, 1)) {
7545 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7546 goto error;
7547 }
7548 lua_pushstring(hlua->T, args[i]);
7549 hlua->nargs++;
7550 }
7551
7552 /* We must initialize the execution timeouts. */
7553 hlua->max_time = hlua_timeout_session;
7554
7555 /* At this point the execution is safe. */
7556 RESET_SAFE_LJMP(hlua->T);
7557
7558 /* It's ok */
7559 return 0;
7560
7561 /* It's not ok. */
7562error:
7563 RESET_SAFE_LJMP(hlua->T);
7564 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007565 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007566 return 1;
7567}
7568
7569static int hlua_cli_io_handler_fct(struct appctx *appctx)
7570{
7571 struct hlua *hlua;
7572 struct stream_interface *si;
7573 struct hlua_function *fcn;
7574
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007575 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007576 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007577 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007578
7579 /* If the stream is disconnect or closed, ldo nothing. */
7580 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7581 return 1;
7582
7583 /* Execute the function. */
7584 switch (hlua_ctx_resume(hlua, 1)) {
7585
7586 /* finished. */
7587 case HLUA_E_OK:
7588 return 1;
7589
7590 /* yield. */
7591 case HLUA_E_AGAIN:
7592 /* We want write. */
7593 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007594 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007595 /* Set the timeout. */
7596 if (hlua->wake_time != TICK_ETERNITY)
7597 task_schedule(hlua->task, hlua->wake_time);
7598 return 0;
7599
7600 /* finished with error. */
7601 case HLUA_E_ERRMSG:
7602 /* Display log. */
7603 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7604 fcn->name, lua_tostring(hlua->T, -1));
7605 lua_pop(hlua->T, 1);
7606 return 1;
7607
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007608 case HLUA_E_ETMOUT:
7609 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7610 fcn->name);
7611 return 1;
7612
7613 case HLUA_E_NOMEM:
7614 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7615 fcn->name);
7616 return 1;
7617
7618 case HLUA_E_YIELD: /* unexpected */
7619 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7620 fcn->name);
7621 return 1;
7622
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007623 case HLUA_E_ERR:
7624 /* Display log. */
7625 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7626 fcn->name);
7627 return 1;
7628
7629 default:
7630 return 1;
7631 }
7632
7633 return 1;
7634}
7635
7636static void hlua_cli_io_release_fct(struct appctx *appctx)
7637{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007638 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007639 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007640}
7641
7642/* This function is an LUA binding used for registering
7643 * new keywords in the cli. It expects a list of keywords
7644 * which are the "path". It is limited to 5 keywords. A
7645 * description of the command, a function to be executed
7646 * for the parsing and a function for io handlers.
7647 */
7648__LJMP static int hlua_register_cli(lua_State *L)
7649{
7650 struct cli_kw_list *cli_kws;
7651 const char *message;
7652 int ref_io;
7653 int len;
7654 struct hlua_function *fcn;
7655 int index;
7656 int i;
7657
7658 MAY_LJMP(check_args(L, 3, "register_cli"));
7659
7660 /* First argument : an array of maximum 5 keywords. */
7661 if (!lua_istable(L, 1))
7662 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7663
7664 /* Second argument : string with contextual message. */
7665 message = MAY_LJMP(luaL_checkstring(L, 2));
7666
7667 /* Third and fourth argument : lua function. */
7668 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7669
7670 /* Allocate and fill the sample fetch keyword struct. */
7671 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7672 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007673 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007674 fcn = calloc(1, sizeof(*fcn));
7675 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007676 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007677
7678 /* Fill path. */
7679 index = 0;
7680 lua_pushnil(L);
7681 while(lua_next(L, 1) != 0) {
7682 if (index >= 5)
7683 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7684 if (lua_type(L, -1) != LUA_TSTRING)
7685 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7686 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7687 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007688 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007689 index++;
7690 lua_pop(L, 1);
7691 }
7692
7693 /* Copy help message. */
7694 cli_kws->kw[0].usage = strdup(message);
7695 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007696 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007697
7698 /* Fill fcn io handler. */
7699 len = strlen("<lua.cli>") + 1;
7700 for (i = 0; i < index; i++)
7701 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7702 fcn->name = calloc(1, len);
7703 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007704 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007705 strncat((char *)fcn->name, "<lua.cli", len);
7706 for (i = 0; i < index; i++) {
7707 strncat((char *)fcn->name, ".", len);
7708 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7709 }
7710 strncat((char *)fcn->name, ">", len);
7711 fcn->function_ref = ref_io;
7712
7713 /* Fill last entries. */
7714 cli_kws->kw[0].private = fcn;
7715 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7716 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7717 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7718
7719 /* Register this new converter */
7720 cli_register_kw(cli_kws);
7721
7722 return 0;
7723}
7724
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007725static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7726 struct proxy *defpx, const char *file, int line,
7727 char **err, unsigned int *timeout)
7728{
7729 const char *error;
7730
7731 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007732 if (error == PARSE_TIME_OVER) {
7733 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7734 args[1], args[0]);
7735 return -1;
7736 }
7737 else if (error == PARSE_TIME_UNDER) {
7738 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7739 args[1], args[0]);
7740 return -1;
7741 }
7742 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007743 memprintf(err, "%s: invalid timeout", args[0]);
7744 return -1;
7745 }
7746 return 0;
7747}
7748
7749static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7750 struct proxy *defpx, const char *file, int line,
7751 char **err)
7752{
7753 return hlua_read_timeout(args, section_type, curpx, defpx,
7754 file, line, err, &hlua_timeout_session);
7755}
7756
7757static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7758 struct proxy *defpx, const char *file, int line,
7759 char **err)
7760{
7761 return hlua_read_timeout(args, section_type, curpx, defpx,
7762 file, line, err, &hlua_timeout_task);
7763}
7764
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007765static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7766 struct proxy *defpx, const char *file, int line,
7767 char **err)
7768{
7769 return hlua_read_timeout(args, section_type, curpx, defpx,
7770 file, line, err, &hlua_timeout_applet);
7771}
7772
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007773static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7774 struct proxy *defpx, const char *file, int line,
7775 char **err)
7776{
7777 char *error;
7778
7779 hlua_nb_instruction = strtoll(args[1], &error, 10);
7780 if (*error != '\0') {
7781 memprintf(err, "%s: invalid number", args[0]);
7782 return -1;
7783 }
7784 return 0;
7785}
7786
Willy Tarreau32f61e22015-03-18 17:54:59 +01007787static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7788 struct proxy *defpx, const char *file, int line,
7789 char **err)
7790{
7791 char *error;
7792
7793 if (*(args[1]) == 0) {
7794 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7795 return -1;
7796 }
7797 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7798 if (*error != '\0') {
7799 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7800 return -1;
7801 }
7802 return 0;
7803}
7804
7805
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007806/* This function is called by the main configuration key "lua-load". It loads and
7807 * execute an lua file during the parsing of the HAProxy configuration file. It is
7808 * the main lua entry point.
7809 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007810 * This function runs with the HAProxy keywords API. It returns -1 if an error
7811 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007812 *
7813 * In some error case, LUA set an error message in top of the stack. This function
7814 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007815 *
7816 * This function can fail with an abort() due to an Lua critical error.
7817 * We are in the configuration parsing process of HAProxy, this abort() is
7818 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007819 */
7820static int hlua_load(char **args, int section_type, struct proxy *curpx,
7821 struct proxy *defpx, const char *file, int line,
7822 char **err)
7823{
7824 int error;
7825
7826 /* Just load and compile the file. */
7827 error = luaL_loadfile(gL.T, args[1]);
7828 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007829 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007830 lua_pop(gL.T, 1);
7831 return -1;
7832 }
7833
7834 /* If no syntax error where detected, execute the code. */
7835 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7836 switch (error) {
7837 case LUA_OK:
7838 break;
7839 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007840 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007841 lua_pop(gL.T, 1);
7842 return -1;
7843 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007844 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007845 return -1;
7846 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007847 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007848 lua_pop(gL.T, 1);
7849 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02007850#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007851 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007852 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007853 lua_pop(gL.T, 1);
7854 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02007855#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007856 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05007857 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007858 lua_pop(gL.T, 1);
7859 return -1;
7860 }
7861
7862 return 0;
7863}
7864
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01007865/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
7866 * in the given <ctx>.
7867 */
7868static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
7869{
7870 lua_getglobal(ctx.T, "package"); /* push package variable */
7871 lua_pushstring(ctx.T, path); /* push given path */
7872 lua_pushstring(ctx.T, ";"); /* push semicolon */
7873 lua_getfield(ctx.T, -3, type); /* push old path */
7874 lua_concat(ctx.T, 3); /* concatenate to new path */
7875 lua_setfield(ctx.T, -2, type); /* store new path */
7876 lua_pop(ctx.T, 1); /* pop package variable */
7877
7878 return 0;
7879}
7880
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007881static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
7882 struct proxy *defpx, const char *file, int line,
7883 char **err)
7884{
7885 char *path;
7886 char *type = "path";
7887 if (too_many_args(2, args, err, NULL)) {
7888 return -1;
7889 }
7890
7891 if (!(*args[1])) {
7892 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
7893 return -1;
7894 }
7895 path = args[1];
7896
7897 if (*args[2]) {
7898 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
7899 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
7900 return -1;
7901 }
7902 type = args[2];
7903 }
7904
7905 return hlua_prepend_path(gL, type, path);
7906}
7907
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007908/* configuration keywords declaration */
7909static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007910 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007911 { CFG_GLOBAL, "lua-load", hlua_load },
7912 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7913 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007914 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007915 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007916 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007917 { 0, NULL, NULL },
7918}};
7919
Willy Tarreau0108d902018-11-25 19:14:37 +01007920INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7921
Christopher Fauletafd8f102018-11-08 11:34:21 +01007922
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007923/* This function can fail with an abort() due to an Lua critical error.
7924 * We are in the initialisation process of HAProxy, this abort() is
7925 * tolerated.
7926 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007927int hlua_post_init()
7928{
7929 struct hlua_init_function *init;
7930 const char *msg;
7931 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007932 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007933
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007934 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007935 if (!SET_SAFE_LJMP(gL.T)) {
7936 if (lua_type(gL.T, -1) == LUA_TSTRING)
7937 error = lua_tostring(gL.T, -1);
7938 else
7939 error = "critical error";
7940 fprintf(stderr, "Lua post-init: %s.\n", error);
7941 exit(1);
7942 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007943
7944#if USE_OPENSSL
7945 /* Initialize SSL server. */
7946 if (socket_ssl.xprt->prepare_srv) {
7947 int saved_used_backed = global.ssl_used_backend;
7948 // don't affect maxconn automatic computation
7949 socket_ssl.xprt->prepare_srv(&socket_ssl);
7950 global.ssl_used_backend = saved_used_backed;
7951 }
7952#endif
7953
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007954 hlua_fcn_post_init(gL.T);
7955 RESET_SAFE_LJMP(gL.T);
7956
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007957 list_for_each_entry(init, &hlua_init_functions, l) {
7958 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7959 ret = hlua_ctx_resume(&gL, 0);
7960 switch (ret) {
7961 case HLUA_E_OK:
7962 lua_pop(gL.T, -1);
7963 return 1;
7964 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007965 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007966 return 0;
7967 case HLUA_E_ERRMSG:
7968 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007969 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007970 return 0;
7971 case HLUA_E_ERR:
7972 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007973 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007974 return 0;
7975 }
7976 }
7977 return 1;
7978}
7979
Willy Tarreau32f61e22015-03-18 17:54:59 +01007980/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7981 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7982 * is the previously allocated size or the kind of object in case of a new
7983 * allocation. <nsize> is the requested new size.
7984 */
7985static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7986{
7987 struct hlua_mem_allocator *zone = ud;
7988
7989 if (nsize == 0) {
7990 /* it's a free */
7991 if (ptr)
7992 zone->allocated -= osize;
7993 free(ptr);
7994 return NULL;
7995 }
7996
7997 if (!ptr) {
7998 /* it's a new allocation */
7999 if (zone->limit && zone->allocated + nsize > zone->limit)
8000 return NULL;
8001
8002 ptr = malloc(nsize);
8003 if (ptr)
8004 zone->allocated += nsize;
8005 return ptr;
8006 }
8007
8008 /* it's a realloc */
8009 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8010 return NULL;
8011
8012 ptr = realloc(ptr, nsize);
8013 if (ptr)
8014 zone->allocated += nsize - osize;
8015 return ptr;
8016}
8017
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008018/* Ithis function can fail with an abort() due to an Lua critical error.
8019 * We are in the initialisation process of HAProxy, this abort() is
8020 * tolerated.
8021 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008022void hlua_init(void)
8023{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008024 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008025 int idx;
8026 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008027 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008028 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008029 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008030#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008031 struct srv_kw *kw;
8032 int tmp_error;
8033 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008034 char *args[] = { /* SSL client configuration. */
8035 "ssl",
8036 "verify",
8037 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008038 NULL
8039 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008040#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008041
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008042 /* Init main lua stack. */
8043 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008044 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008045 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008046 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008047 hlua_sethlua(&gL);
8048 gL.Tref = LUA_REFNIL;
8049 gL.task = NULL;
8050
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008051 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008052 * the Lua function can fail with an abort. We are in the initialisation
8053 * process of HAProxy, this abort() is tolerated.
8054 */
8055
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008056 /* Initialise lua. */
8057 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008058#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8059#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8060#ifdef HLUA_PREPEND_PATH
8061 hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
8062#endif
8063#ifdef HLUA_PREPEND_CPATH
8064 hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
8065#endif
8066#undef HLUA_PREPEND_PATH_TOSTRING
8067#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008068
Thierry Fournier75933d42016-01-21 09:30:18 +01008069 /* Set safe environment for the initialisation. */
8070 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008071 if (lua_type(gL.T, -1) == LUA_TSTRING)
8072 error_msg = lua_tostring(gL.T, -1);
8073 else
8074 error_msg = "critical error";
8075 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008076 exit(1);
8077 }
8078
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008079 /*
8080 *
8081 * Create "core" object.
8082 *
8083 */
8084
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008085 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008086 lua_newtable(gL.T);
8087
8088 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008089 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008090 hlua_class_const_int(gL.T, log_levels[i], i);
8091
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008092 /* Register special functions. */
8093 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008094 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008095 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008096 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008097 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008098 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008099 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008100 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008101 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008102 hlua_class_function(gL.T, "sleep", hlua_sleep);
8103 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008104 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8105 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8106 hlua_class_function(gL.T, "set_map", hlua_set_map);
8107 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008108 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008109 hlua_class_function(gL.T, "log", hlua_log);
8110 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8111 hlua_class_function(gL.T, "Info", hlua_log_info);
8112 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8113 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008114 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008115 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008116
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008117 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008118
8119 /*
8120 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008121 * Create "act" object.
8122 *
8123 */
8124
8125 /* This table entry is the object "act" base. */
8126 lua_newtable(gL.T);
8127
8128 /* push action return constants */
8129 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8130 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8131 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8132 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8133 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8134 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8135 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8136 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8137
Christopher Faulet501465d2020-02-26 14:54:16 +01008138 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008139
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008140 lua_setglobal(gL.T, "act");
8141
8142 /*
8143 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008144 * Register class Map
8145 *
8146 */
8147
8148 /* This table entry is the object "Map" base. */
8149 lua_newtable(gL.T);
8150
8151 /* register pattern types. */
8152 for (i=0; i<PAT_MATCH_NUM; i++)
8153 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008154 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008155 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8156 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008157 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008158
8159 /* register constructor. */
8160 hlua_class_function(gL.T, "new", hlua_map_new);
8161
8162 /* Create and fill the metatable. */
8163 lua_newtable(gL.T);
8164
Ilya Shipitsind4259502020-04-08 01:07:56 +05008165 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008166 lua_pushstring(gL.T, "__index");
8167 lua_newtable(gL.T);
8168
8169 /* Register . */
8170 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8171 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8172
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008173 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008174
Thierry Fournier45e78d72016-02-19 18:34:46 +01008175 /* Register previous table in the registry with reference and named entry.
8176 * The function hlua_register_metatable() pops the stack, so we
8177 * previously create a copy of the table.
8178 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008179 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008180 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008181
8182 /* Assign the metatable to the mai Map object. */
8183 lua_setmetatable(gL.T, -2);
8184
8185 /* Set a name to the table. */
8186 lua_setglobal(gL.T, "Map");
8187
8188 /*
8189 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008190 * Register class Channel
8191 *
8192 */
8193
8194 /* Create and fill the metatable. */
8195 lua_newtable(gL.T);
8196
Ilya Shipitsind4259502020-04-08 01:07:56 +05008197 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008198 lua_pushstring(gL.T, "__index");
8199 lua_newtable(gL.T);
8200
8201 /* Register . */
8202 hlua_class_function(gL.T, "get", hlua_channel_get);
8203 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8204 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8205 hlua_class_function(gL.T, "set", hlua_channel_set);
8206 hlua_class_function(gL.T, "append", hlua_channel_append);
8207 hlua_class_function(gL.T, "send", hlua_channel_send);
8208 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8209 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8210 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008211 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008212 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008213
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008214 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008215
8216 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008217 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008218
8219 /*
8220 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008221 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008222 *
8223 */
8224
8225 /* Create and fill the metatable. */
8226 lua_newtable(gL.T);
8227
Ilya Shipitsind4259502020-04-08 01:07:56 +05008228 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008229 lua_pushstring(gL.T, "__index");
8230 lua_newtable(gL.T);
8231
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008232 /* Browse existing fetches and create the associated
8233 * object method.
8234 */
8235 sf = NULL;
8236 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8237
8238 /* Dont register the keywork if the arguments check function are
8239 * not safe during the runtime.
8240 */
8241 if ((sf->val_args != NULL) &&
8242 (sf->val_args != val_payload_lv) &&
8243 (sf->val_args != val_hdr))
8244 continue;
8245
8246 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8247 * by an underscore.
8248 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008249 strncpy(trash.area, sf->kw, trash.size);
8250 trash.area[trash.size - 1] = '\0';
8251 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008252 if (*p == '.' || *p == '-' || *p == '+')
8253 *p = '_';
8254
8255 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008256 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008257 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008258 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008259 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008260 }
8261
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008262 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008263
8264 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008265 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008266
8267 /*
8268 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008269 * Register class Converters
8270 *
8271 */
8272
8273 /* Create and fill the metatable. */
8274 lua_newtable(gL.T);
8275
8276 /* Create and fill the __index entry. */
8277 lua_pushstring(gL.T, "__index");
8278 lua_newtable(gL.T);
8279
8280 /* Browse existing converters and create the associated
8281 * object method.
8282 */
8283 sc = NULL;
8284 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8285 /* Dont register the keywork if the arguments check function are
8286 * not safe during the runtime.
8287 */
8288 if (sc->val_args != NULL)
8289 continue;
8290
8291 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8292 * by an underscore.
8293 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008294 strncpy(trash.area, sc->kw, trash.size);
8295 trash.area[trash.size - 1] = '\0';
8296 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008297 if (*p == '.' || *p == '-' || *p == '+')
8298 *p = '_';
8299
8300 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008301 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008302 lua_pushlightuserdata(gL.T, sc);
8303 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008304 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008305 }
8306
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008307 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008308
8309 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008310 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008311
8312 /*
8313 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008314 * Register class HTTP
8315 *
8316 */
8317
8318 /* Create and fill the metatable. */
8319 lua_newtable(gL.T);
8320
Ilya Shipitsind4259502020-04-08 01:07:56 +05008321 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008322 lua_pushstring(gL.T, "__index");
8323 lua_newtable(gL.T);
8324
8325 /* Register Lua functions. */
8326 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8327 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8328 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8329 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8330 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8331 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8332 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8333 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8334 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8335 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8336
8337 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8338 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8339 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8340 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8341 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8342 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008343 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008344
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008345 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008346
8347 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008348 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008349
8350 /*
8351 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008352 * Register class AppletTCP
8353 *
8354 */
8355
8356 /* Create and fill the metatable. */
8357 lua_newtable(gL.T);
8358
Ilya Shipitsind4259502020-04-08 01:07:56 +05008359 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008360 lua_pushstring(gL.T, "__index");
8361 lua_newtable(gL.T);
8362
8363 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008364 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8365 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8366 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8367 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8368 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008369 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8370 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8371 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008372
8373 lua_settable(gL.T, -3);
8374
8375 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008376 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008377
8378 /*
8379 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008380 * Register class AppletHTTP
8381 *
8382 */
8383
8384 /* Create and fill the metatable. */
8385 lua_newtable(gL.T);
8386
Ilya Shipitsind4259502020-04-08 01:07:56 +05008387 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008388 lua_pushstring(gL.T, "__index");
8389 lua_newtable(gL.T);
8390
8391 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008392 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8393 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008394 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8395 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8396 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008397 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8398 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8399 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8400 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8401 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8402 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8403
8404 lua_settable(gL.T, -3);
8405
8406 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008407 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008408
8409 /*
8410 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008411 * Register class TXN
8412 *
8413 */
8414
8415 /* Create and fill the metatable. */
8416 lua_newtable(gL.T);
8417
Ilya Shipitsind4259502020-04-08 01:07:56 +05008418 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008419 lua_pushstring(gL.T, "__index");
8420 lua_newtable(gL.T);
8421
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008422 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008423 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8424 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8425 hlua_class_function(gL.T, "set_var", hlua_set_var);
8426 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8427 hlua_class_function(gL.T, "get_var", hlua_get_var);
8428 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008429 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008430 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8431 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8432 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8433 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8434 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8435 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8436 hlua_class_function(gL.T, "log", hlua_txn_log);
8437 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8438 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8439 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8440 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008441
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008442 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008443
8444 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008445 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008446
8447 /*
8448 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008449 * Register class reply
8450 *
8451 */
8452 lua_newtable(gL.T);
8453 lua_pushstring(gL.T, "__index");
8454 lua_newtable(gL.T);
8455 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8456 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8457 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8458 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8459 lua_settable(gL.T, -3); /* Sets the __index entry. */
8460 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8461
8462
8463 /*
8464 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008465 * Register class Socket
8466 *
8467 */
8468
8469 /* Create and fill the metatable. */
8470 lua_newtable(gL.T);
8471
Ilya Shipitsind4259502020-04-08 01:07:56 +05008472 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008473 lua_pushstring(gL.T, "__index");
8474 lua_newtable(gL.T);
8475
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008476#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008477 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008478#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008479 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8480 hlua_class_function(gL.T, "send", hlua_socket_send);
8481 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8482 hlua_class_function(gL.T, "close", hlua_socket_close);
8483 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8484 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8485 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8486 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8487
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008488 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008489
8490 /* Register the garbage collector entry. */
8491 lua_pushstring(gL.T, "__gc");
8492 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008493 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008494
8495 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008496 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008497
8498 /* Proxy and server configuration initialisation. */
8499 memset(&socket_proxy, 0, sizeof(socket_proxy));
8500 init_new_proxy(&socket_proxy);
8501 socket_proxy.parent = NULL;
8502 socket_proxy.last_change = now.tv_sec;
8503 socket_proxy.id = "LUA-SOCKET";
8504 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8505 socket_proxy.maxconn = 0;
8506 socket_proxy.accept = NULL;
8507 socket_proxy.options2 |= PR_O2_INDEPSTR;
8508 socket_proxy.srv = NULL;
8509 socket_proxy.conn_retries = 0;
8510 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8511
8512 /* Init TCP server: unchanged parameters */
8513 memset(&socket_tcp, 0, sizeof(socket_tcp));
8514 socket_tcp.next = NULL;
8515 socket_tcp.proxy = &socket_proxy;
8516 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8517 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008518 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008519 socket_tcp.idle_conns = NULL;
8520 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008521 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008522 socket_tcp.last_change = 0;
8523 socket_tcp.id = "LUA-TCP-CONN";
8524 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8525 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8526 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8527
8528 /* XXX: Copy default parameter from default server,
8529 * but the default server is not initialized.
8530 */
8531 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8532 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8533 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8534 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8535 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8536 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8537 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8538 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8539 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8540 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8541
8542 socket_tcp.check.status = HCHK_STATUS_INI;
8543 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8544 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8545 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8546 socket_tcp.check.server = &socket_tcp;
8547
8548 socket_tcp.agent.status = HCHK_STATUS_INI;
8549 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8550 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8551 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8552 socket_tcp.agent.server = &socket_tcp;
8553
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008554 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008555
8556#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008557 /* Init TCP server: unchanged parameters */
8558 memset(&socket_ssl, 0, sizeof(socket_ssl));
8559 socket_ssl.next = NULL;
8560 socket_ssl.proxy = &socket_proxy;
8561 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8562 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008563 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008564 socket_ssl.idle_conns = NULL;
8565 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008566 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008567 socket_ssl.last_change = 0;
8568 socket_ssl.id = "LUA-SSL-CONN";
8569 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8570 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8571 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8572
8573 /* XXX: Copy default parameter from default server,
8574 * but the default server is not initialized.
8575 */
8576 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8577 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8578 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8579 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8580 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8581 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8582 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8583 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8584 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8585 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8586
8587 socket_ssl.check.status = HCHK_STATUS_INI;
8588 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8589 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8590 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8591 socket_ssl.check.server = &socket_ssl;
8592
8593 socket_ssl.agent.status = HCHK_STATUS_INI;
8594 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8595 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8596 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8597 socket_ssl.agent.server = &socket_ssl;
8598
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008599 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008600 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008601
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008602 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008603 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8604 /*
8605 *
8606 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008607 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008608 * features like client certificates and ssl_verify.
8609 *
8610 */
8611 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8612 if (tmp_error != 0) {
8613 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8614 abort(); /* This must be never arrives because the command line
8615 not editable by the user. */
8616 }
8617 idx += kw->skip;
8618 }
8619 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008620#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008621
8622 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008623}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008624
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02008625static void hlua_deinit()
8626{
8627 lua_close(gL.T);
8628}
8629
8630REGISTER_POST_DEINIT(hlua_deinit);
8631
Willy Tarreau80713382018-11-26 10:19:54 +01008632static void hlua_register_build_options(void)
8633{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008634 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008635
Willy Tarreaubb57d942016-12-21 19:04:56 +01008636 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8637 hap_register_build_opts(ptr, 1);
8638}
Willy Tarreau80713382018-11-26 10:19:54 +01008639
8640INITCALL0(STG_REGISTER, hlua_register_build_options);