blob: afff59a02ea30c0af8ad0a8980e725592efb0edd [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 Tarreau3f0f82e2020-06-04 19:42:41 +020024#include <haproxy/applet.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020025#include <haproxy/api.h>
Willy Tarreau8d2b7772020-05-27 10:58:19 +020026#include <import/ebpttree.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010027
28#include <common/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020029#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020030#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020031#include <haproxy/connection.h>
Willy Tarreau87735332020-06-04 09:08:41 +020032#include <haproxy/http_htx.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020033#include <haproxy/thread.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreau374b4422020-06-02 17:46:16 +020035#include <haproxy/xref.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020036#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020037#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020038#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020039#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020040#include <haproxy/http_fetch.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020041#include <haproxy/http_rules.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020042#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020043#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020044#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020045#include <haproxy/payload.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020046#include <haproxy/sample.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020047#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020048#include <haproxy/stats-t.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020049#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020050#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020051#include <haproxy/tcp_rules.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020052#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020053#include <haproxy/vars.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010054
Thierry FOURNIER380d0932015-01-23 14:27:52 +010055#include <types/proxy.h>
56
Willy Tarreauaa74c4e2020-06-04 10:19:23 +020057#include <haproxy/arg.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040058#include <proto/queue.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010059#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020060#include <proto/stream.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010061
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010062/* Lua uses longjmp to perform yield or throwing errors. This
63 * macro is used only for identifying the function that can
64 * not return because a longjmp is executed.
65 * __LJMP marks a prototype of hlua file that can use longjmp.
66 * WILL_LJMP() marks an lua function that will use longjmp.
67 * MAY_LJMP() marks an lua function that may use longjmp.
68 */
69#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020070#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010071#define MAY_LJMP(func) func
72
Thierry FOURNIERbabae282015-09-17 11:36:37 +020073/* This couple of function executes securely some Lua calls outside of
74 * the lua runtime environment. Each Lua call can return a longjmp
75 * if it encounter a memory error.
76 *
77 * Lua documentation extract:
78 *
79 * If an error happens outside any protected environment, Lua calls
80 * a panic function (see lua_atpanic) and then calls abort, thus
81 * exiting the host application. Your panic function can avoid this
82 * exit by never returning (e.g., doing a long jump to your own
83 * recovery point outside Lua).
84 *
85 * The panic function runs as if it were a message handler (see
86 * §2.3); in particular, the error message is at the top of the
87 * stack. However, there is no guarantee about stack space. To push
88 * anything on the stack, the panic function must first check the
89 * available space (see §4.2).
90 *
91 * We must check all the Lua entry point. This includes:
92 * - The include/proto/hlua.h exported functions
93 * - the task wrapper function
94 * - The action wrapper function
95 * - The converters wrapper function
96 * - The sample-fetch wrapper functions
97 *
98 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080099 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200100 *
101 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
102 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
103 * because they must be exists in the program stack when the longjmp
104 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200105 *
106 * Note that the Lua processing is not really thread safe. It provides
107 * heavy system which consists to add our own lock function in the Lua
108 * code and recompile the library. This system will probably not accepted
109 * by maintainers of various distribs.
110 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500111 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200112 * quick looking on the Lua sources displays a lua_lock() a the start
113 * of function and a lua_unlock() at the end of the function. So I
114 * conclude that the Lua thread safe mode just perform a mutex around
115 * all execution. So I prefer to do this in the HAProxy code, it will be
116 * easier for distro maintainers.
117 *
118 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
119 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
120 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200121 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100122__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200123THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200124static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100125static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200126
127#define SET_SAFE_LJMP(__L) \
128 ({ \
129 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100130 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200131 if (setjmp(safe_ljmp_env) != 0) { \
132 lua_atpanic(__L, hlua_panic_safe); \
133 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100134 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200135 } else { \
136 lua_atpanic(__L, hlua_panic_ljmp); \
137 ret = 1; \
138 } \
139 ret; \
140 })
141
142/* If we are the last function catching Lua errors, we
143 * must reset the panic function.
144 */
145#define RESET_SAFE_LJMP(__L) \
146 do { \
147 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100148 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200149 } while(0)
150
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200151/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200152#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100153/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200154#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200155/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100156#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100157#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200158
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100159/* The main Lua execution context. */
160struct hlua gL;
161
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100162/* This is the memory pool containing struct lua for applets
163 * (including cli).
164 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100165DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100166
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100167/* Used for Socket connection. */
168static struct proxy socket_proxy;
169static struct server socket_tcp;
170#ifdef USE_OPENSSL
171static struct server socket_ssl;
172#endif
173
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100174/* List head of the function called at the initialisation time. */
175struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
176
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100177/* The following variables contains the reference of the different
178 * Lua classes. These references are useful for identify metadata
179 * associated with an object.
180 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100181static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100182static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100183static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100184static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100185static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100186static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200187static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200188static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200189static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100190static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100191
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100192/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200193 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100194 * short timeout. Lua linked with tasks doesn't have a timeout
195 * because a task may remain alive during all the haproxy execution.
196 */
197static unsigned int hlua_timeout_session = 4000; /* session timeout. */
198static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200199static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100200
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100201/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
202 * it is used for preventing infinite loops.
203 *
204 * I test the scheer with an infinite loop containing one incrementation
205 * and one test. I run this loop between 10 seconds, I raise a ceil of
206 * 710M loops from one interrupt each 9000 instructions, so I fix the value
207 * to one interrupt each 10 000 instructions.
208 *
209 * configured | Number of
210 * instructions | loops executed
211 * between two | in milions
212 * forced yields |
213 * ---------------+---------------
214 * 10 | 160
215 * 500 | 670
216 * 1000 | 680
217 * 5000 | 700
218 * 7000 | 700
219 * 8000 | 700
220 * 9000 | 710 <- ceil
221 * 10000 | 710
222 * 100000 | 710
223 * 1000000 | 710
224 *
225 */
226static unsigned int hlua_nb_instruction = 10000;
227
Willy Tarreau32f61e22015-03-18 17:54:59 +0100228/* Descriptor for the memory allocation state. If limit is not null, it will
229 * be enforced on any memory allocation.
230 */
231struct hlua_mem_allocator {
232 size_t allocated;
233 size_t limit;
234};
235
236static struct hlua_mem_allocator hlua_global_allocator;
237
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100238/* These functions converts types between HAProxy internal args or
239 * sample and LUA types. Another function permits to check if the
240 * LUA stack contains arguments according with an required ARG_T
241 * format.
242 */
243static int hlua_arg2lua(lua_State *L, const struct arg *arg);
244static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100245__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100246 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100247static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100248static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100249static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
250
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100251__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200252
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200253#define SEND_ERR(__be, __fmt, __args...) \
254 do { \
255 send_log(__be, LOG_ERR, __fmt, ## __args); \
256 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100257 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200258 } while (0)
259
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100260/* Used to check an Lua function type in the stack. It creates and
261 * returns a reference of the function. This function throws an
262 * error if the rgument is not a "function".
263 */
264__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
265{
266 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100267 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100268 WILL_LJMP(luaL_argerror(L, argno, msg));
269 }
270 lua_pushvalue(L, argno);
271 return luaL_ref(L, LUA_REGISTRYINDEX);
272}
273
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200274/* Return the string that is of the top of the stack. */
275const char *hlua_get_top_error_string(lua_State *L)
276{
277 if (lua_gettop(L) < 1)
278 return "unknown error";
279 if (lua_type(L, -1) != LUA_TSTRING)
280 return "unknown error";
281 return lua_tostring(L, -1);
282}
283
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200284__LJMP static const char *hlua_traceback(lua_State *L)
285{
286 lua_Debug ar;
287 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200288 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200289 int filled = 0;
290
291 while (lua_getstack(L, level++, &ar)) {
292
293 /* Add separator */
294 if (filled)
295 chunk_appendf(msg, ", ");
296 filled = 1;
297
298 /* Fill fields:
299 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
300 * 'l': fills in the field currentline;
301 * 'n': fills in the field name and namewhat;
302 * 't': fills in the field istailcall;
303 */
304 lua_getinfo(L, "Slnt", &ar);
305
306 /* Append code localisation */
307 if (ar.currentline > 0)
308 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
309 else
310 chunk_appendf(msg, "%s ", ar.short_src);
311
312 /*
313 * Get function name
314 *
315 * if namewhat is no empty, name is defined.
316 * what contains "Lua" for Lua function, "C" for C function,
317 * or "main" for main code.
318 */
319 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
320 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
321
322 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
323 chunk_appendf(msg, "main chunk");
324
325 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
326 chunk_appendf(msg, "C function line %d", ar.linedefined);
327
328 else /* nothing left... */
329 chunk_appendf(msg, "?");
330
331
332 /* Display tailed call */
333 if (ar.istailcall)
334 chunk_appendf(msg, " ...");
335 }
336
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200337 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200338}
339
340
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100341/* This function check the number of arguments available in the
342 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500343 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100344 */
345__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
346{
347 if (lua_gettop(L) == nb)
348 return;
349 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
350}
351
Mark Lakes22154b42018-01-29 14:38:40 -0800352/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100353 * and the line number where the error is encountered.
354 */
355static int hlua_pusherror(lua_State *L, const char *fmt, ...)
356{
357 va_list argp;
358 va_start(argp, fmt);
359 luaL_where(L, 1);
360 lua_pushvfstring(L, fmt, argp);
361 va_end(argp);
362 lua_concat(L, 2);
363 return 1;
364}
365
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100366/* This functions is used with sample fetch and converters. It
367 * converts the HAProxy configuration argument in a lua stack
368 * values.
369 *
370 * It takes an array of "arg", and each entry of the array is
371 * converted and pushed in the LUA stack.
372 */
373static int hlua_arg2lua(lua_State *L, const struct arg *arg)
374{
375 switch (arg->type) {
376 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100377 case ARGT_TIME:
378 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100379 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100380 break;
381
382 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200383 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100384 break;
385
386 case ARGT_IPV4:
387 case ARGT_IPV6:
388 case ARGT_MSK4:
389 case ARGT_MSK6:
390 case ARGT_FE:
391 case ARGT_BE:
392 case ARGT_TAB:
393 case ARGT_SRV:
394 case ARGT_USR:
395 case ARGT_MAP:
396 default:
397 lua_pushnil(L);
398 break;
399 }
400 return 1;
401}
402
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500403/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100404 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500405 * with sample fetch wrappers. The input arguments are given to the
406 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100407 */
408static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
409{
410 switch (lua_type(L, ud)) {
411
412 case LUA_TNUMBER:
413 case LUA_TBOOLEAN:
414 arg->type = ARGT_SINT;
415 arg->data.sint = lua_tointeger(L, ud);
416 break;
417
418 case LUA_TSTRING:
419 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200420 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200421 /* We don't know the actual size of the underlying allocation, so be conservative. */
422 arg->data.str.size = arg->data.str.data;
423 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100424 break;
425
426 case LUA_TUSERDATA:
427 case LUA_TNIL:
428 case LUA_TTABLE:
429 case LUA_TFUNCTION:
430 case LUA_TTHREAD:
431 case LUA_TLIGHTUSERDATA:
432 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200433 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100434 break;
435 }
436 return 1;
437}
438
439/* the following functions are used to convert a struct sample
440 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500441 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100442 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100443static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100444{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200445 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200448 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449 break;
450
451 case SMP_T_BIN:
452 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200453 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100454 break;
455
456 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200457 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100458 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
459 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
460 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
461 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
462 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
463 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
464 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
465 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
466 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200467 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100468 break;
469 default:
470 lua_pushnil(L);
471 break;
472 }
473 break;
474
475 case SMP_T_IPV4:
476 case SMP_T_IPV6:
477 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200478 if (sample_casts[smp->data.type][SMP_T_STR] &&
479 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200480 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100481 else
482 lua_pushnil(L);
483 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100484 default:
485 lua_pushnil(L);
486 break;
487 }
488 return 1;
489}
490
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100491/* the following functions are used to convert a struct sample
492 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500493 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100494 */
495static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
496{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200497 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100498
499 case SMP_T_BIN:
500 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100502 break;
503
504 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200505 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100506 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
507 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
508 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
509 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
510 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
511 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
512 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
513 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
514 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200515 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100516 break;
517 default:
518 lua_pushstring(L, "");
519 break;
520 }
521 break;
522
523 case SMP_T_SINT:
524 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100525 case SMP_T_IPV4:
526 case SMP_T_IPV6:
527 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200528 if (sample_casts[smp->data.type][SMP_T_STR] &&
529 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200530 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100531 else
532 lua_pushstring(L, "");
533 break;
534 default:
535 lua_pushstring(L, "");
536 break;
537 }
538 return 1;
539}
540
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100541/* the following functions are used to convert an Lua type in a
542 * struct sample. This is useful to provide data from a converter
543 * to the LUA code.
544 */
545static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
546{
547 switch (lua_type(L, ud)) {
548
549 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200550 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200551 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100552 break;
553
554
555 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200556 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200557 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100558 break;
559
560 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200561 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100562 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200563 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200564 /* We don't know the actual size of the underlying allocation, so be conservative. */
565 smp->data.u.str.size = smp->data.u.str.data;
566 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100567 break;
568
569 case LUA_TUSERDATA:
570 case LUA_TNIL:
571 case LUA_TTABLE:
572 case LUA_TFUNCTION:
573 case LUA_TTHREAD:
574 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200575 case LUA_TNONE:
576 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200577 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200578 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100579 break;
580 }
581 return 1;
582}
583
Ilya Shipitsind4259502020-04-08 01:07:56 +0500584/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800585 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100586 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100587 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500588 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100589 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100590 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100591__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100592 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100593{
594 int min_arg;
595 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100596 struct proxy *px;
597 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100598
599 idx = 0;
600 min_arg = ARGM(mask);
601 mask >>= ARGM_BITS;
602
603 while (1) {
604
605 /* Check oversize. */
606 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100607 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100608 }
609
610 /* Check for mandatory arguments. */
611 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100612 if (idx < min_arg) {
613
614 /* If miss other argument than the first one, we return an error. */
615 if (idx > 0)
616 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
617
618 /* If first argument have a certain type, some default values
619 * may be used. See the function smp_resolve_args().
620 */
621 switch (mask & ARGT_MASK) {
622
623 case ARGT_FE:
624 if (!(p->cap & PR_CAP_FE))
625 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
626 argp[idx].data.prx = p;
627 argp[idx].type = ARGT_FE;
628 argp[idx+1].type = ARGT_STOP;
629 break;
630
631 case ARGT_BE:
632 if (!(p->cap & PR_CAP_BE))
633 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
634 argp[idx].data.prx = p;
635 argp[idx].type = ARGT_BE;
636 argp[idx+1].type = ARGT_STOP;
637 break;
638
639 case ARGT_TAB:
640 argp[idx].data.prx = p;
641 argp[idx].type = ARGT_TAB;
642 argp[idx+1].type = ARGT_STOP;
643 break;
644
645 default:
646 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
647 break;
648 }
649 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100650 return 0;
651 }
652
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500653 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100654 if ((mask & ARGT_MASK) == ARGT_STOP &&
655 argp[idx].type != ARGT_STOP) {
656 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
657 }
658
659 if ((mask & ARGT_MASK) == ARGT_STOP &&
660 argp[idx].type == ARGT_STOP) {
661 return 0;
662 }
663
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100664 /* Convert some argument types. */
665 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100666 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100667 if (argp[idx].type != ARGT_SINT)
668 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
669 argp[idx].type = ARGT_SINT;
670 break;
671
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100672 case ARGT_TIME:
673 if (argp[idx].type != ARGT_SINT)
674 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200675 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100676 break;
677
678 case ARGT_SIZE:
679 if (argp[idx].type != ARGT_SINT)
680 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200681 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100682 break;
683
684 case ARGT_FE:
685 if (argp[idx].type != ARGT_STR)
686 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200687 memcpy(trash.area, argp[idx].data.str.area,
688 argp[idx].data.str.data);
689 trash.area[argp[idx].data.str.data] = 0;
690 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100691 if (!argp[idx].data.prx)
692 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
693 argp[idx].type = ARGT_FE;
694 break;
695
696 case ARGT_BE:
697 if (argp[idx].type != ARGT_STR)
698 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200699 memcpy(trash.area, argp[idx].data.str.area,
700 argp[idx].data.str.data);
701 trash.area[argp[idx].data.str.data] = 0;
702 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100703 if (!argp[idx].data.prx)
704 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
705 argp[idx].type = ARGT_BE;
706 break;
707
708 case ARGT_TAB:
709 if (argp[idx].type != ARGT_STR)
710 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200711 memcpy(trash.area, argp[idx].data.str.area,
712 argp[idx].data.str.data);
713 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhus9fe7c632019-09-29 23:03:09 +0200714 argp[idx].data.t = stktable_find_by_name(trash.area);
715 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100716 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
717 argp[idx].type = ARGT_TAB;
718 break;
719
720 case ARGT_SRV:
721 if (argp[idx].type != ARGT_STR)
722 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200723 memcpy(trash.area, argp[idx].data.str.area,
724 argp[idx].data.str.data);
725 trash.area[argp[idx].data.str.data] = 0;
726 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100727 if (sname) {
728 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200729 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200730 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100731 if (!px)
732 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
733 }
734 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200735 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100736 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100737 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100738 argp[idx].data.srv = findserver(px, sname);
739 if (!argp[idx].data.srv)
740 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
741 argp[idx].type = ARGT_SRV;
742 break;
743
744 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200745 memcpy(trash.area, argp[idx].data.str.area,
746 argp[idx].data.str.data);
747 trash.area[argp[idx].data.str.data] = 0;
748 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100749 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
750 argp[idx].type = ARGT_IPV4;
751 break;
752
753 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200754 memcpy(trash.area, argp[idx].data.str.area,
755 argp[idx].data.str.data);
756 trash.area[argp[idx].data.str.data] = 0;
757 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100758 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
759 argp[idx].type = ARGT_MSK4;
760 break;
761
762 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200763 memcpy(trash.area, argp[idx].data.str.area,
764 argp[idx].data.str.data);
765 trash.area[argp[idx].data.str.data] = 0;
766 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100767 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
768 argp[idx].type = ARGT_IPV6;
769 break;
770
771 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200772 memcpy(trash.area, argp[idx].data.str.area,
773 argp[idx].data.str.data);
774 trash.area[argp[idx].data.str.data] = 0;
775 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100776 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
777 argp[idx].type = ARGT_MSK6;
778 break;
779
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100780 case ARGT_MAP:
781 case ARGT_REG:
782 case ARGT_USR:
783 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100784 break;
785 }
786
787 /* Check for type of argument. */
788 if ((mask & ARGT_MASK) != argp[idx].type) {
789 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
790 arg_type_names[(mask & ARGT_MASK)],
791 arg_type_names[argp[idx].type & ARGT_MASK]);
792 WILL_LJMP(luaL_argerror(L, first + idx, msg));
793 }
794
795 /* Next argument. */
796 mask >>= ARGT_BITS;
797 idx++;
798 }
799}
800
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100801/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500802 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100803 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100804 *
805 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100806 * - hlua_sethlua : create the association between hlua context and lua_state.
807 */
808static inline struct hlua *hlua_gethlua(lua_State *L)
809{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100810 struct hlua **hlua = lua_getextraspace(L);
811 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100812}
813static inline void hlua_sethlua(struct hlua *hlua)
814{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100815 struct hlua **hlua_store = lua_getextraspace(hlua->T);
816 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100817}
818
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100819/* This function is used to send logs. It try to send on screen (stderr)
820 * and on the default syslog server.
821 */
822static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
823{
824 struct tm tm;
825 char *p;
826
827 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200828 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100829 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200830 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200831 /* Break the message if exceed the buffer size. */
832 *(p-4) = ' ';
833 *(p-3) = '.';
834 *(p-2) = '.';
835 *(p-1) = '.';
836 break;
837 }
Willy Tarreau90807112020-02-25 08:16:33 +0100838 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100839 *p = *msg;
840 else
841 *p = '.';
842 }
843 *p = '\0';
844
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200845 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100846 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200847 get_localtime(date.tv_sec, &tm);
848 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100849 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200850 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100851 fflush(stderr);
852 }
853}
854
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100855/* This function just ensure that the yield will be always
856 * returned with a timeout and permit to set some flags
857 */
858__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100859 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100860{
861 struct hlua *hlua = hlua_gethlua(L);
862
863 /* Set the wake timeout. If timeout is required, we set
864 * the expiration time.
865 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200866 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100867
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100868 hlua->flags |= flags;
869
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100870 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200871 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100872}
873
Willy Tarreau87b09662015-04-03 00:22:06 +0200874/* This function initialises the Lua environment stored in the stream.
875 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100876 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200877 *
878 * This function is particular. it initialises a new Lua thread. If the
879 * initialisation fails (example: out of memory error), the lua function
880 * throws an error (longjmp).
881 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100882 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500883 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100884 * threads appear, the safe environment set a lock to ensure only one
885 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500886 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100887 *
888 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500889 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100890 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800891 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200892 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800893 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500894 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100895 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100896int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100897{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100898 if (!already_safe) {
899 if (!SET_SAFE_LJMP(gL.T)) {
900 lua->Tref = LUA_REFNIL;
901 return 0;
902 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200903 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100904 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100905 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +0100906 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +0100907 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100908 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100909 lua->T = lua_newthread(gL.T);
910 if (!lua->T) {
911 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100912 if (!already_safe)
913 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100914 return 0;
915 }
916 hlua_sethlua(lua);
917 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
918 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100919 if (!already_safe)
920 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100921 return 1;
922}
923
Willy Tarreau87b09662015-04-03 00:22:06 +0200924/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100925 * is destroyed. The destroy also the memory context. The struct "lua"
926 * is not freed.
927 */
928void hlua_ctx_destroy(struct hlua *lua)
929{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100930 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100931 return;
932
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100933 if (!lua->T)
934 goto end;
935
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100936 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200937 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100938
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200939 if (!SET_SAFE_LJMP(lua->T))
940 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100941 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200942 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200943
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200944 if (!SET_SAFE_LJMP(gL.T))
945 return;
946 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
947 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200948 /* Forces a garbage collecting process. If the Lua program is finished
949 * without error, we run the GC on the thread pointer. Its freed all
950 * the unused memory.
951 * If the thread is finnish with an error or is currently yielded,
952 * it seems that the GC applied on the thread doesn't clean anything,
953 * so e run the GC on the main thread.
954 * NOTE: maybe this action locks all the Lua threads untiml the en of
955 * the garbage collection.
956 */
Willy Tarreauf31af932020-01-14 09:59:38 +0100957 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200958 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200959 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200960 lua_gc(gL.T, LUA_GCCOLLECT, 0);
961 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200962 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200963
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200964 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100965
966end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100967 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100968}
969
970/* This function is used to restore the Lua context when a coroutine
971 * fails. This function copy the common memory between old coroutine
972 * and the new coroutine. The old coroutine is destroyed, and its
973 * replaced by the new coroutine.
974 * If the flag "keep_msg" is set, the last entry of the old is assumed
975 * as string error message and it is copied in the new stack.
976 */
977static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
978{
979 lua_State *T;
980 int new_ref;
981
982 /* Renew the main LUA stack doesn't have sense. */
983 if (lua == &gL)
984 return 0;
985
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100986 /* New Lua coroutine. */
987 T = lua_newthread(gL.T);
988 if (!T)
989 return 0;
990
991 /* Copy last error message. */
992 if (keep_msg)
993 lua_xmove(lua->T, T, 1);
994
995 /* Copy data between the coroutines. */
996 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
997 lua_xmove(lua->T, T, 1);
998 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
999
1000 /* Destroy old data. */
1001 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1002
1003 /* The thread is garbage collected by Lua. */
1004 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1005
1006 /* Fill the struct with the new coroutine values. */
1007 lua->Mref = new_ref;
1008 lua->T = T;
1009 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1010
1011 /* Set context. */
1012 hlua_sethlua(lua);
1013
1014 return 1;
1015}
1016
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001017void hlua_hook(lua_State *L, lua_Debug *ar)
1018{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001019 struct hlua *hlua = hlua_gethlua(L);
1020
1021 /* Lua cannot yield when its returning from a function,
1022 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001023 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001024 */
1025 if (lua_gethookmask(L) & LUA_MASKRET) {
1026 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1027 return;
1028 }
1029
1030 /* restore the interrupt condition. */
1031 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1032
1033 /* If we interrupt the Lua processing in yieldable state, we yield.
1034 * If the state is not yieldable, trying yield causes an error.
1035 */
1036 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001037 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001038
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001039 /* If we cannot yield, update the clock and check the timeout. */
1040 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001041 hlua->run_time += now_ms - hlua->start_time;
1042 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001043 lua_pushfstring(L, "execution timeout");
1044 WILL_LJMP(lua_error(L));
1045 }
1046
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001047 /* Update the start time. */
1048 hlua->start_time = now_ms;
1049
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001050 /* Try to interrupt the process at the end of the current
1051 * unyieldable function.
1052 */
1053 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001054}
1055
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001056/* This function start or resumes the Lua stack execution. If the flag
1057 * "yield_allowed" if no set and the LUA stack execution returns a yield
1058 * The function return an error.
1059 *
1060 * The function can returns 4 values:
1061 * - HLUA_E_OK : The execution is terminated without any errors.
1062 * - HLUA_E_AGAIN : The execution must continue at the next associated
1063 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001064 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001065 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001066 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001067 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001068 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001069 * LUA code.
1070 */
1071static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1072{
1073 int ret;
1074 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001075 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001076
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001077 /* Initialise run time counter. */
1078 if (!HLUA_IS_RUNNING(lua))
1079 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001080
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001081 /* Lock the whole Lua execution. This lock must be before the
1082 * label "resume_execution".
1083 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001084 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001085
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001086resume_execution:
1087
1088 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1089 * instructions. it is used for preventing infinite loops.
1090 */
1091 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1092
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001093 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001094 HLUA_SET_RUN(lua);
1095 HLUA_CLR_CTRLYIELD(lua);
1096 HLUA_CLR_WAKERESWR(lua);
1097 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001098
Christopher Fauletbc275a92020-02-26 14:55:16 +01001099 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001100 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001101 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001102
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001103 /* Call the function. */
1104 ret = lua_resume(lua->T, gL.T, lua->nargs);
1105 switch (ret) {
1106
1107 case LUA_OK:
1108 ret = HLUA_E_OK;
1109 break;
1110
1111 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001112 /* Check if the execution timeout is expired. It it is the case, we
1113 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001114 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001115 tv_update_date(0, 1);
1116 lua->run_time += now_ms - lua->start_time;
1117 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001118 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001119 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001120 break;
1121 }
1122 /* Process the forced yield. if the general yield is not allowed or
1123 * if no task were associated this the current Lua execution
1124 * coroutine, we resume the execution. Else we want to return in the
1125 * scheduler and we want to be waked up again, to continue the
1126 * current Lua execution. So we schedule our own task.
1127 */
1128 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001129 if (!yield_allowed || !lua->task)
1130 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001131 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001132 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001133 if (!yield_allowed) {
1134 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001135 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001136 break;
1137 }
1138 ret = HLUA_E_AGAIN;
1139 break;
1140
1141 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001142
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001143 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001144 * because the errors ares the only one mean to return immediately
1145 * from and lua execution.
1146 */
1147 if (lua->flags & HLUA_EXIT) {
1148 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001149 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001150 break;
1151 }
1152
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001153 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001154 if (!lua_checkstack(lua->T, 1)) {
1155 ret = HLUA_E_ERR;
1156 break;
1157 }
1158 msg = lua_tostring(lua->T, -1);
1159 lua_settop(lua->T, 0); /* Empty the stack. */
1160 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001161 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001162 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001163 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001164 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001165 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001166 ret = HLUA_E_ERRMSG;
1167 break;
1168
1169 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001170 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001171 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001172 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001173 break;
1174
1175 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001176 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001177 if (!lua_checkstack(lua->T, 1)) {
1178 ret = HLUA_E_ERR;
1179 break;
1180 }
1181 msg = lua_tostring(lua->T, -1);
1182 lua_settop(lua->T, 0); /* Empty the stack. */
1183 lua_pop(lua->T, 1);
1184 if (msg)
1185 lua_pushfstring(lua->T, "message handler error: %s", msg);
1186 else
1187 lua_pushfstring(lua->T, "message handler error");
1188 ret = HLUA_E_ERRMSG;
1189 break;
1190
1191 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001192 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001193 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001194 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001195 break;
1196 }
1197
1198 switch (ret) {
1199 case HLUA_E_AGAIN:
1200 break;
1201
1202 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001203 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001204 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001205 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001206 break;
1207
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001208 case HLUA_E_ETMOUT:
1209 case HLUA_E_NOMEM:
1210 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001211 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001212 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001213 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001214 hlua_ctx_renew(lua, 0);
1215 break;
1216
1217 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001218 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001219 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001220 break;
1221 }
1222
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001223 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001224 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001225
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001226 return ret;
1227}
1228
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001229/* This function exit the current code. */
1230__LJMP static int hlua_done(lua_State *L)
1231{
1232 struct hlua *hlua = hlua_gethlua(L);
1233
1234 hlua->flags |= HLUA_EXIT;
1235 WILL_LJMP(lua_error(L));
1236
1237 return 0;
1238}
1239
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001240/* This function is an LUA binding. It provides a function
1241 * for deleting ACL from a referenced ACL file.
1242 */
1243__LJMP static int hlua_del_acl(lua_State *L)
1244{
1245 const char *name;
1246 const char *key;
1247 struct pat_ref *ref;
1248
1249 MAY_LJMP(check_args(L, 2, "del_acl"));
1250
1251 name = MAY_LJMP(luaL_checkstring(L, 1));
1252 key = MAY_LJMP(luaL_checkstring(L, 2));
1253
1254 ref = pat_ref_lookup(name);
1255 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001256 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001257
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001258 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001259 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001260 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001261 return 0;
1262}
1263
1264/* This function is an LUA binding. It provides a function
1265 * for deleting map entry from a referenced map file.
1266 */
1267static int hlua_del_map(lua_State *L)
1268{
1269 const char *name;
1270 const char *key;
1271 struct pat_ref *ref;
1272
1273 MAY_LJMP(check_args(L, 2, "del_map"));
1274
1275 name = MAY_LJMP(luaL_checkstring(L, 1));
1276 key = MAY_LJMP(luaL_checkstring(L, 2));
1277
1278 ref = pat_ref_lookup(name);
1279 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001280 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001281
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001282 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001283 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001284 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001285 return 0;
1286}
1287
1288/* This function is an LUA binding. It provides a function
1289 * for adding ACL pattern from a referenced ACL file.
1290 */
1291static int hlua_add_acl(lua_State *L)
1292{
1293 const char *name;
1294 const char *key;
1295 struct pat_ref *ref;
1296
1297 MAY_LJMP(check_args(L, 2, "add_acl"));
1298
1299 name = MAY_LJMP(luaL_checkstring(L, 1));
1300 key = MAY_LJMP(luaL_checkstring(L, 2));
1301
1302 ref = pat_ref_lookup(name);
1303 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001304 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001305
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001306 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001307 if (pat_ref_find_elt(ref, key) == NULL)
1308 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001309 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001310 return 0;
1311}
1312
1313/* This function is an LUA binding. It provides a function
1314 * for setting map pattern and sample from a referenced map
1315 * file.
1316 */
1317static int hlua_set_map(lua_State *L)
1318{
1319 const char *name;
1320 const char *key;
1321 const char *value;
1322 struct pat_ref *ref;
1323
1324 MAY_LJMP(check_args(L, 3, "set_map"));
1325
1326 name = MAY_LJMP(luaL_checkstring(L, 1));
1327 key = MAY_LJMP(luaL_checkstring(L, 2));
1328 value = MAY_LJMP(luaL_checkstring(L, 3));
1329
1330 ref = pat_ref_lookup(name);
1331 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001332 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001333
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001334 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001335 if (pat_ref_find_elt(ref, key) != NULL)
1336 pat_ref_set(ref, key, value, NULL);
1337 else
1338 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001339 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001340 return 0;
1341}
1342
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001343/* A class is a lot of memory that contain data. This data can be a table,
1344 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001345 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001346 * the name of the object (_G[<name>] = <metable> ).
1347 *
1348 * A metable is a table that modify the standard behavior of a standard
1349 * access to the associated data. The entries of this new metatable are
1350 * defined as is:
1351 *
1352 * http://lua-users.org/wiki/MetatableEvents
1353 *
1354 * __index
1355 *
1356 * we access an absent field in a table, the result is nil. This is
1357 * true, but it is not the whole truth. Actually, such access triggers
1358 * the interpreter to look for an __index metamethod: If there is no
1359 * such method, as usually happens, then the access results in nil;
1360 * otherwise, the metamethod will provide the result.
1361 *
1362 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1363 * the key does not appear in the table, but the metatable has an __index
1364 * property:
1365 *
1366 * - if the value is a function, the function is called, passing in the
1367 * table and the key; the return value of that function is returned as
1368 * the result.
1369 *
1370 * - if the value is another table, the value of the key in that table is
1371 * asked for and returned (and if it doesn't exist in that table, but that
1372 * table's metatable has an __index property, then it continues on up)
1373 *
1374 * - Use "rawget(myTable,key)" to skip this metamethod.
1375 *
1376 * http://www.lua.org/pil/13.4.1.html
1377 *
1378 * __newindex
1379 *
1380 * Like __index, but control property assignment.
1381 *
1382 * __mode - Control weak references. A string value with one or both
1383 * of the characters 'k' and 'v' which specifies that the the
1384 * keys and/or values in the table are weak references.
1385 *
1386 * __call - Treat a table like a function. When a table is followed by
1387 * parenthesis such as "myTable( 'foo' )" and the metatable has
1388 * a __call key pointing to a function, that function is invoked
1389 * (passing any specified arguments) and the return value is
1390 * returned.
1391 *
1392 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1393 * called, if the metatable for myTable has a __metatable
1394 * key, the value of that key is returned instead of the
1395 * actual metatable.
1396 *
1397 * __tostring - Control string representation. When the builtin
1398 * "tostring( myTable )" function is called, if the metatable
1399 * for myTable has a __tostring property set to a function,
1400 * that function is invoked (passing myTable to it) and the
1401 * return value is used as the string representation.
1402 *
1403 * __len - Control table length. When the table length is requested using
1404 * the length operator ( '#' ), if the metatable for myTable has
1405 * a __len key pointing to a function, that function is invoked
1406 * (passing myTable to it) and the return value used as the value
1407 * of "#myTable".
1408 *
1409 * __gc - Userdata finalizer code. When userdata is set to be garbage
1410 * collected, if the metatable has a __gc field pointing to a
1411 * function, that function is first invoked, passing the userdata
1412 * to it. The __gc metamethod is not called for tables.
1413 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1414 *
1415 * Special metamethods for redefining standard operators:
1416 * http://www.lua.org/pil/13.1.html
1417 *
1418 * __add "+"
1419 * __sub "-"
1420 * __mul "*"
1421 * __div "/"
1422 * __unm "!"
1423 * __pow "^"
1424 * __concat ".."
1425 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001426 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001427 * http://www.lua.org/pil/13.2.html
1428 *
1429 * __eq "=="
1430 * __lt "<"
1431 * __le "<="
1432 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001433
1434/*
1435 *
1436 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001437 * Class Map
1438 *
1439 *
1440 */
1441
1442/* Returns a struct hlua_map if the stack entry "ud" is
1443 * a class session, otherwise it throws an error.
1444 */
1445__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1446{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001447 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001448}
1449
1450/* This function is the map constructor. It don't need
1451 * the class Map object. It creates and return a new Map
1452 * object. It must be called only during "body" or "init"
1453 * context because it process some filesystem accesses.
1454 */
1455__LJMP static int hlua_map_new(struct lua_State *L)
1456{
1457 const char *fn;
1458 int match = PAT_MATCH_STR;
1459 struct sample_conv conv;
1460 const char *file = "";
1461 int line = 0;
1462 lua_Debug ar;
1463 char *err = NULL;
1464 struct arg args[2];
1465
1466 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1467 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1468
1469 fn = MAY_LJMP(luaL_checkstring(L, 1));
1470
1471 if (lua_gettop(L) >= 2) {
1472 match = MAY_LJMP(luaL_checkinteger(L, 2));
1473 if (match < 0 || match >= PAT_MATCH_NUM)
1474 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1475 }
1476
1477 /* Get Lua filename and line number. */
1478 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1479 lua_getinfo(L, "Sl", &ar); /* get info about it */
1480 if (ar.currentline > 0) { /* is there info? */
1481 file = ar.short_src;
1482 line = ar.currentline;
1483 }
1484 }
1485
1486 /* fill fake sample_conv struct. */
1487 conv.kw = ""; /* unused. */
1488 conv.process = NULL; /* unused. */
1489 conv.arg_mask = 0; /* unused. */
1490 conv.val_args = NULL; /* unused. */
1491 conv.out_type = SMP_T_STR;
1492 conv.private = (void *)(long)match;
1493 switch (match) {
1494 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1495 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1496 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1497 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1498 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1499 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1500 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001501 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001502 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1503 default:
1504 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1505 }
1506
1507 /* fill fake args. */
1508 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001509 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001510 args[1].type = ARGT_STOP;
1511
1512 /* load the map. */
1513 if (!sample_load_map(args, &conv, file, line, &err)) {
1514 /* error case: we cant use luaL_error because we must
1515 * free the err variable.
1516 */
1517 luaL_where(L, 1);
1518 lua_pushfstring(L, "'new': %s.", err);
1519 lua_concat(L, 2);
1520 free(err);
1521 WILL_LJMP(lua_error(L));
1522 }
1523
1524 /* create the lua object. */
1525 lua_newtable(L);
1526 lua_pushlightuserdata(L, args[0].data.map);
1527 lua_rawseti(L, -2, 0);
1528
1529 /* Pop a class Map metatable and affect it to the userdata. */
1530 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1531 lua_setmetatable(L, -2);
1532
1533
1534 return 1;
1535}
1536
1537__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1538{
1539 struct map_descriptor *desc;
1540 struct pattern *pat;
1541 struct sample smp;
1542
1543 MAY_LJMP(check_args(L, 2, "lookup"));
1544 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001545 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001546 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001547 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001548 }
1549 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001550 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001551 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001552 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 +02001553 }
1554
1555 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001556 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001557 if (str)
1558 lua_pushstring(L, "");
1559 else
1560 lua_pushnil(L);
1561 return 1;
1562 }
1563
1564 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001565 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001566 return 1;
1567}
1568
1569__LJMP static int hlua_map_lookup(struct lua_State *L)
1570{
1571 return _hlua_map_lookup(L, 0);
1572}
1573
1574__LJMP static int hlua_map_slookup(struct lua_State *L)
1575{
1576 return _hlua_map_lookup(L, 1);
1577}
1578
1579/*
1580 *
1581 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001582 * Class Socket
1583 *
1584 *
1585 */
1586
1587__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1588{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001589 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001590}
1591
1592/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001593 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001594 * received.
1595 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001596static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001597{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001598 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001599
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001600 if (appctx->ctx.hlua_cosocket.die) {
1601 si_shutw(si);
1602 si_shutr(si);
1603 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001604 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1605 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001606 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1607 }
1608
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001609 /* If we cant write, wakeup the pending write signals. */
1610 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001611 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001612
1613 /* If we cant read, wakeup the pending read signals. */
1614 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001615 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001616
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001617 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001618 * to be notified whenever the connection completes.
1619 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001620 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001621 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001622 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001623 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001624 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001625 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001626
1627 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001628 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001629
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001630 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001631 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001632 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001633
1634 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001635 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001636 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001637
1638 /* Some data were injected in the buffer, notify the stream
1639 * interface.
1640 */
1641 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001642 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001643
1644 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001645 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001646 */
1647 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001648 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001649}
1650
Willy Tarreau87b09662015-04-03 00:22:06 +02001651/* This function is called when the "struct stream" is destroyed.
1652 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001653 * Wake all the pending signals.
1654 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001655static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001656{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001657 struct xref *peer;
1658
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001659 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001660 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1661 if (peer)
1662 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001663
1664 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001665 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1666 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001667}
1668
1669/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001670 * uses this object. If the stream does not exists, just quit.
1671 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001672 * pending signal can rest in the read and write lists. destroy
1673 * it.
1674 */
1675__LJMP static int hlua_socket_gc(lua_State *L)
1676{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001677 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001678 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001679 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001680
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001681 MAY_LJMP(check_args(L, 1, "__gc"));
1682
1683 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001684 peer = xref_get_peer_and_lock(&socket->xref);
1685 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001686 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001687 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001688
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001689 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001690 appctx->ctx.hlua_cosocket.die = 1;
1691 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001692
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001693 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001694 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001695 return 0;
1696}
1697
1698/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001699 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001700 */
sada05ed3302018-05-11 11:48:18 -07001701__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001702{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001703 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001704 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001705 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001706 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001707
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001708 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001709
1710 /* Check if we run on the same thread than the xreator thread.
1711 * We cannot access to the socket if the thread is different.
1712 */
1713 if (socket->tid != tid)
1714 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1715
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001716 peer = xref_get_peer_and_lock(&socket->xref);
1717 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001718 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001719
1720 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001721 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001722
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001723 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001724 appctx->ctx.hlua_cosocket.die = 1;
1725 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001726
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001727 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001728 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001729 return 0;
1730}
1731
sada05ed3302018-05-11 11:48:18 -07001732/* The close function calls close_helper.
1733 */
1734__LJMP static int hlua_socket_close(lua_State *L)
1735{
1736 MAY_LJMP(check_args(L, 1, "close"));
1737 return hlua_socket_close_helper(L);
1738}
1739
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001740/* This Lua function assumes that the stack contain three parameters.
1741 * 1 - USERDATA containing a struct socket
1742 * 2 - INTEGER with values of the macro defined below
1743 * If the integer is -1, we must read at most one line.
1744 * If the integer is -2, we ust read all the data until the
1745 * end of the stream.
1746 * If the integer is positive value, we must read a number of
1747 * bytes corresponding to this value.
1748 */
1749#define HLSR_READ_LINE (-1)
1750#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001751__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001752{
1753 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1754 int wanted = lua_tointeger(L, 2);
1755 struct hlua *hlua = hlua_gethlua(L);
1756 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001757 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001758 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001759 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001760 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001761 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001762 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001763 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001764 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001765 struct stream_interface *si;
1766 struct stream *s;
1767 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001768 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001769
1770 /* Check if this lua stack is schedulable. */
1771 if (!hlua || !hlua->task)
1772 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1773 "'frontend', 'backend' or 'task'"));
1774
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001775 /* Check if we run on the same thread than the xreator thread.
1776 * We cannot access to the socket if the thread is different.
1777 */
1778 if (socket->tid != tid)
1779 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1780
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001781 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001782 peer = xref_get_peer_and_lock(&socket->xref);
1783 if (!peer)
1784 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001785 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1786 si = appctx->owner;
1787 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001788
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001789 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001790 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001791 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001792 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793 if (nblk < 0) /* Connection close. */
1794 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001795 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001796 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001797
1798 /* remove final \r\n. */
1799 if (nblk == 1) {
1800 if (blk1[len1-1] == '\n') {
1801 len1--;
1802 skip_at_end++;
1803 if (blk1[len1-1] == '\r') {
1804 len1--;
1805 skip_at_end++;
1806 }
1807 }
1808 }
1809 else {
1810 if (blk2[len2-1] == '\n') {
1811 len2--;
1812 skip_at_end++;
1813 if (blk2[len2-1] == '\r') {
1814 len2--;
1815 skip_at_end++;
1816 }
1817 }
1818 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001819 }
1820
1821 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001822 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001823 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001824 if (nblk < 0) /* Connection close. */
1825 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001826 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001827 goto connection_empty;
1828 }
1829
1830 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001831 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001832 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001833 if (nblk < 0) /* Connection close. */
1834 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001835 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001836 goto connection_empty;
1837
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001838 missing_bytes = wanted - socket->b.n;
1839 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001840 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001841 len1 = missing_bytes;
1842 } if (nblk == 2 && len1 + len2 > missing_bytes)
1843 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001844 }
1845
1846 len = len1;
1847
1848 luaL_addlstring(&socket->b, blk1, len1);
1849 if (nblk == 2) {
1850 len += len2;
1851 luaL_addlstring(&socket->b, blk2, len2);
1852 }
1853
1854 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001855 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001856
1857 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001858 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001859
1860 /* If the pattern reclaim to read all the data
1861 * in the connection, got out.
1862 */
1863 if (wanted == HLSR_READ_ALL)
1864 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001865 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001866 goto connection_empty;
1867
1868 /* Return result. */
1869 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001870 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001871 return 1;
1872
1873connection_closed:
1874
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001875 xref_unlock(&socket->xref, peer);
1876
1877no_peer:
1878
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001879 /* If the buffer containds data. */
1880 if (socket->b.n > 0) {
1881 luaL_pushresult(&socket->b);
1882 return 1;
1883 }
1884 lua_pushnil(L);
1885 lua_pushstring(L, "connection closed.");
1886 return 2;
1887
1888connection_empty:
1889
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001890 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1891 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001892 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001893 }
1894 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001895 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001896 return 0;
1897}
1898
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001899/* This Lua function gets two parameters. The first one can be string
1900 * or a number. If the string is "*l", the user requires one line. If
1901 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001902 * If the value is a number, the user require a number of bytes equal
1903 * to the value. The default value is "*l" (a line).
1904 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001905 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906 * integer takes this values:
1907 * -1 : read a line
1908 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001909 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001910 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001911 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001912 * concatenated with the read data.
1913 */
1914__LJMP static int hlua_socket_receive(struct lua_State *L)
1915{
1916 int wanted = HLSR_READ_LINE;
1917 const char *pattern;
1918 int type;
1919 char *error;
1920 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001921 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001922
1923 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1924 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1925
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001926 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001927
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001928 /* Check if we run on the same thread than the xreator thread.
1929 * We cannot access to the socket if the thread is different.
1930 */
1931 if (socket->tid != tid)
1932 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1933
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001934 /* check for pattern. */
1935 if (lua_gettop(L) >= 2) {
1936 type = lua_type(L, 2);
1937 if (type == LUA_TSTRING) {
1938 pattern = lua_tostring(L, 2);
1939 if (strcmp(pattern, "*a") == 0)
1940 wanted = HLSR_READ_ALL;
1941 else if (strcmp(pattern, "*l") == 0)
1942 wanted = HLSR_READ_LINE;
1943 else {
1944 wanted = strtoll(pattern, &error, 10);
1945 if (*error != '\0')
1946 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1947 }
1948 }
1949 else if (type == LUA_TNUMBER) {
1950 wanted = lua_tointeger(L, 2);
1951 if (wanted < 0)
1952 WILL_LJMP(luaL_error(L, "Unsupported size."));
1953 }
1954 }
1955
1956 /* Set pattern. */
1957 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001958
1959 /* Check if we would replace the top by itself. */
1960 if (lua_gettop(L) != 2)
1961 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001963 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001964 luaL_buffinit(L, &socket->b);
1965
1966 /* Check prefix. */
1967 if (lua_gettop(L) >= 3) {
1968 if (lua_type(L, 3) != LUA_TSTRING)
1969 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1970 pattern = lua_tolstring(L, 3, &len);
1971 luaL_addlstring(&socket->b, pattern, len);
1972 }
1973
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001974 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001975}
1976
1977/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001978 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001979 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001980static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001981{
1982 struct hlua_socket *socket;
1983 struct hlua *hlua = hlua_gethlua(L);
1984 struct appctx *appctx;
1985 size_t buf_len;
1986 const char *buf;
1987 int len;
1988 int send_len;
1989 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001990 struct xref *peer;
1991 struct stream_interface *si;
1992 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001993
1994 /* Check if this lua stack is schedulable. */
1995 if (!hlua || !hlua->task)
1996 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1997 "'frontend', 'backend' or 'task'"));
1998
1999 /* Get object */
2000 socket = MAY_LJMP(hlua_checksocket(L, 1));
2001 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002002 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002003
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002004 /* Check if we run on the same thread than the xreator thread.
2005 * We cannot access to the socket if the thread is different.
2006 */
2007 if (socket->tid != tid)
2008 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2009
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002010 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002011 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002012 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002013 lua_pushinteger(L, -1);
2014 return 1;
2015 }
2016 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2017 si = appctx->owner;
2018 s = si_strm(si);
2019
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002020 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002021 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002022 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002023 lua_pushinteger(L, -1);
2024 return 1;
2025 }
2026
2027 /* Update the input buffer data. */
2028 buf += sent;
2029 send_len = buf_len - sent;
2030
2031 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002032 if (sent >= buf_len) {
2033 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002034 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002035 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002036
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002037 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002038 * the request buffer if its not required.
2039 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002040 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002041 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002042 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002043 }
2044
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002045 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002046 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002047 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002049 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002050
2051 /* send data */
2052 if (len < send_len)
2053 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002054 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002055
2056 /* "Not enough space" (-1), "Buffer too little to contain
2057 * the data" (-2) are not expected because the available length
2058 * is tested.
2059 * Other unknown error are also not expected.
2060 */
2061 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002062 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002063 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002064
sada05ed3302018-05-11 11:48:18 -07002065 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002066 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002067 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002068 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002069 return 1;
2070 }
2071
2072 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002073 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002074
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002075 s->req.rex = TICK_ETERNITY;
2076 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002077
2078 /* Update length sent. */
2079 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002080 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002081
2082 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002083 if (sent + len >= buf_len) {
2084 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002085 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002086 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002087
2088hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002089 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2090 xref_unlock(&socket->xref, peer);
2091 WILL_LJMP(luaL_error(L, "out of memory"));
2092 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002093 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002094 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002095 return 0;
2096}
2097
2098/* This function initiate the send of data. It just check the input
2099 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002100 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002101 * "hlua_socket_write_yield" that can yield.
2102 *
2103 * The Lua function gets between 3 and 4 parameters. The first one is
2104 * the associated object. The second is a string buffer. The third is
2105 * a facultative integer that represents where is the buffer position
2106 * of the start of the data that can send. The first byte is the
2107 * position "1". The default value is "1". The fourth argument is a
2108 * facultative integer that represents where is the buffer position
2109 * of the end of the data that can send. The default is the last byte.
2110 */
2111static int hlua_socket_send(struct lua_State *L)
2112{
2113 int i;
2114 int j;
2115 const char *buf;
2116 size_t buf_len;
2117
2118 /* Check number of arguments. */
2119 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2120 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2121
2122 /* Get the string. */
2123 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2124
2125 /* Get and check j. */
2126 if (lua_gettop(L) == 4) {
2127 j = MAY_LJMP(luaL_checkinteger(L, 4));
2128 if (j < 0)
2129 j = buf_len + j + 1;
2130 if (j > buf_len)
2131 j = buf_len + 1;
2132 lua_pop(L, 1);
2133 }
2134 else
2135 j = buf_len;
2136
2137 /* Get and check i. */
2138 if (lua_gettop(L) == 3) {
2139 i = MAY_LJMP(luaL_checkinteger(L, 3));
2140 if (i < 0)
2141 i = buf_len + i + 1;
2142 if (i > buf_len)
2143 i = buf_len + 1;
2144 lua_pop(L, 1);
2145 } else
2146 i = 1;
2147
2148 /* Check bth i and j. */
2149 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002150 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002151 return 1;
2152 }
2153 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002154 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002155 return 1;
2156 }
2157 if (i == 0)
2158 i = 1;
2159 if (j == 0)
2160 j = 1;
2161
2162 /* Pop the string. */
2163 lua_pop(L, 1);
2164
2165 /* Update the buffer length. */
2166 buf += i - 1;
2167 buf_len = j - i + 1;
2168 lua_pushlstring(L, buf, buf_len);
2169
2170 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002171 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002172
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002173 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002174}
2175
Willy Tarreau22b0a682015-06-17 19:43:49 +02002176#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002177__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2178{
2179 static char buffer[SOCKET_INFO_MAX_LEN];
2180 int ret;
2181 int len;
2182 char *p;
2183
2184 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2185 if (ret <= 0) {
2186 lua_pushnil(L);
2187 return 1;
2188 }
2189
2190 if (ret == AF_UNIX) {
2191 lua_pushstring(L, buffer+1);
2192 return 1;
2193 }
2194 else if (ret == AF_INET6) {
2195 buffer[0] = '[';
2196 len = strlen(buffer);
2197 buffer[len] = ']';
2198 len++;
2199 buffer[len] = ':';
2200 len++;
2201 p = buffer;
2202 }
2203 else if (ret == AF_INET) {
2204 p = buffer + 1;
2205 len = strlen(p);
2206 p[len] = ':';
2207 len++;
2208 }
2209 else {
2210 lua_pushnil(L);
2211 return 1;
2212 }
2213
2214 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2215 lua_pushnil(L);
2216 return 1;
2217 }
2218
2219 lua_pushstring(L, p);
2220 return 1;
2221}
2222
2223/* Returns information about the peer of the connection. */
2224__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2225{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002226 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002227 struct xref *peer;
2228 struct appctx *appctx;
2229 struct stream_interface *si;
2230 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002231 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002232
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002233 MAY_LJMP(check_args(L, 1, "getpeername"));
2234
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002235 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002236
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002237 /* Check if we run on the same thread than the xreator thread.
2238 * We cannot access to the socket if the thread is different.
2239 */
2240 if (socket->tid != tid)
2241 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2242
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002243 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002244 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002245 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002246 lua_pushnil(L);
2247 return 1;
2248 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002249 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2250 si = appctx->owner;
2251 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002252
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002253 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002254 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002255 lua_pushnil(L);
2256 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002257 }
2258
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002259 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002260 xref_unlock(&socket->xref, peer);
2261 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002262}
2263
2264/* Returns information about my connection side. */
2265static int hlua_socket_getsockname(struct lua_State *L)
2266{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002267 struct hlua_socket *socket;
2268 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002269 struct appctx *appctx;
2270 struct xref *peer;
2271 struct stream_interface *si;
2272 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002273 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002274
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002275 MAY_LJMP(check_args(L, 1, "getsockname"));
2276
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002277 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002278
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002279 /* Check if we run on the same thread than the xreator thread.
2280 * We cannot access to the socket if the thread is different.
2281 */
2282 if (socket->tid != tid)
2283 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2284
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002285 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002286 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002287 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002288 lua_pushnil(L);
2289 return 1;
2290 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002291 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2292 si = appctx->owner;
2293 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002295 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002296 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002297 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002298 lua_pushnil(L);
2299 return 1;
2300 }
2301
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002302 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002303 xref_unlock(&socket->xref, peer);
2304 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002305}
2306
2307/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002308static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002309 .obj_type = OBJ_TYPE_APPLET,
2310 .name = "<LUA_TCP>",
2311 .fct = hlua_socket_handler,
2312 .release = hlua_socket_release,
2313};
2314
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002315__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002316{
2317 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2318 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002319 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002320 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002321 struct stream_interface *si;
2322 struct stream *s;
2323
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002324 /* Check if we run on the same thread than the xreator thread.
2325 * We cannot access to the socket if the thread is different.
2326 */
2327 if (socket->tid != tid)
2328 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2329
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002330 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002331 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002332 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002333 lua_pushnil(L);
2334 lua_pushstring(L, "Can't connect");
2335 return 2;
2336 }
2337 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2338 si = appctx->owner;
2339 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002340
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002341 /* Check if we run on the same thread than the xreator thread.
2342 * We cannot access to the socket if the thread is different.
2343 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002344 if (socket->tid != tid) {
2345 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002346 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002347 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002348
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002349 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002350 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002351 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002352 lua_pushnil(L);
2353 lua_pushstring(L, "Can't connect");
2354 return 2;
2355 }
2356
Willy Tarreaue09101e2018-10-16 17:37:12 +02002357 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002358
2359 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002360 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002361 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002362 lua_pushinteger(L, 1);
2363 return 1;
2364 }
2365
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002366 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2367 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002368 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002369 }
2370 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002371 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002372 return 0;
2373}
2374
2375/* This function fail or initite the connection. */
2376__LJMP static int hlua_socket_connect(struct lua_State *L)
2377{
2378 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002379 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002380 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002381 struct hlua *hlua;
2382 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002383 int low, high;
2384 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002385 struct xref *peer;
2386 struct stream_interface *si;
2387 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002388
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002389 if (lua_gettop(L) < 2)
2390 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002391
2392 /* Get args. */
2393 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002394
2395 /* Check if we run on the same thread than the xreator thread.
2396 * We cannot access to the socket if the thread is different.
2397 */
2398 if (socket->tid != tid)
2399 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2400
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002401 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002402 if (lua_gettop(L) >= 3) {
2403 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002404 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002405
Tim Duesterhus6edab862018-01-06 19:04:45 +01002406 /* Force the ip to end with a colon, to support IPv6 addresses
2407 * that are not enclosed within square brackets.
2408 */
2409 if (port > 0) {
2410 luaL_buffinit(L, &b);
2411 luaL_addstring(&b, ip);
2412 luaL_addchar(&b, ':');
2413 luaL_pushresult(&b);
2414 ip = lua_tolstring(L, lua_gettop(L), NULL);
2415 }
2416 }
2417
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002418 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002419 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002420 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002421 lua_pushnil(L);
2422 return 1;
2423 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002424
2425 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002426 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002427 if (!addr) {
2428 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002429 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002430 }
2431 if (low != high) {
2432 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002433 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002434 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002435
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002436 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002437 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002438 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002439 if (port == -1) {
2440 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002441 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002442 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002443 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2444 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002445 if (port == -1) {
2446 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002447 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002448 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002449 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002450 }
2451 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002452
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002453 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2454 si = appctx->owner;
2455 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002456
2457 if (!sockaddr_alloc(&s->target_addr)) {
2458 xref_unlock(&socket->xref, peer);
2459 WILL_LJMP(luaL_error(L, "connect: internal error"));
2460 }
2461 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002462 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002463
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002464 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002465
2466 /* inform the stream that we want to be notified whenever the
2467 * connection completes.
2468 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002469 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002470 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002471 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002472
Willy Tarreauf31af932020-01-14 09:59:38 +01002473 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002474
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002475 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2476 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002477 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002478 }
2479 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002480
2481 task_wakeup(s->task, TASK_WOKEN_INIT);
2482 /* Return yield waiting for connection. */
2483
Willy Tarreau9635e032018-10-16 17:52:55 +02002484 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002485
2486 return 0;
2487}
2488
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002489#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002490__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2491{
2492 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002493 struct xref *peer;
2494 struct appctx *appctx;
2495 struct stream_interface *si;
2496 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002497
2498 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2499 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002500
2501 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002502 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002503 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002504 lua_pushnil(L);
2505 return 1;
2506 }
2507 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2508 si = appctx->owner;
2509 s = si_strm(si);
2510
2511 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002512 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002513 return MAY_LJMP(hlua_socket_connect(L));
2514}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002515#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002516
2517__LJMP static int hlua_socket_setoption(struct lua_State *L)
2518{
2519 return 0;
2520}
2521
2522__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2523{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002524 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002525 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002526 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002527 struct xref *peer;
2528 struct appctx *appctx;
2529 struct stream_interface *si;
2530 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002531
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002532 MAY_LJMP(check_args(L, 2, "settimeout"));
2533
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002534 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002535
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002536 /* convert the timeout to millis */
2537 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002538
Thierry Fournier17a921b2018-03-08 09:59:02 +01002539 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002540 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002541 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2542
Mark Lakes56cc1252018-03-27 09:48:06 +02002543 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002544 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002545
2546 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002547 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002548 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002549
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002550 /* Check if we run on the same thread than the xreator thread.
2551 * We cannot access to the socket if the thread is different.
2552 */
2553 if (socket->tid != tid)
2554 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2555
Mark Lakes56cc1252018-03-27 09:48:06 +02002556 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002557 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002558 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002559 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2560 WILL_LJMP(lua_error(L));
2561 return 0;
2562 }
2563 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2564 si = appctx->owner;
2565 s = si_strm(si);
2566
Cyril Bonté7bb63452018-08-17 23:51:02 +02002567 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002568 s->req.rto = tmout;
2569 s->req.wto = tmout;
2570 s->res.rto = tmout;
2571 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002572 s->req.rex = tick_add_ifset(now_ms, tmout);
2573 s->req.wex = tick_add_ifset(now_ms, tmout);
2574 s->res.rex = tick_add_ifset(now_ms, tmout);
2575 s->res.wex = tick_add_ifset(now_ms, tmout);
2576
2577 s->task->expire = tick_add_ifset(now_ms, tmout);
2578 task_queue(s->task);
2579
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002580 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002581
Thierry Fourniere9636f12018-03-08 09:54:32 +01002582 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002583 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002584}
2585
2586__LJMP static int hlua_socket_new(lua_State *L)
2587{
2588 struct hlua_socket *socket;
2589 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002590 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002591 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002592
2593 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002594 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002595 hlua_pusherror(L, "socket: full stack");
2596 goto out_fail_conf;
2597 }
2598
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002599 /* Create the object: obj[0] = userdata. */
2600 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002601 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002602 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002603 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002604 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002605
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002606 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002607 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002608 hlua_pusherror(L, "socket: uninitialized pools.");
2609 goto out_fail_conf;
2610 }
2611
Willy Tarreau87b09662015-04-03 00:22:06 +02002612 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002613 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2614 lua_setmetatable(L, -2);
2615
Willy Tarreaud420a972015-04-06 00:39:18 +02002616 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002617 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002618 if (!appctx) {
2619 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002620 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002621 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002622
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002623 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002624 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002625 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2626 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002627
Willy Tarreaud420a972015-04-06 00:39:18 +02002628 /* Now create a session, task and stream for this applet */
2629 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2630 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002631 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002632 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002633 }
2634
Willy Tarreau87787ac2017-08-28 16:22:54 +02002635 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002636 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002637 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002638 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002639 }
2640
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002641 /* Initialise cross reference between stream and Lua socket object. */
2642 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002643
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002644 /* Configure "right" stream interface. this "si" is used to connect
2645 * and retrieve data from the server. The connection is initialized
2646 * with the "struct server".
2647 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002648 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002649
2650 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002651 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002652 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002653
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002654 return 1;
2655
Willy Tarreaud420a972015-04-06 00:39:18 +02002656 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002657 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002658 out_fail_sess:
2659 appctx_free(appctx);
2660 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002661 WILL_LJMP(lua_error(L));
2662 return 0;
2663}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002664
2665/*
2666 *
2667 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002668 * Class Channel
2669 *
2670 *
2671 */
2672
2673/* Returns the struct hlua_channel join to the class channel in the
2674 * stack entry "ud" or throws an argument error.
2675 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002676__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002677{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002678 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002679}
2680
Willy Tarreau47860ed2015-03-10 14:07:50 +01002681/* Pushes the channel onto the top of the stack. If the stask does not have a
2682 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002683 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002684static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002685{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002686 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002687 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002688 return 0;
2689
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002690 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002691 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002692 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002693
2694 /* Pop a class sesison metatable and affect it to the userdata. */
2695 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2696 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002697 return 1;
2698}
2699
2700/* Duplicate all the data present in the input channel and put it
2701 * in a string LUA variables. Returns -1 and push a nil value in
2702 * the stack if the channel is closed and all the data are consumed,
2703 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002704 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002705 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002706static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002707{
2708 char *blk1;
2709 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002710 size_t len1;
2711 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002712 int ret;
2713 luaL_Buffer b;
2714
Willy Tarreau06d80a92017-10-19 14:32:15 +02002715 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002716 if (unlikely(ret == 0))
2717 return 0;
2718
2719 if (unlikely(ret < 0)) {
2720 lua_pushnil(L);
2721 return -1;
2722 }
2723
2724 luaL_buffinit(L, &b);
2725 luaL_addlstring(&b, blk1, len1);
2726 if (unlikely(ret == 2))
2727 luaL_addlstring(&b, blk2, len2);
2728 luaL_pushresult(&b);
2729
2730 if (unlikely(ret == 2))
2731 return len1 + len2;
2732 return len1;
2733}
2734
2735/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2736 * a yield. This function keep the data in the buffer.
2737 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002738__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002739{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002740 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002741
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002742 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2743
Christopher Faulet3f829a42018-12-13 21:56:45 +01002744 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2745 WILL_LJMP(lua_error(L));
2746
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002747 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002748 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002749 return 1;
2750}
2751
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002752/* Check arguments for the function "hlua_channel_dup_yield". */
2753__LJMP static int hlua_channel_dup(lua_State *L)
2754{
2755 MAY_LJMP(check_args(L, 1, "dup"));
2756 MAY_LJMP(hlua_checkchannel(L, 1));
2757 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2758}
2759
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002760/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2761 * a yield. This function consumes the data in the buffer. It returns
2762 * a string containing the data or a nil pointer if no data are available
2763 * and the channel is closed.
2764 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002765__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002766{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002767 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002768 int ret;
2769
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002770 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002771
Christopher Faulet3f829a42018-12-13 21:56:45 +01002772 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2773 WILL_LJMP(lua_error(L));
2774
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002775 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002776 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002777 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002778
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002779 if (unlikely(ret == -1))
2780 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002781
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002782 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002783 return 1;
2784}
2785
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002786/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002787__LJMP static int hlua_channel_get(lua_State *L)
2788{
2789 MAY_LJMP(check_args(L, 1, "get"));
2790 MAY_LJMP(hlua_checkchannel(L, 1));
2791 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2792}
2793
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002794/* This functions consumes and returns one line. If the channel is closed,
2795 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002796 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002797 * value.
2798 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002799__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002800{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002801 char *blk1;
2802 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002803 size_t len1;
2804 size_t len2;
2805 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002806 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002807 int ret;
2808 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002809
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002810 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2811
Christopher Faulet3f829a42018-12-13 21:56:45 +01002812 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2813 WILL_LJMP(lua_error(L));
2814
Willy Tarreau06d80a92017-10-19 14:32:15 +02002815 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002816 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002817 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002818
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002819 if (ret == -1) {
2820 lua_pushnil(L);
2821 return 1;
2822 }
2823
2824 luaL_buffinit(L, &b);
2825 luaL_addlstring(&b, blk1, len1);
2826 len = len1;
2827 if (unlikely(ret == 2)) {
2828 luaL_addlstring(&b, blk2, len2);
2829 len += len2;
2830 }
2831 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002832 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002833 return 1;
2834}
2835
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002836/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002837__LJMP static int hlua_channel_getline(lua_State *L)
2838{
2839 MAY_LJMP(check_args(L, 1, "getline"));
2840 MAY_LJMP(hlua_checkchannel(L, 1));
2841 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2842}
2843
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002844/* This function takes a string as input, and append it at the
2845 * input side of channel. If the data is too big, but a space
2846 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002847 * yields. If the data is bigger than the buffer, or if the
2848 * channel is closed, it returns -1. Otherwise, it returns the
2849 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002850 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002851__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002852{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002853 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002854 size_t len;
2855 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2856 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2857 int ret;
2858 int max;
2859
Christopher Faulet3f829a42018-12-13 21:56:45 +01002860 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2861 WILL_LJMP(lua_error(L));
2862
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002863 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002864 * the request buffer if its not required.
2865 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002866 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002867 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002868 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002869 }
2870
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002871 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002872 if (max > len - l)
2873 max = len - l;
2874
Willy Tarreau06d80a92017-10-19 14:32:15 +02002875 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002876 if (ret == -2 || ret == -3) {
2877 lua_pushinteger(L, -1);
2878 return 1;
2879 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002880 if (ret == -1) {
2881 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002882 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002883 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002884 l += ret;
2885 lua_pop(L, 1);
2886 lua_pushinteger(L, l);
2887
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002888 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002889 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002890 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002891 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002892 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002893 */
2894 return 1;
2895 }
2896 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002897 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002898 return 1;
2899}
2900
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002901/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2902 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002903 * buffer size is too little for the data.
2904 */
2905__LJMP static int hlua_channel_append(lua_State *L)
2906{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002907 size_t len;
2908
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002909 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002910 MAY_LJMP(hlua_checkchannel(L, 1));
2911 MAY_LJMP(luaL_checklstring(L, 2, &len));
2912 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002913 lua_pushinteger(L, 0);
2914
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002915 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002916}
2917
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002918/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002919 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002920 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002921 * or -1 if the channel is closed or if the buffer size is too
2922 * little for the data.
2923 */
2924__LJMP static int hlua_channel_set(lua_State *L)
2925{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002926 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002927
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002928 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002929 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002930 lua_pushinteger(L, 0);
2931
Christopher Faulet3f829a42018-12-13 21:56:45 +01002932 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2933 WILL_LJMP(lua_error(L));
2934
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002935 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002936
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002937 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002938}
2939
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002940/* Append data in the output side of the buffer. This data is immediately
2941 * sent. The function returns the amount of data written. If the buffer
2942 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002943 * if the channel is closed.
2944 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002945__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002946{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002947 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002948 size_t len;
2949 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2950 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2951 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002952 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002953
Christopher Faulet3f829a42018-12-13 21:56:45 +01002954 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2955 WILL_LJMP(lua_error(L));
2956
Willy Tarreau47860ed2015-03-10 14:07:50 +01002957 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002958 lua_pushinteger(L, -1);
2959 return 1;
2960 }
2961
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002962 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002963 * the request buffer if its not required.
2964 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002965 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002966 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002967 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002968 }
2969
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002970 /* The written data will be immediately sent, so we can check
2971 * the available space without taking in account the reserve.
2972 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002973 * data, because the buffer will be flushed.
2974 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002975 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002976
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002977 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002978 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002979 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002980 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02002981 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002982 return 1;
2983
2984 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002985 if (max > len - l)
2986 max = len - l;
2987
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002988 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002989 * detects a non contiguous buffer and realign it.
2990 */
Willy Tarreau3f679992018-06-15 15:06:42 +02002991 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002992 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002993
2994 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002995 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002996
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002997 /* buffer replace considers that the input part is filled.
2998 * so, I must forward these new data in the output part.
2999 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003000 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003001
3002 l += max;
3003 lua_pop(L, 1);
3004 lua_pushinteger(L, l);
3005
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003006 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003007 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003008 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003009 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003010 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003011 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003012 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003013
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003014 if (l < len) {
3015 /* If we are waiting for space in the response buffer, we
3016 * must set the flag WAKERESWR. This flag required the task
3017 * wake up if any activity is detected on the response buffer.
3018 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003019 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003020 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003021 else
3022 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003023 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003024 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003025
3026 return 1;
3027}
3028
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003029/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003030 * yield the LUA process, and resume it without checking the
3031 * input arguments.
3032 */
3033__LJMP static int hlua_channel_send(lua_State *L)
3034{
3035 MAY_LJMP(check_args(L, 2, "send"));
3036 lua_pushinteger(L, 0);
3037
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003038 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003039}
3040
3041/* This function forward and amount of butes. The data pass from
3042 * the input side of the buffer to the output side, and can be
3043 * forwarded. This function never fails.
3044 *
3045 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003046 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003047 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003048__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003049{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003050 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003051 int len;
3052 int l;
3053 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003054 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003055
3056 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003057
3058 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3059 WILL_LJMP(lua_error(L));
3060
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003061 len = MAY_LJMP(luaL_checkinteger(L, 2));
3062 l = MAY_LJMP(luaL_checkinteger(L, -1));
3063
3064 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003065 if (max > ci_data(chn))
3066 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003067 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003068 l += max;
3069
3070 lua_pop(L, 1);
3071 lua_pushinteger(L, l);
3072
3073 /* Check if it miss bytes to forward. */
3074 if (l < len) {
3075 /* The the input channel or the output channel are closed, we
3076 * must return the amount of data forwarded.
3077 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003078 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003079 return 1;
3080
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003081 /* If we are waiting for space data in the response buffer, we
3082 * must set the flag WAKERESWR. This flag required the task
3083 * wake up if any activity is detected on the response buffer.
3084 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003085 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003086 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003087 else
3088 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003089
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003090 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003091 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003092 }
3093
3094 return 1;
3095}
3096
3097/* Just check the input and prepare the stack for the previous
3098 * function "hlua_channel_forward_yield"
3099 */
3100__LJMP static int hlua_channel_forward(lua_State *L)
3101{
3102 MAY_LJMP(check_args(L, 2, "forward"));
3103 MAY_LJMP(hlua_checkchannel(L, 1));
3104 MAY_LJMP(luaL_checkinteger(L, 2));
3105
3106 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003107 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003108}
3109
3110/* Just returns the number of bytes available in the input
3111 * side of the buffer. This function never fails.
3112 */
3113__LJMP static int hlua_channel_get_in_len(lua_State *L)
3114{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003115 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003116
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003117 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003118 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003119 if (IS_HTX_STRM(chn_strm(chn))) {
3120 struct htx *htx = htxbuf(&chn->buf);
3121 lua_pushinteger(L, htx->data - co_data(chn));
3122 }
3123 else
3124 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003125 return 1;
3126}
3127
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003128/* Returns true if the channel is full. */
3129__LJMP static int hlua_channel_is_full(lua_State *L)
3130{
3131 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003132
3133 MAY_LJMP(check_args(L, 1, "is_full"));
3134 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003135 /* ignore the reserve, we are not on a producer side (ie in an
3136 * applet).
3137 */
3138 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003139 return 1;
3140}
3141
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003142/* Returns true if the channel is the response channel. */
3143__LJMP static int hlua_channel_is_resp(lua_State *L)
3144{
3145 struct channel *chn;
3146
3147 MAY_LJMP(check_args(L, 1, "is_resp"));
3148 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3149
3150 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3151 return 1;
3152}
3153
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003154/* Just returns the number of bytes available in the output
3155 * side of the buffer. This function never fails.
3156 */
3157__LJMP static int hlua_channel_get_out_len(lua_State *L)
3158{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003159 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003160
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003161 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003162 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003163 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003164 return 1;
3165}
3166
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003167/*
3168 *
3169 *
3170 * Class Fetches
3171 *
3172 *
3173 */
3174
3175/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003176 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003177 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003178__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003179{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003180 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003181}
3182
3183/* This function creates and push in the stack a fetch object according
3184 * with a current TXN.
3185 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003186static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003187{
Willy Tarreau7073c472015-04-06 11:15:40 +02003188 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003189
3190 /* Check stack size. */
3191 if (!lua_checkstack(L, 3))
3192 return 0;
3193
3194 /* Create the object: obj[0] = userdata.
3195 * Note that the base of the Fetches object is the
3196 * transaction object.
3197 */
3198 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003199 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003200 lua_rawseti(L, -2, 0);
3201
Willy Tarreau7073c472015-04-06 11:15:40 +02003202 hsmp->s = txn->s;
3203 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003204 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003205 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003206
3207 /* Pop a class sesison metatable and affect it to the userdata. */
3208 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3209 lua_setmetatable(L, -2);
3210
3211 return 1;
3212}
3213
3214/* This function is an LUA binding. It is called with each sample-fetch.
3215 * It uses closure argument to store the associated sample-fetch. It
3216 * returns only one argument or throws an error. An error is thrown
3217 * only if an error is encountered during the argument parsing. If
3218 * the "sample-fetch" function fails, nil is returned.
3219 */
3220__LJMP static int hlua_run_sample_fetch(lua_State *L)
3221{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003222 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003223 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003224 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003225 int i;
3226 struct sample smp;
3227
3228 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003229 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003230
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003231 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003232 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003233
Thierry FOURNIERca988662015-12-20 18:43:03 +01003234 /* Check execution authorization. */
3235 if (f->use & SMP_USE_HTTP_ANY &&
3236 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3237 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3238 "is not available in Lua services", f->kw);
3239 WILL_LJMP(lua_error(L));
3240 }
3241
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003242 /* Get extra arguments. */
3243 for (i = 0; i < lua_gettop(L) - 1; i++) {
3244 if (i >= ARGM_NBARGS)
3245 break;
3246 hlua_lua2arg(L, i + 2, &args[i]);
3247 }
3248 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003249 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003250
3251 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003252 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003253
3254 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003255 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003256 lua_pushfstring(L, "error in arguments");
3257 WILL_LJMP(lua_error(L));
3258 }
3259
3260 /* Initialise the sample. */
3261 memset(&smp, 0, sizeof(smp));
3262
3263 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003264 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003265 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003266 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003267 lua_pushstring(L, "");
3268 else
3269 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003270 return 1;
3271 }
3272
3273 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003274 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003275 hlua_smp2lua_str(L, &smp);
3276 else
3277 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003278 return 1;
3279}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003280
3281/*
3282 *
3283 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003284 * Class Converters
3285 *
3286 *
3287 */
3288
3289/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003290 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003291 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003292__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003293{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003294 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003295}
3296
3297/* This function creates and push in the stack a Converters object
3298 * according with a current TXN.
3299 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003300static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003301{
Willy Tarreau7073c472015-04-06 11:15:40 +02003302 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003303
3304 /* Check stack size. */
3305 if (!lua_checkstack(L, 3))
3306 return 0;
3307
3308 /* Create the object: obj[0] = userdata.
3309 * Note that the base of the Converters object is the
3310 * same than the TXN object.
3311 */
3312 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003313 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003314 lua_rawseti(L, -2, 0);
3315
Willy Tarreau7073c472015-04-06 11:15:40 +02003316 hsmp->s = txn->s;
3317 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003318 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003319 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003320
Willy Tarreau87b09662015-04-03 00:22:06 +02003321 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003322 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3323 lua_setmetatable(L, -2);
3324
3325 return 1;
3326}
3327
3328/* This function is an LUA binding. It is called with each converter.
3329 * It uses closure argument to store the associated converter. It
3330 * returns only one argument or throws an error. An error is thrown
3331 * only if an error is encountered during the argument parsing. If
3332 * the converter function function fails, nil is returned.
3333 */
3334__LJMP static int hlua_run_sample_conv(lua_State *L)
3335{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003336 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003337 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003338 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003339 int i;
3340 struct sample smp;
3341
3342 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003343 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003344
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003345 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003346 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003347
3348 /* Get extra arguments. */
3349 for (i = 0; i < lua_gettop(L) - 2; i++) {
3350 if (i >= ARGM_NBARGS)
3351 break;
3352 hlua_lua2arg(L, i + 3, &args[i]);
3353 }
3354 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003355 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003356
3357 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003358 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003359
3360 /* Run the special args checker. */
3361 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3362 hlua_pusherror(L, "error in arguments");
3363 WILL_LJMP(lua_error(L));
3364 }
3365
3366 /* Initialise the sample. */
3367 if (!hlua_lua2smp(L, 2, &smp)) {
3368 hlua_pusherror(L, "error in the input argument");
3369 WILL_LJMP(lua_error(L));
3370 }
3371
Willy Tarreau1777ea62016-03-10 16:15:46 +01003372 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3373
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003374 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003375 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003376 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003377 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003378 WILL_LJMP(lua_error(L));
3379 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003380 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3381 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003382 hlua_pusherror(L, "error during the input argument casting");
3383 WILL_LJMP(lua_error(L));
3384 }
3385
3386 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003387 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003388 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003389 lua_pushstring(L, "");
3390 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003391 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003392 return 1;
3393 }
3394
3395 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003396 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003397 hlua_smp2lua_str(L, &smp);
3398 else
3399 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003400 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003401}
3402
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003403/*
3404 *
3405 *
3406 * Class AppletTCP
3407 *
3408 *
3409 */
3410
3411/* Returns a struct hlua_txn if the stack entry "ud" is
3412 * a class stream, otherwise it throws an error.
3413 */
3414__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3415{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003416 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003417}
3418
3419/* This function creates and push in the stack an Applet object
3420 * according with a current TXN.
3421 */
3422static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3423{
3424 struct hlua_appctx *appctx;
3425 struct stream_interface *si = ctx->owner;
3426 struct stream *s = si_strm(si);
3427 struct proxy *p = s->be;
3428
3429 /* Check stack size. */
3430 if (!lua_checkstack(L, 3))
3431 return 0;
3432
3433 /* Create the object: obj[0] = userdata.
3434 * Note that the base of the Converters object is the
3435 * same than the TXN object.
3436 */
3437 lua_newtable(L);
3438 appctx = lua_newuserdata(L, sizeof(*appctx));
3439 lua_rawseti(L, -2, 0);
3440 appctx->appctx = ctx;
3441 appctx->htxn.s = s;
3442 appctx->htxn.p = p;
3443
3444 /* Create the "f" field that contains a list of fetches. */
3445 lua_pushstring(L, "f");
3446 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3447 return 0;
3448 lua_settable(L, -3);
3449
3450 /* Create the "sf" field that contains a list of stringsafe fetches. */
3451 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003452 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003453 return 0;
3454 lua_settable(L, -3);
3455
3456 /* Create the "c" field that contains a list of converters. */
3457 lua_pushstring(L, "c");
3458 if (!hlua_converters_new(L, &appctx->htxn, 0))
3459 return 0;
3460 lua_settable(L, -3);
3461
3462 /* Create the "sc" field that contains a list of stringsafe converters. */
3463 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003464 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003465 return 0;
3466 lua_settable(L, -3);
3467
3468 /* Pop a class stream metatable and affect it to the table. */
3469 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3470 lua_setmetatable(L, -2);
3471
3472 return 1;
3473}
3474
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003475__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3476{
3477 struct hlua_appctx *appctx;
3478 struct stream *s;
3479 const char *name;
3480 size_t len;
3481 struct sample smp;
3482
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003483 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3484 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003485
3486 /* It is useles to retrieve the stream, but this function
3487 * runs only in a stream context.
3488 */
3489 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3490 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3491 s = appctx->htxn.s;
3492
3493 /* Converts the third argument in a sample. */
3494 hlua_lua2smp(L, 3, &smp);
3495
3496 /* Store the sample in a variable. */
3497 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003498
3499 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3500 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3501 else
3502 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3503
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003504 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003505}
3506
3507__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3508{
3509 struct hlua_appctx *appctx;
3510 struct stream *s;
3511 const char *name;
3512 size_t len;
3513 struct sample smp;
3514
3515 MAY_LJMP(check_args(L, 2, "unset_var"));
3516
3517 /* It is useles to retrieve the stream, but this function
3518 * runs only in a stream context.
3519 */
3520 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3521 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3522 s = appctx->htxn.s;
3523
3524 /* Unset the variable. */
3525 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003526 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3527 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003528}
3529
3530__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3531{
3532 struct hlua_appctx *appctx;
3533 struct stream *s;
3534 const char *name;
3535 size_t len;
3536 struct sample smp;
3537
3538 MAY_LJMP(check_args(L, 2, "get_var"));
3539
3540 /* It is useles to retrieve the stream, but this function
3541 * runs only in a stream context.
3542 */
3543 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3544 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3545 s = appctx->htxn.s;
3546
3547 smp_set_owner(&smp, s->be, s->sess, s, 0);
3548 if (!vars_get_by_name(name, len, &smp)) {
3549 lua_pushnil(L);
3550 return 1;
3551 }
3552
3553 return hlua_smp2lua(L, &smp);
3554}
3555
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003556__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3557{
3558 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3559 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003560 struct hlua *hlua;
3561
3562 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003563 if (!s->hlua)
3564 return 0;
3565 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003566
3567 MAY_LJMP(check_args(L, 2, "set_priv"));
3568
3569 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003570 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003571
3572 /* Get and store new value. */
3573 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3574 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3575
3576 return 0;
3577}
3578
3579__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3580{
3581 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3582 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003583 struct hlua *hlua;
3584
3585 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003586 if (!s->hlua) {
3587 lua_pushnil(L);
3588 return 1;
3589 }
3590 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003591
3592 /* Push configuration index in the stack. */
3593 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3594
3595 return 1;
3596}
3597
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003598/* If expected data not yet available, it returns a yield. This function
3599 * consumes the data in the buffer. It returns a string containing the
3600 * data. This string can be empty.
3601 */
3602__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3603{
3604 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3605 struct stream_interface *si = appctx->appctx->owner;
3606 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003607 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003608 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003609 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003610 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003611
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003612 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003613 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003614
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003615 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003616 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003617 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003618 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003619 }
3620
3621 /* End of data: commit the total strings and return. */
3622 if (ret < 0) {
3623 luaL_pushresult(&appctx->b);
3624 return 1;
3625 }
3626
3627 /* Ensure that the block 2 length is usable. */
3628 if (ret == 1)
3629 len2 = 0;
3630
3631 /* dont check the max length read and dont check. */
3632 luaL_addlstring(&appctx->b, blk1, len1);
3633 luaL_addlstring(&appctx->b, blk2, len2);
3634
3635 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003636 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003637 luaL_pushresult(&appctx->b);
3638 return 1;
3639}
3640
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003641/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003642__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3643{
3644 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3645
3646 /* Initialise the string catenation. */
3647 luaL_buffinit(L, &appctx->b);
3648
3649 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3650}
3651
3652/* If expected data not yet available, it returns a yield. This function
3653 * consumes the data in the buffer. It returns a string containing the
3654 * data. This string can be empty.
3655 */
3656__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3657{
3658 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3659 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003660 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003661 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003662 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003663 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003664 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003665 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003666
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003667 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003668 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003669
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003670 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003671 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003672 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003673 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003674 }
3675
3676 /* End of data: commit the total strings and return. */
3677 if (ret < 0) {
3678 luaL_pushresult(&appctx->b);
3679 return 1;
3680 }
3681
3682 /* Ensure that the block 2 length is usable. */
3683 if (ret == 1)
3684 len2 = 0;
3685
3686 if (len == -1) {
3687
3688 /* If len == -1, catenate all the data avalaile and
3689 * yield because we want to get all the data until
3690 * the end of data stream.
3691 */
3692 luaL_addlstring(&appctx->b, blk1, len1);
3693 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003694 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003695 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003696 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003697
3698 } else {
3699
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003700 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003701 if (len1 > len)
3702 len1 = len;
3703 luaL_addlstring(&appctx->b, blk1, len1);
3704 len -= len1;
3705
3706 /* Copy the second block. */
3707 if (len2 > len)
3708 len2 = len;
3709 luaL_addlstring(&appctx->b, blk2, len2);
3710 len -= len2;
3711
3712 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003713 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003714
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003715 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003716 if (len > 0) {
3717 lua_pushinteger(L, len);
3718 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003719 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003720 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003721 }
3722
3723 /* return the result. */
3724 luaL_pushresult(&appctx->b);
3725 return 1;
3726 }
3727
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003728 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003729 hlua_pusherror(L, "Lua: internal error");
3730 WILL_LJMP(lua_error(L));
3731 return 0;
3732}
3733
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003734/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003735__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3736{
3737 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3738 int len = -1;
3739
3740 if (lua_gettop(L) > 2)
3741 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3742 if (lua_gettop(L) >= 2) {
3743 len = MAY_LJMP(luaL_checkinteger(L, 2));
3744 lua_pop(L, 1);
3745 }
3746
3747 /* Confirm or set the required length */
3748 lua_pushinteger(L, len);
3749
3750 /* Initialise the string catenation. */
3751 luaL_buffinit(L, &appctx->b);
3752
3753 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3754}
3755
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003756/* Append data in the output side of the buffer. This data is immediately
3757 * sent. The function returns the amount of data written. If the buffer
3758 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003759 * if the channel is closed.
3760 */
3761__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3762{
3763 size_t len;
3764 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3765 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3766 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3767 struct stream_interface *si = appctx->appctx->owner;
3768 struct channel *chn = si_ic(si);
3769 int max;
3770
3771 /* Get the max amount of data which can write as input in the channel. */
3772 max = channel_recv_max(chn);
3773 if (max > (len - l))
3774 max = len - l;
3775
3776 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003777 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003778
3779 /* update counters. */
3780 l += max;
3781 lua_pop(L, 1);
3782 lua_pushinteger(L, l);
3783
3784 /* If some data is not send, declares the situation to the
3785 * applet, and returns a yield.
3786 */
3787 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003788 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003789 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003790 }
3791
3792 return 1;
3793}
3794
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003795/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003796 * yield the LUA process, and resume it without checking the
3797 * input arguments.
3798 */
3799__LJMP static int hlua_applet_tcp_send(lua_State *L)
3800{
3801 MAY_LJMP(check_args(L, 2, "send"));
3802 lua_pushinteger(L, 0);
3803
3804 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3805}
3806
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003807/*
3808 *
3809 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003810 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003811 *
3812 *
3813 */
3814
3815/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003816 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003817 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003818__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003819{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003820 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003821}
3822
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003823/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003824 * according with a current TXN.
3825 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003826static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003827{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003828 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003829 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003830 struct stream_interface *si = ctx->owner;
3831 struct stream *s = si_strm(si);
3832 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003833 struct htx *htx;
3834 struct htx_blk *blk;
3835 struct htx_sl *sl;
3836 struct ist path;
3837 unsigned long long len = 0;
3838 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003839
3840 /* Check stack size. */
3841 if (!lua_checkstack(L, 3))
3842 return 0;
3843
3844 /* Create the object: obj[0] = userdata.
3845 * Note that the base of the Converters object is the
3846 * same than the TXN object.
3847 */
3848 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003849 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003850 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003851 appctx->appctx = ctx;
3852 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003853 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003854 appctx->htxn.s = s;
3855 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003856
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003857 /* Create the "f" field that contains a list of fetches. */
3858 lua_pushstring(L, "f");
3859 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3860 return 0;
3861 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003862
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003863 /* Create the "sf" field that contains a list of stringsafe fetches. */
3864 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003865 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003866 return 0;
3867 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003868
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003869 /* Create the "c" field that contains a list of converters. */
3870 lua_pushstring(L, "c");
3871 if (!hlua_converters_new(L, &appctx->htxn, 0))
3872 return 0;
3873 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003874
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003875 /* Create the "sc" field that contains a list of stringsafe converters. */
3876 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003877 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003878 return 0;
3879 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003880
Christopher Fauleta2097962019-07-15 16:25:33 +02003881 htx = htxbuf(&s->req.buf);
3882 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003883 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003884 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003885
Christopher Fauleta2097962019-07-15 16:25:33 +02003886 /* Stores the request method. */
3887 lua_pushstring(L, "method");
3888 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3889 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003890
Christopher Fauleta2097962019-07-15 16:25:33 +02003891 /* Stores the http version. */
3892 lua_pushstring(L, "version");
3893 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3894 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003895
Christopher Fauleta2097962019-07-15 16:25:33 +02003896 /* creates an array of headers. hlua_http_get_headers() crates and push
3897 * the array on the top of the stack.
3898 */
3899 lua_pushstring(L, "headers");
3900 htxn.s = s;
3901 htxn.p = px;
3902 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01003903 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02003904 return 0;
3905 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003906
Christopher Fauleta2097962019-07-15 16:25:33 +02003907 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01003908 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02003909 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003910
Christopher Fauleta2097962019-07-15 16:25:33 +02003911 p = path.ptr;
3912 end = path.ptr + path.len;
3913 q = p;
3914 while (q < end && *q != '?')
3915 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003916
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003917 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003918 lua_pushstring(L, "path");
3919 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003920 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003921
Christopher Fauleta2097962019-07-15 16:25:33 +02003922 /* Stores the query string. */
3923 lua_pushstring(L, "qs");
3924 if (*q == '?')
3925 q++;
3926 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003927 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003928 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003929
Christopher Fauleta2097962019-07-15 16:25:33 +02003930 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3931 struct htx_blk *blk = htx_get_blk(htx, pos);
3932 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003933
Christopher Fauleta2097962019-07-15 16:25:33 +02003934 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3935 break;
3936 if (type == HTX_BLK_DATA)
3937 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003938 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003939 if (htx->extra != ULLONG_MAX)
3940 len += htx->extra;
3941
3942 /* Stores the request path. */
3943 lua_pushstring(L, "length");
3944 lua_pushinteger(L, len);
3945 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003946
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003947 /* Create an empty array of HTTP request headers. */
3948 lua_pushstring(L, "response");
3949 lua_newtable(L);
3950 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003951
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003952 /* Pop a class stream metatable and affect it to the table. */
3953 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3954 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003955
3956 return 1;
3957}
3958
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003959__LJMP static int hlua_applet_http_set_var(lua_State *L)
3960{
3961 struct hlua_appctx *appctx;
3962 struct stream *s;
3963 const char *name;
3964 size_t len;
3965 struct sample smp;
3966
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003967 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3968 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003969
3970 /* It is useles to retrieve the stream, but this function
3971 * runs only in a stream context.
3972 */
3973 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3974 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3975 s = appctx->htxn.s;
3976
3977 /* Converts the third argument in a sample. */
3978 hlua_lua2smp(L, 3, &smp);
3979
3980 /* Store the sample in a variable. */
3981 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003982
3983 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3984 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3985 else
3986 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3987
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003988 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003989}
3990
3991__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3992{
3993 struct hlua_appctx *appctx;
3994 struct stream *s;
3995 const char *name;
3996 size_t len;
3997 struct sample smp;
3998
3999 MAY_LJMP(check_args(L, 2, "unset_var"));
4000
4001 /* It is useles to retrieve the stream, but this function
4002 * runs only in a stream context.
4003 */
4004 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4005 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4006 s = appctx->htxn.s;
4007
4008 /* Unset the variable. */
4009 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004010 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4011 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004012}
4013
4014__LJMP static int hlua_applet_http_get_var(lua_State *L)
4015{
4016 struct hlua_appctx *appctx;
4017 struct stream *s;
4018 const char *name;
4019 size_t len;
4020 struct sample smp;
4021
4022 MAY_LJMP(check_args(L, 2, "get_var"));
4023
4024 /* It is useles to retrieve the stream, but this function
4025 * runs only in a stream context.
4026 */
4027 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4028 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4029 s = appctx->htxn.s;
4030
4031 smp_set_owner(&smp, s->be, s->sess, s, 0);
4032 if (!vars_get_by_name(name, len, &smp)) {
4033 lua_pushnil(L);
4034 return 1;
4035 }
4036
4037 return hlua_smp2lua(L, &smp);
4038}
4039
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004040__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4041{
4042 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4043 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004044 struct hlua *hlua;
4045
4046 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004047 if (!s->hlua)
4048 return 0;
4049 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004050
4051 MAY_LJMP(check_args(L, 2, "set_priv"));
4052
4053 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004054 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004055
4056 /* Get and store new value. */
4057 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4058 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4059
4060 return 0;
4061}
4062
4063__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4064{
4065 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4066 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004067 struct hlua *hlua;
4068
4069 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004070 if (!s->hlua) {
4071 lua_pushnil(L);
4072 return 1;
4073 }
4074 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004075
4076 /* Push configuration index in the stack. */
4077 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4078
4079 return 1;
4080}
4081
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004082/* If expected data not yet available, it returns a yield. This function
4083 * consumes the data in the buffer. It returns a string containing the
4084 * data. This string can be empty.
4085 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004086__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004087{
4088 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4089 struct stream_interface *si = appctx->appctx->owner;
4090 struct channel *req = si_oc(si);
4091 struct htx *htx;
4092 struct htx_blk *blk;
4093 size_t count;
4094 int stop = 0;
4095
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004096 htx = htx_from_buf(&req->buf);
4097 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004098 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004099
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004100 while (count && !stop && blk) {
4101 enum htx_blk_type type = htx_get_blk_type(blk);
4102 uint32_t sz = htx_get_blksz(blk);
4103 struct ist v;
4104 uint32_t vlen;
4105 char *nl;
4106
Christopher Faulete461e342018-12-18 16:43:35 +01004107 if (type == HTX_BLK_EOM) {
4108 stop = 1;
4109 break;
4110 }
4111
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004112 vlen = sz;
4113 if (vlen > count) {
4114 if (type != HTX_BLK_DATA)
4115 break;
4116 vlen = count;
4117 }
4118
4119 switch (type) {
4120 case HTX_BLK_UNUSED:
4121 break;
4122
4123 case HTX_BLK_DATA:
4124 v = htx_get_blk_value(htx, blk);
4125 v.len = vlen;
4126 nl = istchr(v, '\n');
4127 if (nl != NULL) {
4128 stop = 1;
4129 vlen = nl - v.ptr + 1;
4130 }
4131 luaL_addlstring(&appctx->b, v.ptr, vlen);
4132 break;
4133
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004134 case HTX_BLK_TLR:
4135 case HTX_BLK_EOM:
4136 stop = 1;
4137 break;
4138
4139 default:
4140 break;
4141 }
4142
4143 co_set_data(req, co_data(req) - vlen);
4144 count -= vlen;
4145 if (sz == vlen)
4146 blk = htx_remove_blk(htx, blk);
4147 else {
4148 htx_cut_data_blk(htx, blk, vlen);
4149 break;
4150 }
4151 }
4152
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004153 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004154 if (!stop) {
4155 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004156 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004157 }
4158
4159 /* return the result. */
4160 luaL_pushresult(&appctx->b);
4161 return 1;
4162}
4163
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004164
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004165/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004166__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004167{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004168 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004169
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004170 /* Initialise the string catenation. */
4171 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004172
Christopher Fauleta2097962019-07-15 16:25:33 +02004173 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004174}
4175
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004176/* If expected data not yet available, it returns a yield. This function
4177 * consumes the data in the buffer. It returns a string containing the
4178 * data. This string can be empty.
4179 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004180__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004181{
4182 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4183 struct stream_interface *si = appctx->appctx->owner;
4184 struct channel *req = si_oc(si);
4185 struct htx *htx;
4186 struct htx_blk *blk;
4187 size_t count;
4188 int len;
4189
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004190 htx = htx_from_buf(&req->buf);
4191 len = MAY_LJMP(luaL_checkinteger(L, 2));
4192 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004193 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004194 while (count && len && blk) {
4195 enum htx_blk_type type = htx_get_blk_type(blk);
4196 uint32_t sz = htx_get_blksz(blk);
4197 struct ist v;
4198 uint32_t vlen;
4199
Christopher Faulete461e342018-12-18 16:43:35 +01004200 if (type == HTX_BLK_EOM) {
4201 len = 0;
4202 break;
4203 }
4204
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004205 vlen = sz;
4206 if (len > 0 && vlen > len)
4207 vlen = len;
4208 if (vlen > count) {
4209 if (type != HTX_BLK_DATA)
4210 break;
4211 vlen = count;
4212 }
4213
4214 switch (type) {
4215 case HTX_BLK_UNUSED:
4216 break;
4217
4218 case HTX_BLK_DATA:
4219 v = htx_get_blk_value(htx, blk);
4220 luaL_addlstring(&appctx->b, v.ptr, vlen);
4221 break;
4222
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004223 case HTX_BLK_TLR:
4224 case HTX_BLK_EOM:
4225 len = 0;
4226 break;
4227
4228 default:
4229 break;
4230 }
4231
4232 co_set_data(req, co_data(req) - vlen);
4233 count -= vlen;
4234 if (len > 0)
4235 len -= vlen;
4236 if (sz == vlen)
4237 blk = htx_remove_blk(htx, blk);
4238 else {
4239 htx_cut_data_blk(htx, blk, vlen);
4240 break;
4241 }
4242 }
4243
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004244 htx_to_buf(htx, &req->buf);
4245
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004246 /* If we are no other data available, yield waiting for new data. */
4247 if (len) {
4248 if (len > 0) {
4249 lua_pushinteger(L, len);
4250 lua_replace(L, 2);
4251 }
4252 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004253 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004254 }
4255
4256 /* return the result. */
4257 luaL_pushresult(&appctx->b);
4258 return 1;
4259}
4260
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004261/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004262__LJMP static int hlua_applet_http_recv(lua_State *L)
4263{
4264 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4265 int len = -1;
4266
4267 /* Check arguments. */
4268 if (lua_gettop(L) > 2)
4269 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4270 if (lua_gettop(L) >= 2) {
4271 len = MAY_LJMP(luaL_checkinteger(L, 2));
4272 lua_pop(L, 1);
4273 }
4274
Christopher Fauleta2097962019-07-15 16:25:33 +02004275 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004276
Christopher Fauleta2097962019-07-15 16:25:33 +02004277 /* Initialise the string catenation. */
4278 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004279
Christopher Fauleta2097962019-07-15 16:25:33 +02004280 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004281}
4282
4283/* Append data in the output side of the buffer. This data is immediately
4284 * sent. The function returns the amount of data written. If the buffer
4285 * cannot contain the data, the function yields. The function returns -1
4286 * if the channel is closed.
4287 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004288__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004289{
4290 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4291 struct stream_interface *si = appctx->appctx->owner;
4292 struct channel *res = si_ic(si);
4293 struct htx *htx = htx_from_buf(&res->buf);
4294 const char *data;
4295 size_t len;
4296 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4297 int max;
4298
Christopher Faulet9060fc02019-07-03 11:39:30 +02004299 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004300 if (!max)
4301 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004302
4303 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4304
4305 /* Get the max amount of data which can write as input in the channel. */
4306 if (max > (len - l))
4307 max = len - l;
4308
4309 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004310 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004311 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004312
4313 /* update counters. */
4314 l += max;
4315 lua_pop(L, 1);
4316 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004317
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004318 /* If some data is not send, declares the situation to the
4319 * applet, and returns a yield.
4320 */
4321 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004322 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004323 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004324 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004325 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004326 }
4327
Christopher Fauleta2097962019-07-15 16:25:33 +02004328 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004329 return 1;
4330}
4331
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004332/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004333 * yield the LUA process, and resume it without checking the
4334 * input arguments.
4335 */
4336__LJMP static int hlua_applet_http_send(lua_State *L)
4337{
4338 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004339
4340 /* We want to send some data. Headers must be sent. */
4341 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4342 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4343 WILL_LJMP(lua_error(L));
4344 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004345
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004346 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004347 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004348
Christopher Fauleta2097962019-07-15 16:25:33 +02004349 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004350}
4351
4352__LJMP static int hlua_applet_http_addheader(lua_State *L)
4353{
4354 const char *name;
4355 int ret;
4356
4357 MAY_LJMP(hlua_checkapplet_http(L, 1));
4358 name = MAY_LJMP(luaL_checkstring(L, 2));
4359 MAY_LJMP(luaL_checkstring(L, 3));
4360
4361 /* Push in the stack the "response" entry. */
4362 ret = lua_getfield(L, 1, "response");
4363 if (ret != LUA_TTABLE) {
4364 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4365 "is expected as an array. %s found", lua_typename(L, ret));
4366 WILL_LJMP(lua_error(L));
4367 }
4368
4369 /* check if the header is already registered if it is not
4370 * the case, register it.
4371 */
4372 ret = lua_getfield(L, -1, name);
4373 if (ret == LUA_TNIL) {
4374
4375 /* Entry not found. */
4376 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4377
4378 /* Insert the new header name in the array in the top of the stack.
4379 * It left the new array in the top of the stack.
4380 */
4381 lua_newtable(L);
4382 lua_pushvalue(L, 2);
4383 lua_pushvalue(L, -2);
4384 lua_settable(L, -4);
4385
4386 } else if (ret != LUA_TTABLE) {
4387
4388 /* corruption error. */
4389 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4390 "is expected as an array. %s found", name, lua_typename(L, ret));
4391 WILL_LJMP(lua_error(L));
4392 }
4393
4394 /* Now the top od thestack is an array of values. We push
4395 * the header value as new entry.
4396 */
4397 lua_pushvalue(L, 3);
4398 ret = lua_rawlen(L, -2);
4399 lua_rawseti(L, -2, ret + 1);
4400 lua_pushboolean(L, 1);
4401 return 1;
4402}
4403
4404__LJMP static int hlua_applet_http_status(lua_State *L)
4405{
4406 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4407 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004408 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004409
4410 if (status < 100 || status > 599) {
4411 lua_pushboolean(L, 0);
4412 return 1;
4413 }
4414
4415 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004416 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004417 lua_pushboolean(L, 1);
4418 return 1;
4419}
4420
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004421
Christopher Fauleta2097962019-07-15 16:25:33 +02004422__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004423{
4424 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4425 struct stream_interface *si = appctx->appctx->owner;
4426 struct channel *res = si_ic(si);
4427 struct htx *htx;
4428 struct htx_sl *sl;
4429 struct h1m h1m;
4430 const char *status, *reason;
4431 const char *name, *value;
4432 size_t nlen, vlen;
4433 unsigned int flags;
4434
4435 /* Send the message at once. */
4436 htx = htx_from_buf(&res->buf);
4437 h1m_init_res(&h1m);
4438
4439 /* Use the same http version than the request. */
4440 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4441 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4442 if (reason == NULL)
4443 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4444 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4445 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4446 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4447 }
4448 else {
4449 flags = HTX_SL_F_IS_RESP;
4450 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4451 }
4452 if (!sl) {
4453 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4454 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4455 WILL_LJMP(lua_error(L));
4456 }
4457 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4458
4459 /* Get the array associated to the field "response" in the object AppletHTTP. */
4460 lua_pushvalue(L, 0);
4461 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4462 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4463 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4464 WILL_LJMP(lua_error(L));
4465 }
4466
4467 /* Browse the list of headers. */
4468 lua_pushnil(L);
4469 while(lua_next(L, -2) != 0) {
4470 /* We expect a string as -2. */
4471 if (lua_type(L, -2) != LUA_TSTRING) {
4472 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4473 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4474 lua_typename(L, lua_type(L, -2)));
4475 WILL_LJMP(lua_error(L));
4476 }
4477 name = lua_tolstring(L, -2, &nlen);
4478
4479 /* We expect an array as -1. */
4480 if (lua_type(L, -1) != LUA_TTABLE) {
4481 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4482 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4483 name,
4484 lua_typename(L, lua_type(L, -1)));
4485 WILL_LJMP(lua_error(L));
4486 }
4487
4488 /* Browse the table who is on the top of the stack. */
4489 lua_pushnil(L);
4490 while(lua_next(L, -2) != 0) {
4491 int id;
4492
4493 /* We expect a number as -2. */
4494 if (lua_type(L, -2) != LUA_TNUMBER) {
4495 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4496 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4497 name,
4498 lua_typename(L, lua_type(L, -2)));
4499 WILL_LJMP(lua_error(L));
4500 }
4501 id = lua_tointeger(L, -2);
4502
4503 /* We expect a string as -2. */
4504 if (lua_type(L, -1) != LUA_TSTRING) {
4505 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4506 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4507 name, id,
4508 lua_typename(L, lua_type(L, -1)));
4509 WILL_LJMP(lua_error(L));
4510 }
4511 value = lua_tolstring(L, -1, &vlen);
4512
4513 /* Simple Protocol checks. */
4514 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004515 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004516 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4517 struct ist v = ist2(value, vlen);
4518 int ret;
4519
4520 ret = h1_parse_cont_len_header(&h1m, &v);
4521 if (ret < 0) {
4522 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4523 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4524 name);
4525 WILL_LJMP(lua_error(L));
4526 }
4527 else if (ret == 0)
4528 goto next; /* Skip it */
4529 }
4530
4531 /* Add a new header */
4532 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4533 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4534 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4535 name);
4536 WILL_LJMP(lua_error(L));
4537 }
4538 next:
4539 /* Remove the array from the stack, and get next element with a remaining string. */
4540 lua_pop(L, 1);
4541 }
4542
4543 /* Remove the array from the stack, and get next element with a remaining string. */
4544 lua_pop(L, 1);
4545 }
4546
4547 if (h1m.flags & H1_MF_CHNK)
4548 h1m.flags &= ~H1_MF_CLEN;
4549 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4550 h1m.flags |= H1_MF_XFER_LEN;
4551
4552 /* Uset HTX start-line flags */
4553 if (h1m.flags & H1_MF_XFER_ENC)
4554 flags |= HTX_SL_F_XFER_ENC;
4555 if (h1m.flags & H1_MF_XFER_LEN) {
4556 flags |= HTX_SL_F_XFER_LEN;
4557 if (h1m.flags & H1_MF_CHNK)
4558 flags |= HTX_SL_F_CHNK;
4559 else if (h1m.flags & H1_MF_CLEN)
4560 flags |= HTX_SL_F_CLEN;
4561 if (h1m.body_len == 0)
4562 flags |= HTX_SL_F_BODYLESS;
4563 }
4564 sl->flags |= flags;
4565
4566 /* If we dont have a content-length set, and the HTTP version is 1.1
4567 * and the status code implies the presence of a message body, we must
4568 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004569 * for the keepalive compliance. If the applet announces a transfer-encoding
4570 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004571 */
4572 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4573 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4574 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4575 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4576 /* Add a new header */
4577 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4578 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4579 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4580 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4581 WILL_LJMP(lua_error(L));
4582 }
4583 }
4584
4585 /* Finalize headers. */
4586 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4587 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4588 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4589 WILL_LJMP(lua_error(L));
4590 }
4591
4592 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4593 b_reset(&res->buf);
4594 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4595 WILL_LJMP(lua_error(L));
4596 }
4597
4598 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004599 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004600
4601 /* Headers sent, set the flag. */
4602 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4603 return 0;
4604
4605}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004606/* We will build the status line and the headers of the HTTP response.
4607 * We will try send at once if its not possible, we give back the hand
4608 * waiting for more room.
4609 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004610__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004611{
4612 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4613 struct stream_interface *si = appctx->appctx->owner;
4614 struct channel *res = si_ic(si);
4615
4616 if (co_data(res)) {
4617 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004618 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004619 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004620 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004621}
4622
4623
Christopher Fauleta2097962019-07-15 16:25:33 +02004624__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004625{
Christopher Fauleta2097962019-07-15 16:25:33 +02004626 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004627}
4628
Christopher Fauleta2097962019-07-15 16:25:33 +02004629/*
4630 *
4631 *
4632 * Class HTTP
4633 *
4634 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004635 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004636
4637/* Returns a struct hlua_txn if the stack entry "ud" is
4638 * a class stream, otherwise it throws an error.
4639 */
4640__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004641{
Christopher Fauleta2097962019-07-15 16:25:33 +02004642 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4643}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004644
Christopher Fauleta2097962019-07-15 16:25:33 +02004645/* This function creates and push in the stack a HTTP object
4646 * according with a current TXN.
4647 */
4648static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4649{
4650 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004651
Christopher Fauleta2097962019-07-15 16:25:33 +02004652 /* Check stack size. */
4653 if (!lua_checkstack(L, 3))
4654 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004655
Christopher Fauleta2097962019-07-15 16:25:33 +02004656 /* Create the object: obj[0] = userdata.
4657 * Note that the base of the Converters object is the
4658 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004659 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004660 lua_newtable(L);
4661 htxn = lua_newuserdata(L, sizeof(*htxn));
4662 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004663
4664 htxn->s = txn->s;
4665 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004666 htxn->dir = txn->dir;
4667 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004668
4669 /* Pop a class stream metatable and affect it to the table. */
4670 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4671 lua_setmetatable(L, -2);
4672
4673 return 1;
4674}
4675
4676/* This function creates ans returns an array of HTTP headers.
4677 * This function does not fails. It is used as wrapper with the
4678 * 2 following functions.
4679 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004680__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004681{
Christopher Fauleta2097962019-07-15 16:25:33 +02004682 struct htx *htx;
4683 int32_t pos;
4684
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004685 /* Create the table. */
4686 lua_newtable(L);
4687
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004688
Christopher Fauleta2097962019-07-15 16:25:33 +02004689 htx = htxbuf(&msg->chn->buf);
4690 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4691 struct htx_blk *blk = htx_get_blk(htx, pos);
4692 enum htx_blk_type type = htx_get_blk_type(blk);
4693 struct ist n, v;
4694 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004695
Christopher Fauleta2097962019-07-15 16:25:33 +02004696 if (type == HTX_BLK_HDR) {
4697 n = htx_get_blk_name(htx,blk);
4698 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004699 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004700 else if (type == HTX_BLK_EOH)
4701 break;
4702 else
4703 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004704
Christopher Fauleta2097962019-07-15 16:25:33 +02004705 /* Check for existing entry:
4706 * assume that the table is on the top of the stack, and
4707 * push the key in the stack, the function lua_gettable()
4708 * perform the lookup.
4709 */
4710 lua_pushlstring(L, n.ptr, n.len);
4711 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004712
Christopher Fauleta2097962019-07-15 16:25:33 +02004713 switch (lua_type(L, -1)) {
4714 case LUA_TNIL:
4715 /* Table not found, create it. */
4716 lua_pop(L, 1); /* remove the nil value. */
4717 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4718 lua_newtable(L); /* create and push empty table. */
4719 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4720 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4721 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004722 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004723
Christopher Fauleta2097962019-07-15 16:25:33 +02004724 case LUA_TTABLE:
4725 /* Entry found: push the value in the table. */
4726 len = lua_rawlen(L, -1);
4727 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4728 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4729 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4730 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004731
Christopher Fauleta2097962019-07-15 16:25:33 +02004732 default:
4733 /* Other cases are errors. */
4734 hlua_pusherror(L, "internal error during the parsing of headers.");
4735 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004736 }
4737 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004738 return 1;
4739}
4740
4741__LJMP static int hlua_http_req_get_headers(lua_State *L)
4742{
4743 struct hlua_txn *htxn;
4744
4745 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4746 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4747
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004748 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004749 WILL_LJMP(lua_error(L));
4750
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004751 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004752}
4753
4754__LJMP static int hlua_http_res_get_headers(lua_State *L)
4755{
4756 struct hlua_txn *htxn;
4757
4758 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4759 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4760
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004761 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004762 WILL_LJMP(lua_error(L));
4763
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004764 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004765}
4766
4767/* This function replace full header, or just a value in
4768 * the request or in the response. It is a wrapper fir the
4769 * 4 following functions.
4770 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004771__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004772{
4773 size_t name_len;
4774 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4775 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4776 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004777 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004778 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004779
Dragan Dosen26743032019-04-30 15:54:36 +02004780 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004781 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4782
Christopher Fauleta2097962019-07-15 16:25:33 +02004783 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004784 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004785 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004786 return 0;
4787}
4788
4789__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4790{
4791 struct hlua_txn *htxn;
4792
4793 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4794 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4795
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004796 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004797 WILL_LJMP(lua_error(L));
4798
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004799 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004800}
4801
4802__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4803{
4804 struct hlua_txn *htxn;
4805
4806 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4807 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4808
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004809 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004810 WILL_LJMP(lua_error(L));
4811
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004812 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004813}
4814
4815__LJMP static int hlua_http_req_rep_val(lua_State *L)
4816{
4817 struct hlua_txn *htxn;
4818
4819 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4820 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4821
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004822 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004823 WILL_LJMP(lua_error(L));
4824
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004825 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004826}
4827
4828__LJMP static int hlua_http_res_rep_val(lua_State *L)
4829{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004830 struct hlua_txn *htxn;
4831
4832 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4833 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4834
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004835 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004836 WILL_LJMP(lua_error(L));
4837
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004838 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004839}
4840
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004841/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004842 * It is a wrapper for the 2 following functions.
4843 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004844__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004845{
4846 size_t len;
4847 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004848 struct htx *htx = htxbuf(&msg->chn->buf);
4849 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004850
Christopher Fauleta2097962019-07-15 16:25:33 +02004851 ctx.blk = NULL;
4852 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4853 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004854 return 0;
4855}
4856
4857__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4858{
4859 struct hlua_txn *htxn;
4860
4861 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4862 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4863
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004864 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004865 WILL_LJMP(lua_error(L));
4866
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004867 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004868}
4869
4870__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4871{
4872 struct hlua_txn *htxn;
4873
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004874 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004875 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4876
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004877 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004878 WILL_LJMP(lua_error(L));
4879
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004880 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004881}
4882
4883/* This function adds an header. It is a wrapper used by
4884 * the 2 following functions.
4885 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004886__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004887{
4888 size_t name_len;
4889 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4890 size_t value_len;
4891 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004892 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004893
Christopher Fauleta2097962019-07-15 16:25:33 +02004894 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4895 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004896 return 0;
4897}
4898
4899__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4900{
4901 struct hlua_txn *htxn;
4902
4903 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4904 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4905
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004906 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004907 WILL_LJMP(lua_error(L));
4908
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004909 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004910}
4911
4912__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4913{
4914 struct hlua_txn *htxn;
4915
4916 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4917 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4918
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004919 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004920 WILL_LJMP(lua_error(L));
4921
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004922 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004923}
4924
4925static int hlua_http_req_set_hdr(lua_State *L)
4926{
4927 struct hlua_txn *htxn;
4928
4929 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4930 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4931
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004932 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004933 WILL_LJMP(lua_error(L));
4934
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004935 hlua_http_del_hdr(L, &htxn->s->txn->req);
4936 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004937}
4938
4939static int hlua_http_res_set_hdr(lua_State *L)
4940{
4941 struct hlua_txn *htxn;
4942
4943 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4944 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4945
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004946 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004947 WILL_LJMP(lua_error(L));
4948
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004949 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
4950 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004951}
4952
4953/* This function set the method. */
4954static int hlua_http_req_set_meth(lua_State *L)
4955{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004956 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004957 size_t name_len;
4958 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004959
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004960 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004961 WILL_LJMP(lua_error(L));
4962
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004963 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004964 return 1;
4965}
4966
4967/* This function set the method. */
4968static int hlua_http_req_set_path(lua_State *L)
4969{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004970 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004971 size_t name_len;
4972 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004973
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004974 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004975 WILL_LJMP(lua_error(L));
4976
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004977 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004978 return 1;
4979}
4980
4981/* This function set the query-string. */
4982static int hlua_http_req_set_query(lua_State *L)
4983{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004984 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004985 size_t name_len;
4986 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004987
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004988 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004989 WILL_LJMP(lua_error(L));
4990
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004991 /* Check length. */
4992 if (name_len > trash.size - 1) {
4993 lua_pushboolean(L, 0);
4994 return 1;
4995 }
4996
4997 /* Add the mark question as prefix. */
4998 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004999 trash.area[trash.data++] = '?';
5000 memcpy(trash.area + trash.data, name, name_len);
5001 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005002
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005003 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005004 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005005 return 1;
5006}
5007
5008/* This function set the uri. */
5009static int hlua_http_req_set_uri(lua_State *L)
5010{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005011 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005012 size_t name_len;
5013 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005014
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005015 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005016 WILL_LJMP(lua_error(L));
5017
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005018 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005019 return 1;
5020}
5021
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005022/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005023static int hlua_http_res_set_status(lua_State *L)
5024{
5025 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5026 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005027 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5028 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005029
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005030 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005031 WILL_LJMP(lua_error(L));
5032
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005033 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005034 return 0;
5035}
5036
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005037/*
5038 *
5039 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005040 * Class TXN
5041 *
5042 *
5043 */
5044
5045/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005046 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005047 */
5048__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5049{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005050 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005051}
5052
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005053__LJMP static int hlua_set_var(lua_State *L)
5054{
5055 struct hlua_txn *htxn;
5056 const char *name;
5057 size_t len;
5058 struct sample smp;
5059
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005060 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5061 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005062
5063 /* It is useles to retrieve the stream, but this function
5064 * runs only in a stream context.
5065 */
5066 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5067 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5068
5069 /* Converts the third argument in a sample. */
5070 hlua_lua2smp(L, 3, &smp);
5071
5072 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005073 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005074
5075 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5076 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5077 else
5078 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5079
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005080 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005081}
5082
Christopher Faulet85d79c92016-11-09 16:54:56 +01005083__LJMP static int hlua_unset_var(lua_State *L)
5084{
5085 struct hlua_txn *htxn;
5086 const char *name;
5087 size_t len;
5088 struct sample smp;
5089
5090 MAY_LJMP(check_args(L, 2, "unset_var"));
5091
5092 /* It is useles to retrieve the stream, but this function
5093 * runs only in a stream context.
5094 */
5095 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5096 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5097
5098 /* Unset the variable. */
5099 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005100 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5101 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005102}
5103
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005104__LJMP static int hlua_get_var(lua_State *L)
5105{
5106 struct hlua_txn *htxn;
5107 const char *name;
5108 size_t len;
5109 struct sample smp;
5110
5111 MAY_LJMP(check_args(L, 2, "get_var"));
5112
5113 /* It is useles to retrieve the stream, but this function
5114 * runs only in a stream context.
5115 */
5116 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5117 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5118
Willy Tarreau7560dd42016-03-10 16:28:58 +01005119 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005120 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005121 lua_pushnil(L);
5122 return 1;
5123 }
5124
5125 return hlua_smp2lua(L, &smp);
5126}
5127
Willy Tarreau59551662015-03-10 14:23:13 +01005128__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005129{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005130 struct hlua *hlua;
5131
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005132 MAY_LJMP(check_args(L, 2, "set_priv"));
5133
Willy Tarreau87b09662015-04-03 00:22:06 +02005134 /* It is useles to retrieve the stream, but this function
5135 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005136 */
5137 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005138 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005139
5140 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005141 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005142
5143 /* Get and store new value. */
5144 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5145 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5146
5147 return 0;
5148}
5149
Willy Tarreau59551662015-03-10 14:23:13 +01005150__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005151{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005152 struct hlua *hlua;
5153
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005154 MAY_LJMP(check_args(L, 1, "get_priv"));
5155
Willy Tarreau87b09662015-04-03 00:22:06 +02005156 /* It is useles to retrieve the stream, but this function
5157 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005158 */
5159 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005160 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005161
5162 /* Push configuration index in the stack. */
5163 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5164
5165 return 1;
5166}
5167
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005168/* Create stack entry containing a class TXN. This function
5169 * return 0 if the stack does not contains free slots,
5170 * otherwise it returns 1.
5171 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005172static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005173{
Willy Tarreaude491382015-04-06 11:04:28 +02005174 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005175
5176 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005177 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005178 return 0;
5179
5180 /* NOTE: The allocation never fails. The failure
5181 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005182 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005183 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005184 /* Create the object: obj[0] = userdata. */
5185 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005186 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005187 lua_rawseti(L, -2, 0);
5188
Willy Tarreaude491382015-04-06 11:04:28 +02005189 htxn->s = s;
5190 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005191 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005192 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005193
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005194 /* Create the "f" field that contains a list of fetches. */
5195 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005196 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005197 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005198 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005199
5200 /* Create the "sf" field that contains a list of stringsafe fetches. */
5201 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005202 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005203 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005204 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005205
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005206 /* Create the "c" field that contains a list of converters. */
5207 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005208 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005209 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005210 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005211
5212 /* Create the "sc" field that contains a list of stringsafe converters. */
5213 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005214 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005215 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005216 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005217
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005218 /* Create the "req" field that contains the request channel object. */
5219 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005220 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005221 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005222 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005223
5224 /* Create the "res" field that contains the response channel object. */
5225 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005226 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005227 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005228 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005229
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005230 /* Creates the HTTP object is the current proxy allows http. */
5231 lua_pushstring(L, "http");
5232 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005233 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005234 return 0;
5235 }
5236 else
5237 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005238 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005239
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005240 /* Pop a class sesison metatable and affect it to the userdata. */
5241 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5242 lua_setmetatable(L, -2);
5243
5244 return 1;
5245}
5246
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005247__LJMP static int hlua_txn_deflog(lua_State *L)
5248{
5249 const char *msg;
5250 struct hlua_txn *htxn;
5251
5252 MAY_LJMP(check_args(L, 2, "deflog"));
5253 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5254 msg = MAY_LJMP(luaL_checkstring(L, 2));
5255
5256 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5257 return 0;
5258}
5259
5260__LJMP static int hlua_txn_log(lua_State *L)
5261{
5262 int level;
5263 const char *msg;
5264 struct hlua_txn *htxn;
5265
5266 MAY_LJMP(check_args(L, 3, "log"));
5267 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5268 level = MAY_LJMP(luaL_checkinteger(L, 2));
5269 msg = MAY_LJMP(luaL_checkstring(L, 3));
5270
5271 if (level < 0 || level >= NB_LOG_LEVELS)
5272 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5273
5274 hlua_sendlog(htxn->s->be, level, msg);
5275 return 0;
5276}
5277
5278__LJMP static int hlua_txn_log_debug(lua_State *L)
5279{
5280 const char *msg;
5281 struct hlua_txn *htxn;
5282
5283 MAY_LJMP(check_args(L, 2, "Debug"));
5284 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5285 msg = MAY_LJMP(luaL_checkstring(L, 2));
5286 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5287 return 0;
5288}
5289
5290__LJMP static int hlua_txn_log_info(lua_State *L)
5291{
5292 const char *msg;
5293 struct hlua_txn *htxn;
5294
5295 MAY_LJMP(check_args(L, 2, "Info"));
5296 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5297 msg = MAY_LJMP(luaL_checkstring(L, 2));
5298 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5299 return 0;
5300}
5301
5302__LJMP static int hlua_txn_log_warning(lua_State *L)
5303{
5304 const char *msg;
5305 struct hlua_txn *htxn;
5306
5307 MAY_LJMP(check_args(L, 2, "Warning"));
5308 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5309 msg = MAY_LJMP(luaL_checkstring(L, 2));
5310 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5311 return 0;
5312}
5313
5314__LJMP static int hlua_txn_log_alert(lua_State *L)
5315{
5316 const char *msg;
5317 struct hlua_txn *htxn;
5318
5319 MAY_LJMP(check_args(L, 2, "Alert"));
5320 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5321 msg = MAY_LJMP(luaL_checkstring(L, 2));
5322 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5323 return 0;
5324}
5325
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005326__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5327{
5328 struct hlua_txn *htxn;
5329 int ll;
5330
5331 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5332 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5333 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5334
5335 if (ll < 0 || ll > 7)
5336 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5337
5338 htxn->s->logs.level = ll;
5339 return 0;
5340}
5341
5342__LJMP static int hlua_txn_set_tos(lua_State *L)
5343{
5344 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005345 int tos;
5346
5347 MAY_LJMP(check_args(L, 2, "set_tos"));
5348 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5349 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5350
Willy Tarreau1a18b542018-12-11 16:37:42 +01005351 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005352 return 0;
5353}
5354
5355__LJMP static int hlua_txn_set_mark(lua_State *L)
5356{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005357 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005358 int mark;
5359
5360 MAY_LJMP(check_args(L, 2, "set_mark"));
5361 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5362 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5363
Lukas Tribus579e3e32019-08-11 18:03:45 +02005364 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005365 return 0;
5366}
5367
Patrick Hemmer268a7072018-05-11 12:52:31 -04005368__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5369{
5370 struct hlua_txn *htxn;
5371
5372 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5373 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5374 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5375 return 0;
5376}
5377
5378__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5379{
5380 struct hlua_txn *htxn;
5381
5382 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5383 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5384 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5385 return 0;
5386}
5387
Christopher Faulet700d9e82020-01-31 12:21:52 +01005388/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005389 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005390 * message and terminate the transaction. It returns 1 on success and 0 on
5391 * error. The Reply must be on top of the stack.
5392 */
5393__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5394{
5395 struct htx *htx;
5396 struct htx_sl *sl;
5397 struct h1m h1m;
5398 const char *status, *reason, *body;
5399 size_t status_len, reason_len, body_len;
5400 int ret, code, flags;
5401
5402 code = 200;
5403 status = "200";
5404 status_len = 3;
5405 ret = lua_getfield(L, -1, "status");
5406 if (ret == LUA_TNUMBER) {
5407 code = lua_tointeger(L, -1);
5408 status = lua_tolstring(L, -1, &status_len);
5409 }
5410 lua_pop(L, 1);
5411
5412 reason = http_get_reason(code);
5413 reason_len = strlen(reason);
5414 ret = lua_getfield(L, -1, "reason");
5415 if (ret == LUA_TSTRING)
5416 reason = lua_tolstring(L, -1, &reason_len);
5417 lua_pop(L, 1);
5418
5419 body = NULL;
5420 body_len = 0;
5421 ret = lua_getfield(L, -1, "body");
5422 if (ret == LUA_TSTRING)
5423 body = lua_tolstring(L, -1, &body_len);
5424 lua_pop(L, 1);
5425
5426 /* Prepare the response before inserting the headers */
5427 h1m_init_res(&h1m);
5428 htx = htx_from_buf(&s->res.buf);
5429 channel_htx_truncate(&s->res, htx);
5430 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5431 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5432 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5433 ist2(status, status_len), ist2(reason, reason_len));
5434 }
5435 else {
5436 flags = HTX_SL_F_IS_RESP;
5437 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5438 ist2(status, status_len), ist2(reason, reason_len));
5439 }
5440 if (!sl)
5441 goto fail;
5442 sl->info.res.status = code;
5443
5444 /* Push in the stack the "headers" entry. */
5445 ret = lua_getfield(L, -1, "headers");
5446 if (ret != LUA_TTABLE)
5447 goto skip_headers;
5448
5449 lua_pushnil(L);
5450 while (lua_next(L, -2) != 0) {
5451 struct ist name, value;
5452 const char *n, *v;
5453 size_t nlen, vlen;
5454
5455 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5456 /* Skip element if the key is not a string or if the value is not a table */
5457 goto next_hdr;
5458 }
5459
5460 n = lua_tolstring(L, -2, &nlen);
5461 name = ist2(n, nlen);
5462 if (isteqi(name, ist("content-length"))) {
5463 /* Always skip content-length header. It will be added
5464 * later with the correct len
5465 */
5466 goto next_hdr;
5467 }
5468
5469 /* Loop on header's values */
5470 lua_pushnil(L);
5471 while (lua_next(L, -2)) {
5472 if (!lua_isstring(L, -1)) {
5473 /* Skip the value if it is not a string */
5474 goto next_value;
5475 }
5476
5477 v = lua_tolstring(L, -1, &vlen);
5478 value = ist2(v, vlen);
5479
5480 if (isteqi(name, ist("transfer-encoding")))
5481 h1_parse_xfer_enc_header(&h1m, value);
5482 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5483 goto fail;
5484
5485 next_value:
5486 lua_pop(L, 1);
5487 }
5488
5489 next_hdr:
5490 lua_pop(L, 1);
5491 }
5492 skip_headers:
5493 lua_pop(L, 1);
5494
5495 /* Update h1m flags: CLEN is set if CHNK is not present */
5496 if (!(h1m.flags & H1_MF_CHNK)) {
5497 const char *clen = ultoa(body_len);
5498
5499 h1m.flags |= H1_MF_CLEN;
5500 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5501 goto fail;
5502 }
5503 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5504 h1m.flags |= H1_MF_XFER_LEN;
5505
5506 /* Update HTX start-line flags */
5507 if (h1m.flags & H1_MF_XFER_ENC)
5508 flags |= HTX_SL_F_XFER_ENC;
5509 if (h1m.flags & H1_MF_XFER_LEN) {
5510 flags |= HTX_SL_F_XFER_LEN;
5511 if (h1m.flags & H1_MF_CHNK)
5512 flags |= HTX_SL_F_CHNK;
5513 else if (h1m.flags & H1_MF_CLEN)
5514 flags |= HTX_SL_F_CLEN;
5515 if (h1m.body_len == 0)
5516 flags |= HTX_SL_F_BODYLESS;
5517 }
5518 sl->flags |= flags;
5519
5520
5521 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5522 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5523 !htx_add_endof(htx, HTX_BLK_EOM))
5524 goto fail;
5525
5526 /* Now, forward the response and terminate the transaction */
5527 s->txn->status = code;
5528 htx_to_buf(htx, &s->res.buf);
5529 if (!http_forward_proxy_resp(s, 1))
5530 goto fail;
5531
5532 return 1;
5533
5534 fail:
5535 channel_htx_truncate(&s->res, htx);
5536 return 0;
5537}
5538
5539/* Terminate a transaction if called from a lua action. For TCP streams,
5540 * processing is just aborted. Nothing is returned to the client and all
5541 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5542 * is forwarded to the client before terminating the transaction. On success,
5543 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5544 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5545 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005546 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005547__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005548{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005549 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005550 struct stream *s;
5551 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005552
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005553 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005554
Christopher Faulet700d9e82020-01-31 12:21:52 +01005555 /* If the flags NOTERM is set, we cannot terminate the session, so we
5556 * just end the execution of the current lua code. */
5557 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005558 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005559
Christopher Faulet700d9e82020-01-31 12:21:52 +01005560 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005561 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005562 struct channel *req = &s->req;
5563 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005564
Christopher Faulet700d9e82020-01-31 12:21:52 +01005565 channel_auto_read(req);
5566 channel_abort(req);
5567 channel_auto_close(req);
5568 channel_erase(req);
5569
5570 res->wex = tick_add_ifset(now_ms, res->wto);
5571 channel_auto_read(res);
5572 channel_auto_close(res);
5573 channel_shutr_now(res);
5574
5575 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5576 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005577 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005578
Christopher Faulet700d9e82020-01-31 12:21:52 +01005579 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5580 /* No reply or invalid reply */
5581 s->txn->status = 0;
5582 http_reply_and_close(s, 0, NULL);
5583 }
5584 else {
5585 /* Remove extra args to have the reply on top of the stack */
5586 if (lua_gettop(L) > 2)
5587 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005588
Christopher Faulet700d9e82020-01-31 12:21:52 +01005589 if (!hlua_txn_forward_reply(L, s)) {
5590 if (!(s->flags & SF_ERR_MASK))
5591 s->flags |= SF_ERR_PRXCOND;
5592 lua_pushinteger(L, ACT_RET_ERR);
5593 WILL_LJMP(hlua_done(L));
5594 return 0; /* Never reached */
5595 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005596 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005597
Christopher Faulet700d9e82020-01-31 12:21:52 +01005598 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5599 if (htxn->dir == SMP_OPT_DIR_REQ) {
5600 /* let's log the request time */
5601 s->logs.tv_request = now;
5602 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5603 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5604 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005605
Christopher Faulet700d9e82020-01-31 12:21:52 +01005606 done:
5607 if (!(s->flags & SF_ERR_MASK))
5608 s->flags |= SF_ERR_LOCAL;
5609 if (!(s->flags & SF_FINST_MASK))
5610 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005611
Christopher Faulet4ad73102020-03-05 11:07:31 +01005612 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005613 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005614 return 0;
5615}
5616
Christopher Faulet700d9e82020-01-31 12:21:52 +01005617/*
5618 *
5619 *
5620 * Class REPLY
5621 *
5622 *
5623 */
5624
5625/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5626 * free slots, the function fails and returns 0;
5627 */
5628static int hlua_txn_reply_new(lua_State *L)
5629{
5630 struct hlua_txn *htxn;
5631 const char *reason, *body = NULL;
5632 int ret, status;
5633
5634 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005635 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005636 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5637 WILL_LJMP(lua_error(L));
5638 }
5639
5640 /* Default value */
5641 status = 200;
5642 reason = http_get_reason(status);
5643
5644 if (lua_istable(L, 2)) {
5645 /* load status and reason from the table argument at index 2 */
5646 ret = lua_getfield(L, 2, "status");
5647 if (ret == LUA_TNIL)
5648 goto reason;
5649 else if (ret != LUA_TNUMBER) {
5650 /* invalid status: ignore the reason */
5651 goto body;
5652 }
5653 status = lua_tointeger(L, -1);
5654
5655 reason:
5656 lua_pop(L, 1); /* restore the stack: remove status */
5657 ret = lua_getfield(L, 2, "reason");
5658 if (ret == LUA_TSTRING)
5659 reason = lua_tostring(L, -1);
5660
5661 body:
5662 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5663 ret = lua_getfield(L, 2, "body");
5664 if (ret == LUA_TSTRING)
5665 body = lua_tostring(L, -1);
5666 lua_pop(L, 1); /* restore the stack: remove body */
5667 }
5668
5669 /* Create the Reply table */
5670 lua_newtable(L);
5671
5672 /* Add status element */
5673 lua_pushstring(L, "status");
5674 lua_pushinteger(L, status);
5675 lua_settable(L, -3);
5676
5677 /* Add reason element */
5678 reason = http_get_reason(status);
5679 lua_pushstring(L, "reason");
5680 lua_pushstring(L, reason);
5681 lua_settable(L, -3);
5682
5683 /* Add body element, nil if undefined */
5684 lua_pushstring(L, "body");
5685 if (body)
5686 lua_pushstring(L, body);
5687 else
5688 lua_pushnil(L);
5689 lua_settable(L, -3);
5690
5691 /* Add headers element */
5692 lua_pushstring(L, "headers");
5693 lua_newtable(L);
5694
5695 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5696 if (lua_istable(L, 2)) {
5697 /* load headers from the table argument at index 2. If it is a table, copy it. */
5698 ret = lua_getfield(L, 2, "headers");
5699 if (ret == LUA_TTABLE) {
5700 /* stack: [ ... <headers:table>, <table> ] */
5701 lua_pushnil(L);
5702 while (lua_next(L, -2) != 0) {
5703 /* stack: [ ... <headers:table>, <table>, k, v] */
5704 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5705 /* invalid value type, skip it */
5706 lua_pop(L, 1);
5707 continue;
5708 }
5709
5710
5711 /* Duplicate the key and swap it with the value. */
5712 lua_pushvalue(L, -2);
5713 lua_insert(L, -2);
5714 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5715
5716 lua_newtable(L);
5717 lua_insert(L, -2);
5718 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5719
5720 if (lua_isstring(L, -1)) {
5721 /* push the value in the inner table */
5722 lua_rawseti(L, -2, 1);
5723 }
5724 else { /* table */
5725 lua_pushnil(L);
5726 while (lua_next(L, -2) != 0) {
5727 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5728 if (!lua_isstring(L, -1)) {
5729 /* invalid value type, skip it*/
5730 lua_pop(L, 1);
5731 continue;
5732 }
5733 /* push the value in the inner table */
5734 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5735 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5736 }
5737 lua_pop(L, 1);
5738 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5739 }
5740
5741 /* push (k,v) on the stack in the headers table:
5742 * stack: [ ... <headers:table>, <table>, k, k, v ]
5743 */
5744 lua_settable(L, -5);
5745 /* stack: [ ... <headers:table>, <table>, k ] */
5746 }
5747 }
5748 lua_pop(L, 1);
5749 }
5750 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5751 lua_settable(L, -3);
5752 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5753
5754 /* Pop a class sesison metatable and affect it to the userdata. */
5755 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5756 lua_setmetatable(L, -2);
5757 return 1;
5758}
5759
5760/* Set the reply status code, and optionally the reason. If no reason is
5761 * provided, the default one corresponding to the status code is used.
5762 */
5763__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5764{
5765 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5766 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5767
5768 /* First argument (self) must be a table */
5769 luaL_checktype(L, 1, LUA_TTABLE);
5770
5771 if (status < 100 || status > 599) {
5772 lua_pushboolean(L, 0);
5773 return 1;
5774 }
5775 if (!reason)
5776 reason = http_get_reason(status);
5777
5778 lua_pushinteger(L, status);
5779 lua_setfield(L, 1, "status");
5780
5781 lua_pushstring(L, reason);
5782 lua_setfield(L, 1, "reason");
5783
5784 lua_pushboolean(L, 1);
5785 return 1;
5786}
5787
5788/* Add a header into the reply object. Each header name is associated to an
5789 * array of values in the "headers" table. If the header name is not found, a
5790 * new entry is created.
5791 */
5792__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5793{
5794 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5795 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5796 int ret;
5797
5798 /* First argument (self) must be a table */
5799 luaL_checktype(L, 1, LUA_TTABLE);
5800
5801 /* Push in the stack the "headers" entry. */
5802 ret = lua_getfield(L, 1, "headers");
5803 if (ret != LUA_TTABLE) {
5804 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5805 WILL_LJMP(lua_error(L));
5806 }
5807
5808 /* check if the header is already registered. If not, register it. */
5809 ret = lua_getfield(L, -1, name);
5810 if (ret == LUA_TNIL) {
5811 /* Entry not found. */
5812 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5813
5814 /* Insert the new header name in the array in the top of the stack.
5815 * It left the new array in the top of the stack.
5816 */
5817 lua_newtable(L);
5818 lua_pushstring(L, name);
5819 lua_pushvalue(L, -2);
5820 lua_settable(L, -4);
5821 }
5822 else if (ret != LUA_TTABLE) {
5823 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5824 WILL_LJMP(lua_error(L));
5825 }
5826
5827 /* Now the top od thestack is an array of values. We push
5828 * the header value as new entry.
5829 */
5830 lua_pushstring(L, value);
5831 ret = lua_rawlen(L, -2);
5832 lua_rawseti(L, -2, ret + 1);
5833
5834 lua_pushboolean(L, 1);
5835 return 1;
5836}
5837
5838/* Remove all occurrences of a given header name. */
5839__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5840{
5841 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5842 int ret;
5843
5844 /* First argument (self) must be a table */
5845 luaL_checktype(L, 1, LUA_TTABLE);
5846
5847 /* Push in the stack the "headers" entry. */
5848 ret = lua_getfield(L, 1, "headers");
5849 if (ret != LUA_TTABLE) {
5850 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5851 WILL_LJMP(lua_error(L));
5852 }
5853
5854 lua_pushstring(L, name);
5855 lua_pushnil(L);
5856 lua_settable(L, -3);
5857
5858 lua_pushboolean(L, 1);
5859 return 1;
5860}
5861
5862/* Set the reply's body. Overwrite any existing entry. */
5863__LJMP static int hlua_txn_reply_set_body(lua_State *L)
5864{
5865 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
5866
5867 /* First argument (self) must be a table */
5868 luaL_checktype(L, 1, LUA_TTABLE);
5869
5870 lua_pushstring(L, payload);
5871 lua_setfield(L, 1, "body");
5872
5873 lua_pushboolean(L, 1);
5874 return 1;
5875}
5876
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005877__LJMP static int hlua_log(lua_State *L)
5878{
5879 int level;
5880 const char *msg;
5881
5882 MAY_LJMP(check_args(L, 2, "log"));
5883 level = MAY_LJMP(luaL_checkinteger(L, 1));
5884 msg = MAY_LJMP(luaL_checkstring(L, 2));
5885
5886 if (level < 0 || level >= NB_LOG_LEVELS)
5887 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5888
5889 hlua_sendlog(NULL, level, msg);
5890 return 0;
5891}
5892
5893__LJMP static int hlua_log_debug(lua_State *L)
5894{
5895 const char *msg;
5896
5897 MAY_LJMP(check_args(L, 1, "debug"));
5898 msg = MAY_LJMP(luaL_checkstring(L, 1));
5899 hlua_sendlog(NULL, LOG_DEBUG, msg);
5900 return 0;
5901}
5902
5903__LJMP static int hlua_log_info(lua_State *L)
5904{
5905 const char *msg;
5906
5907 MAY_LJMP(check_args(L, 1, "info"));
5908 msg = MAY_LJMP(luaL_checkstring(L, 1));
5909 hlua_sendlog(NULL, LOG_INFO, msg);
5910 return 0;
5911}
5912
5913__LJMP static int hlua_log_warning(lua_State *L)
5914{
5915 const char *msg;
5916
5917 MAY_LJMP(check_args(L, 1, "warning"));
5918 msg = MAY_LJMP(luaL_checkstring(L, 1));
5919 hlua_sendlog(NULL, LOG_WARNING, msg);
5920 return 0;
5921}
5922
5923__LJMP static int hlua_log_alert(lua_State *L)
5924{
5925 const char *msg;
5926
5927 MAY_LJMP(check_args(L, 1, "alert"));
5928 msg = MAY_LJMP(luaL_checkstring(L, 1));
5929 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005930 return 0;
5931}
5932
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005933__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005934{
5935 int wakeup_ms = lua_tointeger(L, -1);
5936 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005937 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005938 return 0;
5939}
5940
5941__LJMP static int hlua_sleep(lua_State *L)
5942{
5943 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005944 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005945
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005946 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005947
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005948 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005949 wakeup_ms = tick_add(now_ms, delay);
5950 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005951
Willy Tarreau9635e032018-10-16 17:52:55 +02005952 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005953 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005954}
5955
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005956__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005957{
5958 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005959 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005960
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005961 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005962
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005963 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005964 wakeup_ms = tick_add(now_ms, delay);
5965 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005966
Willy Tarreau9635e032018-10-16 17:52:55 +02005967 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005968 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005969}
5970
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005971/* This functionis an LUA binding. it permits to give back
5972 * the hand at the HAProxy scheduler. It is used when the
5973 * LUA processing consumes a lot of time.
5974 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005975__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005976{
5977 return 0;
5978}
5979
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005980__LJMP static int hlua_yield(lua_State *L)
5981{
Willy Tarreau9635e032018-10-16 17:52:55 +02005982 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005983 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005984}
5985
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005986/* This function change the nice of the currently executed
5987 * task. It is used set low or high priority at the current
5988 * task.
5989 */
Willy Tarreau59551662015-03-10 14:23:13 +01005990__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005991{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005992 struct hlua *hlua;
5993 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005994
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005995 MAY_LJMP(check_args(L, 1, "set_nice"));
5996 hlua = hlua_gethlua(L);
5997 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005998
5999 /* If he task is not set, I'm in a start mode. */
6000 if (!hlua || !hlua->task)
6001 return 0;
6002
6003 if (nice < -1024)
6004 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006005 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006006 nice = 1024;
6007
6008 hlua->task->nice = nice;
6009 return 0;
6010}
6011
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006012/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006013 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6014 * return an E_AGAIN signal, the emmiter of this signal must set a
6015 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006016 *
6017 * Task wrapper are longjmp safe because the only one Lua code
6018 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006019 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006020struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006021{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006022 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006023 enum hlua_exec status;
6024
Christopher Faulet5bc99722018-04-25 10:34:45 +02006025 if (task->thread_mask == MAX_THREADS_MASK)
6026 task_set_affinity(task, tid_bit);
6027
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006028 /* If it is the first call to the task, we must initialize the
6029 * execution timeouts.
6030 */
6031 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006032 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006033
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006034 /* Execute the Lua code. */
6035 status = hlua_ctx_resume(hlua, 1);
6036
6037 switch (status) {
6038 /* finished or yield */
6039 case HLUA_E_OK:
6040 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006041 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006042 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006043 break;
6044
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006045 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006046 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006047 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006048 break;
6049
6050 /* finished with error. */
6051 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006052 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006053 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006054 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006055 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006056 break;
6057
6058 case HLUA_E_ERR:
6059 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006060 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006061 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006062 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006063 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006064 break;
6065 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006066 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006067}
6068
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006069/* This function is an LUA binding that register LUA function to be
6070 * executed after the HAProxy configuration parsing and before the
6071 * HAProxy scheduler starts. This function expect only one LUA
6072 * argument that is a function. This function returns nothing, but
6073 * throws if an error is encountered.
6074 */
6075__LJMP static int hlua_register_init(lua_State *L)
6076{
6077 struct hlua_init_function *init;
6078 int ref;
6079
6080 MAY_LJMP(check_args(L, 1, "register_init"));
6081
6082 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6083
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006084 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006085 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006086 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006087
6088 init->function_ref = ref;
6089 LIST_ADDQ(&hlua_init_functions, &init->l);
6090 return 0;
6091}
6092
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006093/* This functio is an LUA binding. It permits to register a task
6094 * executed in parallel of the main HAroxy activity. The task is
6095 * created and it is set in the HAProxy scheduler. It can be called
6096 * from the "init" section, "post init" or during the runtime.
6097 *
6098 * Lua prototype:
6099 *
6100 * <none> core.register_task(<function>)
6101 */
6102static int hlua_register_task(lua_State *L)
6103{
6104 struct hlua *hlua;
6105 struct task *task;
6106 int ref;
6107
6108 MAY_LJMP(check_args(L, 1, "register_task"));
6109
6110 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6111
Willy Tarreaubafbe012017-11-24 17:34:44 +01006112 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006113 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006114 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006115
Emeric Brunc60def82017-09-27 14:59:38 +02006116 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006117 if (!task)
6118 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6119
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006120 task->context = hlua;
6121 task->process = hlua_process_task;
6122
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006123 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006124 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006125
6126 /* Restore the function in the stack. */
6127 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6128 hlua->nargs = 0;
6129
6130 /* Schedule task. */
6131 task_schedule(task, now_ms);
6132
6133 return 0;
6134}
6135
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006136/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6137 * doesn't allow "yield" functions because the HAProxy engine cannot
6138 * resume converters.
6139 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006140static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006141{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006142 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006143 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006144 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006145
Willy Tarreaube508f12016-03-10 11:47:01 +01006146 if (!stream)
6147 return 0;
6148
Willy Tarreau87b09662015-04-03 00:22:06 +02006149 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006150 * Lua context can be not initialized. This behavior
6151 * permits to save performances because a systematic
6152 * Lua initialization cause 5% performances loss.
6153 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006154 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006155 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006156 if (!stream->hlua) {
6157 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6158 return 0;
6159 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006160 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006161 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6162 return 0;
6163 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006164 }
6165
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006166 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006167 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006168
6169 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006170 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6171 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6172 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006173 else
6174 error = "critical error";
6175 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006176 return 0;
6177 }
6178
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006179 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006180 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006181 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006182 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006183 return 0;
6184 }
6185
6186 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006187 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006188
6189 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006190 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006191 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006192 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006193 return 0;
6194 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006195 hlua_smp2lua(stream->hlua->T, smp);
6196 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006197
6198 /* push keywords in the stack. */
6199 if (arg_p) {
6200 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006201 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006202 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006203 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006204 return 0;
6205 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006206 hlua_arg2lua(stream->hlua->T, arg_p);
6207 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006208 }
6209 }
6210
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006211 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006212 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006213
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006214 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006215 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006216 }
6217
6218 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006219 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006220 /* finished. */
6221 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006222 /* If the stack is empty, the function fails. */
6223 if (lua_gettop(stream->hlua->T) <= 0)
6224 return 0;
6225
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006226 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006227 hlua_lua2smp(stream->hlua->T, -1, smp);
6228 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006229 return 1;
6230
6231 /* yield. */
6232 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006233 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006234 return 0;
6235
6236 /* finished with error. */
6237 case HLUA_E_ERRMSG:
6238 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006239 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006240 fcn->name, lua_tostring(stream->hlua->T, -1));
6241 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006242 return 0;
6243
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006244 case HLUA_E_ETMOUT:
6245 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6246 return 0;
6247
6248 case HLUA_E_NOMEM:
6249 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6250 return 0;
6251
6252 case HLUA_E_YIELD:
6253 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6254 return 0;
6255
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006256 case HLUA_E_ERR:
6257 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006258 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006259
6260 default:
6261 return 0;
6262 }
6263}
6264
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006265/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6266 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006267 * resume sample-fetches. This function will be called by the sample
6268 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006269 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006270static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6271 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006272{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006273 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006274 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006275 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006276 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006277
Willy Tarreaube508f12016-03-10 11:47:01 +01006278 if (!stream)
6279 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006280
Willy Tarreau87b09662015-04-03 00:22:06 +02006281 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006282 * Lua context can be not initialized. This behavior
6283 * permits to save performances because a systematic
6284 * Lua initialization cause 5% performances loss.
6285 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006286 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006287 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006288 if (!stream->hlua) {
6289 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6290 return 0;
6291 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006292 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006293 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6294 return 0;
6295 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006296 }
6297
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006298 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006299 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006300
6301 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006302 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6303 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6304 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006305 else
6306 error = "critical error";
6307 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006308 return 0;
6309 }
6310
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006311 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006312 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006313 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006314 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006315 return 0;
6316 }
6317
6318 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006319 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006320
6321 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006322 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006323 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006324 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006325 return 0;
6326 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006327 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006328
6329 /* push keywords in the stack. */
6330 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6331 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006332 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006333 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006334 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006335 return 0;
6336 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006337 hlua_arg2lua(stream->hlua->T, arg_p);
6338 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006339 }
6340
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006341 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006342 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006343
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006344 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006345 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006346 }
6347
6348 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006349 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006350 /* finished. */
6351 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006352 /* If the stack is empty, the function fails. */
6353 if (lua_gettop(stream->hlua->T) <= 0)
6354 return 0;
6355
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006356 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006357 hlua_lua2smp(stream->hlua->T, -1, smp);
6358 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006359
6360 /* Set the end of execution flag. */
6361 smp->flags &= ~SMP_F_MAY_CHANGE;
6362 return 1;
6363
6364 /* yield. */
6365 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006366 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006367 return 0;
6368
6369 /* finished with error. */
6370 case HLUA_E_ERRMSG:
6371 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006372 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006373 fcn->name, lua_tostring(stream->hlua->T, -1));
6374 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006375 return 0;
6376
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006377 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006378 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6379 return 0;
6380
6381 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006382 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6383 return 0;
6384
6385 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006386 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6387 return 0;
6388
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006389 case HLUA_E_ERR:
6390 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006391 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006392
6393 default:
6394 return 0;
6395 }
6396}
6397
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006398/* This function is an LUA binding used for registering
6399 * "sample-conv" functions. It expects a converter name used
6400 * in the haproxy configuration file, and an LUA function.
6401 */
6402__LJMP static int hlua_register_converters(lua_State *L)
6403{
6404 struct sample_conv_kw_list *sck;
6405 const char *name;
6406 int ref;
6407 int len;
6408 struct hlua_function *fcn;
6409
6410 MAY_LJMP(check_args(L, 2, "register_converters"));
6411
6412 /* First argument : converter name. */
6413 name = MAY_LJMP(luaL_checkstring(L, 1));
6414
6415 /* Second argument : lua function. */
6416 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6417
6418 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006419 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006420 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006421 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006422 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006423 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006424 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006425
6426 /* Fill fcn. */
6427 fcn->name = strdup(name);
6428 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006429 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006430 fcn->function_ref = ref;
6431
6432 /* List head */
6433 sck->list.n = sck->list.p = NULL;
6434
6435 /* converter keyword. */
6436 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006437 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006438 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006439 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006440
6441 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6442 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006443 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 +01006444 sck->kw[0].val_args = NULL;
6445 sck->kw[0].in_type = SMP_T_STR;
6446 sck->kw[0].out_type = SMP_T_STR;
6447 sck->kw[0].private = fcn;
6448
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006449 /* Register this new converter */
6450 sample_register_convs(sck);
6451
6452 return 0;
6453}
6454
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006455/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006456 * "sample-fetch" functions. It expects a converter name used
6457 * in the haproxy configuration file, and an LUA function.
6458 */
6459__LJMP static int hlua_register_fetches(lua_State *L)
6460{
6461 const char *name;
6462 int ref;
6463 int len;
6464 struct sample_fetch_kw_list *sfk;
6465 struct hlua_function *fcn;
6466
6467 MAY_LJMP(check_args(L, 2, "register_fetches"));
6468
6469 /* First argument : sample-fetch name. */
6470 name = MAY_LJMP(luaL_checkstring(L, 1));
6471
6472 /* Second argument : lua function. */
6473 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6474
6475 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006476 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006477 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006478 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006479 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006480 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006481 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006482
6483 /* Fill fcn. */
6484 fcn->name = strdup(name);
6485 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006486 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006487 fcn->function_ref = ref;
6488
6489 /* List head */
6490 sfk->list.n = sfk->list.p = NULL;
6491
6492 /* sample-fetch keyword. */
6493 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006494 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006495 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006496 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006497
6498 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6499 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006500 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 +01006501 sfk->kw[0].val_args = NULL;
6502 sfk->kw[0].out_type = SMP_T_STR;
6503 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6504 sfk->kw[0].val = 0;
6505 sfk->kw[0].private = fcn;
6506
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006507 /* Register this new fetch. */
6508 sample_register_fetches(sfk);
6509
6510 return 0;
6511}
6512
Christopher Faulet501465d2020-02-26 14:54:16 +01006513/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006514 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006515__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006516{
6517 struct hlua *hlua = hlua_gethlua(L);
6518 unsigned int delay;
6519 unsigned int wakeup_ms;
6520
6521 MAY_LJMP(check_args(L, 1, "wake_time"));
6522
6523 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6524 wakeup_ms = tick_add(now_ms, delay);
6525 hlua->wake_time = wakeup_ms;
6526 return 0;
6527}
6528
6529
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006530/* This function is a wrapper to execute each LUA function declared as an action
6531 * wrapper during the initialisation period. This function may return any
6532 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6533 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6534 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006535 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006536static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006537 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006538{
6539 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006540 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006541 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006542 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006543
6544 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006545 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6546 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6547 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6548 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006549 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006550 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006551 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006552 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006553
Willy Tarreau87b09662015-04-03 00:22:06 +02006554 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006555 * Lua context can be not initialized. This behavior
6556 * permits to save performances because a systematic
6557 * Lua initialization cause 5% performances loss.
6558 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006559 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006560 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006561 if (!s->hlua) {
6562 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6563 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006564 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006565 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006566 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006567 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6568 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006569 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006570 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006571 }
6572
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006573 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006574 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006575
6576 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006577 if (!SET_SAFE_LJMP(s->hlua->T)) {
6578 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6579 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006580 else
6581 error = "critical error";
6582 SEND_ERR(px, "Lua function '%s': %s.\n",
6583 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006584 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006585 }
6586
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006587 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006588 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006589 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006590 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006591 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006592 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006593 }
6594
6595 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006596 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006597
Willy Tarreau87b09662015-04-03 00:22:06 +02006598 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006599 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006600 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006601 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006602 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006603 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006604 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006605 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006606
6607 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006608 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006609 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006610 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006611 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006612 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006613 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006614 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006615 lua_pushstring(s->hlua->T, *arg);
6616 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006617 }
6618
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006619 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006620 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006621
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006622 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006623 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006624 }
6625
Christopher Faulet23308eb2020-06-02 18:46:07 +02006626 /* Always reset the analyse expiration timeout for the corresponding
6627 * channel in case the lua script yield, to be sure to not keep an
6628 * expired timeout.
6629 */
6630 if (dir == SMP_OPT_DIR_REQ)
6631 s->req.analyse_exp = TICK_ETERNITY;
6632 else
6633 s->res.analyse_exp = TICK_ETERNITY;
6634
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006635 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006636 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006637 /* finished. */
6638 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006639 /* Catch the return value */
6640 if (lua_gettop(s->hlua->T) > 0)
6641 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006642
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006643 /* Set timeout in the required channel. */
6644 if (act_ret == ACT_RET_YIELD && s->hlua->wake_time != TICK_ETERNITY) {
6645 if (dir == SMP_OPT_DIR_REQ)
6646 s->req.analyse_exp = s->hlua->wake_time;
6647 else
6648 s->res.analyse_exp = s->hlua->wake_time;
6649 }
6650 goto end;
6651
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006652 /* yield. */
6653 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006654 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006655 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Faulet81921b12019-08-14 23:19:45 +02006656 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006657 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006658 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006659 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006660 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006661 /* Some actions can be wake up when a "write" event
6662 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006663 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006664 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006665 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006666 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006667 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006668 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006669 act_ret = ACT_RET_YIELD;
6670 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006671
6672 /* finished with error. */
6673 case HLUA_E_ERRMSG:
6674 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006675 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006676 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6677 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006678 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006679
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006680 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006681 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006682 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006683
6684 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006685 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006686 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006687
6688 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006689 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6690 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006691 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006692
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006693 case HLUA_E_ERR:
6694 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006695 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006696 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006697
6698 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006699 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006700 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006701
6702 end:
6703 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006704}
6705
Olivier Houchard9f6af332018-05-25 14:04:04 +02006706struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006707{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006708 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006709
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006710 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006711 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006712 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006713}
6714
6715static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6716{
6717 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006718 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006719 struct task *task;
6720 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006721 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006722
Willy Tarreaubafbe012017-11-24 17:34:44 +01006723 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006724 if (!hlua) {
6725 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6726 ctx->rule->arg.hlua_rule->fcn.name);
6727 return 0;
6728 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006729 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006730 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006731 ctx->ctx.hlua_apptcp.flags = 0;
6732
6733 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006734 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006735 if (!task) {
6736 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6737 ctx->rule->arg.hlua_rule->fcn.name);
6738 return 0;
6739 }
6740 task->nice = 0;
6741 task->context = ctx;
6742 task->process = hlua_applet_wakeup;
6743 ctx->ctx.hlua_apptcp.task = task;
6744
6745 /* In the execution wrappers linked with a stream, the
6746 * Lua context can be not initialized. This behavior
6747 * permits to save performances because a systematic
6748 * Lua initialization cause 5% performances loss.
6749 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006750 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006751 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6752 ctx->rule->arg.hlua_rule->fcn.name);
6753 return 0;
6754 }
6755
6756 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006757 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006758
6759 /* The following Lua calls can fail. */
6760 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006761 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6762 error = lua_tostring(hlua->T, -1);
6763 else
6764 error = "critical error";
6765 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6766 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006767 return 0;
6768 }
6769
6770 /* Check stack available size. */
6771 if (!lua_checkstack(hlua->T, 1)) {
6772 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6773 ctx->rule->arg.hlua_rule->fcn.name);
6774 RESET_SAFE_LJMP(hlua->T);
6775 return 0;
6776 }
6777
6778 /* Restore the function in the stack. */
6779 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6780
6781 /* Create and and push object stream in the stack. */
6782 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6783 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6784 ctx->rule->arg.hlua_rule->fcn.name);
6785 RESET_SAFE_LJMP(hlua->T);
6786 return 0;
6787 }
6788 hlua->nargs = 1;
6789
6790 /* push keywords in the stack. */
6791 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6792 if (!lua_checkstack(hlua->T, 1)) {
6793 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6794 ctx->rule->arg.hlua_rule->fcn.name);
6795 RESET_SAFE_LJMP(hlua->T);
6796 return 0;
6797 }
6798 lua_pushstring(hlua->T, *arg);
6799 hlua->nargs++;
6800 }
6801
6802 RESET_SAFE_LJMP(hlua->T);
6803
6804 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006805 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006806 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006807
6808 return 1;
6809}
6810
Willy Tarreau60409db2019-08-21 14:14:50 +02006811void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006812{
6813 struct stream_interface *si = ctx->owner;
6814 struct stream *strm = si_strm(si);
6815 struct channel *res = si_ic(si);
6816 struct act_rule *rule = ctx->rule;
6817 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006818 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006819
6820 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006821 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6822 /* eat the whole request */
6823 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006824 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006825 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006826
6827 /* If the stream is disconnect or closed, ldo nothing. */
6828 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6829 return;
6830
6831 /* Execute the function. */
6832 switch (hlua_ctx_resume(hlua, 1)) {
6833 /* finished. */
6834 case HLUA_E_OK:
6835 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6836
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006837 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006838 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006839 res->flags |= CF_READ_NULL;
6840 si_shutr(si);
6841 return;
6842
6843 /* yield. */
6844 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006845 if (hlua->wake_time != TICK_ETERNITY)
6846 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006847 return;
6848
6849 /* finished with error. */
6850 case HLUA_E_ERRMSG:
6851 /* Display log. */
6852 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6853 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6854 lua_pop(hlua->T, 1);
6855 goto error;
6856
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006857 case HLUA_E_ETMOUT:
6858 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6859 rule->arg.hlua_rule->fcn.name);
6860 goto error;
6861
6862 case HLUA_E_NOMEM:
6863 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6864 rule->arg.hlua_rule->fcn.name);
6865 goto error;
6866
6867 case HLUA_E_YIELD: /* unexpected */
6868 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6869 rule->arg.hlua_rule->fcn.name);
6870 goto error;
6871
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006872 case HLUA_E_ERR:
6873 /* Display log. */
6874 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6875 rule->arg.hlua_rule->fcn.name);
6876 goto error;
6877
6878 default:
6879 goto error;
6880 }
6881
6882error:
6883
6884 /* For all other cases, just close the stream. */
6885 si_shutw(si);
6886 si_shutr(si);
6887 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6888}
6889
6890static void hlua_applet_tcp_release(struct appctx *ctx)
6891{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006892 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006893 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006894 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006895 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006896}
6897
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006898/* The function returns 1 if the initialisation is complete, 0 if
6899 * an errors occurs and -1 if more data are required for initializing
6900 * the applet.
6901 */
6902static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6903{
6904 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006905 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006906 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006907 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006908 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006909 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006910
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006911 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006912 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006913 if (!hlua) {
6914 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6915 ctx->rule->arg.hlua_rule->fcn.name);
6916 return 0;
6917 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006918 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006919 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006920 ctx->ctx.hlua_apphttp.left_bytes = -1;
6921 ctx->ctx.hlua_apphttp.flags = 0;
6922
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006923 if (txn->req.flags & HTTP_MSGF_VER_11)
6924 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6925
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006926 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006927 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006928 if (!task) {
6929 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6930 ctx->rule->arg.hlua_rule->fcn.name);
6931 return 0;
6932 }
6933 task->nice = 0;
6934 task->context = ctx;
6935 task->process = hlua_applet_wakeup;
6936 ctx->ctx.hlua_apphttp.task = task;
6937
6938 /* In the execution wrappers linked with a stream, the
6939 * Lua context can be not initialized. This behavior
6940 * permits to save performances because a systematic
6941 * Lua initialization cause 5% performances loss.
6942 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006943 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006944 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6945 ctx->rule->arg.hlua_rule->fcn.name);
6946 return 0;
6947 }
6948
6949 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006950 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006951
6952 /* The following Lua calls can fail. */
6953 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006954 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6955 error = lua_tostring(hlua->T, -1);
6956 else
6957 error = "critical error";
6958 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6959 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006960 return 0;
6961 }
6962
6963 /* Check stack available size. */
6964 if (!lua_checkstack(hlua->T, 1)) {
6965 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6966 ctx->rule->arg.hlua_rule->fcn.name);
6967 RESET_SAFE_LJMP(hlua->T);
6968 return 0;
6969 }
6970
6971 /* Restore the function in the stack. */
6972 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6973
6974 /* Create and and push object stream in the stack. */
6975 if (!hlua_applet_http_new(hlua->T, ctx)) {
6976 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6977 ctx->rule->arg.hlua_rule->fcn.name);
6978 RESET_SAFE_LJMP(hlua->T);
6979 return 0;
6980 }
6981 hlua->nargs = 1;
6982
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006983 /* push keywords in the stack. */
6984 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6985 if (!lua_checkstack(hlua->T, 1)) {
6986 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6987 ctx->rule->arg.hlua_rule->fcn.name);
6988 RESET_SAFE_LJMP(hlua->T);
6989 return 0;
6990 }
6991 lua_pushstring(hlua->T, *arg);
6992 hlua->nargs++;
6993 }
6994
6995 RESET_SAFE_LJMP(hlua->T);
6996
6997 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006998 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006999
7000 return 1;
7001}
7002
Willy Tarreau60409db2019-08-21 14:14:50 +02007003void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007004{
7005 struct stream_interface *si = ctx->owner;
7006 struct stream *strm = si_strm(si);
7007 struct channel *req = si_oc(si);
7008 struct channel *res = si_ic(si);
7009 struct act_rule *rule = ctx->rule;
7010 struct proxy *px = strm->be;
7011 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7012 struct htx *req_htx, *res_htx;
7013
7014 res_htx = htx_from_buf(&res->buf);
7015
7016 /* If the stream is disconnect or closed, ldo nothing. */
7017 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7018 goto out;
7019
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007020 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007021 if (!b_size(&res->buf)) {
7022 si_rx_room_blk(si);
7023 goto out;
7024 }
7025 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007026 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007027 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7028
7029 /* Set the currently running flag. */
7030 if (!HLUA_IS_RUNNING(hlua) &&
7031 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7032 struct htx_blk *blk;
7033 size_t count = co_data(req);
7034
7035 if (!count) {
7036 si_cant_get(si);
7037 goto out;
7038 }
7039
7040 /* We need to flush the request header. This left the body for
7041 * the Lua.
7042 */
7043 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007044 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007045 while (count && blk) {
7046 enum htx_blk_type type = htx_get_blk_type(blk);
7047 uint32_t sz = htx_get_blksz(blk);
7048
7049 if (sz > count) {
7050 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007051 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007052 goto out;
7053 }
7054
7055 count -= sz;
7056 co_set_data(req, co_data(req) - sz);
7057 blk = htx_remove_blk(req_htx, blk);
7058
7059 if (type == HTX_BLK_EOH)
7060 break;
7061 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007062 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007063 }
7064
7065 /* Executes The applet if it is not done. */
7066 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7067
7068 /* Execute the function. */
7069 switch (hlua_ctx_resume(hlua, 1)) {
7070 /* finished. */
7071 case HLUA_E_OK:
7072 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7073 break;
7074
7075 /* yield. */
7076 case HLUA_E_AGAIN:
7077 if (hlua->wake_time != TICK_ETERNITY)
7078 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007079 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007080
7081 /* finished with error. */
7082 case HLUA_E_ERRMSG:
7083 /* Display log. */
7084 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7085 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7086 lua_pop(hlua->T, 1);
7087 goto error;
7088
7089 case HLUA_E_ETMOUT:
7090 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7091 rule->arg.hlua_rule->fcn.name);
7092 goto error;
7093
7094 case HLUA_E_NOMEM:
7095 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7096 rule->arg.hlua_rule->fcn.name);
7097 goto error;
7098
7099 case HLUA_E_YIELD: /* unexpected */
7100 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7101 rule->arg.hlua_rule->fcn.name);
7102 goto error;
7103
7104 case HLUA_E_ERR:
7105 /* Display log. */
7106 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7107 rule->arg.hlua_rule->fcn.name);
7108 goto error;
7109
7110 default:
7111 goto error;
7112 }
7113 }
7114
7115 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007116 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7117 goto done;
7118
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007119 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7120 goto error;
7121
Christopher Faulet54b5e212019-06-04 10:08:28 +02007122 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007123 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7124 si_rx_room_blk(si);
7125 goto out;
7126 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007127 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007128 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7129 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007130 }
7131
7132 done:
7133 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007134 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007135 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007136 si_shutr(si);
7137 }
7138
7139 /* eat the whole request */
7140 if (co_data(req)) {
7141 req_htx = htx_from_buf(&req->buf);
7142 co_htx_skip(req, req_htx, co_data(req));
7143 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007144 }
7145 }
7146
7147 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007148 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007149 return;
7150
7151 error:
7152
7153 /* If we are in HTTP mode, and we are not send any
7154 * data, return a 500 server error in best effort:
7155 * if there is no room available in the buffer,
7156 * just close the connection.
7157 */
7158 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007159 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007160
7161 channel_erase(res);
7162 res->buf.data = b_data(err);
7163 memcpy(res->buf.area, b_head(err), b_data(err));
7164 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007165 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007166 }
7167 if (!(strm->flags & SF_ERR_MASK))
7168 strm->flags |= SF_ERR_RESOURCE;
7169 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7170 goto done;
7171}
7172
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007173static void hlua_applet_http_release(struct appctx *ctx)
7174{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007175 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007176 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007177 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007178 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007179}
7180
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007181/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007182 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007183 *
7184 * This function can fail with an abort() due to an Lua critical error.
7185 * We are in the configuration parsing process of HAProxy, this abort() is
7186 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007187 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007188static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7189 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007190{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007191 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007192 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007193
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007194 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007195 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007196 if (!rule->arg.hlua_rule) {
7197 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007198 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007199 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007200
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007201 /* Memory for arguments. */
7202 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7203 if (!rule->arg.hlua_rule->args) {
7204 memprintf(err, "out of memory error");
7205 return ACT_RET_PRS_ERR;
7206 }
7207
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007208 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007209 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007210
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007211 /* Expect some arguments */
7212 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007213 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007214 memprintf(err, "expect %d arguments", fcn->nargs);
7215 return ACT_RET_PRS_ERR;
7216 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007217 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007218 if (!rule->arg.hlua_rule->args[i]) {
7219 memprintf(err, "out of memory error");
7220 return ACT_RET_PRS_ERR;
7221 }
7222 (*cur_arg)++;
7223 }
7224 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007225
Thierry FOURNIER42148732015-09-02 17:17:33 +02007226 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007227 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007228 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007229}
7230
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007231static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7232 struct act_rule *rule, char **err)
7233{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007234 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007235
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007236 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007237 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007238 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007239 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007240 * the call of this analyzer.
7241 */
7242 if (rule->from != ACT_F_HTTP_REQ) {
7243 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7244 return ACT_RET_PRS_ERR;
7245 }
7246
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007247 /* Memory for the rule. */
7248 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7249 if (!rule->arg.hlua_rule) {
7250 memprintf(err, "out of memory error");
7251 return ACT_RET_PRS_ERR;
7252 }
7253
7254 /* Reference the Lua function and store the reference. */
7255 rule->arg.hlua_rule->fcn = *fcn;
7256
7257 /* TODO: later accept arguments. */
7258 rule->arg.hlua_rule->args = NULL;
7259
7260 /* Add applet pointer in the rule. */
7261 rule->applet.obj_type = OBJ_TYPE_APPLET;
7262 rule->applet.name = fcn->name;
7263 rule->applet.init = hlua_applet_http_init;
7264 rule->applet.fct = hlua_applet_http_fct;
7265 rule->applet.release = hlua_applet_http_release;
7266 rule->applet.timeout = hlua_timeout_applet;
7267
7268 return ACT_RET_PRS_OK;
7269}
7270
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007271/* This function is an LUA binding used for registering
7272 * "sample-conv" functions. It expects a converter name used
7273 * in the haproxy configuration file, and an LUA function.
7274 */
7275__LJMP static int hlua_register_action(lua_State *L)
7276{
7277 struct action_kw_list *akl;
7278 const char *name;
7279 int ref;
7280 int len;
7281 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007282 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007283
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007284 /* Initialise the number of expected arguments at 0. */
7285 nargs = 0;
7286
7287 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7288 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007289
7290 /* First argument : converter name. */
7291 name = MAY_LJMP(luaL_checkstring(L, 1));
7292
7293 /* Second argument : environment. */
7294 if (lua_type(L, 2) != LUA_TTABLE)
7295 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7296
7297 /* Third argument : lua function. */
7298 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7299
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007300 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007301 if (lua_gettop(L) >= 4)
7302 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7303
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007304 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007305 lua_pushnil(L);
7306 while (lua_next(L, 2) != 0) {
7307 if (lua_type(L, -1) != LUA_TSTRING)
7308 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7309
7310 /* Check required environment. Only accepted "http" or "tcp". */
7311 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007312 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007313 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007314 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007315 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007316 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007317 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007318
7319 /* Fill fcn. */
7320 fcn->name = strdup(name);
7321 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007322 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007323 fcn->function_ref = ref;
7324
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007325 /* Set the expected number od arguments. */
7326 fcn->nargs = nargs;
7327
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007328 /* List head */
7329 akl->list.n = akl->list.p = NULL;
7330
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007331 /* action keyword. */
7332 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007333 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007334 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007335 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007336
7337 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7338
7339 akl->kw[0].match_pfx = 0;
7340 akl->kw[0].private = fcn;
7341 akl->kw[0].parse = action_register_lua;
7342
7343 /* select the action registering point. */
7344 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7345 tcp_req_cont_keywords_register(akl);
7346 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7347 tcp_res_cont_keywords_register(akl);
7348 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7349 http_req_keywords_register(akl);
7350 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7351 http_res_keywords_register(akl);
7352 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007353 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007354 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7355 "are expected.", lua_tostring(L, -1)));
7356
7357 /* pop the environment string. */
7358 lua_pop(L, 1);
7359 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007360 return ACT_RET_PRS_OK;
7361}
7362
7363static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7364 struct act_rule *rule, char **err)
7365{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007366 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007367
Christopher Faulet280f85b2019-07-15 15:02:04 +02007368 if (px->mode == PR_MODE_HTTP) {
7369 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007370 return ACT_RET_PRS_ERR;
7371 }
7372
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007373 /* Memory for the rule. */
7374 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7375 if (!rule->arg.hlua_rule) {
7376 memprintf(err, "out of memory error");
7377 return ACT_RET_PRS_ERR;
7378 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007379
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007380 /* Reference the Lua function and store the reference. */
7381 rule->arg.hlua_rule->fcn = *fcn;
7382
7383 /* TODO: later accept arguments. */
7384 rule->arg.hlua_rule->args = NULL;
7385
7386 /* Add applet pointer in the rule. */
7387 rule->applet.obj_type = OBJ_TYPE_APPLET;
7388 rule->applet.name = fcn->name;
7389 rule->applet.init = hlua_applet_tcp_init;
7390 rule->applet.fct = hlua_applet_tcp_fct;
7391 rule->applet.release = hlua_applet_tcp_release;
7392 rule->applet.timeout = hlua_timeout_applet;
7393
7394 return 0;
7395}
7396
7397/* This function is an LUA binding used for registering
7398 * "sample-conv" functions. It expects a converter name used
7399 * in the haproxy configuration file, and an LUA function.
7400 */
7401__LJMP static int hlua_register_service(lua_State *L)
7402{
7403 struct action_kw_list *akl;
7404 const char *name;
7405 const char *env;
7406 int ref;
7407 int len;
7408 struct hlua_function *fcn;
7409
7410 MAY_LJMP(check_args(L, 3, "register_service"));
7411
7412 /* First argument : converter name. */
7413 name = MAY_LJMP(luaL_checkstring(L, 1));
7414
7415 /* Second argument : environment. */
7416 env = MAY_LJMP(luaL_checkstring(L, 2));
7417
7418 /* Third argument : lua function. */
7419 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7420
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007421 /* Allocate and fill the sample fetch keyword struct. */
7422 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7423 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007424 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007425 fcn = calloc(1, sizeof(*fcn));
7426 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007427 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007428
7429 /* Fill fcn. */
7430 len = strlen("<lua.>") + strlen(name) + 1;
7431 fcn->name = calloc(1, len);
7432 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007433 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007434 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7435 fcn->function_ref = ref;
7436
7437 /* List head */
7438 akl->list.n = akl->list.p = NULL;
7439
7440 /* converter keyword. */
7441 len = strlen("lua.") + strlen(name) + 1;
7442 akl->kw[0].kw = calloc(1, len);
7443 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007444 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007445
7446 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7447
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007448 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007449 if (strcmp(env, "tcp") == 0)
7450 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007451 else if (strcmp(env, "http") == 0)
7452 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007453 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007454 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007455 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007456
7457 akl->kw[0].match_pfx = 0;
7458 akl->kw[0].private = fcn;
7459
7460 /* End of array. */
7461 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7462
7463 /* Register this new converter */
7464 service_keywords_register(akl);
7465
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007466 return 0;
7467}
7468
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007469/* This function initialises Lua cli handler. It copies the
7470 * arguments in the Lua stack and create channel IO objects.
7471 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007472static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007473{
7474 struct hlua *hlua;
7475 struct hlua_function *fcn;
7476 int i;
7477 const char *error;
7478
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007479 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007480 appctx->ctx.hlua_cli.fcn = private;
7481
Willy Tarreaubafbe012017-11-24 17:34:44 +01007482 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007483 if (!hlua) {
7484 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007485 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007486 }
7487 HLUA_INIT(hlua);
7488 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007489
7490 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007491 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007492 * applet_http. It is absolutely compatible.
7493 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007494 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007495 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007496 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007497 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007498 }
7499 appctx->ctx.hlua_cli.task->nice = 0;
7500 appctx->ctx.hlua_cli.task->context = appctx;
7501 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7502
7503 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007504 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007505 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007506 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007507 }
7508
7509 /* The following Lua calls can fail. */
7510 if (!SET_SAFE_LJMP(hlua->T)) {
7511 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7512 error = lua_tostring(hlua->T, -1);
7513 else
7514 error = "critical error";
7515 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7516 goto error;
7517 }
7518
7519 /* Check stack available size. */
7520 if (!lua_checkstack(hlua->T, 2)) {
7521 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7522 goto error;
7523 }
7524
7525 /* Restore the function in the stack. */
7526 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7527
7528 /* Once the arguments parsed, the CLI is like an AppletTCP,
7529 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007530 */
7531 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7532 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7533 goto error;
7534 }
7535 hlua->nargs = 1;
7536
7537 /* push keywords in the stack. */
7538 for (i = 0; *args[i]; i++) {
7539 /* Check stack available size. */
7540 if (!lua_checkstack(hlua->T, 1)) {
7541 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7542 goto error;
7543 }
7544 lua_pushstring(hlua->T, args[i]);
7545 hlua->nargs++;
7546 }
7547
7548 /* We must initialize the execution timeouts. */
7549 hlua->max_time = hlua_timeout_session;
7550
7551 /* At this point the execution is safe. */
7552 RESET_SAFE_LJMP(hlua->T);
7553
7554 /* It's ok */
7555 return 0;
7556
7557 /* It's not ok. */
7558error:
7559 RESET_SAFE_LJMP(hlua->T);
7560 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007561 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007562 return 1;
7563}
7564
7565static int hlua_cli_io_handler_fct(struct appctx *appctx)
7566{
7567 struct hlua *hlua;
7568 struct stream_interface *si;
7569 struct hlua_function *fcn;
7570
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007571 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007572 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007573 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007574
7575 /* If the stream is disconnect or closed, ldo nothing. */
7576 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7577 return 1;
7578
7579 /* Execute the function. */
7580 switch (hlua_ctx_resume(hlua, 1)) {
7581
7582 /* finished. */
7583 case HLUA_E_OK:
7584 return 1;
7585
7586 /* yield. */
7587 case HLUA_E_AGAIN:
7588 /* We want write. */
7589 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007590 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007591 /* Set the timeout. */
7592 if (hlua->wake_time != TICK_ETERNITY)
7593 task_schedule(hlua->task, hlua->wake_time);
7594 return 0;
7595
7596 /* finished with error. */
7597 case HLUA_E_ERRMSG:
7598 /* Display log. */
7599 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7600 fcn->name, lua_tostring(hlua->T, -1));
7601 lua_pop(hlua->T, 1);
7602 return 1;
7603
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007604 case HLUA_E_ETMOUT:
7605 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7606 fcn->name);
7607 return 1;
7608
7609 case HLUA_E_NOMEM:
7610 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7611 fcn->name);
7612 return 1;
7613
7614 case HLUA_E_YIELD: /* unexpected */
7615 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7616 fcn->name);
7617 return 1;
7618
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007619 case HLUA_E_ERR:
7620 /* Display log. */
7621 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7622 fcn->name);
7623 return 1;
7624
7625 default:
7626 return 1;
7627 }
7628
7629 return 1;
7630}
7631
7632static void hlua_cli_io_release_fct(struct appctx *appctx)
7633{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007634 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007635 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007636}
7637
7638/* This function is an LUA binding used for registering
7639 * new keywords in the cli. It expects a list of keywords
7640 * which are the "path". It is limited to 5 keywords. A
7641 * description of the command, a function to be executed
7642 * for the parsing and a function for io handlers.
7643 */
7644__LJMP static int hlua_register_cli(lua_State *L)
7645{
7646 struct cli_kw_list *cli_kws;
7647 const char *message;
7648 int ref_io;
7649 int len;
7650 struct hlua_function *fcn;
7651 int index;
7652 int i;
7653
7654 MAY_LJMP(check_args(L, 3, "register_cli"));
7655
7656 /* First argument : an array of maximum 5 keywords. */
7657 if (!lua_istable(L, 1))
7658 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7659
7660 /* Second argument : string with contextual message. */
7661 message = MAY_LJMP(luaL_checkstring(L, 2));
7662
7663 /* Third and fourth argument : lua function. */
7664 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7665
7666 /* Allocate and fill the sample fetch keyword struct. */
7667 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7668 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007669 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007670 fcn = calloc(1, sizeof(*fcn));
7671 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007672 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007673
7674 /* Fill path. */
7675 index = 0;
7676 lua_pushnil(L);
7677 while(lua_next(L, 1) != 0) {
7678 if (index >= 5)
7679 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7680 if (lua_type(L, -1) != LUA_TSTRING)
7681 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7682 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7683 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007684 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007685 index++;
7686 lua_pop(L, 1);
7687 }
7688
7689 /* Copy help message. */
7690 cli_kws->kw[0].usage = strdup(message);
7691 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007692 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007693
7694 /* Fill fcn io handler. */
7695 len = strlen("<lua.cli>") + 1;
7696 for (i = 0; i < index; i++)
7697 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7698 fcn->name = calloc(1, len);
7699 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007700 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007701 strncat((char *)fcn->name, "<lua.cli", len);
7702 for (i = 0; i < index; i++) {
7703 strncat((char *)fcn->name, ".", len);
7704 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7705 }
7706 strncat((char *)fcn->name, ">", len);
7707 fcn->function_ref = ref_io;
7708
7709 /* Fill last entries. */
7710 cli_kws->kw[0].private = fcn;
7711 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7712 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7713 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7714
7715 /* Register this new converter */
7716 cli_register_kw(cli_kws);
7717
7718 return 0;
7719}
7720
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007721static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7722 struct proxy *defpx, const char *file, int line,
7723 char **err, unsigned int *timeout)
7724{
7725 const char *error;
7726
7727 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007728 if (error == PARSE_TIME_OVER) {
7729 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7730 args[1], args[0]);
7731 return -1;
7732 }
7733 else if (error == PARSE_TIME_UNDER) {
7734 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7735 args[1], args[0]);
7736 return -1;
7737 }
7738 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007739 memprintf(err, "%s: invalid timeout", args[0]);
7740 return -1;
7741 }
7742 return 0;
7743}
7744
7745static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7746 struct proxy *defpx, const char *file, int line,
7747 char **err)
7748{
7749 return hlua_read_timeout(args, section_type, curpx, defpx,
7750 file, line, err, &hlua_timeout_session);
7751}
7752
7753static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7754 struct proxy *defpx, const char *file, int line,
7755 char **err)
7756{
7757 return hlua_read_timeout(args, section_type, curpx, defpx,
7758 file, line, err, &hlua_timeout_task);
7759}
7760
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007761static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7762 struct proxy *defpx, const char *file, int line,
7763 char **err)
7764{
7765 return hlua_read_timeout(args, section_type, curpx, defpx,
7766 file, line, err, &hlua_timeout_applet);
7767}
7768
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007769static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7770 struct proxy *defpx, const char *file, int line,
7771 char **err)
7772{
7773 char *error;
7774
7775 hlua_nb_instruction = strtoll(args[1], &error, 10);
7776 if (*error != '\0') {
7777 memprintf(err, "%s: invalid number", args[0]);
7778 return -1;
7779 }
7780 return 0;
7781}
7782
Willy Tarreau32f61e22015-03-18 17:54:59 +01007783static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7784 struct proxy *defpx, const char *file, int line,
7785 char **err)
7786{
7787 char *error;
7788
7789 if (*(args[1]) == 0) {
7790 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7791 return -1;
7792 }
7793 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7794 if (*error != '\0') {
7795 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7796 return -1;
7797 }
7798 return 0;
7799}
7800
7801
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007802/* This function is called by the main configuration key "lua-load". It loads and
7803 * execute an lua file during the parsing of the HAProxy configuration file. It is
7804 * the main lua entry point.
7805 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007806 * This function runs with the HAProxy keywords API. It returns -1 if an error
7807 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007808 *
7809 * In some error case, LUA set an error message in top of the stack. This function
7810 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007811 *
7812 * This function can fail with an abort() due to an Lua critical error.
7813 * We are in the configuration parsing process of HAProxy, this abort() is
7814 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007815 */
7816static int hlua_load(char **args, int section_type, struct proxy *curpx,
7817 struct proxy *defpx, const char *file, int line,
7818 char **err)
7819{
7820 int error;
7821
7822 /* Just load and compile the file. */
7823 error = luaL_loadfile(gL.T, args[1]);
7824 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007825 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007826 lua_pop(gL.T, 1);
7827 return -1;
7828 }
7829
7830 /* If no syntax error where detected, execute the code. */
7831 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7832 switch (error) {
7833 case LUA_OK:
7834 break;
7835 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007836 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007837 lua_pop(gL.T, 1);
7838 return -1;
7839 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007840 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007841 return -1;
7842 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007843 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007844 lua_pop(gL.T, 1);
7845 return -1;
7846 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007847 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007848 lua_pop(gL.T, 1);
7849 return -1;
7850 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05007851 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007852 lua_pop(gL.T, 1);
7853 return -1;
7854 }
7855
7856 return 0;
7857}
7858
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01007859/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
7860 * in the given <ctx>.
7861 */
7862static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
7863{
7864 lua_getglobal(ctx.T, "package"); /* push package variable */
7865 lua_pushstring(ctx.T, path); /* push given path */
7866 lua_pushstring(ctx.T, ";"); /* push semicolon */
7867 lua_getfield(ctx.T, -3, type); /* push old path */
7868 lua_concat(ctx.T, 3); /* concatenate to new path */
7869 lua_setfield(ctx.T, -2, type); /* store new path */
7870 lua_pop(ctx.T, 1); /* pop package variable */
7871
7872 return 0;
7873}
7874
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007875static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
7876 struct proxy *defpx, const char *file, int line,
7877 char **err)
7878{
7879 char *path;
7880 char *type = "path";
7881 if (too_many_args(2, args, err, NULL)) {
7882 return -1;
7883 }
7884
7885 if (!(*args[1])) {
7886 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
7887 return -1;
7888 }
7889 path = args[1];
7890
7891 if (*args[2]) {
7892 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
7893 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
7894 return -1;
7895 }
7896 type = args[2];
7897 }
7898
7899 return hlua_prepend_path(gL, type, path);
7900}
7901
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007902/* configuration keywords declaration */
7903static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007904 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007905 { CFG_GLOBAL, "lua-load", hlua_load },
7906 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7907 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007908 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007909 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007910 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007911 { 0, NULL, NULL },
7912}};
7913
Willy Tarreau0108d902018-11-25 19:14:37 +01007914INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7915
Christopher Fauletafd8f102018-11-08 11:34:21 +01007916
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007917/* This function can fail with an abort() due to an Lua critical error.
7918 * We are in the initialisation process of HAProxy, this abort() is
7919 * tolerated.
7920 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007921int hlua_post_init()
7922{
7923 struct hlua_init_function *init;
7924 const char *msg;
7925 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007926 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007927
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007928 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007929 if (!SET_SAFE_LJMP(gL.T)) {
7930 if (lua_type(gL.T, -1) == LUA_TSTRING)
7931 error = lua_tostring(gL.T, -1);
7932 else
7933 error = "critical error";
7934 fprintf(stderr, "Lua post-init: %s.\n", error);
7935 exit(1);
7936 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007937
7938#if USE_OPENSSL
7939 /* Initialize SSL server. */
7940 if (socket_ssl.xprt->prepare_srv) {
7941 int saved_used_backed = global.ssl_used_backend;
7942 // don't affect maxconn automatic computation
7943 socket_ssl.xprt->prepare_srv(&socket_ssl);
7944 global.ssl_used_backend = saved_used_backed;
7945 }
7946#endif
7947
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007948 hlua_fcn_post_init(gL.T);
7949 RESET_SAFE_LJMP(gL.T);
7950
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007951 list_for_each_entry(init, &hlua_init_functions, l) {
7952 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7953 ret = hlua_ctx_resume(&gL, 0);
7954 switch (ret) {
7955 case HLUA_E_OK:
7956 lua_pop(gL.T, -1);
7957 return 1;
7958 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007959 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007960 return 0;
7961 case HLUA_E_ERRMSG:
7962 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007963 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007964 return 0;
7965 case HLUA_E_ERR:
7966 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007967 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007968 return 0;
7969 }
7970 }
7971 return 1;
7972}
7973
Willy Tarreau32f61e22015-03-18 17:54:59 +01007974/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7975 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7976 * is the previously allocated size or the kind of object in case of a new
7977 * allocation. <nsize> is the requested new size.
7978 */
7979static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7980{
7981 struct hlua_mem_allocator *zone = ud;
7982
7983 if (nsize == 0) {
7984 /* it's a free */
7985 if (ptr)
7986 zone->allocated -= osize;
7987 free(ptr);
7988 return NULL;
7989 }
7990
7991 if (!ptr) {
7992 /* it's a new allocation */
7993 if (zone->limit && zone->allocated + nsize > zone->limit)
7994 return NULL;
7995
7996 ptr = malloc(nsize);
7997 if (ptr)
7998 zone->allocated += nsize;
7999 return ptr;
8000 }
8001
8002 /* it's a realloc */
8003 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8004 return NULL;
8005
8006 ptr = realloc(ptr, nsize);
8007 if (ptr)
8008 zone->allocated += nsize - osize;
8009 return ptr;
8010}
8011
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008012/* Ithis function can fail with an abort() due to an Lua critical error.
8013 * We are in the initialisation process of HAProxy, this abort() is
8014 * tolerated.
8015 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008016void hlua_init(void)
8017{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008018 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008019 int idx;
8020 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008021 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008022 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008023 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008024#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008025 struct srv_kw *kw;
8026 int tmp_error;
8027 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008028 char *args[] = { /* SSL client configuration. */
8029 "ssl",
8030 "verify",
8031 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008032 NULL
8033 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008034#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008035
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008036 /* Init main lua stack. */
8037 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008038 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008039 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008040 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008041 hlua_sethlua(&gL);
8042 gL.Tref = LUA_REFNIL;
8043 gL.task = NULL;
8044
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008045 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008046 * the Lua function can fail with an abort. We are in the initialisation
8047 * process of HAProxy, this abort() is tolerated.
8048 */
8049
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008050 /* Initialise lua. */
8051 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008052#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8053#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8054#ifdef HLUA_PREPEND_PATH
8055 hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
8056#endif
8057#ifdef HLUA_PREPEND_CPATH
8058 hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
8059#endif
8060#undef HLUA_PREPEND_PATH_TOSTRING
8061#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008062
Thierry Fournier75933d42016-01-21 09:30:18 +01008063 /* Set safe environment for the initialisation. */
8064 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008065 if (lua_type(gL.T, -1) == LUA_TSTRING)
8066 error_msg = lua_tostring(gL.T, -1);
8067 else
8068 error_msg = "critical error";
8069 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008070 exit(1);
8071 }
8072
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008073 /*
8074 *
8075 * Create "core" object.
8076 *
8077 */
8078
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008079 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008080 lua_newtable(gL.T);
8081
8082 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008083 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008084 hlua_class_const_int(gL.T, log_levels[i], i);
8085
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008086 /* Register special functions. */
8087 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008088 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008089 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008090 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008091 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008092 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008093 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008094 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008095 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008096 hlua_class_function(gL.T, "sleep", hlua_sleep);
8097 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008098 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8099 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8100 hlua_class_function(gL.T, "set_map", hlua_set_map);
8101 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008102 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008103 hlua_class_function(gL.T, "log", hlua_log);
8104 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8105 hlua_class_function(gL.T, "Info", hlua_log_info);
8106 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8107 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008108 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008109 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008110
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008111 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008112
8113 /*
8114 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008115 * Create "act" object.
8116 *
8117 */
8118
8119 /* This table entry is the object "act" base. */
8120 lua_newtable(gL.T);
8121
8122 /* push action return constants */
8123 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8124 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8125 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8126 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8127 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8128 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8129 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8130 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8131
Christopher Faulet501465d2020-02-26 14:54:16 +01008132 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008133
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008134 lua_setglobal(gL.T, "act");
8135
8136 /*
8137 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008138 * Register class Map
8139 *
8140 */
8141
8142 /* This table entry is the object "Map" base. */
8143 lua_newtable(gL.T);
8144
8145 /* register pattern types. */
8146 for (i=0; i<PAT_MATCH_NUM; i++)
8147 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008148 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008149 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8150 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008151 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008152
8153 /* register constructor. */
8154 hlua_class_function(gL.T, "new", hlua_map_new);
8155
8156 /* Create and fill the metatable. */
8157 lua_newtable(gL.T);
8158
Ilya Shipitsind4259502020-04-08 01:07:56 +05008159 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008160 lua_pushstring(gL.T, "__index");
8161 lua_newtable(gL.T);
8162
8163 /* Register . */
8164 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8165 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8166
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008167 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008168
Thierry Fournier45e78d72016-02-19 18:34:46 +01008169 /* Register previous table in the registry with reference and named entry.
8170 * The function hlua_register_metatable() pops the stack, so we
8171 * previously create a copy of the table.
8172 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008173 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008174 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008175
8176 /* Assign the metatable to the mai Map object. */
8177 lua_setmetatable(gL.T, -2);
8178
8179 /* Set a name to the table. */
8180 lua_setglobal(gL.T, "Map");
8181
8182 /*
8183 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008184 * Register class Channel
8185 *
8186 */
8187
8188 /* Create and fill the metatable. */
8189 lua_newtable(gL.T);
8190
Ilya Shipitsind4259502020-04-08 01:07:56 +05008191 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008192 lua_pushstring(gL.T, "__index");
8193 lua_newtable(gL.T);
8194
8195 /* Register . */
8196 hlua_class_function(gL.T, "get", hlua_channel_get);
8197 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8198 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8199 hlua_class_function(gL.T, "set", hlua_channel_set);
8200 hlua_class_function(gL.T, "append", hlua_channel_append);
8201 hlua_class_function(gL.T, "send", hlua_channel_send);
8202 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8203 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8204 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008205 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008206 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008207
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008208 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008209
8210 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008211 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008212
8213 /*
8214 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008215 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008216 *
8217 */
8218
8219 /* Create and fill the metatable. */
8220 lua_newtable(gL.T);
8221
Ilya Shipitsind4259502020-04-08 01:07:56 +05008222 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008223 lua_pushstring(gL.T, "__index");
8224 lua_newtable(gL.T);
8225
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008226 /* Browse existing fetches and create the associated
8227 * object method.
8228 */
8229 sf = NULL;
8230 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8231
8232 /* Dont register the keywork if the arguments check function are
8233 * not safe during the runtime.
8234 */
8235 if ((sf->val_args != NULL) &&
8236 (sf->val_args != val_payload_lv) &&
8237 (sf->val_args != val_hdr))
8238 continue;
8239
8240 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8241 * by an underscore.
8242 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008243 strncpy(trash.area, sf->kw, trash.size);
8244 trash.area[trash.size - 1] = '\0';
8245 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008246 if (*p == '.' || *p == '-' || *p == '+')
8247 *p = '_';
8248
8249 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008250 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008251 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008252 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008253 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008254 }
8255
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008256 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008257
8258 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008259 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008260
8261 /*
8262 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008263 * Register class Converters
8264 *
8265 */
8266
8267 /* Create and fill the metatable. */
8268 lua_newtable(gL.T);
8269
8270 /* Create and fill the __index entry. */
8271 lua_pushstring(gL.T, "__index");
8272 lua_newtable(gL.T);
8273
8274 /* Browse existing converters and create the associated
8275 * object method.
8276 */
8277 sc = NULL;
8278 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8279 /* Dont register the keywork if the arguments check function are
8280 * not safe during the runtime.
8281 */
8282 if (sc->val_args != NULL)
8283 continue;
8284
8285 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8286 * by an underscore.
8287 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008288 strncpy(trash.area, sc->kw, trash.size);
8289 trash.area[trash.size - 1] = '\0';
8290 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008291 if (*p == '.' || *p == '-' || *p == '+')
8292 *p = '_';
8293
8294 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008295 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008296 lua_pushlightuserdata(gL.T, sc);
8297 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008298 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008299 }
8300
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008301 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008302
8303 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008304 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008305
8306 /*
8307 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008308 * Register class HTTP
8309 *
8310 */
8311
8312 /* Create and fill the metatable. */
8313 lua_newtable(gL.T);
8314
Ilya Shipitsind4259502020-04-08 01:07:56 +05008315 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008316 lua_pushstring(gL.T, "__index");
8317 lua_newtable(gL.T);
8318
8319 /* Register Lua functions. */
8320 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8321 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8322 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8323 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8324 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8325 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8326 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8327 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8328 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8329 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8330
8331 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8332 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8333 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8334 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8335 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8336 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008337 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008338
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008339 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008340
8341 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008342 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008343
8344 /*
8345 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008346 * Register class AppletTCP
8347 *
8348 */
8349
8350 /* Create and fill the metatable. */
8351 lua_newtable(gL.T);
8352
Ilya Shipitsind4259502020-04-08 01:07:56 +05008353 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008354 lua_pushstring(gL.T, "__index");
8355 lua_newtable(gL.T);
8356
8357 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008358 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8359 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8360 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8361 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8362 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008363 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8364 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8365 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008366
8367 lua_settable(gL.T, -3);
8368
8369 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008370 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008371
8372 /*
8373 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008374 * Register class AppletHTTP
8375 *
8376 */
8377
8378 /* Create and fill the metatable. */
8379 lua_newtable(gL.T);
8380
Ilya Shipitsind4259502020-04-08 01:07:56 +05008381 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008382 lua_pushstring(gL.T, "__index");
8383 lua_newtable(gL.T);
8384
8385 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008386 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8387 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008388 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8389 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8390 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008391 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8392 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8393 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8394 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8395 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8396 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8397
8398 lua_settable(gL.T, -3);
8399
8400 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008401 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008402
8403 /*
8404 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008405 * Register class TXN
8406 *
8407 */
8408
8409 /* Create and fill the metatable. */
8410 lua_newtable(gL.T);
8411
Ilya Shipitsind4259502020-04-08 01:07:56 +05008412 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008413 lua_pushstring(gL.T, "__index");
8414 lua_newtable(gL.T);
8415
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008416 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008417 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8418 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8419 hlua_class_function(gL.T, "set_var", hlua_set_var);
8420 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8421 hlua_class_function(gL.T, "get_var", hlua_get_var);
8422 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008423 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008424 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8425 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8426 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8427 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8428 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8429 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8430 hlua_class_function(gL.T, "log", hlua_txn_log);
8431 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8432 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8433 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8434 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008435
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008436 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008437
8438 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008439 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008440
8441 /*
8442 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008443 * Register class reply
8444 *
8445 */
8446 lua_newtable(gL.T);
8447 lua_pushstring(gL.T, "__index");
8448 lua_newtable(gL.T);
8449 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8450 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8451 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8452 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8453 lua_settable(gL.T, -3); /* Sets the __index entry. */
8454 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8455
8456
8457 /*
8458 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008459 * Register class Socket
8460 *
8461 */
8462
8463 /* Create and fill the metatable. */
8464 lua_newtable(gL.T);
8465
Ilya Shipitsind4259502020-04-08 01:07:56 +05008466 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008467 lua_pushstring(gL.T, "__index");
8468 lua_newtable(gL.T);
8469
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008470#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008471 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008472#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008473 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8474 hlua_class_function(gL.T, "send", hlua_socket_send);
8475 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8476 hlua_class_function(gL.T, "close", hlua_socket_close);
8477 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8478 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8479 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8480 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8481
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008482 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008483
8484 /* Register the garbage collector entry. */
8485 lua_pushstring(gL.T, "__gc");
8486 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008487 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008488
8489 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008490 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008491
8492 /* Proxy and server configuration initialisation. */
8493 memset(&socket_proxy, 0, sizeof(socket_proxy));
8494 init_new_proxy(&socket_proxy);
8495 socket_proxy.parent = NULL;
8496 socket_proxy.last_change = now.tv_sec;
8497 socket_proxy.id = "LUA-SOCKET";
8498 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8499 socket_proxy.maxconn = 0;
8500 socket_proxy.accept = NULL;
8501 socket_proxy.options2 |= PR_O2_INDEPSTR;
8502 socket_proxy.srv = NULL;
8503 socket_proxy.conn_retries = 0;
8504 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8505
8506 /* Init TCP server: unchanged parameters */
8507 memset(&socket_tcp, 0, sizeof(socket_tcp));
8508 socket_tcp.next = NULL;
8509 socket_tcp.proxy = &socket_proxy;
8510 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8511 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008512 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008513 socket_tcp.idle_conns = NULL;
8514 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008515 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008516 socket_tcp.last_change = 0;
8517 socket_tcp.id = "LUA-TCP-CONN";
8518 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8519 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8520 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8521
8522 /* XXX: Copy default parameter from default server,
8523 * but the default server is not initialized.
8524 */
8525 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8526 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8527 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8528 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8529 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8530 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8531 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8532 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8533 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8534 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8535
8536 socket_tcp.check.status = HCHK_STATUS_INI;
8537 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8538 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8539 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8540 socket_tcp.check.server = &socket_tcp;
8541
8542 socket_tcp.agent.status = HCHK_STATUS_INI;
8543 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8544 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8545 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8546 socket_tcp.agent.server = &socket_tcp;
8547
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008548 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008549
8550#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008551 /* Init TCP server: unchanged parameters */
8552 memset(&socket_ssl, 0, sizeof(socket_ssl));
8553 socket_ssl.next = NULL;
8554 socket_ssl.proxy = &socket_proxy;
8555 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8556 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008557 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008558 socket_ssl.idle_conns = NULL;
8559 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008560 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008561 socket_ssl.last_change = 0;
8562 socket_ssl.id = "LUA-SSL-CONN";
8563 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8564 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8565 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8566
8567 /* XXX: Copy default parameter from default server,
8568 * but the default server is not initialized.
8569 */
8570 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8571 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8572 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8573 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8574 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8575 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8576 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8577 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8578 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8579 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8580
8581 socket_ssl.check.status = HCHK_STATUS_INI;
8582 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8583 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8584 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8585 socket_ssl.check.server = &socket_ssl;
8586
8587 socket_ssl.agent.status = HCHK_STATUS_INI;
8588 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8589 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8590 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8591 socket_ssl.agent.server = &socket_ssl;
8592
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008593 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008594 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008595
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008596 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008597 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8598 /*
8599 *
8600 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008601 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008602 * features like client certificates and ssl_verify.
8603 *
8604 */
8605 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8606 if (tmp_error != 0) {
8607 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8608 abort(); /* This must be never arrives because the command line
8609 not editable by the user. */
8610 }
8611 idx += kw->skip;
8612 }
8613 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008614#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008615
8616 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008617}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008618
Willy Tarreau80713382018-11-26 10:19:54 +01008619static void hlua_register_build_options(void)
8620{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008621 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008622
Willy Tarreaubb57d942016-12-21 19:04:56 +01008623 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8624 hap_register_build_opts(ptr, 1);
8625}
Willy Tarreau80713382018-11-26 10:19:54 +01008626
8627INITCALL0(STG_REGISTER, hlua_register_build_options);