blob: 15c34cd325140fd15201048ad70a635c498d790b [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 Tarreau4c7e4b72020-05-27 12:58:42 +020024#include <haproxy/api.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020025#include <import/ebpttree.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010026
27#include <common/cfgparse.h>
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +020028#include <common/hathreads.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010029#include <common/xref.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010030#include <common/h1.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010031
William Lallemand9ed62032016-11-21 17:49:11 +010032#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010033#include <types/hlua.h>
34#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010035#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010036
Thierry FOURNIER55da1652015-01-23 11:36:30 +010037#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020038#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010039#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010040#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010041#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010042#include <proto/stats.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010043#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010044#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020045#include <proto/http_fetch.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010046#include <proto/http_htx.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020047#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020048#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010049#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040050#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010051#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010052#include <proto/payload.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020053#include <proto/http_ana.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010054#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010055#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020056#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020057#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010058#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010059#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010060#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020061#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010062
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010063/* Lua uses longjmp to perform yield or throwing errors. This
64 * macro is used only for identifying the function that can
65 * not return because a longjmp is executed.
66 * __LJMP marks a prototype of hlua file that can use longjmp.
67 * WILL_LJMP() marks an lua function that will use longjmp.
68 * MAY_LJMP() marks an lua function that may use longjmp.
69 */
70#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020071#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010072#define MAY_LJMP(func) func
73
Thierry FOURNIERbabae282015-09-17 11:36:37 +020074/* This couple of function executes securely some Lua calls outside of
75 * the lua runtime environment. Each Lua call can return a longjmp
76 * if it encounter a memory error.
77 *
78 * Lua documentation extract:
79 *
80 * If an error happens outside any protected environment, Lua calls
81 * a panic function (see lua_atpanic) and then calls abort, thus
82 * exiting the host application. Your panic function can avoid this
83 * exit by never returning (e.g., doing a long jump to your own
84 * recovery point outside Lua).
85 *
86 * The panic function runs as if it were a message handler (see
87 * §2.3); in particular, the error message is at the top of the
88 * stack. However, there is no guarantee about stack space. To push
89 * anything on the stack, the panic function must first check the
90 * available space (see §4.2).
91 *
92 * We must check all the Lua entry point. This includes:
93 * - The include/proto/hlua.h exported functions
94 * - the task wrapper function
95 * - The action wrapper function
96 * - The converters wrapper function
97 * - The sample-fetch wrapper functions
98 *
99 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800100 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200101 *
102 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
103 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
104 * because they must be exists in the program stack when the longjmp
105 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200106 *
107 * Note that the Lua processing is not really thread safe. It provides
108 * heavy system which consists to add our own lock function in the Lua
109 * code and recompile the library. This system will probably not accepted
110 * by maintainers of various distribs.
111 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500112 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200113 * quick looking on the Lua sources displays a lua_lock() a the start
114 * of function and a lua_unlock() at the end of the function. So I
115 * conclude that the Lua thread safe mode just perform a mutex around
116 * all execution. So I prefer to do this in the HAProxy code, it will be
117 * easier for distro maintainers.
118 *
119 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
120 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
121 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200122 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100123__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200124THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200125static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100126static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200127
128#define SET_SAFE_LJMP(__L) \
129 ({ \
130 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100131 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200132 if (setjmp(safe_ljmp_env) != 0) { \
133 lua_atpanic(__L, hlua_panic_safe); \
134 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100135 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200136 } else { \
137 lua_atpanic(__L, hlua_panic_ljmp); \
138 ret = 1; \
139 } \
140 ret; \
141 })
142
143/* If we are the last function catching Lua errors, we
144 * must reset the panic function.
145 */
146#define RESET_SAFE_LJMP(__L) \
147 do { \
148 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100149 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200150 } while(0)
151
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200152/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200153#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100154/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200155#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200156/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100157#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100158#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200159
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100160/* The main Lua execution context. */
161struct hlua gL;
162
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100163/* This is the memory pool containing struct lua for applets
164 * (including cli).
165 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100166DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100167
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100168/* Used for Socket connection. */
169static struct proxy socket_proxy;
170static struct server socket_tcp;
171#ifdef USE_OPENSSL
172static struct server socket_ssl;
173#endif
174
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100175/* List head of the function called at the initialisation time. */
176struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
177
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100178/* The following variables contains the reference of the different
179 * Lua classes. These references are useful for identify metadata
180 * associated with an object.
181 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100182static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100183static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100184static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100185static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100186static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100187static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200188static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200189static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200190static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100191static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100192
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100193/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200194 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100195 * short timeout. Lua linked with tasks doesn't have a timeout
196 * because a task may remain alive during all the haproxy execution.
197 */
198static unsigned int hlua_timeout_session = 4000; /* session timeout. */
199static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200200static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100201
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100202/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
203 * it is used for preventing infinite loops.
204 *
205 * I test the scheer with an infinite loop containing one incrementation
206 * and one test. I run this loop between 10 seconds, I raise a ceil of
207 * 710M loops from one interrupt each 9000 instructions, so I fix the value
208 * to one interrupt each 10 000 instructions.
209 *
210 * configured | Number of
211 * instructions | loops executed
212 * between two | in milions
213 * forced yields |
214 * ---------------+---------------
215 * 10 | 160
216 * 500 | 670
217 * 1000 | 680
218 * 5000 | 700
219 * 7000 | 700
220 * 8000 | 700
221 * 9000 | 710 <- ceil
222 * 10000 | 710
223 * 100000 | 710
224 * 1000000 | 710
225 *
226 */
227static unsigned int hlua_nb_instruction = 10000;
228
Willy Tarreau32f61e22015-03-18 17:54:59 +0100229/* Descriptor for the memory allocation state. If limit is not null, it will
230 * be enforced on any memory allocation.
231 */
232struct hlua_mem_allocator {
233 size_t allocated;
234 size_t limit;
235};
236
237static struct hlua_mem_allocator hlua_global_allocator;
238
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100239/* These functions converts types between HAProxy internal args or
240 * sample and LUA types. Another function permits to check if the
241 * LUA stack contains arguments according with an required ARG_T
242 * format.
243 */
244static int hlua_arg2lua(lua_State *L, const struct arg *arg);
245static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100246__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100247 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100248static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100249static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100250static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
251
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100252__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200253
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200254#define SEND_ERR(__be, __fmt, __args...) \
255 do { \
256 send_log(__be, LOG_ERR, __fmt, ## __args); \
257 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100258 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200259 } while (0)
260
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100261/* Used to check an Lua function type in the stack. It creates and
262 * returns a reference of the function. This function throws an
263 * error if the rgument is not a "function".
264 */
265__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
266{
267 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100268 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100269 WILL_LJMP(luaL_argerror(L, argno, msg));
270 }
271 lua_pushvalue(L, argno);
272 return luaL_ref(L, LUA_REGISTRYINDEX);
273}
274
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200275/* Return the string that is of the top of the stack. */
276const char *hlua_get_top_error_string(lua_State *L)
277{
278 if (lua_gettop(L) < 1)
279 return "unknown error";
280 if (lua_type(L, -1) != LUA_TSTRING)
281 return "unknown error";
282 return lua_tostring(L, -1);
283}
284
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200285__LJMP static const char *hlua_traceback(lua_State *L)
286{
287 lua_Debug ar;
288 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200289 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200290 int filled = 0;
291
292 while (lua_getstack(L, level++, &ar)) {
293
294 /* Add separator */
295 if (filled)
296 chunk_appendf(msg, ", ");
297 filled = 1;
298
299 /* Fill fields:
300 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
301 * 'l': fills in the field currentline;
302 * 'n': fills in the field name and namewhat;
303 * 't': fills in the field istailcall;
304 */
305 lua_getinfo(L, "Slnt", &ar);
306
307 /* Append code localisation */
308 if (ar.currentline > 0)
309 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
310 else
311 chunk_appendf(msg, "%s ", ar.short_src);
312
313 /*
314 * Get function name
315 *
316 * if namewhat is no empty, name is defined.
317 * what contains "Lua" for Lua function, "C" for C function,
318 * or "main" for main code.
319 */
320 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
321 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
322
323 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
324 chunk_appendf(msg, "main chunk");
325
326 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
327 chunk_appendf(msg, "C function line %d", ar.linedefined);
328
329 else /* nothing left... */
330 chunk_appendf(msg, "?");
331
332
333 /* Display tailed call */
334 if (ar.istailcall)
335 chunk_appendf(msg, " ...");
336 }
337
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200338 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200339}
340
341
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100342/* This function check the number of arguments available in the
343 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500344 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100345 */
346__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
347{
348 if (lua_gettop(L) == nb)
349 return;
350 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
351}
352
Mark Lakes22154b42018-01-29 14:38:40 -0800353/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100354 * and the line number where the error is encountered.
355 */
356static int hlua_pusherror(lua_State *L, const char *fmt, ...)
357{
358 va_list argp;
359 va_start(argp, fmt);
360 luaL_where(L, 1);
361 lua_pushvfstring(L, fmt, argp);
362 va_end(argp);
363 lua_concat(L, 2);
364 return 1;
365}
366
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100367/* This functions is used with sample fetch and converters. It
368 * converts the HAProxy configuration argument in a lua stack
369 * values.
370 *
371 * It takes an array of "arg", and each entry of the array is
372 * converted and pushed in the LUA stack.
373 */
374static int hlua_arg2lua(lua_State *L, const struct arg *arg)
375{
376 switch (arg->type) {
377 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100378 case ARGT_TIME:
379 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100380 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100381 break;
382
383 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200384 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100385 break;
386
387 case ARGT_IPV4:
388 case ARGT_IPV6:
389 case ARGT_MSK4:
390 case ARGT_MSK6:
391 case ARGT_FE:
392 case ARGT_BE:
393 case ARGT_TAB:
394 case ARGT_SRV:
395 case ARGT_USR:
396 case ARGT_MAP:
397 default:
398 lua_pushnil(L);
399 break;
400 }
401 return 1;
402}
403
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500404/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100405 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500406 * with sample fetch wrappers. The input arguments are given to the
407 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100408 */
409static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
410{
411 switch (lua_type(L, ud)) {
412
413 case LUA_TNUMBER:
414 case LUA_TBOOLEAN:
415 arg->type = ARGT_SINT;
416 arg->data.sint = lua_tointeger(L, ud);
417 break;
418
419 case LUA_TSTRING:
420 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200421 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200422 /* We don't know the actual size of the underlying allocation, so be conservative. */
423 arg->data.str.size = arg->data.str.data;
424 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100425 break;
426
427 case LUA_TUSERDATA:
428 case LUA_TNIL:
429 case LUA_TTABLE:
430 case LUA_TFUNCTION:
431 case LUA_TTHREAD:
432 case LUA_TLIGHTUSERDATA:
433 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200434 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100435 break;
436 }
437 return 1;
438}
439
440/* the following functions are used to convert a struct sample
441 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500442 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100443 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100444static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100445{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200446 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100448 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200449 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100450 break;
451
452 case SMP_T_BIN:
453 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200454 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100455 break;
456
457 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200458 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100459 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
460 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
461 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
462 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
463 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
464 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
465 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
466 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
467 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200468 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100469 break;
470 default:
471 lua_pushnil(L);
472 break;
473 }
474 break;
475
476 case SMP_T_IPV4:
477 case SMP_T_IPV6:
478 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200479 if (sample_casts[smp->data.type][SMP_T_STR] &&
480 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200481 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100482 else
483 lua_pushnil(L);
484 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100485 default:
486 lua_pushnil(L);
487 break;
488 }
489 return 1;
490}
491
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100492/* the following functions are used to convert a struct sample
493 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500494 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100495 */
496static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
497{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200498 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100499
500 case SMP_T_BIN:
501 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200502 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100503 break;
504
505 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200506 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100507 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
508 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
509 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
510 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
511 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
512 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
513 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
514 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
515 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200516 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100517 break;
518 default:
519 lua_pushstring(L, "");
520 break;
521 }
522 break;
523
524 case SMP_T_SINT:
525 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100526 case SMP_T_IPV4:
527 case SMP_T_IPV6:
528 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200529 if (sample_casts[smp->data.type][SMP_T_STR] &&
530 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200531 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100532 else
533 lua_pushstring(L, "");
534 break;
535 default:
536 lua_pushstring(L, "");
537 break;
538 }
539 return 1;
540}
541
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100542/* the following functions are used to convert an Lua type in a
543 * struct sample. This is useful to provide data from a converter
544 * to the LUA code.
545 */
546static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
547{
548 switch (lua_type(L, ud)) {
549
550 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200551 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200552 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100553 break;
554
555
556 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200557 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200558 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100559 break;
560
561 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200562 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100563 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200564 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200565 /* We don't know the actual size of the underlying allocation, so be conservative. */
566 smp->data.u.str.size = smp->data.u.str.data;
567 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100568 break;
569
570 case LUA_TUSERDATA:
571 case LUA_TNIL:
572 case LUA_TTABLE:
573 case LUA_TFUNCTION:
574 case LUA_TTHREAD:
575 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200576 case LUA_TNONE:
577 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200578 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200579 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100580 break;
581 }
582 return 1;
583}
584
Ilya Shipitsind4259502020-04-08 01:07:56 +0500585/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800586 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100587 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100588 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500589 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100590 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100591 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100592__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100593 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100594{
595 int min_arg;
596 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100597 struct proxy *px;
598 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100599
600 idx = 0;
601 min_arg = ARGM(mask);
602 mask >>= ARGM_BITS;
603
604 while (1) {
605
606 /* Check oversize. */
607 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100608 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100609 }
610
611 /* Check for mandatory arguments. */
612 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100613 if (idx < min_arg) {
614
615 /* If miss other argument than the first one, we return an error. */
616 if (idx > 0)
617 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
618
619 /* If first argument have a certain type, some default values
620 * may be used. See the function smp_resolve_args().
621 */
622 switch (mask & ARGT_MASK) {
623
624 case ARGT_FE:
625 if (!(p->cap & PR_CAP_FE))
626 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
627 argp[idx].data.prx = p;
628 argp[idx].type = ARGT_FE;
629 argp[idx+1].type = ARGT_STOP;
630 break;
631
632 case ARGT_BE:
633 if (!(p->cap & PR_CAP_BE))
634 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
635 argp[idx].data.prx = p;
636 argp[idx].type = ARGT_BE;
637 argp[idx+1].type = ARGT_STOP;
638 break;
639
640 case ARGT_TAB:
641 argp[idx].data.prx = p;
642 argp[idx].type = ARGT_TAB;
643 argp[idx+1].type = ARGT_STOP;
644 break;
645
646 default:
647 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
648 break;
649 }
650 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100651 return 0;
652 }
653
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500654 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100655 if ((mask & ARGT_MASK) == ARGT_STOP &&
656 argp[idx].type != ARGT_STOP) {
657 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
658 }
659
660 if ((mask & ARGT_MASK) == ARGT_STOP &&
661 argp[idx].type == ARGT_STOP) {
662 return 0;
663 }
664
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100665 /* Convert some argument types. */
666 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100667 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100668 if (argp[idx].type != ARGT_SINT)
669 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
670 argp[idx].type = ARGT_SINT;
671 break;
672
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100673 case ARGT_TIME:
674 if (argp[idx].type != ARGT_SINT)
675 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200676 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100677 break;
678
679 case ARGT_SIZE:
680 if (argp[idx].type != ARGT_SINT)
681 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200682 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100683 break;
684
685 case ARGT_FE:
686 if (argp[idx].type != ARGT_STR)
687 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200688 memcpy(trash.area, argp[idx].data.str.area,
689 argp[idx].data.str.data);
690 trash.area[argp[idx].data.str.data] = 0;
691 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100692 if (!argp[idx].data.prx)
693 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
694 argp[idx].type = ARGT_FE;
695 break;
696
697 case ARGT_BE:
698 if (argp[idx].type != ARGT_STR)
699 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200700 memcpy(trash.area, argp[idx].data.str.area,
701 argp[idx].data.str.data);
702 trash.area[argp[idx].data.str.data] = 0;
703 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100704 if (!argp[idx].data.prx)
705 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
706 argp[idx].type = ARGT_BE;
707 break;
708
709 case ARGT_TAB:
710 if (argp[idx].type != ARGT_STR)
711 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200712 memcpy(trash.area, argp[idx].data.str.area,
713 argp[idx].data.str.data);
714 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhus9fe7c632019-09-29 23:03:09 +0200715 argp[idx].data.t = stktable_find_by_name(trash.area);
716 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100717 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
718 argp[idx].type = ARGT_TAB;
719 break;
720
721 case ARGT_SRV:
722 if (argp[idx].type != ARGT_STR)
723 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200724 memcpy(trash.area, argp[idx].data.str.area,
725 argp[idx].data.str.data);
726 trash.area[argp[idx].data.str.data] = 0;
727 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100728 if (sname) {
729 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200730 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200731 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100732 if (!px)
733 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
734 }
735 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200736 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100737 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100738 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100739 argp[idx].data.srv = findserver(px, sname);
740 if (!argp[idx].data.srv)
741 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
742 argp[idx].type = ARGT_SRV;
743 break;
744
745 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200746 memcpy(trash.area, argp[idx].data.str.area,
747 argp[idx].data.str.data);
748 trash.area[argp[idx].data.str.data] = 0;
749 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100750 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
751 argp[idx].type = ARGT_IPV4;
752 break;
753
754 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200755 memcpy(trash.area, argp[idx].data.str.area,
756 argp[idx].data.str.data);
757 trash.area[argp[idx].data.str.data] = 0;
758 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100759 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
760 argp[idx].type = ARGT_MSK4;
761 break;
762
763 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200764 memcpy(trash.area, argp[idx].data.str.area,
765 argp[idx].data.str.data);
766 trash.area[argp[idx].data.str.data] = 0;
767 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100768 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
769 argp[idx].type = ARGT_IPV6;
770 break;
771
772 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200773 memcpy(trash.area, argp[idx].data.str.area,
774 argp[idx].data.str.data);
775 trash.area[argp[idx].data.str.data] = 0;
776 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100777 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
778 argp[idx].type = ARGT_MSK6;
779 break;
780
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100781 case ARGT_MAP:
782 case ARGT_REG:
783 case ARGT_USR:
784 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100785 break;
786 }
787
788 /* Check for type of argument. */
789 if ((mask & ARGT_MASK) != argp[idx].type) {
790 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
791 arg_type_names[(mask & ARGT_MASK)],
792 arg_type_names[argp[idx].type & ARGT_MASK]);
793 WILL_LJMP(luaL_argerror(L, first + idx, msg));
794 }
795
796 /* Next argument. */
797 mask >>= ARGT_BITS;
798 idx++;
799 }
800}
801
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100802/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500803 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100804 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100805 *
806 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100807 * - hlua_sethlua : create the association between hlua context and lua_state.
808 */
809static inline struct hlua *hlua_gethlua(lua_State *L)
810{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100811 struct hlua **hlua = lua_getextraspace(L);
812 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100813}
814static inline void hlua_sethlua(struct hlua *hlua)
815{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100816 struct hlua **hlua_store = lua_getextraspace(hlua->T);
817 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100818}
819
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100820/* This function is used to send logs. It try to send on screen (stderr)
821 * and on the default syslog server.
822 */
823static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
824{
825 struct tm tm;
826 char *p;
827
828 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200829 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100830 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200831 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200832 /* Break the message if exceed the buffer size. */
833 *(p-4) = ' ';
834 *(p-3) = '.';
835 *(p-2) = '.';
836 *(p-1) = '.';
837 break;
838 }
Willy Tarreau90807112020-02-25 08:16:33 +0100839 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100840 *p = *msg;
841 else
842 *p = '.';
843 }
844 *p = '\0';
845
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200846 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100847 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200848 get_localtime(date.tv_sec, &tm);
849 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100850 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200851 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100852 fflush(stderr);
853 }
854}
855
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100856/* This function just ensure that the yield will be always
857 * returned with a timeout and permit to set some flags
858 */
859__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100860 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100861{
862 struct hlua *hlua = hlua_gethlua(L);
863
864 /* Set the wake timeout. If timeout is required, we set
865 * the expiration time.
866 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200867 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100868
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100869 hlua->flags |= flags;
870
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100871 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200872 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100873}
874
Willy Tarreau87b09662015-04-03 00:22:06 +0200875/* This function initialises the Lua environment stored in the stream.
876 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100877 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200878 *
879 * This function is particular. it initialises a new Lua thread. If the
880 * initialisation fails (example: out of memory error), the lua function
881 * throws an error (longjmp).
882 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100883 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500884 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100885 * threads appear, the safe environment set a lock to ensure only one
886 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500887 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100888 *
889 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500890 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100891 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800892 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200893 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800894 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500895 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100896 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100897int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100898{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100899 if (!already_safe) {
900 if (!SET_SAFE_LJMP(gL.T)) {
901 lua->Tref = LUA_REFNIL;
902 return 0;
903 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200904 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100905 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100906 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +0100907 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +0100908 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100909 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100910 lua->T = lua_newthread(gL.T);
911 if (!lua->T) {
912 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100913 if (!already_safe)
914 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100915 return 0;
916 }
917 hlua_sethlua(lua);
918 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
919 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100920 if (!already_safe)
921 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100922 return 1;
923}
924
Willy Tarreau87b09662015-04-03 00:22:06 +0200925/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100926 * is destroyed. The destroy also the memory context. The struct "lua"
927 * is not freed.
928 */
929void hlua_ctx_destroy(struct hlua *lua)
930{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100931 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100932 return;
933
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100934 if (!lua->T)
935 goto end;
936
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100937 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200938 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100939
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200940 if (!SET_SAFE_LJMP(lua->T))
941 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100942 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200943 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200944
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200945 if (!SET_SAFE_LJMP(gL.T))
946 return;
947 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
948 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200949 /* Forces a garbage collecting process. If the Lua program is finished
950 * without error, we run the GC on the thread pointer. Its freed all
951 * the unused memory.
952 * If the thread is finnish with an error or is currently yielded,
953 * it seems that the GC applied on the thread doesn't clean anything,
954 * so e run the GC on the main thread.
955 * NOTE: maybe this action locks all the Lua threads untiml the en of
956 * the garbage collection.
957 */
Willy Tarreauf31af932020-01-14 09:59:38 +0100958 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200959 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200960 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200961 lua_gc(gL.T, LUA_GCCOLLECT, 0);
962 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200963 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200964
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200965 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100966
967end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100968 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100969}
970
971/* This function is used to restore the Lua context when a coroutine
972 * fails. This function copy the common memory between old coroutine
973 * and the new coroutine. The old coroutine is destroyed, and its
974 * replaced by the new coroutine.
975 * If the flag "keep_msg" is set, the last entry of the old is assumed
976 * as string error message and it is copied in the new stack.
977 */
978static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
979{
980 lua_State *T;
981 int new_ref;
982
983 /* Renew the main LUA stack doesn't have sense. */
984 if (lua == &gL)
985 return 0;
986
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100987 /* New Lua coroutine. */
988 T = lua_newthread(gL.T);
989 if (!T)
990 return 0;
991
992 /* Copy last error message. */
993 if (keep_msg)
994 lua_xmove(lua->T, T, 1);
995
996 /* Copy data between the coroutines. */
997 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
998 lua_xmove(lua->T, T, 1);
999 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
1000
1001 /* Destroy old data. */
1002 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1003
1004 /* The thread is garbage collected by Lua. */
1005 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1006
1007 /* Fill the struct with the new coroutine values. */
1008 lua->Mref = new_ref;
1009 lua->T = T;
1010 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1011
1012 /* Set context. */
1013 hlua_sethlua(lua);
1014
1015 return 1;
1016}
1017
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001018void hlua_hook(lua_State *L, lua_Debug *ar)
1019{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001020 struct hlua *hlua = hlua_gethlua(L);
1021
1022 /* Lua cannot yield when its returning from a function,
1023 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001024 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001025 */
1026 if (lua_gethookmask(L) & LUA_MASKRET) {
1027 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1028 return;
1029 }
1030
1031 /* restore the interrupt condition. */
1032 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1033
1034 /* If we interrupt the Lua processing in yieldable state, we yield.
1035 * If the state is not yieldable, trying yield causes an error.
1036 */
1037 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001038 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001039
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001040 /* If we cannot yield, update the clock and check the timeout. */
1041 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001042 hlua->run_time += now_ms - hlua->start_time;
1043 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001044 lua_pushfstring(L, "execution timeout");
1045 WILL_LJMP(lua_error(L));
1046 }
1047
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001048 /* Update the start time. */
1049 hlua->start_time = now_ms;
1050
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001051 /* Try to interrupt the process at the end of the current
1052 * unyieldable function.
1053 */
1054 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001055}
1056
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001057/* This function start or resumes the Lua stack execution. If the flag
1058 * "yield_allowed" if no set and the LUA stack execution returns a yield
1059 * The function return an error.
1060 *
1061 * The function can returns 4 values:
1062 * - HLUA_E_OK : The execution is terminated without any errors.
1063 * - HLUA_E_AGAIN : The execution must continue at the next associated
1064 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001065 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001066 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001067 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001068 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001069 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001070 * LUA code.
1071 */
1072static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1073{
1074 int ret;
1075 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001076 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001077
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001078 /* Initialise run time counter. */
1079 if (!HLUA_IS_RUNNING(lua))
1080 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001081
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001082 /* Lock the whole Lua execution. This lock must be before the
1083 * label "resume_execution".
1084 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001085 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001086
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001087resume_execution:
1088
1089 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1090 * instructions. it is used for preventing infinite loops.
1091 */
1092 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1093
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001094 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001095 HLUA_SET_RUN(lua);
1096 HLUA_CLR_CTRLYIELD(lua);
1097 HLUA_CLR_WAKERESWR(lua);
1098 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001099
Christopher Fauletbc275a92020-02-26 14:55:16 +01001100 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001101 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001102 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001103
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001104 /* Call the function. */
1105 ret = lua_resume(lua->T, gL.T, lua->nargs);
1106 switch (ret) {
1107
1108 case LUA_OK:
1109 ret = HLUA_E_OK;
1110 break;
1111
1112 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001113 /* Check if the execution timeout is expired. It it is the case, we
1114 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001115 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001116 tv_update_date(0, 1);
1117 lua->run_time += now_ms - lua->start_time;
1118 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001119 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001120 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001121 break;
1122 }
1123 /* Process the forced yield. if the general yield is not allowed or
1124 * if no task were associated this the current Lua execution
1125 * coroutine, we resume the execution. Else we want to return in the
1126 * scheduler and we want to be waked up again, to continue the
1127 * current Lua execution. So we schedule our own task.
1128 */
1129 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001130 if (!yield_allowed || !lua->task)
1131 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001132 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001133 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001134 if (!yield_allowed) {
1135 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001136 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001137 break;
1138 }
1139 ret = HLUA_E_AGAIN;
1140 break;
1141
1142 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001143
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001144 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001145 * because the errors ares the only one mean to return immediately
1146 * from and lua execution.
1147 */
1148 if (lua->flags & HLUA_EXIT) {
1149 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001150 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001151 break;
1152 }
1153
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001154 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001155 if (!lua_checkstack(lua->T, 1)) {
1156 ret = HLUA_E_ERR;
1157 break;
1158 }
1159 msg = lua_tostring(lua->T, -1);
1160 lua_settop(lua->T, 0); /* Empty the stack. */
1161 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001162 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001163 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001164 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001165 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001166 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001167 ret = HLUA_E_ERRMSG;
1168 break;
1169
1170 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001171 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001172 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001173 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001174 break;
1175
1176 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001177 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001178 if (!lua_checkstack(lua->T, 1)) {
1179 ret = HLUA_E_ERR;
1180 break;
1181 }
1182 msg = lua_tostring(lua->T, -1);
1183 lua_settop(lua->T, 0); /* Empty the stack. */
1184 lua_pop(lua->T, 1);
1185 if (msg)
1186 lua_pushfstring(lua->T, "message handler error: %s", msg);
1187 else
1188 lua_pushfstring(lua->T, "message handler error");
1189 ret = HLUA_E_ERRMSG;
1190 break;
1191
1192 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001193 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001194 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001195 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001196 break;
1197 }
1198
1199 switch (ret) {
1200 case HLUA_E_AGAIN:
1201 break;
1202
1203 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001204 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001205 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001206 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001207 break;
1208
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001209 case HLUA_E_ETMOUT:
1210 case HLUA_E_NOMEM:
1211 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001212 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001213 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001214 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001215 hlua_ctx_renew(lua, 0);
1216 break;
1217
1218 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001219 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001220 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001221 break;
1222 }
1223
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001224 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001225 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001226
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001227 return ret;
1228}
1229
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001230/* This function exit the current code. */
1231__LJMP static int hlua_done(lua_State *L)
1232{
1233 struct hlua *hlua = hlua_gethlua(L);
1234
1235 hlua->flags |= HLUA_EXIT;
1236 WILL_LJMP(lua_error(L));
1237
1238 return 0;
1239}
1240
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001241/* This function is an LUA binding. It provides a function
1242 * for deleting ACL from a referenced ACL file.
1243 */
1244__LJMP static int hlua_del_acl(lua_State *L)
1245{
1246 const char *name;
1247 const char *key;
1248 struct pat_ref *ref;
1249
1250 MAY_LJMP(check_args(L, 2, "del_acl"));
1251
1252 name = MAY_LJMP(luaL_checkstring(L, 1));
1253 key = MAY_LJMP(luaL_checkstring(L, 2));
1254
1255 ref = pat_ref_lookup(name);
1256 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001257 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001258
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001259 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001260 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001261 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001262 return 0;
1263}
1264
1265/* This function is an LUA binding. It provides a function
1266 * for deleting map entry from a referenced map file.
1267 */
1268static int hlua_del_map(lua_State *L)
1269{
1270 const char *name;
1271 const char *key;
1272 struct pat_ref *ref;
1273
1274 MAY_LJMP(check_args(L, 2, "del_map"));
1275
1276 name = MAY_LJMP(luaL_checkstring(L, 1));
1277 key = MAY_LJMP(luaL_checkstring(L, 2));
1278
1279 ref = pat_ref_lookup(name);
1280 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001281 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001282
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001283 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001284 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001285 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001286 return 0;
1287}
1288
1289/* This function is an LUA binding. It provides a function
1290 * for adding ACL pattern from a referenced ACL file.
1291 */
1292static int hlua_add_acl(lua_State *L)
1293{
1294 const char *name;
1295 const char *key;
1296 struct pat_ref *ref;
1297
1298 MAY_LJMP(check_args(L, 2, "add_acl"));
1299
1300 name = MAY_LJMP(luaL_checkstring(L, 1));
1301 key = MAY_LJMP(luaL_checkstring(L, 2));
1302
1303 ref = pat_ref_lookup(name);
1304 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001305 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001306
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001307 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001308 if (pat_ref_find_elt(ref, key) == NULL)
1309 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001310 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001311 return 0;
1312}
1313
1314/* This function is an LUA binding. It provides a function
1315 * for setting map pattern and sample from a referenced map
1316 * file.
1317 */
1318static int hlua_set_map(lua_State *L)
1319{
1320 const char *name;
1321 const char *key;
1322 const char *value;
1323 struct pat_ref *ref;
1324
1325 MAY_LJMP(check_args(L, 3, "set_map"));
1326
1327 name = MAY_LJMP(luaL_checkstring(L, 1));
1328 key = MAY_LJMP(luaL_checkstring(L, 2));
1329 value = MAY_LJMP(luaL_checkstring(L, 3));
1330
1331 ref = pat_ref_lookup(name);
1332 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001333 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001334
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001335 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001336 if (pat_ref_find_elt(ref, key) != NULL)
1337 pat_ref_set(ref, key, value, NULL);
1338 else
1339 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001340 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001341 return 0;
1342}
1343
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001344/* A class is a lot of memory that contain data. This data can be a table,
1345 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001346 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001347 * the name of the object (_G[<name>] = <metable> ).
1348 *
1349 * A metable is a table that modify the standard behavior of a standard
1350 * access to the associated data. The entries of this new metatable are
1351 * defined as is:
1352 *
1353 * http://lua-users.org/wiki/MetatableEvents
1354 *
1355 * __index
1356 *
1357 * we access an absent field in a table, the result is nil. This is
1358 * true, but it is not the whole truth. Actually, such access triggers
1359 * the interpreter to look for an __index metamethod: If there is no
1360 * such method, as usually happens, then the access results in nil;
1361 * otherwise, the metamethod will provide the result.
1362 *
1363 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1364 * the key does not appear in the table, but the metatable has an __index
1365 * property:
1366 *
1367 * - if the value is a function, the function is called, passing in the
1368 * table and the key; the return value of that function is returned as
1369 * the result.
1370 *
1371 * - if the value is another table, the value of the key in that table is
1372 * asked for and returned (and if it doesn't exist in that table, but that
1373 * table's metatable has an __index property, then it continues on up)
1374 *
1375 * - Use "rawget(myTable,key)" to skip this metamethod.
1376 *
1377 * http://www.lua.org/pil/13.4.1.html
1378 *
1379 * __newindex
1380 *
1381 * Like __index, but control property assignment.
1382 *
1383 * __mode - Control weak references. A string value with one or both
1384 * of the characters 'k' and 'v' which specifies that the the
1385 * keys and/or values in the table are weak references.
1386 *
1387 * __call - Treat a table like a function. When a table is followed by
1388 * parenthesis such as "myTable( 'foo' )" and the metatable has
1389 * a __call key pointing to a function, that function is invoked
1390 * (passing any specified arguments) and the return value is
1391 * returned.
1392 *
1393 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1394 * called, if the metatable for myTable has a __metatable
1395 * key, the value of that key is returned instead of the
1396 * actual metatable.
1397 *
1398 * __tostring - Control string representation. When the builtin
1399 * "tostring( myTable )" function is called, if the metatable
1400 * for myTable has a __tostring property set to a function,
1401 * that function is invoked (passing myTable to it) and the
1402 * return value is used as the string representation.
1403 *
1404 * __len - Control table length. When the table length is requested using
1405 * the length operator ( '#' ), if the metatable for myTable has
1406 * a __len key pointing to a function, that function is invoked
1407 * (passing myTable to it) and the return value used as the value
1408 * of "#myTable".
1409 *
1410 * __gc - Userdata finalizer code. When userdata is set to be garbage
1411 * collected, if the metatable has a __gc field pointing to a
1412 * function, that function is first invoked, passing the userdata
1413 * to it. The __gc metamethod is not called for tables.
1414 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1415 *
1416 * Special metamethods for redefining standard operators:
1417 * http://www.lua.org/pil/13.1.html
1418 *
1419 * __add "+"
1420 * __sub "-"
1421 * __mul "*"
1422 * __div "/"
1423 * __unm "!"
1424 * __pow "^"
1425 * __concat ".."
1426 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001427 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001428 * http://www.lua.org/pil/13.2.html
1429 *
1430 * __eq "=="
1431 * __lt "<"
1432 * __le "<="
1433 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001434
1435/*
1436 *
1437 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001438 * Class Map
1439 *
1440 *
1441 */
1442
1443/* Returns a struct hlua_map if the stack entry "ud" is
1444 * a class session, otherwise it throws an error.
1445 */
1446__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1447{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001448 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001449}
1450
1451/* This function is the map constructor. It don't need
1452 * the class Map object. It creates and return a new Map
1453 * object. It must be called only during "body" or "init"
1454 * context because it process some filesystem accesses.
1455 */
1456__LJMP static int hlua_map_new(struct lua_State *L)
1457{
1458 const char *fn;
1459 int match = PAT_MATCH_STR;
1460 struct sample_conv conv;
1461 const char *file = "";
1462 int line = 0;
1463 lua_Debug ar;
1464 char *err = NULL;
1465 struct arg args[2];
1466
1467 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1468 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1469
1470 fn = MAY_LJMP(luaL_checkstring(L, 1));
1471
1472 if (lua_gettop(L) >= 2) {
1473 match = MAY_LJMP(luaL_checkinteger(L, 2));
1474 if (match < 0 || match >= PAT_MATCH_NUM)
1475 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1476 }
1477
1478 /* Get Lua filename and line number. */
1479 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1480 lua_getinfo(L, "Sl", &ar); /* get info about it */
1481 if (ar.currentline > 0) { /* is there info? */
1482 file = ar.short_src;
1483 line = ar.currentline;
1484 }
1485 }
1486
1487 /* fill fake sample_conv struct. */
1488 conv.kw = ""; /* unused. */
1489 conv.process = NULL; /* unused. */
1490 conv.arg_mask = 0; /* unused. */
1491 conv.val_args = NULL; /* unused. */
1492 conv.out_type = SMP_T_STR;
1493 conv.private = (void *)(long)match;
1494 switch (match) {
1495 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1496 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1497 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1498 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1499 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1500 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1501 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001502 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001503 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1504 default:
1505 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1506 }
1507
1508 /* fill fake args. */
1509 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001510 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001511 args[1].type = ARGT_STOP;
1512
1513 /* load the map. */
1514 if (!sample_load_map(args, &conv, file, line, &err)) {
1515 /* error case: we cant use luaL_error because we must
1516 * free the err variable.
1517 */
1518 luaL_where(L, 1);
1519 lua_pushfstring(L, "'new': %s.", err);
1520 lua_concat(L, 2);
1521 free(err);
1522 WILL_LJMP(lua_error(L));
1523 }
1524
1525 /* create the lua object. */
1526 lua_newtable(L);
1527 lua_pushlightuserdata(L, args[0].data.map);
1528 lua_rawseti(L, -2, 0);
1529
1530 /* Pop a class Map metatable and affect it to the userdata. */
1531 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1532 lua_setmetatable(L, -2);
1533
1534
1535 return 1;
1536}
1537
1538__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1539{
1540 struct map_descriptor *desc;
1541 struct pattern *pat;
1542 struct sample smp;
1543
1544 MAY_LJMP(check_args(L, 2, "lookup"));
1545 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001546 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001547 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001548 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001549 }
1550 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001551 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001552 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001553 smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001554 }
1555
1556 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001557 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001558 if (str)
1559 lua_pushstring(L, "");
1560 else
1561 lua_pushnil(L);
1562 return 1;
1563 }
1564
1565 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001566 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001567 return 1;
1568}
1569
1570__LJMP static int hlua_map_lookup(struct lua_State *L)
1571{
1572 return _hlua_map_lookup(L, 0);
1573}
1574
1575__LJMP static int hlua_map_slookup(struct lua_State *L)
1576{
1577 return _hlua_map_lookup(L, 1);
1578}
1579
1580/*
1581 *
1582 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001583 * Class Socket
1584 *
1585 *
1586 */
1587
1588__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1589{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001590 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001591}
1592
1593/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001594 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001595 * received.
1596 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001597static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001598{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001599 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001600
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001601 if (appctx->ctx.hlua_cosocket.die) {
1602 si_shutw(si);
1603 si_shutr(si);
1604 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001605 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1606 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001607 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1608 }
1609
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001610 /* If we cant write, wakeup the pending write signals. */
1611 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001612 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001613
1614 /* If we cant read, wakeup the pending read signals. */
1615 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001616 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001617
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001618 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001619 * to be notified whenever the connection completes.
1620 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001621 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001622 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001623 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001624 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001625 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001626 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001627
1628 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001629 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001630
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001631 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001632 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001633 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001634
1635 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001636 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001637 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001638
1639 /* Some data were injected in the buffer, notify the stream
1640 * interface.
1641 */
1642 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001643 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001644
1645 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001646 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001647 */
1648 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001649 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001650}
1651
Willy Tarreau87b09662015-04-03 00:22:06 +02001652/* This function is called when the "struct stream" is destroyed.
1653 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001654 * Wake all the pending signals.
1655 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001656static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001657{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001658 struct xref *peer;
1659
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001660 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001661 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1662 if (peer)
1663 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001664
1665 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001666 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1667 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001668}
1669
1670/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001671 * uses this object. If the stream does not exists, just quit.
1672 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001673 * pending signal can rest in the read and write lists. destroy
1674 * it.
1675 */
1676__LJMP static int hlua_socket_gc(lua_State *L)
1677{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001678 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001679 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001680 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001681
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001682 MAY_LJMP(check_args(L, 1, "__gc"));
1683
1684 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001685 peer = xref_get_peer_and_lock(&socket->xref);
1686 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001687 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001688 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001689
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001690 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001691 appctx->ctx.hlua_cosocket.die = 1;
1692 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001693
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001694 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001695 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001696 return 0;
1697}
1698
1699/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001700 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701 */
sada05ed3302018-05-11 11:48:18 -07001702__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001704 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001706 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001707 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001708
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001709 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001710
1711 /* Check if we run on the same thread than the xreator thread.
1712 * We cannot access to the socket if the thread is different.
1713 */
1714 if (socket->tid != tid)
1715 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1716
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001717 peer = xref_get_peer_and_lock(&socket->xref);
1718 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001719 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001720
1721 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001722 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001723
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001724 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001725 appctx->ctx.hlua_cosocket.die = 1;
1726 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001727
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001728 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001729 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001730 return 0;
1731}
1732
sada05ed3302018-05-11 11:48:18 -07001733/* The close function calls close_helper.
1734 */
1735__LJMP static int hlua_socket_close(lua_State *L)
1736{
1737 MAY_LJMP(check_args(L, 1, "close"));
1738 return hlua_socket_close_helper(L);
1739}
1740
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001741/* This Lua function assumes that the stack contain three parameters.
1742 * 1 - USERDATA containing a struct socket
1743 * 2 - INTEGER with values of the macro defined below
1744 * If the integer is -1, we must read at most one line.
1745 * If the integer is -2, we ust read all the data until the
1746 * end of the stream.
1747 * If the integer is positive value, we must read a number of
1748 * bytes corresponding to this value.
1749 */
1750#define HLSR_READ_LINE (-1)
1751#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001752__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001753{
1754 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1755 int wanted = lua_tointeger(L, 2);
1756 struct hlua *hlua = hlua_gethlua(L);
1757 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001758 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001759 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001760 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001761 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001762 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001763 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001764 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001765 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001766 struct stream_interface *si;
1767 struct stream *s;
1768 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001769 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001770
1771 /* Check if this lua stack is schedulable. */
1772 if (!hlua || !hlua->task)
1773 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1774 "'frontend', 'backend' or 'task'"));
1775
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001776 /* Check if we run on the same thread than the xreator thread.
1777 * We cannot access to the socket if the thread is different.
1778 */
1779 if (socket->tid != tid)
1780 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1781
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001782 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001783 peer = xref_get_peer_and_lock(&socket->xref);
1784 if (!peer)
1785 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001786 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1787 si = appctx->owner;
1788 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001790 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001791 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001792 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001793 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001794 if (nblk < 0) /* Connection close. */
1795 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001796 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001797 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001798
1799 /* remove final \r\n. */
1800 if (nblk == 1) {
1801 if (blk1[len1-1] == '\n') {
1802 len1--;
1803 skip_at_end++;
1804 if (blk1[len1-1] == '\r') {
1805 len1--;
1806 skip_at_end++;
1807 }
1808 }
1809 }
1810 else {
1811 if (blk2[len2-1] == '\n') {
1812 len2--;
1813 skip_at_end++;
1814 if (blk2[len2-1] == '\r') {
1815 len2--;
1816 skip_at_end++;
1817 }
1818 }
1819 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001820 }
1821
1822 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001823 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001824 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825 if (nblk < 0) /* Connection close. */
1826 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001827 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001828 goto connection_empty;
1829 }
1830
1831 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001833 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001834 if (nblk < 0) /* Connection close. */
1835 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001836 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001837 goto connection_empty;
1838
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001839 missing_bytes = wanted - socket->b.n;
1840 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001841 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001842 len1 = missing_bytes;
1843 } if (nblk == 2 && len1 + len2 > missing_bytes)
1844 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001845 }
1846
1847 len = len1;
1848
1849 luaL_addlstring(&socket->b, blk1, len1);
1850 if (nblk == 2) {
1851 len += len2;
1852 luaL_addlstring(&socket->b, blk2, len2);
1853 }
1854
1855 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001856 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001857
1858 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001859 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001860
1861 /* If the pattern reclaim to read all the data
1862 * in the connection, got out.
1863 */
1864 if (wanted == HLSR_READ_ALL)
1865 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001866 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001867 goto connection_empty;
1868
1869 /* Return result. */
1870 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001871 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001872 return 1;
1873
1874connection_closed:
1875
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001876 xref_unlock(&socket->xref, peer);
1877
1878no_peer:
1879
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001880 /* If the buffer containds data. */
1881 if (socket->b.n > 0) {
1882 luaL_pushresult(&socket->b);
1883 return 1;
1884 }
1885 lua_pushnil(L);
1886 lua_pushstring(L, "connection closed.");
1887 return 2;
1888
1889connection_empty:
1890
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001891 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1892 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001893 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001894 }
1895 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001896 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001897 return 0;
1898}
1899
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001900/* This Lua function gets two parameters. The first one can be string
1901 * or a number. If the string is "*l", the user requires one line. If
1902 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001903 * If the value is a number, the user require a number of bytes equal
1904 * to the value. The default value is "*l" (a line).
1905 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001906 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001907 * integer takes this values:
1908 * -1 : read a line
1909 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001910 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001911 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001912 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001913 * concatenated with the read data.
1914 */
1915__LJMP static int hlua_socket_receive(struct lua_State *L)
1916{
1917 int wanted = HLSR_READ_LINE;
1918 const char *pattern;
1919 int type;
1920 char *error;
1921 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001922 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001923
1924 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1925 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1926
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001927 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001928
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001929 /* Check if we run on the same thread than the xreator thread.
1930 * We cannot access to the socket if the thread is different.
1931 */
1932 if (socket->tid != tid)
1933 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1934
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001935 /* check for pattern. */
1936 if (lua_gettop(L) >= 2) {
1937 type = lua_type(L, 2);
1938 if (type == LUA_TSTRING) {
1939 pattern = lua_tostring(L, 2);
1940 if (strcmp(pattern, "*a") == 0)
1941 wanted = HLSR_READ_ALL;
1942 else if (strcmp(pattern, "*l") == 0)
1943 wanted = HLSR_READ_LINE;
1944 else {
1945 wanted = strtoll(pattern, &error, 10);
1946 if (*error != '\0')
1947 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1948 }
1949 }
1950 else if (type == LUA_TNUMBER) {
1951 wanted = lua_tointeger(L, 2);
1952 if (wanted < 0)
1953 WILL_LJMP(luaL_error(L, "Unsupported size."));
1954 }
1955 }
1956
1957 /* Set pattern. */
1958 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001959
1960 /* Check if we would replace the top by itself. */
1961 if (lua_gettop(L) != 2)
1962 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001963
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001964 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001965 luaL_buffinit(L, &socket->b);
1966
1967 /* Check prefix. */
1968 if (lua_gettop(L) >= 3) {
1969 if (lua_type(L, 3) != LUA_TSTRING)
1970 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1971 pattern = lua_tolstring(L, 3, &len);
1972 luaL_addlstring(&socket->b, pattern, len);
1973 }
1974
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001975 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001976}
1977
1978/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001979 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001980 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001981static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001982{
1983 struct hlua_socket *socket;
1984 struct hlua *hlua = hlua_gethlua(L);
1985 struct appctx *appctx;
1986 size_t buf_len;
1987 const char *buf;
1988 int len;
1989 int send_len;
1990 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001991 struct xref *peer;
1992 struct stream_interface *si;
1993 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001994
1995 /* Check if this lua stack is schedulable. */
1996 if (!hlua || !hlua->task)
1997 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1998 "'frontend', 'backend' or 'task'"));
1999
2000 /* Get object */
2001 socket = MAY_LJMP(hlua_checksocket(L, 1));
2002 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002003 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002004
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002005 /* Check if we run on the same thread than the xreator thread.
2006 * We cannot access to the socket if the thread is different.
2007 */
2008 if (socket->tid != tid)
2009 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2010
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002011 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002012 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002013 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002014 lua_pushinteger(L, -1);
2015 return 1;
2016 }
2017 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2018 si = appctx->owner;
2019 s = si_strm(si);
2020
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002021 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002022 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002023 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002024 lua_pushinteger(L, -1);
2025 return 1;
2026 }
2027
2028 /* Update the input buffer data. */
2029 buf += sent;
2030 send_len = buf_len - sent;
2031
2032 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002033 if (sent >= buf_len) {
2034 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002035 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002036 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002038 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002039 * the request buffer if its not required.
2040 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002041 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002042 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002043 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002044 }
2045
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002046 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002047 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002048 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002049 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002050 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002051
2052 /* send data */
2053 if (len < send_len)
2054 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002055 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002056
2057 /* "Not enough space" (-1), "Buffer too little to contain
2058 * the data" (-2) are not expected because the available length
2059 * is tested.
2060 * Other unknown error are also not expected.
2061 */
2062 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002063 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002064 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002065
sada05ed3302018-05-11 11:48:18 -07002066 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002067 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002068 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002069 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002070 return 1;
2071 }
2072
2073 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002074 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002075
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002076 s->req.rex = TICK_ETERNITY;
2077 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002078
2079 /* Update length sent. */
2080 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002081 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002082
2083 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002084 if (sent + len >= buf_len) {
2085 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002086 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002087 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002088
2089hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002090 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2091 xref_unlock(&socket->xref, peer);
2092 WILL_LJMP(luaL_error(L, "out of memory"));
2093 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002094 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002095 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002096 return 0;
2097}
2098
2099/* This function initiate the send of data. It just check the input
2100 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002101 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002102 * "hlua_socket_write_yield" that can yield.
2103 *
2104 * The Lua function gets between 3 and 4 parameters. The first one is
2105 * the associated object. The second is a string buffer. The third is
2106 * a facultative integer that represents where is the buffer position
2107 * of the start of the data that can send. The first byte is the
2108 * position "1". The default value is "1". The fourth argument is a
2109 * facultative integer that represents where is the buffer position
2110 * of the end of the data that can send. The default is the last byte.
2111 */
2112static int hlua_socket_send(struct lua_State *L)
2113{
2114 int i;
2115 int j;
2116 const char *buf;
2117 size_t buf_len;
2118
2119 /* Check number of arguments. */
2120 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2121 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2122
2123 /* Get the string. */
2124 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2125
2126 /* Get and check j. */
2127 if (lua_gettop(L) == 4) {
2128 j = MAY_LJMP(luaL_checkinteger(L, 4));
2129 if (j < 0)
2130 j = buf_len + j + 1;
2131 if (j > buf_len)
2132 j = buf_len + 1;
2133 lua_pop(L, 1);
2134 }
2135 else
2136 j = buf_len;
2137
2138 /* Get and check i. */
2139 if (lua_gettop(L) == 3) {
2140 i = MAY_LJMP(luaL_checkinteger(L, 3));
2141 if (i < 0)
2142 i = buf_len + i + 1;
2143 if (i > buf_len)
2144 i = buf_len + 1;
2145 lua_pop(L, 1);
2146 } else
2147 i = 1;
2148
2149 /* Check bth i and j. */
2150 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002151 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002152 return 1;
2153 }
2154 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002155 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002156 return 1;
2157 }
2158 if (i == 0)
2159 i = 1;
2160 if (j == 0)
2161 j = 1;
2162
2163 /* Pop the string. */
2164 lua_pop(L, 1);
2165
2166 /* Update the buffer length. */
2167 buf += i - 1;
2168 buf_len = j - i + 1;
2169 lua_pushlstring(L, buf, buf_len);
2170
2171 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002172 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002173
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002174 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002175}
2176
Willy Tarreau22b0a682015-06-17 19:43:49 +02002177#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002178__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2179{
2180 static char buffer[SOCKET_INFO_MAX_LEN];
2181 int ret;
2182 int len;
2183 char *p;
2184
2185 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2186 if (ret <= 0) {
2187 lua_pushnil(L);
2188 return 1;
2189 }
2190
2191 if (ret == AF_UNIX) {
2192 lua_pushstring(L, buffer+1);
2193 return 1;
2194 }
2195 else if (ret == AF_INET6) {
2196 buffer[0] = '[';
2197 len = strlen(buffer);
2198 buffer[len] = ']';
2199 len++;
2200 buffer[len] = ':';
2201 len++;
2202 p = buffer;
2203 }
2204 else if (ret == AF_INET) {
2205 p = buffer + 1;
2206 len = strlen(p);
2207 p[len] = ':';
2208 len++;
2209 }
2210 else {
2211 lua_pushnil(L);
2212 return 1;
2213 }
2214
2215 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2216 lua_pushnil(L);
2217 return 1;
2218 }
2219
2220 lua_pushstring(L, p);
2221 return 1;
2222}
2223
2224/* Returns information about the peer of the connection. */
2225__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2226{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002227 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002228 struct xref *peer;
2229 struct appctx *appctx;
2230 struct stream_interface *si;
2231 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002232 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002233
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002234 MAY_LJMP(check_args(L, 1, "getpeername"));
2235
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002236 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002237
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002238 /* Check if we run on the same thread than the xreator thread.
2239 * We cannot access to the socket if the thread is different.
2240 */
2241 if (socket->tid != tid)
2242 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2243
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002244 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002245 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002246 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002247 lua_pushnil(L);
2248 return 1;
2249 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002250 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2251 si = appctx->owner;
2252 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002253
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002254 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002255 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002256 lua_pushnil(L);
2257 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002258 }
2259
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002260 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002261 xref_unlock(&socket->xref, peer);
2262 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002263}
2264
2265/* Returns information about my connection side. */
2266static int hlua_socket_getsockname(struct lua_State *L)
2267{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002268 struct hlua_socket *socket;
2269 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002270 struct appctx *appctx;
2271 struct xref *peer;
2272 struct stream_interface *si;
2273 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002274 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002275
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002276 MAY_LJMP(check_args(L, 1, "getsockname"));
2277
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002278 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002279
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002280 /* Check if we run on the same thread than the xreator thread.
2281 * We cannot access to the socket if the thread is different.
2282 */
2283 if (socket->tid != tid)
2284 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2285
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002286 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002287 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002288 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002289 lua_pushnil(L);
2290 return 1;
2291 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002292 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2293 si = appctx->owner;
2294 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002295
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002296 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002297 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002298 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002299 lua_pushnil(L);
2300 return 1;
2301 }
2302
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002303 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002304 xref_unlock(&socket->xref, peer);
2305 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002306}
2307
2308/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002309static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002310 .obj_type = OBJ_TYPE_APPLET,
2311 .name = "<LUA_TCP>",
2312 .fct = hlua_socket_handler,
2313 .release = hlua_socket_release,
2314};
2315
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002316__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002317{
2318 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2319 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002320 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002321 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002322 struct stream_interface *si;
2323 struct stream *s;
2324
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002325 /* Check if we run on the same thread than the xreator thread.
2326 * We cannot access to the socket if the thread is different.
2327 */
2328 if (socket->tid != tid)
2329 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2330
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002331 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002332 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002333 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002334 lua_pushnil(L);
2335 lua_pushstring(L, "Can't connect");
2336 return 2;
2337 }
2338 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2339 si = appctx->owner;
2340 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002341
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002342 /* Check if we run on the same thread than the xreator thread.
2343 * We cannot access to the socket if the thread is different.
2344 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002345 if (socket->tid != tid) {
2346 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002347 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002348 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002349
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002351 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002352 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002353 lua_pushnil(L);
2354 lua_pushstring(L, "Can't connect");
2355 return 2;
2356 }
2357
Willy Tarreaue09101e2018-10-16 17:37:12 +02002358 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002359
2360 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002361 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002362 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002363 lua_pushinteger(L, 1);
2364 return 1;
2365 }
2366
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002367 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2368 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002369 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002370 }
2371 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002372 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002373 return 0;
2374}
2375
2376/* This function fail or initite the connection. */
2377__LJMP static int hlua_socket_connect(struct lua_State *L)
2378{
2379 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002380 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002381 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002382 struct hlua *hlua;
2383 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002384 int low, high;
2385 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002386 struct xref *peer;
2387 struct stream_interface *si;
2388 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002390 if (lua_gettop(L) < 2)
2391 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002392
2393 /* Get args. */
2394 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002395
2396 /* Check if we run on the same thread than the xreator thread.
2397 * We cannot access to the socket if the thread is different.
2398 */
2399 if (socket->tid != tid)
2400 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2401
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002402 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002403 if (lua_gettop(L) >= 3) {
2404 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002405 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002406
Tim Duesterhus6edab862018-01-06 19:04:45 +01002407 /* Force the ip to end with a colon, to support IPv6 addresses
2408 * that are not enclosed within square brackets.
2409 */
2410 if (port > 0) {
2411 luaL_buffinit(L, &b);
2412 luaL_addstring(&b, ip);
2413 luaL_addchar(&b, ':');
2414 luaL_pushresult(&b);
2415 ip = lua_tolstring(L, lua_gettop(L), NULL);
2416 }
2417 }
2418
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002419 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002420 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002421 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002422 lua_pushnil(L);
2423 return 1;
2424 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002425
2426 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002427 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002428 if (!addr) {
2429 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002430 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002431 }
2432 if (low != high) {
2433 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002434 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002435 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002436
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002437 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002438 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002439 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002440 if (port == -1) {
2441 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002442 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002443 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002444 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2445 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002446 if (port == -1) {
2447 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002448 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002449 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002450 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002451 }
2452 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002453
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002454 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2455 si = appctx->owner;
2456 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002457
2458 if (!sockaddr_alloc(&s->target_addr)) {
2459 xref_unlock(&socket->xref, peer);
2460 WILL_LJMP(luaL_error(L, "connect: internal error"));
2461 }
2462 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002463 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002464
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002465 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002466
2467 /* inform the stream that we want to be notified whenever the
2468 * connection completes.
2469 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002470 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002471 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002472 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002473
Willy Tarreauf31af932020-01-14 09:59:38 +01002474 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002475
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002476 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2477 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002478 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002479 }
2480 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002481
2482 task_wakeup(s->task, TASK_WOKEN_INIT);
2483 /* Return yield waiting for connection. */
2484
Willy Tarreau9635e032018-10-16 17:52:55 +02002485 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002486
2487 return 0;
2488}
2489
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002490#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002491__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2492{
2493 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002494 struct xref *peer;
2495 struct appctx *appctx;
2496 struct stream_interface *si;
2497 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002498
2499 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2500 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002501
2502 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002503 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002504 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002505 lua_pushnil(L);
2506 return 1;
2507 }
2508 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2509 si = appctx->owner;
2510 s = si_strm(si);
2511
2512 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002513 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002514 return MAY_LJMP(hlua_socket_connect(L));
2515}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002516#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002517
2518__LJMP static int hlua_socket_setoption(struct lua_State *L)
2519{
2520 return 0;
2521}
2522
2523__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2524{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002525 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002526 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002527 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002528 struct xref *peer;
2529 struct appctx *appctx;
2530 struct stream_interface *si;
2531 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002532
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002533 MAY_LJMP(check_args(L, 2, "settimeout"));
2534
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002535 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002536
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002537 /* convert the timeout to millis */
2538 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002539
Thierry Fournier17a921b2018-03-08 09:59:02 +01002540 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002541 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002542 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2543
Mark Lakes56cc1252018-03-27 09:48:06 +02002544 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002545 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002546
2547 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002548 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002549 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002550
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002551 /* Check if we run on the same thread than the xreator thread.
2552 * We cannot access to the socket if the thread is different.
2553 */
2554 if (socket->tid != tid)
2555 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2556
Mark Lakes56cc1252018-03-27 09:48:06 +02002557 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002558 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002559 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002560 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2561 WILL_LJMP(lua_error(L));
2562 return 0;
2563 }
2564 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2565 si = appctx->owner;
2566 s = si_strm(si);
2567
Cyril Bonté7bb63452018-08-17 23:51:02 +02002568 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002569 s->req.rto = tmout;
2570 s->req.wto = tmout;
2571 s->res.rto = tmout;
2572 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002573 s->req.rex = tick_add_ifset(now_ms, tmout);
2574 s->req.wex = tick_add_ifset(now_ms, tmout);
2575 s->res.rex = tick_add_ifset(now_ms, tmout);
2576 s->res.wex = tick_add_ifset(now_ms, tmout);
2577
2578 s->task->expire = tick_add_ifset(now_ms, tmout);
2579 task_queue(s->task);
2580
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002581 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002582
Thierry Fourniere9636f12018-03-08 09:54:32 +01002583 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002584 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002585}
2586
2587__LJMP static int hlua_socket_new(lua_State *L)
2588{
2589 struct hlua_socket *socket;
2590 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002591 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002592 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002593
2594 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002595 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002596 hlua_pusherror(L, "socket: full stack");
2597 goto out_fail_conf;
2598 }
2599
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002600 /* Create the object: obj[0] = userdata. */
2601 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002602 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002603 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002604 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002605 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002607 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002608 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002609 hlua_pusherror(L, "socket: uninitialized pools.");
2610 goto out_fail_conf;
2611 }
2612
Willy Tarreau87b09662015-04-03 00:22:06 +02002613 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002614 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2615 lua_setmetatable(L, -2);
2616
Willy Tarreaud420a972015-04-06 00:39:18 +02002617 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002618 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002619 if (!appctx) {
2620 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002621 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002622 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002623
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002624 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002625 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002626 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2627 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002628
Willy Tarreaud420a972015-04-06 00:39:18 +02002629 /* Now create a session, task and stream for this applet */
2630 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2631 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002632 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002633 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002634 }
2635
Willy Tarreau87787ac2017-08-28 16:22:54 +02002636 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002637 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002638 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002639 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002640 }
2641
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002642 /* Initialise cross reference between stream and Lua socket object. */
2643 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002644
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002645 /* Configure "right" stream interface. this "si" is used to connect
2646 * and retrieve data from the server. The connection is initialized
2647 * with the "struct server".
2648 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002649 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002650
2651 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002652 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002653 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002654
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002655 return 1;
2656
Willy Tarreaud420a972015-04-06 00:39:18 +02002657 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002658 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002659 out_fail_sess:
2660 appctx_free(appctx);
2661 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002662 WILL_LJMP(lua_error(L));
2663 return 0;
2664}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002665
2666/*
2667 *
2668 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002669 * Class Channel
2670 *
2671 *
2672 */
2673
2674/* Returns the struct hlua_channel join to the class channel in the
2675 * stack entry "ud" or throws an argument error.
2676 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002677__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002678{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002679 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002680}
2681
Willy Tarreau47860ed2015-03-10 14:07:50 +01002682/* Pushes the channel onto the top of the stack. If the stask does not have a
2683 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002684 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002685static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002686{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002687 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002688 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002689 return 0;
2690
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002691 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002692 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002693 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002694
2695 /* Pop a class sesison metatable and affect it to the userdata. */
2696 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2697 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002698 return 1;
2699}
2700
2701/* Duplicate all the data present in the input channel and put it
2702 * in a string LUA variables. Returns -1 and push a nil value in
2703 * the stack if the channel is closed and all the data are consumed,
2704 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002705 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002706 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002707static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002708{
2709 char *blk1;
2710 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002711 size_t len1;
2712 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002713 int ret;
2714 luaL_Buffer b;
2715
Willy Tarreau06d80a92017-10-19 14:32:15 +02002716 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002717 if (unlikely(ret == 0))
2718 return 0;
2719
2720 if (unlikely(ret < 0)) {
2721 lua_pushnil(L);
2722 return -1;
2723 }
2724
2725 luaL_buffinit(L, &b);
2726 luaL_addlstring(&b, blk1, len1);
2727 if (unlikely(ret == 2))
2728 luaL_addlstring(&b, blk2, len2);
2729 luaL_pushresult(&b);
2730
2731 if (unlikely(ret == 2))
2732 return len1 + len2;
2733 return len1;
2734}
2735
2736/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2737 * a yield. This function keep the data in the buffer.
2738 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002739__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002740{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002741 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002742
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002743 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2744
Christopher Faulet3f829a42018-12-13 21:56:45 +01002745 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2746 WILL_LJMP(lua_error(L));
2747
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002748 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002749 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002750 return 1;
2751}
2752
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002753/* Check arguments for the function "hlua_channel_dup_yield". */
2754__LJMP static int hlua_channel_dup(lua_State *L)
2755{
2756 MAY_LJMP(check_args(L, 1, "dup"));
2757 MAY_LJMP(hlua_checkchannel(L, 1));
2758 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2759}
2760
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002761/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2762 * a yield. This function consumes the data in the buffer. It returns
2763 * a string containing the data or a nil pointer if no data are available
2764 * and the channel is closed.
2765 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002766__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002767{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002768 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002769 int ret;
2770
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002771 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002772
Christopher Faulet3f829a42018-12-13 21:56:45 +01002773 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2774 WILL_LJMP(lua_error(L));
2775
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002776 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002777 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002778 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002779
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002780 if (unlikely(ret == -1))
2781 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002782
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002783 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002784 return 1;
2785}
2786
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002787/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002788__LJMP static int hlua_channel_get(lua_State *L)
2789{
2790 MAY_LJMP(check_args(L, 1, "get"));
2791 MAY_LJMP(hlua_checkchannel(L, 1));
2792 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2793}
2794
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002795/* This functions consumes and returns one line. If the channel is closed,
2796 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002797 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002798 * value.
2799 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002800__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002801{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002802 char *blk1;
2803 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002804 size_t len1;
2805 size_t len2;
2806 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002807 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002808 int ret;
2809 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002810
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002811 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2812
Christopher Faulet3f829a42018-12-13 21:56:45 +01002813 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2814 WILL_LJMP(lua_error(L));
2815
Willy Tarreau06d80a92017-10-19 14:32:15 +02002816 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002817 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002818 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002819
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002820 if (ret == -1) {
2821 lua_pushnil(L);
2822 return 1;
2823 }
2824
2825 luaL_buffinit(L, &b);
2826 luaL_addlstring(&b, blk1, len1);
2827 len = len1;
2828 if (unlikely(ret == 2)) {
2829 luaL_addlstring(&b, blk2, len2);
2830 len += len2;
2831 }
2832 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002833 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002834 return 1;
2835}
2836
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002837/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002838__LJMP static int hlua_channel_getline(lua_State *L)
2839{
2840 MAY_LJMP(check_args(L, 1, "getline"));
2841 MAY_LJMP(hlua_checkchannel(L, 1));
2842 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2843}
2844
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002845/* This function takes a string as input, and append it at the
2846 * input side of channel. If the data is too big, but a space
2847 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002848 * yields. If the data is bigger than the buffer, or if the
2849 * channel is closed, it returns -1. Otherwise, it returns the
2850 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002851 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002852__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002853{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002854 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002855 size_t len;
2856 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2857 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2858 int ret;
2859 int max;
2860
Christopher Faulet3f829a42018-12-13 21:56:45 +01002861 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2862 WILL_LJMP(lua_error(L));
2863
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002864 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002865 * the request buffer if its not required.
2866 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002867 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002868 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002869 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002870 }
2871
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002872 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002873 if (max > len - l)
2874 max = len - l;
2875
Willy Tarreau06d80a92017-10-19 14:32:15 +02002876 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002877 if (ret == -2 || ret == -3) {
2878 lua_pushinteger(L, -1);
2879 return 1;
2880 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002881 if (ret == -1) {
2882 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002883 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002884 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002885 l += ret;
2886 lua_pop(L, 1);
2887 lua_pushinteger(L, l);
2888
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002889 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002890 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002891 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002892 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002893 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002894 */
2895 return 1;
2896 }
2897 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002898 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002899 return 1;
2900}
2901
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002902/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2903 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002904 * buffer size is too little for the data.
2905 */
2906__LJMP static int hlua_channel_append(lua_State *L)
2907{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002908 size_t len;
2909
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002910 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002911 MAY_LJMP(hlua_checkchannel(L, 1));
2912 MAY_LJMP(luaL_checklstring(L, 2, &len));
2913 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002914 lua_pushinteger(L, 0);
2915
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002916 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002917}
2918
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002919/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002920 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002921 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002922 * or -1 if the channel is closed or if the buffer size is too
2923 * little for the data.
2924 */
2925__LJMP static int hlua_channel_set(lua_State *L)
2926{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002927 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002928
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002929 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002930 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002931 lua_pushinteger(L, 0);
2932
Christopher Faulet3f829a42018-12-13 21:56:45 +01002933 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2934 WILL_LJMP(lua_error(L));
2935
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002936 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002938 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002939}
2940
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002941/* Append data in the output side of the buffer. This data is immediately
2942 * sent. The function returns the amount of data written. If the buffer
2943 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002944 * if the channel is closed.
2945 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002946__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002947{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002948 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002949 size_t len;
2950 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2951 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2952 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002953 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002954
Christopher Faulet3f829a42018-12-13 21:56:45 +01002955 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2956 WILL_LJMP(lua_error(L));
2957
Willy Tarreau47860ed2015-03-10 14:07:50 +01002958 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002959 lua_pushinteger(L, -1);
2960 return 1;
2961 }
2962
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002963 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002964 * the request buffer if its not required.
2965 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002966 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002967 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002968 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002969 }
2970
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002971 /* The written data will be immediately sent, so we can check
2972 * the available space without taking in account the reserve.
2973 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002974 * data, because the buffer will be flushed.
2975 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002976 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002977
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002978 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002979 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002980 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002981 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02002982 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002983 return 1;
2984
2985 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002986 if (max > len - l)
2987 max = len - l;
2988
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002989 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002990 * detects a non contiguous buffer and realign it.
2991 */
Willy Tarreau3f679992018-06-15 15:06:42 +02002992 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002993 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002994
2995 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002996 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002997
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002998 /* buffer replace considers that the input part is filled.
2999 * so, I must forward these new data in the output part.
3000 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003001 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003002
3003 l += max;
3004 lua_pop(L, 1);
3005 lua_pushinteger(L, l);
3006
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003007 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003008 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003009 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003010 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003011 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003012 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003013 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003014
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003015 if (l < len) {
3016 /* If we are waiting for space in the response buffer, we
3017 * must set the flag WAKERESWR. This flag required the task
3018 * wake up if any activity is detected on the response buffer.
3019 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003020 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003021 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003022 else
3023 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003024 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003025 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003026
3027 return 1;
3028}
3029
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003030/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031 * yield the LUA process, and resume it without checking the
3032 * input arguments.
3033 */
3034__LJMP static int hlua_channel_send(lua_State *L)
3035{
3036 MAY_LJMP(check_args(L, 2, "send"));
3037 lua_pushinteger(L, 0);
3038
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003039 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003040}
3041
3042/* This function forward and amount of butes. The data pass from
3043 * the input side of the buffer to the output side, and can be
3044 * forwarded. This function never fails.
3045 *
3046 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003047 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003048 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003049__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003050{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003051 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003052 int len;
3053 int l;
3054 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003055 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003056
3057 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003058
3059 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3060 WILL_LJMP(lua_error(L));
3061
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003062 len = MAY_LJMP(luaL_checkinteger(L, 2));
3063 l = MAY_LJMP(luaL_checkinteger(L, -1));
3064
3065 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003066 if (max > ci_data(chn))
3067 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003068 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003069 l += max;
3070
3071 lua_pop(L, 1);
3072 lua_pushinteger(L, l);
3073
3074 /* Check if it miss bytes to forward. */
3075 if (l < len) {
3076 /* The the input channel or the output channel are closed, we
3077 * must return the amount of data forwarded.
3078 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003079 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003080 return 1;
3081
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003082 /* If we are waiting for space data in the response buffer, we
3083 * must set the flag WAKERESWR. This flag required the task
3084 * wake up if any activity is detected on the response buffer.
3085 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003086 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003087 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003088 else
3089 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003090
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003091 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003092 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003093 }
3094
3095 return 1;
3096}
3097
3098/* Just check the input and prepare the stack for the previous
3099 * function "hlua_channel_forward_yield"
3100 */
3101__LJMP static int hlua_channel_forward(lua_State *L)
3102{
3103 MAY_LJMP(check_args(L, 2, "forward"));
3104 MAY_LJMP(hlua_checkchannel(L, 1));
3105 MAY_LJMP(luaL_checkinteger(L, 2));
3106
3107 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003108 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003109}
3110
3111/* Just returns the number of bytes available in the input
3112 * side of the buffer. This function never fails.
3113 */
3114__LJMP static int hlua_channel_get_in_len(lua_State *L)
3115{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003116 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003117
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003118 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003119 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003120 if (IS_HTX_STRM(chn_strm(chn))) {
3121 struct htx *htx = htxbuf(&chn->buf);
3122 lua_pushinteger(L, htx->data - co_data(chn));
3123 }
3124 else
3125 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003126 return 1;
3127}
3128
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003129/* Returns true if the channel is full. */
3130__LJMP static int hlua_channel_is_full(lua_State *L)
3131{
3132 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003133
3134 MAY_LJMP(check_args(L, 1, "is_full"));
3135 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003136 /* ignore the reserve, we are not on a producer side (ie in an
3137 * applet).
3138 */
3139 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003140 return 1;
3141}
3142
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003143/* Returns true if the channel is the response channel. */
3144__LJMP static int hlua_channel_is_resp(lua_State *L)
3145{
3146 struct channel *chn;
3147
3148 MAY_LJMP(check_args(L, 1, "is_resp"));
3149 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3150
3151 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3152 return 1;
3153}
3154
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003155/* Just returns the number of bytes available in the output
3156 * side of the buffer. This function never fails.
3157 */
3158__LJMP static int hlua_channel_get_out_len(lua_State *L)
3159{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003160 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003161
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003162 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003163 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003164 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003165 return 1;
3166}
3167
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003168/*
3169 *
3170 *
3171 * Class Fetches
3172 *
3173 *
3174 */
3175
3176/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003177 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003178 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003179__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003180{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003181 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003182}
3183
3184/* This function creates and push in the stack a fetch object according
3185 * with a current TXN.
3186 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003187static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003188{
Willy Tarreau7073c472015-04-06 11:15:40 +02003189 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003190
3191 /* Check stack size. */
3192 if (!lua_checkstack(L, 3))
3193 return 0;
3194
3195 /* Create the object: obj[0] = userdata.
3196 * Note that the base of the Fetches object is the
3197 * transaction object.
3198 */
3199 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003200 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003201 lua_rawseti(L, -2, 0);
3202
Willy Tarreau7073c472015-04-06 11:15:40 +02003203 hsmp->s = txn->s;
3204 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003205 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003206 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003207
3208 /* Pop a class sesison metatable and affect it to the userdata. */
3209 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3210 lua_setmetatable(L, -2);
3211
3212 return 1;
3213}
3214
3215/* This function is an LUA binding. It is called with each sample-fetch.
3216 * It uses closure argument to store the associated sample-fetch. It
3217 * returns only one argument or throws an error. An error is thrown
3218 * only if an error is encountered during the argument parsing. If
3219 * the "sample-fetch" function fails, nil is returned.
3220 */
3221__LJMP static int hlua_run_sample_fetch(lua_State *L)
3222{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003223 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003224 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003225 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003226 int i;
3227 struct sample smp;
3228
3229 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003230 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003231
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003232 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003233 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003234
Thierry FOURNIERca988662015-12-20 18:43:03 +01003235 /* Check execution authorization. */
3236 if (f->use & SMP_USE_HTTP_ANY &&
3237 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3238 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3239 "is not available in Lua services", f->kw);
3240 WILL_LJMP(lua_error(L));
3241 }
3242
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003243 /* Get extra arguments. */
3244 for (i = 0; i < lua_gettop(L) - 1; i++) {
3245 if (i >= ARGM_NBARGS)
3246 break;
3247 hlua_lua2arg(L, i + 2, &args[i]);
3248 }
3249 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003250 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003251
3252 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003253 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003254
3255 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003256 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003257 lua_pushfstring(L, "error in arguments");
3258 WILL_LJMP(lua_error(L));
3259 }
3260
3261 /* Initialise the sample. */
3262 memset(&smp, 0, sizeof(smp));
3263
3264 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003265 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003266 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003267 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003268 lua_pushstring(L, "");
3269 else
3270 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003271 return 1;
3272 }
3273
3274 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003275 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003276 hlua_smp2lua_str(L, &smp);
3277 else
3278 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003279 return 1;
3280}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003281
3282/*
3283 *
3284 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003285 * Class Converters
3286 *
3287 *
3288 */
3289
3290/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003291 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003292 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003293__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003294{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003295 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003296}
3297
3298/* This function creates and push in the stack a Converters object
3299 * according with a current TXN.
3300 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003301static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003302{
Willy Tarreau7073c472015-04-06 11:15:40 +02003303 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003304
3305 /* Check stack size. */
3306 if (!lua_checkstack(L, 3))
3307 return 0;
3308
3309 /* Create the object: obj[0] = userdata.
3310 * Note that the base of the Converters object is the
3311 * same than the TXN object.
3312 */
3313 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003314 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003315 lua_rawseti(L, -2, 0);
3316
Willy Tarreau7073c472015-04-06 11:15:40 +02003317 hsmp->s = txn->s;
3318 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003319 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003320 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003321
Willy Tarreau87b09662015-04-03 00:22:06 +02003322 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003323 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3324 lua_setmetatable(L, -2);
3325
3326 return 1;
3327}
3328
3329/* This function is an LUA binding. It is called with each converter.
3330 * It uses closure argument to store the associated converter. It
3331 * returns only one argument or throws an error. An error is thrown
3332 * only if an error is encountered during the argument parsing. If
3333 * the converter function function fails, nil is returned.
3334 */
3335__LJMP static int hlua_run_sample_conv(lua_State *L)
3336{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003337 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003338 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003339 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003340 int i;
3341 struct sample smp;
3342
3343 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003344 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003345
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003346 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003347 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003348
3349 /* Get extra arguments. */
3350 for (i = 0; i < lua_gettop(L) - 2; i++) {
3351 if (i >= ARGM_NBARGS)
3352 break;
3353 hlua_lua2arg(L, i + 3, &args[i]);
3354 }
3355 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003356 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003357
3358 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003359 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003360
3361 /* Run the special args checker. */
3362 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3363 hlua_pusherror(L, "error in arguments");
3364 WILL_LJMP(lua_error(L));
3365 }
3366
3367 /* Initialise the sample. */
3368 if (!hlua_lua2smp(L, 2, &smp)) {
3369 hlua_pusherror(L, "error in the input argument");
3370 WILL_LJMP(lua_error(L));
3371 }
3372
Willy Tarreau1777ea62016-03-10 16:15:46 +01003373 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3374
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003375 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003376 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003377 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003378 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003379 WILL_LJMP(lua_error(L));
3380 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003381 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3382 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003383 hlua_pusherror(L, "error during the input argument casting");
3384 WILL_LJMP(lua_error(L));
3385 }
3386
3387 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003388 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003389 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003390 lua_pushstring(L, "");
3391 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003392 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003393 return 1;
3394 }
3395
3396 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003397 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003398 hlua_smp2lua_str(L, &smp);
3399 else
3400 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003401 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003402}
3403
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003404/*
3405 *
3406 *
3407 * Class AppletTCP
3408 *
3409 *
3410 */
3411
3412/* Returns a struct hlua_txn if the stack entry "ud" is
3413 * a class stream, otherwise it throws an error.
3414 */
3415__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3416{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003417 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003418}
3419
3420/* This function creates and push in the stack an Applet object
3421 * according with a current TXN.
3422 */
3423static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3424{
3425 struct hlua_appctx *appctx;
3426 struct stream_interface *si = ctx->owner;
3427 struct stream *s = si_strm(si);
3428 struct proxy *p = s->be;
3429
3430 /* Check stack size. */
3431 if (!lua_checkstack(L, 3))
3432 return 0;
3433
3434 /* Create the object: obj[0] = userdata.
3435 * Note that the base of the Converters object is the
3436 * same than the TXN object.
3437 */
3438 lua_newtable(L);
3439 appctx = lua_newuserdata(L, sizeof(*appctx));
3440 lua_rawseti(L, -2, 0);
3441 appctx->appctx = ctx;
3442 appctx->htxn.s = s;
3443 appctx->htxn.p = p;
3444
3445 /* Create the "f" field that contains a list of fetches. */
3446 lua_pushstring(L, "f");
3447 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3448 return 0;
3449 lua_settable(L, -3);
3450
3451 /* Create the "sf" field that contains a list of stringsafe fetches. */
3452 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003453 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003454 return 0;
3455 lua_settable(L, -3);
3456
3457 /* Create the "c" field that contains a list of converters. */
3458 lua_pushstring(L, "c");
3459 if (!hlua_converters_new(L, &appctx->htxn, 0))
3460 return 0;
3461 lua_settable(L, -3);
3462
3463 /* Create the "sc" field that contains a list of stringsafe converters. */
3464 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003465 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003466 return 0;
3467 lua_settable(L, -3);
3468
3469 /* Pop a class stream metatable and affect it to the table. */
3470 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3471 lua_setmetatable(L, -2);
3472
3473 return 1;
3474}
3475
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003476__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3477{
3478 struct hlua_appctx *appctx;
3479 struct stream *s;
3480 const char *name;
3481 size_t len;
3482 struct sample smp;
3483
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003484 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3485 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003486
3487 /* It is useles to retrieve the stream, but this function
3488 * runs only in a stream context.
3489 */
3490 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3491 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3492 s = appctx->htxn.s;
3493
3494 /* Converts the third argument in a sample. */
3495 hlua_lua2smp(L, 3, &smp);
3496
3497 /* Store the sample in a variable. */
3498 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003499
3500 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3501 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3502 else
3503 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3504
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003505 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003506}
3507
3508__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3509{
3510 struct hlua_appctx *appctx;
3511 struct stream *s;
3512 const char *name;
3513 size_t len;
3514 struct sample smp;
3515
3516 MAY_LJMP(check_args(L, 2, "unset_var"));
3517
3518 /* It is useles to retrieve the stream, but this function
3519 * runs only in a stream context.
3520 */
3521 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3522 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3523 s = appctx->htxn.s;
3524
3525 /* Unset the variable. */
3526 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003527 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3528 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003529}
3530
3531__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3532{
3533 struct hlua_appctx *appctx;
3534 struct stream *s;
3535 const char *name;
3536 size_t len;
3537 struct sample smp;
3538
3539 MAY_LJMP(check_args(L, 2, "get_var"));
3540
3541 /* It is useles to retrieve the stream, but this function
3542 * runs only in a stream context.
3543 */
3544 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3545 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3546 s = appctx->htxn.s;
3547
3548 smp_set_owner(&smp, s->be, s->sess, s, 0);
3549 if (!vars_get_by_name(name, len, &smp)) {
3550 lua_pushnil(L);
3551 return 1;
3552 }
3553
3554 return hlua_smp2lua(L, &smp);
3555}
3556
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003557__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3558{
3559 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3560 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003561 struct hlua *hlua;
3562
3563 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003564 if (!s->hlua)
3565 return 0;
3566 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003567
3568 MAY_LJMP(check_args(L, 2, "set_priv"));
3569
3570 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003571 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003572
3573 /* Get and store new value. */
3574 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3575 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3576
3577 return 0;
3578}
3579
3580__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3581{
3582 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3583 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003584 struct hlua *hlua;
3585
3586 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003587 if (!s->hlua) {
3588 lua_pushnil(L);
3589 return 1;
3590 }
3591 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003592
3593 /* Push configuration index in the stack. */
3594 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3595
3596 return 1;
3597}
3598
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003599/* If expected data not yet available, it returns a yield. This function
3600 * consumes the data in the buffer. It returns a string containing the
3601 * data. This string can be empty.
3602 */
3603__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3604{
3605 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3606 struct stream_interface *si = appctx->appctx->owner;
3607 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003608 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003609 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003610 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003611 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003612
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003613 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003614 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003615
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003616 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003617 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003618 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003619 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003620 }
3621
3622 /* End of data: commit the total strings and return. */
3623 if (ret < 0) {
3624 luaL_pushresult(&appctx->b);
3625 return 1;
3626 }
3627
3628 /* Ensure that the block 2 length is usable. */
3629 if (ret == 1)
3630 len2 = 0;
3631
3632 /* dont check the max length read and dont check. */
3633 luaL_addlstring(&appctx->b, blk1, len1);
3634 luaL_addlstring(&appctx->b, blk2, len2);
3635
3636 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003637 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003638 luaL_pushresult(&appctx->b);
3639 return 1;
3640}
3641
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003642/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003643__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3644{
3645 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3646
3647 /* Initialise the string catenation. */
3648 luaL_buffinit(L, &appctx->b);
3649
3650 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3651}
3652
3653/* If expected data not yet available, it returns a yield. This function
3654 * consumes the data in the buffer. It returns a string containing the
3655 * data. This string can be empty.
3656 */
3657__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3658{
3659 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3660 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003661 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003662 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003663 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003664 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003665 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003666 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003667
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003668 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003669 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003670
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003671 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003672 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003673 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003674 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003675 }
3676
3677 /* End of data: commit the total strings and return. */
3678 if (ret < 0) {
3679 luaL_pushresult(&appctx->b);
3680 return 1;
3681 }
3682
3683 /* Ensure that the block 2 length is usable. */
3684 if (ret == 1)
3685 len2 = 0;
3686
3687 if (len == -1) {
3688
3689 /* If len == -1, catenate all the data avalaile and
3690 * yield because we want to get all the data until
3691 * the end of data stream.
3692 */
3693 luaL_addlstring(&appctx->b, blk1, len1);
3694 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003695 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003696 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003697 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003698
3699 } else {
3700
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003701 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003702 if (len1 > len)
3703 len1 = len;
3704 luaL_addlstring(&appctx->b, blk1, len1);
3705 len -= len1;
3706
3707 /* Copy the second block. */
3708 if (len2 > len)
3709 len2 = len;
3710 luaL_addlstring(&appctx->b, blk2, len2);
3711 len -= len2;
3712
3713 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003714 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003715
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003716 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003717 if (len > 0) {
3718 lua_pushinteger(L, len);
3719 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003720 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003721 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003722 }
3723
3724 /* return the result. */
3725 luaL_pushresult(&appctx->b);
3726 return 1;
3727 }
3728
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003729 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003730 hlua_pusherror(L, "Lua: internal error");
3731 WILL_LJMP(lua_error(L));
3732 return 0;
3733}
3734
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003735/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003736__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3737{
3738 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3739 int len = -1;
3740
3741 if (lua_gettop(L) > 2)
3742 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3743 if (lua_gettop(L) >= 2) {
3744 len = MAY_LJMP(luaL_checkinteger(L, 2));
3745 lua_pop(L, 1);
3746 }
3747
3748 /* Confirm or set the required length */
3749 lua_pushinteger(L, len);
3750
3751 /* Initialise the string catenation. */
3752 luaL_buffinit(L, &appctx->b);
3753
3754 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3755}
3756
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003757/* Append data in the output side of the buffer. This data is immediately
3758 * sent. The function returns the amount of data written. If the buffer
3759 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003760 * if the channel is closed.
3761 */
3762__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3763{
3764 size_t len;
3765 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3766 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3767 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3768 struct stream_interface *si = appctx->appctx->owner;
3769 struct channel *chn = si_ic(si);
3770 int max;
3771
3772 /* Get the max amount of data which can write as input in the channel. */
3773 max = channel_recv_max(chn);
3774 if (max > (len - l))
3775 max = len - l;
3776
3777 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003778 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003779
3780 /* update counters. */
3781 l += max;
3782 lua_pop(L, 1);
3783 lua_pushinteger(L, l);
3784
3785 /* If some data is not send, declares the situation to the
3786 * applet, and returns a yield.
3787 */
3788 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003789 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003790 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003791 }
3792
3793 return 1;
3794}
3795
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003796/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003797 * yield the LUA process, and resume it without checking the
3798 * input arguments.
3799 */
3800__LJMP static int hlua_applet_tcp_send(lua_State *L)
3801{
3802 MAY_LJMP(check_args(L, 2, "send"));
3803 lua_pushinteger(L, 0);
3804
3805 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3806}
3807
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003808/*
3809 *
3810 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003811 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003812 *
3813 *
3814 */
3815
3816/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003817 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003818 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003819__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003820{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003821 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003822}
3823
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003824/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003825 * according with a current TXN.
3826 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003827static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003828{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003829 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003830 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003831 struct stream_interface *si = ctx->owner;
3832 struct stream *s = si_strm(si);
3833 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003834 struct htx *htx;
3835 struct htx_blk *blk;
3836 struct htx_sl *sl;
3837 struct ist path;
3838 unsigned long long len = 0;
3839 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003840
3841 /* Check stack size. */
3842 if (!lua_checkstack(L, 3))
3843 return 0;
3844
3845 /* Create the object: obj[0] = userdata.
3846 * Note that the base of the Converters object is the
3847 * same than the TXN object.
3848 */
3849 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003850 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003851 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003852 appctx->appctx = ctx;
3853 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003854 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003855 appctx->htxn.s = s;
3856 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003857
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003858 /* Create the "f" field that contains a list of fetches. */
3859 lua_pushstring(L, "f");
3860 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3861 return 0;
3862 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003863
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003864 /* Create the "sf" field that contains a list of stringsafe fetches. */
3865 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003866 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003867 return 0;
3868 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003869
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003870 /* Create the "c" field that contains a list of converters. */
3871 lua_pushstring(L, "c");
3872 if (!hlua_converters_new(L, &appctx->htxn, 0))
3873 return 0;
3874 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003875
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003876 /* Create the "sc" field that contains a list of stringsafe converters. */
3877 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003878 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003879 return 0;
3880 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003881
Christopher Fauleta2097962019-07-15 16:25:33 +02003882 htx = htxbuf(&s->req.buf);
3883 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003884 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003885 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003886
Christopher Fauleta2097962019-07-15 16:25:33 +02003887 /* Stores the request method. */
3888 lua_pushstring(L, "method");
3889 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3890 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003891
Christopher Fauleta2097962019-07-15 16:25:33 +02003892 /* Stores the http version. */
3893 lua_pushstring(L, "version");
3894 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3895 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003896
Christopher Fauleta2097962019-07-15 16:25:33 +02003897 /* creates an array of headers. hlua_http_get_headers() crates and push
3898 * the array on the top of the stack.
3899 */
3900 lua_pushstring(L, "headers");
3901 htxn.s = s;
3902 htxn.p = px;
3903 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01003904 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02003905 return 0;
3906 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003907
Christopher Fauleta2097962019-07-15 16:25:33 +02003908 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01003909 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02003910 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003911
Christopher Fauleta2097962019-07-15 16:25:33 +02003912 p = path.ptr;
3913 end = path.ptr + path.len;
3914 q = p;
3915 while (q < end && *q != '?')
3916 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003917
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003918 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003919 lua_pushstring(L, "path");
3920 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003921 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003922
Christopher Fauleta2097962019-07-15 16:25:33 +02003923 /* Stores the query string. */
3924 lua_pushstring(L, "qs");
3925 if (*q == '?')
3926 q++;
3927 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003928 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003929 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003930
Christopher Fauleta2097962019-07-15 16:25:33 +02003931 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3932 struct htx_blk *blk = htx_get_blk(htx, pos);
3933 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003934
Christopher Fauleta2097962019-07-15 16:25:33 +02003935 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3936 break;
3937 if (type == HTX_BLK_DATA)
3938 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003939 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003940 if (htx->extra != ULLONG_MAX)
3941 len += htx->extra;
3942
3943 /* Stores the request path. */
3944 lua_pushstring(L, "length");
3945 lua_pushinteger(L, len);
3946 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003947
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003948 /* Create an empty array of HTTP request headers. */
3949 lua_pushstring(L, "response");
3950 lua_newtable(L);
3951 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003952
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003953 /* Pop a class stream metatable and affect it to the table. */
3954 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3955 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003956
3957 return 1;
3958}
3959
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003960__LJMP static int hlua_applet_http_set_var(lua_State *L)
3961{
3962 struct hlua_appctx *appctx;
3963 struct stream *s;
3964 const char *name;
3965 size_t len;
3966 struct sample smp;
3967
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003968 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3969 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003970
3971 /* It is useles to retrieve the stream, but this function
3972 * runs only in a stream context.
3973 */
3974 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3975 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3976 s = appctx->htxn.s;
3977
3978 /* Converts the third argument in a sample. */
3979 hlua_lua2smp(L, 3, &smp);
3980
3981 /* Store the sample in a variable. */
3982 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003983
3984 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3985 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3986 else
3987 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3988
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003989 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003990}
3991
3992__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3993{
3994 struct hlua_appctx *appctx;
3995 struct stream *s;
3996 const char *name;
3997 size_t len;
3998 struct sample smp;
3999
4000 MAY_LJMP(check_args(L, 2, "unset_var"));
4001
4002 /* It is useles to retrieve the stream, but this function
4003 * runs only in a stream context.
4004 */
4005 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4006 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4007 s = appctx->htxn.s;
4008
4009 /* Unset the variable. */
4010 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004011 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4012 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004013}
4014
4015__LJMP static int hlua_applet_http_get_var(lua_State *L)
4016{
4017 struct hlua_appctx *appctx;
4018 struct stream *s;
4019 const char *name;
4020 size_t len;
4021 struct sample smp;
4022
4023 MAY_LJMP(check_args(L, 2, "get_var"));
4024
4025 /* It is useles to retrieve the stream, but this function
4026 * runs only in a stream context.
4027 */
4028 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4029 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4030 s = appctx->htxn.s;
4031
4032 smp_set_owner(&smp, s->be, s->sess, s, 0);
4033 if (!vars_get_by_name(name, len, &smp)) {
4034 lua_pushnil(L);
4035 return 1;
4036 }
4037
4038 return hlua_smp2lua(L, &smp);
4039}
4040
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004041__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4042{
4043 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4044 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004045 struct hlua *hlua;
4046
4047 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004048 if (!s->hlua)
4049 return 0;
4050 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004051
4052 MAY_LJMP(check_args(L, 2, "set_priv"));
4053
4054 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004055 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004056
4057 /* Get and store new value. */
4058 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4059 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4060
4061 return 0;
4062}
4063
4064__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4065{
4066 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4067 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004068 struct hlua *hlua;
4069
4070 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004071 if (!s->hlua) {
4072 lua_pushnil(L);
4073 return 1;
4074 }
4075 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004076
4077 /* Push configuration index in the stack. */
4078 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4079
4080 return 1;
4081}
4082
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004083/* If expected data not yet available, it returns a yield. This function
4084 * consumes the data in the buffer. It returns a string containing the
4085 * data. This string can be empty.
4086 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004087__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004088{
4089 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4090 struct stream_interface *si = appctx->appctx->owner;
4091 struct channel *req = si_oc(si);
4092 struct htx *htx;
4093 struct htx_blk *blk;
4094 size_t count;
4095 int stop = 0;
4096
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004097 htx = htx_from_buf(&req->buf);
4098 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004099 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004100
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004101 while (count && !stop && blk) {
4102 enum htx_blk_type type = htx_get_blk_type(blk);
4103 uint32_t sz = htx_get_blksz(blk);
4104 struct ist v;
4105 uint32_t vlen;
4106 char *nl;
4107
Christopher Faulete461e342018-12-18 16:43:35 +01004108 if (type == HTX_BLK_EOM) {
4109 stop = 1;
4110 break;
4111 }
4112
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004113 vlen = sz;
4114 if (vlen > count) {
4115 if (type != HTX_BLK_DATA)
4116 break;
4117 vlen = count;
4118 }
4119
4120 switch (type) {
4121 case HTX_BLK_UNUSED:
4122 break;
4123
4124 case HTX_BLK_DATA:
4125 v = htx_get_blk_value(htx, blk);
4126 v.len = vlen;
4127 nl = istchr(v, '\n');
4128 if (nl != NULL) {
4129 stop = 1;
4130 vlen = nl - v.ptr + 1;
4131 }
4132 luaL_addlstring(&appctx->b, v.ptr, vlen);
4133 break;
4134
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004135 case HTX_BLK_TLR:
4136 case HTX_BLK_EOM:
4137 stop = 1;
4138 break;
4139
4140 default:
4141 break;
4142 }
4143
4144 co_set_data(req, co_data(req) - vlen);
4145 count -= vlen;
4146 if (sz == vlen)
4147 blk = htx_remove_blk(htx, blk);
4148 else {
4149 htx_cut_data_blk(htx, blk, vlen);
4150 break;
4151 }
4152 }
4153
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004154 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004155 if (!stop) {
4156 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004157 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004158 }
4159
4160 /* return the result. */
4161 luaL_pushresult(&appctx->b);
4162 return 1;
4163}
4164
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004165
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004166/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004167__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004168{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004169 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004170
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004171 /* Initialise the string catenation. */
4172 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004173
Christopher Fauleta2097962019-07-15 16:25:33 +02004174 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004175}
4176
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004177/* If expected data not yet available, it returns a yield. This function
4178 * consumes the data in the buffer. It returns a string containing the
4179 * data. This string can be empty.
4180 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004181__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004182{
4183 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4184 struct stream_interface *si = appctx->appctx->owner;
4185 struct channel *req = si_oc(si);
4186 struct htx *htx;
4187 struct htx_blk *blk;
4188 size_t count;
4189 int len;
4190
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004191 htx = htx_from_buf(&req->buf);
4192 len = MAY_LJMP(luaL_checkinteger(L, 2));
4193 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004194 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004195 while (count && len && blk) {
4196 enum htx_blk_type type = htx_get_blk_type(blk);
4197 uint32_t sz = htx_get_blksz(blk);
4198 struct ist v;
4199 uint32_t vlen;
4200
Christopher Faulete461e342018-12-18 16:43:35 +01004201 if (type == HTX_BLK_EOM) {
4202 len = 0;
4203 break;
4204 }
4205
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004206 vlen = sz;
4207 if (len > 0 && vlen > len)
4208 vlen = len;
4209 if (vlen > count) {
4210 if (type != HTX_BLK_DATA)
4211 break;
4212 vlen = count;
4213 }
4214
4215 switch (type) {
4216 case HTX_BLK_UNUSED:
4217 break;
4218
4219 case HTX_BLK_DATA:
4220 v = htx_get_blk_value(htx, blk);
4221 luaL_addlstring(&appctx->b, v.ptr, vlen);
4222 break;
4223
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004224 case HTX_BLK_TLR:
4225 case HTX_BLK_EOM:
4226 len = 0;
4227 break;
4228
4229 default:
4230 break;
4231 }
4232
4233 co_set_data(req, co_data(req) - vlen);
4234 count -= vlen;
4235 if (len > 0)
4236 len -= vlen;
4237 if (sz == vlen)
4238 blk = htx_remove_blk(htx, blk);
4239 else {
4240 htx_cut_data_blk(htx, blk, vlen);
4241 break;
4242 }
4243 }
4244
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004245 htx_to_buf(htx, &req->buf);
4246
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004247 /* If we are no other data available, yield waiting for new data. */
4248 if (len) {
4249 if (len > 0) {
4250 lua_pushinteger(L, len);
4251 lua_replace(L, 2);
4252 }
4253 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004254 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004255 }
4256
4257 /* return the result. */
4258 luaL_pushresult(&appctx->b);
4259 return 1;
4260}
4261
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004262/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004263__LJMP static int hlua_applet_http_recv(lua_State *L)
4264{
4265 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4266 int len = -1;
4267
4268 /* Check arguments. */
4269 if (lua_gettop(L) > 2)
4270 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4271 if (lua_gettop(L) >= 2) {
4272 len = MAY_LJMP(luaL_checkinteger(L, 2));
4273 lua_pop(L, 1);
4274 }
4275
Christopher Fauleta2097962019-07-15 16:25:33 +02004276 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004277
Christopher Fauleta2097962019-07-15 16:25:33 +02004278 /* Initialise the string catenation. */
4279 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004280
Christopher Fauleta2097962019-07-15 16:25:33 +02004281 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004282}
4283
4284/* Append data in the output side of the buffer. This data is immediately
4285 * sent. The function returns the amount of data written. If the buffer
4286 * cannot contain the data, the function yields. The function returns -1
4287 * if the channel is closed.
4288 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004289__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004290{
4291 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4292 struct stream_interface *si = appctx->appctx->owner;
4293 struct channel *res = si_ic(si);
4294 struct htx *htx = htx_from_buf(&res->buf);
4295 const char *data;
4296 size_t len;
4297 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4298 int max;
4299
Christopher Faulet9060fc02019-07-03 11:39:30 +02004300 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004301 if (!max)
4302 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004303
4304 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4305
4306 /* Get the max amount of data which can write as input in the channel. */
4307 if (max > (len - l))
4308 max = len - l;
4309
4310 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004311 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004312 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004313
4314 /* update counters. */
4315 l += max;
4316 lua_pop(L, 1);
4317 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004318
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004319 /* If some data is not send, declares the situation to the
4320 * applet, and returns a yield.
4321 */
4322 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004323 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004324 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004325 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004326 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004327 }
4328
Christopher Fauleta2097962019-07-15 16:25:33 +02004329 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004330 return 1;
4331}
4332
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004333/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004334 * yield the LUA process, and resume it without checking the
4335 * input arguments.
4336 */
4337__LJMP static int hlua_applet_http_send(lua_State *L)
4338{
4339 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004340
4341 /* We want to send some data. Headers must be sent. */
4342 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4343 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4344 WILL_LJMP(lua_error(L));
4345 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004346
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004347 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004348 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004349
Christopher Fauleta2097962019-07-15 16:25:33 +02004350 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004351}
4352
4353__LJMP static int hlua_applet_http_addheader(lua_State *L)
4354{
4355 const char *name;
4356 int ret;
4357
4358 MAY_LJMP(hlua_checkapplet_http(L, 1));
4359 name = MAY_LJMP(luaL_checkstring(L, 2));
4360 MAY_LJMP(luaL_checkstring(L, 3));
4361
4362 /* Push in the stack the "response" entry. */
4363 ret = lua_getfield(L, 1, "response");
4364 if (ret != LUA_TTABLE) {
4365 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4366 "is expected as an array. %s found", lua_typename(L, ret));
4367 WILL_LJMP(lua_error(L));
4368 }
4369
4370 /* check if the header is already registered if it is not
4371 * the case, register it.
4372 */
4373 ret = lua_getfield(L, -1, name);
4374 if (ret == LUA_TNIL) {
4375
4376 /* Entry not found. */
4377 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4378
4379 /* Insert the new header name in the array in the top of the stack.
4380 * It left the new array in the top of the stack.
4381 */
4382 lua_newtable(L);
4383 lua_pushvalue(L, 2);
4384 lua_pushvalue(L, -2);
4385 lua_settable(L, -4);
4386
4387 } else if (ret != LUA_TTABLE) {
4388
4389 /* corruption error. */
4390 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4391 "is expected as an array. %s found", name, lua_typename(L, ret));
4392 WILL_LJMP(lua_error(L));
4393 }
4394
4395 /* Now the top od thestack is an array of values. We push
4396 * the header value as new entry.
4397 */
4398 lua_pushvalue(L, 3);
4399 ret = lua_rawlen(L, -2);
4400 lua_rawseti(L, -2, ret + 1);
4401 lua_pushboolean(L, 1);
4402 return 1;
4403}
4404
4405__LJMP static int hlua_applet_http_status(lua_State *L)
4406{
4407 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4408 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004409 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004410
4411 if (status < 100 || status > 599) {
4412 lua_pushboolean(L, 0);
4413 return 1;
4414 }
4415
4416 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004417 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004418 lua_pushboolean(L, 1);
4419 return 1;
4420}
4421
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004422
Christopher Fauleta2097962019-07-15 16:25:33 +02004423__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004424{
4425 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4426 struct stream_interface *si = appctx->appctx->owner;
4427 struct channel *res = si_ic(si);
4428 struct htx *htx;
4429 struct htx_sl *sl;
4430 struct h1m h1m;
4431 const char *status, *reason;
4432 const char *name, *value;
4433 size_t nlen, vlen;
4434 unsigned int flags;
4435
4436 /* Send the message at once. */
4437 htx = htx_from_buf(&res->buf);
4438 h1m_init_res(&h1m);
4439
4440 /* Use the same http version than the request. */
4441 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4442 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4443 if (reason == NULL)
4444 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4445 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4446 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4447 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4448 }
4449 else {
4450 flags = HTX_SL_F_IS_RESP;
4451 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4452 }
4453 if (!sl) {
4454 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4455 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4456 WILL_LJMP(lua_error(L));
4457 }
4458 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4459
4460 /* Get the array associated to the field "response" in the object AppletHTTP. */
4461 lua_pushvalue(L, 0);
4462 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4463 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4464 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4465 WILL_LJMP(lua_error(L));
4466 }
4467
4468 /* Browse the list of headers. */
4469 lua_pushnil(L);
4470 while(lua_next(L, -2) != 0) {
4471 /* We expect a string as -2. */
4472 if (lua_type(L, -2) != LUA_TSTRING) {
4473 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4474 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4475 lua_typename(L, lua_type(L, -2)));
4476 WILL_LJMP(lua_error(L));
4477 }
4478 name = lua_tolstring(L, -2, &nlen);
4479
4480 /* We expect an array as -1. */
4481 if (lua_type(L, -1) != LUA_TTABLE) {
4482 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4483 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4484 name,
4485 lua_typename(L, lua_type(L, -1)));
4486 WILL_LJMP(lua_error(L));
4487 }
4488
4489 /* Browse the table who is on the top of the stack. */
4490 lua_pushnil(L);
4491 while(lua_next(L, -2) != 0) {
4492 int id;
4493
4494 /* We expect a number as -2. */
4495 if (lua_type(L, -2) != LUA_TNUMBER) {
4496 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4497 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4498 name,
4499 lua_typename(L, lua_type(L, -2)));
4500 WILL_LJMP(lua_error(L));
4501 }
4502 id = lua_tointeger(L, -2);
4503
4504 /* We expect a string as -2. */
4505 if (lua_type(L, -1) != LUA_TSTRING) {
4506 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4507 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4508 name, id,
4509 lua_typename(L, lua_type(L, -1)));
4510 WILL_LJMP(lua_error(L));
4511 }
4512 value = lua_tolstring(L, -1, &vlen);
4513
4514 /* Simple Protocol checks. */
4515 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004516 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004517 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4518 struct ist v = ist2(value, vlen);
4519 int ret;
4520
4521 ret = h1_parse_cont_len_header(&h1m, &v);
4522 if (ret < 0) {
4523 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4524 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4525 name);
4526 WILL_LJMP(lua_error(L));
4527 }
4528 else if (ret == 0)
4529 goto next; /* Skip it */
4530 }
4531
4532 /* Add a new header */
4533 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4534 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4535 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4536 name);
4537 WILL_LJMP(lua_error(L));
4538 }
4539 next:
4540 /* Remove the array from the stack, and get next element with a remaining string. */
4541 lua_pop(L, 1);
4542 }
4543
4544 /* Remove the array from the stack, and get next element with a remaining string. */
4545 lua_pop(L, 1);
4546 }
4547
4548 if (h1m.flags & H1_MF_CHNK)
4549 h1m.flags &= ~H1_MF_CLEN;
4550 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4551 h1m.flags |= H1_MF_XFER_LEN;
4552
4553 /* Uset HTX start-line flags */
4554 if (h1m.flags & H1_MF_XFER_ENC)
4555 flags |= HTX_SL_F_XFER_ENC;
4556 if (h1m.flags & H1_MF_XFER_LEN) {
4557 flags |= HTX_SL_F_XFER_LEN;
4558 if (h1m.flags & H1_MF_CHNK)
4559 flags |= HTX_SL_F_CHNK;
4560 else if (h1m.flags & H1_MF_CLEN)
4561 flags |= HTX_SL_F_CLEN;
4562 if (h1m.body_len == 0)
4563 flags |= HTX_SL_F_BODYLESS;
4564 }
4565 sl->flags |= flags;
4566
4567 /* If we dont have a content-length set, and the HTTP version is 1.1
4568 * and the status code implies the presence of a message body, we must
4569 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004570 * for the keepalive compliance. If the applet announces a transfer-encoding
4571 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004572 */
4573 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4574 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4575 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4576 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4577 /* Add a new header */
4578 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4579 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4580 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4581 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4582 WILL_LJMP(lua_error(L));
4583 }
4584 }
4585
4586 /* Finalize headers. */
4587 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4588 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4589 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4590 WILL_LJMP(lua_error(L));
4591 }
4592
4593 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4594 b_reset(&res->buf);
4595 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4596 WILL_LJMP(lua_error(L));
4597 }
4598
4599 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004600 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004601
4602 /* Headers sent, set the flag. */
4603 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4604 return 0;
4605
4606}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004607/* We will build the status line and the headers of the HTTP response.
4608 * We will try send at once if its not possible, we give back the hand
4609 * waiting for more room.
4610 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004611__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004612{
4613 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4614 struct stream_interface *si = appctx->appctx->owner;
4615 struct channel *res = si_ic(si);
4616
4617 if (co_data(res)) {
4618 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004619 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004620 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004621 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004622}
4623
4624
Christopher Fauleta2097962019-07-15 16:25:33 +02004625__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004626{
Christopher Fauleta2097962019-07-15 16:25:33 +02004627 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004628}
4629
Christopher Fauleta2097962019-07-15 16:25:33 +02004630/*
4631 *
4632 *
4633 * Class HTTP
4634 *
4635 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004636 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004637
4638/* Returns a struct hlua_txn if the stack entry "ud" is
4639 * a class stream, otherwise it throws an error.
4640 */
4641__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004642{
Christopher Fauleta2097962019-07-15 16:25:33 +02004643 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4644}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004645
Christopher Fauleta2097962019-07-15 16:25:33 +02004646/* This function creates and push in the stack a HTTP object
4647 * according with a current TXN.
4648 */
4649static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4650{
4651 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004652
Christopher Fauleta2097962019-07-15 16:25:33 +02004653 /* Check stack size. */
4654 if (!lua_checkstack(L, 3))
4655 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004656
Christopher Fauleta2097962019-07-15 16:25:33 +02004657 /* Create the object: obj[0] = userdata.
4658 * Note that the base of the Converters object is the
4659 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004660 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004661 lua_newtable(L);
4662 htxn = lua_newuserdata(L, sizeof(*htxn));
4663 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004664
4665 htxn->s = txn->s;
4666 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004667 htxn->dir = txn->dir;
4668 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004669
4670 /* Pop a class stream metatable and affect it to the table. */
4671 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4672 lua_setmetatable(L, -2);
4673
4674 return 1;
4675}
4676
4677/* This function creates ans returns an array of HTTP headers.
4678 * This function does not fails. It is used as wrapper with the
4679 * 2 following functions.
4680 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004681__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004682{
Christopher Fauleta2097962019-07-15 16:25:33 +02004683 struct htx *htx;
4684 int32_t pos;
4685
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004686 /* Create the table. */
4687 lua_newtable(L);
4688
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004689
Christopher Fauleta2097962019-07-15 16:25:33 +02004690 htx = htxbuf(&msg->chn->buf);
4691 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4692 struct htx_blk *blk = htx_get_blk(htx, pos);
4693 enum htx_blk_type type = htx_get_blk_type(blk);
4694 struct ist n, v;
4695 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004696
Christopher Fauleta2097962019-07-15 16:25:33 +02004697 if (type == HTX_BLK_HDR) {
4698 n = htx_get_blk_name(htx,blk);
4699 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004700 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004701 else if (type == HTX_BLK_EOH)
4702 break;
4703 else
4704 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004705
Christopher Fauleta2097962019-07-15 16:25:33 +02004706 /* Check for existing entry:
4707 * assume that the table is on the top of the stack, and
4708 * push the key in the stack, the function lua_gettable()
4709 * perform the lookup.
4710 */
4711 lua_pushlstring(L, n.ptr, n.len);
4712 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004713
Christopher Fauleta2097962019-07-15 16:25:33 +02004714 switch (lua_type(L, -1)) {
4715 case LUA_TNIL:
4716 /* Table not found, create it. */
4717 lua_pop(L, 1); /* remove the nil value. */
4718 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4719 lua_newtable(L); /* create and push empty table. */
4720 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4721 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4722 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004723 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004724
Christopher Fauleta2097962019-07-15 16:25:33 +02004725 case LUA_TTABLE:
4726 /* Entry found: push the value in the table. */
4727 len = lua_rawlen(L, -1);
4728 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4729 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4730 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4731 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004732
Christopher Fauleta2097962019-07-15 16:25:33 +02004733 default:
4734 /* Other cases are errors. */
4735 hlua_pusherror(L, "internal error during the parsing of headers.");
4736 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004737 }
4738 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004739 return 1;
4740}
4741
4742__LJMP static int hlua_http_req_get_headers(lua_State *L)
4743{
4744 struct hlua_txn *htxn;
4745
4746 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4747 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4748
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004749 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004750 WILL_LJMP(lua_error(L));
4751
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004752 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004753}
4754
4755__LJMP static int hlua_http_res_get_headers(lua_State *L)
4756{
4757 struct hlua_txn *htxn;
4758
4759 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4760 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4761
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004762 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004763 WILL_LJMP(lua_error(L));
4764
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004765 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004766}
4767
4768/* This function replace full header, or just a value in
4769 * the request or in the response. It is a wrapper fir the
4770 * 4 following functions.
4771 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004772__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004773{
4774 size_t name_len;
4775 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4776 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4777 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004778 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004779 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004780
Dragan Dosen26743032019-04-30 15:54:36 +02004781 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004782 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4783
Christopher Fauleta2097962019-07-15 16:25:33 +02004784 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004785 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004786 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004787 return 0;
4788}
4789
4790__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4791{
4792 struct hlua_txn *htxn;
4793
4794 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4795 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4796
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004797 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004798 WILL_LJMP(lua_error(L));
4799
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004800 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004801}
4802
4803__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4804{
4805 struct hlua_txn *htxn;
4806
4807 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4808 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4809
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004810 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004811 WILL_LJMP(lua_error(L));
4812
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004813 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004814}
4815
4816__LJMP static int hlua_http_req_rep_val(lua_State *L)
4817{
4818 struct hlua_txn *htxn;
4819
4820 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4821 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4822
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004823 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004824 WILL_LJMP(lua_error(L));
4825
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004826 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004827}
4828
4829__LJMP static int hlua_http_res_rep_val(lua_State *L)
4830{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004831 struct hlua_txn *htxn;
4832
4833 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4834 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4835
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004836 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004837 WILL_LJMP(lua_error(L));
4838
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004839 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004840}
4841
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004842/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004843 * It is a wrapper for the 2 following functions.
4844 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004845__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004846{
4847 size_t len;
4848 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004849 struct htx *htx = htxbuf(&msg->chn->buf);
4850 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004851
Christopher Fauleta2097962019-07-15 16:25:33 +02004852 ctx.blk = NULL;
4853 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4854 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004855 return 0;
4856}
4857
4858__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4859{
4860 struct hlua_txn *htxn;
4861
4862 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4863 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4864
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004865 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004866 WILL_LJMP(lua_error(L));
4867
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004868 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004869}
4870
4871__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4872{
4873 struct hlua_txn *htxn;
4874
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004875 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004876 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4877
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004878 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004879 WILL_LJMP(lua_error(L));
4880
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004881 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004882}
4883
4884/* This function adds an header. It is a wrapper used by
4885 * the 2 following functions.
4886 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004887__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004888{
4889 size_t name_len;
4890 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4891 size_t value_len;
4892 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004893 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004894
Christopher Fauleta2097962019-07-15 16:25:33 +02004895 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4896 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004897 return 0;
4898}
4899
4900__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4901{
4902 struct hlua_txn *htxn;
4903
4904 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4905 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4906
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004907 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004908 WILL_LJMP(lua_error(L));
4909
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004910 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004911}
4912
4913__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4914{
4915 struct hlua_txn *htxn;
4916
4917 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4918 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4919
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004920 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004921 WILL_LJMP(lua_error(L));
4922
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004923 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004924}
4925
4926static int hlua_http_req_set_hdr(lua_State *L)
4927{
4928 struct hlua_txn *htxn;
4929
4930 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4931 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4932
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004933 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004934 WILL_LJMP(lua_error(L));
4935
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004936 hlua_http_del_hdr(L, &htxn->s->txn->req);
4937 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004938}
4939
4940static int hlua_http_res_set_hdr(lua_State *L)
4941{
4942 struct hlua_txn *htxn;
4943
4944 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4945 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4946
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004947 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004948 WILL_LJMP(lua_error(L));
4949
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004950 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
4951 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004952}
4953
4954/* This function set the method. */
4955static int hlua_http_req_set_meth(lua_State *L)
4956{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004957 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004958 size_t name_len;
4959 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004960
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004961 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004962 WILL_LJMP(lua_error(L));
4963
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004964 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004965 return 1;
4966}
4967
4968/* This function set the method. */
4969static int hlua_http_req_set_path(lua_State *L)
4970{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004971 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004972 size_t name_len;
4973 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004974
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004975 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004976 WILL_LJMP(lua_error(L));
4977
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004978 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004979 return 1;
4980}
4981
4982/* This function set the query-string. */
4983static int hlua_http_req_set_query(lua_State *L)
4984{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004985 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004986 size_t name_len;
4987 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004988
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004989 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004990 WILL_LJMP(lua_error(L));
4991
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004992 /* Check length. */
4993 if (name_len > trash.size - 1) {
4994 lua_pushboolean(L, 0);
4995 return 1;
4996 }
4997
4998 /* Add the mark question as prefix. */
4999 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005000 trash.area[trash.data++] = '?';
5001 memcpy(trash.area + trash.data, name, name_len);
5002 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005003
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005004 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005005 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005006 return 1;
5007}
5008
5009/* This function set the uri. */
5010static int hlua_http_req_set_uri(lua_State *L)
5011{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005012 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005013 size_t name_len;
5014 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005015
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005016 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005017 WILL_LJMP(lua_error(L));
5018
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005019 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005020 return 1;
5021}
5022
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005023/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005024static int hlua_http_res_set_status(lua_State *L)
5025{
5026 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5027 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005028 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5029 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005030
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005031 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005032 WILL_LJMP(lua_error(L));
5033
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005034 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005035 return 0;
5036}
5037
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005038/*
5039 *
5040 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005041 * Class TXN
5042 *
5043 *
5044 */
5045
5046/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005047 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005048 */
5049__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5050{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005051 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005052}
5053
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005054__LJMP static int hlua_set_var(lua_State *L)
5055{
5056 struct hlua_txn *htxn;
5057 const char *name;
5058 size_t len;
5059 struct sample smp;
5060
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005061 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5062 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005063
5064 /* It is useles to retrieve the stream, but this function
5065 * runs only in a stream context.
5066 */
5067 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5068 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5069
5070 /* Converts the third argument in a sample. */
5071 hlua_lua2smp(L, 3, &smp);
5072
5073 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005074 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005075
5076 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5077 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5078 else
5079 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5080
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005081 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005082}
5083
Christopher Faulet85d79c92016-11-09 16:54:56 +01005084__LJMP static int hlua_unset_var(lua_State *L)
5085{
5086 struct hlua_txn *htxn;
5087 const char *name;
5088 size_t len;
5089 struct sample smp;
5090
5091 MAY_LJMP(check_args(L, 2, "unset_var"));
5092
5093 /* It is useles to retrieve the stream, but this function
5094 * runs only in a stream context.
5095 */
5096 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5097 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5098
5099 /* Unset the variable. */
5100 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005101 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5102 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005103}
5104
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005105__LJMP static int hlua_get_var(lua_State *L)
5106{
5107 struct hlua_txn *htxn;
5108 const char *name;
5109 size_t len;
5110 struct sample smp;
5111
5112 MAY_LJMP(check_args(L, 2, "get_var"));
5113
5114 /* It is useles to retrieve the stream, but this function
5115 * runs only in a stream context.
5116 */
5117 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5118 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5119
Willy Tarreau7560dd42016-03-10 16:28:58 +01005120 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005121 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005122 lua_pushnil(L);
5123 return 1;
5124 }
5125
5126 return hlua_smp2lua(L, &smp);
5127}
5128
Willy Tarreau59551662015-03-10 14:23:13 +01005129__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005130{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005131 struct hlua *hlua;
5132
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005133 MAY_LJMP(check_args(L, 2, "set_priv"));
5134
Willy Tarreau87b09662015-04-03 00:22:06 +02005135 /* It is useles to retrieve the stream, but this function
5136 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005137 */
5138 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005139 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005140
5141 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005142 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005143
5144 /* Get and store new value. */
5145 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5146 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5147
5148 return 0;
5149}
5150
Willy Tarreau59551662015-03-10 14:23:13 +01005151__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005152{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005153 struct hlua *hlua;
5154
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005155 MAY_LJMP(check_args(L, 1, "get_priv"));
5156
Willy Tarreau87b09662015-04-03 00:22:06 +02005157 /* It is useles to retrieve the stream, but this function
5158 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005159 */
5160 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005161 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005162
5163 /* Push configuration index in the stack. */
5164 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5165
5166 return 1;
5167}
5168
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005169/* Create stack entry containing a class TXN. This function
5170 * return 0 if the stack does not contains free slots,
5171 * otherwise it returns 1.
5172 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005173static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005174{
Willy Tarreaude491382015-04-06 11:04:28 +02005175 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005176
5177 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005178 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005179 return 0;
5180
5181 /* NOTE: The allocation never fails. The failure
5182 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005183 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005184 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005185 /* Create the object: obj[0] = userdata. */
5186 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005187 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005188 lua_rawseti(L, -2, 0);
5189
Willy Tarreaude491382015-04-06 11:04:28 +02005190 htxn->s = s;
5191 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005192 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005193 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005194
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005195 /* Create the "f" field that contains a list of fetches. */
5196 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005197 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005198 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005199 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005200
5201 /* Create the "sf" field that contains a list of stringsafe fetches. */
5202 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005203 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005204 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005205 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005206
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005207 /* Create the "c" field that contains a list of converters. */
5208 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005209 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005210 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005211 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005212
5213 /* Create the "sc" field that contains a list of stringsafe converters. */
5214 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005215 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005216 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005217 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005218
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005219 /* Create the "req" field that contains the request channel object. */
5220 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005221 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005222 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005223 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005224
5225 /* Create the "res" field that contains the response channel object. */
5226 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005227 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005228 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005229 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005230
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005231 /* Creates the HTTP object is the current proxy allows http. */
5232 lua_pushstring(L, "http");
5233 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005234 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005235 return 0;
5236 }
5237 else
5238 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005239 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005240
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005241 /* Pop a class sesison metatable and affect it to the userdata. */
5242 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5243 lua_setmetatable(L, -2);
5244
5245 return 1;
5246}
5247
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005248__LJMP static int hlua_txn_deflog(lua_State *L)
5249{
5250 const char *msg;
5251 struct hlua_txn *htxn;
5252
5253 MAY_LJMP(check_args(L, 2, "deflog"));
5254 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5255 msg = MAY_LJMP(luaL_checkstring(L, 2));
5256
5257 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5258 return 0;
5259}
5260
5261__LJMP static int hlua_txn_log(lua_State *L)
5262{
5263 int level;
5264 const char *msg;
5265 struct hlua_txn *htxn;
5266
5267 MAY_LJMP(check_args(L, 3, "log"));
5268 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5269 level = MAY_LJMP(luaL_checkinteger(L, 2));
5270 msg = MAY_LJMP(luaL_checkstring(L, 3));
5271
5272 if (level < 0 || level >= NB_LOG_LEVELS)
5273 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5274
5275 hlua_sendlog(htxn->s->be, level, msg);
5276 return 0;
5277}
5278
5279__LJMP static int hlua_txn_log_debug(lua_State *L)
5280{
5281 const char *msg;
5282 struct hlua_txn *htxn;
5283
5284 MAY_LJMP(check_args(L, 2, "Debug"));
5285 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5286 msg = MAY_LJMP(luaL_checkstring(L, 2));
5287 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5288 return 0;
5289}
5290
5291__LJMP static int hlua_txn_log_info(lua_State *L)
5292{
5293 const char *msg;
5294 struct hlua_txn *htxn;
5295
5296 MAY_LJMP(check_args(L, 2, "Info"));
5297 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5298 msg = MAY_LJMP(luaL_checkstring(L, 2));
5299 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5300 return 0;
5301}
5302
5303__LJMP static int hlua_txn_log_warning(lua_State *L)
5304{
5305 const char *msg;
5306 struct hlua_txn *htxn;
5307
5308 MAY_LJMP(check_args(L, 2, "Warning"));
5309 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5310 msg = MAY_LJMP(luaL_checkstring(L, 2));
5311 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5312 return 0;
5313}
5314
5315__LJMP static int hlua_txn_log_alert(lua_State *L)
5316{
5317 const char *msg;
5318 struct hlua_txn *htxn;
5319
5320 MAY_LJMP(check_args(L, 2, "Alert"));
5321 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5322 msg = MAY_LJMP(luaL_checkstring(L, 2));
5323 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5324 return 0;
5325}
5326
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005327__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5328{
5329 struct hlua_txn *htxn;
5330 int ll;
5331
5332 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5333 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5334 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5335
5336 if (ll < 0 || ll > 7)
5337 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5338
5339 htxn->s->logs.level = ll;
5340 return 0;
5341}
5342
5343__LJMP static int hlua_txn_set_tos(lua_State *L)
5344{
5345 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005346 int tos;
5347
5348 MAY_LJMP(check_args(L, 2, "set_tos"));
5349 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5350 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5351
Willy Tarreau1a18b542018-12-11 16:37:42 +01005352 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005353 return 0;
5354}
5355
5356__LJMP static int hlua_txn_set_mark(lua_State *L)
5357{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005358 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005359 int mark;
5360
5361 MAY_LJMP(check_args(L, 2, "set_mark"));
5362 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5363 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5364
Lukas Tribus579e3e32019-08-11 18:03:45 +02005365 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005366 return 0;
5367}
5368
Patrick Hemmer268a7072018-05-11 12:52:31 -04005369__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5370{
5371 struct hlua_txn *htxn;
5372
5373 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5374 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5375 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5376 return 0;
5377}
5378
5379__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5380{
5381 struct hlua_txn *htxn;
5382
5383 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5384 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5385 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5386 return 0;
5387}
5388
Christopher Faulet700d9e82020-01-31 12:21:52 +01005389/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005390 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005391 * message and terminate the transaction. It returns 1 on success and 0 on
5392 * error. The Reply must be on top of the stack.
5393 */
5394__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5395{
5396 struct htx *htx;
5397 struct htx_sl *sl;
5398 struct h1m h1m;
5399 const char *status, *reason, *body;
5400 size_t status_len, reason_len, body_len;
5401 int ret, code, flags;
5402
5403 code = 200;
5404 status = "200";
5405 status_len = 3;
5406 ret = lua_getfield(L, -1, "status");
5407 if (ret == LUA_TNUMBER) {
5408 code = lua_tointeger(L, -1);
5409 status = lua_tolstring(L, -1, &status_len);
5410 }
5411 lua_pop(L, 1);
5412
5413 reason = http_get_reason(code);
5414 reason_len = strlen(reason);
5415 ret = lua_getfield(L, -1, "reason");
5416 if (ret == LUA_TSTRING)
5417 reason = lua_tolstring(L, -1, &reason_len);
5418 lua_pop(L, 1);
5419
5420 body = NULL;
5421 body_len = 0;
5422 ret = lua_getfield(L, -1, "body");
5423 if (ret == LUA_TSTRING)
5424 body = lua_tolstring(L, -1, &body_len);
5425 lua_pop(L, 1);
5426
5427 /* Prepare the response before inserting the headers */
5428 h1m_init_res(&h1m);
5429 htx = htx_from_buf(&s->res.buf);
5430 channel_htx_truncate(&s->res, htx);
5431 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5432 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5433 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5434 ist2(status, status_len), ist2(reason, reason_len));
5435 }
5436 else {
5437 flags = HTX_SL_F_IS_RESP;
5438 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5439 ist2(status, status_len), ist2(reason, reason_len));
5440 }
5441 if (!sl)
5442 goto fail;
5443 sl->info.res.status = code;
5444
5445 /* Push in the stack the "headers" entry. */
5446 ret = lua_getfield(L, -1, "headers");
5447 if (ret != LUA_TTABLE)
5448 goto skip_headers;
5449
5450 lua_pushnil(L);
5451 while (lua_next(L, -2) != 0) {
5452 struct ist name, value;
5453 const char *n, *v;
5454 size_t nlen, vlen;
5455
5456 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5457 /* Skip element if the key is not a string or if the value is not a table */
5458 goto next_hdr;
5459 }
5460
5461 n = lua_tolstring(L, -2, &nlen);
5462 name = ist2(n, nlen);
5463 if (isteqi(name, ist("content-length"))) {
5464 /* Always skip content-length header. It will be added
5465 * later with the correct len
5466 */
5467 goto next_hdr;
5468 }
5469
5470 /* Loop on header's values */
5471 lua_pushnil(L);
5472 while (lua_next(L, -2)) {
5473 if (!lua_isstring(L, -1)) {
5474 /* Skip the value if it is not a string */
5475 goto next_value;
5476 }
5477
5478 v = lua_tolstring(L, -1, &vlen);
5479 value = ist2(v, vlen);
5480
5481 if (isteqi(name, ist("transfer-encoding")))
5482 h1_parse_xfer_enc_header(&h1m, value);
5483 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5484 goto fail;
5485
5486 next_value:
5487 lua_pop(L, 1);
5488 }
5489
5490 next_hdr:
5491 lua_pop(L, 1);
5492 }
5493 skip_headers:
5494 lua_pop(L, 1);
5495
5496 /* Update h1m flags: CLEN is set if CHNK is not present */
5497 if (!(h1m.flags & H1_MF_CHNK)) {
5498 const char *clen = ultoa(body_len);
5499
5500 h1m.flags |= H1_MF_CLEN;
5501 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5502 goto fail;
5503 }
5504 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5505 h1m.flags |= H1_MF_XFER_LEN;
5506
5507 /* Update HTX start-line flags */
5508 if (h1m.flags & H1_MF_XFER_ENC)
5509 flags |= HTX_SL_F_XFER_ENC;
5510 if (h1m.flags & H1_MF_XFER_LEN) {
5511 flags |= HTX_SL_F_XFER_LEN;
5512 if (h1m.flags & H1_MF_CHNK)
5513 flags |= HTX_SL_F_CHNK;
5514 else if (h1m.flags & H1_MF_CLEN)
5515 flags |= HTX_SL_F_CLEN;
5516 if (h1m.body_len == 0)
5517 flags |= HTX_SL_F_BODYLESS;
5518 }
5519 sl->flags |= flags;
5520
5521
5522 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5523 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5524 !htx_add_endof(htx, HTX_BLK_EOM))
5525 goto fail;
5526
5527 /* Now, forward the response and terminate the transaction */
5528 s->txn->status = code;
5529 htx_to_buf(htx, &s->res.buf);
5530 if (!http_forward_proxy_resp(s, 1))
5531 goto fail;
5532
5533 return 1;
5534
5535 fail:
5536 channel_htx_truncate(&s->res, htx);
5537 return 0;
5538}
5539
5540/* Terminate a transaction if called from a lua action. For TCP streams,
5541 * processing is just aborted. Nothing is returned to the client and all
5542 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5543 * is forwarded to the client before terminating the transaction. On success,
5544 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5545 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5546 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005547 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005548__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005549{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005550 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005551 struct stream *s;
5552 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005553
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005554 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005555
Christopher Faulet700d9e82020-01-31 12:21:52 +01005556 /* If the flags NOTERM is set, we cannot terminate the session, so we
5557 * just end the execution of the current lua code. */
5558 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005559 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005560
Christopher Faulet700d9e82020-01-31 12:21:52 +01005561 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005562 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005563 struct channel *req = &s->req;
5564 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005565
Christopher Faulet700d9e82020-01-31 12:21:52 +01005566 channel_auto_read(req);
5567 channel_abort(req);
5568 channel_auto_close(req);
5569 channel_erase(req);
5570
5571 res->wex = tick_add_ifset(now_ms, res->wto);
5572 channel_auto_read(res);
5573 channel_auto_close(res);
5574 channel_shutr_now(res);
5575
5576 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5577 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005578 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005579
Christopher Faulet700d9e82020-01-31 12:21:52 +01005580 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5581 /* No reply or invalid reply */
5582 s->txn->status = 0;
5583 http_reply_and_close(s, 0, NULL);
5584 }
5585 else {
5586 /* Remove extra args to have the reply on top of the stack */
5587 if (lua_gettop(L) > 2)
5588 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005589
Christopher Faulet700d9e82020-01-31 12:21:52 +01005590 if (!hlua_txn_forward_reply(L, s)) {
5591 if (!(s->flags & SF_ERR_MASK))
5592 s->flags |= SF_ERR_PRXCOND;
5593 lua_pushinteger(L, ACT_RET_ERR);
5594 WILL_LJMP(hlua_done(L));
5595 return 0; /* Never reached */
5596 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005597 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005598
Christopher Faulet700d9e82020-01-31 12:21:52 +01005599 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5600 if (htxn->dir == SMP_OPT_DIR_REQ) {
5601 /* let's log the request time */
5602 s->logs.tv_request = now;
5603 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5604 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5605 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005606
Christopher Faulet700d9e82020-01-31 12:21:52 +01005607 done:
5608 if (!(s->flags & SF_ERR_MASK))
5609 s->flags |= SF_ERR_LOCAL;
5610 if (!(s->flags & SF_FINST_MASK))
5611 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005612
Christopher Faulet4ad73102020-03-05 11:07:31 +01005613 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005614 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005615 return 0;
5616}
5617
Christopher Faulet700d9e82020-01-31 12:21:52 +01005618/*
5619 *
5620 *
5621 * Class REPLY
5622 *
5623 *
5624 */
5625
5626/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5627 * free slots, the function fails and returns 0;
5628 */
5629static int hlua_txn_reply_new(lua_State *L)
5630{
5631 struct hlua_txn *htxn;
5632 const char *reason, *body = NULL;
5633 int ret, status;
5634
5635 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005636 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005637 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5638 WILL_LJMP(lua_error(L));
5639 }
5640
5641 /* Default value */
5642 status = 200;
5643 reason = http_get_reason(status);
5644
5645 if (lua_istable(L, 2)) {
5646 /* load status and reason from the table argument at index 2 */
5647 ret = lua_getfield(L, 2, "status");
5648 if (ret == LUA_TNIL)
5649 goto reason;
5650 else if (ret != LUA_TNUMBER) {
5651 /* invalid status: ignore the reason */
5652 goto body;
5653 }
5654 status = lua_tointeger(L, -1);
5655
5656 reason:
5657 lua_pop(L, 1); /* restore the stack: remove status */
5658 ret = lua_getfield(L, 2, "reason");
5659 if (ret == LUA_TSTRING)
5660 reason = lua_tostring(L, -1);
5661
5662 body:
5663 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5664 ret = lua_getfield(L, 2, "body");
5665 if (ret == LUA_TSTRING)
5666 body = lua_tostring(L, -1);
5667 lua_pop(L, 1); /* restore the stack: remove body */
5668 }
5669
5670 /* Create the Reply table */
5671 lua_newtable(L);
5672
5673 /* Add status element */
5674 lua_pushstring(L, "status");
5675 lua_pushinteger(L, status);
5676 lua_settable(L, -3);
5677
5678 /* Add reason element */
5679 reason = http_get_reason(status);
5680 lua_pushstring(L, "reason");
5681 lua_pushstring(L, reason);
5682 lua_settable(L, -3);
5683
5684 /* Add body element, nil if undefined */
5685 lua_pushstring(L, "body");
5686 if (body)
5687 lua_pushstring(L, body);
5688 else
5689 lua_pushnil(L);
5690 lua_settable(L, -3);
5691
5692 /* Add headers element */
5693 lua_pushstring(L, "headers");
5694 lua_newtable(L);
5695
5696 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5697 if (lua_istable(L, 2)) {
5698 /* load headers from the table argument at index 2. If it is a table, copy it. */
5699 ret = lua_getfield(L, 2, "headers");
5700 if (ret == LUA_TTABLE) {
5701 /* stack: [ ... <headers:table>, <table> ] */
5702 lua_pushnil(L);
5703 while (lua_next(L, -2) != 0) {
5704 /* stack: [ ... <headers:table>, <table>, k, v] */
5705 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5706 /* invalid value type, skip it */
5707 lua_pop(L, 1);
5708 continue;
5709 }
5710
5711
5712 /* Duplicate the key and swap it with the value. */
5713 lua_pushvalue(L, -2);
5714 lua_insert(L, -2);
5715 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5716
5717 lua_newtable(L);
5718 lua_insert(L, -2);
5719 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5720
5721 if (lua_isstring(L, -1)) {
5722 /* push the value in the inner table */
5723 lua_rawseti(L, -2, 1);
5724 }
5725 else { /* table */
5726 lua_pushnil(L);
5727 while (lua_next(L, -2) != 0) {
5728 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5729 if (!lua_isstring(L, -1)) {
5730 /* invalid value type, skip it*/
5731 lua_pop(L, 1);
5732 continue;
5733 }
5734 /* push the value in the inner table */
5735 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5736 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5737 }
5738 lua_pop(L, 1);
5739 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5740 }
5741
5742 /* push (k,v) on the stack in the headers table:
5743 * stack: [ ... <headers:table>, <table>, k, k, v ]
5744 */
5745 lua_settable(L, -5);
5746 /* stack: [ ... <headers:table>, <table>, k ] */
5747 }
5748 }
5749 lua_pop(L, 1);
5750 }
5751 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5752 lua_settable(L, -3);
5753 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5754
5755 /* Pop a class sesison metatable and affect it to the userdata. */
5756 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5757 lua_setmetatable(L, -2);
5758 return 1;
5759}
5760
5761/* Set the reply status code, and optionally the reason. If no reason is
5762 * provided, the default one corresponding to the status code is used.
5763 */
5764__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5765{
5766 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5767 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5768
5769 /* First argument (self) must be a table */
5770 luaL_checktype(L, 1, LUA_TTABLE);
5771
5772 if (status < 100 || status > 599) {
5773 lua_pushboolean(L, 0);
5774 return 1;
5775 }
5776 if (!reason)
5777 reason = http_get_reason(status);
5778
5779 lua_pushinteger(L, status);
5780 lua_setfield(L, 1, "status");
5781
5782 lua_pushstring(L, reason);
5783 lua_setfield(L, 1, "reason");
5784
5785 lua_pushboolean(L, 1);
5786 return 1;
5787}
5788
5789/* Add a header into the reply object. Each header name is associated to an
5790 * array of values in the "headers" table. If the header name is not found, a
5791 * new entry is created.
5792 */
5793__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5794{
5795 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5796 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5797 int ret;
5798
5799 /* First argument (self) must be a table */
5800 luaL_checktype(L, 1, LUA_TTABLE);
5801
5802 /* Push in the stack the "headers" entry. */
5803 ret = lua_getfield(L, 1, "headers");
5804 if (ret != LUA_TTABLE) {
5805 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5806 WILL_LJMP(lua_error(L));
5807 }
5808
5809 /* check if the header is already registered. If not, register it. */
5810 ret = lua_getfield(L, -1, name);
5811 if (ret == LUA_TNIL) {
5812 /* Entry not found. */
5813 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5814
5815 /* Insert the new header name in the array in the top of the stack.
5816 * It left the new array in the top of the stack.
5817 */
5818 lua_newtable(L);
5819 lua_pushstring(L, name);
5820 lua_pushvalue(L, -2);
5821 lua_settable(L, -4);
5822 }
5823 else if (ret != LUA_TTABLE) {
5824 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5825 WILL_LJMP(lua_error(L));
5826 }
5827
5828 /* Now the top od thestack is an array of values. We push
5829 * the header value as new entry.
5830 */
5831 lua_pushstring(L, value);
5832 ret = lua_rawlen(L, -2);
5833 lua_rawseti(L, -2, ret + 1);
5834
5835 lua_pushboolean(L, 1);
5836 return 1;
5837}
5838
5839/* Remove all occurrences of a given header name. */
5840__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5841{
5842 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5843 int ret;
5844
5845 /* First argument (self) must be a table */
5846 luaL_checktype(L, 1, LUA_TTABLE);
5847
5848 /* Push in the stack the "headers" entry. */
5849 ret = lua_getfield(L, 1, "headers");
5850 if (ret != LUA_TTABLE) {
5851 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5852 WILL_LJMP(lua_error(L));
5853 }
5854
5855 lua_pushstring(L, name);
5856 lua_pushnil(L);
5857 lua_settable(L, -3);
5858
5859 lua_pushboolean(L, 1);
5860 return 1;
5861}
5862
5863/* Set the reply's body. Overwrite any existing entry. */
5864__LJMP static int hlua_txn_reply_set_body(lua_State *L)
5865{
5866 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
5867
5868 /* First argument (self) must be a table */
5869 luaL_checktype(L, 1, LUA_TTABLE);
5870
5871 lua_pushstring(L, payload);
5872 lua_setfield(L, 1, "body");
5873
5874 lua_pushboolean(L, 1);
5875 return 1;
5876}
5877
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005878__LJMP static int hlua_log(lua_State *L)
5879{
5880 int level;
5881 const char *msg;
5882
5883 MAY_LJMP(check_args(L, 2, "log"));
5884 level = MAY_LJMP(luaL_checkinteger(L, 1));
5885 msg = MAY_LJMP(luaL_checkstring(L, 2));
5886
5887 if (level < 0 || level >= NB_LOG_LEVELS)
5888 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5889
5890 hlua_sendlog(NULL, level, msg);
5891 return 0;
5892}
5893
5894__LJMP static int hlua_log_debug(lua_State *L)
5895{
5896 const char *msg;
5897
5898 MAY_LJMP(check_args(L, 1, "debug"));
5899 msg = MAY_LJMP(luaL_checkstring(L, 1));
5900 hlua_sendlog(NULL, LOG_DEBUG, msg);
5901 return 0;
5902}
5903
5904__LJMP static int hlua_log_info(lua_State *L)
5905{
5906 const char *msg;
5907
5908 MAY_LJMP(check_args(L, 1, "info"));
5909 msg = MAY_LJMP(luaL_checkstring(L, 1));
5910 hlua_sendlog(NULL, LOG_INFO, msg);
5911 return 0;
5912}
5913
5914__LJMP static int hlua_log_warning(lua_State *L)
5915{
5916 const char *msg;
5917
5918 MAY_LJMP(check_args(L, 1, "warning"));
5919 msg = MAY_LJMP(luaL_checkstring(L, 1));
5920 hlua_sendlog(NULL, LOG_WARNING, msg);
5921 return 0;
5922}
5923
5924__LJMP static int hlua_log_alert(lua_State *L)
5925{
5926 const char *msg;
5927
5928 MAY_LJMP(check_args(L, 1, "alert"));
5929 msg = MAY_LJMP(luaL_checkstring(L, 1));
5930 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005931 return 0;
5932}
5933
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005934__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005935{
5936 int wakeup_ms = lua_tointeger(L, -1);
5937 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005938 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005939 return 0;
5940}
5941
5942__LJMP static int hlua_sleep(lua_State *L)
5943{
5944 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005945 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005946
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005947 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005948
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005949 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005950 wakeup_ms = tick_add(now_ms, delay);
5951 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005952
Willy Tarreau9635e032018-10-16 17:52:55 +02005953 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005954 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005955}
5956
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005957__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005958{
5959 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005960 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005961
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005962 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005963
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005964 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005965 wakeup_ms = tick_add(now_ms, delay);
5966 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005967
Willy Tarreau9635e032018-10-16 17:52:55 +02005968 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005969 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005970}
5971
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005972/* This functionis an LUA binding. it permits to give back
5973 * the hand at the HAProxy scheduler. It is used when the
5974 * LUA processing consumes a lot of time.
5975 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005976__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005977{
5978 return 0;
5979}
5980
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005981__LJMP static int hlua_yield(lua_State *L)
5982{
Willy Tarreau9635e032018-10-16 17:52:55 +02005983 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005984 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005985}
5986
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005987/* This function change the nice of the currently executed
5988 * task. It is used set low or high priority at the current
5989 * task.
5990 */
Willy Tarreau59551662015-03-10 14:23:13 +01005991__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005992{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005993 struct hlua *hlua;
5994 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005995
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005996 MAY_LJMP(check_args(L, 1, "set_nice"));
5997 hlua = hlua_gethlua(L);
5998 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005999
6000 /* If he task is not set, I'm in a start mode. */
6001 if (!hlua || !hlua->task)
6002 return 0;
6003
6004 if (nice < -1024)
6005 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006006 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006007 nice = 1024;
6008
6009 hlua->task->nice = nice;
6010 return 0;
6011}
6012
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006013/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006014 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6015 * return an E_AGAIN signal, the emmiter of this signal must set a
6016 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006017 *
6018 * Task wrapper are longjmp safe because the only one Lua code
6019 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006020 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006021struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006022{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006023 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006024 enum hlua_exec status;
6025
Christopher Faulet5bc99722018-04-25 10:34:45 +02006026 if (task->thread_mask == MAX_THREADS_MASK)
6027 task_set_affinity(task, tid_bit);
6028
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006029 /* If it is the first call to the task, we must initialize the
6030 * execution timeouts.
6031 */
6032 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006033 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006034
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006035 /* Execute the Lua code. */
6036 status = hlua_ctx_resume(hlua, 1);
6037
6038 switch (status) {
6039 /* finished or yield */
6040 case HLUA_E_OK:
6041 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006042 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006043 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006044 break;
6045
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006046 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006047 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006048 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006049 break;
6050
6051 /* finished with error. */
6052 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006053 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006054 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006055 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006056 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006057 break;
6058
6059 case HLUA_E_ERR:
6060 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006061 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006062 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006063 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006064 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006065 break;
6066 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006067 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006068}
6069
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006070/* This function is an LUA binding that register LUA function to be
6071 * executed after the HAProxy configuration parsing and before the
6072 * HAProxy scheduler starts. This function expect only one LUA
6073 * argument that is a function. This function returns nothing, but
6074 * throws if an error is encountered.
6075 */
6076__LJMP static int hlua_register_init(lua_State *L)
6077{
6078 struct hlua_init_function *init;
6079 int ref;
6080
6081 MAY_LJMP(check_args(L, 1, "register_init"));
6082
6083 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6084
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006085 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006086 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006087 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006088
6089 init->function_ref = ref;
6090 LIST_ADDQ(&hlua_init_functions, &init->l);
6091 return 0;
6092}
6093
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006094/* This functio is an LUA binding. It permits to register a task
6095 * executed in parallel of the main HAroxy activity. The task is
6096 * created and it is set in the HAProxy scheduler. It can be called
6097 * from the "init" section, "post init" or during the runtime.
6098 *
6099 * Lua prototype:
6100 *
6101 * <none> core.register_task(<function>)
6102 */
6103static int hlua_register_task(lua_State *L)
6104{
6105 struct hlua *hlua;
6106 struct task *task;
6107 int ref;
6108
6109 MAY_LJMP(check_args(L, 1, "register_task"));
6110
6111 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6112
Willy Tarreaubafbe012017-11-24 17:34:44 +01006113 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006114 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006115 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006116
Emeric Brunc60def82017-09-27 14:59:38 +02006117 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006118 if (!task)
6119 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6120
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006121 task->context = hlua;
6122 task->process = hlua_process_task;
6123
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006124 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006125 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006126
6127 /* Restore the function in the stack. */
6128 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6129 hlua->nargs = 0;
6130
6131 /* Schedule task. */
6132 task_schedule(task, now_ms);
6133
6134 return 0;
6135}
6136
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006137/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6138 * doesn't allow "yield" functions because the HAProxy engine cannot
6139 * resume converters.
6140 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006141static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006142{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006143 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006144 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006145 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006146
Willy Tarreaube508f12016-03-10 11:47:01 +01006147 if (!stream)
6148 return 0;
6149
Willy Tarreau87b09662015-04-03 00:22:06 +02006150 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006151 * Lua context can be not initialized. This behavior
6152 * permits to save performances because a systematic
6153 * Lua initialization cause 5% performances loss.
6154 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006155 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006156 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006157 if (!stream->hlua) {
6158 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6159 return 0;
6160 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006161 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006162 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6163 return 0;
6164 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006165 }
6166
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006167 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006168 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006169
6170 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006171 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6172 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6173 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006174 else
6175 error = "critical error";
6176 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006177 return 0;
6178 }
6179
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006180 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006181 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006182 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006183 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006184 return 0;
6185 }
6186
6187 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006188 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006189
6190 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006191 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006192 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006193 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006194 return 0;
6195 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006196 hlua_smp2lua(stream->hlua->T, smp);
6197 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006198
6199 /* push keywords in the stack. */
6200 if (arg_p) {
6201 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006202 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006203 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006204 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006205 return 0;
6206 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006207 hlua_arg2lua(stream->hlua->T, arg_p);
6208 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006209 }
6210 }
6211
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006212 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006213 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006214
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006215 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006216 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006217 }
6218
6219 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006220 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006221 /* finished. */
6222 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006223 /* If the stack is empty, the function fails. */
6224 if (lua_gettop(stream->hlua->T) <= 0)
6225 return 0;
6226
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006227 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006228 hlua_lua2smp(stream->hlua->T, -1, smp);
6229 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006230 return 1;
6231
6232 /* yield. */
6233 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006234 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006235 return 0;
6236
6237 /* finished with error. */
6238 case HLUA_E_ERRMSG:
6239 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006240 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006241 fcn->name, lua_tostring(stream->hlua->T, -1));
6242 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006243 return 0;
6244
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006245 case HLUA_E_ETMOUT:
6246 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6247 return 0;
6248
6249 case HLUA_E_NOMEM:
6250 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6251 return 0;
6252
6253 case HLUA_E_YIELD:
6254 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6255 return 0;
6256
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006257 case HLUA_E_ERR:
6258 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006259 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006260
6261 default:
6262 return 0;
6263 }
6264}
6265
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006266/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6267 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006268 * resume sample-fetches. This function will be called by the sample
6269 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006270 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006271static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6272 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006273{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006274 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006275 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006276 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006277 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006278
Willy Tarreaube508f12016-03-10 11:47:01 +01006279 if (!stream)
6280 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006281
Willy Tarreau87b09662015-04-03 00:22:06 +02006282 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006283 * Lua context can be not initialized. This behavior
6284 * permits to save performances because a systematic
6285 * Lua initialization cause 5% performances loss.
6286 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006287 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006288 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006289 if (!stream->hlua) {
6290 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6291 return 0;
6292 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006293 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006294 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6295 return 0;
6296 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006297 }
6298
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006299 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006300 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006301
6302 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006303 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6304 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6305 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006306 else
6307 error = "critical error";
6308 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006309 return 0;
6310 }
6311
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006312 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006313 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006314 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006315 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006316 return 0;
6317 }
6318
6319 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006320 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006321
6322 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006323 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006324 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006325 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006326 return 0;
6327 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006328 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006329
6330 /* push keywords in the stack. */
6331 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6332 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006333 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006334 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006335 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006336 return 0;
6337 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006338 hlua_arg2lua(stream->hlua->T, arg_p);
6339 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006340 }
6341
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006342 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006343 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006344
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006345 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006346 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006347 }
6348
6349 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006350 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006351 /* finished. */
6352 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006353 /* If the stack is empty, the function fails. */
6354 if (lua_gettop(stream->hlua->T) <= 0)
6355 return 0;
6356
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006357 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006358 hlua_lua2smp(stream->hlua->T, -1, smp);
6359 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006360
6361 /* Set the end of execution flag. */
6362 smp->flags &= ~SMP_F_MAY_CHANGE;
6363 return 1;
6364
6365 /* yield. */
6366 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006367 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006368 return 0;
6369
6370 /* finished with error. */
6371 case HLUA_E_ERRMSG:
6372 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006373 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006374 fcn->name, lua_tostring(stream->hlua->T, -1));
6375 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006376 return 0;
6377
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006378 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006379 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6380 return 0;
6381
6382 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006383 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6384 return 0;
6385
6386 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006387 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6388 return 0;
6389
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006390 case HLUA_E_ERR:
6391 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006392 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
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);