blob: 18c830778c777ceeb6527ce5b6e51ed5025c4a38 [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 *
97 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080098 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +020099 *
100 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
101 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
102 * because they must be exists in the program stack when the longjmp
103 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200104 *
105 * Note that the Lua processing is not really thread safe. It provides
106 * heavy system which consists to add our own lock function in the Lua
107 * code and recompile the library. This system will probably not accepted
108 * by maintainers of various distribs.
109 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500110 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200111 * quick looking on the Lua sources displays a lua_lock() a the start
112 * of function and a lua_unlock() at the end of the function. So I
113 * conclude that the Lua thread safe mode just perform a mutex around
114 * all execution. So I prefer to do this in the HAProxy code, it will be
115 * easier for distro maintainers.
116 *
117 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
118 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
119 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200120 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100121__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200122THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100124static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200125
126#define SET_SAFE_LJMP(__L) \
127 ({ \
128 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100129 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200130 if (setjmp(safe_ljmp_env) != 0) { \
131 lua_atpanic(__L, hlua_panic_safe); \
132 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100133 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200134 } else { \
135 lua_atpanic(__L, hlua_panic_ljmp); \
136 ret = 1; \
137 } \
138 ret; \
139 })
140
141/* If we are the last function catching Lua errors, we
142 * must reset the panic function.
143 */
144#define RESET_SAFE_LJMP(__L) \
145 do { \
146 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100147 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200148 } while(0)
149
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200150/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200151#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100152/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200153#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200154/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100155#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100156#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200157
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100158/* The main Lua execution context. */
159struct hlua gL;
160
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100161/* This is the memory pool containing struct lua for applets
162 * (including cli).
163 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100164DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100165
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100166/* Used for Socket connection. */
167static struct proxy socket_proxy;
168static struct server socket_tcp;
169#ifdef USE_OPENSSL
170static struct server socket_ssl;
171#endif
172
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100173/* List head of the function called at the initialisation time. */
174struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
175
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100176/* The following variables contains the reference of the different
177 * Lua classes. These references are useful for identify metadata
178 * associated with an object.
179 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100180static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100181static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100182static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100183static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100184static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100185static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200186static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200187static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200188static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100189static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100190
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100191/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200192 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100193 * short timeout. Lua linked with tasks doesn't have a timeout
194 * because a task may remain alive during all the haproxy execution.
195 */
196static unsigned int hlua_timeout_session = 4000; /* session timeout. */
197static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200198static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100199
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100200/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
201 * it is used for preventing infinite loops.
202 *
203 * I test the scheer with an infinite loop containing one incrementation
204 * and one test. I run this loop between 10 seconds, I raise a ceil of
205 * 710M loops from one interrupt each 9000 instructions, so I fix the value
206 * to one interrupt each 10 000 instructions.
207 *
208 * configured | Number of
209 * instructions | loops executed
210 * between two | in milions
211 * forced yields |
212 * ---------------+---------------
213 * 10 | 160
214 * 500 | 670
215 * 1000 | 680
216 * 5000 | 700
217 * 7000 | 700
218 * 8000 | 700
219 * 9000 | 710 <- ceil
220 * 10000 | 710
221 * 100000 | 710
222 * 1000000 | 710
223 *
224 */
225static unsigned int hlua_nb_instruction = 10000;
226
Willy Tarreau32f61e22015-03-18 17:54:59 +0100227/* Descriptor for the memory allocation state. If limit is not null, it will
228 * be enforced on any memory allocation.
229 */
230struct hlua_mem_allocator {
231 size_t allocated;
232 size_t limit;
233};
234
235static struct hlua_mem_allocator hlua_global_allocator;
236
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100237/* These functions converts types between HAProxy internal args or
238 * sample and LUA types. Another function permits to check if the
239 * LUA stack contains arguments according with an required ARG_T
240 * format.
241 */
242static int hlua_arg2lua(lua_State *L, const struct arg *arg);
243static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100244__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100245 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100246static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100247static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100248static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
249
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100250__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200251
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200252#define SEND_ERR(__be, __fmt, __args...) \
253 do { \
254 send_log(__be, LOG_ERR, __fmt, ## __args); \
255 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100256 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200257 } while (0)
258
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100259/* Used to check an Lua function type in the stack. It creates and
260 * returns a reference of the function. This function throws an
261 * error if the rgument is not a "function".
262 */
263__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
264{
265 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100266 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100267 WILL_LJMP(luaL_argerror(L, argno, msg));
268 }
269 lua_pushvalue(L, argno);
270 return luaL_ref(L, LUA_REGISTRYINDEX);
271}
272
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200273/* Return the string that is of the top of the stack. */
274const char *hlua_get_top_error_string(lua_State *L)
275{
276 if (lua_gettop(L) < 1)
277 return "unknown error";
278 if (lua_type(L, -1) != LUA_TSTRING)
279 return "unknown error";
280 return lua_tostring(L, -1);
281}
282
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200283__LJMP static const char *hlua_traceback(lua_State *L)
284{
285 lua_Debug ar;
286 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200287 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200288 int filled = 0;
289
290 while (lua_getstack(L, level++, &ar)) {
291
292 /* Add separator */
293 if (filled)
294 chunk_appendf(msg, ", ");
295 filled = 1;
296
297 /* Fill fields:
298 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
299 * 'l': fills in the field currentline;
300 * 'n': fills in the field name and namewhat;
301 * 't': fills in the field istailcall;
302 */
303 lua_getinfo(L, "Slnt", &ar);
304
305 /* Append code localisation */
306 if (ar.currentline > 0)
307 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
308 else
309 chunk_appendf(msg, "%s ", ar.short_src);
310
311 /*
312 * Get function name
313 *
314 * if namewhat is no empty, name is defined.
315 * what contains "Lua" for Lua function, "C" for C function,
316 * or "main" for main code.
317 */
318 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
319 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
320
321 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
322 chunk_appendf(msg, "main chunk");
323
324 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
325 chunk_appendf(msg, "C function line %d", ar.linedefined);
326
327 else /* nothing left... */
328 chunk_appendf(msg, "?");
329
330
331 /* Display tailed call */
332 if (ar.istailcall)
333 chunk_appendf(msg, " ...");
334 }
335
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200336 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200337}
338
339
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100340/* This function check the number of arguments available in the
341 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500342 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100343 */
344__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
345{
346 if (lua_gettop(L) == nb)
347 return;
348 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
349}
350
Mark Lakes22154b42018-01-29 14:38:40 -0800351/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100352 * and the line number where the error is encountered.
353 */
354static int hlua_pusherror(lua_State *L, const char *fmt, ...)
355{
356 va_list argp;
357 va_start(argp, fmt);
358 luaL_where(L, 1);
359 lua_pushvfstring(L, fmt, argp);
360 va_end(argp);
361 lua_concat(L, 2);
362 return 1;
363}
364
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100365/* This functions is used with sample fetch and converters. It
366 * converts the HAProxy configuration argument in a lua stack
367 * values.
368 *
369 * It takes an array of "arg", and each entry of the array is
370 * converted and pushed in the LUA stack.
371 */
372static int hlua_arg2lua(lua_State *L, const struct arg *arg)
373{
374 switch (arg->type) {
375 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100376 case ARGT_TIME:
377 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100378 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100379 break;
380
381 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200382 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100383 break;
384
385 case ARGT_IPV4:
386 case ARGT_IPV6:
387 case ARGT_MSK4:
388 case ARGT_MSK6:
389 case ARGT_FE:
390 case ARGT_BE:
391 case ARGT_TAB:
392 case ARGT_SRV:
393 case ARGT_USR:
394 case ARGT_MAP:
395 default:
396 lua_pushnil(L);
397 break;
398 }
399 return 1;
400}
401
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500402/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100403 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500404 * with sample fetch wrappers. The input arguments are given to the
405 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100406 */
407static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
408{
409 switch (lua_type(L, ud)) {
410
411 case LUA_TNUMBER:
412 case LUA_TBOOLEAN:
413 arg->type = ARGT_SINT;
414 arg->data.sint = lua_tointeger(L, ud);
415 break;
416
417 case LUA_TSTRING:
418 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200419 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200420 /* We don't know the actual size of the underlying allocation, so be conservative. */
421 arg->data.str.size = arg->data.str.data;
422 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100423 break;
424
425 case LUA_TUSERDATA:
426 case LUA_TNIL:
427 case LUA_TTABLE:
428 case LUA_TFUNCTION:
429 case LUA_TTHREAD:
430 case LUA_TLIGHTUSERDATA:
431 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200432 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100433 break;
434 }
435 return 1;
436}
437
438/* the following functions are used to convert a struct sample
439 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500440 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100441 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100442static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100443{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200444 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100445 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200447 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100448 break;
449
450 case SMP_T_BIN:
451 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200452 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100453 break;
454
455 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200456 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
458 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
459 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
460 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
461 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
462 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
463 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
464 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
465 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200466 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100467 break;
468 default:
469 lua_pushnil(L);
470 break;
471 }
472 break;
473
474 case SMP_T_IPV4:
475 case SMP_T_IPV6:
476 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200477 if (sample_casts[smp->data.type][SMP_T_STR] &&
478 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100480 else
481 lua_pushnil(L);
482 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100483 default:
484 lua_pushnil(L);
485 break;
486 }
487 return 1;
488}
489
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100490/* the following functions are used to convert a struct sample
491 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500492 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100493 */
494static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
495{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200496 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100497
498 case SMP_T_BIN:
499 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100501 break;
502
503 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200504 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
506 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
507 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
508 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
509 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
510 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
511 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
512 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
513 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100515 break;
516 default:
517 lua_pushstring(L, "");
518 break;
519 }
520 break;
521
522 case SMP_T_SINT:
523 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100524 case SMP_T_IPV4:
525 case SMP_T_IPV6:
526 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200527 if (sample_casts[smp->data.type][SMP_T_STR] &&
528 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200529 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100530 else
531 lua_pushstring(L, "");
532 break;
533 default:
534 lua_pushstring(L, "");
535 break;
536 }
537 return 1;
538}
539
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100540/* the following functions are used to convert an Lua type in a
541 * struct sample. This is useful to provide data from a converter
542 * to the LUA code.
543 */
544static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
545{
546 switch (lua_type(L, ud)) {
547
548 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200549 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200550 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100551 break;
552
553
554 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200555 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100557 break;
558
559 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200560 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200562 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200563 /* We don't know the actual size of the underlying allocation, so be conservative. */
564 smp->data.u.str.size = smp->data.u.str.data;
565 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100566 break;
567
568 case LUA_TUSERDATA:
569 case LUA_TNIL:
570 case LUA_TTABLE:
571 case LUA_TFUNCTION:
572 case LUA_TTHREAD:
573 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200574 case LUA_TNONE:
575 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200576 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200577 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100578 break;
579 }
580 return 1;
581}
582
Ilya Shipitsind4259502020-04-08 01:07:56 +0500583/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800584 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100585 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100586 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500587 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100588 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100589 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100590__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100591 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100592{
593 int min_arg;
594 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100595 struct proxy *px;
596 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100597
598 idx = 0;
599 min_arg = ARGM(mask);
600 mask >>= ARGM_BITS;
601
602 while (1) {
603
604 /* Check oversize. */
605 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100606 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100607 }
608
609 /* Check for mandatory arguments. */
610 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100611 if (idx < min_arg) {
612
613 /* If miss other argument than the first one, we return an error. */
614 if (idx > 0)
615 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
616
617 /* If first argument have a certain type, some default values
618 * may be used. See the function smp_resolve_args().
619 */
620 switch (mask & ARGT_MASK) {
621
622 case ARGT_FE:
623 if (!(p->cap & PR_CAP_FE))
624 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
625 argp[idx].data.prx = p;
626 argp[idx].type = ARGT_FE;
627 argp[idx+1].type = ARGT_STOP;
628 break;
629
630 case ARGT_BE:
631 if (!(p->cap & PR_CAP_BE))
632 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
633 argp[idx].data.prx = p;
634 argp[idx].type = ARGT_BE;
635 argp[idx+1].type = ARGT_STOP;
636 break;
637
638 case ARGT_TAB:
639 argp[idx].data.prx = p;
640 argp[idx].type = ARGT_TAB;
641 argp[idx+1].type = ARGT_STOP;
642 break;
643
644 default:
645 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
646 break;
647 }
648 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100649 return 0;
650 }
651
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500652 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100653 if ((mask & ARGT_MASK) == ARGT_STOP &&
654 argp[idx].type != ARGT_STOP) {
655 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
656 }
657
658 if ((mask & ARGT_MASK) == ARGT_STOP &&
659 argp[idx].type == ARGT_STOP) {
660 return 0;
661 }
662
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100663 /* Convert some argument types. */
664 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100665 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100666 if (argp[idx].type != ARGT_SINT)
667 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
668 argp[idx].type = ARGT_SINT;
669 break;
670
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100671 case ARGT_TIME:
672 if (argp[idx].type != ARGT_SINT)
673 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200674 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100675 break;
676
677 case ARGT_SIZE:
678 if (argp[idx].type != ARGT_SINT)
679 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200680 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100681 break;
682
683 case ARGT_FE:
684 if (argp[idx].type != ARGT_STR)
685 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200686 memcpy(trash.area, argp[idx].data.str.area,
687 argp[idx].data.str.data);
688 trash.area[argp[idx].data.str.data] = 0;
689 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100690 if (!argp[idx].data.prx)
691 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
692 argp[idx].type = ARGT_FE;
693 break;
694
695 case ARGT_BE:
696 if (argp[idx].type != ARGT_STR)
697 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200698 memcpy(trash.area, argp[idx].data.str.area,
699 argp[idx].data.str.data);
700 trash.area[argp[idx].data.str.data] = 0;
701 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100702 if (!argp[idx].data.prx)
703 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
704 argp[idx].type = ARGT_BE;
705 break;
706
707 case ARGT_TAB:
708 if (argp[idx].type != ARGT_STR)
709 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200710 memcpy(trash.area, argp[idx].data.str.area,
711 argp[idx].data.str.data);
712 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhus9fe7c632019-09-29 23:03:09 +0200713 argp[idx].data.t = stktable_find_by_name(trash.area);
714 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100715 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
716 argp[idx].type = ARGT_TAB;
717 break;
718
719 case ARGT_SRV:
720 if (argp[idx].type != ARGT_STR)
721 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200722 memcpy(trash.area, argp[idx].data.str.area,
723 argp[idx].data.str.data);
724 trash.area[argp[idx].data.str.data] = 0;
725 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100726 if (sname) {
727 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200728 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200729 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100730 if (!px)
731 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
732 }
733 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200734 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100735 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100736 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100737 argp[idx].data.srv = findserver(px, sname);
738 if (!argp[idx].data.srv)
739 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
740 argp[idx].type = ARGT_SRV;
741 break;
742
743 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200744 memcpy(trash.area, argp[idx].data.str.area,
745 argp[idx].data.str.data);
746 trash.area[argp[idx].data.str.data] = 0;
747 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100748 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
749 argp[idx].type = ARGT_IPV4;
750 break;
751
752 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200753 memcpy(trash.area, argp[idx].data.str.area,
754 argp[idx].data.str.data);
755 trash.area[argp[idx].data.str.data] = 0;
756 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100757 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
758 argp[idx].type = ARGT_MSK4;
759 break;
760
761 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200762 memcpy(trash.area, argp[idx].data.str.area,
763 argp[idx].data.str.data);
764 trash.area[argp[idx].data.str.data] = 0;
765 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100766 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
767 argp[idx].type = ARGT_IPV6;
768 break;
769
770 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200771 memcpy(trash.area, argp[idx].data.str.area,
772 argp[idx].data.str.data);
773 trash.area[argp[idx].data.str.data] = 0;
774 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100775 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
776 argp[idx].type = ARGT_MSK6;
777 break;
778
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100779 case ARGT_MAP:
780 case ARGT_REG:
781 case ARGT_USR:
782 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100783 break;
784 }
785
786 /* Check for type of argument. */
787 if ((mask & ARGT_MASK) != argp[idx].type) {
788 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
789 arg_type_names[(mask & ARGT_MASK)],
790 arg_type_names[argp[idx].type & ARGT_MASK]);
791 WILL_LJMP(luaL_argerror(L, first + idx, msg));
792 }
793
794 /* Next argument. */
795 mask >>= ARGT_BITS;
796 idx++;
797 }
798}
799
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100800/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500801 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100802 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100803 *
804 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100805 * - hlua_sethlua : create the association between hlua context and lua_state.
806 */
807static inline struct hlua *hlua_gethlua(lua_State *L)
808{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100809 struct hlua **hlua = lua_getextraspace(L);
810 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100811}
812static inline void hlua_sethlua(struct hlua *hlua)
813{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100814 struct hlua **hlua_store = lua_getextraspace(hlua->T);
815 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100816}
817
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100818/* This function is used to send logs. It try to send on screen (stderr)
819 * and on the default syslog server.
820 */
821static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
822{
823 struct tm tm;
824 char *p;
825
826 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200827 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100828 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200829 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200830 /* Break the message if exceed the buffer size. */
831 *(p-4) = ' ';
832 *(p-3) = '.';
833 *(p-2) = '.';
834 *(p-1) = '.';
835 break;
836 }
Willy Tarreau90807112020-02-25 08:16:33 +0100837 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100838 *p = *msg;
839 else
840 *p = '.';
841 }
842 *p = '\0';
843
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200844 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100845 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200846 get_localtime(date.tv_sec, &tm);
847 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100848 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200849 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100850 fflush(stderr);
851 }
852}
853
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100854/* This function just ensure that the yield will be always
855 * returned with a timeout and permit to set some flags
856 */
857__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100858 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100859{
860 struct hlua *hlua = hlua_gethlua(L);
861
862 /* Set the wake timeout. If timeout is required, we set
863 * the expiration time.
864 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200865 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100866
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100867 hlua->flags |= flags;
868
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100869 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200870 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100871}
872
Willy Tarreau87b09662015-04-03 00:22:06 +0200873/* This function initialises the Lua environment stored in the stream.
874 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100875 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200876 *
877 * This function is particular. it initialises a new Lua thread. If the
878 * initialisation fails (example: out of memory error), the lua function
879 * throws an error (longjmp).
880 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100881 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500882 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100883 * threads appear, the safe environment set a lock to ensure only one
884 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500885 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100886 *
887 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500888 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100889 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800890 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200891 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800892 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500893 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100894 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100895int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100896{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100897 if (!already_safe) {
898 if (!SET_SAFE_LJMP(gL.T)) {
899 lua->Tref = LUA_REFNIL;
900 return 0;
901 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200902 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100903 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100904 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +0100905 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +0100906 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100907 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100908 lua->T = lua_newthread(gL.T);
909 if (!lua->T) {
910 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100911 if (!already_safe)
912 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100913 return 0;
914 }
915 hlua_sethlua(lua);
916 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
917 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100918 if (!already_safe)
919 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100920 return 1;
921}
922
Willy Tarreau87b09662015-04-03 00:22:06 +0200923/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100924 * is destroyed. The destroy also the memory context. The struct "lua"
925 * is not freed.
926 */
927void hlua_ctx_destroy(struct hlua *lua)
928{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100929 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100930 return;
931
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100932 if (!lua->T)
933 goto end;
934
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100935 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200936 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100937
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200938 if (!SET_SAFE_LJMP(lua->T))
939 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100940 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200941 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200942
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200943 if (!SET_SAFE_LJMP(gL.T))
944 return;
945 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
946 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200947 /* Forces a garbage collecting process. If the Lua program is finished
948 * without error, we run the GC on the thread pointer. Its freed all
949 * the unused memory.
950 * If the thread is finnish with an error or is currently yielded,
951 * it seems that the GC applied on the thread doesn't clean anything,
952 * so e run the GC on the main thread.
953 * NOTE: maybe this action locks all the Lua threads untiml the en of
954 * the garbage collection.
955 */
Willy Tarreauf31af932020-01-14 09:59:38 +0100956 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200957 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200958 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200959 lua_gc(gL.T, LUA_GCCOLLECT, 0);
960 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200961 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200962
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200963 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100964
965end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100966 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100967}
968
969/* This function is used to restore the Lua context when a coroutine
970 * fails. This function copy the common memory between old coroutine
971 * and the new coroutine. The old coroutine is destroyed, and its
972 * replaced by the new coroutine.
973 * If the flag "keep_msg" is set, the last entry of the old is assumed
974 * as string error message and it is copied in the new stack.
975 */
976static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
977{
978 lua_State *T;
979 int new_ref;
980
981 /* Renew the main LUA stack doesn't have sense. */
982 if (lua == &gL)
983 return 0;
984
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100985 /* New Lua coroutine. */
986 T = lua_newthread(gL.T);
987 if (!T)
988 return 0;
989
990 /* Copy last error message. */
991 if (keep_msg)
992 lua_xmove(lua->T, T, 1);
993
994 /* Copy data between the coroutines. */
995 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
996 lua_xmove(lua->T, T, 1);
997 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
998
999 /* Destroy old data. */
1000 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1001
1002 /* The thread is garbage collected by Lua. */
1003 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1004
1005 /* Fill the struct with the new coroutine values. */
1006 lua->Mref = new_ref;
1007 lua->T = T;
1008 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1009
1010 /* Set context. */
1011 hlua_sethlua(lua);
1012
1013 return 1;
1014}
1015
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001016void hlua_hook(lua_State *L, lua_Debug *ar)
1017{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001018 struct hlua *hlua = hlua_gethlua(L);
1019
1020 /* Lua cannot yield when its returning from a function,
1021 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001022 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001023 */
1024 if (lua_gethookmask(L) & LUA_MASKRET) {
1025 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1026 return;
1027 }
1028
1029 /* restore the interrupt condition. */
1030 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1031
1032 /* If we interrupt the Lua processing in yieldable state, we yield.
1033 * If the state is not yieldable, trying yield causes an error.
1034 */
1035 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001036 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001037
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001038 /* If we cannot yield, update the clock and check the timeout. */
1039 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001040 hlua->run_time += now_ms - hlua->start_time;
1041 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001042 lua_pushfstring(L, "execution timeout");
1043 WILL_LJMP(lua_error(L));
1044 }
1045
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001046 /* Update the start time. */
1047 hlua->start_time = now_ms;
1048
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001049 /* Try to interrupt the process at the end of the current
1050 * unyieldable function.
1051 */
1052 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001053}
1054
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001055/* This function start or resumes the Lua stack execution. If the flag
1056 * "yield_allowed" if no set and the LUA stack execution returns a yield
1057 * The function return an error.
1058 *
1059 * The function can returns 4 values:
1060 * - HLUA_E_OK : The execution is terminated without any errors.
1061 * - HLUA_E_AGAIN : The execution must continue at the next associated
1062 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001063 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001064 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001065 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001066 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001067 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001068 * LUA code.
1069 */
1070static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1071{
1072 int ret;
1073 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001074 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001075
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001076 /* Initialise run time counter. */
1077 if (!HLUA_IS_RUNNING(lua))
1078 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001079
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001080 /* Lock the whole Lua execution. This lock must be before the
1081 * label "resume_execution".
1082 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001083 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001084
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001085resume_execution:
1086
1087 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1088 * instructions. it is used for preventing infinite loops.
1089 */
1090 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1091
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001092 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001093 HLUA_SET_RUN(lua);
1094 HLUA_CLR_CTRLYIELD(lua);
1095 HLUA_CLR_WAKERESWR(lua);
1096 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001097
Christopher Fauletbc275a92020-02-26 14:55:16 +01001098 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001099 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001100 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001101
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001102 /* Call the function. */
1103 ret = lua_resume(lua->T, gL.T, lua->nargs);
1104 switch (ret) {
1105
1106 case LUA_OK:
1107 ret = HLUA_E_OK;
1108 break;
1109
1110 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001111 /* Check if the execution timeout is expired. It it is the case, we
1112 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001113 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001114 tv_update_date(0, 1);
1115 lua->run_time += now_ms - lua->start_time;
1116 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001117 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001118 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001119 break;
1120 }
1121 /* Process the forced yield. if the general yield is not allowed or
1122 * if no task were associated this the current Lua execution
1123 * coroutine, we resume the execution. Else we want to return in the
1124 * scheduler and we want to be waked up again, to continue the
1125 * current Lua execution. So we schedule our own task.
1126 */
1127 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001128 if (!yield_allowed || !lua->task)
1129 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001130 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001131 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001132 if (!yield_allowed) {
1133 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001134 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001135 break;
1136 }
1137 ret = HLUA_E_AGAIN;
1138 break;
1139
1140 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001141
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001142 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001143 * because the errors ares the only one mean to return immediately
1144 * from and lua execution.
1145 */
1146 if (lua->flags & HLUA_EXIT) {
1147 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001148 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001149 break;
1150 }
1151
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001152 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001153 if (!lua_checkstack(lua->T, 1)) {
1154 ret = HLUA_E_ERR;
1155 break;
1156 }
1157 msg = lua_tostring(lua->T, -1);
1158 lua_settop(lua->T, 0); /* Empty the stack. */
1159 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001160 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001161 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001162 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001163 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001164 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001165 ret = HLUA_E_ERRMSG;
1166 break;
1167
1168 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001169 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001170 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001171 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001172 break;
1173
1174 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001175 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001176 if (!lua_checkstack(lua->T, 1)) {
1177 ret = HLUA_E_ERR;
1178 break;
1179 }
1180 msg = lua_tostring(lua->T, -1);
1181 lua_settop(lua->T, 0); /* Empty the stack. */
1182 lua_pop(lua->T, 1);
1183 if (msg)
1184 lua_pushfstring(lua->T, "message handler error: %s", msg);
1185 else
1186 lua_pushfstring(lua->T, "message handler error");
1187 ret = HLUA_E_ERRMSG;
1188 break;
1189
1190 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001191 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001192 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001193 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001194 break;
1195 }
1196
1197 switch (ret) {
1198 case HLUA_E_AGAIN:
1199 break;
1200
1201 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001202 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001203 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001204 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001205 break;
1206
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001207 case HLUA_E_ETMOUT:
1208 case HLUA_E_NOMEM:
1209 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001210 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001211 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001212 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001213 hlua_ctx_renew(lua, 0);
1214 break;
1215
1216 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001217 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001218 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001219 break;
1220 }
1221
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001222 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001223 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001224
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001225 return ret;
1226}
1227
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001228/* This function exit the current code. */
1229__LJMP static int hlua_done(lua_State *L)
1230{
1231 struct hlua *hlua = hlua_gethlua(L);
1232
1233 hlua->flags |= HLUA_EXIT;
1234 WILL_LJMP(lua_error(L));
1235
1236 return 0;
1237}
1238
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001239/* This function is an LUA binding. It provides a function
1240 * for deleting ACL from a referenced ACL file.
1241 */
1242__LJMP static int hlua_del_acl(lua_State *L)
1243{
1244 const char *name;
1245 const char *key;
1246 struct pat_ref *ref;
1247
1248 MAY_LJMP(check_args(L, 2, "del_acl"));
1249
1250 name = MAY_LJMP(luaL_checkstring(L, 1));
1251 key = MAY_LJMP(luaL_checkstring(L, 2));
1252
1253 ref = pat_ref_lookup(name);
1254 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001255 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001256
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001257 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001258 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001259 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001260 return 0;
1261}
1262
1263/* This function is an LUA binding. It provides a function
1264 * for deleting map entry from a referenced map file.
1265 */
1266static int hlua_del_map(lua_State *L)
1267{
1268 const char *name;
1269 const char *key;
1270 struct pat_ref *ref;
1271
1272 MAY_LJMP(check_args(L, 2, "del_map"));
1273
1274 name = MAY_LJMP(luaL_checkstring(L, 1));
1275 key = MAY_LJMP(luaL_checkstring(L, 2));
1276
1277 ref = pat_ref_lookup(name);
1278 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001279 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001280
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001281 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001282 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001283 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001284 return 0;
1285}
1286
1287/* This function is an LUA binding. It provides a function
1288 * for adding ACL pattern from a referenced ACL file.
1289 */
1290static int hlua_add_acl(lua_State *L)
1291{
1292 const char *name;
1293 const char *key;
1294 struct pat_ref *ref;
1295
1296 MAY_LJMP(check_args(L, 2, "add_acl"));
1297
1298 name = MAY_LJMP(luaL_checkstring(L, 1));
1299 key = MAY_LJMP(luaL_checkstring(L, 2));
1300
1301 ref = pat_ref_lookup(name);
1302 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001303 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001304
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001305 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001306 if (pat_ref_find_elt(ref, key) == NULL)
1307 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001308 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001309 return 0;
1310}
1311
1312/* This function is an LUA binding. It provides a function
1313 * for setting map pattern and sample from a referenced map
1314 * file.
1315 */
1316static int hlua_set_map(lua_State *L)
1317{
1318 const char *name;
1319 const char *key;
1320 const char *value;
1321 struct pat_ref *ref;
1322
1323 MAY_LJMP(check_args(L, 3, "set_map"));
1324
1325 name = MAY_LJMP(luaL_checkstring(L, 1));
1326 key = MAY_LJMP(luaL_checkstring(L, 2));
1327 value = MAY_LJMP(luaL_checkstring(L, 3));
1328
1329 ref = pat_ref_lookup(name);
1330 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001331 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001332
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001333 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001334 if (pat_ref_find_elt(ref, key) != NULL)
1335 pat_ref_set(ref, key, value, NULL);
1336 else
1337 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001338 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001339 return 0;
1340}
1341
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001342/* A class is a lot of memory that contain data. This data can be a table,
1343 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001344 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001345 * the name of the object (_G[<name>] = <metable> ).
1346 *
1347 * A metable is a table that modify the standard behavior of a standard
1348 * access to the associated data. The entries of this new metatable are
1349 * defined as is:
1350 *
1351 * http://lua-users.org/wiki/MetatableEvents
1352 *
1353 * __index
1354 *
1355 * we access an absent field in a table, the result is nil. This is
1356 * true, but it is not the whole truth. Actually, such access triggers
1357 * the interpreter to look for an __index metamethod: If there is no
1358 * such method, as usually happens, then the access results in nil;
1359 * otherwise, the metamethod will provide the result.
1360 *
1361 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1362 * the key does not appear in the table, but the metatable has an __index
1363 * property:
1364 *
1365 * - if the value is a function, the function is called, passing in the
1366 * table and the key; the return value of that function is returned as
1367 * the result.
1368 *
1369 * - if the value is another table, the value of the key in that table is
1370 * asked for and returned (and if it doesn't exist in that table, but that
1371 * table's metatable has an __index property, then it continues on up)
1372 *
1373 * - Use "rawget(myTable,key)" to skip this metamethod.
1374 *
1375 * http://www.lua.org/pil/13.4.1.html
1376 *
1377 * __newindex
1378 *
1379 * Like __index, but control property assignment.
1380 *
1381 * __mode - Control weak references. A string value with one or both
1382 * of the characters 'k' and 'v' which specifies that the the
1383 * keys and/or values in the table are weak references.
1384 *
1385 * __call - Treat a table like a function. When a table is followed by
1386 * parenthesis such as "myTable( 'foo' )" and the metatable has
1387 * a __call key pointing to a function, that function is invoked
1388 * (passing any specified arguments) and the return value is
1389 * returned.
1390 *
1391 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1392 * called, if the metatable for myTable has a __metatable
1393 * key, the value of that key is returned instead of the
1394 * actual metatable.
1395 *
1396 * __tostring - Control string representation. When the builtin
1397 * "tostring( myTable )" function is called, if the metatable
1398 * for myTable has a __tostring property set to a function,
1399 * that function is invoked (passing myTable to it) and the
1400 * return value is used as the string representation.
1401 *
1402 * __len - Control table length. When the table length is requested using
1403 * the length operator ( '#' ), if the metatable for myTable has
1404 * a __len key pointing to a function, that function is invoked
1405 * (passing myTable to it) and the return value used as the value
1406 * of "#myTable".
1407 *
1408 * __gc - Userdata finalizer code. When userdata is set to be garbage
1409 * collected, if the metatable has a __gc field pointing to a
1410 * function, that function is first invoked, passing the userdata
1411 * to it. The __gc metamethod is not called for tables.
1412 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1413 *
1414 * Special metamethods for redefining standard operators:
1415 * http://www.lua.org/pil/13.1.html
1416 *
1417 * __add "+"
1418 * __sub "-"
1419 * __mul "*"
1420 * __div "/"
1421 * __unm "!"
1422 * __pow "^"
1423 * __concat ".."
1424 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001425 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001426 * http://www.lua.org/pil/13.2.html
1427 *
1428 * __eq "=="
1429 * __lt "<"
1430 * __le "<="
1431 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001432
1433/*
1434 *
1435 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001436 * Class Map
1437 *
1438 *
1439 */
1440
1441/* Returns a struct hlua_map if the stack entry "ud" is
1442 * a class session, otherwise it throws an error.
1443 */
1444__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1445{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001446 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001447}
1448
1449/* This function is the map constructor. It don't need
1450 * the class Map object. It creates and return a new Map
1451 * object. It must be called only during "body" or "init"
1452 * context because it process some filesystem accesses.
1453 */
1454__LJMP static int hlua_map_new(struct lua_State *L)
1455{
1456 const char *fn;
1457 int match = PAT_MATCH_STR;
1458 struct sample_conv conv;
1459 const char *file = "";
1460 int line = 0;
1461 lua_Debug ar;
1462 char *err = NULL;
1463 struct arg args[2];
1464
1465 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1466 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1467
1468 fn = MAY_LJMP(luaL_checkstring(L, 1));
1469
1470 if (lua_gettop(L) >= 2) {
1471 match = MAY_LJMP(luaL_checkinteger(L, 2));
1472 if (match < 0 || match >= PAT_MATCH_NUM)
1473 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1474 }
1475
1476 /* Get Lua filename and line number. */
1477 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1478 lua_getinfo(L, "Sl", &ar); /* get info about it */
1479 if (ar.currentline > 0) { /* is there info? */
1480 file = ar.short_src;
1481 line = ar.currentline;
1482 }
1483 }
1484
1485 /* fill fake sample_conv struct. */
1486 conv.kw = ""; /* unused. */
1487 conv.process = NULL; /* unused. */
1488 conv.arg_mask = 0; /* unused. */
1489 conv.val_args = NULL; /* unused. */
1490 conv.out_type = SMP_T_STR;
1491 conv.private = (void *)(long)match;
1492 switch (match) {
1493 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1494 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1495 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1496 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1497 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1498 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1499 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001500 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001501 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1502 default:
1503 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1504 }
1505
1506 /* fill fake args. */
1507 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001508 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001509 args[1].type = ARGT_STOP;
1510
1511 /* load the map. */
1512 if (!sample_load_map(args, &conv, file, line, &err)) {
1513 /* error case: we cant use luaL_error because we must
1514 * free the err variable.
1515 */
1516 luaL_where(L, 1);
1517 lua_pushfstring(L, "'new': %s.", err);
1518 lua_concat(L, 2);
1519 free(err);
1520 WILL_LJMP(lua_error(L));
1521 }
1522
1523 /* create the lua object. */
1524 lua_newtable(L);
1525 lua_pushlightuserdata(L, args[0].data.map);
1526 lua_rawseti(L, -2, 0);
1527
1528 /* Pop a class Map metatable and affect it to the userdata. */
1529 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1530 lua_setmetatable(L, -2);
1531
1532
1533 return 1;
1534}
1535
1536__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1537{
1538 struct map_descriptor *desc;
1539 struct pattern *pat;
1540 struct sample smp;
1541
1542 MAY_LJMP(check_args(L, 2, "lookup"));
1543 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001544 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001545 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001546 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001547 }
1548 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001549 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001550 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001551 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 +02001552 }
1553
1554 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001555 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001556 if (str)
1557 lua_pushstring(L, "");
1558 else
1559 lua_pushnil(L);
1560 return 1;
1561 }
1562
1563 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001564 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001565 return 1;
1566}
1567
1568__LJMP static int hlua_map_lookup(struct lua_State *L)
1569{
1570 return _hlua_map_lookup(L, 0);
1571}
1572
1573__LJMP static int hlua_map_slookup(struct lua_State *L)
1574{
1575 return _hlua_map_lookup(L, 1);
1576}
1577
1578/*
1579 *
1580 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001581 * Class Socket
1582 *
1583 *
1584 */
1585
1586__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1587{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001588 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001589}
1590
1591/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001592 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001593 * received.
1594 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001595static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001596{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001597 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001598
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001599 if (appctx->ctx.hlua_cosocket.die) {
1600 si_shutw(si);
1601 si_shutr(si);
1602 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001603 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1604 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001605 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1606 }
1607
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001608 /* If we cant write, wakeup the pending write signals. */
1609 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001610 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001611
1612 /* If we cant read, wakeup the pending read signals. */
1613 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001614 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001615
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001616 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001617 * to be notified whenever the connection completes.
1618 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001619 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001620 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001621 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001622 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001623 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001624 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001625
1626 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001627 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001628
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001629 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001630 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001631 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001632
1633 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001634 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001635 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001636
1637 /* Some data were injected in the buffer, notify the stream
1638 * interface.
1639 */
1640 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001641 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001642
1643 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001644 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001645 */
1646 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001647 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001648}
1649
Willy Tarreau87b09662015-04-03 00:22:06 +02001650/* This function is called when the "struct stream" is destroyed.
1651 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001652 * Wake all the pending signals.
1653 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001654static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001655{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001656 struct xref *peer;
1657
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001658 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001659 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1660 if (peer)
1661 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001662
1663 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001664 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1665 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001666}
1667
1668/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001669 * uses this object. If the stream does not exists, just quit.
1670 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001671 * pending signal can rest in the read and write lists. destroy
1672 * it.
1673 */
1674__LJMP static int hlua_socket_gc(lua_State *L)
1675{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001676 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001677 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001678 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001679
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001680 MAY_LJMP(check_args(L, 1, "__gc"));
1681
1682 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001683 peer = xref_get_peer_and_lock(&socket->xref);
1684 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001686 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001687
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001688 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001689 appctx->ctx.hlua_cosocket.die = 1;
1690 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001691
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001692 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001693 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001694 return 0;
1695}
1696
1697/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001698 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699 */
sada05ed3302018-05-11 11:48:18 -07001700__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001702 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001704 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001705 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001706
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001707 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001708
1709 /* Check if we run on the same thread than the xreator thread.
1710 * We cannot access to the socket if the thread is different.
1711 */
1712 if (socket->tid != tid)
1713 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1714
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001715 peer = xref_get_peer_and_lock(&socket->xref);
1716 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001717 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001718
1719 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001720 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001721
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001722 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001723 appctx->ctx.hlua_cosocket.die = 1;
1724 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001725
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001726 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001727 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001728 return 0;
1729}
1730
sada05ed3302018-05-11 11:48:18 -07001731/* The close function calls close_helper.
1732 */
1733__LJMP static int hlua_socket_close(lua_State *L)
1734{
1735 MAY_LJMP(check_args(L, 1, "close"));
1736 return hlua_socket_close_helper(L);
1737}
1738
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001739/* This Lua function assumes that the stack contain three parameters.
1740 * 1 - USERDATA containing a struct socket
1741 * 2 - INTEGER with values of the macro defined below
1742 * If the integer is -1, we must read at most one line.
1743 * If the integer is -2, we ust read all the data until the
1744 * end of the stream.
1745 * If the integer is positive value, we must read a number of
1746 * bytes corresponding to this value.
1747 */
1748#define HLSR_READ_LINE (-1)
1749#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001750__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001751{
1752 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1753 int wanted = lua_tointeger(L, 2);
1754 struct hlua *hlua = hlua_gethlua(L);
1755 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001756 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001757 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001758 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001759 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001760 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001761 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001762 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001763 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001764 struct stream_interface *si;
1765 struct stream *s;
1766 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001767 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001768
1769 /* Check if this lua stack is schedulable. */
1770 if (!hlua || !hlua->task)
1771 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1772 "'frontend', 'backend' or 'task'"));
1773
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001774 /* Check if we run on the same thread than the xreator thread.
1775 * We cannot access to the socket if the thread is different.
1776 */
1777 if (socket->tid != tid)
1778 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1779
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001780 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001781 peer = xref_get_peer_and_lock(&socket->xref);
1782 if (!peer)
1783 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001784 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1785 si = appctx->owner;
1786 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001787
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001788 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001790 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001791 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001792 if (nblk < 0) /* Connection close. */
1793 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001794 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001795 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001796
1797 /* remove final \r\n. */
1798 if (nblk == 1) {
1799 if (blk1[len1-1] == '\n') {
1800 len1--;
1801 skip_at_end++;
1802 if (blk1[len1-1] == '\r') {
1803 len1--;
1804 skip_at_end++;
1805 }
1806 }
1807 }
1808 else {
1809 if (blk2[len2-1] == '\n') {
1810 len2--;
1811 skip_at_end++;
1812 if (blk2[len2-1] == '\r') {
1813 len2--;
1814 skip_at_end++;
1815 }
1816 }
1817 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818 }
1819
1820 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001821 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001822 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001823 if (nblk < 0) /* Connection close. */
1824 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001825 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001826 goto connection_empty;
1827 }
1828
1829 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001830 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001831 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832 if (nblk < 0) /* Connection close. */
1833 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001834 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001835 goto connection_empty;
1836
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001837 missing_bytes = wanted - socket->b.n;
1838 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001839 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001840 len1 = missing_bytes;
1841 } if (nblk == 2 && len1 + len2 > missing_bytes)
1842 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001843 }
1844
1845 len = len1;
1846
1847 luaL_addlstring(&socket->b, blk1, len1);
1848 if (nblk == 2) {
1849 len += len2;
1850 luaL_addlstring(&socket->b, blk2, len2);
1851 }
1852
1853 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001854 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001855
1856 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001857 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001858
1859 /* If the pattern reclaim to read all the data
1860 * in the connection, got out.
1861 */
1862 if (wanted == HLSR_READ_ALL)
1863 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001864 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001865 goto connection_empty;
1866
1867 /* Return result. */
1868 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001869 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001870 return 1;
1871
1872connection_closed:
1873
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001874 xref_unlock(&socket->xref, peer);
1875
1876no_peer:
1877
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001878 /* If the buffer containds data. */
1879 if (socket->b.n > 0) {
1880 luaL_pushresult(&socket->b);
1881 return 1;
1882 }
1883 lua_pushnil(L);
1884 lua_pushstring(L, "connection closed.");
1885 return 2;
1886
1887connection_empty:
1888
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001889 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1890 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001891 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001892 }
1893 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001894 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001895 return 0;
1896}
1897
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001898/* This Lua function gets two parameters. The first one can be string
1899 * or a number. If the string is "*l", the user requires one line. If
1900 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001901 * If the value is a number, the user require a number of bytes equal
1902 * to the value. The default value is "*l" (a line).
1903 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001904 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001905 * integer takes this values:
1906 * -1 : read a line
1907 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001908 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001910 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001911 * concatenated with the read data.
1912 */
1913__LJMP static int hlua_socket_receive(struct lua_State *L)
1914{
1915 int wanted = HLSR_READ_LINE;
1916 const char *pattern;
1917 int type;
1918 char *error;
1919 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001920 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001921
1922 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1923 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1924
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001925 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001926
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001927 /* Check if we run on the same thread than the xreator thread.
1928 * We cannot access to the socket if the thread is different.
1929 */
1930 if (socket->tid != tid)
1931 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1932
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001933 /* check for pattern. */
1934 if (lua_gettop(L) >= 2) {
1935 type = lua_type(L, 2);
1936 if (type == LUA_TSTRING) {
1937 pattern = lua_tostring(L, 2);
1938 if (strcmp(pattern, "*a") == 0)
1939 wanted = HLSR_READ_ALL;
1940 else if (strcmp(pattern, "*l") == 0)
1941 wanted = HLSR_READ_LINE;
1942 else {
1943 wanted = strtoll(pattern, &error, 10);
1944 if (*error != '\0')
1945 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1946 }
1947 }
1948 else if (type == LUA_TNUMBER) {
1949 wanted = lua_tointeger(L, 2);
1950 if (wanted < 0)
1951 WILL_LJMP(luaL_error(L, "Unsupported size."));
1952 }
1953 }
1954
1955 /* Set pattern. */
1956 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001957
1958 /* Check if we would replace the top by itself. */
1959 if (lua_gettop(L) != 2)
1960 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001961
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001962 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001963 luaL_buffinit(L, &socket->b);
1964
1965 /* Check prefix. */
1966 if (lua_gettop(L) >= 3) {
1967 if (lua_type(L, 3) != LUA_TSTRING)
1968 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1969 pattern = lua_tolstring(L, 3, &len);
1970 luaL_addlstring(&socket->b, pattern, len);
1971 }
1972
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001973 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001974}
1975
1976/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001977 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001978 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001979static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001980{
1981 struct hlua_socket *socket;
1982 struct hlua *hlua = hlua_gethlua(L);
1983 struct appctx *appctx;
1984 size_t buf_len;
1985 const char *buf;
1986 int len;
1987 int send_len;
1988 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001989 struct xref *peer;
1990 struct stream_interface *si;
1991 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001992
1993 /* Check if this lua stack is schedulable. */
1994 if (!hlua || !hlua->task)
1995 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1996 "'frontend', 'backend' or 'task'"));
1997
1998 /* Get object */
1999 socket = MAY_LJMP(hlua_checksocket(L, 1));
2000 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002001 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002002
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002003 /* Check if we run on the same thread than the xreator thread.
2004 * We cannot access to the socket if the thread is different.
2005 */
2006 if (socket->tid != tid)
2007 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2008
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002009 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002010 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002011 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002012 lua_pushinteger(L, -1);
2013 return 1;
2014 }
2015 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2016 si = appctx->owner;
2017 s = si_strm(si);
2018
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002019 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002020 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002021 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002022 lua_pushinteger(L, -1);
2023 return 1;
2024 }
2025
2026 /* Update the input buffer data. */
2027 buf += sent;
2028 send_len = buf_len - sent;
2029
2030 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002031 if (sent >= buf_len) {
2032 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002033 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002034 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002035
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002036 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002037 * the request buffer if its not required.
2038 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002039 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002040 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002041 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002042 }
2043
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002044 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002045 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002046 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002047 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002048 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002049
2050 /* send data */
2051 if (len < send_len)
2052 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002053 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002054
2055 /* "Not enough space" (-1), "Buffer too little to contain
2056 * the data" (-2) are not expected because the available length
2057 * is tested.
2058 * Other unknown error are also not expected.
2059 */
2060 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002061 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002062 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002063
sada05ed3302018-05-11 11:48:18 -07002064 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002065 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002066 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002067 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002068 return 1;
2069 }
2070
2071 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002072 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002073
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002074 s->req.rex = TICK_ETERNITY;
2075 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002076
2077 /* Update length sent. */
2078 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002079 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002080
2081 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002082 if (sent + len >= buf_len) {
2083 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002084 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002085 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002086
2087hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002088 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2089 xref_unlock(&socket->xref, peer);
2090 WILL_LJMP(luaL_error(L, "out of memory"));
2091 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002092 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002093 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002094 return 0;
2095}
2096
2097/* This function initiate the send of data. It just check the input
2098 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002099 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002100 * "hlua_socket_write_yield" that can yield.
2101 *
2102 * The Lua function gets between 3 and 4 parameters. The first one is
2103 * the associated object. The second is a string buffer. The third is
2104 * a facultative integer that represents where is the buffer position
2105 * of the start of the data that can send. The first byte is the
2106 * position "1". The default value is "1". The fourth argument is a
2107 * facultative integer that represents where is the buffer position
2108 * of the end of the data that can send. The default is the last byte.
2109 */
2110static int hlua_socket_send(struct lua_State *L)
2111{
2112 int i;
2113 int j;
2114 const char *buf;
2115 size_t buf_len;
2116
2117 /* Check number of arguments. */
2118 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2119 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2120
2121 /* Get the string. */
2122 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2123
2124 /* Get and check j. */
2125 if (lua_gettop(L) == 4) {
2126 j = MAY_LJMP(luaL_checkinteger(L, 4));
2127 if (j < 0)
2128 j = buf_len + j + 1;
2129 if (j > buf_len)
2130 j = buf_len + 1;
2131 lua_pop(L, 1);
2132 }
2133 else
2134 j = buf_len;
2135
2136 /* Get and check i. */
2137 if (lua_gettop(L) == 3) {
2138 i = MAY_LJMP(luaL_checkinteger(L, 3));
2139 if (i < 0)
2140 i = buf_len + i + 1;
2141 if (i > buf_len)
2142 i = buf_len + 1;
2143 lua_pop(L, 1);
2144 } else
2145 i = 1;
2146
2147 /* Check bth i and j. */
2148 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002149 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002150 return 1;
2151 }
2152 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002153 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002154 return 1;
2155 }
2156 if (i == 0)
2157 i = 1;
2158 if (j == 0)
2159 j = 1;
2160
2161 /* Pop the string. */
2162 lua_pop(L, 1);
2163
2164 /* Update the buffer length. */
2165 buf += i - 1;
2166 buf_len = j - i + 1;
2167 lua_pushlstring(L, buf, buf_len);
2168
2169 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002170 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002171
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002172 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002173}
2174
Willy Tarreau22b0a682015-06-17 19:43:49 +02002175#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002176__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2177{
2178 static char buffer[SOCKET_INFO_MAX_LEN];
2179 int ret;
2180 int len;
2181 char *p;
2182
2183 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2184 if (ret <= 0) {
2185 lua_pushnil(L);
2186 return 1;
2187 }
2188
2189 if (ret == AF_UNIX) {
2190 lua_pushstring(L, buffer+1);
2191 return 1;
2192 }
2193 else if (ret == AF_INET6) {
2194 buffer[0] = '[';
2195 len = strlen(buffer);
2196 buffer[len] = ']';
2197 len++;
2198 buffer[len] = ':';
2199 len++;
2200 p = buffer;
2201 }
2202 else if (ret == AF_INET) {
2203 p = buffer + 1;
2204 len = strlen(p);
2205 p[len] = ':';
2206 len++;
2207 }
2208 else {
2209 lua_pushnil(L);
2210 return 1;
2211 }
2212
2213 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2214 lua_pushnil(L);
2215 return 1;
2216 }
2217
2218 lua_pushstring(L, p);
2219 return 1;
2220}
2221
2222/* Returns information about the peer of the connection. */
2223__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2224{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002225 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002226 struct xref *peer;
2227 struct appctx *appctx;
2228 struct stream_interface *si;
2229 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002230 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002231
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002232 MAY_LJMP(check_args(L, 1, "getpeername"));
2233
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002234 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002235
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002236 /* Check if we run on the same thread than the xreator thread.
2237 * We cannot access to the socket if the thread is different.
2238 */
2239 if (socket->tid != tid)
2240 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2241
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002242 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002243 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002244 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002245 lua_pushnil(L);
2246 return 1;
2247 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002248 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2249 si = appctx->owner;
2250 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002251
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002252 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002253 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002254 lua_pushnil(L);
2255 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002256 }
2257
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002258 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002259 xref_unlock(&socket->xref, peer);
2260 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002261}
2262
2263/* Returns information about my connection side. */
2264static int hlua_socket_getsockname(struct lua_State *L)
2265{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002266 struct hlua_socket *socket;
2267 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002268 struct appctx *appctx;
2269 struct xref *peer;
2270 struct stream_interface *si;
2271 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002272 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002273
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002274 MAY_LJMP(check_args(L, 1, "getsockname"));
2275
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002276 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002277
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002278 /* Check if we run on the same thread than the xreator thread.
2279 * We cannot access to the socket if the thread is different.
2280 */
2281 if (socket->tid != tid)
2282 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2283
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002284 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002285 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002286 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002287 lua_pushnil(L);
2288 return 1;
2289 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002290 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2291 si = appctx->owner;
2292 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002293
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002294 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002295 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002296 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297 lua_pushnil(L);
2298 return 1;
2299 }
2300
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002301 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002302 xref_unlock(&socket->xref, peer);
2303 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002304}
2305
2306/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002307static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002308 .obj_type = OBJ_TYPE_APPLET,
2309 .name = "<LUA_TCP>",
2310 .fct = hlua_socket_handler,
2311 .release = hlua_socket_release,
2312};
2313
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002314__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002315{
2316 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2317 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002318 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002319 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002320 struct stream_interface *si;
2321 struct stream *s;
2322
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002323 /* Check if we run on the same thread than the xreator thread.
2324 * We cannot access to the socket if the thread is different.
2325 */
2326 if (socket->tid != tid)
2327 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2328
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002329 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002330 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002331 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002332 lua_pushnil(L);
2333 lua_pushstring(L, "Can't connect");
2334 return 2;
2335 }
2336 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2337 si = appctx->owner;
2338 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002339
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002340 /* Check if we run on the same thread than the xreator thread.
2341 * We cannot access to the socket if the thread is different.
2342 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002343 if (socket->tid != tid) {
2344 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002345 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002346 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002347
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002348 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002349 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002350 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002351 lua_pushnil(L);
2352 lua_pushstring(L, "Can't connect");
2353 return 2;
2354 }
2355
Willy Tarreaue09101e2018-10-16 17:37:12 +02002356 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002357
2358 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002359 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002360 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002361 lua_pushinteger(L, 1);
2362 return 1;
2363 }
2364
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002365 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2366 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002367 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002368 }
2369 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002370 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002371 return 0;
2372}
2373
2374/* This function fail or initite the connection. */
2375__LJMP static int hlua_socket_connect(struct lua_State *L)
2376{
2377 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002378 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002379 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002380 struct hlua *hlua;
2381 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002382 int low, high;
2383 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002384 struct xref *peer;
2385 struct stream_interface *si;
2386 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002387
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002388 if (lua_gettop(L) < 2)
2389 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002390
2391 /* Get args. */
2392 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002393
2394 /* Check if we run on the same thread than the xreator thread.
2395 * We cannot access to the socket if the thread is different.
2396 */
2397 if (socket->tid != tid)
2398 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2399
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002400 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002401 if (lua_gettop(L) >= 3) {
2402 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002403 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002404
Tim Duesterhus6edab862018-01-06 19:04:45 +01002405 /* Force the ip to end with a colon, to support IPv6 addresses
2406 * that are not enclosed within square brackets.
2407 */
2408 if (port > 0) {
2409 luaL_buffinit(L, &b);
2410 luaL_addstring(&b, ip);
2411 luaL_addchar(&b, ':');
2412 luaL_pushresult(&b);
2413 ip = lua_tolstring(L, lua_gettop(L), NULL);
2414 }
2415 }
2416
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002417 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002418 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002419 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002420 lua_pushnil(L);
2421 return 1;
2422 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002423
2424 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002425 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002426 if (!addr) {
2427 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002428 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002429 }
2430 if (low != high) {
2431 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002432 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002433 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002434
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002435 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002436 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002437 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002438 if (port == -1) {
2439 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002440 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002441 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002442 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2443 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002444 if (port == -1) {
2445 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002446 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002447 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002448 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002449 }
2450 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002451
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002452 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2453 si = appctx->owner;
2454 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002455
2456 if (!sockaddr_alloc(&s->target_addr)) {
2457 xref_unlock(&socket->xref, peer);
2458 WILL_LJMP(luaL_error(L, "connect: internal error"));
2459 }
2460 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002461 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002462
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002463 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002464
2465 /* inform the stream that we want to be notified whenever the
2466 * connection completes.
2467 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002468 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002469 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002470 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002471
Willy Tarreauf31af932020-01-14 09:59:38 +01002472 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002473
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002474 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2475 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002476 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002477 }
2478 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002479
2480 task_wakeup(s->task, TASK_WOKEN_INIT);
2481 /* Return yield waiting for connection. */
2482
Willy Tarreau9635e032018-10-16 17:52:55 +02002483 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002484
2485 return 0;
2486}
2487
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002488#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002489__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2490{
2491 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002492 struct xref *peer;
2493 struct appctx *appctx;
2494 struct stream_interface *si;
2495 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002496
2497 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2498 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002499
2500 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002501 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002502 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002503 lua_pushnil(L);
2504 return 1;
2505 }
2506 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2507 si = appctx->owner;
2508 s = si_strm(si);
2509
2510 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002511 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002512 return MAY_LJMP(hlua_socket_connect(L));
2513}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002514#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002515
2516__LJMP static int hlua_socket_setoption(struct lua_State *L)
2517{
2518 return 0;
2519}
2520
2521__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2522{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002523 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002524 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002525 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002526 struct xref *peer;
2527 struct appctx *appctx;
2528 struct stream_interface *si;
2529 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002530
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002531 MAY_LJMP(check_args(L, 2, "settimeout"));
2532
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002533 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002534
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002535 /* convert the timeout to millis */
2536 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002537
Thierry Fournier17a921b2018-03-08 09:59:02 +01002538 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002539 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002540 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2541
Mark Lakes56cc1252018-03-27 09:48:06 +02002542 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002543 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002544
2545 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002546 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002547 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002548
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002549 /* Check if we run on the same thread than the xreator thread.
2550 * We cannot access to the socket if the thread is different.
2551 */
2552 if (socket->tid != tid)
2553 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2554
Mark Lakes56cc1252018-03-27 09:48:06 +02002555 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002556 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002557 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002558 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2559 WILL_LJMP(lua_error(L));
2560 return 0;
2561 }
2562 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2563 si = appctx->owner;
2564 s = si_strm(si);
2565
Cyril Bonté7bb63452018-08-17 23:51:02 +02002566 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002567 s->req.rto = tmout;
2568 s->req.wto = tmout;
2569 s->res.rto = tmout;
2570 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002571 s->req.rex = tick_add_ifset(now_ms, tmout);
2572 s->req.wex = tick_add_ifset(now_ms, tmout);
2573 s->res.rex = tick_add_ifset(now_ms, tmout);
2574 s->res.wex = tick_add_ifset(now_ms, tmout);
2575
2576 s->task->expire = tick_add_ifset(now_ms, tmout);
2577 task_queue(s->task);
2578
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002579 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002580
Thierry Fourniere9636f12018-03-08 09:54:32 +01002581 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002582 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002583}
2584
2585__LJMP static int hlua_socket_new(lua_State *L)
2586{
2587 struct hlua_socket *socket;
2588 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002589 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002590 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002591
2592 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002593 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002594 hlua_pusherror(L, "socket: full stack");
2595 goto out_fail_conf;
2596 }
2597
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002598 /* Create the object: obj[0] = userdata. */
2599 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002600 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002601 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002602 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002603 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002604
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002605 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002606 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002607 hlua_pusherror(L, "socket: uninitialized pools.");
2608 goto out_fail_conf;
2609 }
2610
Willy Tarreau87b09662015-04-03 00:22:06 +02002611 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002612 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2613 lua_setmetatable(L, -2);
2614
Willy Tarreaud420a972015-04-06 00:39:18 +02002615 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002616 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002617 if (!appctx) {
2618 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002619 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002620 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002621
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002622 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002623 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002624 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2625 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002626
Willy Tarreaud420a972015-04-06 00:39:18 +02002627 /* Now create a session, task and stream for this applet */
2628 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2629 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002630 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002631 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002632 }
2633
Willy Tarreau87787ac2017-08-28 16:22:54 +02002634 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002635 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002636 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002637 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002638 }
2639
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002640 /* Initialise cross reference between stream and Lua socket object. */
2641 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002642
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002643 /* Configure "right" stream interface. this "si" is used to connect
2644 * and retrieve data from the server. The connection is initialized
2645 * with the "struct server".
2646 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002647 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002648
2649 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002650 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002651 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002652
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002653 return 1;
2654
Willy Tarreaud420a972015-04-06 00:39:18 +02002655 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002656 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002657 out_fail_sess:
2658 appctx_free(appctx);
2659 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002660 WILL_LJMP(lua_error(L));
2661 return 0;
2662}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002663
2664/*
2665 *
2666 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002667 * Class Channel
2668 *
2669 *
2670 */
2671
2672/* Returns the struct hlua_channel join to the class channel in the
2673 * stack entry "ud" or throws an argument error.
2674 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002675__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002676{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002677 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002678}
2679
Willy Tarreau47860ed2015-03-10 14:07:50 +01002680/* Pushes the channel onto the top of the stack. If the stask does not have a
2681 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002682 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002683static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002684{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002685 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002686 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002687 return 0;
2688
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002689 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002690 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002691 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002692
2693 /* Pop a class sesison metatable and affect it to the userdata. */
2694 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2695 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002696 return 1;
2697}
2698
2699/* Duplicate all the data present in the input channel and put it
2700 * in a string LUA variables. Returns -1 and push a nil value in
2701 * the stack if the channel is closed and all the data are consumed,
2702 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002703 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002704 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002705static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002706{
2707 char *blk1;
2708 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002709 size_t len1;
2710 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002711 int ret;
2712 luaL_Buffer b;
2713
Willy Tarreau06d80a92017-10-19 14:32:15 +02002714 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002715 if (unlikely(ret == 0))
2716 return 0;
2717
2718 if (unlikely(ret < 0)) {
2719 lua_pushnil(L);
2720 return -1;
2721 }
2722
2723 luaL_buffinit(L, &b);
2724 luaL_addlstring(&b, blk1, len1);
2725 if (unlikely(ret == 2))
2726 luaL_addlstring(&b, blk2, len2);
2727 luaL_pushresult(&b);
2728
2729 if (unlikely(ret == 2))
2730 return len1 + len2;
2731 return len1;
2732}
2733
2734/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2735 * a yield. This function keep the data in the buffer.
2736 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002737__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002738{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002739 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002740
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002741 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2742
Christopher Faulet3f829a42018-12-13 21:56:45 +01002743 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2744 WILL_LJMP(lua_error(L));
2745
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002746 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002747 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002748 return 1;
2749}
2750
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002751/* Check arguments for the function "hlua_channel_dup_yield". */
2752__LJMP static int hlua_channel_dup(lua_State *L)
2753{
2754 MAY_LJMP(check_args(L, 1, "dup"));
2755 MAY_LJMP(hlua_checkchannel(L, 1));
2756 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2757}
2758
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002759/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2760 * a yield. This function consumes the data in the buffer. It returns
2761 * a string containing the data or a nil pointer if no data are available
2762 * and the channel is closed.
2763 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002764__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002765{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002766 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002767 int ret;
2768
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002769 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002770
Christopher Faulet3f829a42018-12-13 21:56:45 +01002771 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2772 WILL_LJMP(lua_error(L));
2773
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002774 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002775 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002776 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002777
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002778 if (unlikely(ret == -1))
2779 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002780
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002781 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002782 return 1;
2783}
2784
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002785/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002786__LJMP static int hlua_channel_get(lua_State *L)
2787{
2788 MAY_LJMP(check_args(L, 1, "get"));
2789 MAY_LJMP(hlua_checkchannel(L, 1));
2790 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2791}
2792
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002793/* This functions consumes and returns one line. If the channel is closed,
2794 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002795 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002796 * value.
2797 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002798__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002799{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002800 char *blk1;
2801 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002802 size_t len1;
2803 size_t len2;
2804 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002805 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002806 int ret;
2807 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002808
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002809 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2810
Christopher Faulet3f829a42018-12-13 21:56:45 +01002811 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2812 WILL_LJMP(lua_error(L));
2813
Willy Tarreau06d80a92017-10-19 14:32:15 +02002814 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002815 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002816 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002817
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002818 if (ret == -1) {
2819 lua_pushnil(L);
2820 return 1;
2821 }
2822
2823 luaL_buffinit(L, &b);
2824 luaL_addlstring(&b, blk1, len1);
2825 len = len1;
2826 if (unlikely(ret == 2)) {
2827 luaL_addlstring(&b, blk2, len2);
2828 len += len2;
2829 }
2830 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002831 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002832 return 1;
2833}
2834
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002835/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002836__LJMP static int hlua_channel_getline(lua_State *L)
2837{
2838 MAY_LJMP(check_args(L, 1, "getline"));
2839 MAY_LJMP(hlua_checkchannel(L, 1));
2840 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2841}
2842
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002843/* This function takes a string as input, and append it at the
2844 * input side of channel. If the data is too big, but a space
2845 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002846 * yields. If the data is bigger than the buffer, or if the
2847 * channel is closed, it returns -1. Otherwise, it returns the
2848 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002849 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002850__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002851{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002852 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002853 size_t len;
2854 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2855 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2856 int ret;
2857 int max;
2858
Christopher Faulet3f829a42018-12-13 21:56:45 +01002859 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2860 WILL_LJMP(lua_error(L));
2861
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002862 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002863 * the request buffer if its not required.
2864 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002865 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002866 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002867 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002868 }
2869
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002870 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002871 if (max > len - l)
2872 max = len - l;
2873
Willy Tarreau06d80a92017-10-19 14:32:15 +02002874 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002875 if (ret == -2 || ret == -3) {
2876 lua_pushinteger(L, -1);
2877 return 1;
2878 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002879 if (ret == -1) {
2880 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002881 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002882 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002883 l += ret;
2884 lua_pop(L, 1);
2885 lua_pushinteger(L, l);
2886
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002887 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002888 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002889 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002890 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002891 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002892 */
2893 return 1;
2894 }
2895 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002896 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897 return 1;
2898}
2899
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002900/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2901 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002902 * buffer size is too little for the data.
2903 */
2904__LJMP static int hlua_channel_append(lua_State *L)
2905{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002906 size_t len;
2907
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002908 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002909 MAY_LJMP(hlua_checkchannel(L, 1));
2910 MAY_LJMP(luaL_checklstring(L, 2, &len));
2911 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912 lua_pushinteger(L, 0);
2913
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002914 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002915}
2916
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002917/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002919 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002920 * or -1 if the channel is closed or if the buffer size is too
2921 * little for the data.
2922 */
2923__LJMP static int hlua_channel_set(lua_State *L)
2924{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002925 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002926
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002927 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002928 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002929 lua_pushinteger(L, 0);
2930
Christopher Faulet3f829a42018-12-13 21:56:45 +01002931 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2932 WILL_LJMP(lua_error(L));
2933
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002934 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002935
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002936 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937}
2938
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002939/* Append data in the output side of the buffer. This data is immediately
2940 * sent. The function returns the amount of data written. If the buffer
2941 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002942 * if the channel is closed.
2943 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002944__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002945{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002946 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002947 size_t len;
2948 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2949 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2950 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002951 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002952
Christopher Faulet3f829a42018-12-13 21:56:45 +01002953 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2954 WILL_LJMP(lua_error(L));
2955
Willy Tarreau47860ed2015-03-10 14:07:50 +01002956 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002957 lua_pushinteger(L, -1);
2958 return 1;
2959 }
2960
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002961 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002962 * the request buffer if its not required.
2963 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002964 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002965 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002966 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002967 }
2968
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002969 /* The written data will be immediately sent, so we can check
2970 * the available space without taking in account the reserve.
2971 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002972 * data, because the buffer will be flushed.
2973 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002974 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002975
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002976 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002977 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002978 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002979 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02002980 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002981 return 1;
2982
2983 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002984 if (max > len - l)
2985 max = len - l;
2986
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002987 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002988 * detects a non contiguous buffer and realign it.
2989 */
Willy Tarreau3f679992018-06-15 15:06:42 +02002990 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002991 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002992
2993 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002994 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002995
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002996 /* buffer replace considers that the input part is filled.
2997 * so, I must forward these new data in the output part.
2998 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02002999 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003000
3001 l += max;
3002 lua_pop(L, 1);
3003 lua_pushinteger(L, l);
3004
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003005 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003006 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003007 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003008 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003009 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003010 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003011 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003012
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003013 if (l < len) {
3014 /* If we are waiting for space in the response buffer, we
3015 * must set the flag WAKERESWR. This flag required the task
3016 * wake up if any activity is detected on the response buffer.
3017 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003018 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003019 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003020 else
3021 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003022 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003023 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024
3025 return 1;
3026}
3027
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003028/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003029 * yield the LUA process, and resume it without checking the
3030 * input arguments.
3031 */
3032__LJMP static int hlua_channel_send(lua_State *L)
3033{
3034 MAY_LJMP(check_args(L, 2, "send"));
3035 lua_pushinteger(L, 0);
3036
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003037 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003038}
3039
3040/* This function forward and amount of butes. The data pass from
3041 * the input side of the buffer to the output side, and can be
3042 * forwarded. This function never fails.
3043 *
3044 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003045 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003046 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003047__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003048{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003049 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003050 int len;
3051 int l;
3052 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003053 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003054
3055 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003056
3057 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3058 WILL_LJMP(lua_error(L));
3059
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003060 len = MAY_LJMP(luaL_checkinteger(L, 2));
3061 l = MAY_LJMP(luaL_checkinteger(L, -1));
3062
3063 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003064 if (max > ci_data(chn))
3065 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003066 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003067 l += max;
3068
3069 lua_pop(L, 1);
3070 lua_pushinteger(L, l);
3071
3072 /* Check if it miss bytes to forward. */
3073 if (l < len) {
3074 /* The the input channel or the output channel are closed, we
3075 * must return the amount of data forwarded.
3076 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003077 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003078 return 1;
3079
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003080 /* If we are waiting for space data in the response buffer, we
3081 * must set the flag WAKERESWR. This flag required the task
3082 * wake up if any activity is detected on the response buffer.
3083 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003084 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003085 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003086 else
3087 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003088
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003089 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003090 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003091 }
3092
3093 return 1;
3094}
3095
3096/* Just check the input and prepare the stack for the previous
3097 * function "hlua_channel_forward_yield"
3098 */
3099__LJMP static int hlua_channel_forward(lua_State *L)
3100{
3101 MAY_LJMP(check_args(L, 2, "forward"));
3102 MAY_LJMP(hlua_checkchannel(L, 1));
3103 MAY_LJMP(luaL_checkinteger(L, 2));
3104
3105 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003106 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003107}
3108
3109/* Just returns the number of bytes available in the input
3110 * side of the buffer. This function never fails.
3111 */
3112__LJMP static int hlua_channel_get_in_len(lua_State *L)
3113{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003114 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003115
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003116 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003117 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003118 if (IS_HTX_STRM(chn_strm(chn))) {
3119 struct htx *htx = htxbuf(&chn->buf);
3120 lua_pushinteger(L, htx->data - co_data(chn));
3121 }
3122 else
3123 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003124 return 1;
3125}
3126
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003127/* Returns true if the channel is full. */
3128__LJMP static int hlua_channel_is_full(lua_State *L)
3129{
3130 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003131
3132 MAY_LJMP(check_args(L, 1, "is_full"));
3133 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003134 /* ignore the reserve, we are not on a producer side (ie in an
3135 * applet).
3136 */
3137 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003138 return 1;
3139}
3140
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003141/* Returns true if the channel is the response channel. */
3142__LJMP static int hlua_channel_is_resp(lua_State *L)
3143{
3144 struct channel *chn;
3145
3146 MAY_LJMP(check_args(L, 1, "is_resp"));
3147 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3148
3149 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3150 return 1;
3151}
3152
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003153/* Just returns the number of bytes available in the output
3154 * side of the buffer. This function never fails.
3155 */
3156__LJMP static int hlua_channel_get_out_len(lua_State *L)
3157{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003158 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003159
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003160 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003161 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003162 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003163 return 1;
3164}
3165
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003166/*
3167 *
3168 *
3169 * Class Fetches
3170 *
3171 *
3172 */
3173
3174/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003175 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003176 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003177__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003178{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003179 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003180}
3181
3182/* This function creates and push in the stack a fetch object according
3183 * with a current TXN.
3184 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003185static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003186{
Willy Tarreau7073c472015-04-06 11:15:40 +02003187 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003188
3189 /* Check stack size. */
3190 if (!lua_checkstack(L, 3))
3191 return 0;
3192
3193 /* Create the object: obj[0] = userdata.
3194 * Note that the base of the Fetches object is the
3195 * transaction object.
3196 */
3197 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003198 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003199 lua_rawseti(L, -2, 0);
3200
Willy Tarreau7073c472015-04-06 11:15:40 +02003201 hsmp->s = txn->s;
3202 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003203 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003204 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003205
3206 /* Pop a class sesison metatable and affect it to the userdata. */
3207 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3208 lua_setmetatable(L, -2);
3209
3210 return 1;
3211}
3212
3213/* This function is an LUA binding. It is called with each sample-fetch.
3214 * It uses closure argument to store the associated sample-fetch. It
3215 * returns only one argument or throws an error. An error is thrown
3216 * only if an error is encountered during the argument parsing. If
3217 * the "sample-fetch" function fails, nil is returned.
3218 */
3219__LJMP static int hlua_run_sample_fetch(lua_State *L)
3220{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003221 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003222 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003223 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003224 int i;
3225 struct sample smp;
3226
3227 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003228 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003229
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003230 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003231 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003232
Thierry FOURNIERca988662015-12-20 18:43:03 +01003233 /* Check execution authorization. */
3234 if (f->use & SMP_USE_HTTP_ANY &&
3235 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3236 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3237 "is not available in Lua services", f->kw);
3238 WILL_LJMP(lua_error(L));
3239 }
3240
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003241 /* Get extra arguments. */
3242 for (i = 0; i < lua_gettop(L) - 1; i++) {
3243 if (i >= ARGM_NBARGS)
3244 break;
3245 hlua_lua2arg(L, i + 2, &args[i]);
3246 }
3247 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003248 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003249
3250 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003251 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003252
3253 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003254 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003255 lua_pushfstring(L, "error in arguments");
3256 WILL_LJMP(lua_error(L));
3257 }
3258
3259 /* Initialise the sample. */
3260 memset(&smp, 0, sizeof(smp));
3261
3262 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003263 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003264 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003265 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003266 lua_pushstring(L, "");
3267 else
3268 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003269 return 1;
3270 }
3271
3272 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003273 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003274 hlua_smp2lua_str(L, &smp);
3275 else
3276 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003277 return 1;
3278}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003279
3280/*
3281 *
3282 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003283 * Class Converters
3284 *
3285 *
3286 */
3287
3288/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003289 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003290 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003291__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003292{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003293 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003294}
3295
3296/* This function creates and push in the stack a Converters object
3297 * according with a current TXN.
3298 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003299static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003300{
Willy Tarreau7073c472015-04-06 11:15:40 +02003301 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003302
3303 /* Check stack size. */
3304 if (!lua_checkstack(L, 3))
3305 return 0;
3306
3307 /* Create the object: obj[0] = userdata.
3308 * Note that the base of the Converters object is the
3309 * same than the TXN object.
3310 */
3311 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003312 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003313 lua_rawseti(L, -2, 0);
3314
Willy Tarreau7073c472015-04-06 11:15:40 +02003315 hsmp->s = txn->s;
3316 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003317 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003318 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003319
Willy Tarreau87b09662015-04-03 00:22:06 +02003320 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003321 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3322 lua_setmetatable(L, -2);
3323
3324 return 1;
3325}
3326
3327/* This function is an LUA binding. It is called with each converter.
3328 * It uses closure argument to store the associated converter. It
3329 * returns only one argument or throws an error. An error is thrown
3330 * only if an error is encountered during the argument parsing. If
3331 * the converter function function fails, nil is returned.
3332 */
3333__LJMP static int hlua_run_sample_conv(lua_State *L)
3334{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003335 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003336 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003337 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003338 int i;
3339 struct sample smp;
3340
3341 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003342 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003343
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003344 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003345 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003346
3347 /* Get extra arguments. */
3348 for (i = 0; i < lua_gettop(L) - 2; i++) {
3349 if (i >= ARGM_NBARGS)
3350 break;
3351 hlua_lua2arg(L, i + 3, &args[i]);
3352 }
3353 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003354 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003355
3356 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003357 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003358
3359 /* Run the special args checker. */
3360 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3361 hlua_pusherror(L, "error in arguments");
3362 WILL_LJMP(lua_error(L));
3363 }
3364
3365 /* Initialise the sample. */
3366 if (!hlua_lua2smp(L, 2, &smp)) {
3367 hlua_pusherror(L, "error in the input argument");
3368 WILL_LJMP(lua_error(L));
3369 }
3370
Willy Tarreau1777ea62016-03-10 16:15:46 +01003371 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3372
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003373 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003374 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003375 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003376 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003377 WILL_LJMP(lua_error(L));
3378 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003379 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3380 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003381 hlua_pusherror(L, "error during the input argument casting");
3382 WILL_LJMP(lua_error(L));
3383 }
3384
3385 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003386 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003387 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003388 lua_pushstring(L, "");
3389 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003390 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003391 return 1;
3392 }
3393
3394 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003395 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003396 hlua_smp2lua_str(L, &smp);
3397 else
3398 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003399 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003400}
3401
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003402/*
3403 *
3404 *
3405 * Class AppletTCP
3406 *
3407 *
3408 */
3409
3410/* Returns a struct hlua_txn if the stack entry "ud" is
3411 * a class stream, otherwise it throws an error.
3412 */
3413__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3414{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003415 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003416}
3417
3418/* This function creates and push in the stack an Applet object
3419 * according with a current TXN.
3420 */
3421static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3422{
3423 struct hlua_appctx *appctx;
3424 struct stream_interface *si = ctx->owner;
3425 struct stream *s = si_strm(si);
3426 struct proxy *p = s->be;
3427
3428 /* Check stack size. */
3429 if (!lua_checkstack(L, 3))
3430 return 0;
3431
3432 /* Create the object: obj[0] = userdata.
3433 * Note that the base of the Converters object is the
3434 * same than the TXN object.
3435 */
3436 lua_newtable(L);
3437 appctx = lua_newuserdata(L, sizeof(*appctx));
3438 lua_rawseti(L, -2, 0);
3439 appctx->appctx = ctx;
3440 appctx->htxn.s = s;
3441 appctx->htxn.p = p;
3442
3443 /* Create the "f" field that contains a list of fetches. */
3444 lua_pushstring(L, "f");
3445 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3446 return 0;
3447 lua_settable(L, -3);
3448
3449 /* Create the "sf" field that contains a list of stringsafe fetches. */
3450 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003451 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003452 return 0;
3453 lua_settable(L, -3);
3454
3455 /* Create the "c" field that contains a list of converters. */
3456 lua_pushstring(L, "c");
3457 if (!hlua_converters_new(L, &appctx->htxn, 0))
3458 return 0;
3459 lua_settable(L, -3);
3460
3461 /* Create the "sc" field that contains a list of stringsafe converters. */
3462 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003463 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003464 return 0;
3465 lua_settable(L, -3);
3466
3467 /* Pop a class stream metatable and affect it to the table. */
3468 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3469 lua_setmetatable(L, -2);
3470
3471 return 1;
3472}
3473
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003474__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3475{
3476 struct hlua_appctx *appctx;
3477 struct stream *s;
3478 const char *name;
3479 size_t len;
3480 struct sample smp;
3481
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003482 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3483 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003484
3485 /* It is useles to retrieve the stream, but this function
3486 * runs only in a stream context.
3487 */
3488 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3489 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3490 s = appctx->htxn.s;
3491
3492 /* Converts the third argument in a sample. */
3493 hlua_lua2smp(L, 3, &smp);
3494
3495 /* Store the sample in a variable. */
3496 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003497
3498 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3499 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3500 else
3501 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3502
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003503 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003504}
3505
3506__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3507{
3508 struct hlua_appctx *appctx;
3509 struct stream *s;
3510 const char *name;
3511 size_t len;
3512 struct sample smp;
3513
3514 MAY_LJMP(check_args(L, 2, "unset_var"));
3515
3516 /* It is useles to retrieve the stream, but this function
3517 * runs only in a stream context.
3518 */
3519 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3520 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3521 s = appctx->htxn.s;
3522
3523 /* Unset the variable. */
3524 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003525 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3526 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003527}
3528
3529__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3530{
3531 struct hlua_appctx *appctx;
3532 struct stream *s;
3533 const char *name;
3534 size_t len;
3535 struct sample smp;
3536
3537 MAY_LJMP(check_args(L, 2, "get_var"));
3538
3539 /* It is useles to retrieve the stream, but this function
3540 * runs only in a stream context.
3541 */
3542 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3543 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3544 s = appctx->htxn.s;
3545
3546 smp_set_owner(&smp, s->be, s->sess, s, 0);
3547 if (!vars_get_by_name(name, len, &smp)) {
3548 lua_pushnil(L);
3549 return 1;
3550 }
3551
3552 return hlua_smp2lua(L, &smp);
3553}
3554
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003555__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3556{
3557 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3558 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003559 struct hlua *hlua;
3560
3561 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003562 if (!s->hlua)
3563 return 0;
3564 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003565
3566 MAY_LJMP(check_args(L, 2, "set_priv"));
3567
3568 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003569 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003570
3571 /* Get and store new value. */
3572 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3573 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3574
3575 return 0;
3576}
3577
3578__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3579{
3580 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3581 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003582 struct hlua *hlua;
3583
3584 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003585 if (!s->hlua) {
3586 lua_pushnil(L);
3587 return 1;
3588 }
3589 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003590
3591 /* Push configuration index in the stack. */
3592 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3593
3594 return 1;
3595}
3596
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003597/* If expected data not yet available, it returns a yield. This function
3598 * consumes the data in the buffer. It returns a string containing the
3599 * data. This string can be empty.
3600 */
3601__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3602{
3603 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3604 struct stream_interface *si = appctx->appctx->owner;
3605 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003606 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003607 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003608 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003609 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003610
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003611 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003612 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003613
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003614 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003615 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003616 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003617 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003618 }
3619
3620 /* End of data: commit the total strings and return. */
3621 if (ret < 0) {
3622 luaL_pushresult(&appctx->b);
3623 return 1;
3624 }
3625
3626 /* Ensure that the block 2 length is usable. */
3627 if (ret == 1)
3628 len2 = 0;
3629
3630 /* dont check the max length read and dont check. */
3631 luaL_addlstring(&appctx->b, blk1, len1);
3632 luaL_addlstring(&appctx->b, blk2, len2);
3633
3634 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003635 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003636 luaL_pushresult(&appctx->b);
3637 return 1;
3638}
3639
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003640/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003641__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3642{
3643 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3644
3645 /* Initialise the string catenation. */
3646 luaL_buffinit(L, &appctx->b);
3647
3648 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3649}
3650
3651/* If expected data not yet available, it returns a yield. This function
3652 * consumes the data in the buffer. It returns a string containing the
3653 * data. This string can be empty.
3654 */
3655__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3656{
3657 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3658 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003659 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003660 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003661 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003662 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003663 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003664 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003665
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003666 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003667 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003668
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003669 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003670 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003671 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003672 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003673 }
3674
3675 /* End of data: commit the total strings and return. */
3676 if (ret < 0) {
3677 luaL_pushresult(&appctx->b);
3678 return 1;
3679 }
3680
3681 /* Ensure that the block 2 length is usable. */
3682 if (ret == 1)
3683 len2 = 0;
3684
3685 if (len == -1) {
3686
3687 /* If len == -1, catenate all the data avalaile and
3688 * yield because we want to get all the data until
3689 * the end of data stream.
3690 */
3691 luaL_addlstring(&appctx->b, blk1, len1);
3692 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003693 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003694 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003695 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003696
3697 } else {
3698
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003699 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003700 if (len1 > len)
3701 len1 = len;
3702 luaL_addlstring(&appctx->b, blk1, len1);
3703 len -= len1;
3704
3705 /* Copy the second block. */
3706 if (len2 > len)
3707 len2 = len;
3708 luaL_addlstring(&appctx->b, blk2, len2);
3709 len -= len2;
3710
3711 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003712 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003713
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003714 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003715 if (len > 0) {
3716 lua_pushinteger(L, len);
3717 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003718 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003719 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003720 }
3721
3722 /* return the result. */
3723 luaL_pushresult(&appctx->b);
3724 return 1;
3725 }
3726
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003727 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003728 hlua_pusherror(L, "Lua: internal error");
3729 WILL_LJMP(lua_error(L));
3730 return 0;
3731}
3732
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003733/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003734__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3735{
3736 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3737 int len = -1;
3738
3739 if (lua_gettop(L) > 2)
3740 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3741 if (lua_gettop(L) >= 2) {
3742 len = MAY_LJMP(luaL_checkinteger(L, 2));
3743 lua_pop(L, 1);
3744 }
3745
3746 /* Confirm or set the required length */
3747 lua_pushinteger(L, len);
3748
3749 /* Initialise the string catenation. */
3750 luaL_buffinit(L, &appctx->b);
3751
3752 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3753}
3754
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003755/* Append data in the output side of the buffer. This data is immediately
3756 * sent. The function returns the amount of data written. If the buffer
3757 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003758 * if the channel is closed.
3759 */
3760__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3761{
3762 size_t len;
3763 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3764 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3765 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3766 struct stream_interface *si = appctx->appctx->owner;
3767 struct channel *chn = si_ic(si);
3768 int max;
3769
3770 /* Get the max amount of data which can write as input in the channel. */
3771 max = channel_recv_max(chn);
3772 if (max > (len - l))
3773 max = len - l;
3774
3775 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003776 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003777
3778 /* update counters. */
3779 l += max;
3780 lua_pop(L, 1);
3781 lua_pushinteger(L, l);
3782
3783 /* If some data is not send, declares the situation to the
3784 * applet, and returns a yield.
3785 */
3786 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003787 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003788 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003789 }
3790
3791 return 1;
3792}
3793
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003794/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003795 * yield the LUA process, and resume it without checking the
3796 * input arguments.
3797 */
3798__LJMP static int hlua_applet_tcp_send(lua_State *L)
3799{
3800 MAY_LJMP(check_args(L, 2, "send"));
3801 lua_pushinteger(L, 0);
3802
3803 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3804}
3805
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003806/*
3807 *
3808 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003809 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003810 *
3811 *
3812 */
3813
3814/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003815 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003816 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003817__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003818{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003819 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003820}
3821
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003822/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003823 * according with a current TXN.
3824 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003825static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003826{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003827 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003828 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003829 struct stream_interface *si = ctx->owner;
3830 struct stream *s = si_strm(si);
3831 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003832 struct htx *htx;
3833 struct htx_blk *blk;
3834 struct htx_sl *sl;
3835 struct ist path;
3836 unsigned long long len = 0;
3837 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003838
3839 /* Check stack size. */
3840 if (!lua_checkstack(L, 3))
3841 return 0;
3842
3843 /* Create the object: obj[0] = userdata.
3844 * Note that the base of the Converters object is the
3845 * same than the TXN object.
3846 */
3847 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003848 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003849 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003850 appctx->appctx = ctx;
3851 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003852 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003853 appctx->htxn.s = s;
3854 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003855
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003856 /* Create the "f" field that contains a list of fetches. */
3857 lua_pushstring(L, "f");
3858 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3859 return 0;
3860 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003861
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003862 /* Create the "sf" field that contains a list of stringsafe fetches. */
3863 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003864 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003865 return 0;
3866 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003867
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003868 /* Create the "c" field that contains a list of converters. */
3869 lua_pushstring(L, "c");
3870 if (!hlua_converters_new(L, &appctx->htxn, 0))
3871 return 0;
3872 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003873
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003874 /* Create the "sc" field that contains a list of stringsafe converters. */
3875 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003876 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003877 return 0;
3878 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003879
Christopher Fauleta2097962019-07-15 16:25:33 +02003880 htx = htxbuf(&s->req.buf);
3881 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003882 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003883 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003884
Christopher Fauleta2097962019-07-15 16:25:33 +02003885 /* Stores the request method. */
3886 lua_pushstring(L, "method");
3887 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3888 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003889
Christopher Fauleta2097962019-07-15 16:25:33 +02003890 /* Stores the http version. */
3891 lua_pushstring(L, "version");
3892 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3893 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003894
Christopher Fauleta2097962019-07-15 16:25:33 +02003895 /* creates an array of headers. hlua_http_get_headers() crates and push
3896 * the array on the top of the stack.
3897 */
3898 lua_pushstring(L, "headers");
3899 htxn.s = s;
3900 htxn.p = px;
3901 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01003902 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02003903 return 0;
3904 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003905
Christopher Fauleta2097962019-07-15 16:25:33 +02003906 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01003907 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02003908 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003909
Christopher Fauleta2097962019-07-15 16:25:33 +02003910 p = path.ptr;
3911 end = path.ptr + path.len;
3912 q = p;
3913 while (q < end && *q != '?')
3914 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003915
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003916 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003917 lua_pushstring(L, "path");
3918 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003919 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003920
Christopher Fauleta2097962019-07-15 16:25:33 +02003921 /* Stores the query string. */
3922 lua_pushstring(L, "qs");
3923 if (*q == '?')
3924 q++;
3925 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003926 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003927 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003928
Christopher Fauleta2097962019-07-15 16:25:33 +02003929 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3930 struct htx_blk *blk = htx_get_blk(htx, pos);
3931 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003932
Christopher Fauleta2097962019-07-15 16:25:33 +02003933 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3934 break;
3935 if (type == HTX_BLK_DATA)
3936 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003937 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003938 if (htx->extra != ULLONG_MAX)
3939 len += htx->extra;
3940
3941 /* Stores the request path. */
3942 lua_pushstring(L, "length");
3943 lua_pushinteger(L, len);
3944 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003945
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003946 /* Create an empty array of HTTP request headers. */
3947 lua_pushstring(L, "response");
3948 lua_newtable(L);
3949 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003950
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003951 /* Pop a class stream metatable and affect it to the table. */
3952 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3953 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003954
3955 return 1;
3956}
3957
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003958__LJMP static int hlua_applet_http_set_var(lua_State *L)
3959{
3960 struct hlua_appctx *appctx;
3961 struct stream *s;
3962 const char *name;
3963 size_t len;
3964 struct sample smp;
3965
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003966 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3967 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003968
3969 /* It is useles to retrieve the stream, but this function
3970 * runs only in a stream context.
3971 */
3972 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3973 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3974 s = appctx->htxn.s;
3975
3976 /* Converts the third argument in a sample. */
3977 hlua_lua2smp(L, 3, &smp);
3978
3979 /* Store the sample in a variable. */
3980 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003981
3982 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3983 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3984 else
3985 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3986
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003987 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003988}
3989
3990__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3991{
3992 struct hlua_appctx *appctx;
3993 struct stream *s;
3994 const char *name;
3995 size_t len;
3996 struct sample smp;
3997
3998 MAY_LJMP(check_args(L, 2, "unset_var"));
3999
4000 /* It is useles to retrieve the stream, but this function
4001 * runs only in a stream context.
4002 */
4003 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4004 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4005 s = appctx->htxn.s;
4006
4007 /* Unset the variable. */
4008 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004009 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4010 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004011}
4012
4013__LJMP static int hlua_applet_http_get_var(lua_State *L)
4014{
4015 struct hlua_appctx *appctx;
4016 struct stream *s;
4017 const char *name;
4018 size_t len;
4019 struct sample smp;
4020
4021 MAY_LJMP(check_args(L, 2, "get_var"));
4022
4023 /* It is useles to retrieve the stream, but this function
4024 * runs only in a stream context.
4025 */
4026 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4027 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4028 s = appctx->htxn.s;
4029
4030 smp_set_owner(&smp, s->be, s->sess, s, 0);
4031 if (!vars_get_by_name(name, len, &smp)) {
4032 lua_pushnil(L);
4033 return 1;
4034 }
4035
4036 return hlua_smp2lua(L, &smp);
4037}
4038
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004039__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4040{
4041 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4042 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004043 struct hlua *hlua;
4044
4045 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004046 if (!s->hlua)
4047 return 0;
4048 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004049
4050 MAY_LJMP(check_args(L, 2, "set_priv"));
4051
4052 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004053 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004054
4055 /* Get and store new value. */
4056 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4057 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4058
4059 return 0;
4060}
4061
4062__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4063{
4064 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4065 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004066 struct hlua *hlua;
4067
4068 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004069 if (!s->hlua) {
4070 lua_pushnil(L);
4071 return 1;
4072 }
4073 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004074
4075 /* Push configuration index in the stack. */
4076 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4077
4078 return 1;
4079}
4080
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004081/* If expected data not yet available, it returns a yield. This function
4082 * consumes the data in the buffer. It returns a string containing the
4083 * data. This string can be empty.
4084 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004085__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004086{
4087 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4088 struct stream_interface *si = appctx->appctx->owner;
4089 struct channel *req = si_oc(si);
4090 struct htx *htx;
4091 struct htx_blk *blk;
4092 size_t count;
4093 int stop = 0;
4094
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004095 htx = htx_from_buf(&req->buf);
4096 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004097 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004098
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004099 while (count && !stop && blk) {
4100 enum htx_blk_type type = htx_get_blk_type(blk);
4101 uint32_t sz = htx_get_blksz(blk);
4102 struct ist v;
4103 uint32_t vlen;
4104 char *nl;
4105
Christopher Faulete461e342018-12-18 16:43:35 +01004106 if (type == HTX_BLK_EOM) {
4107 stop = 1;
4108 break;
4109 }
4110
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004111 vlen = sz;
4112 if (vlen > count) {
4113 if (type != HTX_BLK_DATA)
4114 break;
4115 vlen = count;
4116 }
4117
4118 switch (type) {
4119 case HTX_BLK_UNUSED:
4120 break;
4121
4122 case HTX_BLK_DATA:
4123 v = htx_get_blk_value(htx, blk);
4124 v.len = vlen;
4125 nl = istchr(v, '\n');
4126 if (nl != NULL) {
4127 stop = 1;
4128 vlen = nl - v.ptr + 1;
4129 }
4130 luaL_addlstring(&appctx->b, v.ptr, vlen);
4131 break;
4132
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004133 case HTX_BLK_TLR:
4134 case HTX_BLK_EOM:
4135 stop = 1;
4136 break;
4137
4138 default:
4139 break;
4140 }
4141
4142 co_set_data(req, co_data(req) - vlen);
4143 count -= vlen;
4144 if (sz == vlen)
4145 blk = htx_remove_blk(htx, blk);
4146 else {
4147 htx_cut_data_blk(htx, blk, vlen);
4148 break;
4149 }
4150 }
4151
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004152 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004153 if (!stop) {
4154 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004155 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004156 }
4157
4158 /* return the result. */
4159 luaL_pushresult(&appctx->b);
4160 return 1;
4161}
4162
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004163
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004164/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004165__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004166{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004167 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004168
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004169 /* Initialise the string catenation. */
4170 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004171
Christopher Fauleta2097962019-07-15 16:25:33 +02004172 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004173}
4174
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004175/* If expected data not yet available, it returns a yield. This function
4176 * consumes the data in the buffer. It returns a string containing the
4177 * data. This string can be empty.
4178 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004179__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004180{
4181 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4182 struct stream_interface *si = appctx->appctx->owner;
4183 struct channel *req = si_oc(si);
4184 struct htx *htx;
4185 struct htx_blk *blk;
4186 size_t count;
4187 int len;
4188
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004189 htx = htx_from_buf(&req->buf);
4190 len = MAY_LJMP(luaL_checkinteger(L, 2));
4191 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004192 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004193 while (count && len && blk) {
4194 enum htx_blk_type type = htx_get_blk_type(blk);
4195 uint32_t sz = htx_get_blksz(blk);
4196 struct ist v;
4197 uint32_t vlen;
4198
Christopher Faulete461e342018-12-18 16:43:35 +01004199 if (type == HTX_BLK_EOM) {
4200 len = 0;
4201 break;
4202 }
4203
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004204 vlen = sz;
4205 if (len > 0 && vlen > len)
4206 vlen = len;
4207 if (vlen > count) {
4208 if (type != HTX_BLK_DATA)
4209 break;
4210 vlen = count;
4211 }
4212
4213 switch (type) {
4214 case HTX_BLK_UNUSED:
4215 break;
4216
4217 case HTX_BLK_DATA:
4218 v = htx_get_blk_value(htx, blk);
4219 luaL_addlstring(&appctx->b, v.ptr, vlen);
4220 break;
4221
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004222 case HTX_BLK_TLR:
4223 case HTX_BLK_EOM:
4224 len = 0;
4225 break;
4226
4227 default:
4228 break;
4229 }
4230
4231 co_set_data(req, co_data(req) - vlen);
4232 count -= vlen;
4233 if (len > 0)
4234 len -= vlen;
4235 if (sz == vlen)
4236 blk = htx_remove_blk(htx, blk);
4237 else {
4238 htx_cut_data_blk(htx, blk, vlen);
4239 break;
4240 }
4241 }
4242
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004243 htx_to_buf(htx, &req->buf);
4244
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004245 /* If we are no other data available, yield waiting for new data. */
4246 if (len) {
4247 if (len > 0) {
4248 lua_pushinteger(L, len);
4249 lua_replace(L, 2);
4250 }
4251 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004252 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004253 }
4254
4255 /* return the result. */
4256 luaL_pushresult(&appctx->b);
4257 return 1;
4258}
4259
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004260/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004261__LJMP static int hlua_applet_http_recv(lua_State *L)
4262{
4263 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4264 int len = -1;
4265
4266 /* Check arguments. */
4267 if (lua_gettop(L) > 2)
4268 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4269 if (lua_gettop(L) >= 2) {
4270 len = MAY_LJMP(luaL_checkinteger(L, 2));
4271 lua_pop(L, 1);
4272 }
4273
Christopher Fauleta2097962019-07-15 16:25:33 +02004274 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004275
Christopher Fauleta2097962019-07-15 16:25:33 +02004276 /* Initialise the string catenation. */
4277 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004278
Christopher Fauleta2097962019-07-15 16:25:33 +02004279 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004280}
4281
4282/* Append data in the output side of the buffer. This data is immediately
4283 * sent. The function returns the amount of data written. If the buffer
4284 * cannot contain the data, the function yields. The function returns -1
4285 * if the channel is closed.
4286 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004287__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004288{
4289 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4290 struct stream_interface *si = appctx->appctx->owner;
4291 struct channel *res = si_ic(si);
4292 struct htx *htx = htx_from_buf(&res->buf);
4293 const char *data;
4294 size_t len;
4295 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4296 int max;
4297
Christopher Faulet9060fc02019-07-03 11:39:30 +02004298 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004299 if (!max)
4300 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004301
4302 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4303
4304 /* Get the max amount of data which can write as input in the channel. */
4305 if (max > (len - l))
4306 max = len - l;
4307
4308 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004309 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004310 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004311
4312 /* update counters. */
4313 l += max;
4314 lua_pop(L, 1);
4315 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004316
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004317 /* If some data is not send, declares the situation to the
4318 * applet, and returns a yield.
4319 */
4320 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004321 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004322 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004323 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004324 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004325 }
4326
Christopher Fauleta2097962019-07-15 16:25:33 +02004327 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004328 return 1;
4329}
4330
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004331/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004332 * yield the LUA process, and resume it without checking the
4333 * input arguments.
4334 */
4335__LJMP static int hlua_applet_http_send(lua_State *L)
4336{
4337 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004338
4339 /* We want to send some data. Headers must be sent. */
4340 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4341 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4342 WILL_LJMP(lua_error(L));
4343 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004344
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004345 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004346 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004347
Christopher Fauleta2097962019-07-15 16:25:33 +02004348 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004349}
4350
4351__LJMP static int hlua_applet_http_addheader(lua_State *L)
4352{
4353 const char *name;
4354 int ret;
4355
4356 MAY_LJMP(hlua_checkapplet_http(L, 1));
4357 name = MAY_LJMP(luaL_checkstring(L, 2));
4358 MAY_LJMP(luaL_checkstring(L, 3));
4359
4360 /* Push in the stack the "response" entry. */
4361 ret = lua_getfield(L, 1, "response");
4362 if (ret != LUA_TTABLE) {
4363 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4364 "is expected as an array. %s found", lua_typename(L, ret));
4365 WILL_LJMP(lua_error(L));
4366 }
4367
4368 /* check if the header is already registered if it is not
4369 * the case, register it.
4370 */
4371 ret = lua_getfield(L, -1, name);
4372 if (ret == LUA_TNIL) {
4373
4374 /* Entry not found. */
4375 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4376
4377 /* Insert the new header name in the array in the top of the stack.
4378 * It left the new array in the top of the stack.
4379 */
4380 lua_newtable(L);
4381 lua_pushvalue(L, 2);
4382 lua_pushvalue(L, -2);
4383 lua_settable(L, -4);
4384
4385 } else if (ret != LUA_TTABLE) {
4386
4387 /* corruption error. */
4388 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4389 "is expected as an array. %s found", name, lua_typename(L, ret));
4390 WILL_LJMP(lua_error(L));
4391 }
4392
4393 /* Now the top od thestack is an array of values. We push
4394 * the header value as new entry.
4395 */
4396 lua_pushvalue(L, 3);
4397 ret = lua_rawlen(L, -2);
4398 lua_rawseti(L, -2, ret + 1);
4399 lua_pushboolean(L, 1);
4400 return 1;
4401}
4402
4403__LJMP static int hlua_applet_http_status(lua_State *L)
4404{
4405 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4406 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004407 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004408
4409 if (status < 100 || status > 599) {
4410 lua_pushboolean(L, 0);
4411 return 1;
4412 }
4413
4414 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004415 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004416 lua_pushboolean(L, 1);
4417 return 1;
4418}
4419
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004420
Christopher Fauleta2097962019-07-15 16:25:33 +02004421__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004422{
4423 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4424 struct stream_interface *si = appctx->appctx->owner;
4425 struct channel *res = si_ic(si);
4426 struct htx *htx;
4427 struct htx_sl *sl;
4428 struct h1m h1m;
4429 const char *status, *reason;
4430 const char *name, *value;
4431 size_t nlen, vlen;
4432 unsigned int flags;
4433
4434 /* Send the message at once. */
4435 htx = htx_from_buf(&res->buf);
4436 h1m_init_res(&h1m);
4437
4438 /* Use the same http version than the request. */
4439 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4440 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4441 if (reason == NULL)
4442 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4443 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4444 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4445 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4446 }
4447 else {
4448 flags = HTX_SL_F_IS_RESP;
4449 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4450 }
4451 if (!sl) {
4452 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4453 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4454 WILL_LJMP(lua_error(L));
4455 }
4456 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4457
4458 /* Get the array associated to the field "response" in the object AppletHTTP. */
4459 lua_pushvalue(L, 0);
4460 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4461 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4462 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4463 WILL_LJMP(lua_error(L));
4464 }
4465
4466 /* Browse the list of headers. */
4467 lua_pushnil(L);
4468 while(lua_next(L, -2) != 0) {
4469 /* We expect a string as -2. */
4470 if (lua_type(L, -2) != LUA_TSTRING) {
4471 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4472 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4473 lua_typename(L, lua_type(L, -2)));
4474 WILL_LJMP(lua_error(L));
4475 }
4476 name = lua_tolstring(L, -2, &nlen);
4477
4478 /* We expect an array as -1. */
4479 if (lua_type(L, -1) != LUA_TTABLE) {
4480 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4481 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4482 name,
4483 lua_typename(L, lua_type(L, -1)));
4484 WILL_LJMP(lua_error(L));
4485 }
4486
4487 /* Browse the table who is on the top of the stack. */
4488 lua_pushnil(L);
4489 while(lua_next(L, -2) != 0) {
4490 int id;
4491
4492 /* We expect a number as -2. */
4493 if (lua_type(L, -2) != LUA_TNUMBER) {
4494 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4495 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4496 name,
4497 lua_typename(L, lua_type(L, -2)));
4498 WILL_LJMP(lua_error(L));
4499 }
4500 id = lua_tointeger(L, -2);
4501
4502 /* We expect a string as -2. */
4503 if (lua_type(L, -1) != LUA_TSTRING) {
4504 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4505 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4506 name, id,
4507 lua_typename(L, lua_type(L, -1)));
4508 WILL_LJMP(lua_error(L));
4509 }
4510 value = lua_tolstring(L, -1, &vlen);
4511
4512 /* Simple Protocol checks. */
4513 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004514 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004515 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4516 struct ist v = ist2(value, vlen);
4517 int ret;
4518
4519 ret = h1_parse_cont_len_header(&h1m, &v);
4520 if (ret < 0) {
4521 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4522 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4523 name);
4524 WILL_LJMP(lua_error(L));
4525 }
4526 else if (ret == 0)
4527 goto next; /* Skip it */
4528 }
4529
4530 /* Add a new header */
4531 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4532 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4533 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4534 name);
4535 WILL_LJMP(lua_error(L));
4536 }
4537 next:
4538 /* Remove the array from the stack, and get next element with a remaining string. */
4539 lua_pop(L, 1);
4540 }
4541
4542 /* Remove the array from the stack, and get next element with a remaining string. */
4543 lua_pop(L, 1);
4544 }
4545
4546 if (h1m.flags & H1_MF_CHNK)
4547 h1m.flags &= ~H1_MF_CLEN;
4548 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4549 h1m.flags |= H1_MF_XFER_LEN;
4550
4551 /* Uset HTX start-line flags */
4552 if (h1m.flags & H1_MF_XFER_ENC)
4553 flags |= HTX_SL_F_XFER_ENC;
4554 if (h1m.flags & H1_MF_XFER_LEN) {
4555 flags |= HTX_SL_F_XFER_LEN;
4556 if (h1m.flags & H1_MF_CHNK)
4557 flags |= HTX_SL_F_CHNK;
4558 else if (h1m.flags & H1_MF_CLEN)
4559 flags |= HTX_SL_F_CLEN;
4560 if (h1m.body_len == 0)
4561 flags |= HTX_SL_F_BODYLESS;
4562 }
4563 sl->flags |= flags;
4564
4565 /* If we dont have a content-length set, and the HTTP version is 1.1
4566 * and the status code implies the presence of a message body, we must
4567 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004568 * for the keepalive compliance. If the applet announces a transfer-encoding
4569 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004570 */
4571 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4572 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4573 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4574 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4575 /* Add a new header */
4576 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4577 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4578 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4579 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4580 WILL_LJMP(lua_error(L));
4581 }
4582 }
4583
4584 /* Finalize headers. */
4585 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4586 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4587 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4588 WILL_LJMP(lua_error(L));
4589 }
4590
4591 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4592 b_reset(&res->buf);
4593 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4594 WILL_LJMP(lua_error(L));
4595 }
4596
4597 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004598 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004599
4600 /* Headers sent, set the flag. */
4601 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4602 return 0;
4603
4604}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004605/* We will build the status line and the headers of the HTTP response.
4606 * We will try send at once if its not possible, we give back the hand
4607 * waiting for more room.
4608 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004609__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004610{
4611 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4612 struct stream_interface *si = appctx->appctx->owner;
4613 struct channel *res = si_ic(si);
4614
4615 if (co_data(res)) {
4616 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004617 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004618 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004619 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004620}
4621
4622
Christopher Fauleta2097962019-07-15 16:25:33 +02004623__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004624{
Christopher Fauleta2097962019-07-15 16:25:33 +02004625 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004626}
4627
Christopher Fauleta2097962019-07-15 16:25:33 +02004628/*
4629 *
4630 *
4631 * Class HTTP
4632 *
4633 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004634 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004635
4636/* Returns a struct hlua_txn if the stack entry "ud" is
4637 * a class stream, otherwise it throws an error.
4638 */
4639__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004640{
Christopher Fauleta2097962019-07-15 16:25:33 +02004641 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4642}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004643
Christopher Fauleta2097962019-07-15 16:25:33 +02004644/* This function creates and push in the stack a HTTP object
4645 * according with a current TXN.
4646 */
4647static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4648{
4649 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004650
Christopher Fauleta2097962019-07-15 16:25:33 +02004651 /* Check stack size. */
4652 if (!lua_checkstack(L, 3))
4653 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004654
Christopher Fauleta2097962019-07-15 16:25:33 +02004655 /* Create the object: obj[0] = userdata.
4656 * Note that the base of the Converters object is the
4657 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004658 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004659 lua_newtable(L);
4660 htxn = lua_newuserdata(L, sizeof(*htxn));
4661 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004662
4663 htxn->s = txn->s;
4664 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004665 htxn->dir = txn->dir;
4666 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004667
4668 /* Pop a class stream metatable and affect it to the table. */
4669 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4670 lua_setmetatable(L, -2);
4671
4672 return 1;
4673}
4674
4675/* This function creates ans returns an array of HTTP headers.
4676 * This function does not fails. It is used as wrapper with the
4677 * 2 following functions.
4678 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004679__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004680{
Christopher Fauleta2097962019-07-15 16:25:33 +02004681 struct htx *htx;
4682 int32_t pos;
4683
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004684 /* Create the table. */
4685 lua_newtable(L);
4686
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004687
Christopher Fauleta2097962019-07-15 16:25:33 +02004688 htx = htxbuf(&msg->chn->buf);
4689 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4690 struct htx_blk *blk = htx_get_blk(htx, pos);
4691 enum htx_blk_type type = htx_get_blk_type(blk);
4692 struct ist n, v;
4693 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004694
Christopher Fauleta2097962019-07-15 16:25:33 +02004695 if (type == HTX_BLK_HDR) {
4696 n = htx_get_blk_name(htx,blk);
4697 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004698 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004699 else if (type == HTX_BLK_EOH)
4700 break;
4701 else
4702 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004703
Christopher Fauleta2097962019-07-15 16:25:33 +02004704 /* Check for existing entry:
4705 * assume that the table is on the top of the stack, and
4706 * push the key in the stack, the function lua_gettable()
4707 * perform the lookup.
4708 */
4709 lua_pushlstring(L, n.ptr, n.len);
4710 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004711
Christopher Fauleta2097962019-07-15 16:25:33 +02004712 switch (lua_type(L, -1)) {
4713 case LUA_TNIL:
4714 /* Table not found, create it. */
4715 lua_pop(L, 1); /* remove the nil value. */
4716 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4717 lua_newtable(L); /* create and push empty table. */
4718 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4719 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4720 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004721 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004722
Christopher Fauleta2097962019-07-15 16:25:33 +02004723 case LUA_TTABLE:
4724 /* Entry found: push the value in the table. */
4725 len = lua_rawlen(L, -1);
4726 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4727 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4728 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4729 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004730
Christopher Fauleta2097962019-07-15 16:25:33 +02004731 default:
4732 /* Other cases are errors. */
4733 hlua_pusherror(L, "internal error during the parsing of headers.");
4734 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004735 }
4736 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004737 return 1;
4738}
4739
4740__LJMP static int hlua_http_req_get_headers(lua_State *L)
4741{
4742 struct hlua_txn *htxn;
4743
4744 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4745 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4746
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004747 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004748 WILL_LJMP(lua_error(L));
4749
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004750 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004751}
4752
4753__LJMP static int hlua_http_res_get_headers(lua_State *L)
4754{
4755 struct hlua_txn *htxn;
4756
4757 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4758 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4759
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004760 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004761 WILL_LJMP(lua_error(L));
4762
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004763 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004764}
4765
4766/* This function replace full header, or just a value in
4767 * the request or in the response. It is a wrapper fir the
4768 * 4 following functions.
4769 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004770__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004771{
4772 size_t name_len;
4773 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4774 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4775 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004776 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004777 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004778
Dragan Dosen26743032019-04-30 15:54:36 +02004779 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004780 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4781
Christopher Fauleta2097962019-07-15 16:25:33 +02004782 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004783 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004784 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004785 return 0;
4786}
4787
4788__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4789{
4790 struct hlua_txn *htxn;
4791
4792 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4793 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4794
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004795 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004796 WILL_LJMP(lua_error(L));
4797
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004798 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004799}
4800
4801__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4802{
4803 struct hlua_txn *htxn;
4804
4805 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4806 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4807
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004808 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004809 WILL_LJMP(lua_error(L));
4810
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004811 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004812}
4813
4814__LJMP static int hlua_http_req_rep_val(lua_State *L)
4815{
4816 struct hlua_txn *htxn;
4817
4818 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4819 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4820
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004821 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004822 WILL_LJMP(lua_error(L));
4823
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004824 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004825}
4826
4827__LJMP static int hlua_http_res_rep_val(lua_State *L)
4828{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004829 struct hlua_txn *htxn;
4830
4831 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4832 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4833
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004834 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004835 WILL_LJMP(lua_error(L));
4836
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004837 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004838}
4839
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004840/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004841 * It is a wrapper for the 2 following functions.
4842 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004843__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004844{
4845 size_t len;
4846 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004847 struct htx *htx = htxbuf(&msg->chn->buf);
4848 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004849
Christopher Fauleta2097962019-07-15 16:25:33 +02004850 ctx.blk = NULL;
4851 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4852 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004853 return 0;
4854}
4855
4856__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4857{
4858 struct hlua_txn *htxn;
4859
4860 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4861 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4862
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004863 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004864 WILL_LJMP(lua_error(L));
4865
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004866 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004867}
4868
4869__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4870{
4871 struct hlua_txn *htxn;
4872
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004873 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004874 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4875
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004876 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004877 WILL_LJMP(lua_error(L));
4878
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004879 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004880}
4881
4882/* This function adds an header. It is a wrapper used by
4883 * the 2 following functions.
4884 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004885__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004886{
4887 size_t name_len;
4888 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4889 size_t value_len;
4890 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004891 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004892
Christopher Fauleta2097962019-07-15 16:25:33 +02004893 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4894 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004895 return 0;
4896}
4897
4898__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4899{
4900 struct hlua_txn *htxn;
4901
4902 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4903 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4904
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004905 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004906 WILL_LJMP(lua_error(L));
4907
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004908 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004909}
4910
4911__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4912{
4913 struct hlua_txn *htxn;
4914
4915 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4916 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4917
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004918 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004919 WILL_LJMP(lua_error(L));
4920
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004921 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004922}
4923
4924static int hlua_http_req_set_hdr(lua_State *L)
4925{
4926 struct hlua_txn *htxn;
4927
4928 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4929 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4930
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004931 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004932 WILL_LJMP(lua_error(L));
4933
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004934 hlua_http_del_hdr(L, &htxn->s->txn->req);
4935 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004936}
4937
4938static int hlua_http_res_set_hdr(lua_State *L)
4939{
4940 struct hlua_txn *htxn;
4941
4942 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4943 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4944
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004945 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004946 WILL_LJMP(lua_error(L));
4947
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004948 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
4949 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004950}
4951
4952/* This function set the method. */
4953static int hlua_http_req_set_meth(lua_State *L)
4954{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004955 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004956 size_t name_len;
4957 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004958
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004959 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004960 WILL_LJMP(lua_error(L));
4961
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004962 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004963 return 1;
4964}
4965
4966/* This function set the method. */
4967static int hlua_http_req_set_path(lua_State *L)
4968{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004969 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004970 size_t name_len;
4971 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004972
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004973 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004974 WILL_LJMP(lua_error(L));
4975
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004976 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004977 return 1;
4978}
4979
4980/* This function set the query-string. */
4981static int hlua_http_req_set_query(lua_State *L)
4982{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004983 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004984 size_t name_len;
4985 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004986
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004987 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004988 WILL_LJMP(lua_error(L));
4989
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004990 /* Check length. */
4991 if (name_len > trash.size - 1) {
4992 lua_pushboolean(L, 0);
4993 return 1;
4994 }
4995
4996 /* Add the mark question as prefix. */
4997 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004998 trash.area[trash.data++] = '?';
4999 memcpy(trash.area + trash.data, name, name_len);
5000 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005001
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005002 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005003 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005004 return 1;
5005}
5006
5007/* This function set the uri. */
5008static int hlua_http_req_set_uri(lua_State *L)
5009{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005010 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005011 size_t name_len;
5012 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005013
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005014 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005015 WILL_LJMP(lua_error(L));
5016
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005017 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005018 return 1;
5019}
5020
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005021/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005022static int hlua_http_res_set_status(lua_State *L)
5023{
5024 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5025 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005026 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5027 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005028
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005029 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005030 WILL_LJMP(lua_error(L));
5031
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005032 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005033 return 0;
5034}
5035
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005036/*
5037 *
5038 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005039 * Class TXN
5040 *
5041 *
5042 */
5043
5044/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005045 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005046 */
5047__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5048{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005049 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005050}
5051
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005052__LJMP static int hlua_set_var(lua_State *L)
5053{
5054 struct hlua_txn *htxn;
5055 const char *name;
5056 size_t len;
5057 struct sample smp;
5058
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005059 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5060 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005061
5062 /* It is useles to retrieve the stream, but this function
5063 * runs only in a stream context.
5064 */
5065 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5066 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5067
5068 /* Converts the third argument in a sample. */
5069 hlua_lua2smp(L, 3, &smp);
5070
5071 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005072 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005073
5074 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5075 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5076 else
5077 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5078
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005079 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005080}
5081
Christopher Faulet85d79c92016-11-09 16:54:56 +01005082__LJMP static int hlua_unset_var(lua_State *L)
5083{
5084 struct hlua_txn *htxn;
5085 const char *name;
5086 size_t len;
5087 struct sample smp;
5088
5089 MAY_LJMP(check_args(L, 2, "unset_var"));
5090
5091 /* It is useles to retrieve the stream, but this function
5092 * runs only in a stream context.
5093 */
5094 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5095 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5096
5097 /* Unset the variable. */
5098 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005099 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5100 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005101}
5102
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005103__LJMP static int hlua_get_var(lua_State *L)
5104{
5105 struct hlua_txn *htxn;
5106 const char *name;
5107 size_t len;
5108 struct sample smp;
5109
5110 MAY_LJMP(check_args(L, 2, "get_var"));
5111
5112 /* It is useles to retrieve the stream, but this function
5113 * runs only in a stream context.
5114 */
5115 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5116 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5117
Willy Tarreau7560dd42016-03-10 16:28:58 +01005118 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005119 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005120 lua_pushnil(L);
5121 return 1;
5122 }
5123
5124 return hlua_smp2lua(L, &smp);
5125}
5126
Willy Tarreau59551662015-03-10 14:23:13 +01005127__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005128{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005129 struct hlua *hlua;
5130
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005131 MAY_LJMP(check_args(L, 2, "set_priv"));
5132
Willy Tarreau87b09662015-04-03 00:22:06 +02005133 /* It is useles to retrieve the stream, but this function
5134 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005135 */
5136 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005137 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005138
5139 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005140 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005141
5142 /* Get and store new value. */
5143 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5144 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5145
5146 return 0;
5147}
5148
Willy Tarreau59551662015-03-10 14:23:13 +01005149__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005150{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005151 struct hlua *hlua;
5152
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005153 MAY_LJMP(check_args(L, 1, "get_priv"));
5154
Willy Tarreau87b09662015-04-03 00:22:06 +02005155 /* It is useles to retrieve the stream, but this function
5156 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005157 */
5158 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005159 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005160
5161 /* Push configuration index in the stack. */
5162 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5163
5164 return 1;
5165}
5166
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005167/* Create stack entry containing a class TXN. This function
5168 * return 0 if the stack does not contains free slots,
5169 * otherwise it returns 1.
5170 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005171static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005172{
Willy Tarreaude491382015-04-06 11:04:28 +02005173 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005174
5175 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005176 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005177 return 0;
5178
5179 /* NOTE: The allocation never fails. The failure
5180 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005181 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005182 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005183 /* Create the object: obj[0] = userdata. */
5184 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005185 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005186 lua_rawseti(L, -2, 0);
5187
Willy Tarreaude491382015-04-06 11:04:28 +02005188 htxn->s = s;
5189 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005190 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005191 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005192
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005193 /* Create the "f" field that contains a list of fetches. */
5194 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005195 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005196 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005197 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005198
5199 /* Create the "sf" field that contains a list of stringsafe fetches. */
5200 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005201 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005202 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005203 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005204
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005205 /* Create the "c" field that contains a list of converters. */
5206 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005207 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005208 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005209 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005210
5211 /* Create the "sc" field that contains a list of stringsafe converters. */
5212 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005213 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005214 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005215 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005216
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005217 /* Create the "req" field that contains the request channel object. */
5218 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005219 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005220 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005221 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005222
5223 /* Create the "res" field that contains the response channel object. */
5224 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005225 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005226 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005227 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005228
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005229 /* Creates the HTTP object is the current proxy allows http. */
5230 lua_pushstring(L, "http");
5231 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005232 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005233 return 0;
5234 }
5235 else
5236 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005237 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005238
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005239 /* Pop a class sesison metatable and affect it to the userdata. */
5240 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5241 lua_setmetatable(L, -2);
5242
5243 return 1;
5244}
5245
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005246__LJMP static int hlua_txn_deflog(lua_State *L)
5247{
5248 const char *msg;
5249 struct hlua_txn *htxn;
5250
5251 MAY_LJMP(check_args(L, 2, "deflog"));
5252 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5253 msg = MAY_LJMP(luaL_checkstring(L, 2));
5254
5255 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5256 return 0;
5257}
5258
5259__LJMP static int hlua_txn_log(lua_State *L)
5260{
5261 int level;
5262 const char *msg;
5263 struct hlua_txn *htxn;
5264
5265 MAY_LJMP(check_args(L, 3, "log"));
5266 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5267 level = MAY_LJMP(luaL_checkinteger(L, 2));
5268 msg = MAY_LJMP(luaL_checkstring(L, 3));
5269
5270 if (level < 0 || level >= NB_LOG_LEVELS)
5271 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5272
5273 hlua_sendlog(htxn->s->be, level, msg);
5274 return 0;
5275}
5276
5277__LJMP static int hlua_txn_log_debug(lua_State *L)
5278{
5279 const char *msg;
5280 struct hlua_txn *htxn;
5281
5282 MAY_LJMP(check_args(L, 2, "Debug"));
5283 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5284 msg = MAY_LJMP(luaL_checkstring(L, 2));
5285 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5286 return 0;
5287}
5288
5289__LJMP static int hlua_txn_log_info(lua_State *L)
5290{
5291 const char *msg;
5292 struct hlua_txn *htxn;
5293
5294 MAY_LJMP(check_args(L, 2, "Info"));
5295 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5296 msg = MAY_LJMP(luaL_checkstring(L, 2));
5297 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5298 return 0;
5299}
5300
5301__LJMP static int hlua_txn_log_warning(lua_State *L)
5302{
5303 const char *msg;
5304 struct hlua_txn *htxn;
5305
5306 MAY_LJMP(check_args(L, 2, "Warning"));
5307 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5308 msg = MAY_LJMP(luaL_checkstring(L, 2));
5309 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5310 return 0;
5311}
5312
5313__LJMP static int hlua_txn_log_alert(lua_State *L)
5314{
5315 const char *msg;
5316 struct hlua_txn *htxn;
5317
5318 MAY_LJMP(check_args(L, 2, "Alert"));
5319 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5320 msg = MAY_LJMP(luaL_checkstring(L, 2));
5321 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5322 return 0;
5323}
5324
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005325__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5326{
5327 struct hlua_txn *htxn;
5328 int ll;
5329
5330 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5331 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5332 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5333
5334 if (ll < 0 || ll > 7)
5335 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5336
5337 htxn->s->logs.level = ll;
5338 return 0;
5339}
5340
5341__LJMP static int hlua_txn_set_tos(lua_State *L)
5342{
5343 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005344 int tos;
5345
5346 MAY_LJMP(check_args(L, 2, "set_tos"));
5347 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5348 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5349
Willy Tarreau1a18b542018-12-11 16:37:42 +01005350 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005351 return 0;
5352}
5353
5354__LJMP static int hlua_txn_set_mark(lua_State *L)
5355{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005356 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005357 int mark;
5358
5359 MAY_LJMP(check_args(L, 2, "set_mark"));
5360 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5361 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5362
Lukas Tribus579e3e32019-08-11 18:03:45 +02005363 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005364 return 0;
5365}
5366
Patrick Hemmer268a7072018-05-11 12:52:31 -04005367__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5368{
5369 struct hlua_txn *htxn;
5370
5371 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5372 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5373 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5374 return 0;
5375}
5376
5377__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5378{
5379 struct hlua_txn *htxn;
5380
5381 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5382 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5383 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5384 return 0;
5385}
5386
Christopher Faulet700d9e82020-01-31 12:21:52 +01005387/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005388 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005389 * message and terminate the transaction. It returns 1 on success and 0 on
5390 * error. The Reply must be on top of the stack.
5391 */
5392__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5393{
5394 struct htx *htx;
5395 struct htx_sl *sl;
5396 struct h1m h1m;
5397 const char *status, *reason, *body;
5398 size_t status_len, reason_len, body_len;
5399 int ret, code, flags;
5400
5401 code = 200;
5402 status = "200";
5403 status_len = 3;
5404 ret = lua_getfield(L, -1, "status");
5405 if (ret == LUA_TNUMBER) {
5406 code = lua_tointeger(L, -1);
5407 status = lua_tolstring(L, -1, &status_len);
5408 }
5409 lua_pop(L, 1);
5410
5411 reason = http_get_reason(code);
5412 reason_len = strlen(reason);
5413 ret = lua_getfield(L, -1, "reason");
5414 if (ret == LUA_TSTRING)
5415 reason = lua_tolstring(L, -1, &reason_len);
5416 lua_pop(L, 1);
5417
5418 body = NULL;
5419 body_len = 0;
5420 ret = lua_getfield(L, -1, "body");
5421 if (ret == LUA_TSTRING)
5422 body = lua_tolstring(L, -1, &body_len);
5423 lua_pop(L, 1);
5424
5425 /* Prepare the response before inserting the headers */
5426 h1m_init_res(&h1m);
5427 htx = htx_from_buf(&s->res.buf);
5428 channel_htx_truncate(&s->res, htx);
5429 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5430 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5431 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5432 ist2(status, status_len), ist2(reason, reason_len));
5433 }
5434 else {
5435 flags = HTX_SL_F_IS_RESP;
5436 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5437 ist2(status, status_len), ist2(reason, reason_len));
5438 }
5439 if (!sl)
5440 goto fail;
5441 sl->info.res.status = code;
5442
5443 /* Push in the stack the "headers" entry. */
5444 ret = lua_getfield(L, -1, "headers");
5445 if (ret != LUA_TTABLE)
5446 goto skip_headers;
5447
5448 lua_pushnil(L);
5449 while (lua_next(L, -2) != 0) {
5450 struct ist name, value;
5451 const char *n, *v;
5452 size_t nlen, vlen;
5453
5454 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5455 /* Skip element if the key is not a string or if the value is not a table */
5456 goto next_hdr;
5457 }
5458
5459 n = lua_tolstring(L, -2, &nlen);
5460 name = ist2(n, nlen);
5461 if (isteqi(name, ist("content-length"))) {
5462 /* Always skip content-length header. It will be added
5463 * later with the correct len
5464 */
5465 goto next_hdr;
5466 }
5467
5468 /* Loop on header's values */
5469 lua_pushnil(L);
5470 while (lua_next(L, -2)) {
5471 if (!lua_isstring(L, -1)) {
5472 /* Skip the value if it is not a string */
5473 goto next_value;
5474 }
5475
5476 v = lua_tolstring(L, -1, &vlen);
5477 value = ist2(v, vlen);
5478
5479 if (isteqi(name, ist("transfer-encoding")))
5480 h1_parse_xfer_enc_header(&h1m, value);
5481 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5482 goto fail;
5483
5484 next_value:
5485 lua_pop(L, 1);
5486 }
5487
5488 next_hdr:
5489 lua_pop(L, 1);
5490 }
5491 skip_headers:
5492 lua_pop(L, 1);
5493
5494 /* Update h1m flags: CLEN is set if CHNK is not present */
5495 if (!(h1m.flags & H1_MF_CHNK)) {
5496 const char *clen = ultoa(body_len);
5497
5498 h1m.flags |= H1_MF_CLEN;
5499 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5500 goto fail;
5501 }
5502 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5503 h1m.flags |= H1_MF_XFER_LEN;
5504
5505 /* Update HTX start-line flags */
5506 if (h1m.flags & H1_MF_XFER_ENC)
5507 flags |= HTX_SL_F_XFER_ENC;
5508 if (h1m.flags & H1_MF_XFER_LEN) {
5509 flags |= HTX_SL_F_XFER_LEN;
5510 if (h1m.flags & H1_MF_CHNK)
5511 flags |= HTX_SL_F_CHNK;
5512 else if (h1m.flags & H1_MF_CLEN)
5513 flags |= HTX_SL_F_CLEN;
5514 if (h1m.body_len == 0)
5515 flags |= HTX_SL_F_BODYLESS;
5516 }
5517 sl->flags |= flags;
5518
5519
5520 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5521 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5522 !htx_add_endof(htx, HTX_BLK_EOM))
5523 goto fail;
5524
5525 /* Now, forward the response and terminate the transaction */
5526 s->txn->status = code;
5527 htx_to_buf(htx, &s->res.buf);
5528 if (!http_forward_proxy_resp(s, 1))
5529 goto fail;
5530
5531 return 1;
5532
5533 fail:
5534 channel_htx_truncate(&s->res, htx);
5535 return 0;
5536}
5537
5538/* Terminate a transaction if called from a lua action. For TCP streams,
5539 * processing is just aborted. Nothing is returned to the client and all
5540 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5541 * is forwarded to the client before terminating the transaction. On success,
5542 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5543 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5544 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005545 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005546__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005547{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005548 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005549 struct stream *s;
5550 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005551
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005552 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005553
Christopher Faulet700d9e82020-01-31 12:21:52 +01005554 /* If the flags NOTERM is set, we cannot terminate the session, so we
5555 * just end the execution of the current lua code. */
5556 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005557 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005558
Christopher Faulet700d9e82020-01-31 12:21:52 +01005559 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005560 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005561 struct channel *req = &s->req;
5562 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005563
Christopher Faulet700d9e82020-01-31 12:21:52 +01005564 channel_auto_read(req);
5565 channel_abort(req);
5566 channel_auto_close(req);
5567 channel_erase(req);
5568
5569 res->wex = tick_add_ifset(now_ms, res->wto);
5570 channel_auto_read(res);
5571 channel_auto_close(res);
5572 channel_shutr_now(res);
5573
5574 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5575 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005576 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005577
Christopher Faulet700d9e82020-01-31 12:21:52 +01005578 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5579 /* No reply or invalid reply */
5580 s->txn->status = 0;
5581 http_reply_and_close(s, 0, NULL);
5582 }
5583 else {
5584 /* Remove extra args to have the reply on top of the stack */
5585 if (lua_gettop(L) > 2)
5586 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005587
Christopher Faulet700d9e82020-01-31 12:21:52 +01005588 if (!hlua_txn_forward_reply(L, s)) {
5589 if (!(s->flags & SF_ERR_MASK))
5590 s->flags |= SF_ERR_PRXCOND;
5591 lua_pushinteger(L, ACT_RET_ERR);
5592 WILL_LJMP(hlua_done(L));
5593 return 0; /* Never reached */
5594 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005595 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005596
Christopher Faulet700d9e82020-01-31 12:21:52 +01005597 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5598 if (htxn->dir == SMP_OPT_DIR_REQ) {
5599 /* let's log the request time */
5600 s->logs.tv_request = now;
5601 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5602 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5603 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005604
Christopher Faulet700d9e82020-01-31 12:21:52 +01005605 done:
5606 if (!(s->flags & SF_ERR_MASK))
5607 s->flags |= SF_ERR_LOCAL;
5608 if (!(s->flags & SF_FINST_MASK))
5609 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005610
Christopher Faulet4ad73102020-03-05 11:07:31 +01005611 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005612 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005613 return 0;
5614}
5615
Christopher Faulet700d9e82020-01-31 12:21:52 +01005616/*
5617 *
5618 *
5619 * Class REPLY
5620 *
5621 *
5622 */
5623
5624/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5625 * free slots, the function fails and returns 0;
5626 */
5627static int hlua_txn_reply_new(lua_State *L)
5628{
5629 struct hlua_txn *htxn;
5630 const char *reason, *body = NULL;
5631 int ret, status;
5632
5633 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005634 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005635 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5636 WILL_LJMP(lua_error(L));
5637 }
5638
5639 /* Default value */
5640 status = 200;
5641 reason = http_get_reason(status);
5642
5643 if (lua_istable(L, 2)) {
5644 /* load status and reason from the table argument at index 2 */
5645 ret = lua_getfield(L, 2, "status");
5646 if (ret == LUA_TNIL)
5647 goto reason;
5648 else if (ret != LUA_TNUMBER) {
5649 /* invalid status: ignore the reason */
5650 goto body;
5651 }
5652 status = lua_tointeger(L, -1);
5653
5654 reason:
5655 lua_pop(L, 1); /* restore the stack: remove status */
5656 ret = lua_getfield(L, 2, "reason");
5657 if (ret == LUA_TSTRING)
5658 reason = lua_tostring(L, -1);
5659
5660 body:
5661 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5662 ret = lua_getfield(L, 2, "body");
5663 if (ret == LUA_TSTRING)
5664 body = lua_tostring(L, -1);
5665 lua_pop(L, 1); /* restore the stack: remove body */
5666 }
5667
5668 /* Create the Reply table */
5669 lua_newtable(L);
5670
5671 /* Add status element */
5672 lua_pushstring(L, "status");
5673 lua_pushinteger(L, status);
5674 lua_settable(L, -3);
5675
5676 /* Add reason element */
5677 reason = http_get_reason(status);
5678 lua_pushstring(L, "reason");
5679 lua_pushstring(L, reason);
5680 lua_settable(L, -3);
5681
5682 /* Add body element, nil if undefined */
5683 lua_pushstring(L, "body");
5684 if (body)
5685 lua_pushstring(L, body);
5686 else
5687 lua_pushnil(L);
5688 lua_settable(L, -3);
5689
5690 /* Add headers element */
5691 lua_pushstring(L, "headers");
5692 lua_newtable(L);
5693
5694 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5695 if (lua_istable(L, 2)) {
5696 /* load headers from the table argument at index 2. If it is a table, copy it. */
5697 ret = lua_getfield(L, 2, "headers");
5698 if (ret == LUA_TTABLE) {
5699 /* stack: [ ... <headers:table>, <table> ] */
5700 lua_pushnil(L);
5701 while (lua_next(L, -2) != 0) {
5702 /* stack: [ ... <headers:table>, <table>, k, v] */
5703 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5704 /* invalid value type, skip it */
5705 lua_pop(L, 1);
5706 continue;
5707 }
5708
5709
5710 /* Duplicate the key and swap it with the value. */
5711 lua_pushvalue(L, -2);
5712 lua_insert(L, -2);
5713 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5714
5715 lua_newtable(L);
5716 lua_insert(L, -2);
5717 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5718
5719 if (lua_isstring(L, -1)) {
5720 /* push the value in the inner table */
5721 lua_rawseti(L, -2, 1);
5722 }
5723 else { /* table */
5724 lua_pushnil(L);
5725 while (lua_next(L, -2) != 0) {
5726 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5727 if (!lua_isstring(L, -1)) {
5728 /* invalid value type, skip it*/
5729 lua_pop(L, 1);
5730 continue;
5731 }
5732 /* push the value in the inner table */
5733 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5734 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5735 }
5736 lua_pop(L, 1);
5737 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5738 }
5739
5740 /* push (k,v) on the stack in the headers table:
5741 * stack: [ ... <headers:table>, <table>, k, k, v ]
5742 */
5743 lua_settable(L, -5);
5744 /* stack: [ ... <headers:table>, <table>, k ] */
5745 }
5746 }
5747 lua_pop(L, 1);
5748 }
5749 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5750 lua_settable(L, -3);
5751 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5752
5753 /* Pop a class sesison metatable and affect it to the userdata. */
5754 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5755 lua_setmetatable(L, -2);
5756 return 1;
5757}
5758
5759/* Set the reply status code, and optionally the reason. If no reason is
5760 * provided, the default one corresponding to the status code is used.
5761 */
5762__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5763{
5764 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5765 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5766
5767 /* First argument (self) must be a table */
5768 luaL_checktype(L, 1, LUA_TTABLE);
5769
5770 if (status < 100 || status > 599) {
5771 lua_pushboolean(L, 0);
5772 return 1;
5773 }
5774 if (!reason)
5775 reason = http_get_reason(status);
5776
5777 lua_pushinteger(L, status);
5778 lua_setfield(L, 1, "status");
5779
5780 lua_pushstring(L, reason);
5781 lua_setfield(L, 1, "reason");
5782
5783 lua_pushboolean(L, 1);
5784 return 1;
5785}
5786
5787/* Add a header into the reply object. Each header name is associated to an
5788 * array of values in the "headers" table. If the header name is not found, a
5789 * new entry is created.
5790 */
5791__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5792{
5793 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5794 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5795 int ret;
5796
5797 /* First argument (self) must be a table */
5798 luaL_checktype(L, 1, LUA_TTABLE);
5799
5800 /* Push in the stack the "headers" entry. */
5801 ret = lua_getfield(L, 1, "headers");
5802 if (ret != LUA_TTABLE) {
5803 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5804 WILL_LJMP(lua_error(L));
5805 }
5806
5807 /* check if the header is already registered. If not, register it. */
5808 ret = lua_getfield(L, -1, name);
5809 if (ret == LUA_TNIL) {
5810 /* Entry not found. */
5811 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5812
5813 /* Insert the new header name in the array in the top of the stack.
5814 * It left the new array in the top of the stack.
5815 */
5816 lua_newtable(L);
5817 lua_pushstring(L, name);
5818 lua_pushvalue(L, -2);
5819 lua_settable(L, -4);
5820 }
5821 else if (ret != LUA_TTABLE) {
5822 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5823 WILL_LJMP(lua_error(L));
5824 }
5825
5826 /* Now the top od thestack is an array of values. We push
5827 * the header value as new entry.
5828 */
5829 lua_pushstring(L, value);
5830 ret = lua_rawlen(L, -2);
5831 lua_rawseti(L, -2, ret + 1);
5832
5833 lua_pushboolean(L, 1);
5834 return 1;
5835}
5836
5837/* Remove all occurrences of a given header name. */
5838__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5839{
5840 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5841 int ret;
5842
5843 /* First argument (self) must be a table */
5844 luaL_checktype(L, 1, LUA_TTABLE);
5845
5846 /* Push in the stack the "headers" entry. */
5847 ret = lua_getfield(L, 1, "headers");
5848 if (ret != LUA_TTABLE) {
5849 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5850 WILL_LJMP(lua_error(L));
5851 }
5852
5853 lua_pushstring(L, name);
5854 lua_pushnil(L);
5855 lua_settable(L, -3);
5856
5857 lua_pushboolean(L, 1);
5858 return 1;
5859}
5860
5861/* Set the reply's body. Overwrite any existing entry. */
5862__LJMP static int hlua_txn_reply_set_body(lua_State *L)
5863{
5864 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
5865
5866 /* First argument (self) must be a table */
5867 luaL_checktype(L, 1, LUA_TTABLE);
5868
5869 lua_pushstring(L, payload);
5870 lua_setfield(L, 1, "body");
5871
5872 lua_pushboolean(L, 1);
5873 return 1;
5874}
5875
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005876__LJMP static int hlua_log(lua_State *L)
5877{
5878 int level;
5879 const char *msg;
5880
5881 MAY_LJMP(check_args(L, 2, "log"));
5882 level = MAY_LJMP(luaL_checkinteger(L, 1));
5883 msg = MAY_LJMP(luaL_checkstring(L, 2));
5884
5885 if (level < 0 || level >= NB_LOG_LEVELS)
5886 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5887
5888 hlua_sendlog(NULL, level, msg);
5889 return 0;
5890}
5891
5892__LJMP static int hlua_log_debug(lua_State *L)
5893{
5894 const char *msg;
5895
5896 MAY_LJMP(check_args(L, 1, "debug"));
5897 msg = MAY_LJMP(luaL_checkstring(L, 1));
5898 hlua_sendlog(NULL, LOG_DEBUG, msg);
5899 return 0;
5900}
5901
5902__LJMP static int hlua_log_info(lua_State *L)
5903{
5904 const char *msg;
5905
5906 MAY_LJMP(check_args(L, 1, "info"));
5907 msg = MAY_LJMP(luaL_checkstring(L, 1));
5908 hlua_sendlog(NULL, LOG_INFO, msg);
5909 return 0;
5910}
5911
5912__LJMP static int hlua_log_warning(lua_State *L)
5913{
5914 const char *msg;
5915
5916 MAY_LJMP(check_args(L, 1, "warning"));
5917 msg = MAY_LJMP(luaL_checkstring(L, 1));
5918 hlua_sendlog(NULL, LOG_WARNING, msg);
5919 return 0;
5920}
5921
5922__LJMP static int hlua_log_alert(lua_State *L)
5923{
5924 const char *msg;
5925
5926 MAY_LJMP(check_args(L, 1, "alert"));
5927 msg = MAY_LJMP(luaL_checkstring(L, 1));
5928 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005929 return 0;
5930}
5931
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005932__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005933{
5934 int wakeup_ms = lua_tointeger(L, -1);
5935 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005936 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005937 return 0;
5938}
5939
5940__LJMP static int hlua_sleep(lua_State *L)
5941{
5942 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005943 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005944
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005945 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005946
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005947 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005948 wakeup_ms = tick_add(now_ms, delay);
5949 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005950
Willy Tarreau9635e032018-10-16 17:52:55 +02005951 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005952 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005953}
5954
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005955__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005956{
5957 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005958 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005959
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005960 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005961
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005962 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005963 wakeup_ms = tick_add(now_ms, delay);
5964 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005965
Willy Tarreau9635e032018-10-16 17:52:55 +02005966 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005967 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005968}
5969
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005970/* This functionis an LUA binding. it permits to give back
5971 * the hand at the HAProxy scheduler. It is used when the
5972 * LUA processing consumes a lot of time.
5973 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005974__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005975{
5976 return 0;
5977}
5978
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005979__LJMP static int hlua_yield(lua_State *L)
5980{
Willy Tarreau9635e032018-10-16 17:52:55 +02005981 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005982 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005983}
5984
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005985/* This function change the nice of the currently executed
5986 * task. It is used set low or high priority at the current
5987 * task.
5988 */
Willy Tarreau59551662015-03-10 14:23:13 +01005989__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005990{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005991 struct hlua *hlua;
5992 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005993
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005994 MAY_LJMP(check_args(L, 1, "set_nice"));
5995 hlua = hlua_gethlua(L);
5996 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005997
5998 /* If he task is not set, I'm in a start mode. */
5999 if (!hlua || !hlua->task)
6000 return 0;
6001
6002 if (nice < -1024)
6003 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006004 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006005 nice = 1024;
6006
6007 hlua->task->nice = nice;
6008 return 0;
6009}
6010
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006011/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006012 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6013 * return an E_AGAIN signal, the emmiter of this signal must set a
6014 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006015 *
6016 * Task wrapper are longjmp safe because the only one Lua code
6017 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006018 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006019struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006020{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006021 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006022 enum hlua_exec status;
6023
Christopher Faulet5bc99722018-04-25 10:34:45 +02006024 if (task->thread_mask == MAX_THREADS_MASK)
6025 task_set_affinity(task, tid_bit);
6026
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006027 /* If it is the first call to the task, we must initialize the
6028 * execution timeouts.
6029 */
6030 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006031 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006032
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006033 /* Execute the Lua code. */
6034 status = hlua_ctx_resume(hlua, 1);
6035
6036 switch (status) {
6037 /* finished or yield */
6038 case HLUA_E_OK:
6039 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006040 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006041 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006042 break;
6043
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006044 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006045 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006046 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006047 break;
6048
6049 /* finished with error. */
6050 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006051 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006052 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006053 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006054 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006055 break;
6056
6057 case HLUA_E_ERR:
6058 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006059 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006060 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006061 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006062 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006063 break;
6064 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006065 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006066}
6067
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006068/* This function is an LUA binding that register LUA function to be
6069 * executed after the HAProxy configuration parsing and before the
6070 * HAProxy scheduler starts. This function expect only one LUA
6071 * argument that is a function. This function returns nothing, but
6072 * throws if an error is encountered.
6073 */
6074__LJMP static int hlua_register_init(lua_State *L)
6075{
6076 struct hlua_init_function *init;
6077 int ref;
6078
6079 MAY_LJMP(check_args(L, 1, "register_init"));
6080
6081 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6082
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006083 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006084 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006085 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006086
6087 init->function_ref = ref;
6088 LIST_ADDQ(&hlua_init_functions, &init->l);
6089 return 0;
6090}
6091
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006092/* This functio is an LUA binding. It permits to register a task
6093 * executed in parallel of the main HAroxy activity. The task is
6094 * created and it is set in the HAProxy scheduler. It can be called
6095 * from the "init" section, "post init" or during the runtime.
6096 *
6097 * Lua prototype:
6098 *
6099 * <none> core.register_task(<function>)
6100 */
6101static int hlua_register_task(lua_State *L)
6102{
6103 struct hlua *hlua;
6104 struct task *task;
6105 int ref;
6106
6107 MAY_LJMP(check_args(L, 1, "register_task"));
6108
6109 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6110
Willy Tarreaubafbe012017-11-24 17:34:44 +01006111 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006112 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006113 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006114
Emeric Brunc60def82017-09-27 14:59:38 +02006115 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006116 if (!task)
6117 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6118
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006119 task->context = hlua;
6120 task->process = hlua_process_task;
6121
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006122 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006123 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006124
6125 /* Restore the function in the stack. */
6126 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6127 hlua->nargs = 0;
6128
6129 /* Schedule task. */
6130 task_schedule(task, now_ms);
6131
6132 return 0;
6133}
6134
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006135/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6136 * doesn't allow "yield" functions because the HAProxy engine cannot
6137 * resume converters.
6138 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006139static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006140{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006141 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006142 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006143 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006144
Willy Tarreaube508f12016-03-10 11:47:01 +01006145 if (!stream)
6146 return 0;
6147
Willy Tarreau87b09662015-04-03 00:22:06 +02006148 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006149 * Lua context can be not initialized. This behavior
6150 * permits to save performances because a systematic
6151 * Lua initialization cause 5% performances loss.
6152 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006153 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006154 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006155 if (!stream->hlua) {
6156 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6157 return 0;
6158 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006159 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006160 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6161 return 0;
6162 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006163 }
6164
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006165 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006166 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006167
6168 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006169 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6170 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6171 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006172 else
6173 error = "critical error";
6174 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006175 return 0;
6176 }
6177
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006178 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006179 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006180 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006181 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006182 return 0;
6183 }
6184
6185 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006186 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006187
6188 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006189 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006190 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006191 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006192 return 0;
6193 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006194 hlua_smp2lua(stream->hlua->T, smp);
6195 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006196
6197 /* push keywords in the stack. */
6198 if (arg_p) {
6199 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006200 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006201 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006202 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006203 return 0;
6204 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006205 hlua_arg2lua(stream->hlua->T, arg_p);
6206 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006207 }
6208 }
6209
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006210 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006211 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006212
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006213 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006214 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006215 }
6216
6217 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006218 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006219 /* finished. */
6220 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006221 /* If the stack is empty, the function fails. */
6222 if (lua_gettop(stream->hlua->T) <= 0)
6223 return 0;
6224
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006225 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006226 hlua_lua2smp(stream->hlua->T, -1, smp);
6227 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006228 return 1;
6229
6230 /* yield. */
6231 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006232 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006233 return 0;
6234
6235 /* finished with error. */
6236 case HLUA_E_ERRMSG:
6237 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006238 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006239 fcn->name, lua_tostring(stream->hlua->T, -1));
6240 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006241 return 0;
6242
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006243 case HLUA_E_ETMOUT:
6244 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6245 return 0;
6246
6247 case HLUA_E_NOMEM:
6248 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6249 return 0;
6250
6251 case HLUA_E_YIELD:
6252 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6253 return 0;
6254
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006255 case HLUA_E_ERR:
6256 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006257 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006258 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006259
6260 default:
6261 return 0;
6262 }
6263}
6264
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006265/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6266 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006267 * resume sample-fetches. This function will be called by the sample
6268 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006269 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006270static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6271 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006272{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006273 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006274 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006275 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006276 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006277
Willy Tarreaube508f12016-03-10 11:47:01 +01006278 if (!stream)
6279 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006280
Willy Tarreau87b09662015-04-03 00:22:06 +02006281 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006282 * Lua context can be not initialized. This behavior
6283 * permits to save performances because a systematic
6284 * Lua initialization cause 5% performances loss.
6285 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006286 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006287 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006288 if (!stream->hlua) {
6289 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6290 return 0;
6291 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006292 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006293 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6294 return 0;
6295 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006296 }
6297
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006298 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006299 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006300
6301 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006302 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6303 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6304 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006305 else
6306 error = "critical error";
6307 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006308 return 0;
6309 }
6310
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006311 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006312 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006313 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006314 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006315 return 0;
6316 }
6317
6318 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006319 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006320
6321 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006322 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006323 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006324 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006325 return 0;
6326 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006327 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006328
6329 /* push keywords in the stack. */
6330 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6331 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006332 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006333 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006334 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006335 return 0;
6336 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006337 hlua_arg2lua(stream->hlua->T, arg_p);
6338 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006339 }
6340
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006341 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006342 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006343
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006344 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006345 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006346 }
6347
6348 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006349 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006350 /* finished. */
6351 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006352 /* If the stack is empty, the function fails. */
6353 if (lua_gettop(stream->hlua->T) <= 0)
6354 return 0;
6355
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006356 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006357 hlua_lua2smp(stream->hlua->T, -1, smp);
6358 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006359
6360 /* Set the end of execution flag. */
6361 smp->flags &= ~SMP_F_MAY_CHANGE;
6362 return 1;
6363
6364 /* yield. */
6365 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006366 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006367 return 0;
6368
6369 /* finished with error. */
6370 case HLUA_E_ERRMSG:
6371 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006372 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006373 fcn->name, lua_tostring(stream->hlua->T, -1));
6374 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006375 return 0;
6376
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006377 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006378 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6379 return 0;
6380
6381 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006382 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6383 return 0;
6384
6385 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006386 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6387 return 0;
6388
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006389 case HLUA_E_ERR:
6390 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006391 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006392 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006393
6394 default:
6395 return 0;
6396 }
6397}
6398
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006399/* This function is an LUA binding used for registering
6400 * "sample-conv" functions. It expects a converter name used
6401 * in the haproxy configuration file, and an LUA function.
6402 */
6403__LJMP static int hlua_register_converters(lua_State *L)
6404{
6405 struct sample_conv_kw_list *sck;
6406 const char *name;
6407 int ref;
6408 int len;
6409 struct hlua_function *fcn;
6410
6411 MAY_LJMP(check_args(L, 2, "register_converters"));
6412
6413 /* First argument : converter name. */
6414 name = MAY_LJMP(luaL_checkstring(L, 1));
6415
6416 /* Second argument : lua function. */
6417 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6418
6419 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006420 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006421 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006422 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006423 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006424 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006425 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006426
6427 /* Fill fcn. */
6428 fcn->name = strdup(name);
6429 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006430 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006431 fcn->function_ref = ref;
6432
6433 /* List head */
6434 sck->list.n = sck->list.p = NULL;
6435
6436 /* converter keyword. */
6437 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006438 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006439 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006440 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006441
6442 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6443 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006444 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 +01006445 sck->kw[0].val_args = NULL;
6446 sck->kw[0].in_type = SMP_T_STR;
6447 sck->kw[0].out_type = SMP_T_STR;
6448 sck->kw[0].private = fcn;
6449
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006450 /* Register this new converter */
6451 sample_register_convs(sck);
6452
6453 return 0;
6454}
6455
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006456/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006457 * "sample-fetch" functions. It expects a converter name used
6458 * in the haproxy configuration file, and an LUA function.
6459 */
6460__LJMP static int hlua_register_fetches(lua_State *L)
6461{
6462 const char *name;
6463 int ref;
6464 int len;
6465 struct sample_fetch_kw_list *sfk;
6466 struct hlua_function *fcn;
6467
6468 MAY_LJMP(check_args(L, 2, "register_fetches"));
6469
6470 /* First argument : sample-fetch name. */
6471 name = MAY_LJMP(luaL_checkstring(L, 1));
6472
6473 /* Second argument : lua function. */
6474 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6475
6476 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006477 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006478 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006479 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006480 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006481 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006482 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006483
6484 /* Fill fcn. */
6485 fcn->name = strdup(name);
6486 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006487 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006488 fcn->function_ref = ref;
6489
6490 /* List head */
6491 sfk->list.n = sfk->list.p = NULL;
6492
6493 /* sample-fetch keyword. */
6494 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006495 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006496 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006497 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006498
6499 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6500 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006501 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 +01006502 sfk->kw[0].val_args = NULL;
6503 sfk->kw[0].out_type = SMP_T_STR;
6504 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6505 sfk->kw[0].val = 0;
6506 sfk->kw[0].private = fcn;
6507
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006508 /* Register this new fetch. */
6509 sample_register_fetches(sfk);
6510
6511 return 0;
6512}
6513
Christopher Faulet501465d2020-02-26 14:54:16 +01006514/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006515 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006516__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006517{
6518 struct hlua *hlua = hlua_gethlua(L);
6519 unsigned int delay;
6520 unsigned int wakeup_ms;
6521
6522 MAY_LJMP(check_args(L, 1, "wake_time"));
6523
6524 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6525 wakeup_ms = tick_add(now_ms, delay);
6526 hlua->wake_time = wakeup_ms;
6527 return 0;
6528}
6529
6530
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006531/* This function is a wrapper to execute each LUA function declared as an action
6532 * wrapper during the initialisation period. This function may return any
6533 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6534 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6535 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006536 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006537static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006538 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006539{
6540 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006541 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006542 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006543 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006544
6545 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006546 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6547 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6548 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6549 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006550 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006551 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006552 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006553 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006554
Willy Tarreau87b09662015-04-03 00:22:06 +02006555 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006556 * Lua context can be not initialized. This behavior
6557 * permits to save performances because a systematic
6558 * Lua initialization cause 5% performances loss.
6559 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006560 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006561 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006562 if (!s->hlua) {
6563 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6564 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006565 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006566 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006567 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006568 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6569 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006570 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006571 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006572 }
6573
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006574 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006575 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006576
6577 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006578 if (!SET_SAFE_LJMP(s->hlua->T)) {
6579 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6580 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006581 else
6582 error = "critical error";
6583 SEND_ERR(px, "Lua function '%s': %s.\n",
6584 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006585 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006586 }
6587
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006588 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006589 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006590 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006591 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006592 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006593 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006594 }
6595
6596 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006597 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006598
Willy Tarreau87b09662015-04-03 00:22:06 +02006599 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006600 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006601 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006602 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006603 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006604 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006605 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006606 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006607
6608 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006609 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006610 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006611 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006612 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006613 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006614 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006615 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006616 lua_pushstring(s->hlua->T, *arg);
6617 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006618 }
6619
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006620 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006621 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006622
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006623 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006624 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006625 }
6626
Christopher Faulet23308eb2020-06-02 18:46:07 +02006627 /* Always reset the analyse expiration timeout for the corresponding
6628 * channel in case the lua script yield, to be sure to not keep an
6629 * expired timeout.
6630 */
6631 if (dir == SMP_OPT_DIR_REQ)
6632 s->req.analyse_exp = TICK_ETERNITY;
6633 else
6634 s->res.analyse_exp = TICK_ETERNITY;
6635
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006636 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006637 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006638 /* finished. */
6639 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006640 /* Catch the return value */
6641 if (lua_gettop(s->hlua->T) > 0)
6642 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006643
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006644 /* Set timeout in the required channel. */
6645 if (act_ret == ACT_RET_YIELD && s->hlua->wake_time != TICK_ETERNITY) {
6646 if (dir == SMP_OPT_DIR_REQ)
6647 s->req.analyse_exp = s->hlua->wake_time;
6648 else
6649 s->res.analyse_exp = s->hlua->wake_time;
6650 }
6651 goto end;
6652
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006653 /* yield. */
6654 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006655 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006656 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Faulet81921b12019-08-14 23:19:45 +02006657 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006658 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006659 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006660 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006661 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006662 /* Some actions can be wake up when a "write" event
6663 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006664 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006665 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006666 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006667 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006668 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006669 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006670 act_ret = ACT_RET_YIELD;
6671 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006672
6673 /* finished with error. */
6674 case HLUA_E_ERRMSG:
6675 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006676 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006677 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6678 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006679 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006680
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006681 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006682 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006683 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006684
6685 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006686 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006687 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006688
6689 case HLUA_E_YIELD:
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:
6704 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006705}
6706
Olivier Houchard9f6af332018-05-25 14:04:04 +02006707struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006708{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006709 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006710
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006711 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006712 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006713 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006714}
6715
6716static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6717{
6718 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006719 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006720 struct task *task;
6721 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006722 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006723
Willy Tarreaubafbe012017-11-24 17:34:44 +01006724 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006725 if (!hlua) {
6726 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6727 ctx->rule->arg.hlua_rule->fcn.name);
6728 return 0;
6729 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006730 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006731 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006732 ctx->ctx.hlua_apptcp.flags = 0;
6733
6734 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006735 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006736 if (!task) {
6737 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6738 ctx->rule->arg.hlua_rule->fcn.name);
6739 return 0;
6740 }
6741 task->nice = 0;
6742 task->context = ctx;
6743 task->process = hlua_applet_wakeup;
6744 ctx->ctx.hlua_apptcp.task = task;
6745
6746 /* In the execution wrappers linked with a stream, the
6747 * Lua context can be not initialized. This behavior
6748 * permits to save performances because a systematic
6749 * Lua initialization cause 5% performances loss.
6750 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006751 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006752 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6753 ctx->rule->arg.hlua_rule->fcn.name);
6754 return 0;
6755 }
6756
6757 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006758 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006759
6760 /* The following Lua calls can fail. */
6761 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006762 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6763 error = lua_tostring(hlua->T, -1);
6764 else
6765 error = "critical error";
6766 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6767 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006768 return 0;
6769 }
6770
6771 /* Check stack available size. */
6772 if (!lua_checkstack(hlua->T, 1)) {
6773 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6774 ctx->rule->arg.hlua_rule->fcn.name);
6775 RESET_SAFE_LJMP(hlua->T);
6776 return 0;
6777 }
6778
6779 /* Restore the function in the stack. */
6780 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6781
6782 /* Create and and push object stream in the stack. */
6783 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6784 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6785 ctx->rule->arg.hlua_rule->fcn.name);
6786 RESET_SAFE_LJMP(hlua->T);
6787 return 0;
6788 }
6789 hlua->nargs = 1;
6790
6791 /* push keywords in the stack. */
6792 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6793 if (!lua_checkstack(hlua->T, 1)) {
6794 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6795 ctx->rule->arg.hlua_rule->fcn.name);
6796 RESET_SAFE_LJMP(hlua->T);
6797 return 0;
6798 }
6799 lua_pushstring(hlua->T, *arg);
6800 hlua->nargs++;
6801 }
6802
6803 RESET_SAFE_LJMP(hlua->T);
6804
6805 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006806 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006807 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006808
6809 return 1;
6810}
6811
Willy Tarreau60409db2019-08-21 14:14:50 +02006812void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006813{
6814 struct stream_interface *si = ctx->owner;
6815 struct stream *strm = si_strm(si);
6816 struct channel *res = si_ic(si);
6817 struct act_rule *rule = ctx->rule;
6818 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006819 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006820
6821 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006822 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6823 /* eat the whole request */
6824 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006825 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006826 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006827
6828 /* If the stream is disconnect or closed, ldo nothing. */
6829 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6830 return;
6831
6832 /* Execute the function. */
6833 switch (hlua_ctx_resume(hlua, 1)) {
6834 /* finished. */
6835 case HLUA_E_OK:
6836 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6837
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006838 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006839 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006840 res->flags |= CF_READ_NULL;
6841 si_shutr(si);
6842 return;
6843
6844 /* yield. */
6845 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006846 if (hlua->wake_time != TICK_ETERNITY)
6847 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006848 return;
6849
6850 /* finished with error. */
6851 case HLUA_E_ERRMSG:
6852 /* Display log. */
6853 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6854 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6855 lua_pop(hlua->T, 1);
6856 goto error;
6857
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006858 case HLUA_E_ETMOUT:
6859 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6860 rule->arg.hlua_rule->fcn.name);
6861 goto error;
6862
6863 case HLUA_E_NOMEM:
6864 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6865 rule->arg.hlua_rule->fcn.name);
6866 goto error;
6867
6868 case HLUA_E_YIELD: /* unexpected */
6869 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6870 rule->arg.hlua_rule->fcn.name);
6871 goto error;
6872
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006873 case HLUA_E_ERR:
6874 /* Display log. */
6875 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6876 rule->arg.hlua_rule->fcn.name);
6877 goto error;
6878
6879 default:
6880 goto error;
6881 }
6882
6883error:
6884
6885 /* For all other cases, just close the stream. */
6886 si_shutw(si);
6887 si_shutr(si);
6888 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6889}
6890
6891static void hlua_applet_tcp_release(struct appctx *ctx)
6892{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006893 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006894 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006895 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006896 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006897}
6898
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006899/* The function returns 1 if the initialisation is complete, 0 if
6900 * an errors occurs and -1 if more data are required for initializing
6901 * the applet.
6902 */
6903static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6904{
6905 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006906 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006907 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006908 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006909 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006910 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006911
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006912 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006913 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006914 if (!hlua) {
6915 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6916 ctx->rule->arg.hlua_rule->fcn.name);
6917 return 0;
6918 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006919 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006920 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006921 ctx->ctx.hlua_apphttp.left_bytes = -1;
6922 ctx->ctx.hlua_apphttp.flags = 0;
6923
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006924 if (txn->req.flags & HTTP_MSGF_VER_11)
6925 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6926
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006927 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006928 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006929 if (!task) {
6930 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6931 ctx->rule->arg.hlua_rule->fcn.name);
6932 return 0;
6933 }
6934 task->nice = 0;
6935 task->context = ctx;
6936 task->process = hlua_applet_wakeup;
6937 ctx->ctx.hlua_apphttp.task = task;
6938
6939 /* In the execution wrappers linked with a stream, the
6940 * Lua context can be not initialized. This behavior
6941 * permits to save performances because a systematic
6942 * Lua initialization cause 5% performances loss.
6943 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006944 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006945 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6946 ctx->rule->arg.hlua_rule->fcn.name);
6947 return 0;
6948 }
6949
6950 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006951 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006952
6953 /* The following Lua calls can fail. */
6954 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006955 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6956 error = lua_tostring(hlua->T, -1);
6957 else
6958 error = "critical error";
6959 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6960 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006961 return 0;
6962 }
6963
6964 /* Check stack available size. */
6965 if (!lua_checkstack(hlua->T, 1)) {
6966 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6967 ctx->rule->arg.hlua_rule->fcn.name);
6968 RESET_SAFE_LJMP(hlua->T);
6969 return 0;
6970 }
6971
6972 /* Restore the function in the stack. */
6973 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6974
6975 /* Create and and push object stream in the stack. */
6976 if (!hlua_applet_http_new(hlua->T, ctx)) {
6977 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6978 ctx->rule->arg.hlua_rule->fcn.name);
6979 RESET_SAFE_LJMP(hlua->T);
6980 return 0;
6981 }
6982 hlua->nargs = 1;
6983
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006984 /* push keywords in the stack. */
6985 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6986 if (!lua_checkstack(hlua->T, 1)) {
6987 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6988 ctx->rule->arg.hlua_rule->fcn.name);
6989 RESET_SAFE_LJMP(hlua->T);
6990 return 0;
6991 }
6992 lua_pushstring(hlua->T, *arg);
6993 hlua->nargs++;
6994 }
6995
6996 RESET_SAFE_LJMP(hlua->T);
6997
6998 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006999 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007000
7001 return 1;
7002}
7003
Willy Tarreau60409db2019-08-21 14:14:50 +02007004void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007005{
7006 struct stream_interface *si = ctx->owner;
7007 struct stream *strm = si_strm(si);
7008 struct channel *req = si_oc(si);
7009 struct channel *res = si_ic(si);
7010 struct act_rule *rule = ctx->rule;
7011 struct proxy *px = strm->be;
7012 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7013 struct htx *req_htx, *res_htx;
7014
7015 res_htx = htx_from_buf(&res->buf);
7016
7017 /* If the stream is disconnect or closed, ldo nothing. */
7018 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7019 goto out;
7020
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007021 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007022 if (!b_size(&res->buf)) {
7023 si_rx_room_blk(si);
7024 goto out;
7025 }
7026 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007027 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007028 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7029
7030 /* Set the currently running flag. */
7031 if (!HLUA_IS_RUNNING(hlua) &&
7032 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7033 struct htx_blk *blk;
7034 size_t count = co_data(req);
7035
7036 if (!count) {
7037 si_cant_get(si);
7038 goto out;
7039 }
7040
7041 /* We need to flush the request header. This left the body for
7042 * the Lua.
7043 */
7044 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007045 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007046 while (count && blk) {
7047 enum htx_blk_type type = htx_get_blk_type(blk);
7048 uint32_t sz = htx_get_blksz(blk);
7049
7050 if (sz > count) {
7051 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007052 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007053 goto out;
7054 }
7055
7056 count -= sz;
7057 co_set_data(req, co_data(req) - sz);
7058 blk = htx_remove_blk(req_htx, blk);
7059
7060 if (type == HTX_BLK_EOH)
7061 break;
7062 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007063 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007064 }
7065
7066 /* Executes The applet if it is not done. */
7067 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7068
7069 /* Execute the function. */
7070 switch (hlua_ctx_resume(hlua, 1)) {
7071 /* finished. */
7072 case HLUA_E_OK:
7073 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7074 break;
7075
7076 /* yield. */
7077 case HLUA_E_AGAIN:
7078 if (hlua->wake_time != TICK_ETERNITY)
7079 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007080 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007081
7082 /* finished with error. */
7083 case HLUA_E_ERRMSG:
7084 /* Display log. */
7085 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7086 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7087 lua_pop(hlua->T, 1);
7088 goto error;
7089
7090 case HLUA_E_ETMOUT:
7091 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7092 rule->arg.hlua_rule->fcn.name);
7093 goto error;
7094
7095 case HLUA_E_NOMEM:
7096 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7097 rule->arg.hlua_rule->fcn.name);
7098 goto error;
7099
7100 case HLUA_E_YIELD: /* unexpected */
7101 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7102 rule->arg.hlua_rule->fcn.name);
7103 goto error;
7104
7105 case HLUA_E_ERR:
7106 /* Display log. */
7107 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7108 rule->arg.hlua_rule->fcn.name);
7109 goto error;
7110
7111 default:
7112 goto error;
7113 }
7114 }
7115
7116 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007117 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7118 goto done;
7119
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007120 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7121 goto error;
7122
Christopher Faulet54b5e212019-06-04 10:08:28 +02007123 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007124 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7125 si_rx_room_blk(si);
7126 goto out;
7127 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007128 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007129 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7130 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007131 }
7132
7133 done:
7134 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007135 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007136 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007137 si_shutr(si);
7138 }
7139
7140 /* eat the whole request */
7141 if (co_data(req)) {
7142 req_htx = htx_from_buf(&req->buf);
7143 co_htx_skip(req, req_htx, co_data(req));
7144 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007145 }
7146 }
7147
7148 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007149 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007150 return;
7151
7152 error:
7153
7154 /* If we are in HTTP mode, and we are not send any
7155 * data, return a 500 server error in best effort:
7156 * if there is no room available in the buffer,
7157 * just close the connection.
7158 */
7159 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007160 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007161
7162 channel_erase(res);
7163 res->buf.data = b_data(err);
7164 memcpy(res->buf.area, b_head(err), b_data(err));
7165 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007166 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007167 }
7168 if (!(strm->flags & SF_ERR_MASK))
7169 strm->flags |= SF_ERR_RESOURCE;
7170 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7171 goto done;
7172}
7173
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007174static void hlua_applet_http_release(struct appctx *ctx)
7175{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007176 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007177 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007178 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007179 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007180}
7181
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007182/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007183 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007184 *
7185 * This function can fail with an abort() due to an Lua critical error.
7186 * We are in the configuration parsing process of HAProxy, this abort() is
7187 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007188 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007189static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7190 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007191{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007192 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007193 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007194
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007195 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007196 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007197 if (!rule->arg.hlua_rule) {
7198 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007199 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007200 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007201
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007202 /* Memory for arguments. */
7203 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7204 if (!rule->arg.hlua_rule->args) {
7205 memprintf(err, "out of memory error");
7206 return ACT_RET_PRS_ERR;
7207 }
7208
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007209 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007210 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007211
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007212 /* Expect some arguments */
7213 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007214 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007215 memprintf(err, "expect %d arguments", fcn->nargs);
7216 return ACT_RET_PRS_ERR;
7217 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007218 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007219 if (!rule->arg.hlua_rule->args[i]) {
7220 memprintf(err, "out of memory error");
7221 return ACT_RET_PRS_ERR;
7222 }
7223 (*cur_arg)++;
7224 }
7225 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007226
Thierry FOURNIER42148732015-09-02 17:17:33 +02007227 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007228 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007229 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007230}
7231
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007232static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7233 struct act_rule *rule, char **err)
7234{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007235 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007236
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007237 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007238 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007239 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007240 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007241 * the call of this analyzer.
7242 */
7243 if (rule->from != ACT_F_HTTP_REQ) {
7244 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7245 return ACT_RET_PRS_ERR;
7246 }
7247
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007248 /* Memory for the rule. */
7249 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7250 if (!rule->arg.hlua_rule) {
7251 memprintf(err, "out of memory error");
7252 return ACT_RET_PRS_ERR;
7253 }
7254
7255 /* Reference the Lua function and store the reference. */
7256 rule->arg.hlua_rule->fcn = *fcn;
7257
7258 /* TODO: later accept arguments. */
7259 rule->arg.hlua_rule->args = NULL;
7260
7261 /* Add applet pointer in the rule. */
7262 rule->applet.obj_type = OBJ_TYPE_APPLET;
7263 rule->applet.name = fcn->name;
7264 rule->applet.init = hlua_applet_http_init;
7265 rule->applet.fct = hlua_applet_http_fct;
7266 rule->applet.release = hlua_applet_http_release;
7267 rule->applet.timeout = hlua_timeout_applet;
7268
7269 return ACT_RET_PRS_OK;
7270}
7271
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007272/* This function is an LUA binding used for registering
7273 * "sample-conv" functions. It expects a converter name used
7274 * in the haproxy configuration file, and an LUA function.
7275 */
7276__LJMP static int hlua_register_action(lua_State *L)
7277{
7278 struct action_kw_list *akl;
7279 const char *name;
7280 int ref;
7281 int len;
7282 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007283 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007284
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007285 /* Initialise the number of expected arguments at 0. */
7286 nargs = 0;
7287
7288 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7289 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007290
7291 /* First argument : converter name. */
7292 name = MAY_LJMP(luaL_checkstring(L, 1));
7293
7294 /* Second argument : environment. */
7295 if (lua_type(L, 2) != LUA_TTABLE)
7296 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7297
7298 /* Third argument : lua function. */
7299 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7300
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007301 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007302 if (lua_gettop(L) >= 4)
7303 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7304
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007305 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007306 lua_pushnil(L);
7307 while (lua_next(L, 2) != 0) {
7308 if (lua_type(L, -1) != LUA_TSTRING)
7309 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7310
7311 /* Check required environment. Only accepted "http" or "tcp". */
7312 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007313 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007314 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007315 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007316 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007317 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007318 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007319
7320 /* Fill fcn. */
7321 fcn->name = strdup(name);
7322 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007323 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007324 fcn->function_ref = ref;
7325
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007326 /* Set the expected number od arguments. */
7327 fcn->nargs = nargs;
7328
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007329 /* List head */
7330 akl->list.n = akl->list.p = NULL;
7331
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007332 /* action keyword. */
7333 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007334 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007335 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007336 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007337
7338 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7339
7340 akl->kw[0].match_pfx = 0;
7341 akl->kw[0].private = fcn;
7342 akl->kw[0].parse = action_register_lua;
7343
7344 /* select the action registering point. */
7345 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7346 tcp_req_cont_keywords_register(akl);
7347 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7348 tcp_res_cont_keywords_register(akl);
7349 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7350 http_req_keywords_register(akl);
7351 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7352 http_res_keywords_register(akl);
7353 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007354 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007355 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7356 "are expected.", lua_tostring(L, -1)));
7357
7358 /* pop the environment string. */
7359 lua_pop(L, 1);
7360 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007361 return ACT_RET_PRS_OK;
7362}
7363
7364static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7365 struct act_rule *rule, char **err)
7366{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007367 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007368
Christopher Faulet280f85b2019-07-15 15:02:04 +02007369 if (px->mode == PR_MODE_HTTP) {
7370 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007371 return ACT_RET_PRS_ERR;
7372 }
7373
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007374 /* Memory for the rule. */
7375 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7376 if (!rule->arg.hlua_rule) {
7377 memprintf(err, "out of memory error");
7378 return ACT_RET_PRS_ERR;
7379 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007380
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007381 /* Reference the Lua function and store the reference. */
7382 rule->arg.hlua_rule->fcn = *fcn;
7383
7384 /* TODO: later accept arguments. */
7385 rule->arg.hlua_rule->args = NULL;
7386
7387 /* Add applet pointer in the rule. */
7388 rule->applet.obj_type = OBJ_TYPE_APPLET;
7389 rule->applet.name = fcn->name;
7390 rule->applet.init = hlua_applet_tcp_init;
7391 rule->applet.fct = hlua_applet_tcp_fct;
7392 rule->applet.release = hlua_applet_tcp_release;
7393 rule->applet.timeout = hlua_timeout_applet;
7394
7395 return 0;
7396}
7397
7398/* This function is an LUA binding used for registering
7399 * "sample-conv" functions. It expects a converter name used
7400 * in the haproxy configuration file, and an LUA function.
7401 */
7402__LJMP static int hlua_register_service(lua_State *L)
7403{
7404 struct action_kw_list *akl;
7405 const char *name;
7406 const char *env;
7407 int ref;
7408 int len;
7409 struct hlua_function *fcn;
7410
7411 MAY_LJMP(check_args(L, 3, "register_service"));
7412
7413 /* First argument : converter name. */
7414 name = MAY_LJMP(luaL_checkstring(L, 1));
7415
7416 /* Second argument : environment. */
7417 env = MAY_LJMP(luaL_checkstring(L, 2));
7418
7419 /* Third argument : lua function. */
7420 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7421
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007422 /* Allocate and fill the sample fetch keyword struct. */
7423 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7424 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007425 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007426 fcn = calloc(1, sizeof(*fcn));
7427 if (!fcn)
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
7430 /* Fill fcn. */
7431 len = strlen("<lua.>") + strlen(name) + 1;
7432 fcn->name = calloc(1, len);
7433 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007434 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007435 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7436 fcn->function_ref = ref;
7437
7438 /* List head */
7439 akl->list.n = akl->list.p = NULL;
7440
7441 /* converter keyword. */
7442 len = strlen("lua.") + strlen(name) + 1;
7443 akl->kw[0].kw = calloc(1, len);
7444 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007445 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007446
7447 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7448
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007449 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007450 if (strcmp(env, "tcp") == 0)
7451 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007452 else if (strcmp(env, "http") == 0)
7453 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007454 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007455 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007456 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007457
7458 akl->kw[0].match_pfx = 0;
7459 akl->kw[0].private = fcn;
7460
7461 /* End of array. */
7462 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7463
7464 /* Register this new converter */
7465 service_keywords_register(akl);
7466
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007467 return 0;
7468}
7469
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007470/* This function initialises Lua cli handler. It copies the
7471 * arguments in the Lua stack and create channel IO objects.
7472 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007473static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007474{
7475 struct hlua *hlua;
7476 struct hlua_function *fcn;
7477 int i;
7478 const char *error;
7479
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007480 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007481 appctx->ctx.hlua_cli.fcn = private;
7482
Willy Tarreaubafbe012017-11-24 17:34:44 +01007483 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007484 if (!hlua) {
7485 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007486 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007487 }
7488 HLUA_INIT(hlua);
7489 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007490
7491 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007492 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007493 * applet_http. It is absolutely compatible.
7494 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007495 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007496 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007497 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007498 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007499 }
7500 appctx->ctx.hlua_cli.task->nice = 0;
7501 appctx->ctx.hlua_cli.task->context = appctx;
7502 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7503
7504 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007505 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007506 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007507 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007508 }
7509
7510 /* The following Lua calls can fail. */
7511 if (!SET_SAFE_LJMP(hlua->T)) {
7512 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7513 error = lua_tostring(hlua->T, -1);
7514 else
7515 error = "critical error";
7516 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7517 goto error;
7518 }
7519
7520 /* Check stack available size. */
7521 if (!lua_checkstack(hlua->T, 2)) {
7522 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7523 goto error;
7524 }
7525
7526 /* Restore the function in the stack. */
7527 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7528
7529 /* Once the arguments parsed, the CLI is like an AppletTCP,
7530 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007531 */
7532 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7533 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7534 goto error;
7535 }
7536 hlua->nargs = 1;
7537
7538 /* push keywords in the stack. */
7539 for (i = 0; *args[i]; i++) {
7540 /* Check stack available size. */
7541 if (!lua_checkstack(hlua->T, 1)) {
7542 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7543 goto error;
7544 }
7545 lua_pushstring(hlua->T, args[i]);
7546 hlua->nargs++;
7547 }
7548
7549 /* We must initialize the execution timeouts. */
7550 hlua->max_time = hlua_timeout_session;
7551
7552 /* At this point the execution is safe. */
7553 RESET_SAFE_LJMP(hlua->T);
7554
7555 /* It's ok */
7556 return 0;
7557
7558 /* It's not ok. */
7559error:
7560 RESET_SAFE_LJMP(hlua->T);
7561 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007562 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007563 return 1;
7564}
7565
7566static int hlua_cli_io_handler_fct(struct appctx *appctx)
7567{
7568 struct hlua *hlua;
7569 struct stream_interface *si;
7570 struct hlua_function *fcn;
7571
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007572 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007573 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007574 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007575
7576 /* If the stream is disconnect or closed, ldo nothing. */
7577 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7578 return 1;
7579
7580 /* Execute the function. */
7581 switch (hlua_ctx_resume(hlua, 1)) {
7582
7583 /* finished. */
7584 case HLUA_E_OK:
7585 return 1;
7586
7587 /* yield. */
7588 case HLUA_E_AGAIN:
7589 /* We want write. */
7590 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007591 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007592 /* Set the timeout. */
7593 if (hlua->wake_time != TICK_ETERNITY)
7594 task_schedule(hlua->task, hlua->wake_time);
7595 return 0;
7596
7597 /* finished with error. */
7598 case HLUA_E_ERRMSG:
7599 /* Display log. */
7600 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7601 fcn->name, lua_tostring(hlua->T, -1));
7602 lua_pop(hlua->T, 1);
7603 return 1;
7604
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007605 case HLUA_E_ETMOUT:
7606 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7607 fcn->name);
7608 return 1;
7609
7610 case HLUA_E_NOMEM:
7611 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7612 fcn->name);
7613 return 1;
7614
7615 case HLUA_E_YIELD: /* unexpected */
7616 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7617 fcn->name);
7618 return 1;
7619
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007620 case HLUA_E_ERR:
7621 /* Display log. */
7622 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7623 fcn->name);
7624 return 1;
7625
7626 default:
7627 return 1;
7628 }
7629
7630 return 1;
7631}
7632
7633static void hlua_cli_io_release_fct(struct appctx *appctx)
7634{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007635 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007636 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007637}
7638
7639/* This function is an LUA binding used for registering
7640 * new keywords in the cli. It expects a list of keywords
7641 * which are the "path". It is limited to 5 keywords. A
7642 * description of the command, a function to be executed
7643 * for the parsing and a function for io handlers.
7644 */
7645__LJMP static int hlua_register_cli(lua_State *L)
7646{
7647 struct cli_kw_list *cli_kws;
7648 const char *message;
7649 int ref_io;
7650 int len;
7651 struct hlua_function *fcn;
7652 int index;
7653 int i;
7654
7655 MAY_LJMP(check_args(L, 3, "register_cli"));
7656
7657 /* First argument : an array of maximum 5 keywords. */
7658 if (!lua_istable(L, 1))
7659 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7660
7661 /* Second argument : string with contextual message. */
7662 message = MAY_LJMP(luaL_checkstring(L, 2));
7663
7664 /* Third and fourth argument : lua function. */
7665 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7666
7667 /* Allocate and fill the sample fetch keyword struct. */
7668 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7669 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007670 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007671 fcn = calloc(1, sizeof(*fcn));
7672 if (!fcn)
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
7675 /* Fill path. */
7676 index = 0;
7677 lua_pushnil(L);
7678 while(lua_next(L, 1) != 0) {
7679 if (index >= 5)
7680 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7681 if (lua_type(L, -1) != LUA_TSTRING)
7682 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7683 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7684 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007685 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007686 index++;
7687 lua_pop(L, 1);
7688 }
7689
7690 /* Copy help message. */
7691 cli_kws->kw[0].usage = strdup(message);
7692 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007693 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007694
7695 /* Fill fcn io handler. */
7696 len = strlen("<lua.cli>") + 1;
7697 for (i = 0; i < index; i++)
7698 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7699 fcn->name = calloc(1, len);
7700 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007701 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007702 strncat((char *)fcn->name, "<lua.cli", len);
7703 for (i = 0; i < index; i++) {
7704 strncat((char *)fcn->name, ".", len);
7705 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7706 }
7707 strncat((char *)fcn->name, ">", len);
7708 fcn->function_ref = ref_io;
7709
7710 /* Fill last entries. */
7711 cli_kws->kw[0].private = fcn;
7712 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7713 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7714 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7715
7716 /* Register this new converter */
7717 cli_register_kw(cli_kws);
7718
7719 return 0;
7720}
7721
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007722static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7723 struct proxy *defpx, const char *file, int line,
7724 char **err, unsigned int *timeout)
7725{
7726 const char *error;
7727
7728 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007729 if (error == PARSE_TIME_OVER) {
7730 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7731 args[1], args[0]);
7732 return -1;
7733 }
7734 else if (error == PARSE_TIME_UNDER) {
7735 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7736 args[1], args[0]);
7737 return -1;
7738 }
7739 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007740 memprintf(err, "%s: invalid timeout", args[0]);
7741 return -1;
7742 }
7743 return 0;
7744}
7745
7746static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7747 struct proxy *defpx, const char *file, int line,
7748 char **err)
7749{
7750 return hlua_read_timeout(args, section_type, curpx, defpx,
7751 file, line, err, &hlua_timeout_session);
7752}
7753
7754static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7755 struct proxy *defpx, const char *file, int line,
7756 char **err)
7757{
7758 return hlua_read_timeout(args, section_type, curpx, defpx,
7759 file, line, err, &hlua_timeout_task);
7760}
7761
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007762static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7763 struct proxy *defpx, const char *file, int line,
7764 char **err)
7765{
7766 return hlua_read_timeout(args, section_type, curpx, defpx,
7767 file, line, err, &hlua_timeout_applet);
7768}
7769
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007770static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7771 struct proxy *defpx, const char *file, int line,
7772 char **err)
7773{
7774 char *error;
7775
7776 hlua_nb_instruction = strtoll(args[1], &error, 10);
7777 if (*error != '\0') {
7778 memprintf(err, "%s: invalid number", args[0]);
7779 return -1;
7780 }
7781 return 0;
7782}
7783
Willy Tarreau32f61e22015-03-18 17:54:59 +01007784static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7785 struct proxy *defpx, const char *file, int line,
7786 char **err)
7787{
7788 char *error;
7789
7790 if (*(args[1]) == 0) {
7791 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7792 return -1;
7793 }
7794 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7795 if (*error != '\0') {
7796 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7797 return -1;
7798 }
7799 return 0;
7800}
7801
7802
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007803/* This function is called by the main configuration key "lua-load". It loads and
7804 * execute an lua file during the parsing of the HAProxy configuration file. It is
7805 * the main lua entry point.
7806 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007807 * This function runs with the HAProxy keywords API. It returns -1 if an error
7808 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007809 *
7810 * In some error case, LUA set an error message in top of the stack. This function
7811 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007812 *
7813 * This function can fail with an abort() due to an Lua critical error.
7814 * We are in the configuration parsing process of HAProxy, this abort() is
7815 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007816 */
7817static int hlua_load(char **args, int section_type, struct proxy *curpx,
7818 struct proxy *defpx, const char *file, int line,
7819 char **err)
7820{
7821 int error;
7822
7823 /* Just load and compile the file. */
7824 error = luaL_loadfile(gL.T, args[1]);
7825 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007826 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007827 lua_pop(gL.T, 1);
7828 return -1;
7829 }
7830
7831 /* If no syntax error where detected, execute the code. */
7832 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7833 switch (error) {
7834 case LUA_OK:
7835 break;
7836 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007837 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007838 lua_pop(gL.T, 1);
7839 return -1;
7840 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007841 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007842 return -1;
7843 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007844 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007845 lua_pop(gL.T, 1);
7846 return -1;
7847 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007848 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007849 lua_pop(gL.T, 1);
7850 return -1;
7851 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05007852 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007853 lua_pop(gL.T, 1);
7854 return -1;
7855 }
7856
7857 return 0;
7858}
7859
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01007860/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
7861 * in the given <ctx>.
7862 */
7863static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
7864{
7865 lua_getglobal(ctx.T, "package"); /* push package variable */
7866 lua_pushstring(ctx.T, path); /* push given path */
7867 lua_pushstring(ctx.T, ";"); /* push semicolon */
7868 lua_getfield(ctx.T, -3, type); /* push old path */
7869 lua_concat(ctx.T, 3); /* concatenate to new path */
7870 lua_setfield(ctx.T, -2, type); /* store new path */
7871 lua_pop(ctx.T, 1); /* pop package variable */
7872
7873 return 0;
7874}
7875
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007876static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
7877 struct proxy *defpx, const char *file, int line,
7878 char **err)
7879{
7880 char *path;
7881 char *type = "path";
7882 if (too_many_args(2, args, err, NULL)) {
7883 return -1;
7884 }
7885
7886 if (!(*args[1])) {
7887 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
7888 return -1;
7889 }
7890 path = args[1];
7891
7892 if (*args[2]) {
7893 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
7894 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
7895 return -1;
7896 }
7897 type = args[2];
7898 }
7899
7900 return hlua_prepend_path(gL, type, path);
7901}
7902
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007903/* configuration keywords declaration */
7904static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007905 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007906 { CFG_GLOBAL, "lua-load", hlua_load },
7907 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7908 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007909 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007910 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007911 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007912 { 0, NULL, NULL },
7913}};
7914
Willy Tarreau0108d902018-11-25 19:14:37 +01007915INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7916
Christopher Fauletafd8f102018-11-08 11:34:21 +01007917
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007918/* This function can fail with an abort() due to an Lua critical error.
7919 * We are in the initialisation process of HAProxy, this abort() is
7920 * tolerated.
7921 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007922int hlua_post_init()
7923{
7924 struct hlua_init_function *init;
7925 const char *msg;
7926 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007927 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007928
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007929 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007930 if (!SET_SAFE_LJMP(gL.T)) {
7931 if (lua_type(gL.T, -1) == LUA_TSTRING)
7932 error = lua_tostring(gL.T, -1);
7933 else
7934 error = "critical error";
7935 fprintf(stderr, "Lua post-init: %s.\n", error);
7936 exit(1);
7937 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007938
7939#if USE_OPENSSL
7940 /* Initialize SSL server. */
7941 if (socket_ssl.xprt->prepare_srv) {
7942 int saved_used_backed = global.ssl_used_backend;
7943 // don't affect maxconn automatic computation
7944 socket_ssl.xprt->prepare_srv(&socket_ssl);
7945 global.ssl_used_backend = saved_used_backed;
7946 }
7947#endif
7948
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007949 hlua_fcn_post_init(gL.T);
7950 RESET_SAFE_LJMP(gL.T);
7951
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007952 list_for_each_entry(init, &hlua_init_functions, l) {
7953 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7954 ret = hlua_ctx_resume(&gL, 0);
7955 switch (ret) {
7956 case HLUA_E_OK:
7957 lua_pop(gL.T, -1);
7958 return 1;
7959 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007960 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007961 return 0;
7962 case HLUA_E_ERRMSG:
7963 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007964 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007965 return 0;
7966 case HLUA_E_ERR:
7967 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007968 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007969 return 0;
7970 }
7971 }
7972 return 1;
7973}
7974
Willy Tarreau32f61e22015-03-18 17:54:59 +01007975/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7976 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7977 * is the previously allocated size or the kind of object in case of a new
7978 * allocation. <nsize> is the requested new size.
7979 */
7980static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7981{
7982 struct hlua_mem_allocator *zone = ud;
7983
7984 if (nsize == 0) {
7985 /* it's a free */
7986 if (ptr)
7987 zone->allocated -= osize;
7988 free(ptr);
7989 return NULL;
7990 }
7991
7992 if (!ptr) {
7993 /* it's a new allocation */
7994 if (zone->limit && zone->allocated + nsize > zone->limit)
7995 return NULL;
7996
7997 ptr = malloc(nsize);
7998 if (ptr)
7999 zone->allocated += nsize;
8000 return ptr;
8001 }
8002
8003 /* it's a realloc */
8004 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8005 return NULL;
8006
8007 ptr = realloc(ptr, nsize);
8008 if (ptr)
8009 zone->allocated += nsize - osize;
8010 return ptr;
8011}
8012
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008013/* Ithis function can fail with an abort() due to an Lua critical error.
8014 * We are in the initialisation process of HAProxy, this abort() is
8015 * tolerated.
8016 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008017void hlua_init(void)
8018{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008019 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008020 int idx;
8021 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008022 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008023 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008024 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008025#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008026 struct srv_kw *kw;
8027 int tmp_error;
8028 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008029 char *args[] = { /* SSL client configuration. */
8030 "ssl",
8031 "verify",
8032 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008033 NULL
8034 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008035#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008036
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008037 /* Init main lua stack. */
8038 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008039 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008040 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008041 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008042 hlua_sethlua(&gL);
8043 gL.Tref = LUA_REFNIL;
8044 gL.task = NULL;
8045
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008046 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008047 * the Lua function can fail with an abort. We are in the initialisation
8048 * process of HAProxy, this abort() is tolerated.
8049 */
8050
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008051 /* Initialise lua. */
8052 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008053#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8054#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8055#ifdef HLUA_PREPEND_PATH
8056 hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
8057#endif
8058#ifdef HLUA_PREPEND_CPATH
8059 hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
8060#endif
8061#undef HLUA_PREPEND_PATH_TOSTRING
8062#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008063
Thierry Fournier75933d42016-01-21 09:30:18 +01008064 /* Set safe environment for the initialisation. */
8065 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008066 if (lua_type(gL.T, -1) == LUA_TSTRING)
8067 error_msg = lua_tostring(gL.T, -1);
8068 else
8069 error_msg = "critical error";
8070 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008071 exit(1);
8072 }
8073
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008074 /*
8075 *
8076 * Create "core" object.
8077 *
8078 */
8079
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008080 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008081 lua_newtable(gL.T);
8082
8083 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008084 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008085 hlua_class_const_int(gL.T, log_levels[i], i);
8086
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008087 /* Register special functions. */
8088 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008089 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008090 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008091 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008092 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008093 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008094 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008095 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008096 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008097 hlua_class_function(gL.T, "sleep", hlua_sleep);
8098 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008099 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8100 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8101 hlua_class_function(gL.T, "set_map", hlua_set_map);
8102 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008103 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008104 hlua_class_function(gL.T, "log", hlua_log);
8105 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8106 hlua_class_function(gL.T, "Info", hlua_log_info);
8107 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8108 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008109 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008110 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008111
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008112 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008113
8114 /*
8115 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008116 * Create "act" object.
8117 *
8118 */
8119
8120 /* This table entry is the object "act" base. */
8121 lua_newtable(gL.T);
8122
8123 /* push action return constants */
8124 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8125 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8126 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8127 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8128 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8129 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8130 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8131 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8132
Christopher Faulet501465d2020-02-26 14:54:16 +01008133 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008134
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008135 lua_setglobal(gL.T, "act");
8136
8137 /*
8138 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008139 * Register class Map
8140 *
8141 */
8142
8143 /* This table entry is the object "Map" base. */
8144 lua_newtable(gL.T);
8145
8146 /* register pattern types. */
8147 for (i=0; i<PAT_MATCH_NUM; i++)
8148 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008149 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008150 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8151 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008152 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008153
8154 /* register constructor. */
8155 hlua_class_function(gL.T, "new", hlua_map_new);
8156
8157 /* Create and fill the metatable. */
8158 lua_newtable(gL.T);
8159
Ilya Shipitsind4259502020-04-08 01:07:56 +05008160 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008161 lua_pushstring(gL.T, "__index");
8162 lua_newtable(gL.T);
8163
8164 /* Register . */
8165 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8166 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8167
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008168 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008169
Thierry Fournier45e78d72016-02-19 18:34:46 +01008170 /* Register previous table in the registry with reference and named entry.
8171 * The function hlua_register_metatable() pops the stack, so we
8172 * previously create a copy of the table.
8173 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008174 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008175 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008176
8177 /* Assign the metatable to the mai Map object. */
8178 lua_setmetatable(gL.T, -2);
8179
8180 /* Set a name to the table. */
8181 lua_setglobal(gL.T, "Map");
8182
8183 /*
8184 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008185 * Register class Channel
8186 *
8187 */
8188
8189 /* Create and fill the metatable. */
8190 lua_newtable(gL.T);
8191
Ilya Shipitsind4259502020-04-08 01:07:56 +05008192 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008193 lua_pushstring(gL.T, "__index");
8194 lua_newtable(gL.T);
8195
8196 /* Register . */
8197 hlua_class_function(gL.T, "get", hlua_channel_get);
8198 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8199 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8200 hlua_class_function(gL.T, "set", hlua_channel_set);
8201 hlua_class_function(gL.T, "append", hlua_channel_append);
8202 hlua_class_function(gL.T, "send", hlua_channel_send);
8203 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8204 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8205 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008206 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008207 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008208
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008209 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008210
8211 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008212 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008213
8214 /*
8215 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008216 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008217 *
8218 */
8219
8220 /* Create and fill the metatable. */
8221 lua_newtable(gL.T);
8222
Ilya Shipitsind4259502020-04-08 01:07:56 +05008223 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008224 lua_pushstring(gL.T, "__index");
8225 lua_newtable(gL.T);
8226
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008227 /* Browse existing fetches and create the associated
8228 * object method.
8229 */
8230 sf = NULL;
8231 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8232
8233 /* Dont register the keywork if the arguments check function are
8234 * not safe during the runtime.
8235 */
8236 if ((sf->val_args != NULL) &&
8237 (sf->val_args != val_payload_lv) &&
8238 (sf->val_args != val_hdr))
8239 continue;
8240
8241 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8242 * by an underscore.
8243 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008244 strncpy(trash.area, sf->kw, trash.size);
8245 trash.area[trash.size - 1] = '\0';
8246 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008247 if (*p == '.' || *p == '-' || *p == '+')
8248 *p = '_';
8249
8250 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008251 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008252 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008253 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008254 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008255 }
8256
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008257 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008258
8259 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008260 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008261
8262 /*
8263 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008264 * Register class Converters
8265 *
8266 */
8267
8268 /* Create and fill the metatable. */
8269 lua_newtable(gL.T);
8270
8271 /* Create and fill the __index entry. */
8272 lua_pushstring(gL.T, "__index");
8273 lua_newtable(gL.T);
8274
8275 /* Browse existing converters and create the associated
8276 * object method.
8277 */
8278 sc = NULL;
8279 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8280 /* Dont register the keywork if the arguments check function are
8281 * not safe during the runtime.
8282 */
8283 if (sc->val_args != NULL)
8284 continue;
8285
8286 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8287 * by an underscore.
8288 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008289 strncpy(trash.area, sc->kw, trash.size);
8290 trash.area[trash.size - 1] = '\0';
8291 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008292 if (*p == '.' || *p == '-' || *p == '+')
8293 *p = '_';
8294
8295 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008296 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008297 lua_pushlightuserdata(gL.T, sc);
8298 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008299 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008300 }
8301
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008302 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008303
8304 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008305 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008306
8307 /*
8308 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008309 * Register class HTTP
8310 *
8311 */
8312
8313 /* Create and fill the metatable. */
8314 lua_newtable(gL.T);
8315
Ilya Shipitsind4259502020-04-08 01:07:56 +05008316 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008317 lua_pushstring(gL.T, "__index");
8318 lua_newtable(gL.T);
8319
8320 /* Register Lua functions. */
8321 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8322 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8323 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8324 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8325 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8326 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8327 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8328 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8329 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8330 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8331
8332 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8333 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8334 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8335 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8336 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8337 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008338 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008339
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008340 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008341
8342 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008343 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008344
8345 /*
8346 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008347 * Register class AppletTCP
8348 *
8349 */
8350
8351 /* Create and fill the metatable. */
8352 lua_newtable(gL.T);
8353
Ilya Shipitsind4259502020-04-08 01:07:56 +05008354 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008355 lua_pushstring(gL.T, "__index");
8356 lua_newtable(gL.T);
8357
8358 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008359 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8360 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8361 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8362 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8363 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008364 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8365 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8366 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008367
8368 lua_settable(gL.T, -3);
8369
8370 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008371 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008372
8373 /*
8374 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008375 * Register class AppletHTTP
8376 *
8377 */
8378
8379 /* Create and fill the metatable. */
8380 lua_newtable(gL.T);
8381
Ilya Shipitsind4259502020-04-08 01:07:56 +05008382 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008383 lua_pushstring(gL.T, "__index");
8384 lua_newtable(gL.T);
8385
8386 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008387 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8388 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008389 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8390 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8391 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008392 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8393 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8394 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8395 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8396 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8397 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8398
8399 lua_settable(gL.T, -3);
8400
8401 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008402 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008403
8404 /*
8405 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008406 * Register class TXN
8407 *
8408 */
8409
8410 /* Create and fill the metatable. */
8411 lua_newtable(gL.T);
8412
Ilya Shipitsind4259502020-04-08 01:07:56 +05008413 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008414 lua_pushstring(gL.T, "__index");
8415 lua_newtable(gL.T);
8416
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008417 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008418 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8419 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8420 hlua_class_function(gL.T, "set_var", hlua_set_var);
8421 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8422 hlua_class_function(gL.T, "get_var", hlua_get_var);
8423 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008424 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008425 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8426 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8427 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8428 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8429 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8430 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8431 hlua_class_function(gL.T, "log", hlua_txn_log);
8432 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8433 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8434 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8435 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008436
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008437 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008438
8439 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008440 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008441
8442 /*
8443 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008444 * Register class reply
8445 *
8446 */
8447 lua_newtable(gL.T);
8448 lua_pushstring(gL.T, "__index");
8449 lua_newtable(gL.T);
8450 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8451 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8452 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8453 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8454 lua_settable(gL.T, -3); /* Sets the __index entry. */
8455 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8456
8457
8458 /*
8459 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008460 * Register class Socket
8461 *
8462 */
8463
8464 /* Create and fill the metatable. */
8465 lua_newtable(gL.T);
8466
Ilya Shipitsind4259502020-04-08 01:07:56 +05008467 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008468 lua_pushstring(gL.T, "__index");
8469 lua_newtable(gL.T);
8470
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008471#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008472 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008473#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008474 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8475 hlua_class_function(gL.T, "send", hlua_socket_send);
8476 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8477 hlua_class_function(gL.T, "close", hlua_socket_close);
8478 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8479 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8480 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8481 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8482
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008483 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008484
8485 /* Register the garbage collector entry. */
8486 lua_pushstring(gL.T, "__gc");
8487 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
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 previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008491 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008492
8493 /* Proxy and server configuration initialisation. */
8494 memset(&socket_proxy, 0, sizeof(socket_proxy));
8495 init_new_proxy(&socket_proxy);
8496 socket_proxy.parent = NULL;
8497 socket_proxy.last_change = now.tv_sec;
8498 socket_proxy.id = "LUA-SOCKET";
8499 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8500 socket_proxy.maxconn = 0;
8501 socket_proxy.accept = NULL;
8502 socket_proxy.options2 |= PR_O2_INDEPSTR;
8503 socket_proxy.srv = NULL;
8504 socket_proxy.conn_retries = 0;
8505 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8506
8507 /* Init TCP server: unchanged parameters */
8508 memset(&socket_tcp, 0, sizeof(socket_tcp));
8509 socket_tcp.next = NULL;
8510 socket_tcp.proxy = &socket_proxy;
8511 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8512 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008513 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008514 socket_tcp.idle_conns = NULL;
8515 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008516 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008517 socket_tcp.last_change = 0;
8518 socket_tcp.id = "LUA-TCP-CONN";
8519 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8520 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8521 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8522
8523 /* XXX: Copy default parameter from default server,
8524 * but the default server is not initialized.
8525 */
8526 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8527 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8528 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8529 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8530 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8531 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8532 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8533 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8534 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8535 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8536
8537 socket_tcp.check.status = HCHK_STATUS_INI;
8538 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8539 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8540 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8541 socket_tcp.check.server = &socket_tcp;
8542
8543 socket_tcp.agent.status = HCHK_STATUS_INI;
8544 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8545 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8546 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8547 socket_tcp.agent.server = &socket_tcp;
8548
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008549 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008550
8551#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008552 /* Init TCP server: unchanged parameters */
8553 memset(&socket_ssl, 0, sizeof(socket_ssl));
8554 socket_ssl.next = NULL;
8555 socket_ssl.proxy = &socket_proxy;
8556 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8557 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008558 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008559 socket_ssl.idle_conns = NULL;
8560 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008561 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008562 socket_ssl.last_change = 0;
8563 socket_ssl.id = "LUA-SSL-CONN";
8564 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8565 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8566 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8567
8568 /* XXX: Copy default parameter from default server,
8569 * but the default server is not initialized.
8570 */
8571 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8572 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8573 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8574 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8575 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8576 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8577 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8578 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8579 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8580 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8581
8582 socket_ssl.check.status = HCHK_STATUS_INI;
8583 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8584 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8585 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8586 socket_ssl.check.server = &socket_ssl;
8587
8588 socket_ssl.agent.status = HCHK_STATUS_INI;
8589 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8590 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8591 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8592 socket_ssl.agent.server = &socket_ssl;
8593
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008594 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008595 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008596
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008597 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008598 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8599 /*
8600 *
8601 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008602 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008603 * features like client certificates and ssl_verify.
8604 *
8605 */
8606 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8607 if (tmp_error != 0) {
8608 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8609 abort(); /* This must be never arrives because the command line
8610 not editable by the user. */
8611 }
8612 idx += kw->skip;
8613 }
8614 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008615#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008616
8617 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008618}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008619
Willy Tarreau80713382018-11-26 10:19:54 +01008620static void hlua_register_build_options(void)
8621{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008622 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008623
Willy Tarreaubb57d942016-12-21 19:04:56 +01008624 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8625 hap_register_build_opts(ptr, 1);
8626}
Willy Tarreau80713382018-11-26 10:19:54 +01008627
8628INITCALL0(STG_REGISTER, hlua_register_build_options);