blob: fd5ba062eab2054b6685245f9300312cb32ad728 [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
Willy Tarreaudfd3de82020-06-04 23:46:14 +020028#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020029#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020030#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020031#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020032#include <haproxy/connection.h>
Willy Tarreau87735332020-06-04 09:08:41 +020033#include <haproxy/http_htx.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020034#include <haproxy/thread.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020035#include <haproxy/regex.h>
Willy Tarreau374b4422020-06-02 17:46:16 +020036#include <haproxy/xref.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020037#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020038#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020039#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020040#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020041#include <haproxy/http_fetch.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020042#include <haproxy/http_rules.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020043#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020044#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020045#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020046#include <haproxy/payload.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020047#include <haproxy/proxy-t.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020048#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020049#include <haproxy/server-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020050#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020051#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020052#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020053#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020054#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020055#include <haproxy/tcp_rules.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020056#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020057#include <haproxy/vars.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010058
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010059/* Lua uses longjmp to perform yield or throwing errors. This
60 * macro is used only for identifying the function that can
61 * not return because a longjmp is executed.
62 * __LJMP marks a prototype of hlua file that can use longjmp.
63 * WILL_LJMP() marks an lua function that will use longjmp.
64 * MAY_LJMP() marks an lua function that may use longjmp.
65 */
66#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020067#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010068#define MAY_LJMP(func) func
69
Thierry FOURNIERbabae282015-09-17 11:36:37 +020070/* This couple of function executes securely some Lua calls outside of
71 * the lua runtime environment. Each Lua call can return a longjmp
72 * if it encounter a memory error.
73 *
74 * Lua documentation extract:
75 *
76 * If an error happens outside any protected environment, Lua calls
77 * a panic function (see lua_atpanic) and then calls abort, thus
78 * exiting the host application. Your panic function can avoid this
79 * exit by never returning (e.g., doing a long jump to your own
80 * recovery point outside Lua).
81 *
82 * The panic function runs as if it were a message handler (see
83 * §2.3); in particular, the error message is at the top of the
84 * stack. However, there is no guarantee about stack space. To push
85 * anything on the stack, the panic function must first check the
86 * available space (see §4.2).
87 *
88 * We must check all the Lua entry point. This includes:
89 * - The include/proto/hlua.h exported functions
90 * - the task wrapper function
91 * - The action wrapper function
92 * - The converters wrapper function
93 * - The sample-fetch wrapper functions
94 *
95 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -080096 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +020097 *
98 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
99 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
100 * because they must be exists in the program stack when the longjmp
101 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200102 *
103 * Note that the Lua processing is not really thread safe. It provides
104 * heavy system which consists to add our own lock function in the Lua
105 * code and recompile the library. This system will probably not accepted
106 * by maintainers of various distribs.
107 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500108 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200109 * quick looking on the Lua sources displays a lua_lock() a the start
110 * of function and a lua_unlock() at the end of the function. So I
111 * conclude that the Lua thread safe mode just perform a mutex around
112 * all execution. So I prefer to do this in the HAProxy code, it will be
113 * easier for distro maintainers.
114 *
115 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
116 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
117 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200118 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100119__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200120THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200121static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100122static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123
124#define SET_SAFE_LJMP(__L) \
125 ({ \
126 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100127 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128 if (setjmp(safe_ljmp_env) != 0) { \
129 lua_atpanic(__L, hlua_panic_safe); \
130 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100131 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200132 } else { \
133 lua_atpanic(__L, hlua_panic_ljmp); \
134 ret = 1; \
135 } \
136 ret; \
137 })
138
139/* If we are the last function catching Lua errors, we
140 * must reset the panic function.
141 */
142#define RESET_SAFE_LJMP(__L) \
143 do { \
144 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100145 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200146 } while(0)
147
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200148/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200149#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100150/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200151#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200152/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100153#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100154#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200155
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100156/* The main Lua execution context. */
157struct hlua gL;
158
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100159/* This is the memory pool containing struct lua for applets
160 * (including cli).
161 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100162DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100163
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100164/* Used for Socket connection. */
165static struct proxy socket_proxy;
166static struct server socket_tcp;
167#ifdef USE_OPENSSL
168static struct server socket_ssl;
169#endif
170
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100171/* List head of the function called at the initialisation time. */
172struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
173
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100174/* The following variables contains the reference of the different
175 * Lua classes. These references are useful for identify metadata
176 * associated with an object.
177 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100178static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100179static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100180static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100181static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100182static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100183static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200184static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200185static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200186static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100187static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100188
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100189/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200190 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100191 * short timeout. Lua linked with tasks doesn't have a timeout
192 * because a task may remain alive during all the haproxy execution.
193 */
194static unsigned int hlua_timeout_session = 4000; /* session timeout. */
195static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200196static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100197
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100198/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
199 * it is used for preventing infinite loops.
200 *
201 * I test the scheer with an infinite loop containing one incrementation
202 * and one test. I run this loop between 10 seconds, I raise a ceil of
203 * 710M loops from one interrupt each 9000 instructions, so I fix the value
204 * to one interrupt each 10 000 instructions.
205 *
206 * configured | Number of
207 * instructions | loops executed
208 * between two | in milions
209 * forced yields |
210 * ---------------+---------------
211 * 10 | 160
212 * 500 | 670
213 * 1000 | 680
214 * 5000 | 700
215 * 7000 | 700
216 * 8000 | 700
217 * 9000 | 710 <- ceil
218 * 10000 | 710
219 * 100000 | 710
220 * 1000000 | 710
221 *
222 */
223static unsigned int hlua_nb_instruction = 10000;
224
Willy Tarreau32f61e22015-03-18 17:54:59 +0100225/* Descriptor for the memory allocation state. If limit is not null, it will
226 * be enforced on any memory allocation.
227 */
228struct hlua_mem_allocator {
229 size_t allocated;
230 size_t limit;
231};
232
233static struct hlua_mem_allocator hlua_global_allocator;
234
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100235/* These functions converts types between HAProxy internal args or
236 * sample and LUA types. Another function permits to check if the
237 * LUA stack contains arguments according with an required ARG_T
238 * format.
239 */
240static int hlua_arg2lua(lua_State *L, const struct arg *arg);
241static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100242__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100243 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100244static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100245static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100246static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
247
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100248__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200249
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200250#define SEND_ERR(__be, __fmt, __args...) \
251 do { \
252 send_log(__be, LOG_ERR, __fmt, ## __args); \
253 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100254 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200255 } while (0)
256
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100257/* Used to check an Lua function type in the stack. It creates and
258 * returns a reference of the function. This function throws an
259 * error if the rgument is not a "function".
260 */
261__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
262{
263 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100264 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100265 WILL_LJMP(luaL_argerror(L, argno, msg));
266 }
267 lua_pushvalue(L, argno);
268 return luaL_ref(L, LUA_REGISTRYINDEX);
269}
270
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200271/* Return the string that is of the top of the stack. */
272const char *hlua_get_top_error_string(lua_State *L)
273{
274 if (lua_gettop(L) < 1)
275 return "unknown error";
276 if (lua_type(L, -1) != LUA_TSTRING)
277 return "unknown error";
278 return lua_tostring(L, -1);
279}
280
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200281__LJMP static const char *hlua_traceback(lua_State *L)
282{
283 lua_Debug ar;
284 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200285 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200286 int filled = 0;
287
288 while (lua_getstack(L, level++, &ar)) {
289
290 /* Add separator */
291 if (filled)
292 chunk_appendf(msg, ", ");
293 filled = 1;
294
295 /* Fill fields:
296 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
297 * 'l': fills in the field currentline;
298 * 'n': fills in the field name and namewhat;
299 * 't': fills in the field istailcall;
300 */
301 lua_getinfo(L, "Slnt", &ar);
302
303 /* Append code localisation */
304 if (ar.currentline > 0)
305 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
306 else
307 chunk_appendf(msg, "%s ", ar.short_src);
308
309 /*
310 * Get function name
311 *
312 * if namewhat is no empty, name is defined.
313 * what contains "Lua" for Lua function, "C" for C function,
314 * or "main" for main code.
315 */
316 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
317 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
318
319 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
320 chunk_appendf(msg, "main chunk");
321
322 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
323 chunk_appendf(msg, "C function line %d", ar.linedefined);
324
325 else /* nothing left... */
326 chunk_appendf(msg, "?");
327
328
329 /* Display tailed call */
330 if (ar.istailcall)
331 chunk_appendf(msg, " ...");
332 }
333
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200334 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200335}
336
337
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100338/* This function check the number of arguments available in the
339 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500340 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100341 */
342__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
343{
344 if (lua_gettop(L) == nb)
345 return;
346 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
347}
348
Mark Lakes22154b42018-01-29 14:38:40 -0800349/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100350 * and the line number where the error is encountered.
351 */
352static int hlua_pusherror(lua_State *L, const char *fmt, ...)
353{
354 va_list argp;
355 va_start(argp, fmt);
356 luaL_where(L, 1);
357 lua_pushvfstring(L, fmt, argp);
358 va_end(argp);
359 lua_concat(L, 2);
360 return 1;
361}
362
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100363/* This functions is used with sample fetch and converters. It
364 * converts the HAProxy configuration argument in a lua stack
365 * values.
366 *
367 * It takes an array of "arg", and each entry of the array is
368 * converted and pushed in the LUA stack.
369 */
370static int hlua_arg2lua(lua_State *L, const struct arg *arg)
371{
372 switch (arg->type) {
373 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100374 case ARGT_TIME:
375 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100376 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100377 break;
378
379 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200380 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100381 break;
382
383 case ARGT_IPV4:
384 case ARGT_IPV6:
385 case ARGT_MSK4:
386 case ARGT_MSK6:
387 case ARGT_FE:
388 case ARGT_BE:
389 case ARGT_TAB:
390 case ARGT_SRV:
391 case ARGT_USR:
392 case ARGT_MAP:
393 default:
394 lua_pushnil(L);
395 break;
396 }
397 return 1;
398}
399
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500400/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100401 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500402 * with sample fetch wrappers. The input arguments are given to the
403 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100404 */
405static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
406{
407 switch (lua_type(L, ud)) {
408
409 case LUA_TNUMBER:
410 case LUA_TBOOLEAN:
411 arg->type = ARGT_SINT;
412 arg->data.sint = lua_tointeger(L, ud);
413 break;
414
415 case LUA_TSTRING:
416 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200417 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200418 /* We don't know the actual size of the underlying allocation, so be conservative. */
419 arg->data.str.size = arg->data.str.data;
420 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100421 break;
422
423 case LUA_TUSERDATA:
424 case LUA_TNIL:
425 case LUA_TTABLE:
426 case LUA_TFUNCTION:
427 case LUA_TTHREAD:
428 case LUA_TLIGHTUSERDATA:
429 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200430 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100431 break;
432 }
433 return 1;
434}
435
436/* the following functions are used to convert a struct sample
437 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500438 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100439 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100440static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100441{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200442 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100443 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100444 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200445 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 break;
447
448 case SMP_T_BIN:
449 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200450 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100451 break;
452
453 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200454 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100455 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
456 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
457 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
458 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
459 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
460 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
461 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
462 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
463 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200464 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100465 break;
466 default:
467 lua_pushnil(L);
468 break;
469 }
470 break;
471
472 case SMP_T_IPV4:
473 case SMP_T_IPV6:
474 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200475 if (sample_casts[smp->data.type][SMP_T_STR] &&
476 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200477 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100478 else
479 lua_pushnil(L);
480 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100481 default:
482 lua_pushnil(L);
483 break;
484 }
485 return 1;
486}
487
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100488/* the following functions are used to convert a struct sample
489 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500490 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100491 */
492static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
493{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200494 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100495
496 case SMP_T_BIN:
497 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200498 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100499 break;
500
501 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200502 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100503 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
504 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
505 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
506 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
507 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
508 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
509 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
510 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
511 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200512 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100513 break;
514 default:
515 lua_pushstring(L, "");
516 break;
517 }
518 break;
519
520 case SMP_T_SINT:
521 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100522 case SMP_T_IPV4:
523 case SMP_T_IPV6:
524 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200525 if (sample_casts[smp->data.type][SMP_T_STR] &&
526 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200527 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100528 else
529 lua_pushstring(L, "");
530 break;
531 default:
532 lua_pushstring(L, "");
533 break;
534 }
535 return 1;
536}
537
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100538/* the following functions are used to convert an Lua type in a
539 * struct sample. This is useful to provide data from a converter
540 * to the LUA code.
541 */
542static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
543{
544 switch (lua_type(L, ud)) {
545
546 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200547 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200548 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100549 break;
550
551
552 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200553 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200554 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100555 break;
556
557 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200558 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100559 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200560 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200561 /* We don't know the actual size of the underlying allocation, so be conservative. */
562 smp->data.u.str.size = smp->data.u.str.data;
563 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100564 break;
565
566 case LUA_TUSERDATA:
567 case LUA_TNIL:
568 case LUA_TTABLE:
569 case LUA_TFUNCTION:
570 case LUA_TTHREAD:
571 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200572 case LUA_TNONE:
573 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200574 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200575 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100576 break;
577 }
578 return 1;
579}
580
Ilya Shipitsind4259502020-04-08 01:07:56 +0500581/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800582 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100583 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100584 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500585 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100586 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100587 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100588__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100589 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100590{
591 int min_arg;
592 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100593 struct proxy *px;
594 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100595
596 idx = 0;
597 min_arg = ARGM(mask);
598 mask >>= ARGM_BITS;
599
600 while (1) {
601
602 /* Check oversize. */
603 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100604 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100605 }
606
607 /* Check for mandatory arguments. */
608 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100609 if (idx < min_arg) {
610
611 /* If miss other argument than the first one, we return an error. */
612 if (idx > 0)
613 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
614
615 /* If first argument have a certain type, some default values
616 * may be used. See the function smp_resolve_args().
617 */
618 switch (mask & ARGT_MASK) {
619
620 case ARGT_FE:
621 if (!(p->cap & PR_CAP_FE))
622 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
623 argp[idx].data.prx = p;
624 argp[idx].type = ARGT_FE;
625 argp[idx+1].type = ARGT_STOP;
626 break;
627
628 case ARGT_BE:
629 if (!(p->cap & PR_CAP_BE))
630 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
631 argp[idx].data.prx = p;
632 argp[idx].type = ARGT_BE;
633 argp[idx+1].type = ARGT_STOP;
634 break;
635
636 case ARGT_TAB:
637 argp[idx].data.prx = p;
638 argp[idx].type = ARGT_TAB;
639 argp[idx+1].type = ARGT_STOP;
640 break;
641
642 default:
643 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
644 break;
645 }
646 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100647 return 0;
648 }
649
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500650 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100651 if ((mask & ARGT_MASK) == ARGT_STOP &&
652 argp[idx].type != ARGT_STOP) {
653 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
654 }
655
656 if ((mask & ARGT_MASK) == ARGT_STOP &&
657 argp[idx].type == ARGT_STOP) {
658 return 0;
659 }
660
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100661 /* Convert some argument types. */
662 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100663 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100664 if (argp[idx].type != ARGT_SINT)
665 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
666 argp[idx].type = ARGT_SINT;
667 break;
668
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100669 case ARGT_TIME:
670 if (argp[idx].type != ARGT_SINT)
671 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200672 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100673 break;
674
675 case ARGT_SIZE:
676 if (argp[idx].type != ARGT_SINT)
677 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200678 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100679 break;
680
681 case ARGT_FE:
682 if (argp[idx].type != ARGT_STR)
683 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200684 memcpy(trash.area, argp[idx].data.str.area,
685 argp[idx].data.str.data);
686 trash.area[argp[idx].data.str.data] = 0;
687 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100688 if (!argp[idx].data.prx)
689 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
690 argp[idx].type = ARGT_FE;
691 break;
692
693 case ARGT_BE:
694 if (argp[idx].type != ARGT_STR)
695 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200696 memcpy(trash.area, argp[idx].data.str.area,
697 argp[idx].data.str.data);
698 trash.area[argp[idx].data.str.data] = 0;
699 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100700 if (!argp[idx].data.prx)
701 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
702 argp[idx].type = ARGT_BE;
703 break;
704
705 case ARGT_TAB:
706 if (argp[idx].type != ARGT_STR)
707 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200708 memcpy(trash.area, argp[idx].data.str.area,
709 argp[idx].data.str.data);
710 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhus9fe7c632019-09-29 23:03:09 +0200711 argp[idx].data.t = stktable_find_by_name(trash.area);
712 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100713 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
714 argp[idx].type = ARGT_TAB;
715 break;
716
717 case ARGT_SRV:
718 if (argp[idx].type != ARGT_STR)
719 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200720 memcpy(trash.area, argp[idx].data.str.area,
721 argp[idx].data.str.data);
722 trash.area[argp[idx].data.str.data] = 0;
723 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100724 if (sname) {
725 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200726 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200727 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100728 if (!px)
729 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
730 }
731 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200732 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100733 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100734 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100735 argp[idx].data.srv = findserver(px, sname);
736 if (!argp[idx].data.srv)
737 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
738 argp[idx].type = ARGT_SRV;
739 break;
740
741 case ARGT_IPV4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200742 memcpy(trash.area, argp[idx].data.str.area,
743 argp[idx].data.str.data);
744 trash.area[argp[idx].data.str.data] = 0;
745 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100746 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
747 argp[idx].type = ARGT_IPV4;
748 break;
749
750 case ARGT_MSK4:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200751 memcpy(trash.area, argp[idx].data.str.area,
752 argp[idx].data.str.data);
753 trash.area[argp[idx].data.str.data] = 0;
754 if (!str2mask(trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100755 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
756 argp[idx].type = ARGT_MSK4;
757 break;
758
759 case ARGT_IPV6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200760 memcpy(trash.area, argp[idx].data.str.area,
761 argp[idx].data.str.data);
762 trash.area[argp[idx].data.str.data] = 0;
763 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100764 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
765 argp[idx].type = ARGT_IPV6;
766 break;
767
768 case ARGT_MSK6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200769 memcpy(trash.area, argp[idx].data.str.area,
770 argp[idx].data.str.data);
771 trash.area[argp[idx].data.str.data] = 0;
772 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100773 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
774 argp[idx].type = ARGT_MSK6;
775 break;
776
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100777 case ARGT_MAP:
778 case ARGT_REG:
779 case ARGT_USR:
780 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100781 break;
782 }
783
784 /* Check for type of argument. */
785 if ((mask & ARGT_MASK) != argp[idx].type) {
786 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
787 arg_type_names[(mask & ARGT_MASK)],
788 arg_type_names[argp[idx].type & ARGT_MASK]);
789 WILL_LJMP(luaL_argerror(L, first + idx, msg));
790 }
791
792 /* Next argument. */
793 mask >>= ARGT_BITS;
794 idx++;
795 }
796}
797
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100798/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500799 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100800 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100801 *
802 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100803 * - hlua_sethlua : create the association between hlua context and lua_state.
804 */
805static inline struct hlua *hlua_gethlua(lua_State *L)
806{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100807 struct hlua **hlua = lua_getextraspace(L);
808 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100809}
810static inline void hlua_sethlua(struct hlua *hlua)
811{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100812 struct hlua **hlua_store = lua_getextraspace(hlua->T);
813 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100814}
815
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100816/* This function is used to send logs. It try to send on screen (stderr)
817 * and on the default syslog server.
818 */
819static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
820{
821 struct tm tm;
822 char *p;
823
824 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200825 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100826 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200827 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200828 /* Break the message if exceed the buffer size. */
829 *(p-4) = ' ';
830 *(p-3) = '.';
831 *(p-2) = '.';
832 *(p-1) = '.';
833 break;
834 }
Willy Tarreau90807112020-02-25 08:16:33 +0100835 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100836 *p = *msg;
837 else
838 *p = '.';
839 }
840 *p = '\0';
841
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200842 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100843 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200844 get_localtime(date.tv_sec, &tm);
845 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100846 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200847 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100848 fflush(stderr);
849 }
850}
851
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100852/* This function just ensure that the yield will be always
853 * returned with a timeout and permit to set some flags
854 */
855__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100856 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100857{
858 struct hlua *hlua = hlua_gethlua(L);
859
860 /* Set the wake timeout. If timeout is required, we set
861 * the expiration time.
862 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200863 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100864
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100865 hlua->flags |= flags;
866
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100867 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200868 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100869}
870
Willy Tarreau87b09662015-04-03 00:22:06 +0200871/* This function initialises the Lua environment stored in the stream.
872 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100873 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200874 *
875 * This function is particular. it initialises a new Lua thread. If the
876 * initialisation fails (example: out of memory error), the lua function
877 * throws an error (longjmp).
878 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100879 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500880 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100881 * threads appear, the safe environment set a lock to ensure only one
882 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500883 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100884 *
885 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500886 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100887 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800888 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200889 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800890 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500891 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100892 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100893int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100894{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100895 if (!already_safe) {
896 if (!SET_SAFE_LJMP(gL.T)) {
897 lua->Tref = LUA_REFNIL;
898 return 0;
899 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200900 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100901 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100902 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +0100903 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +0100904 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100905 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100906 lua->T = lua_newthread(gL.T);
907 if (!lua->T) {
908 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100909 if (!already_safe)
910 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100911 return 0;
912 }
913 hlua_sethlua(lua);
914 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
915 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100916 if (!already_safe)
917 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100918 return 1;
919}
920
Willy Tarreau87b09662015-04-03 00:22:06 +0200921/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100922 * is destroyed. The destroy also the memory context. The struct "lua"
923 * is not freed.
924 */
925void hlua_ctx_destroy(struct hlua *lua)
926{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100927 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100928 return;
929
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100930 if (!lua->T)
931 goto end;
932
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100933 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200934 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100935
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200936 if (!SET_SAFE_LJMP(lua->T))
937 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100938 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200939 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200940
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200941 if (!SET_SAFE_LJMP(gL.T))
942 return;
943 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
944 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200945 /* Forces a garbage collecting process. If the Lua program is finished
946 * without error, we run the GC on the thread pointer. Its freed all
947 * the unused memory.
948 * If the thread is finnish with an error or is currently yielded,
949 * it seems that the GC applied on the thread doesn't clean anything,
950 * so e run the GC on the main thread.
951 * NOTE: maybe this action locks all the Lua threads untiml the en of
952 * the garbage collection.
953 */
Willy Tarreauf31af932020-01-14 09:59:38 +0100954 if (lua->gc_count) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200955 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200956 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200957 lua_gc(gL.T, LUA_GCCOLLECT, 0);
958 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200959 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200960
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200961 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100962
963end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100964 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100965}
966
967/* This function is used to restore the Lua context when a coroutine
968 * fails. This function copy the common memory between old coroutine
969 * and the new coroutine. The old coroutine is destroyed, and its
970 * replaced by the new coroutine.
971 * If the flag "keep_msg" is set, the last entry of the old is assumed
972 * as string error message and it is copied in the new stack.
973 */
974static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
975{
976 lua_State *T;
977 int new_ref;
978
979 /* Renew the main LUA stack doesn't have sense. */
980 if (lua == &gL)
981 return 0;
982
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100983 /* New Lua coroutine. */
984 T = lua_newthread(gL.T);
985 if (!T)
986 return 0;
987
988 /* Copy last error message. */
989 if (keep_msg)
990 lua_xmove(lua->T, T, 1);
991
992 /* Copy data between the coroutines. */
993 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
994 lua_xmove(lua->T, T, 1);
995 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
996
997 /* Destroy old data. */
998 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
999
1000 /* The thread is garbage collected by Lua. */
1001 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1002
1003 /* Fill the struct with the new coroutine values. */
1004 lua->Mref = new_ref;
1005 lua->T = T;
1006 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1007
1008 /* Set context. */
1009 hlua_sethlua(lua);
1010
1011 return 1;
1012}
1013
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001014void hlua_hook(lua_State *L, lua_Debug *ar)
1015{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001016 struct hlua *hlua = hlua_gethlua(L);
1017
1018 /* Lua cannot yield when its returning from a function,
1019 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001020 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001021 */
1022 if (lua_gethookmask(L) & LUA_MASKRET) {
1023 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1024 return;
1025 }
1026
1027 /* restore the interrupt condition. */
1028 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1029
1030 /* If we interrupt the Lua processing in yieldable state, we yield.
1031 * If the state is not yieldable, trying yield causes an error.
1032 */
1033 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001034 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001035
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001036 /* If we cannot yield, update the clock and check the timeout. */
1037 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001038 hlua->run_time += now_ms - hlua->start_time;
1039 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001040 lua_pushfstring(L, "execution timeout");
1041 WILL_LJMP(lua_error(L));
1042 }
1043
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001044 /* Update the start time. */
1045 hlua->start_time = now_ms;
1046
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001047 /* Try to interrupt the process at the end of the current
1048 * unyieldable function.
1049 */
1050 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001051}
1052
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001053/* This function start or resumes the Lua stack execution. If the flag
1054 * "yield_allowed" if no set and the LUA stack execution returns a yield
1055 * The function return an error.
1056 *
1057 * The function can returns 4 values:
1058 * - HLUA_E_OK : The execution is terminated without any errors.
1059 * - HLUA_E_AGAIN : The execution must continue at the next associated
1060 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001061 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001062 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001063 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001064 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001065 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001066 * LUA code.
1067 */
1068static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1069{
1070 int ret;
1071 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001072 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001073
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001074 /* Initialise run time counter. */
1075 if (!HLUA_IS_RUNNING(lua))
1076 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001077
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001078 /* Lock the whole Lua execution. This lock must be before the
1079 * label "resume_execution".
1080 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001081 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001082
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001083resume_execution:
1084
1085 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1086 * instructions. it is used for preventing infinite loops.
1087 */
1088 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1089
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001090 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001091 HLUA_SET_RUN(lua);
1092 HLUA_CLR_CTRLYIELD(lua);
1093 HLUA_CLR_WAKERESWR(lua);
1094 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001095
Christopher Fauletbc275a92020-02-26 14:55:16 +01001096 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001097 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001098 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001099
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001100 /* Call the function. */
1101 ret = lua_resume(lua->T, gL.T, lua->nargs);
1102 switch (ret) {
1103
1104 case LUA_OK:
1105 ret = HLUA_E_OK;
1106 break;
1107
1108 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001109 /* Check if the execution timeout is expired. It it is the case, we
1110 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001111 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001112 tv_update_date(0, 1);
1113 lua->run_time += now_ms - lua->start_time;
1114 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001115 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001116 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001117 break;
1118 }
1119 /* Process the forced yield. if the general yield is not allowed or
1120 * if no task were associated this the current Lua execution
1121 * coroutine, we resume the execution. Else we want to return in the
1122 * scheduler and we want to be waked up again, to continue the
1123 * current Lua execution. So we schedule our own task.
1124 */
1125 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001126 if (!yield_allowed || !lua->task)
1127 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001128 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001129 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001130 if (!yield_allowed) {
1131 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001132 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001133 break;
1134 }
1135 ret = HLUA_E_AGAIN;
1136 break;
1137
1138 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001139
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001140 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001141 * because the errors ares the only one mean to return immediately
1142 * from and lua execution.
1143 */
1144 if (lua->flags & HLUA_EXIT) {
1145 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001146 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001147 break;
1148 }
1149
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001150 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001151 if (!lua_checkstack(lua->T, 1)) {
1152 ret = HLUA_E_ERR;
1153 break;
1154 }
1155 msg = lua_tostring(lua->T, -1);
1156 lua_settop(lua->T, 0); /* Empty the stack. */
1157 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001158 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001159 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001160 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001161 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001162 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001163 ret = HLUA_E_ERRMSG;
1164 break;
1165
1166 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001167 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001168 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001169 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001170 break;
1171
1172 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001173 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001174 if (!lua_checkstack(lua->T, 1)) {
1175 ret = HLUA_E_ERR;
1176 break;
1177 }
1178 msg = lua_tostring(lua->T, -1);
1179 lua_settop(lua->T, 0); /* Empty the stack. */
1180 lua_pop(lua->T, 1);
1181 if (msg)
1182 lua_pushfstring(lua->T, "message handler error: %s", msg);
1183 else
1184 lua_pushfstring(lua->T, "message handler error");
1185 ret = HLUA_E_ERRMSG;
1186 break;
1187
1188 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001189 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001190 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001191 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001192 break;
1193 }
1194
1195 switch (ret) {
1196 case HLUA_E_AGAIN:
1197 break;
1198
1199 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001200 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001201 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001202 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001203 break;
1204
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001205 case HLUA_E_ETMOUT:
1206 case HLUA_E_NOMEM:
1207 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001208 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001209 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001210 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001211 hlua_ctx_renew(lua, 0);
1212 break;
1213
1214 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001215 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001216 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001217 break;
1218 }
1219
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001220 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001221 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001222
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001223 return ret;
1224}
1225
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001226/* This function exit the current code. */
1227__LJMP static int hlua_done(lua_State *L)
1228{
1229 struct hlua *hlua = hlua_gethlua(L);
1230
1231 hlua->flags |= HLUA_EXIT;
1232 WILL_LJMP(lua_error(L));
1233
1234 return 0;
1235}
1236
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001237/* This function is an LUA binding. It provides a function
1238 * for deleting ACL from a referenced ACL file.
1239 */
1240__LJMP static int hlua_del_acl(lua_State *L)
1241{
1242 const char *name;
1243 const char *key;
1244 struct pat_ref *ref;
1245
1246 MAY_LJMP(check_args(L, 2, "del_acl"));
1247
1248 name = MAY_LJMP(luaL_checkstring(L, 1));
1249 key = MAY_LJMP(luaL_checkstring(L, 2));
1250
1251 ref = pat_ref_lookup(name);
1252 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001253 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001254
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001255 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001256 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001257 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001258 return 0;
1259}
1260
1261/* This function is an LUA binding. It provides a function
1262 * for deleting map entry from a referenced map file.
1263 */
1264static int hlua_del_map(lua_State *L)
1265{
1266 const char *name;
1267 const char *key;
1268 struct pat_ref *ref;
1269
1270 MAY_LJMP(check_args(L, 2, "del_map"));
1271
1272 name = MAY_LJMP(luaL_checkstring(L, 1));
1273 key = MAY_LJMP(luaL_checkstring(L, 2));
1274
1275 ref = pat_ref_lookup(name);
1276 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001277 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001278
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001279 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001280 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001281 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001282 return 0;
1283}
1284
1285/* This function is an LUA binding. It provides a function
1286 * for adding ACL pattern from a referenced ACL file.
1287 */
1288static int hlua_add_acl(lua_State *L)
1289{
1290 const char *name;
1291 const char *key;
1292 struct pat_ref *ref;
1293
1294 MAY_LJMP(check_args(L, 2, "add_acl"));
1295
1296 name = MAY_LJMP(luaL_checkstring(L, 1));
1297 key = MAY_LJMP(luaL_checkstring(L, 2));
1298
1299 ref = pat_ref_lookup(name);
1300 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001301 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001302
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001303 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001304 if (pat_ref_find_elt(ref, key) == NULL)
1305 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001306 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001307 return 0;
1308}
1309
1310/* This function is an LUA binding. It provides a function
1311 * for setting map pattern and sample from a referenced map
1312 * file.
1313 */
1314static int hlua_set_map(lua_State *L)
1315{
1316 const char *name;
1317 const char *key;
1318 const char *value;
1319 struct pat_ref *ref;
1320
1321 MAY_LJMP(check_args(L, 3, "set_map"));
1322
1323 name = MAY_LJMP(luaL_checkstring(L, 1));
1324 key = MAY_LJMP(luaL_checkstring(L, 2));
1325 value = MAY_LJMP(luaL_checkstring(L, 3));
1326
1327 ref = pat_ref_lookup(name);
1328 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001329 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001330
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001331 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001332 if (pat_ref_find_elt(ref, key) != NULL)
1333 pat_ref_set(ref, key, value, NULL);
1334 else
1335 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001336 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001337 return 0;
1338}
1339
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001340/* A class is a lot of memory that contain data. This data can be a table,
1341 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001342 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001343 * the name of the object (_G[<name>] = <metable> ).
1344 *
1345 * A metable is a table that modify the standard behavior of a standard
1346 * access to the associated data. The entries of this new metatable are
1347 * defined as is:
1348 *
1349 * http://lua-users.org/wiki/MetatableEvents
1350 *
1351 * __index
1352 *
1353 * we access an absent field in a table, the result is nil. This is
1354 * true, but it is not the whole truth. Actually, such access triggers
1355 * the interpreter to look for an __index metamethod: If there is no
1356 * such method, as usually happens, then the access results in nil;
1357 * otherwise, the metamethod will provide the result.
1358 *
1359 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1360 * the key does not appear in the table, but the metatable has an __index
1361 * property:
1362 *
1363 * - if the value is a function, the function is called, passing in the
1364 * table and the key; the return value of that function is returned as
1365 * the result.
1366 *
1367 * - if the value is another table, the value of the key in that table is
1368 * asked for and returned (and if it doesn't exist in that table, but that
1369 * table's metatable has an __index property, then it continues on up)
1370 *
1371 * - Use "rawget(myTable,key)" to skip this metamethod.
1372 *
1373 * http://www.lua.org/pil/13.4.1.html
1374 *
1375 * __newindex
1376 *
1377 * Like __index, but control property assignment.
1378 *
1379 * __mode - Control weak references. A string value with one or both
1380 * of the characters 'k' and 'v' which specifies that the the
1381 * keys and/or values in the table are weak references.
1382 *
1383 * __call - Treat a table like a function. When a table is followed by
1384 * parenthesis such as "myTable( 'foo' )" and the metatable has
1385 * a __call key pointing to a function, that function is invoked
1386 * (passing any specified arguments) and the return value is
1387 * returned.
1388 *
1389 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1390 * called, if the metatable for myTable has a __metatable
1391 * key, the value of that key is returned instead of the
1392 * actual metatable.
1393 *
1394 * __tostring - Control string representation. When the builtin
1395 * "tostring( myTable )" function is called, if the metatable
1396 * for myTable has a __tostring property set to a function,
1397 * that function is invoked (passing myTable to it) and the
1398 * return value is used as the string representation.
1399 *
1400 * __len - Control table length. When the table length is requested using
1401 * the length operator ( '#' ), if the metatable for myTable has
1402 * a __len key pointing to a function, that function is invoked
1403 * (passing myTable to it) and the return value used as the value
1404 * of "#myTable".
1405 *
1406 * __gc - Userdata finalizer code. When userdata is set to be garbage
1407 * collected, if the metatable has a __gc field pointing to a
1408 * function, that function is first invoked, passing the userdata
1409 * to it. The __gc metamethod is not called for tables.
1410 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1411 *
1412 * Special metamethods for redefining standard operators:
1413 * http://www.lua.org/pil/13.1.html
1414 *
1415 * __add "+"
1416 * __sub "-"
1417 * __mul "*"
1418 * __div "/"
1419 * __unm "!"
1420 * __pow "^"
1421 * __concat ".."
1422 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001423 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001424 * http://www.lua.org/pil/13.2.html
1425 *
1426 * __eq "=="
1427 * __lt "<"
1428 * __le "<="
1429 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001430
1431/*
1432 *
1433 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001434 * Class Map
1435 *
1436 *
1437 */
1438
1439/* Returns a struct hlua_map if the stack entry "ud" is
1440 * a class session, otherwise it throws an error.
1441 */
1442__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1443{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001444 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001445}
1446
1447/* This function is the map constructor. It don't need
1448 * the class Map object. It creates and return a new Map
1449 * object. It must be called only during "body" or "init"
1450 * context because it process some filesystem accesses.
1451 */
1452__LJMP static int hlua_map_new(struct lua_State *L)
1453{
1454 const char *fn;
1455 int match = PAT_MATCH_STR;
1456 struct sample_conv conv;
1457 const char *file = "";
1458 int line = 0;
1459 lua_Debug ar;
1460 char *err = NULL;
1461 struct arg args[2];
1462
1463 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1464 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1465
1466 fn = MAY_LJMP(luaL_checkstring(L, 1));
1467
1468 if (lua_gettop(L) >= 2) {
1469 match = MAY_LJMP(luaL_checkinteger(L, 2));
1470 if (match < 0 || match >= PAT_MATCH_NUM)
1471 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1472 }
1473
1474 /* Get Lua filename and line number. */
1475 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1476 lua_getinfo(L, "Sl", &ar); /* get info about it */
1477 if (ar.currentline > 0) { /* is there info? */
1478 file = ar.short_src;
1479 line = ar.currentline;
1480 }
1481 }
1482
1483 /* fill fake sample_conv struct. */
1484 conv.kw = ""; /* unused. */
1485 conv.process = NULL; /* unused. */
1486 conv.arg_mask = 0; /* unused. */
1487 conv.val_args = NULL; /* unused. */
1488 conv.out_type = SMP_T_STR;
1489 conv.private = (void *)(long)match;
1490 switch (match) {
1491 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1492 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1493 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1494 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1495 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1496 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1497 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001498 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001499 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1500 default:
1501 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1502 }
1503
1504 /* fill fake args. */
1505 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001506 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001507 args[1].type = ARGT_STOP;
1508
1509 /* load the map. */
1510 if (!sample_load_map(args, &conv, file, line, &err)) {
1511 /* error case: we cant use luaL_error because we must
1512 * free the err variable.
1513 */
1514 luaL_where(L, 1);
1515 lua_pushfstring(L, "'new': %s.", err);
1516 lua_concat(L, 2);
1517 free(err);
1518 WILL_LJMP(lua_error(L));
1519 }
1520
1521 /* create the lua object. */
1522 lua_newtable(L);
1523 lua_pushlightuserdata(L, args[0].data.map);
1524 lua_rawseti(L, -2, 0);
1525
1526 /* Pop a class Map metatable and affect it to the userdata. */
1527 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1528 lua_setmetatable(L, -2);
1529
1530
1531 return 1;
1532}
1533
1534__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1535{
1536 struct map_descriptor *desc;
1537 struct pattern *pat;
1538 struct sample smp;
1539
1540 MAY_LJMP(check_args(L, 2, "lookup"));
1541 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001542 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001543 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001544 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001545 }
1546 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001547 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001548 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001549 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 +02001550 }
1551
1552 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001553 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001554 if (str)
1555 lua_pushstring(L, "");
1556 else
1557 lua_pushnil(L);
1558 return 1;
1559 }
1560
1561 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001562 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001563 return 1;
1564}
1565
1566__LJMP static int hlua_map_lookup(struct lua_State *L)
1567{
1568 return _hlua_map_lookup(L, 0);
1569}
1570
1571__LJMP static int hlua_map_slookup(struct lua_State *L)
1572{
1573 return _hlua_map_lookup(L, 1);
1574}
1575
1576/*
1577 *
1578 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001579 * Class Socket
1580 *
1581 *
1582 */
1583
1584__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1585{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001586 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001587}
1588
1589/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001590 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001591 * received.
1592 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001593static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001594{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001595 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001596
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001597 if (appctx->ctx.hlua_cosocket.die) {
1598 si_shutw(si);
1599 si_shutr(si);
1600 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001601 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1602 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001603 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1604 }
1605
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001606 /* If we cant write, wakeup the pending write signals. */
1607 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001608 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001609
1610 /* If we cant read, wakeup the pending read signals. */
1611 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001612 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001613
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001614 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001615 * to be notified whenever the connection completes.
1616 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001617 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001618 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001619 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001620 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001621 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001622 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001623
1624 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001625 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001626
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001627 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001628 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001629 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001630
1631 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001632 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001633 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001634
1635 /* Some data were injected in the buffer, notify the stream
1636 * interface.
1637 */
1638 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001639 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001640
1641 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001642 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001643 */
1644 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001645 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001646}
1647
Willy Tarreau87b09662015-04-03 00:22:06 +02001648/* This function is called when the "struct stream" is destroyed.
1649 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001650 * Wake all the pending signals.
1651 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001652static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001653{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001654 struct xref *peer;
1655
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001656 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001657 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1658 if (peer)
1659 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001660
1661 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001662 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1663 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001664}
1665
1666/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001667 * uses this object. If the stream does not exists, just quit.
1668 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001669 * pending signal can rest in the read and write lists. destroy
1670 * it.
1671 */
1672__LJMP static int hlua_socket_gc(lua_State *L)
1673{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001674 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001675 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001676 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001677
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001678 MAY_LJMP(check_args(L, 1, "__gc"));
1679
1680 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001681 peer = xref_get_peer_and_lock(&socket->xref);
1682 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001683 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001684 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001686 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001687 appctx->ctx.hlua_cosocket.die = 1;
1688 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001689
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001690 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001691 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001692 return 0;
1693}
1694
1695/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001696 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001697 */
sada05ed3302018-05-11 11:48:18 -07001698__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001700 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001702 struct xref *peer;
Willy Tarreauf31af932020-01-14 09:59:38 +01001703 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001704
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001705 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001706
1707 /* Check if we run on the same thread than the xreator thread.
1708 * We cannot access to the socket if the thread is different.
1709 */
1710 if (socket->tid != tid)
1711 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1712
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001713 peer = xref_get_peer_and_lock(&socket->xref);
1714 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001715 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001716
1717 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001718 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001719
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001720 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001721 appctx->ctx.hlua_cosocket.die = 1;
1722 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001723
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001724 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001725 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001726 return 0;
1727}
1728
sada05ed3302018-05-11 11:48:18 -07001729/* The close function calls close_helper.
1730 */
1731__LJMP static int hlua_socket_close(lua_State *L)
1732{
1733 MAY_LJMP(check_args(L, 1, "close"));
1734 return hlua_socket_close_helper(L);
1735}
1736
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001737/* This Lua function assumes that the stack contain three parameters.
1738 * 1 - USERDATA containing a struct socket
1739 * 2 - INTEGER with values of the macro defined below
1740 * If the integer is -1, we must read at most one line.
1741 * If the integer is -2, we ust read all the data until the
1742 * end of the stream.
1743 * If the integer is positive value, we must read a number of
1744 * bytes corresponding to this value.
1745 */
1746#define HLSR_READ_LINE (-1)
1747#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001748__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001749{
1750 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1751 int wanted = lua_tointeger(L, 2);
1752 struct hlua *hlua = hlua_gethlua(L);
1753 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001754 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001755 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001756 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001757 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001758 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001759 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001760 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001761 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001762 struct stream_interface *si;
1763 struct stream *s;
1764 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001765 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001766
1767 /* Check if this lua stack is schedulable. */
1768 if (!hlua || !hlua->task)
1769 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1770 "'frontend', 'backend' or 'task'"));
1771
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001772 /* Check if we run on the same thread than the xreator thread.
1773 * We cannot access to the socket if the thread is different.
1774 */
1775 if (socket->tid != tid)
1776 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1777
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001778 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001779 peer = xref_get_peer_and_lock(&socket->xref);
1780 if (!peer)
1781 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001782 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1783 si = appctx->owner;
1784 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001785
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001786 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001787 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001788 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001789 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001790 if (nblk < 0) /* Connection close. */
1791 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001792 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001794
1795 /* remove final \r\n. */
1796 if (nblk == 1) {
1797 if (blk1[len1-1] == '\n') {
1798 len1--;
1799 skip_at_end++;
1800 if (blk1[len1-1] == '\r') {
1801 len1--;
1802 skip_at_end++;
1803 }
1804 }
1805 }
1806 else {
1807 if (blk2[len2-1] == '\n') {
1808 len2--;
1809 skip_at_end++;
1810 if (blk2[len2-1] == '\r') {
1811 len2--;
1812 skip_at_end++;
1813 }
1814 }
1815 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001816 }
1817
1818 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001819 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001820 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001821 if (nblk < 0) /* Connection close. */
1822 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001823 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001824 goto connection_empty;
1825 }
1826
1827 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001828 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001829 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001830 if (nblk < 0) /* Connection close. */
1831 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001832 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001833 goto connection_empty;
1834
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001835 missing_bytes = wanted - socket->b.n;
1836 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001837 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001838 len1 = missing_bytes;
1839 } if (nblk == 2 && len1 + len2 > missing_bytes)
1840 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001841 }
1842
1843 len = len1;
1844
1845 luaL_addlstring(&socket->b, blk1, len1);
1846 if (nblk == 2) {
1847 len += len2;
1848 luaL_addlstring(&socket->b, blk2, len2);
1849 }
1850
1851 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001852 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001853
1854 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001855 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001856
1857 /* If the pattern reclaim to read all the data
1858 * in the connection, got out.
1859 */
1860 if (wanted == HLSR_READ_ALL)
1861 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001862 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001863 goto connection_empty;
1864
1865 /* Return result. */
1866 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001867 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001868 return 1;
1869
1870connection_closed:
1871
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001872 xref_unlock(&socket->xref, peer);
1873
1874no_peer:
1875
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001876 /* If the buffer containds data. */
1877 if (socket->b.n > 0) {
1878 luaL_pushresult(&socket->b);
1879 return 1;
1880 }
1881 lua_pushnil(L);
1882 lua_pushstring(L, "connection closed.");
1883 return 2;
1884
1885connection_empty:
1886
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001887 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1888 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001889 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001890 }
1891 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001892 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001893 return 0;
1894}
1895
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001896/* This Lua function gets two parameters. The first one can be string
1897 * or a number. If the string is "*l", the user requires one line. If
1898 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001899 * If the value is a number, the user require a number of bytes equal
1900 * to the value. The default value is "*l" (a line).
1901 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001902 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001903 * integer takes this values:
1904 * -1 : read a line
1905 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001906 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001907 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001908 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909 * concatenated with the read data.
1910 */
1911__LJMP static int hlua_socket_receive(struct lua_State *L)
1912{
1913 int wanted = HLSR_READ_LINE;
1914 const char *pattern;
1915 int type;
1916 char *error;
1917 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001918 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001919
1920 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1921 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1922
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001923 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001924
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001925 /* Check if we run on the same thread than the xreator thread.
1926 * We cannot access to the socket if the thread is different.
1927 */
1928 if (socket->tid != tid)
1929 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1930
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001931 /* check for pattern. */
1932 if (lua_gettop(L) >= 2) {
1933 type = lua_type(L, 2);
1934 if (type == LUA_TSTRING) {
1935 pattern = lua_tostring(L, 2);
1936 if (strcmp(pattern, "*a") == 0)
1937 wanted = HLSR_READ_ALL;
1938 else if (strcmp(pattern, "*l") == 0)
1939 wanted = HLSR_READ_LINE;
1940 else {
1941 wanted = strtoll(pattern, &error, 10);
1942 if (*error != '\0')
1943 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1944 }
1945 }
1946 else if (type == LUA_TNUMBER) {
1947 wanted = lua_tointeger(L, 2);
1948 if (wanted < 0)
1949 WILL_LJMP(luaL_error(L, "Unsupported size."));
1950 }
1951 }
1952
1953 /* Set pattern. */
1954 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001955
1956 /* Check if we would replace the top by itself. */
1957 if (lua_gettop(L) != 2)
1958 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001959
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001960 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001961 luaL_buffinit(L, &socket->b);
1962
1963 /* Check prefix. */
1964 if (lua_gettop(L) >= 3) {
1965 if (lua_type(L, 3) != LUA_TSTRING)
1966 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1967 pattern = lua_tolstring(L, 3, &len);
1968 luaL_addlstring(&socket->b, pattern, len);
1969 }
1970
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001971 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001972}
1973
1974/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001975 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001976 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001977static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001978{
1979 struct hlua_socket *socket;
1980 struct hlua *hlua = hlua_gethlua(L);
1981 struct appctx *appctx;
1982 size_t buf_len;
1983 const char *buf;
1984 int len;
1985 int send_len;
1986 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001987 struct xref *peer;
1988 struct stream_interface *si;
1989 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001990
1991 /* Check if this lua stack is schedulable. */
1992 if (!hlua || !hlua->task)
1993 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1994 "'frontend', 'backend' or 'task'"));
1995
1996 /* Get object */
1997 socket = MAY_LJMP(hlua_checksocket(L, 1));
1998 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001999 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002000
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002001 /* Check if we run on the same thread than the xreator thread.
2002 * We cannot access to the socket if the thread is different.
2003 */
2004 if (socket->tid != tid)
2005 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2006
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002007 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002008 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002009 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002010 lua_pushinteger(L, -1);
2011 return 1;
2012 }
2013 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2014 si = appctx->owner;
2015 s = si_strm(si);
2016
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002017 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002018 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002019 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002020 lua_pushinteger(L, -1);
2021 return 1;
2022 }
2023
2024 /* Update the input buffer data. */
2025 buf += sent;
2026 send_len = buf_len - sent;
2027
2028 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002029 if (sent >= buf_len) {
2030 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002031 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002032 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002033
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002034 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002035 * the request buffer if its not required.
2036 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002037 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002038 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002039 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002040 }
2041
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002042 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002043 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002044 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002045 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002046 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002047
2048 /* send data */
2049 if (len < send_len)
2050 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002051 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002052
2053 /* "Not enough space" (-1), "Buffer too little to contain
2054 * the data" (-2) are not expected because the available length
2055 * is tested.
2056 * Other unknown error are also not expected.
2057 */
2058 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002059 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002060 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002061
sada05ed3302018-05-11 11:48:18 -07002062 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002063 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002064 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002065 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002066 return 1;
2067 }
2068
2069 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002070 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002071
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002072 s->req.rex = TICK_ETERNITY;
2073 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002074
2075 /* Update length sent. */
2076 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002077 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002078
2079 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002080 if (sent + len >= buf_len) {
2081 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002082 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002083 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002084
2085hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002086 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2087 xref_unlock(&socket->xref, peer);
2088 WILL_LJMP(luaL_error(L, "out of memory"));
2089 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002090 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002091 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002092 return 0;
2093}
2094
2095/* This function initiate the send of data. It just check the input
2096 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002097 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002098 * "hlua_socket_write_yield" that can yield.
2099 *
2100 * The Lua function gets between 3 and 4 parameters. The first one is
2101 * the associated object. The second is a string buffer. The third is
2102 * a facultative integer that represents where is the buffer position
2103 * of the start of the data that can send. The first byte is the
2104 * position "1". The default value is "1". The fourth argument is a
2105 * facultative integer that represents where is the buffer position
2106 * of the end of the data that can send. The default is the last byte.
2107 */
2108static int hlua_socket_send(struct lua_State *L)
2109{
2110 int i;
2111 int j;
2112 const char *buf;
2113 size_t buf_len;
2114
2115 /* Check number of arguments. */
2116 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2117 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2118
2119 /* Get the string. */
2120 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2121
2122 /* Get and check j. */
2123 if (lua_gettop(L) == 4) {
2124 j = MAY_LJMP(luaL_checkinteger(L, 4));
2125 if (j < 0)
2126 j = buf_len + j + 1;
2127 if (j > buf_len)
2128 j = buf_len + 1;
2129 lua_pop(L, 1);
2130 }
2131 else
2132 j = buf_len;
2133
2134 /* Get and check i. */
2135 if (lua_gettop(L) == 3) {
2136 i = MAY_LJMP(luaL_checkinteger(L, 3));
2137 if (i < 0)
2138 i = buf_len + i + 1;
2139 if (i > buf_len)
2140 i = buf_len + 1;
2141 lua_pop(L, 1);
2142 } else
2143 i = 1;
2144
2145 /* Check bth i and j. */
2146 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002147 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002148 return 1;
2149 }
2150 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002151 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002152 return 1;
2153 }
2154 if (i == 0)
2155 i = 1;
2156 if (j == 0)
2157 j = 1;
2158
2159 /* Pop the string. */
2160 lua_pop(L, 1);
2161
2162 /* Update the buffer length. */
2163 buf += i - 1;
2164 buf_len = j - i + 1;
2165 lua_pushlstring(L, buf, buf_len);
2166
2167 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002168 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002169
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002170 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002171}
2172
Willy Tarreau22b0a682015-06-17 19:43:49 +02002173#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002174__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2175{
2176 static char buffer[SOCKET_INFO_MAX_LEN];
2177 int ret;
2178 int len;
2179 char *p;
2180
2181 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2182 if (ret <= 0) {
2183 lua_pushnil(L);
2184 return 1;
2185 }
2186
2187 if (ret == AF_UNIX) {
2188 lua_pushstring(L, buffer+1);
2189 return 1;
2190 }
2191 else if (ret == AF_INET6) {
2192 buffer[0] = '[';
2193 len = strlen(buffer);
2194 buffer[len] = ']';
2195 len++;
2196 buffer[len] = ':';
2197 len++;
2198 p = buffer;
2199 }
2200 else if (ret == AF_INET) {
2201 p = buffer + 1;
2202 len = strlen(p);
2203 p[len] = ':';
2204 len++;
2205 }
2206 else {
2207 lua_pushnil(L);
2208 return 1;
2209 }
2210
2211 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2212 lua_pushnil(L);
2213 return 1;
2214 }
2215
2216 lua_pushstring(L, p);
2217 return 1;
2218}
2219
2220/* Returns information about the peer of the connection. */
2221__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2222{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002223 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002224 struct xref *peer;
2225 struct appctx *appctx;
2226 struct stream_interface *si;
2227 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002228 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002229
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002230 MAY_LJMP(check_args(L, 1, "getpeername"));
2231
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002232 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002233
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002234 /* Check if we run on the same thread than the xreator thread.
2235 * We cannot access to the socket if the thread is different.
2236 */
2237 if (socket->tid != tid)
2238 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2239
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002240 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002241 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002242 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002243 lua_pushnil(L);
2244 return 1;
2245 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002246 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2247 si = appctx->owner;
2248 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002249
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002250 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002251 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002252 lua_pushnil(L);
2253 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002254 }
2255
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002256 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002257 xref_unlock(&socket->xref, peer);
2258 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002259}
2260
2261/* Returns information about my connection side. */
2262static int hlua_socket_getsockname(struct lua_State *L)
2263{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002264 struct hlua_socket *socket;
2265 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002266 struct appctx *appctx;
2267 struct xref *peer;
2268 struct stream_interface *si;
2269 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002270 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002271
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002272 MAY_LJMP(check_args(L, 1, "getsockname"));
2273
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002274 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002275
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002276 /* Check if we run on the same thread than the xreator thread.
2277 * We cannot access to the socket if the thread is different.
2278 */
2279 if (socket->tid != tid)
2280 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2281
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002282 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002283 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002284 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002285 lua_pushnil(L);
2286 return 1;
2287 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002288 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2289 si = appctx->owner;
2290 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002291
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002292 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002293 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002294 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002295 lua_pushnil(L);
2296 return 1;
2297 }
2298
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002299 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002300 xref_unlock(&socket->xref, peer);
2301 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002302}
2303
2304/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002305static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002306 .obj_type = OBJ_TYPE_APPLET,
2307 .name = "<LUA_TCP>",
2308 .fct = hlua_socket_handler,
2309 .release = hlua_socket_release,
2310};
2311
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002312__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002313{
2314 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2315 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002316 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002317 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002318 struct stream_interface *si;
2319 struct stream *s;
2320
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002321 /* Check if we run on the same thread than the xreator thread.
2322 * We cannot access to the socket if the thread is different.
2323 */
2324 if (socket->tid != tid)
2325 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2326
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002327 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002328 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002329 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002330 lua_pushnil(L);
2331 lua_pushstring(L, "Can't connect");
2332 return 2;
2333 }
2334 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2335 si = appctx->owner;
2336 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002337
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002338 /* Check if we run on the same thread than the xreator thread.
2339 * We cannot access to the socket if the thread is different.
2340 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002341 if (socket->tid != tid) {
2342 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002343 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002344 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002345
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002346 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002347 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002348 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002349 lua_pushnil(L);
2350 lua_pushstring(L, "Can't connect");
2351 return 2;
2352 }
2353
Willy Tarreaue09101e2018-10-16 17:37:12 +02002354 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002355
2356 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002357 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002358 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002359 lua_pushinteger(L, 1);
2360 return 1;
2361 }
2362
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002363 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2364 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002365 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002366 }
2367 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002368 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002369 return 0;
2370}
2371
2372/* This function fail or initite the connection. */
2373__LJMP static int hlua_socket_connect(struct lua_State *L)
2374{
2375 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002376 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002377 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002378 struct hlua *hlua;
2379 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002380 int low, high;
2381 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002382 struct xref *peer;
2383 struct stream_interface *si;
2384 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002385
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002386 if (lua_gettop(L) < 2)
2387 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002388
2389 /* Get args. */
2390 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002391
2392 /* Check if we run on the same thread than the xreator thread.
2393 * We cannot access to the socket if the thread is different.
2394 */
2395 if (socket->tid != tid)
2396 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2397
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002398 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002399 if (lua_gettop(L) >= 3) {
2400 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002401 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002402
Tim Duesterhus6edab862018-01-06 19:04:45 +01002403 /* Force the ip to end with a colon, to support IPv6 addresses
2404 * that are not enclosed within square brackets.
2405 */
2406 if (port > 0) {
2407 luaL_buffinit(L, &b);
2408 luaL_addstring(&b, ip);
2409 luaL_addchar(&b, ':');
2410 luaL_pushresult(&b);
2411 ip = lua_tolstring(L, lua_gettop(L), NULL);
2412 }
2413 }
2414
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002415 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002416 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002417 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002418 lua_pushnil(L);
2419 return 1;
2420 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002421
2422 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002423 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002424 if (!addr) {
2425 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002426 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002427 }
2428 if (low != high) {
2429 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002430 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002431 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002432
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002433 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002434 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002435 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002436 if (port == -1) {
2437 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002438 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002439 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002440 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2441 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002442 if (port == -1) {
2443 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002444 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002445 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002446 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002447 }
2448 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002449
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002450 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2451 si = appctx->owner;
2452 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002453
2454 if (!sockaddr_alloc(&s->target_addr)) {
2455 xref_unlock(&socket->xref, peer);
2456 WILL_LJMP(luaL_error(L, "connect: internal error"));
2457 }
2458 *s->target_addr = *addr;
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002459 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002460
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002461 hlua = hlua_gethlua(L);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002462
2463 /* inform the stream that we want to be notified whenever the
2464 * connection completes.
2465 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002466 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002467 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002468 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002469
Willy Tarreauf31af932020-01-14 09:59:38 +01002470 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002471
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002472 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2473 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002474 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002475 }
2476 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002477
2478 task_wakeup(s->task, TASK_WOKEN_INIT);
2479 /* Return yield waiting for connection. */
2480
Willy Tarreau9635e032018-10-16 17:52:55 +02002481 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002482
2483 return 0;
2484}
2485
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002486#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002487__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2488{
2489 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002490 struct xref *peer;
2491 struct appctx *appctx;
2492 struct stream_interface *si;
2493 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002494
2495 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2496 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002497
2498 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002499 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002500 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002501 lua_pushnil(L);
2502 return 1;
2503 }
2504 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2505 si = appctx->owner;
2506 s = si_strm(si);
2507
2508 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002509 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002510 return MAY_LJMP(hlua_socket_connect(L));
2511}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002512#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002513
2514__LJMP static int hlua_socket_setoption(struct lua_State *L)
2515{
2516 return 0;
2517}
2518
2519__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2520{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002521 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002522 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002523 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002524 struct xref *peer;
2525 struct appctx *appctx;
2526 struct stream_interface *si;
2527 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002528
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002529 MAY_LJMP(check_args(L, 2, "settimeout"));
2530
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002531 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002532
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002533 /* convert the timeout to millis */
2534 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002535
Thierry Fournier17a921b2018-03-08 09:59:02 +01002536 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002537 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002538 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2539
Mark Lakes56cc1252018-03-27 09:48:06 +02002540 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002541 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002542
2543 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002544 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002545 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002546
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002547 /* Check if we run on the same thread than the xreator thread.
2548 * We cannot access to the socket if the thread is different.
2549 */
2550 if (socket->tid != tid)
2551 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2552
Mark Lakes56cc1252018-03-27 09:48:06 +02002553 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002554 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002555 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002556 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2557 WILL_LJMP(lua_error(L));
2558 return 0;
2559 }
2560 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2561 si = appctx->owner;
2562 s = si_strm(si);
2563
Cyril Bonté7bb63452018-08-17 23:51:02 +02002564 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002565 s->req.rto = tmout;
2566 s->req.wto = tmout;
2567 s->res.rto = tmout;
2568 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002569 s->req.rex = tick_add_ifset(now_ms, tmout);
2570 s->req.wex = tick_add_ifset(now_ms, tmout);
2571 s->res.rex = tick_add_ifset(now_ms, tmout);
2572 s->res.wex = tick_add_ifset(now_ms, tmout);
2573
2574 s->task->expire = tick_add_ifset(now_ms, tmout);
2575 task_queue(s->task);
2576
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002577 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002578
Thierry Fourniere9636f12018-03-08 09:54:32 +01002579 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002580 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002581}
2582
2583__LJMP static int hlua_socket_new(lua_State *L)
2584{
2585 struct hlua_socket *socket;
2586 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002587 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002588 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002589
2590 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002591 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002592 hlua_pusherror(L, "socket: full stack");
2593 goto out_fail_conf;
2594 }
2595
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002596 /* Create the object: obj[0] = userdata. */
2597 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002598 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002599 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002600 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002601 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002602
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002603 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002604 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002605 hlua_pusherror(L, "socket: uninitialized pools.");
2606 goto out_fail_conf;
2607 }
2608
Willy Tarreau87b09662015-04-03 00:22:06 +02002609 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002610 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2611 lua_setmetatable(L, -2);
2612
Willy Tarreaud420a972015-04-06 00:39:18 +02002613 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002614 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002615 if (!appctx) {
2616 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002617 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002618 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002619
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002620 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002621 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002622 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2623 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002624
Willy Tarreaud420a972015-04-06 00:39:18 +02002625 /* Now create a session, task and stream for this applet */
2626 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2627 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002628 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002629 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002630 }
2631
Willy Tarreau87787ac2017-08-28 16:22:54 +02002632 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002633 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002634 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002635 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002636 }
2637
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002638 /* Initialise cross reference between stream and Lua socket object. */
2639 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002640
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002641 /* Configure "right" stream interface. this "si" is used to connect
2642 * and retrieve data from the server. The connection is initialized
2643 * with the "struct server".
2644 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002645 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002646
2647 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002648 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002649 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002650
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002651 return 1;
2652
Willy Tarreaud420a972015-04-06 00:39:18 +02002653 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002654 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002655 out_fail_sess:
2656 appctx_free(appctx);
2657 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002658 WILL_LJMP(lua_error(L));
2659 return 0;
2660}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002661
2662/*
2663 *
2664 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002665 * Class Channel
2666 *
2667 *
2668 */
2669
2670/* Returns the struct hlua_channel join to the class channel in the
2671 * stack entry "ud" or throws an argument error.
2672 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002673__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002674{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002675 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002676}
2677
Willy Tarreau47860ed2015-03-10 14:07:50 +01002678/* Pushes the channel onto the top of the stack. If the stask does not have a
2679 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002680 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002681static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002682{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002683 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002684 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002685 return 0;
2686
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002687 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002688 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002689 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002690
2691 /* Pop a class sesison metatable and affect it to the userdata. */
2692 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2693 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002694 return 1;
2695}
2696
2697/* Duplicate all the data present in the input channel and put it
2698 * in a string LUA variables. Returns -1 and push a nil value in
2699 * the stack if the channel is closed and all the data are consumed,
2700 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002701 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002702 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002703static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002704{
2705 char *blk1;
2706 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002707 size_t len1;
2708 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002709 int ret;
2710 luaL_Buffer b;
2711
Willy Tarreau06d80a92017-10-19 14:32:15 +02002712 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002713 if (unlikely(ret == 0))
2714 return 0;
2715
2716 if (unlikely(ret < 0)) {
2717 lua_pushnil(L);
2718 return -1;
2719 }
2720
2721 luaL_buffinit(L, &b);
2722 luaL_addlstring(&b, blk1, len1);
2723 if (unlikely(ret == 2))
2724 luaL_addlstring(&b, blk2, len2);
2725 luaL_pushresult(&b);
2726
2727 if (unlikely(ret == 2))
2728 return len1 + len2;
2729 return len1;
2730}
2731
2732/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2733 * a yield. This function keep the data in the buffer.
2734 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002735__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002736{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002737 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002738
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002739 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2740
Christopher Faulet3f829a42018-12-13 21:56:45 +01002741 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2742 WILL_LJMP(lua_error(L));
2743
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002744 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002745 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002746 return 1;
2747}
2748
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002749/* Check arguments for the function "hlua_channel_dup_yield". */
2750__LJMP static int hlua_channel_dup(lua_State *L)
2751{
2752 MAY_LJMP(check_args(L, 1, "dup"));
2753 MAY_LJMP(hlua_checkchannel(L, 1));
2754 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2755}
2756
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002757/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2758 * a yield. This function consumes the data in the buffer. It returns
2759 * a string containing the data or a nil pointer if no data are available
2760 * and the channel is closed.
2761 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002762__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002763{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002764 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002765 int ret;
2766
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002767 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002768
Christopher Faulet3f829a42018-12-13 21:56:45 +01002769 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2770 WILL_LJMP(lua_error(L));
2771
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002772 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002773 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002774 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002775
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002776 if (unlikely(ret == -1))
2777 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002778
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002779 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002780 return 1;
2781}
2782
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002783/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002784__LJMP static int hlua_channel_get(lua_State *L)
2785{
2786 MAY_LJMP(check_args(L, 1, "get"));
2787 MAY_LJMP(hlua_checkchannel(L, 1));
2788 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2789}
2790
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002791/* This functions consumes and returns one line. If the channel is closed,
2792 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002793 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002794 * value.
2795 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002796__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002797{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002798 char *blk1;
2799 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002800 size_t len1;
2801 size_t len2;
2802 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002803 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002804 int ret;
2805 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002806
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002807 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2808
Christopher Faulet3f829a42018-12-13 21:56:45 +01002809 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2810 WILL_LJMP(lua_error(L));
2811
Willy Tarreau06d80a92017-10-19 14:32:15 +02002812 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002813 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002814 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002815
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002816 if (ret == -1) {
2817 lua_pushnil(L);
2818 return 1;
2819 }
2820
2821 luaL_buffinit(L, &b);
2822 luaL_addlstring(&b, blk1, len1);
2823 len = len1;
2824 if (unlikely(ret == 2)) {
2825 luaL_addlstring(&b, blk2, len2);
2826 len += len2;
2827 }
2828 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002829 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002830 return 1;
2831}
2832
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002833/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002834__LJMP static int hlua_channel_getline(lua_State *L)
2835{
2836 MAY_LJMP(check_args(L, 1, "getline"));
2837 MAY_LJMP(hlua_checkchannel(L, 1));
2838 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2839}
2840
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002841/* This function takes a string as input, and append it at the
2842 * input side of channel. If the data is too big, but a space
2843 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002844 * yields. If the data is bigger than the buffer, or if the
2845 * channel is closed, it returns -1. Otherwise, it returns the
2846 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002847 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002848__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002849{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002850 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002851 size_t len;
2852 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2853 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2854 int ret;
2855 int max;
2856
Christopher Faulet3f829a42018-12-13 21:56:45 +01002857 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2858 WILL_LJMP(lua_error(L));
2859
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002860 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002861 * the request buffer if its not required.
2862 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002863 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002864 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002865 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002866 }
2867
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002868 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002869 if (max > len - l)
2870 max = len - l;
2871
Willy Tarreau06d80a92017-10-19 14:32:15 +02002872 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002873 if (ret == -2 || ret == -3) {
2874 lua_pushinteger(L, -1);
2875 return 1;
2876 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002877 if (ret == -1) {
2878 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002879 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002880 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002881 l += ret;
2882 lua_pop(L, 1);
2883 lua_pushinteger(L, l);
2884
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002885 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02002886 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002887 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002888 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002889 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002890 */
2891 return 1;
2892 }
2893 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02002894 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002895 return 1;
2896}
2897
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002898/* Just a wrapper of "hlua_channel_append_yield". It returns the length
2899 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002900 * buffer size is too little for the data.
2901 */
2902__LJMP static int hlua_channel_append(lua_State *L)
2903{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002904 size_t len;
2905
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002906 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002907 MAY_LJMP(hlua_checkchannel(L, 1));
2908 MAY_LJMP(luaL_checklstring(L, 2, &len));
2909 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002910 lua_pushinteger(L, 0);
2911
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002912 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002913}
2914
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002915/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002916 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002917 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918 * or -1 if the channel is closed or if the buffer size is too
2919 * little for the data.
2920 */
2921__LJMP static int hlua_channel_set(lua_State *L)
2922{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002923 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002924
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002925 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002926 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002927 lua_pushinteger(L, 0);
2928
Christopher Faulet3f829a42018-12-13 21:56:45 +01002929 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2930 WILL_LJMP(lua_error(L));
2931
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002932 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002933
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002934 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002935}
2936
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002937/* Append data in the output side of the buffer. This data is immediately
2938 * sent. The function returns the amount of data written. If the buffer
2939 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002940 * if the channel is closed.
2941 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002942__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002943{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002944 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002945 size_t len;
2946 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2947 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2948 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002949 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002950
Christopher Faulet3f829a42018-12-13 21:56:45 +01002951 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2952 WILL_LJMP(lua_error(L));
2953
Willy Tarreau47860ed2015-03-10 14:07:50 +01002954 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002955 lua_pushinteger(L, -1);
2956 return 1;
2957 }
2958
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002959 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002960 * the request buffer if its not required.
2961 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002962 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002963 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002964 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002965 }
2966
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002967 /* The written data will be immediately sent, so we can check
2968 * the available space without taking in account the reserve.
2969 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002970 * data, because the buffer will be flushed.
2971 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002972 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002973
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002974 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002975 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002976 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002977 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02002978 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002979 return 1;
2980
2981 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002982 if (max > len - l)
2983 max = len - l;
2984
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002985 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002986 * detects a non contiguous buffer and realign it.
2987 */
Willy Tarreau3f679992018-06-15 15:06:42 +02002988 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002989 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002990
2991 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002992 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002993
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994 /* buffer replace considers that the input part is filled.
2995 * so, I must forward these new data in the output part.
2996 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02002997 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002998
2999 l += max;
3000 lua_pop(L, 1);
3001 lua_pushinteger(L, l);
3002
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003003 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003004 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003005 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003006 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003007 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003008 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003009 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003010
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003011 if (l < len) {
3012 /* If we are waiting for space in the response buffer, we
3013 * must set the flag WAKERESWR. This flag required the task
3014 * wake up if any activity is detected on the response buffer.
3015 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003016 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003017 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003018 else
3019 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003020 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003021 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003022
3023 return 1;
3024}
3025
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003026/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003027 * yield the LUA process, and resume it without checking the
3028 * input arguments.
3029 */
3030__LJMP static int hlua_channel_send(lua_State *L)
3031{
3032 MAY_LJMP(check_args(L, 2, "send"));
3033 lua_pushinteger(L, 0);
3034
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003035 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003036}
3037
3038/* This function forward and amount of butes. The data pass from
3039 * the input side of the buffer to the output side, and can be
3040 * forwarded. This function never fails.
3041 *
3042 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003043 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003044 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003045__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003046{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003047 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003048 int len;
3049 int l;
3050 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003051 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003052
3053 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003054
3055 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3056 WILL_LJMP(lua_error(L));
3057
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003058 len = MAY_LJMP(luaL_checkinteger(L, 2));
3059 l = MAY_LJMP(luaL_checkinteger(L, -1));
3060
3061 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003062 if (max > ci_data(chn))
3063 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003064 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003065 l += max;
3066
3067 lua_pop(L, 1);
3068 lua_pushinteger(L, l);
3069
3070 /* Check if it miss bytes to forward. */
3071 if (l < len) {
3072 /* The the input channel or the output channel are closed, we
3073 * must return the amount of data forwarded.
3074 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003075 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003076 return 1;
3077
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003078 /* If we are waiting for space data in the response buffer, we
3079 * must set the flag WAKERESWR. This flag required the task
3080 * wake up if any activity is detected on the response buffer.
3081 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003082 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003083 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003084 else
3085 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003086
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003087 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003088 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003089 }
3090
3091 return 1;
3092}
3093
3094/* Just check the input and prepare the stack for the previous
3095 * function "hlua_channel_forward_yield"
3096 */
3097__LJMP static int hlua_channel_forward(lua_State *L)
3098{
3099 MAY_LJMP(check_args(L, 2, "forward"));
3100 MAY_LJMP(hlua_checkchannel(L, 1));
3101 MAY_LJMP(luaL_checkinteger(L, 2));
3102
3103 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003104 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003105}
3106
3107/* Just returns the number of bytes available in the input
3108 * side of the buffer. This function never fails.
3109 */
3110__LJMP static int hlua_channel_get_in_len(lua_State *L)
3111{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003112 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003113
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003114 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003115 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003116 if (IS_HTX_STRM(chn_strm(chn))) {
3117 struct htx *htx = htxbuf(&chn->buf);
3118 lua_pushinteger(L, htx->data - co_data(chn));
3119 }
3120 else
3121 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003122 return 1;
3123}
3124
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003125/* Returns true if the channel is full. */
3126__LJMP static int hlua_channel_is_full(lua_State *L)
3127{
3128 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003129
3130 MAY_LJMP(check_args(L, 1, "is_full"));
3131 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003132 /* ignore the reserve, we are not on a producer side (ie in an
3133 * applet).
3134 */
3135 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003136 return 1;
3137}
3138
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003139/* Returns true if the channel is the response channel. */
3140__LJMP static int hlua_channel_is_resp(lua_State *L)
3141{
3142 struct channel *chn;
3143
3144 MAY_LJMP(check_args(L, 1, "is_resp"));
3145 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3146
3147 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3148 return 1;
3149}
3150
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003151/* Just returns the number of bytes available in the output
3152 * side of the buffer. This function never fails.
3153 */
3154__LJMP static int hlua_channel_get_out_len(lua_State *L)
3155{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003156 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003157
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003158 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003159 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003160 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003161 return 1;
3162}
3163
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003164/*
3165 *
3166 *
3167 * Class Fetches
3168 *
3169 *
3170 */
3171
3172/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003173 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003174 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003175__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003176{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003177 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003178}
3179
3180/* This function creates and push in the stack a fetch object according
3181 * with a current TXN.
3182 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003183static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003184{
Willy Tarreau7073c472015-04-06 11:15:40 +02003185 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003186
3187 /* Check stack size. */
3188 if (!lua_checkstack(L, 3))
3189 return 0;
3190
3191 /* Create the object: obj[0] = userdata.
3192 * Note that the base of the Fetches object is the
3193 * transaction object.
3194 */
3195 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003196 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003197 lua_rawseti(L, -2, 0);
3198
Willy Tarreau7073c472015-04-06 11:15:40 +02003199 hsmp->s = txn->s;
3200 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003201 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003202 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003203
3204 /* Pop a class sesison metatable and affect it to the userdata. */
3205 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3206 lua_setmetatable(L, -2);
3207
3208 return 1;
3209}
3210
3211/* This function is an LUA binding. It is called with each sample-fetch.
3212 * It uses closure argument to store the associated sample-fetch. It
3213 * returns only one argument or throws an error. An error is thrown
3214 * only if an error is encountered during the argument parsing. If
3215 * the "sample-fetch" function fails, nil is returned.
3216 */
3217__LJMP static int hlua_run_sample_fetch(lua_State *L)
3218{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003219 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003220 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003221 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003222 int i;
3223 struct sample smp;
3224
3225 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003226 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003227
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003228 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003229 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003230
Thierry FOURNIERca988662015-12-20 18:43:03 +01003231 /* Check execution authorization. */
3232 if (f->use & SMP_USE_HTTP_ANY &&
3233 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3234 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3235 "is not available in Lua services", f->kw);
3236 WILL_LJMP(lua_error(L));
3237 }
3238
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003239 /* Get extra arguments. */
3240 for (i = 0; i < lua_gettop(L) - 1; i++) {
3241 if (i >= ARGM_NBARGS)
3242 break;
3243 hlua_lua2arg(L, i + 2, &args[i]);
3244 }
3245 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003246 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003247
3248 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003249 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003250
3251 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003252 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003253 lua_pushfstring(L, "error in arguments");
3254 WILL_LJMP(lua_error(L));
3255 }
3256
3257 /* Initialise the sample. */
3258 memset(&smp, 0, sizeof(smp));
3259
3260 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003261 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003262 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003263 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003264 lua_pushstring(L, "");
3265 else
3266 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003267 return 1;
3268 }
3269
3270 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003271 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003272 hlua_smp2lua_str(L, &smp);
3273 else
3274 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003275 return 1;
3276}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003277
3278/*
3279 *
3280 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003281 * Class Converters
3282 *
3283 *
3284 */
3285
3286/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003287 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003288 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003289__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003290{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003291 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003292}
3293
3294/* This function creates and push in the stack a Converters object
3295 * according with a current TXN.
3296 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003297static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003298{
Willy Tarreau7073c472015-04-06 11:15:40 +02003299 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003300
3301 /* Check stack size. */
3302 if (!lua_checkstack(L, 3))
3303 return 0;
3304
3305 /* Create the object: obj[0] = userdata.
3306 * Note that the base of the Converters object is the
3307 * same than the TXN object.
3308 */
3309 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003310 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003311 lua_rawseti(L, -2, 0);
3312
Willy Tarreau7073c472015-04-06 11:15:40 +02003313 hsmp->s = txn->s;
3314 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003315 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003316 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003317
Willy Tarreau87b09662015-04-03 00:22:06 +02003318 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003319 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3320 lua_setmetatable(L, -2);
3321
3322 return 1;
3323}
3324
3325/* This function is an LUA binding. It is called with each converter.
3326 * It uses closure argument to store the associated converter. It
3327 * returns only one argument or throws an error. An error is thrown
3328 * only if an error is encountered during the argument parsing. If
3329 * the converter function function fails, nil is returned.
3330 */
3331__LJMP static int hlua_run_sample_conv(lua_State *L)
3332{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003333 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003334 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003335 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003336 int i;
3337 struct sample smp;
3338
3339 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003340 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003341
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003342 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003343 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003344
3345 /* Get extra arguments. */
3346 for (i = 0; i < lua_gettop(L) - 2; i++) {
3347 if (i >= ARGM_NBARGS)
3348 break;
3349 hlua_lua2arg(L, i + 3, &args[i]);
3350 }
3351 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003352 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003353
3354 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003355 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003356
3357 /* Run the special args checker. */
3358 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3359 hlua_pusherror(L, "error in arguments");
3360 WILL_LJMP(lua_error(L));
3361 }
3362
3363 /* Initialise the sample. */
3364 if (!hlua_lua2smp(L, 2, &smp)) {
3365 hlua_pusherror(L, "error in the input argument");
3366 WILL_LJMP(lua_error(L));
3367 }
3368
Willy Tarreau1777ea62016-03-10 16:15:46 +01003369 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3370
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003371 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003372 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003373 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003374 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003375 WILL_LJMP(lua_error(L));
3376 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003377 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3378 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003379 hlua_pusherror(L, "error during the input argument casting");
3380 WILL_LJMP(lua_error(L));
3381 }
3382
3383 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003384 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003385 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003386 lua_pushstring(L, "");
3387 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003388 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003389 return 1;
3390 }
3391
3392 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003393 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003394 hlua_smp2lua_str(L, &smp);
3395 else
3396 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003397 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003398}
3399
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003400/*
3401 *
3402 *
3403 * Class AppletTCP
3404 *
3405 *
3406 */
3407
3408/* Returns a struct hlua_txn if the stack entry "ud" is
3409 * a class stream, otherwise it throws an error.
3410 */
3411__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3412{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003413 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003414}
3415
3416/* This function creates and push in the stack an Applet object
3417 * according with a current TXN.
3418 */
3419static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3420{
3421 struct hlua_appctx *appctx;
3422 struct stream_interface *si = ctx->owner;
3423 struct stream *s = si_strm(si);
3424 struct proxy *p = s->be;
3425
3426 /* Check stack size. */
3427 if (!lua_checkstack(L, 3))
3428 return 0;
3429
3430 /* Create the object: obj[0] = userdata.
3431 * Note that the base of the Converters object is the
3432 * same than the TXN object.
3433 */
3434 lua_newtable(L);
3435 appctx = lua_newuserdata(L, sizeof(*appctx));
3436 lua_rawseti(L, -2, 0);
3437 appctx->appctx = ctx;
3438 appctx->htxn.s = s;
3439 appctx->htxn.p = p;
3440
3441 /* Create the "f" field that contains a list of fetches. */
3442 lua_pushstring(L, "f");
3443 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3444 return 0;
3445 lua_settable(L, -3);
3446
3447 /* Create the "sf" field that contains a list of stringsafe fetches. */
3448 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003449 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003450 return 0;
3451 lua_settable(L, -3);
3452
3453 /* Create the "c" field that contains a list of converters. */
3454 lua_pushstring(L, "c");
3455 if (!hlua_converters_new(L, &appctx->htxn, 0))
3456 return 0;
3457 lua_settable(L, -3);
3458
3459 /* Create the "sc" field that contains a list of stringsafe converters. */
3460 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003461 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003462 return 0;
3463 lua_settable(L, -3);
3464
3465 /* Pop a class stream metatable and affect it to the table. */
3466 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3467 lua_setmetatable(L, -2);
3468
3469 return 1;
3470}
3471
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003472__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3473{
3474 struct hlua_appctx *appctx;
3475 struct stream *s;
3476 const char *name;
3477 size_t len;
3478 struct sample smp;
3479
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003480 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3481 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003482
3483 /* It is useles to retrieve the stream, but this function
3484 * runs only in a stream context.
3485 */
3486 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3487 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3488 s = appctx->htxn.s;
3489
3490 /* Converts the third argument in a sample. */
3491 hlua_lua2smp(L, 3, &smp);
3492
3493 /* Store the sample in a variable. */
3494 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003495
3496 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3497 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3498 else
3499 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3500
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003501 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003502}
3503
3504__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3505{
3506 struct hlua_appctx *appctx;
3507 struct stream *s;
3508 const char *name;
3509 size_t len;
3510 struct sample smp;
3511
3512 MAY_LJMP(check_args(L, 2, "unset_var"));
3513
3514 /* It is useles to retrieve the stream, but this function
3515 * runs only in a stream context.
3516 */
3517 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3518 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3519 s = appctx->htxn.s;
3520
3521 /* Unset the variable. */
3522 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003523 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3524 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003525}
3526
3527__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3528{
3529 struct hlua_appctx *appctx;
3530 struct stream *s;
3531 const char *name;
3532 size_t len;
3533 struct sample smp;
3534
3535 MAY_LJMP(check_args(L, 2, "get_var"));
3536
3537 /* It is useles to retrieve the stream, but this function
3538 * runs only in a stream context.
3539 */
3540 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3541 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3542 s = appctx->htxn.s;
3543
3544 smp_set_owner(&smp, s->be, s->sess, s, 0);
3545 if (!vars_get_by_name(name, len, &smp)) {
3546 lua_pushnil(L);
3547 return 1;
3548 }
3549
3550 return hlua_smp2lua(L, &smp);
3551}
3552
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003553__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3554{
3555 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3556 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003557 struct hlua *hlua;
3558
3559 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003560 if (!s->hlua)
3561 return 0;
3562 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003563
3564 MAY_LJMP(check_args(L, 2, "set_priv"));
3565
3566 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003567 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003568
3569 /* Get and store new value. */
3570 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3571 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3572
3573 return 0;
3574}
3575
3576__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3577{
3578 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3579 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003580 struct hlua *hlua;
3581
3582 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003583 if (!s->hlua) {
3584 lua_pushnil(L);
3585 return 1;
3586 }
3587 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003588
3589 /* Push configuration index in the stack. */
3590 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3591
3592 return 1;
3593}
3594
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003595/* If expected data not yet available, it returns a yield. This function
3596 * consumes the data in the buffer. It returns a string containing the
3597 * data. This string can be empty.
3598 */
3599__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3600{
3601 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3602 struct stream_interface *si = appctx->appctx->owner;
3603 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003604 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003605 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003606 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003607 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003608
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003609 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003610 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003611
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003612 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003613 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003614 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003615 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003616 }
3617
3618 /* End of data: commit the total strings and return. */
3619 if (ret < 0) {
3620 luaL_pushresult(&appctx->b);
3621 return 1;
3622 }
3623
3624 /* Ensure that the block 2 length is usable. */
3625 if (ret == 1)
3626 len2 = 0;
3627
3628 /* dont check the max length read and dont check. */
3629 luaL_addlstring(&appctx->b, blk1, len1);
3630 luaL_addlstring(&appctx->b, blk2, len2);
3631
3632 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003633 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003634 luaL_pushresult(&appctx->b);
3635 return 1;
3636}
3637
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003638/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003639__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3640{
3641 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3642
3643 /* Initialise the string catenation. */
3644 luaL_buffinit(L, &appctx->b);
3645
3646 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3647}
3648
3649/* If expected data not yet available, it returns a yield. This function
3650 * consumes the data in the buffer. It returns a string containing the
3651 * data. This string can be empty.
3652 */
3653__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3654{
3655 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3656 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003657 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003658 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003659 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003660 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003661 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003662 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003663
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003664 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003665 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003666
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003667 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003668 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003669 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003670 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003671 }
3672
3673 /* End of data: commit the total strings and return. */
3674 if (ret < 0) {
3675 luaL_pushresult(&appctx->b);
3676 return 1;
3677 }
3678
3679 /* Ensure that the block 2 length is usable. */
3680 if (ret == 1)
3681 len2 = 0;
3682
3683 if (len == -1) {
3684
3685 /* If len == -1, catenate all the data avalaile and
3686 * yield because we want to get all the data until
3687 * the end of data stream.
3688 */
3689 luaL_addlstring(&appctx->b, blk1, len1);
3690 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003691 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003692 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003693 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003694
3695 } else {
3696
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003697 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003698 if (len1 > len)
3699 len1 = len;
3700 luaL_addlstring(&appctx->b, blk1, len1);
3701 len -= len1;
3702
3703 /* Copy the second block. */
3704 if (len2 > len)
3705 len2 = len;
3706 luaL_addlstring(&appctx->b, blk2, len2);
3707 len -= len2;
3708
3709 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003710 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003711
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003712 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003713 if (len > 0) {
3714 lua_pushinteger(L, len);
3715 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003716 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003717 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003718 }
3719
3720 /* return the result. */
3721 luaL_pushresult(&appctx->b);
3722 return 1;
3723 }
3724
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003725 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003726 hlua_pusherror(L, "Lua: internal error");
3727 WILL_LJMP(lua_error(L));
3728 return 0;
3729}
3730
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003731/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003732__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3733{
3734 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3735 int len = -1;
3736
3737 if (lua_gettop(L) > 2)
3738 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3739 if (lua_gettop(L) >= 2) {
3740 len = MAY_LJMP(luaL_checkinteger(L, 2));
3741 lua_pop(L, 1);
3742 }
3743
3744 /* Confirm or set the required length */
3745 lua_pushinteger(L, len);
3746
3747 /* Initialise the string catenation. */
3748 luaL_buffinit(L, &appctx->b);
3749
3750 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3751}
3752
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003753/* Append data in the output side of the buffer. This data is immediately
3754 * sent. The function returns the amount of data written. If the buffer
3755 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003756 * if the channel is closed.
3757 */
3758__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3759{
3760 size_t len;
3761 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3762 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3763 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3764 struct stream_interface *si = appctx->appctx->owner;
3765 struct channel *chn = si_ic(si);
3766 int max;
3767
3768 /* Get the max amount of data which can write as input in the channel. */
3769 max = channel_recv_max(chn);
3770 if (max > (len - l))
3771 max = len - l;
3772
3773 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003774 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003775
3776 /* update counters. */
3777 l += max;
3778 lua_pop(L, 1);
3779 lua_pushinteger(L, l);
3780
3781 /* If some data is not send, declares the situation to the
3782 * applet, and returns a yield.
3783 */
3784 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003785 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003786 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003787 }
3788
3789 return 1;
3790}
3791
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003792/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003793 * yield the LUA process, and resume it without checking the
3794 * input arguments.
3795 */
3796__LJMP static int hlua_applet_tcp_send(lua_State *L)
3797{
3798 MAY_LJMP(check_args(L, 2, "send"));
3799 lua_pushinteger(L, 0);
3800
3801 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3802}
3803
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003804/*
3805 *
3806 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003807 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003808 *
3809 *
3810 */
3811
3812/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003813 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003814 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003815__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003816{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003817 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003818}
3819
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003820/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003821 * according with a current TXN.
3822 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003823static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003824{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003825 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003826 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003827 struct stream_interface *si = ctx->owner;
3828 struct stream *s = si_strm(si);
3829 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02003830 struct htx *htx;
3831 struct htx_blk *blk;
3832 struct htx_sl *sl;
3833 struct ist path;
3834 unsigned long long len = 0;
3835 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003836
3837 /* Check stack size. */
3838 if (!lua_checkstack(L, 3))
3839 return 0;
3840
3841 /* Create the object: obj[0] = userdata.
3842 * Note that the base of the Converters object is the
3843 * same than the TXN object.
3844 */
3845 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003846 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003847 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003848 appctx->appctx = ctx;
3849 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003850 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003851 appctx->htxn.s = s;
3852 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003853
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003854 /* Create the "f" field that contains a list of fetches. */
3855 lua_pushstring(L, "f");
3856 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3857 return 0;
3858 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003859
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003860 /* Create the "sf" field that contains a list of stringsafe fetches. */
3861 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003862 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003863 return 0;
3864 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003865
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003866 /* Create the "c" field that contains a list of converters. */
3867 lua_pushstring(L, "c");
3868 if (!hlua_converters_new(L, &appctx->htxn, 0))
3869 return 0;
3870 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003871
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003872 /* Create the "sc" field that contains a list of stringsafe converters. */
3873 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003874 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003875 return 0;
3876 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003877
Christopher Fauleta2097962019-07-15 16:25:33 +02003878 htx = htxbuf(&s->req.buf);
3879 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01003880 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02003881 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003882
Christopher Fauleta2097962019-07-15 16:25:33 +02003883 /* Stores the request method. */
3884 lua_pushstring(L, "method");
3885 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3886 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003887
Christopher Fauleta2097962019-07-15 16:25:33 +02003888 /* Stores the http version. */
3889 lua_pushstring(L, "version");
3890 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3891 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003892
Christopher Fauleta2097962019-07-15 16:25:33 +02003893 /* creates an array of headers. hlua_http_get_headers() crates and push
3894 * the array on the top of the stack.
3895 */
3896 lua_pushstring(L, "headers");
3897 htxn.s = s;
3898 htxn.p = px;
3899 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01003900 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02003901 return 0;
3902 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003903
Christopher Fauleta2097962019-07-15 16:25:33 +02003904 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01003905 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02003906 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003907
Christopher Fauleta2097962019-07-15 16:25:33 +02003908 p = path.ptr;
3909 end = path.ptr + path.len;
3910 q = p;
3911 while (q < end && *q != '?')
3912 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003913
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003914 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02003915 lua_pushstring(L, "path");
3916 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003917 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003918
Christopher Fauleta2097962019-07-15 16:25:33 +02003919 /* Stores the query string. */
3920 lua_pushstring(L, "qs");
3921 if (*q == '?')
3922 q++;
3923 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003924 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02003925 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003926
Christopher Fauleta2097962019-07-15 16:25:33 +02003927 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3928 struct htx_blk *blk = htx_get_blk(htx, pos);
3929 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003930
Christopher Fauleta2097962019-07-15 16:25:33 +02003931 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
3932 break;
3933 if (type == HTX_BLK_DATA)
3934 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003935 }
Christopher Fauleta2097962019-07-15 16:25:33 +02003936 if (htx->extra != ULLONG_MAX)
3937 len += htx->extra;
3938
3939 /* Stores the request path. */
3940 lua_pushstring(L, "length");
3941 lua_pushinteger(L, len);
3942 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003943
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003944 /* Create an empty array of HTTP request headers. */
3945 lua_pushstring(L, "response");
3946 lua_newtable(L);
3947 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003948
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003949 /* Pop a class stream metatable and affect it to the table. */
3950 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3951 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003952
3953 return 1;
3954}
3955
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003956__LJMP static int hlua_applet_http_set_var(lua_State *L)
3957{
3958 struct hlua_appctx *appctx;
3959 struct stream *s;
3960 const char *name;
3961 size_t len;
3962 struct sample smp;
3963
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003964 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3965 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003966
3967 /* It is useles to retrieve the stream, but this function
3968 * runs only in a stream context.
3969 */
3970 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3971 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3972 s = appctx->htxn.s;
3973
3974 /* Converts the third argument in a sample. */
3975 hlua_lua2smp(L, 3, &smp);
3976
3977 /* Store the sample in a variable. */
3978 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003979
3980 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3981 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3982 else
3983 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3984
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003985 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003986}
3987
3988__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3989{
3990 struct hlua_appctx *appctx;
3991 struct stream *s;
3992 const char *name;
3993 size_t len;
3994 struct sample smp;
3995
3996 MAY_LJMP(check_args(L, 2, "unset_var"));
3997
3998 /* It is useles to retrieve the stream, but this function
3999 * runs only in a stream context.
4000 */
4001 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4002 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4003 s = appctx->htxn.s;
4004
4005 /* Unset the variable. */
4006 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004007 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4008 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004009}
4010
4011__LJMP static int hlua_applet_http_get_var(lua_State *L)
4012{
4013 struct hlua_appctx *appctx;
4014 struct stream *s;
4015 const char *name;
4016 size_t len;
4017 struct sample smp;
4018
4019 MAY_LJMP(check_args(L, 2, "get_var"));
4020
4021 /* It is useles to retrieve the stream, but this function
4022 * runs only in a stream context.
4023 */
4024 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4025 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4026 s = appctx->htxn.s;
4027
4028 smp_set_owner(&smp, s->be, s->sess, s, 0);
4029 if (!vars_get_by_name(name, len, &smp)) {
4030 lua_pushnil(L);
4031 return 1;
4032 }
4033
4034 return hlua_smp2lua(L, &smp);
4035}
4036
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004037__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4038{
4039 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4040 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004041 struct hlua *hlua;
4042
4043 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004044 if (!s->hlua)
4045 return 0;
4046 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004047
4048 MAY_LJMP(check_args(L, 2, "set_priv"));
4049
4050 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004051 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004052
4053 /* Get and store new value. */
4054 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4055 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4056
4057 return 0;
4058}
4059
4060__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4061{
4062 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4063 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004064 struct hlua *hlua;
4065
4066 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004067 if (!s->hlua) {
4068 lua_pushnil(L);
4069 return 1;
4070 }
4071 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004072
4073 /* Push configuration index in the stack. */
4074 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4075
4076 return 1;
4077}
4078
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004079/* If expected data not yet available, it returns a yield. This function
4080 * consumes the data in the buffer. It returns a string containing the
4081 * data. This string can be empty.
4082 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004083__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004084{
4085 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4086 struct stream_interface *si = appctx->appctx->owner;
4087 struct channel *req = si_oc(si);
4088 struct htx *htx;
4089 struct htx_blk *blk;
4090 size_t count;
4091 int stop = 0;
4092
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004093 htx = htx_from_buf(&req->buf);
4094 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004095 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004096
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004097 while (count && !stop && blk) {
4098 enum htx_blk_type type = htx_get_blk_type(blk);
4099 uint32_t sz = htx_get_blksz(blk);
4100 struct ist v;
4101 uint32_t vlen;
4102 char *nl;
4103
Christopher Faulete461e342018-12-18 16:43:35 +01004104 if (type == HTX_BLK_EOM) {
4105 stop = 1;
4106 break;
4107 }
4108
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004109 vlen = sz;
4110 if (vlen > count) {
4111 if (type != HTX_BLK_DATA)
4112 break;
4113 vlen = count;
4114 }
4115
4116 switch (type) {
4117 case HTX_BLK_UNUSED:
4118 break;
4119
4120 case HTX_BLK_DATA:
4121 v = htx_get_blk_value(htx, blk);
4122 v.len = vlen;
4123 nl = istchr(v, '\n');
4124 if (nl != NULL) {
4125 stop = 1;
4126 vlen = nl - v.ptr + 1;
4127 }
4128 luaL_addlstring(&appctx->b, v.ptr, vlen);
4129 break;
4130
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004131 case HTX_BLK_TLR:
4132 case HTX_BLK_EOM:
4133 stop = 1;
4134 break;
4135
4136 default:
4137 break;
4138 }
4139
4140 co_set_data(req, co_data(req) - vlen);
4141 count -= vlen;
4142 if (sz == vlen)
4143 blk = htx_remove_blk(htx, blk);
4144 else {
4145 htx_cut_data_blk(htx, blk, vlen);
4146 break;
4147 }
4148 }
4149
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004150 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004151 if (!stop) {
4152 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004153 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004154 }
4155
4156 /* return the result. */
4157 luaL_pushresult(&appctx->b);
4158 return 1;
4159}
4160
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004161
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004162/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004163__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004164{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004165 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004166
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004167 /* Initialise the string catenation. */
4168 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004169
Christopher Fauleta2097962019-07-15 16:25:33 +02004170 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004171}
4172
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004173/* If expected data not yet available, it returns a yield. This function
4174 * consumes the data in the buffer. It returns a string containing the
4175 * data. This string can be empty.
4176 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004177__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004178{
4179 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4180 struct stream_interface *si = appctx->appctx->owner;
4181 struct channel *req = si_oc(si);
4182 struct htx *htx;
4183 struct htx_blk *blk;
4184 size_t count;
4185 int len;
4186
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004187 htx = htx_from_buf(&req->buf);
4188 len = MAY_LJMP(luaL_checkinteger(L, 2));
4189 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004190 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004191 while (count && len && blk) {
4192 enum htx_blk_type type = htx_get_blk_type(blk);
4193 uint32_t sz = htx_get_blksz(blk);
4194 struct ist v;
4195 uint32_t vlen;
4196
Christopher Faulete461e342018-12-18 16:43:35 +01004197 if (type == HTX_BLK_EOM) {
4198 len = 0;
4199 break;
4200 }
4201
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004202 vlen = sz;
4203 if (len > 0 && vlen > len)
4204 vlen = len;
4205 if (vlen > count) {
4206 if (type != HTX_BLK_DATA)
4207 break;
4208 vlen = count;
4209 }
4210
4211 switch (type) {
4212 case HTX_BLK_UNUSED:
4213 break;
4214
4215 case HTX_BLK_DATA:
4216 v = htx_get_blk_value(htx, blk);
4217 luaL_addlstring(&appctx->b, v.ptr, vlen);
4218 break;
4219
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004220 case HTX_BLK_TLR:
4221 case HTX_BLK_EOM:
4222 len = 0;
4223 break;
4224
4225 default:
4226 break;
4227 }
4228
4229 co_set_data(req, co_data(req) - vlen);
4230 count -= vlen;
4231 if (len > 0)
4232 len -= vlen;
4233 if (sz == vlen)
4234 blk = htx_remove_blk(htx, blk);
4235 else {
4236 htx_cut_data_blk(htx, blk, vlen);
4237 break;
4238 }
4239 }
4240
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004241 htx_to_buf(htx, &req->buf);
4242
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004243 /* If we are no other data available, yield waiting for new data. */
4244 if (len) {
4245 if (len > 0) {
4246 lua_pushinteger(L, len);
4247 lua_replace(L, 2);
4248 }
4249 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004250 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004251 }
4252
4253 /* return the result. */
4254 luaL_pushresult(&appctx->b);
4255 return 1;
4256}
4257
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004258/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004259__LJMP static int hlua_applet_http_recv(lua_State *L)
4260{
4261 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4262 int len = -1;
4263
4264 /* Check arguments. */
4265 if (lua_gettop(L) > 2)
4266 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4267 if (lua_gettop(L) >= 2) {
4268 len = MAY_LJMP(luaL_checkinteger(L, 2));
4269 lua_pop(L, 1);
4270 }
4271
Christopher Fauleta2097962019-07-15 16:25:33 +02004272 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004273
Christopher Fauleta2097962019-07-15 16:25:33 +02004274 /* Initialise the string catenation. */
4275 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004276
Christopher Fauleta2097962019-07-15 16:25:33 +02004277 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004278}
4279
4280/* Append data in the output side of the buffer. This data is immediately
4281 * sent. The function returns the amount of data written. If the buffer
4282 * cannot contain the data, the function yields. The function returns -1
4283 * if the channel is closed.
4284 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004285__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004286{
4287 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4288 struct stream_interface *si = appctx->appctx->owner;
4289 struct channel *res = si_ic(si);
4290 struct htx *htx = htx_from_buf(&res->buf);
4291 const char *data;
4292 size_t len;
4293 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4294 int max;
4295
Christopher Faulet9060fc02019-07-03 11:39:30 +02004296 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004297 if (!max)
4298 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004299
4300 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4301
4302 /* Get the max amount of data which can write as input in the channel. */
4303 if (max > (len - l))
4304 max = len - l;
4305
4306 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004307 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004308 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004309
4310 /* update counters. */
4311 l += max;
4312 lua_pop(L, 1);
4313 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004314
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004315 /* If some data is not send, declares the situation to the
4316 * applet, and returns a yield.
4317 */
4318 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004319 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004320 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004321 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004322 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004323 }
4324
Christopher Fauleta2097962019-07-15 16:25:33 +02004325 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004326 return 1;
4327}
4328
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004329/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004330 * yield the LUA process, and resume it without checking the
4331 * input arguments.
4332 */
4333__LJMP static int hlua_applet_http_send(lua_State *L)
4334{
4335 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004336
4337 /* We want to send some data. Headers must be sent. */
4338 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4339 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4340 WILL_LJMP(lua_error(L));
4341 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004342
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004343 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004344 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004345
Christopher Fauleta2097962019-07-15 16:25:33 +02004346 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004347}
4348
4349__LJMP static int hlua_applet_http_addheader(lua_State *L)
4350{
4351 const char *name;
4352 int ret;
4353
4354 MAY_LJMP(hlua_checkapplet_http(L, 1));
4355 name = MAY_LJMP(luaL_checkstring(L, 2));
4356 MAY_LJMP(luaL_checkstring(L, 3));
4357
4358 /* Push in the stack the "response" entry. */
4359 ret = lua_getfield(L, 1, "response");
4360 if (ret != LUA_TTABLE) {
4361 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4362 "is expected as an array. %s found", lua_typename(L, ret));
4363 WILL_LJMP(lua_error(L));
4364 }
4365
4366 /* check if the header is already registered if it is not
4367 * the case, register it.
4368 */
4369 ret = lua_getfield(L, -1, name);
4370 if (ret == LUA_TNIL) {
4371
4372 /* Entry not found. */
4373 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4374
4375 /* Insert the new header name in the array in the top of the stack.
4376 * It left the new array in the top of the stack.
4377 */
4378 lua_newtable(L);
4379 lua_pushvalue(L, 2);
4380 lua_pushvalue(L, -2);
4381 lua_settable(L, -4);
4382
4383 } else if (ret != LUA_TTABLE) {
4384
4385 /* corruption error. */
4386 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4387 "is expected as an array. %s found", name, lua_typename(L, ret));
4388 WILL_LJMP(lua_error(L));
4389 }
4390
4391 /* Now the top od thestack is an array of values. We push
4392 * the header value as new entry.
4393 */
4394 lua_pushvalue(L, 3);
4395 ret = lua_rawlen(L, -2);
4396 lua_rawseti(L, -2, ret + 1);
4397 lua_pushboolean(L, 1);
4398 return 1;
4399}
4400
4401__LJMP static int hlua_applet_http_status(lua_State *L)
4402{
4403 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4404 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004405 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004406
4407 if (status < 100 || status > 599) {
4408 lua_pushboolean(L, 0);
4409 return 1;
4410 }
4411
4412 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004413 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004414 lua_pushboolean(L, 1);
4415 return 1;
4416}
4417
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004418
Christopher Fauleta2097962019-07-15 16:25:33 +02004419__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004420{
4421 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4422 struct stream_interface *si = appctx->appctx->owner;
4423 struct channel *res = si_ic(si);
4424 struct htx *htx;
4425 struct htx_sl *sl;
4426 struct h1m h1m;
4427 const char *status, *reason;
4428 const char *name, *value;
4429 size_t nlen, vlen;
4430 unsigned int flags;
4431
4432 /* Send the message at once. */
4433 htx = htx_from_buf(&res->buf);
4434 h1m_init_res(&h1m);
4435
4436 /* Use the same http version than the request. */
4437 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4438 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4439 if (reason == NULL)
4440 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4441 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4442 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4443 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4444 }
4445 else {
4446 flags = HTX_SL_F_IS_RESP;
4447 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4448 }
4449 if (!sl) {
4450 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4451 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4452 WILL_LJMP(lua_error(L));
4453 }
4454 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4455
4456 /* Get the array associated to the field "response" in the object AppletHTTP. */
4457 lua_pushvalue(L, 0);
4458 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4459 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4460 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4461 WILL_LJMP(lua_error(L));
4462 }
4463
4464 /* Browse the list of headers. */
4465 lua_pushnil(L);
4466 while(lua_next(L, -2) != 0) {
4467 /* We expect a string as -2. */
4468 if (lua_type(L, -2) != LUA_TSTRING) {
4469 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4470 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4471 lua_typename(L, lua_type(L, -2)));
4472 WILL_LJMP(lua_error(L));
4473 }
4474 name = lua_tolstring(L, -2, &nlen);
4475
4476 /* We expect an array as -1. */
4477 if (lua_type(L, -1) != LUA_TTABLE) {
4478 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4479 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4480 name,
4481 lua_typename(L, lua_type(L, -1)));
4482 WILL_LJMP(lua_error(L));
4483 }
4484
4485 /* Browse the table who is on the top of the stack. */
4486 lua_pushnil(L);
4487 while(lua_next(L, -2) != 0) {
4488 int id;
4489
4490 /* We expect a number as -2. */
4491 if (lua_type(L, -2) != LUA_TNUMBER) {
4492 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4493 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4494 name,
4495 lua_typename(L, lua_type(L, -2)));
4496 WILL_LJMP(lua_error(L));
4497 }
4498 id = lua_tointeger(L, -2);
4499
4500 /* We expect a string as -2. */
4501 if (lua_type(L, -1) != LUA_TSTRING) {
4502 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4503 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4504 name, id,
4505 lua_typename(L, lua_type(L, -1)));
4506 WILL_LJMP(lua_error(L));
4507 }
4508 value = lua_tolstring(L, -1, &vlen);
4509
4510 /* Simple Protocol checks. */
4511 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004512 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004513 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4514 struct ist v = ist2(value, vlen);
4515 int ret;
4516
4517 ret = h1_parse_cont_len_header(&h1m, &v);
4518 if (ret < 0) {
4519 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4520 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4521 name);
4522 WILL_LJMP(lua_error(L));
4523 }
4524 else if (ret == 0)
4525 goto next; /* Skip it */
4526 }
4527
4528 /* Add a new header */
4529 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4530 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4531 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4532 name);
4533 WILL_LJMP(lua_error(L));
4534 }
4535 next:
4536 /* Remove the array from the stack, and get next element with a remaining string. */
4537 lua_pop(L, 1);
4538 }
4539
4540 /* Remove the array from the stack, and get next element with a remaining string. */
4541 lua_pop(L, 1);
4542 }
4543
4544 if (h1m.flags & H1_MF_CHNK)
4545 h1m.flags &= ~H1_MF_CLEN;
4546 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4547 h1m.flags |= H1_MF_XFER_LEN;
4548
4549 /* Uset HTX start-line flags */
4550 if (h1m.flags & H1_MF_XFER_ENC)
4551 flags |= HTX_SL_F_XFER_ENC;
4552 if (h1m.flags & H1_MF_XFER_LEN) {
4553 flags |= HTX_SL_F_XFER_LEN;
4554 if (h1m.flags & H1_MF_CHNK)
4555 flags |= HTX_SL_F_CHNK;
4556 else if (h1m.flags & H1_MF_CLEN)
4557 flags |= HTX_SL_F_CLEN;
4558 if (h1m.body_len == 0)
4559 flags |= HTX_SL_F_BODYLESS;
4560 }
4561 sl->flags |= flags;
4562
4563 /* If we dont have a content-length set, and the HTTP version is 1.1
4564 * and the status code implies the presence of a message body, we must
4565 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004566 * for the keepalive compliance. If the applet announces a transfer-encoding
4567 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004568 */
4569 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4570 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4571 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4572 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4573 /* Add a new header */
4574 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4575 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4576 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4577 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4578 WILL_LJMP(lua_error(L));
4579 }
4580 }
4581
4582 /* Finalize headers. */
4583 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4584 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4585 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4586 WILL_LJMP(lua_error(L));
4587 }
4588
4589 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4590 b_reset(&res->buf);
4591 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4592 WILL_LJMP(lua_error(L));
4593 }
4594
4595 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004596 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004597
4598 /* Headers sent, set the flag. */
4599 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4600 return 0;
4601
4602}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004603/* We will build the status line and the headers of the HTTP response.
4604 * We will try send at once if its not possible, we give back the hand
4605 * waiting for more room.
4606 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004607__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004608{
4609 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4610 struct stream_interface *si = appctx->appctx->owner;
4611 struct channel *res = si_ic(si);
4612
4613 if (co_data(res)) {
4614 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004615 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004616 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004617 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004618}
4619
4620
Christopher Fauleta2097962019-07-15 16:25:33 +02004621__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004622{
Christopher Fauleta2097962019-07-15 16:25:33 +02004623 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004624}
4625
Christopher Fauleta2097962019-07-15 16:25:33 +02004626/*
4627 *
4628 *
4629 * Class HTTP
4630 *
4631 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004632 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004633
4634/* Returns a struct hlua_txn if the stack entry "ud" is
4635 * a class stream, otherwise it throws an error.
4636 */
4637__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004638{
Christopher Fauleta2097962019-07-15 16:25:33 +02004639 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4640}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004641
Christopher Fauleta2097962019-07-15 16:25:33 +02004642/* This function creates and push in the stack a HTTP object
4643 * according with a current TXN.
4644 */
4645static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4646{
4647 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004648
Christopher Fauleta2097962019-07-15 16:25:33 +02004649 /* Check stack size. */
4650 if (!lua_checkstack(L, 3))
4651 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004652
Christopher Fauleta2097962019-07-15 16:25:33 +02004653 /* Create the object: obj[0] = userdata.
4654 * Note that the base of the Converters object is the
4655 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004656 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004657 lua_newtable(L);
4658 htxn = lua_newuserdata(L, sizeof(*htxn));
4659 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004660
4661 htxn->s = txn->s;
4662 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004663 htxn->dir = txn->dir;
4664 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004665
4666 /* Pop a class stream metatable and affect it to the table. */
4667 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4668 lua_setmetatable(L, -2);
4669
4670 return 1;
4671}
4672
4673/* This function creates ans returns an array of HTTP headers.
4674 * This function does not fails. It is used as wrapper with the
4675 * 2 following functions.
4676 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004677__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004678{
Christopher Fauleta2097962019-07-15 16:25:33 +02004679 struct htx *htx;
4680 int32_t pos;
4681
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004682 /* Create the table. */
4683 lua_newtable(L);
4684
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004685
Christopher Fauleta2097962019-07-15 16:25:33 +02004686 htx = htxbuf(&msg->chn->buf);
4687 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4688 struct htx_blk *blk = htx_get_blk(htx, pos);
4689 enum htx_blk_type type = htx_get_blk_type(blk);
4690 struct ist n, v;
4691 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004692
Christopher Fauleta2097962019-07-15 16:25:33 +02004693 if (type == HTX_BLK_HDR) {
4694 n = htx_get_blk_name(htx,blk);
4695 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004696 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004697 else if (type == HTX_BLK_EOH)
4698 break;
4699 else
4700 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004701
Christopher Fauleta2097962019-07-15 16:25:33 +02004702 /* Check for existing entry:
4703 * assume that the table is on the top of the stack, and
4704 * push the key in the stack, the function lua_gettable()
4705 * perform the lookup.
4706 */
4707 lua_pushlstring(L, n.ptr, n.len);
4708 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004709
Christopher Fauleta2097962019-07-15 16:25:33 +02004710 switch (lua_type(L, -1)) {
4711 case LUA_TNIL:
4712 /* Table not found, create it. */
4713 lua_pop(L, 1); /* remove the nil value. */
4714 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4715 lua_newtable(L); /* create and push empty table. */
4716 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4717 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4718 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004719 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004720
Christopher Fauleta2097962019-07-15 16:25:33 +02004721 case LUA_TTABLE:
4722 /* Entry found: push the value in the table. */
4723 len = lua_rawlen(L, -1);
4724 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4725 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4726 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4727 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004728
Christopher Fauleta2097962019-07-15 16:25:33 +02004729 default:
4730 /* Other cases are errors. */
4731 hlua_pusherror(L, "internal error during the parsing of headers.");
4732 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004733 }
4734 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004735 return 1;
4736}
4737
4738__LJMP static int hlua_http_req_get_headers(lua_State *L)
4739{
4740 struct hlua_txn *htxn;
4741
4742 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4743 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4744
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004745 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004746 WILL_LJMP(lua_error(L));
4747
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004748 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004749}
4750
4751__LJMP static int hlua_http_res_get_headers(lua_State *L)
4752{
4753 struct hlua_txn *htxn;
4754
4755 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4756 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4757
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004758 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004759 WILL_LJMP(lua_error(L));
4760
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004761 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004762}
4763
4764/* This function replace full header, or just a value in
4765 * the request or in the response. It is a wrapper fir the
4766 * 4 following functions.
4767 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004768__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004769{
4770 size_t name_len;
4771 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4772 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4773 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02004774 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02004775 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004776
Dragan Dosen26743032019-04-30 15:54:36 +02004777 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004778 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4779
Christopher Fauleta2097962019-07-15 16:25:33 +02004780 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004781 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02004782 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004783 return 0;
4784}
4785
4786__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4787{
4788 struct hlua_txn *htxn;
4789
4790 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4791 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4792
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004793 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004794 WILL_LJMP(lua_error(L));
4795
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004796 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004797}
4798
4799__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4800{
4801 struct hlua_txn *htxn;
4802
4803 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4804 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4805
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004806 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004807 WILL_LJMP(lua_error(L));
4808
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004809 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004810}
4811
4812__LJMP static int hlua_http_req_rep_val(lua_State *L)
4813{
4814 struct hlua_txn *htxn;
4815
4816 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4817 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4818
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004819 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004820 WILL_LJMP(lua_error(L));
4821
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004822 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004823}
4824
4825__LJMP static int hlua_http_res_rep_val(lua_State *L)
4826{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004827 struct hlua_txn *htxn;
4828
4829 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4830 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4831
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004832 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004833 WILL_LJMP(lua_error(L));
4834
Christopher Fauletd1914aa2020-02-24 16:52:46 +01004835 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004836}
4837
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004838/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004839 * It is a wrapper for the 2 following functions.
4840 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004841__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004842{
4843 size_t len;
4844 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004845 struct htx *htx = htxbuf(&msg->chn->buf);
4846 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004847
Christopher Fauleta2097962019-07-15 16:25:33 +02004848 ctx.blk = NULL;
4849 while (http_find_header(htx, ist2(name, len), &ctx, 1))
4850 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004851 return 0;
4852}
4853
4854__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4855{
4856 struct hlua_txn *htxn;
4857
4858 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4859 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4860
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004861 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004862 WILL_LJMP(lua_error(L));
4863
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004864 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004865}
4866
4867__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4868{
4869 struct hlua_txn *htxn;
4870
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004871 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004872 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4873
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004874 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004875 WILL_LJMP(lua_error(L));
4876
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004877 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004878}
4879
4880/* This function adds an header. It is a wrapper used by
4881 * the 2 following functions.
4882 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004883__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004884{
4885 size_t name_len;
4886 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4887 size_t value_len;
4888 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02004889 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004890
Christopher Fauleta2097962019-07-15 16:25:33 +02004891 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
4892 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004893 return 0;
4894}
4895
4896__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4897{
4898 struct hlua_txn *htxn;
4899
4900 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4901 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4902
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004903 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004904 WILL_LJMP(lua_error(L));
4905
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004906 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004907}
4908
4909__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4910{
4911 struct hlua_txn *htxn;
4912
4913 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4914 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4915
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004916 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004917 WILL_LJMP(lua_error(L));
4918
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004919 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004920}
4921
4922static int hlua_http_req_set_hdr(lua_State *L)
4923{
4924 struct hlua_txn *htxn;
4925
4926 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4927 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4928
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004929 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004930 WILL_LJMP(lua_error(L));
4931
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004932 hlua_http_del_hdr(L, &htxn->s->txn->req);
4933 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004934}
4935
4936static int hlua_http_res_set_hdr(lua_State *L)
4937{
4938 struct hlua_txn *htxn;
4939
4940 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4941 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4942
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004943 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004944 WILL_LJMP(lua_error(L));
4945
Christopher Fauletd31c7b32020-02-25 09:37:57 +01004946 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
4947 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004948}
4949
4950/* This function set the method. */
4951static int hlua_http_req_set_meth(lua_State *L)
4952{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004953 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004954 size_t name_len;
4955 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004956
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004957 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004958 WILL_LJMP(lua_error(L));
4959
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004960 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004961 return 1;
4962}
4963
4964/* This function set the method. */
4965static int hlua_http_req_set_path(lua_State *L)
4966{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004967 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004968 size_t name_len;
4969 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004970
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004971 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004972 WILL_LJMP(lua_error(L));
4973
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004974 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004975 return 1;
4976}
4977
4978/* This function set the query-string. */
4979static int hlua_http_req_set_query(lua_State *L)
4980{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004981 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004982 size_t name_len;
4983 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004984
Christopher Fauletd8f0e072020-02-25 09:45:51 +01004985 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02004986 WILL_LJMP(lua_error(L));
4987
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004988 /* Check length. */
4989 if (name_len > trash.size - 1) {
4990 lua_pushboolean(L, 0);
4991 return 1;
4992 }
4993
4994 /* Add the mark question as prefix. */
4995 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004996 trash.area[trash.data++] = '?';
4997 memcpy(trash.area + trash.data, name, name_len);
4998 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004999
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005000 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005001 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005002 return 1;
5003}
5004
5005/* This function set the uri. */
5006static int hlua_http_req_set_uri(lua_State *L)
5007{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005008 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005009 size_t name_len;
5010 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005011
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005012 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005013 WILL_LJMP(lua_error(L));
5014
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005015 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005016 return 1;
5017}
5018
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005019/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005020static int hlua_http_res_set_status(lua_State *L)
5021{
5022 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5023 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005024 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5025 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005026
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005027 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005028 WILL_LJMP(lua_error(L));
5029
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005030 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005031 return 0;
5032}
5033
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005034/*
5035 *
5036 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005037 * Class TXN
5038 *
5039 *
5040 */
5041
5042/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005043 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005044 */
5045__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5046{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005047 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005048}
5049
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005050__LJMP static int hlua_set_var(lua_State *L)
5051{
5052 struct hlua_txn *htxn;
5053 const char *name;
5054 size_t len;
5055 struct sample smp;
5056
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005057 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5058 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005059
5060 /* It is useles to retrieve the stream, but this function
5061 * runs only in a stream context.
5062 */
5063 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5064 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5065
5066 /* Converts the third argument in a sample. */
5067 hlua_lua2smp(L, 3, &smp);
5068
5069 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005070 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005071
5072 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5073 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5074 else
5075 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5076
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005077 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005078}
5079
Christopher Faulet85d79c92016-11-09 16:54:56 +01005080__LJMP static int hlua_unset_var(lua_State *L)
5081{
5082 struct hlua_txn *htxn;
5083 const char *name;
5084 size_t len;
5085 struct sample smp;
5086
5087 MAY_LJMP(check_args(L, 2, "unset_var"));
5088
5089 /* It is useles to retrieve the stream, but this function
5090 * runs only in a stream context.
5091 */
5092 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5093 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5094
5095 /* Unset the variable. */
5096 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005097 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5098 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005099}
5100
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005101__LJMP static int hlua_get_var(lua_State *L)
5102{
5103 struct hlua_txn *htxn;
5104 const char *name;
5105 size_t len;
5106 struct sample smp;
5107
5108 MAY_LJMP(check_args(L, 2, "get_var"));
5109
5110 /* It is useles to retrieve the stream, but this function
5111 * runs only in a stream context.
5112 */
5113 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5114 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5115
Willy Tarreau7560dd42016-03-10 16:28:58 +01005116 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005117 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005118 lua_pushnil(L);
5119 return 1;
5120 }
5121
5122 return hlua_smp2lua(L, &smp);
5123}
5124
Willy Tarreau59551662015-03-10 14:23:13 +01005125__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005126{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005127 struct hlua *hlua;
5128
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005129 MAY_LJMP(check_args(L, 2, "set_priv"));
5130
Willy Tarreau87b09662015-04-03 00:22:06 +02005131 /* It is useles to retrieve the stream, but this function
5132 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005133 */
5134 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005135 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005136
5137 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005138 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005139
5140 /* Get and store new value. */
5141 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5142 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5143
5144 return 0;
5145}
5146
Willy Tarreau59551662015-03-10 14:23:13 +01005147__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005148{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005149 struct hlua *hlua;
5150
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005151 MAY_LJMP(check_args(L, 1, "get_priv"));
5152
Willy Tarreau87b09662015-04-03 00:22:06 +02005153 /* It is useles to retrieve the stream, but this function
5154 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005155 */
5156 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005157 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005158
5159 /* Push configuration index in the stack. */
5160 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5161
5162 return 1;
5163}
5164
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005165/* Create stack entry containing a class TXN. This function
5166 * return 0 if the stack does not contains free slots,
5167 * otherwise it returns 1.
5168 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005169static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005170{
Willy Tarreaude491382015-04-06 11:04:28 +02005171 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005172
5173 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005174 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005175 return 0;
5176
5177 /* NOTE: The allocation never fails. The failure
5178 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005179 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005180 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005181 /* Create the object: obj[0] = userdata. */
5182 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005183 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005184 lua_rawseti(L, -2, 0);
5185
Willy Tarreaude491382015-04-06 11:04:28 +02005186 htxn->s = s;
5187 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005188 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005189 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005190
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005191 /* Create the "f" field that contains a list of fetches. */
5192 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005193 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005194 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005195 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005196
5197 /* Create the "sf" field that contains a list of stringsafe fetches. */
5198 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005199 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005200 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005201 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005202
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005203 /* Create the "c" field that contains a list of converters. */
5204 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005205 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005206 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005207 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005208
5209 /* Create the "sc" field that contains a list of stringsafe converters. */
5210 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005211 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005212 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005213 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005214
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005215 /* Create the "req" field that contains the request channel object. */
5216 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005217 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005218 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005219 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005220
5221 /* Create the "res" field that contains the response channel object. */
5222 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005223 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005224 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005225 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005226
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005227 /* Creates the HTTP object is the current proxy allows http. */
5228 lua_pushstring(L, "http");
5229 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005230 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005231 return 0;
5232 }
5233 else
5234 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005235 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005236
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005237 /* Pop a class sesison metatable and affect it to the userdata. */
5238 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5239 lua_setmetatable(L, -2);
5240
5241 return 1;
5242}
5243
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005244__LJMP static int hlua_txn_deflog(lua_State *L)
5245{
5246 const char *msg;
5247 struct hlua_txn *htxn;
5248
5249 MAY_LJMP(check_args(L, 2, "deflog"));
5250 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5251 msg = MAY_LJMP(luaL_checkstring(L, 2));
5252
5253 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5254 return 0;
5255}
5256
5257__LJMP static int hlua_txn_log(lua_State *L)
5258{
5259 int level;
5260 const char *msg;
5261 struct hlua_txn *htxn;
5262
5263 MAY_LJMP(check_args(L, 3, "log"));
5264 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5265 level = MAY_LJMP(luaL_checkinteger(L, 2));
5266 msg = MAY_LJMP(luaL_checkstring(L, 3));
5267
5268 if (level < 0 || level >= NB_LOG_LEVELS)
5269 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5270
5271 hlua_sendlog(htxn->s->be, level, msg);
5272 return 0;
5273}
5274
5275__LJMP static int hlua_txn_log_debug(lua_State *L)
5276{
5277 const char *msg;
5278 struct hlua_txn *htxn;
5279
5280 MAY_LJMP(check_args(L, 2, "Debug"));
5281 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5282 msg = MAY_LJMP(luaL_checkstring(L, 2));
5283 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5284 return 0;
5285}
5286
5287__LJMP static int hlua_txn_log_info(lua_State *L)
5288{
5289 const char *msg;
5290 struct hlua_txn *htxn;
5291
5292 MAY_LJMP(check_args(L, 2, "Info"));
5293 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5294 msg = MAY_LJMP(luaL_checkstring(L, 2));
5295 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5296 return 0;
5297}
5298
5299__LJMP static int hlua_txn_log_warning(lua_State *L)
5300{
5301 const char *msg;
5302 struct hlua_txn *htxn;
5303
5304 MAY_LJMP(check_args(L, 2, "Warning"));
5305 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5306 msg = MAY_LJMP(luaL_checkstring(L, 2));
5307 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5308 return 0;
5309}
5310
5311__LJMP static int hlua_txn_log_alert(lua_State *L)
5312{
5313 const char *msg;
5314 struct hlua_txn *htxn;
5315
5316 MAY_LJMP(check_args(L, 2, "Alert"));
5317 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5318 msg = MAY_LJMP(luaL_checkstring(L, 2));
5319 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5320 return 0;
5321}
5322
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005323__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5324{
5325 struct hlua_txn *htxn;
5326 int ll;
5327
5328 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5329 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5330 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5331
5332 if (ll < 0 || ll > 7)
5333 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5334
5335 htxn->s->logs.level = ll;
5336 return 0;
5337}
5338
5339__LJMP static int hlua_txn_set_tos(lua_State *L)
5340{
5341 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005342 int tos;
5343
5344 MAY_LJMP(check_args(L, 2, "set_tos"));
5345 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5346 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5347
Willy Tarreau1a18b542018-12-11 16:37:42 +01005348 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005349 return 0;
5350}
5351
5352__LJMP static int hlua_txn_set_mark(lua_State *L)
5353{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005354 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005355 int mark;
5356
5357 MAY_LJMP(check_args(L, 2, "set_mark"));
5358 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5359 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5360
Lukas Tribus579e3e32019-08-11 18:03:45 +02005361 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005362 return 0;
5363}
5364
Patrick Hemmer268a7072018-05-11 12:52:31 -04005365__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5366{
5367 struct hlua_txn *htxn;
5368
5369 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5370 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5371 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5372 return 0;
5373}
5374
5375__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5376{
5377 struct hlua_txn *htxn;
5378
5379 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5380 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5381 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5382 return 0;
5383}
5384
Christopher Faulet700d9e82020-01-31 12:21:52 +01005385/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005386 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005387 * message and terminate the transaction. It returns 1 on success and 0 on
5388 * error. The Reply must be on top of the stack.
5389 */
5390__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5391{
5392 struct htx *htx;
5393 struct htx_sl *sl;
5394 struct h1m h1m;
5395 const char *status, *reason, *body;
5396 size_t status_len, reason_len, body_len;
5397 int ret, code, flags;
5398
5399 code = 200;
5400 status = "200";
5401 status_len = 3;
5402 ret = lua_getfield(L, -1, "status");
5403 if (ret == LUA_TNUMBER) {
5404 code = lua_tointeger(L, -1);
5405 status = lua_tolstring(L, -1, &status_len);
5406 }
5407 lua_pop(L, 1);
5408
5409 reason = http_get_reason(code);
5410 reason_len = strlen(reason);
5411 ret = lua_getfield(L, -1, "reason");
5412 if (ret == LUA_TSTRING)
5413 reason = lua_tolstring(L, -1, &reason_len);
5414 lua_pop(L, 1);
5415
5416 body = NULL;
5417 body_len = 0;
5418 ret = lua_getfield(L, -1, "body");
5419 if (ret == LUA_TSTRING)
5420 body = lua_tolstring(L, -1, &body_len);
5421 lua_pop(L, 1);
5422
5423 /* Prepare the response before inserting the headers */
5424 h1m_init_res(&h1m);
5425 htx = htx_from_buf(&s->res.buf);
5426 channel_htx_truncate(&s->res, htx);
5427 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5428 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5429 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5430 ist2(status, status_len), ist2(reason, reason_len));
5431 }
5432 else {
5433 flags = HTX_SL_F_IS_RESP;
5434 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5435 ist2(status, status_len), ist2(reason, reason_len));
5436 }
5437 if (!sl)
5438 goto fail;
5439 sl->info.res.status = code;
5440
5441 /* Push in the stack the "headers" entry. */
5442 ret = lua_getfield(L, -1, "headers");
5443 if (ret != LUA_TTABLE)
5444 goto skip_headers;
5445
5446 lua_pushnil(L);
5447 while (lua_next(L, -2) != 0) {
5448 struct ist name, value;
5449 const char *n, *v;
5450 size_t nlen, vlen;
5451
5452 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5453 /* Skip element if the key is not a string or if the value is not a table */
5454 goto next_hdr;
5455 }
5456
5457 n = lua_tolstring(L, -2, &nlen);
5458 name = ist2(n, nlen);
5459 if (isteqi(name, ist("content-length"))) {
5460 /* Always skip content-length header. It will be added
5461 * later with the correct len
5462 */
5463 goto next_hdr;
5464 }
5465
5466 /* Loop on header's values */
5467 lua_pushnil(L);
5468 while (lua_next(L, -2)) {
5469 if (!lua_isstring(L, -1)) {
5470 /* Skip the value if it is not a string */
5471 goto next_value;
5472 }
5473
5474 v = lua_tolstring(L, -1, &vlen);
5475 value = ist2(v, vlen);
5476
5477 if (isteqi(name, ist("transfer-encoding")))
5478 h1_parse_xfer_enc_header(&h1m, value);
5479 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5480 goto fail;
5481
5482 next_value:
5483 lua_pop(L, 1);
5484 }
5485
5486 next_hdr:
5487 lua_pop(L, 1);
5488 }
5489 skip_headers:
5490 lua_pop(L, 1);
5491
5492 /* Update h1m flags: CLEN is set if CHNK is not present */
5493 if (!(h1m.flags & H1_MF_CHNK)) {
5494 const char *clen = ultoa(body_len);
5495
5496 h1m.flags |= H1_MF_CLEN;
5497 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5498 goto fail;
5499 }
5500 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5501 h1m.flags |= H1_MF_XFER_LEN;
5502
5503 /* Update HTX start-line flags */
5504 if (h1m.flags & H1_MF_XFER_ENC)
5505 flags |= HTX_SL_F_XFER_ENC;
5506 if (h1m.flags & H1_MF_XFER_LEN) {
5507 flags |= HTX_SL_F_XFER_LEN;
5508 if (h1m.flags & H1_MF_CHNK)
5509 flags |= HTX_SL_F_CHNK;
5510 else if (h1m.flags & H1_MF_CLEN)
5511 flags |= HTX_SL_F_CLEN;
5512 if (h1m.body_len == 0)
5513 flags |= HTX_SL_F_BODYLESS;
5514 }
5515 sl->flags |= flags;
5516
5517
5518 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
5519 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) ||
5520 !htx_add_endof(htx, HTX_BLK_EOM))
5521 goto fail;
5522
5523 /* Now, forward the response and terminate the transaction */
5524 s->txn->status = code;
5525 htx_to_buf(htx, &s->res.buf);
5526 if (!http_forward_proxy_resp(s, 1))
5527 goto fail;
5528
5529 return 1;
5530
5531 fail:
5532 channel_htx_truncate(&s->res, htx);
5533 return 0;
5534}
5535
5536/* Terminate a transaction if called from a lua action. For TCP streams,
5537 * processing is just aborted. Nothing is returned to the client and all
5538 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5539 * is forwarded to the client before terminating the transaction. On success,
5540 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5541 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5542 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005543 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005544__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005545{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005546 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005547 struct stream *s;
5548 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005549
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005550 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005551
Christopher Faulet700d9e82020-01-31 12:21:52 +01005552 /* If the flags NOTERM is set, we cannot terminate the session, so we
5553 * just end the execution of the current lua code. */
5554 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005555 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005556
Christopher Faulet700d9e82020-01-31 12:21:52 +01005557 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005558 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005559 struct channel *req = &s->req;
5560 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005561
Christopher Faulet700d9e82020-01-31 12:21:52 +01005562 channel_auto_read(req);
5563 channel_abort(req);
5564 channel_auto_close(req);
5565 channel_erase(req);
5566
5567 res->wex = tick_add_ifset(now_ms, res->wto);
5568 channel_auto_read(res);
5569 channel_auto_close(res);
5570 channel_shutr_now(res);
5571
5572 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5573 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005574 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005575
Christopher Faulet700d9e82020-01-31 12:21:52 +01005576 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5577 /* No reply or invalid reply */
5578 s->txn->status = 0;
5579 http_reply_and_close(s, 0, NULL);
5580 }
5581 else {
5582 /* Remove extra args to have the reply on top of the stack */
5583 if (lua_gettop(L) > 2)
5584 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005585
Christopher Faulet700d9e82020-01-31 12:21:52 +01005586 if (!hlua_txn_forward_reply(L, s)) {
5587 if (!(s->flags & SF_ERR_MASK))
5588 s->flags |= SF_ERR_PRXCOND;
5589 lua_pushinteger(L, ACT_RET_ERR);
5590 WILL_LJMP(hlua_done(L));
5591 return 0; /* Never reached */
5592 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005593 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005594
Christopher Faulet700d9e82020-01-31 12:21:52 +01005595 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5596 if (htxn->dir == SMP_OPT_DIR_REQ) {
5597 /* let's log the request time */
5598 s->logs.tv_request = now;
5599 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5600 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5601 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005602
Christopher Faulet700d9e82020-01-31 12:21:52 +01005603 done:
5604 if (!(s->flags & SF_ERR_MASK))
5605 s->flags |= SF_ERR_LOCAL;
5606 if (!(s->flags & SF_FINST_MASK))
5607 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005608
Christopher Faulet4ad73102020-03-05 11:07:31 +01005609 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005610 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005611 return 0;
5612}
5613
Christopher Faulet700d9e82020-01-31 12:21:52 +01005614/*
5615 *
5616 *
5617 * Class REPLY
5618 *
5619 *
5620 */
5621
5622/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5623 * free slots, the function fails and returns 0;
5624 */
5625static int hlua_txn_reply_new(lua_State *L)
5626{
5627 struct hlua_txn *htxn;
5628 const char *reason, *body = NULL;
5629 int ret, status;
5630
5631 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005632 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005633 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5634 WILL_LJMP(lua_error(L));
5635 }
5636
5637 /* Default value */
5638 status = 200;
5639 reason = http_get_reason(status);
5640
5641 if (lua_istable(L, 2)) {
5642 /* load status and reason from the table argument at index 2 */
5643 ret = lua_getfield(L, 2, "status");
5644 if (ret == LUA_TNIL)
5645 goto reason;
5646 else if (ret != LUA_TNUMBER) {
5647 /* invalid status: ignore the reason */
5648 goto body;
5649 }
5650 status = lua_tointeger(L, -1);
5651
5652 reason:
5653 lua_pop(L, 1); /* restore the stack: remove status */
5654 ret = lua_getfield(L, 2, "reason");
5655 if (ret == LUA_TSTRING)
5656 reason = lua_tostring(L, -1);
5657
5658 body:
5659 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5660 ret = lua_getfield(L, 2, "body");
5661 if (ret == LUA_TSTRING)
5662 body = lua_tostring(L, -1);
5663 lua_pop(L, 1); /* restore the stack: remove body */
5664 }
5665
5666 /* Create the Reply table */
5667 lua_newtable(L);
5668
5669 /* Add status element */
5670 lua_pushstring(L, "status");
5671 lua_pushinteger(L, status);
5672 lua_settable(L, -3);
5673
5674 /* Add reason element */
5675 reason = http_get_reason(status);
5676 lua_pushstring(L, "reason");
5677 lua_pushstring(L, reason);
5678 lua_settable(L, -3);
5679
5680 /* Add body element, nil if undefined */
5681 lua_pushstring(L, "body");
5682 if (body)
5683 lua_pushstring(L, body);
5684 else
5685 lua_pushnil(L);
5686 lua_settable(L, -3);
5687
5688 /* Add headers element */
5689 lua_pushstring(L, "headers");
5690 lua_newtable(L);
5691
5692 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5693 if (lua_istable(L, 2)) {
5694 /* load headers from the table argument at index 2. If it is a table, copy it. */
5695 ret = lua_getfield(L, 2, "headers");
5696 if (ret == LUA_TTABLE) {
5697 /* stack: [ ... <headers:table>, <table> ] */
5698 lua_pushnil(L);
5699 while (lua_next(L, -2) != 0) {
5700 /* stack: [ ... <headers:table>, <table>, k, v] */
5701 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5702 /* invalid value type, skip it */
5703 lua_pop(L, 1);
5704 continue;
5705 }
5706
5707
5708 /* Duplicate the key and swap it with the value. */
5709 lua_pushvalue(L, -2);
5710 lua_insert(L, -2);
5711 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5712
5713 lua_newtable(L);
5714 lua_insert(L, -2);
5715 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
5716
5717 if (lua_isstring(L, -1)) {
5718 /* push the value in the inner table */
5719 lua_rawseti(L, -2, 1);
5720 }
5721 else { /* table */
5722 lua_pushnil(L);
5723 while (lua_next(L, -2) != 0) {
5724 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
5725 if (!lua_isstring(L, -1)) {
5726 /* invalid value type, skip it*/
5727 lua_pop(L, 1);
5728 continue;
5729 }
5730 /* push the value in the inner table */
5731 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
5732 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
5733 }
5734 lua_pop(L, 1);
5735 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
5736 }
5737
5738 /* push (k,v) on the stack in the headers table:
5739 * stack: [ ... <headers:table>, <table>, k, k, v ]
5740 */
5741 lua_settable(L, -5);
5742 /* stack: [ ... <headers:table>, <table>, k ] */
5743 }
5744 }
5745 lua_pop(L, 1);
5746 }
5747 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5748 lua_settable(L, -3);
5749 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
5750
5751 /* Pop a class sesison metatable and affect it to the userdata. */
5752 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
5753 lua_setmetatable(L, -2);
5754 return 1;
5755}
5756
5757/* Set the reply status code, and optionally the reason. If no reason is
5758 * provided, the default one corresponding to the status code is used.
5759 */
5760__LJMP static int hlua_txn_reply_set_status(lua_State *L)
5761{
5762 int status = MAY_LJMP(luaL_checkinteger(L, 2));
5763 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5764
5765 /* First argument (self) must be a table */
5766 luaL_checktype(L, 1, LUA_TTABLE);
5767
5768 if (status < 100 || status > 599) {
5769 lua_pushboolean(L, 0);
5770 return 1;
5771 }
5772 if (!reason)
5773 reason = http_get_reason(status);
5774
5775 lua_pushinteger(L, status);
5776 lua_setfield(L, 1, "status");
5777
5778 lua_pushstring(L, reason);
5779 lua_setfield(L, 1, "reason");
5780
5781 lua_pushboolean(L, 1);
5782 return 1;
5783}
5784
5785/* Add a header into the reply object. Each header name is associated to an
5786 * array of values in the "headers" table. If the header name is not found, a
5787 * new entry is created.
5788 */
5789__LJMP static int hlua_txn_reply_add_header(lua_State *L)
5790{
5791 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5792 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
5793 int ret;
5794
5795 /* First argument (self) must be a table */
5796 luaL_checktype(L, 1, LUA_TTABLE);
5797
5798 /* Push in the stack the "headers" entry. */
5799 ret = lua_getfield(L, 1, "headers");
5800 if (ret != LUA_TTABLE) {
5801 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
5802 WILL_LJMP(lua_error(L));
5803 }
5804
5805 /* check if the header is already registered. If not, register it. */
5806 ret = lua_getfield(L, -1, name);
5807 if (ret == LUA_TNIL) {
5808 /* Entry not found. */
5809 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
5810
5811 /* Insert the new header name in the array in the top of the stack.
5812 * It left the new array in the top of the stack.
5813 */
5814 lua_newtable(L);
5815 lua_pushstring(L, name);
5816 lua_pushvalue(L, -2);
5817 lua_settable(L, -4);
5818 }
5819 else if (ret != LUA_TTABLE) {
5820 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
5821 WILL_LJMP(lua_error(L));
5822 }
5823
5824 /* Now the top od thestack is an array of values. We push
5825 * the header value as new entry.
5826 */
5827 lua_pushstring(L, value);
5828 ret = lua_rawlen(L, -2);
5829 lua_rawseti(L, -2, ret + 1);
5830
5831 lua_pushboolean(L, 1);
5832 return 1;
5833}
5834
5835/* Remove all occurrences of a given header name. */
5836__LJMP static int hlua_txn_reply_del_header(lua_State *L)
5837{
5838 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
5839 int ret;
5840
5841 /* First argument (self) must be a table */
5842 luaL_checktype(L, 1, LUA_TTABLE);
5843
5844 /* Push in the stack the "headers" entry. */
5845 ret = lua_getfield(L, 1, "headers");
5846 if (ret != LUA_TTABLE) {
5847 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
5848 WILL_LJMP(lua_error(L));
5849 }
5850
5851 lua_pushstring(L, name);
5852 lua_pushnil(L);
5853 lua_settable(L, -3);
5854
5855 lua_pushboolean(L, 1);
5856 return 1;
5857}
5858
5859/* Set the reply's body. Overwrite any existing entry. */
5860__LJMP static int hlua_txn_reply_set_body(lua_State *L)
5861{
5862 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
5863
5864 /* First argument (self) must be a table */
5865 luaL_checktype(L, 1, LUA_TTABLE);
5866
5867 lua_pushstring(L, payload);
5868 lua_setfield(L, 1, "body");
5869
5870 lua_pushboolean(L, 1);
5871 return 1;
5872}
5873
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005874__LJMP static int hlua_log(lua_State *L)
5875{
5876 int level;
5877 const char *msg;
5878
5879 MAY_LJMP(check_args(L, 2, "log"));
5880 level = MAY_LJMP(luaL_checkinteger(L, 1));
5881 msg = MAY_LJMP(luaL_checkstring(L, 2));
5882
5883 if (level < 0 || level >= NB_LOG_LEVELS)
5884 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5885
5886 hlua_sendlog(NULL, level, msg);
5887 return 0;
5888}
5889
5890__LJMP static int hlua_log_debug(lua_State *L)
5891{
5892 const char *msg;
5893
5894 MAY_LJMP(check_args(L, 1, "debug"));
5895 msg = MAY_LJMP(luaL_checkstring(L, 1));
5896 hlua_sendlog(NULL, LOG_DEBUG, msg);
5897 return 0;
5898}
5899
5900__LJMP static int hlua_log_info(lua_State *L)
5901{
5902 const char *msg;
5903
5904 MAY_LJMP(check_args(L, 1, "info"));
5905 msg = MAY_LJMP(luaL_checkstring(L, 1));
5906 hlua_sendlog(NULL, LOG_INFO, msg);
5907 return 0;
5908}
5909
5910__LJMP static int hlua_log_warning(lua_State *L)
5911{
5912 const char *msg;
5913
5914 MAY_LJMP(check_args(L, 1, "warning"));
5915 msg = MAY_LJMP(luaL_checkstring(L, 1));
5916 hlua_sendlog(NULL, LOG_WARNING, msg);
5917 return 0;
5918}
5919
5920__LJMP static int hlua_log_alert(lua_State *L)
5921{
5922 const char *msg;
5923
5924 MAY_LJMP(check_args(L, 1, "alert"));
5925 msg = MAY_LJMP(luaL_checkstring(L, 1));
5926 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005927 return 0;
5928}
5929
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005930__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005931{
5932 int wakeup_ms = lua_tointeger(L, -1);
5933 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02005934 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005935 return 0;
5936}
5937
5938__LJMP static int hlua_sleep(lua_State *L)
5939{
5940 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005941 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005942
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005943 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005944
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005945 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005946 wakeup_ms = tick_add(now_ms, delay);
5947 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005948
Willy Tarreau9635e032018-10-16 17:52:55 +02005949 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005950 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005951}
5952
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005953__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005954{
5955 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005956 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005957
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005958 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005959
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005960 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005961 wakeup_ms = tick_add(now_ms, delay);
5962 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005963
Willy Tarreau9635e032018-10-16 17:52:55 +02005964 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005965 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005966}
5967
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005968/* This functionis an LUA binding. it permits to give back
5969 * the hand at the HAProxy scheduler. It is used when the
5970 * LUA processing consumes a lot of time.
5971 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005972__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005973{
5974 return 0;
5975}
5976
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005977__LJMP static int hlua_yield(lua_State *L)
5978{
Willy Tarreau9635e032018-10-16 17:52:55 +02005979 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005980 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005981}
5982
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005983/* This function change the nice of the currently executed
5984 * task. It is used set low or high priority at the current
5985 * task.
5986 */
Willy Tarreau59551662015-03-10 14:23:13 +01005987__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005988{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005989 struct hlua *hlua;
5990 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005991
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005992 MAY_LJMP(check_args(L, 1, "set_nice"));
5993 hlua = hlua_gethlua(L);
5994 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005995
5996 /* If he task is not set, I'm in a start mode. */
5997 if (!hlua || !hlua->task)
5998 return 0;
5999
6000 if (nice < -1024)
6001 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006002 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006003 nice = 1024;
6004
6005 hlua->task->nice = nice;
6006 return 0;
6007}
6008
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006009/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006010 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6011 * return an E_AGAIN signal, the emmiter of this signal must set a
6012 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006013 *
6014 * Task wrapper are longjmp safe because the only one Lua code
6015 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006016 */
Willy Tarreau60409db2019-08-21 14:14:50 +02006017struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006018{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006019 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006020 enum hlua_exec status;
6021
Christopher Faulet5bc99722018-04-25 10:34:45 +02006022 if (task->thread_mask == MAX_THREADS_MASK)
6023 task_set_affinity(task, tid_bit);
6024
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006025 /* If it is the first call to the task, we must initialize the
6026 * execution timeouts.
6027 */
6028 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006029 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006030
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006031 /* Execute the Lua code. */
6032 status = hlua_ctx_resume(hlua, 1);
6033
6034 switch (status) {
6035 /* finished or yield */
6036 case HLUA_E_OK:
6037 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006038 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006039 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006040 break;
6041
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006042 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006043 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006044 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006045 break;
6046
6047 /* finished with error. */
6048 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006049 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006050 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006051 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006052 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006053 break;
6054
6055 case HLUA_E_ERR:
6056 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006057 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006058 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006059 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006060 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006061 break;
6062 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006063 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006064}
6065
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006066/* This function is an LUA binding that register LUA function to be
6067 * executed after the HAProxy configuration parsing and before the
6068 * HAProxy scheduler starts. This function expect only one LUA
6069 * argument that is a function. This function returns nothing, but
6070 * throws if an error is encountered.
6071 */
6072__LJMP static int hlua_register_init(lua_State *L)
6073{
6074 struct hlua_init_function *init;
6075 int ref;
6076
6077 MAY_LJMP(check_args(L, 1, "register_init"));
6078
6079 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6080
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006081 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006082 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006083 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006084
6085 init->function_ref = ref;
6086 LIST_ADDQ(&hlua_init_functions, &init->l);
6087 return 0;
6088}
6089
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006090/* This functio is an LUA binding. It permits to register a task
6091 * executed in parallel of the main HAroxy activity. The task is
6092 * created and it is set in the HAProxy scheduler. It can be called
6093 * from the "init" section, "post init" or during the runtime.
6094 *
6095 * Lua prototype:
6096 *
6097 * <none> core.register_task(<function>)
6098 */
6099static int hlua_register_task(lua_State *L)
6100{
6101 struct hlua *hlua;
6102 struct task *task;
6103 int ref;
6104
6105 MAY_LJMP(check_args(L, 1, "register_task"));
6106
6107 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6108
Willy Tarreaubafbe012017-11-24 17:34:44 +01006109 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006110 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006111 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006112
Emeric Brunc60def82017-09-27 14:59:38 +02006113 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006114 if (!task)
6115 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6116
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006117 task->context = hlua;
6118 task->process = hlua_process_task;
6119
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006120 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006121 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006122
6123 /* Restore the function in the stack. */
6124 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6125 hlua->nargs = 0;
6126
6127 /* Schedule task. */
6128 task_schedule(task, now_ms);
6129
6130 return 0;
6131}
6132
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006133/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6134 * doesn't allow "yield" functions because the HAProxy engine cannot
6135 * resume converters.
6136 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006137static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006138{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006139 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006140 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006141 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006142
Willy Tarreaube508f12016-03-10 11:47:01 +01006143 if (!stream)
6144 return 0;
6145
Willy Tarreau87b09662015-04-03 00:22:06 +02006146 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006147 * Lua context can be not initialized. This behavior
6148 * permits to save performances because a systematic
6149 * Lua initialization cause 5% performances loss.
6150 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006151 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006152 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006153 if (!stream->hlua) {
6154 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6155 return 0;
6156 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006157 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006158 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6159 return 0;
6160 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006161 }
6162
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006163 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006164 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006165
6166 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006167 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6168 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6169 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006170 else
6171 error = "critical error";
6172 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006173 return 0;
6174 }
6175
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006176 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006177 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006178 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006179 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006180 return 0;
6181 }
6182
6183 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006184 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006185
6186 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006187 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006188 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006189 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006190 return 0;
6191 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006192 hlua_smp2lua(stream->hlua->T, smp);
6193 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006194
6195 /* push keywords in the stack. */
6196 if (arg_p) {
6197 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006198 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006199 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006200 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006201 return 0;
6202 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006203 hlua_arg2lua(stream->hlua->T, arg_p);
6204 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006205 }
6206 }
6207
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006208 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006209 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006210
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006211 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006212 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006213 }
6214
6215 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006216 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006217 /* finished. */
6218 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006219 /* If the stack is empty, the function fails. */
6220 if (lua_gettop(stream->hlua->T) <= 0)
6221 return 0;
6222
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006223 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006224 hlua_lua2smp(stream->hlua->T, -1, smp);
6225 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006226 return 1;
6227
6228 /* yield. */
6229 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006230 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006231 return 0;
6232
6233 /* finished with error. */
6234 case HLUA_E_ERRMSG:
6235 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006236 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006237 fcn->name, lua_tostring(stream->hlua->T, -1));
6238 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006239 return 0;
6240
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006241 case HLUA_E_ETMOUT:
6242 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6243 return 0;
6244
6245 case HLUA_E_NOMEM:
6246 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6247 return 0;
6248
6249 case HLUA_E_YIELD:
6250 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6251 return 0;
6252
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006253 case HLUA_E_ERR:
6254 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006255 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006256
6257 default:
6258 return 0;
6259 }
6260}
6261
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006262/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6263 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006264 * resume sample-fetches. This function will be called by the sample
6265 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006266 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006267static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6268 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006269{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006270 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006271 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006272 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006273 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006274
Willy Tarreaube508f12016-03-10 11:47:01 +01006275 if (!stream)
6276 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006277
Willy Tarreau87b09662015-04-03 00:22:06 +02006278 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006279 * Lua context can be not initialized. This behavior
6280 * permits to save performances because a systematic
6281 * Lua initialization cause 5% performances loss.
6282 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006283 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006284 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006285 if (!stream->hlua) {
6286 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6287 return 0;
6288 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006289 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006290 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6291 return 0;
6292 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006293 }
6294
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006295 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006296 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006297
6298 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006299 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6300 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6301 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006302 else
6303 error = "critical error";
6304 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006305 return 0;
6306 }
6307
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006308 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006309 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006310 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006311 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006312 return 0;
6313 }
6314
6315 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006316 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006317
6318 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006319 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006320 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006321 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006322 return 0;
6323 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006324 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006325
6326 /* push keywords in the stack. */
6327 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6328 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006329 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006330 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006331 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006332 return 0;
6333 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006334 hlua_arg2lua(stream->hlua->T, arg_p);
6335 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006336 }
6337
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006338 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006339 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006340
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006341 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006342 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006343 }
6344
6345 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006346 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006347 /* finished. */
6348 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006349 /* If the stack is empty, the function fails. */
6350 if (lua_gettop(stream->hlua->T) <= 0)
6351 return 0;
6352
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006353 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006354 hlua_lua2smp(stream->hlua->T, -1, smp);
6355 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006356
6357 /* Set the end of execution flag. */
6358 smp->flags &= ~SMP_F_MAY_CHANGE;
6359 return 1;
6360
6361 /* yield. */
6362 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006363 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006364 return 0;
6365
6366 /* finished with error. */
6367 case HLUA_E_ERRMSG:
6368 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006369 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006370 fcn->name, lua_tostring(stream->hlua->T, -1));
6371 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006372 return 0;
6373
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006374 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006375 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6376 return 0;
6377
6378 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006379 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6380 return 0;
6381
6382 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006383 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6384 return 0;
6385
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006386 case HLUA_E_ERR:
6387 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006388 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006389
6390 default:
6391 return 0;
6392 }
6393}
6394
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006395/* This function is an LUA binding used for registering
6396 * "sample-conv" functions. It expects a converter name used
6397 * in the haproxy configuration file, and an LUA function.
6398 */
6399__LJMP static int hlua_register_converters(lua_State *L)
6400{
6401 struct sample_conv_kw_list *sck;
6402 const char *name;
6403 int ref;
6404 int len;
6405 struct hlua_function *fcn;
6406
6407 MAY_LJMP(check_args(L, 2, "register_converters"));
6408
6409 /* First argument : converter name. */
6410 name = MAY_LJMP(luaL_checkstring(L, 1));
6411
6412 /* Second argument : lua function. */
6413 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6414
6415 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006416 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006417 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006418 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006419 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006420 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006421 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006422
6423 /* Fill fcn. */
6424 fcn->name = strdup(name);
6425 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006426 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006427 fcn->function_ref = ref;
6428
6429 /* List head */
6430 sck->list.n = sck->list.p = NULL;
6431
6432 /* converter keyword. */
6433 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006434 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006435 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006436 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006437
6438 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6439 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006440 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 +01006441 sck->kw[0].val_args = NULL;
6442 sck->kw[0].in_type = SMP_T_STR;
6443 sck->kw[0].out_type = SMP_T_STR;
6444 sck->kw[0].private = fcn;
6445
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006446 /* Register this new converter */
6447 sample_register_convs(sck);
6448
6449 return 0;
6450}
6451
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006452/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006453 * "sample-fetch" functions. It expects a converter name used
6454 * in the haproxy configuration file, and an LUA function.
6455 */
6456__LJMP static int hlua_register_fetches(lua_State *L)
6457{
6458 const char *name;
6459 int ref;
6460 int len;
6461 struct sample_fetch_kw_list *sfk;
6462 struct hlua_function *fcn;
6463
6464 MAY_LJMP(check_args(L, 2, "register_fetches"));
6465
6466 /* First argument : sample-fetch name. */
6467 name = MAY_LJMP(luaL_checkstring(L, 1));
6468
6469 /* Second argument : lua function. */
6470 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6471
6472 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006473 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006474 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006475 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006476 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006477 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006478 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006479
6480 /* Fill fcn. */
6481 fcn->name = strdup(name);
6482 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006483 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006484 fcn->function_ref = ref;
6485
6486 /* List head */
6487 sfk->list.n = sfk->list.p = NULL;
6488
6489 /* sample-fetch keyword. */
6490 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006491 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006492 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006493 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006494
6495 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6496 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006497 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 +01006498 sfk->kw[0].val_args = NULL;
6499 sfk->kw[0].out_type = SMP_T_STR;
6500 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6501 sfk->kw[0].val = 0;
6502 sfk->kw[0].private = fcn;
6503
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006504 /* Register this new fetch. */
6505 sample_register_fetches(sfk);
6506
6507 return 0;
6508}
6509
Christopher Faulet501465d2020-02-26 14:54:16 +01006510/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006511 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006512__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006513{
6514 struct hlua *hlua = hlua_gethlua(L);
6515 unsigned int delay;
6516 unsigned int wakeup_ms;
6517
6518 MAY_LJMP(check_args(L, 1, "wake_time"));
6519
6520 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6521 wakeup_ms = tick_add(now_ms, delay);
6522 hlua->wake_time = wakeup_ms;
6523 return 0;
6524}
6525
6526
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006527/* This function is a wrapper to execute each LUA function declared as an action
6528 * wrapper during the initialisation period. This function may return any
6529 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6530 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6531 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006532 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006533static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006534 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006535{
6536 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006537 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006538 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006539 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006540
6541 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006542 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6543 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6544 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6545 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006546 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006547 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006548 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006549 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006550
Willy Tarreau87b09662015-04-03 00:22:06 +02006551 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006552 * Lua context can be not initialized. This behavior
6553 * permits to save performances because a systematic
6554 * Lua initialization cause 5% performances loss.
6555 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006556 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006557 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006558 if (!s->hlua) {
6559 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6560 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006561 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006562 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006563 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006564 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6565 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006566 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006567 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006568 }
6569
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006570 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006571 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006572
6573 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006574 if (!SET_SAFE_LJMP(s->hlua->T)) {
6575 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6576 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006577 else
6578 error = "critical error";
6579 SEND_ERR(px, "Lua function '%s': %s.\n",
6580 rule->arg.hlua_rule->fcn.name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006581 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006582 }
6583
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006584 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006585 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006586 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006587 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006588 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006589 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006590 }
6591
6592 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006593 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006594
Willy Tarreau87b09662015-04-03 00:22:06 +02006595 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006596 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006597 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006598 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006599 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006600 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006601 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006602 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006603
6604 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006605 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006606 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006607 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006608 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006609 RESET_SAFE_LJMP(s->hlua->T);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006610 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006611 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006612 lua_pushstring(s->hlua->T, *arg);
6613 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006614 }
6615
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006616 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006617 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006618
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006619 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006620 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006621 }
6622
Christopher Faulet23308eb2020-06-02 18:46:07 +02006623 /* Always reset the analyse expiration timeout for the corresponding
6624 * channel in case the lua script yield, to be sure to not keep an
6625 * expired timeout.
6626 */
6627 if (dir == SMP_OPT_DIR_REQ)
6628 s->req.analyse_exp = TICK_ETERNITY;
6629 else
6630 s->res.analyse_exp = TICK_ETERNITY;
6631
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006632 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006633 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006634 /* finished. */
6635 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006636 /* Catch the return value */
6637 if (lua_gettop(s->hlua->T) > 0)
6638 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006639
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006640 /* Set timeout in the required channel. */
6641 if (act_ret == ACT_RET_YIELD && s->hlua->wake_time != TICK_ETERNITY) {
6642 if (dir == SMP_OPT_DIR_REQ)
6643 s->req.analyse_exp = s->hlua->wake_time;
6644 else
6645 s->res.analyse_exp = s->hlua->wake_time;
6646 }
6647 goto end;
6648
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006649 /* yield. */
6650 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006651 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006652 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Faulet81921b12019-08-14 23:19:45 +02006653 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006654 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006655 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006656 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006657 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006658 /* Some actions can be wake up when a "write" event
6659 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006660 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006661 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02006662 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006663 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006664 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006665 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006666 act_ret = ACT_RET_YIELD;
6667 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006668
6669 /* finished with error. */
6670 case HLUA_E_ERRMSG:
6671 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006672 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006673 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6674 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006675 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006676
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006677 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006678 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006679 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006680
6681 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006682 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006683 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006684
6685 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006686 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6687 rule->arg.hlua_rule->fcn.name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006688 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006689
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006690 case HLUA_E_ERR:
6691 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006692 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006693 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006694
6695 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006696 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006697 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006698
6699 end:
6700 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006701}
6702
Olivier Houchard9f6af332018-05-25 14:04:04 +02006703struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006704{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006705 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006706
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006707 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006708 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006709 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006710}
6711
6712static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6713{
6714 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006715 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006716 struct task *task;
6717 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006718 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006719
Willy Tarreaubafbe012017-11-24 17:34:44 +01006720 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006721 if (!hlua) {
6722 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6723 ctx->rule->arg.hlua_rule->fcn.name);
6724 return 0;
6725 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006726 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006727 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006728 ctx->ctx.hlua_apptcp.flags = 0;
6729
6730 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006731 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006732 if (!task) {
6733 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6734 ctx->rule->arg.hlua_rule->fcn.name);
6735 return 0;
6736 }
6737 task->nice = 0;
6738 task->context = ctx;
6739 task->process = hlua_applet_wakeup;
6740 ctx->ctx.hlua_apptcp.task = task;
6741
6742 /* In the execution wrappers linked with a stream, the
6743 * Lua context can be not initialized. This behavior
6744 * permits to save performances because a systematic
6745 * Lua initialization cause 5% performances loss.
6746 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006747 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006748 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6749 ctx->rule->arg.hlua_rule->fcn.name);
6750 return 0;
6751 }
6752
6753 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006754 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006755
6756 /* The following Lua calls can fail. */
6757 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006758 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6759 error = lua_tostring(hlua->T, -1);
6760 else
6761 error = "critical error";
6762 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6763 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006764 return 0;
6765 }
6766
6767 /* Check stack available size. */
6768 if (!lua_checkstack(hlua->T, 1)) {
6769 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6770 ctx->rule->arg.hlua_rule->fcn.name);
6771 RESET_SAFE_LJMP(hlua->T);
6772 return 0;
6773 }
6774
6775 /* Restore the function in the stack. */
6776 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6777
6778 /* Create and and push object stream in the stack. */
6779 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6780 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6781 ctx->rule->arg.hlua_rule->fcn.name);
6782 RESET_SAFE_LJMP(hlua->T);
6783 return 0;
6784 }
6785 hlua->nargs = 1;
6786
6787 /* push keywords in the stack. */
6788 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6789 if (!lua_checkstack(hlua->T, 1)) {
6790 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6791 ctx->rule->arg.hlua_rule->fcn.name);
6792 RESET_SAFE_LJMP(hlua->T);
6793 return 0;
6794 }
6795 lua_pushstring(hlua->T, *arg);
6796 hlua->nargs++;
6797 }
6798
6799 RESET_SAFE_LJMP(hlua->T);
6800
6801 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006802 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01006803 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006804
6805 return 1;
6806}
6807
Willy Tarreau60409db2019-08-21 14:14:50 +02006808void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006809{
6810 struct stream_interface *si = ctx->owner;
6811 struct stream *strm = si_strm(si);
6812 struct channel *res = si_ic(si);
6813 struct act_rule *rule = ctx->rule;
6814 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006815 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006816
6817 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02006818 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
6819 /* eat the whole request */
6820 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006821 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02006822 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006823
6824 /* If the stream is disconnect or closed, ldo nothing. */
6825 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6826 return;
6827
6828 /* Execute the function. */
6829 switch (hlua_ctx_resume(hlua, 1)) {
6830 /* finished. */
6831 case HLUA_E_OK:
6832 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6833
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006834 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02006835 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006836 res->flags |= CF_READ_NULL;
6837 si_shutr(si);
6838 return;
6839
6840 /* yield. */
6841 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006842 if (hlua->wake_time != TICK_ETERNITY)
6843 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006844 return;
6845
6846 /* finished with error. */
6847 case HLUA_E_ERRMSG:
6848 /* Display log. */
6849 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6850 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6851 lua_pop(hlua->T, 1);
6852 goto error;
6853
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006854 case HLUA_E_ETMOUT:
6855 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6856 rule->arg.hlua_rule->fcn.name);
6857 goto error;
6858
6859 case HLUA_E_NOMEM:
6860 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6861 rule->arg.hlua_rule->fcn.name);
6862 goto error;
6863
6864 case HLUA_E_YIELD: /* unexpected */
6865 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6866 rule->arg.hlua_rule->fcn.name);
6867 goto error;
6868
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006869 case HLUA_E_ERR:
6870 /* Display log. */
6871 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6872 rule->arg.hlua_rule->fcn.name);
6873 goto error;
6874
6875 default:
6876 goto error;
6877 }
6878
6879error:
6880
6881 /* For all other cases, just close the stream. */
6882 si_shutw(si);
6883 si_shutr(si);
6884 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6885}
6886
6887static void hlua_applet_tcp_release(struct appctx *ctx)
6888{
Olivier Houchard3f795f72019-04-17 22:51:06 +02006889 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006890 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006891 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006892 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006893}
6894
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006895/* The function returns 1 if the initialisation is complete, 0 if
6896 * an errors occurs and -1 if more data are required for initializing
6897 * the applet.
6898 */
6899static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6900{
6901 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006902 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006903 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006904 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006905 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006906 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006907
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006908 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006909 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006910 if (!hlua) {
6911 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6912 ctx->rule->arg.hlua_rule->fcn.name);
6913 return 0;
6914 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006915 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006916 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006917 ctx->ctx.hlua_apphttp.left_bytes = -1;
6918 ctx->ctx.hlua_apphttp.flags = 0;
6919
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006920 if (txn->req.flags & HTTP_MSGF_VER_11)
6921 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6922
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006923 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006924 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006925 if (!task) {
6926 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6927 ctx->rule->arg.hlua_rule->fcn.name);
6928 return 0;
6929 }
6930 task->nice = 0;
6931 task->context = ctx;
6932 task->process = hlua_applet_wakeup;
6933 ctx->ctx.hlua_apphttp.task = task;
6934
6935 /* In the execution wrappers linked with a stream, the
6936 * Lua context can be not initialized. This behavior
6937 * permits to save performances because a systematic
6938 * Lua initialization cause 5% performances loss.
6939 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006940 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006941 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6942 ctx->rule->arg.hlua_rule->fcn.name);
6943 return 0;
6944 }
6945
6946 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006947 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006948
6949 /* The following Lua calls can fail. */
6950 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006951 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6952 error = lua_tostring(hlua->T, -1);
6953 else
6954 error = "critical error";
6955 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6956 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006957 return 0;
6958 }
6959
6960 /* Check stack available size. */
6961 if (!lua_checkstack(hlua->T, 1)) {
6962 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6963 ctx->rule->arg.hlua_rule->fcn.name);
6964 RESET_SAFE_LJMP(hlua->T);
6965 return 0;
6966 }
6967
6968 /* Restore the function in the stack. */
6969 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6970
6971 /* Create and and push object stream in the stack. */
6972 if (!hlua_applet_http_new(hlua->T, ctx)) {
6973 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6974 ctx->rule->arg.hlua_rule->fcn.name);
6975 RESET_SAFE_LJMP(hlua->T);
6976 return 0;
6977 }
6978 hlua->nargs = 1;
6979
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006980 /* push keywords in the stack. */
6981 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6982 if (!lua_checkstack(hlua->T, 1)) {
6983 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6984 ctx->rule->arg.hlua_rule->fcn.name);
6985 RESET_SAFE_LJMP(hlua->T);
6986 return 0;
6987 }
6988 lua_pushstring(hlua->T, *arg);
6989 hlua->nargs++;
6990 }
6991
6992 RESET_SAFE_LJMP(hlua->T);
6993
6994 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01006995 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006996
6997 return 1;
6998}
6999
Willy Tarreau60409db2019-08-21 14:14:50 +02007000void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007001{
7002 struct stream_interface *si = ctx->owner;
7003 struct stream *strm = si_strm(si);
7004 struct channel *req = si_oc(si);
7005 struct channel *res = si_ic(si);
7006 struct act_rule *rule = ctx->rule;
7007 struct proxy *px = strm->be;
7008 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7009 struct htx *req_htx, *res_htx;
7010
7011 res_htx = htx_from_buf(&res->buf);
7012
7013 /* If the stream is disconnect or closed, ldo nothing. */
7014 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7015 goto out;
7016
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007017 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007018 if (!b_size(&res->buf)) {
7019 si_rx_room_blk(si);
7020 goto out;
7021 }
7022 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007023 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007024 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7025
7026 /* Set the currently running flag. */
7027 if (!HLUA_IS_RUNNING(hlua) &&
7028 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7029 struct htx_blk *blk;
7030 size_t count = co_data(req);
7031
7032 if (!count) {
7033 si_cant_get(si);
7034 goto out;
7035 }
7036
7037 /* We need to flush the request header. This left the body for
7038 * the Lua.
7039 */
7040 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007041 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007042 while (count && blk) {
7043 enum htx_blk_type type = htx_get_blk_type(blk);
7044 uint32_t sz = htx_get_blksz(blk);
7045
7046 if (sz > count) {
7047 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007048 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007049 goto out;
7050 }
7051
7052 count -= sz;
7053 co_set_data(req, co_data(req) - sz);
7054 blk = htx_remove_blk(req_htx, blk);
7055
7056 if (type == HTX_BLK_EOH)
7057 break;
7058 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007059 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007060 }
7061
7062 /* Executes The applet if it is not done. */
7063 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7064
7065 /* Execute the function. */
7066 switch (hlua_ctx_resume(hlua, 1)) {
7067 /* finished. */
7068 case HLUA_E_OK:
7069 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7070 break;
7071
7072 /* yield. */
7073 case HLUA_E_AGAIN:
7074 if (hlua->wake_time != TICK_ETERNITY)
7075 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007076 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007077
7078 /* finished with error. */
7079 case HLUA_E_ERRMSG:
7080 /* Display log. */
7081 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7082 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7083 lua_pop(hlua->T, 1);
7084 goto error;
7085
7086 case HLUA_E_ETMOUT:
7087 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7088 rule->arg.hlua_rule->fcn.name);
7089 goto error;
7090
7091 case HLUA_E_NOMEM:
7092 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7093 rule->arg.hlua_rule->fcn.name);
7094 goto error;
7095
7096 case HLUA_E_YIELD: /* unexpected */
7097 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7098 rule->arg.hlua_rule->fcn.name);
7099 goto error;
7100
7101 case HLUA_E_ERR:
7102 /* Display log. */
7103 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7104 rule->arg.hlua_rule->fcn.name);
7105 goto error;
7106
7107 default:
7108 goto error;
7109 }
7110 }
7111
7112 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007113 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7114 goto done;
7115
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007116 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7117 goto error;
7118
Christopher Faulet54b5e212019-06-04 10:08:28 +02007119 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007120 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7121 si_rx_room_blk(si);
7122 goto out;
7123 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007124 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007125 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7126 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007127 }
7128
7129 done:
7130 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007131 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007132 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007133 si_shutr(si);
7134 }
7135
7136 /* eat the whole request */
7137 if (co_data(req)) {
7138 req_htx = htx_from_buf(&req->buf);
7139 co_htx_skip(req, req_htx, co_data(req));
7140 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007141 }
7142 }
7143
7144 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007145 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007146 return;
7147
7148 error:
7149
7150 /* If we are in HTTP mode, and we are not send any
7151 * data, return a 500 server error in best effort:
7152 * if there is no room available in the buffer,
7153 * just close the connection.
7154 */
7155 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007156 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007157
7158 channel_erase(res);
7159 res->buf.data = b_data(err);
7160 memcpy(res->buf.area, b_head(err), b_data(err));
7161 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007162 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007163 }
7164 if (!(strm->flags & SF_ERR_MASK))
7165 strm->flags |= SF_ERR_RESOURCE;
7166 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7167 goto done;
7168}
7169
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007170static void hlua_applet_http_release(struct appctx *ctx)
7171{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007172 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007173 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007174 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007175 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007176}
7177
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007178/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007179 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007180 *
7181 * This function can fail with an abort() due to an Lua critical error.
7182 * We are in the configuration parsing process of HAProxy, this abort() is
7183 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007184 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007185static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7186 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007187{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007188 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007189 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007190
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007191 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007192 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007193 if (!rule->arg.hlua_rule) {
7194 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007195 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007196 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007197
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007198 /* Memory for arguments. */
7199 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7200 if (!rule->arg.hlua_rule->args) {
7201 memprintf(err, "out of memory error");
7202 return ACT_RET_PRS_ERR;
7203 }
7204
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007205 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007206 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007207
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007208 /* Expect some arguments */
7209 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007210 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007211 memprintf(err, "expect %d arguments", fcn->nargs);
7212 return ACT_RET_PRS_ERR;
7213 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007214 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007215 if (!rule->arg.hlua_rule->args[i]) {
7216 memprintf(err, "out of memory error");
7217 return ACT_RET_PRS_ERR;
7218 }
7219 (*cur_arg)++;
7220 }
7221 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007222
Thierry FOURNIER42148732015-09-02 17:17:33 +02007223 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007224 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007225 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007226}
7227
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007228static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7229 struct act_rule *rule, char **err)
7230{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007231 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007232
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007233 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007234 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007235 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007236 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007237 * the call of this analyzer.
7238 */
7239 if (rule->from != ACT_F_HTTP_REQ) {
7240 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7241 return ACT_RET_PRS_ERR;
7242 }
7243
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007244 /* Memory for the rule. */
7245 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7246 if (!rule->arg.hlua_rule) {
7247 memprintf(err, "out of memory error");
7248 return ACT_RET_PRS_ERR;
7249 }
7250
7251 /* Reference the Lua function and store the reference. */
7252 rule->arg.hlua_rule->fcn = *fcn;
7253
7254 /* TODO: later accept arguments. */
7255 rule->arg.hlua_rule->args = NULL;
7256
7257 /* Add applet pointer in the rule. */
7258 rule->applet.obj_type = OBJ_TYPE_APPLET;
7259 rule->applet.name = fcn->name;
7260 rule->applet.init = hlua_applet_http_init;
7261 rule->applet.fct = hlua_applet_http_fct;
7262 rule->applet.release = hlua_applet_http_release;
7263 rule->applet.timeout = hlua_timeout_applet;
7264
7265 return ACT_RET_PRS_OK;
7266}
7267
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007268/* This function is an LUA binding used for registering
7269 * "sample-conv" functions. It expects a converter name used
7270 * in the haproxy configuration file, and an LUA function.
7271 */
7272__LJMP static int hlua_register_action(lua_State *L)
7273{
7274 struct action_kw_list *akl;
7275 const char *name;
7276 int ref;
7277 int len;
7278 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007279 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007280
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007281 /* Initialise the number of expected arguments at 0. */
7282 nargs = 0;
7283
7284 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7285 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007286
7287 /* First argument : converter name. */
7288 name = MAY_LJMP(luaL_checkstring(L, 1));
7289
7290 /* Second argument : environment. */
7291 if (lua_type(L, 2) != LUA_TTABLE)
7292 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7293
7294 /* Third argument : lua function. */
7295 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7296
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007297 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007298 if (lua_gettop(L) >= 4)
7299 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7300
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007301 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007302 lua_pushnil(L);
7303 while (lua_next(L, 2) != 0) {
7304 if (lua_type(L, -1) != LUA_TSTRING)
7305 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7306
7307 /* Check required environment. Only accepted "http" or "tcp". */
7308 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007309 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007310 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007311 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007312 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007313 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007314 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007315
7316 /* Fill fcn. */
7317 fcn->name = strdup(name);
7318 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007319 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007320 fcn->function_ref = ref;
7321
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007322 /* Set the expected number od arguments. */
7323 fcn->nargs = nargs;
7324
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007325 /* List head */
7326 akl->list.n = akl->list.p = NULL;
7327
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007328 /* action keyword. */
7329 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007330 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007331 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007332 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007333
7334 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7335
7336 akl->kw[0].match_pfx = 0;
7337 akl->kw[0].private = fcn;
7338 akl->kw[0].parse = action_register_lua;
7339
7340 /* select the action registering point. */
7341 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7342 tcp_req_cont_keywords_register(akl);
7343 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7344 tcp_res_cont_keywords_register(akl);
7345 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7346 http_req_keywords_register(akl);
7347 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7348 http_res_keywords_register(akl);
7349 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007350 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007351 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7352 "are expected.", lua_tostring(L, -1)));
7353
7354 /* pop the environment string. */
7355 lua_pop(L, 1);
7356 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007357 return ACT_RET_PRS_OK;
7358}
7359
7360static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7361 struct act_rule *rule, char **err)
7362{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007363 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007364
Christopher Faulet280f85b2019-07-15 15:02:04 +02007365 if (px->mode == PR_MODE_HTTP) {
7366 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007367 return ACT_RET_PRS_ERR;
7368 }
7369
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007370 /* Memory for the rule. */
7371 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7372 if (!rule->arg.hlua_rule) {
7373 memprintf(err, "out of memory error");
7374 return ACT_RET_PRS_ERR;
7375 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007376
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007377 /* Reference the Lua function and store the reference. */
7378 rule->arg.hlua_rule->fcn = *fcn;
7379
7380 /* TODO: later accept arguments. */
7381 rule->arg.hlua_rule->args = NULL;
7382
7383 /* Add applet pointer in the rule. */
7384 rule->applet.obj_type = OBJ_TYPE_APPLET;
7385 rule->applet.name = fcn->name;
7386 rule->applet.init = hlua_applet_tcp_init;
7387 rule->applet.fct = hlua_applet_tcp_fct;
7388 rule->applet.release = hlua_applet_tcp_release;
7389 rule->applet.timeout = hlua_timeout_applet;
7390
7391 return 0;
7392}
7393
7394/* This function is an LUA binding used for registering
7395 * "sample-conv" functions. It expects a converter name used
7396 * in the haproxy configuration file, and an LUA function.
7397 */
7398__LJMP static int hlua_register_service(lua_State *L)
7399{
7400 struct action_kw_list *akl;
7401 const char *name;
7402 const char *env;
7403 int ref;
7404 int len;
7405 struct hlua_function *fcn;
7406
7407 MAY_LJMP(check_args(L, 3, "register_service"));
7408
7409 /* First argument : converter name. */
7410 name = MAY_LJMP(luaL_checkstring(L, 1));
7411
7412 /* Second argument : environment. */
7413 env = MAY_LJMP(luaL_checkstring(L, 2));
7414
7415 /* Third argument : lua function. */
7416 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7417
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007418 /* Allocate and fill the sample fetch keyword struct. */
7419 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7420 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007421 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007422 fcn = calloc(1, sizeof(*fcn));
7423 if (!fcn)
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
7426 /* Fill fcn. */
7427 len = strlen("<lua.>") + strlen(name) + 1;
7428 fcn->name = calloc(1, len);
7429 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007430 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007431 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7432 fcn->function_ref = ref;
7433
7434 /* List head */
7435 akl->list.n = akl->list.p = NULL;
7436
7437 /* converter keyword. */
7438 len = strlen("lua.") + strlen(name) + 1;
7439 akl->kw[0].kw = calloc(1, len);
7440 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007441 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007442
7443 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7444
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007445 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007446 if (strcmp(env, "tcp") == 0)
7447 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007448 else if (strcmp(env, "http") == 0)
7449 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007450 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007451 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007452 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007453
7454 akl->kw[0].match_pfx = 0;
7455 akl->kw[0].private = fcn;
7456
7457 /* End of array. */
7458 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7459
7460 /* Register this new converter */
7461 service_keywords_register(akl);
7462
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007463 return 0;
7464}
7465
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007466/* This function initialises Lua cli handler. It copies the
7467 * arguments in the Lua stack and create channel IO objects.
7468 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007469static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007470{
7471 struct hlua *hlua;
7472 struct hlua_function *fcn;
7473 int i;
7474 const char *error;
7475
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007476 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007477 appctx->ctx.hlua_cli.fcn = private;
7478
Willy Tarreaubafbe012017-11-24 17:34:44 +01007479 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007480 if (!hlua) {
7481 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007482 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007483 }
7484 HLUA_INIT(hlua);
7485 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007486
7487 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007488 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007489 * applet_http. It is absolutely compatible.
7490 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007491 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007492 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007493 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007494 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007495 }
7496 appctx->ctx.hlua_cli.task->nice = 0;
7497 appctx->ctx.hlua_cli.task->context = appctx;
7498 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7499
7500 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007501 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007502 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007503 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007504 }
7505
7506 /* The following Lua calls can fail. */
7507 if (!SET_SAFE_LJMP(hlua->T)) {
7508 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7509 error = lua_tostring(hlua->T, -1);
7510 else
7511 error = "critical error";
7512 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7513 goto error;
7514 }
7515
7516 /* Check stack available size. */
7517 if (!lua_checkstack(hlua->T, 2)) {
7518 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7519 goto error;
7520 }
7521
7522 /* Restore the function in the stack. */
7523 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7524
7525 /* Once the arguments parsed, the CLI is like an AppletTCP,
7526 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007527 */
7528 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7529 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7530 goto error;
7531 }
7532 hlua->nargs = 1;
7533
7534 /* push keywords in the stack. */
7535 for (i = 0; *args[i]; i++) {
7536 /* Check stack available size. */
7537 if (!lua_checkstack(hlua->T, 1)) {
7538 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7539 goto error;
7540 }
7541 lua_pushstring(hlua->T, args[i]);
7542 hlua->nargs++;
7543 }
7544
7545 /* We must initialize the execution timeouts. */
7546 hlua->max_time = hlua_timeout_session;
7547
7548 /* At this point the execution is safe. */
7549 RESET_SAFE_LJMP(hlua->T);
7550
7551 /* It's ok */
7552 return 0;
7553
7554 /* It's not ok. */
7555error:
7556 RESET_SAFE_LJMP(hlua->T);
7557 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007558 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007559 return 1;
7560}
7561
7562static int hlua_cli_io_handler_fct(struct appctx *appctx)
7563{
7564 struct hlua *hlua;
7565 struct stream_interface *si;
7566 struct hlua_function *fcn;
7567
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007568 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007569 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007570 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007571
7572 /* If the stream is disconnect or closed, ldo nothing. */
7573 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7574 return 1;
7575
7576 /* Execute the function. */
7577 switch (hlua_ctx_resume(hlua, 1)) {
7578
7579 /* finished. */
7580 case HLUA_E_OK:
7581 return 1;
7582
7583 /* yield. */
7584 case HLUA_E_AGAIN:
7585 /* We want write. */
7586 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007587 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007588 /* Set the timeout. */
7589 if (hlua->wake_time != TICK_ETERNITY)
7590 task_schedule(hlua->task, hlua->wake_time);
7591 return 0;
7592
7593 /* finished with error. */
7594 case HLUA_E_ERRMSG:
7595 /* Display log. */
7596 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7597 fcn->name, lua_tostring(hlua->T, -1));
7598 lua_pop(hlua->T, 1);
7599 return 1;
7600
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007601 case HLUA_E_ETMOUT:
7602 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7603 fcn->name);
7604 return 1;
7605
7606 case HLUA_E_NOMEM:
7607 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7608 fcn->name);
7609 return 1;
7610
7611 case HLUA_E_YIELD: /* unexpected */
7612 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7613 fcn->name);
7614 return 1;
7615
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007616 case HLUA_E_ERR:
7617 /* Display log. */
7618 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7619 fcn->name);
7620 return 1;
7621
7622 default:
7623 return 1;
7624 }
7625
7626 return 1;
7627}
7628
7629static void hlua_cli_io_release_fct(struct appctx *appctx)
7630{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007631 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007632 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007633}
7634
7635/* This function is an LUA binding used for registering
7636 * new keywords in the cli. It expects a list of keywords
7637 * which are the "path". It is limited to 5 keywords. A
7638 * description of the command, a function to be executed
7639 * for the parsing and a function for io handlers.
7640 */
7641__LJMP static int hlua_register_cli(lua_State *L)
7642{
7643 struct cli_kw_list *cli_kws;
7644 const char *message;
7645 int ref_io;
7646 int len;
7647 struct hlua_function *fcn;
7648 int index;
7649 int i;
7650
7651 MAY_LJMP(check_args(L, 3, "register_cli"));
7652
7653 /* First argument : an array of maximum 5 keywords. */
7654 if (!lua_istable(L, 1))
7655 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7656
7657 /* Second argument : string with contextual message. */
7658 message = MAY_LJMP(luaL_checkstring(L, 2));
7659
7660 /* Third and fourth argument : lua function. */
7661 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7662
7663 /* Allocate and fill the sample fetch keyword struct. */
7664 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7665 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007666 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007667 fcn = calloc(1, sizeof(*fcn));
7668 if (!fcn)
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
7671 /* Fill path. */
7672 index = 0;
7673 lua_pushnil(L);
7674 while(lua_next(L, 1) != 0) {
7675 if (index >= 5)
7676 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7677 if (lua_type(L, -1) != LUA_TSTRING)
7678 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7679 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7680 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007681 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007682 index++;
7683 lua_pop(L, 1);
7684 }
7685
7686 /* Copy help message. */
7687 cli_kws->kw[0].usage = strdup(message);
7688 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007689 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007690
7691 /* Fill fcn io handler. */
7692 len = strlen("<lua.cli>") + 1;
7693 for (i = 0; i < index; i++)
7694 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7695 fcn->name = calloc(1, len);
7696 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007697 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007698 strncat((char *)fcn->name, "<lua.cli", len);
7699 for (i = 0; i < index; i++) {
7700 strncat((char *)fcn->name, ".", len);
7701 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7702 }
7703 strncat((char *)fcn->name, ">", len);
7704 fcn->function_ref = ref_io;
7705
7706 /* Fill last entries. */
7707 cli_kws->kw[0].private = fcn;
7708 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7709 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7710 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7711
7712 /* Register this new converter */
7713 cli_register_kw(cli_kws);
7714
7715 return 0;
7716}
7717
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007718static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7719 struct proxy *defpx, const char *file, int line,
7720 char **err, unsigned int *timeout)
7721{
7722 const char *error;
7723
7724 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02007725 if (error == PARSE_TIME_OVER) {
7726 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
7727 args[1], args[0]);
7728 return -1;
7729 }
7730 else if (error == PARSE_TIME_UNDER) {
7731 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
7732 args[1], args[0]);
7733 return -1;
7734 }
7735 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007736 memprintf(err, "%s: invalid timeout", args[0]);
7737 return -1;
7738 }
7739 return 0;
7740}
7741
7742static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7743 struct proxy *defpx, const char *file, int line,
7744 char **err)
7745{
7746 return hlua_read_timeout(args, section_type, curpx, defpx,
7747 file, line, err, &hlua_timeout_session);
7748}
7749
7750static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7751 struct proxy *defpx, const char *file, int line,
7752 char **err)
7753{
7754 return hlua_read_timeout(args, section_type, curpx, defpx,
7755 file, line, err, &hlua_timeout_task);
7756}
7757
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007758static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7759 struct proxy *defpx, const char *file, int line,
7760 char **err)
7761{
7762 return hlua_read_timeout(args, section_type, curpx, defpx,
7763 file, line, err, &hlua_timeout_applet);
7764}
7765
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007766static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7767 struct proxy *defpx, const char *file, int line,
7768 char **err)
7769{
7770 char *error;
7771
7772 hlua_nb_instruction = strtoll(args[1], &error, 10);
7773 if (*error != '\0') {
7774 memprintf(err, "%s: invalid number", args[0]);
7775 return -1;
7776 }
7777 return 0;
7778}
7779
Willy Tarreau32f61e22015-03-18 17:54:59 +01007780static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7781 struct proxy *defpx, const char *file, int line,
7782 char **err)
7783{
7784 char *error;
7785
7786 if (*(args[1]) == 0) {
7787 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7788 return -1;
7789 }
7790 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7791 if (*error != '\0') {
7792 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7793 return -1;
7794 }
7795 return 0;
7796}
7797
7798
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007799/* This function is called by the main configuration key "lua-load". It loads and
7800 * execute an lua file during the parsing of the HAProxy configuration file. It is
7801 * the main lua entry point.
7802 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007803 * This function runs with the HAProxy keywords API. It returns -1 if an error
7804 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007805 *
7806 * In some error case, LUA set an error message in top of the stack. This function
7807 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007808 *
7809 * This function can fail with an abort() due to an Lua critical error.
7810 * We are in the configuration parsing process of HAProxy, this abort() is
7811 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007812 */
7813static int hlua_load(char **args, int section_type, struct proxy *curpx,
7814 struct proxy *defpx, const char *file, int line,
7815 char **err)
7816{
7817 int error;
7818
7819 /* Just load and compile the file. */
7820 error = luaL_loadfile(gL.T, args[1]);
7821 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007822 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007823 lua_pop(gL.T, 1);
7824 return -1;
7825 }
7826
7827 /* If no syntax error where detected, execute the code. */
7828 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7829 switch (error) {
7830 case LUA_OK:
7831 break;
7832 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007833 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007834 lua_pop(gL.T, 1);
7835 return -1;
7836 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007837 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007838 return -1;
7839 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007840 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007841 lua_pop(gL.T, 1);
7842 return -1;
7843 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007844 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007845 lua_pop(gL.T, 1);
7846 return -1;
7847 default:
Ilya Shipitsind4259502020-04-08 01:07:56 +05007848 memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007849 lua_pop(gL.T, 1);
7850 return -1;
7851 }
7852
7853 return 0;
7854}
7855
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01007856/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
7857 * in the given <ctx>.
7858 */
7859static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
7860{
7861 lua_getglobal(ctx.T, "package"); /* push package variable */
7862 lua_pushstring(ctx.T, path); /* push given path */
7863 lua_pushstring(ctx.T, ";"); /* push semicolon */
7864 lua_getfield(ctx.T, -3, type); /* push old path */
7865 lua_concat(ctx.T, 3); /* concatenate to new path */
7866 lua_setfield(ctx.T, -2, type); /* store new path */
7867 lua_pop(ctx.T, 1); /* pop package variable */
7868
7869 return 0;
7870}
7871
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007872static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
7873 struct proxy *defpx, const char *file, int line,
7874 char **err)
7875{
7876 char *path;
7877 char *type = "path";
7878 if (too_many_args(2, args, err, NULL)) {
7879 return -1;
7880 }
7881
7882 if (!(*args[1])) {
7883 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
7884 return -1;
7885 }
7886 path = args[1];
7887
7888 if (*args[2]) {
7889 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
7890 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
7891 return -1;
7892 }
7893 type = args[2];
7894 }
7895
7896 return hlua_prepend_path(gL, type, path);
7897}
7898
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007899/* configuration keywords declaration */
7900static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01007901 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007902 { CFG_GLOBAL, "lua-load", hlua_load },
7903 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7904 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007905 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007906 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007907 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007908 { 0, NULL, NULL },
7909}};
7910
Willy Tarreau0108d902018-11-25 19:14:37 +01007911INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
7912
Christopher Fauletafd8f102018-11-08 11:34:21 +01007913
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007914/* This function can fail with an abort() due to an Lua critical error.
7915 * We are in the initialisation process of HAProxy, this abort() is
7916 * tolerated.
7917 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007918int hlua_post_init()
7919{
7920 struct hlua_init_function *init;
7921 const char *msg;
7922 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007923 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007924
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007925 /* Call post initialisation function in safe environment. */
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007926 if (!SET_SAFE_LJMP(gL.T)) {
7927 if (lua_type(gL.T, -1) == LUA_TSTRING)
7928 error = lua_tostring(gL.T, -1);
7929 else
7930 error = "critical error";
7931 fprintf(stderr, "Lua post-init: %s.\n", error);
7932 exit(1);
7933 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02007934
7935#if USE_OPENSSL
7936 /* Initialize SSL server. */
7937 if (socket_ssl.xprt->prepare_srv) {
7938 int saved_used_backed = global.ssl_used_backend;
7939 // don't affect maxconn automatic computation
7940 socket_ssl.xprt->prepare_srv(&socket_ssl);
7941 global.ssl_used_backend = saved_used_backed;
7942 }
7943#endif
7944
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007945 hlua_fcn_post_init(gL.T);
7946 RESET_SAFE_LJMP(gL.T);
7947
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007948 list_for_each_entry(init, &hlua_init_functions, l) {
7949 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7950 ret = hlua_ctx_resume(&gL, 0);
7951 switch (ret) {
7952 case HLUA_E_OK:
7953 lua_pop(gL.T, -1);
7954 return 1;
7955 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007956 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007957 return 0;
7958 case HLUA_E_ERRMSG:
7959 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007960 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007961 return 0;
7962 case HLUA_E_ERR:
7963 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007964 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007965 return 0;
7966 }
7967 }
7968 return 1;
7969}
7970
Willy Tarreau32f61e22015-03-18 17:54:59 +01007971/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7972 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7973 * is the previously allocated size or the kind of object in case of a new
7974 * allocation. <nsize> is the requested new size.
7975 */
7976static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7977{
7978 struct hlua_mem_allocator *zone = ud;
7979
7980 if (nsize == 0) {
7981 /* it's a free */
7982 if (ptr)
7983 zone->allocated -= osize;
7984 free(ptr);
7985 return NULL;
7986 }
7987
7988 if (!ptr) {
7989 /* it's a new allocation */
7990 if (zone->limit && zone->allocated + nsize > zone->limit)
7991 return NULL;
7992
7993 ptr = malloc(nsize);
7994 if (ptr)
7995 zone->allocated += nsize;
7996 return ptr;
7997 }
7998
7999 /* it's a realloc */
8000 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8001 return NULL;
8002
8003 ptr = realloc(ptr, nsize);
8004 if (ptr)
8005 zone->allocated += nsize - osize;
8006 return ptr;
8007}
8008
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008009/* Ithis function can fail with an abort() due to an Lua critical error.
8010 * We are in the initialisation process of HAProxy, this abort() is
8011 * tolerated.
8012 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008013void hlua_init(void)
8014{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008015 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008016 int idx;
8017 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008018 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008019 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008020 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008021#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008022 struct srv_kw *kw;
8023 int tmp_error;
8024 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008025 char *args[] = { /* SSL client configuration. */
8026 "ssl",
8027 "verify",
8028 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008029 NULL
8030 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008031#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008032
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008033 /* Init main lua stack. */
8034 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008035 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008036 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008037 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008038 hlua_sethlua(&gL);
8039 gL.Tref = LUA_REFNIL;
8040 gL.task = NULL;
8041
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008042 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008043 * the Lua function can fail with an abort. We are in the initialisation
8044 * process of HAProxy, this abort() is tolerated.
8045 */
8046
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008047 /* Initialise lua. */
8048 luaL_openlibs(gL.T);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008049#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8050#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8051#ifdef HLUA_PREPEND_PATH
8052 hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
8053#endif
8054#ifdef HLUA_PREPEND_CPATH
8055 hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
8056#endif
8057#undef HLUA_PREPEND_PATH_TOSTRING
8058#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008059
Thierry Fournier75933d42016-01-21 09:30:18 +01008060 /* Set safe environment for the initialisation. */
8061 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008062 if (lua_type(gL.T, -1) == LUA_TSTRING)
8063 error_msg = lua_tostring(gL.T, -1);
8064 else
8065 error_msg = "critical error";
8066 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008067 exit(1);
8068 }
8069
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008070 /*
8071 *
8072 * Create "core" object.
8073 *
8074 */
8075
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008076 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008077 lua_newtable(gL.T);
8078
8079 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008080 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008081 hlua_class_const_int(gL.T, log_levels[i], i);
8082
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008083 /* Register special functions. */
8084 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008085 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008086 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008087 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008088 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008089 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008090 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008091 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008092 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008093 hlua_class_function(gL.T, "sleep", hlua_sleep);
8094 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008095 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8096 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8097 hlua_class_function(gL.T, "set_map", hlua_set_map);
8098 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008099 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008100 hlua_class_function(gL.T, "log", hlua_log);
8101 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8102 hlua_class_function(gL.T, "Info", hlua_log_info);
8103 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8104 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008105 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008106 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008107
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008108 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008109
8110 /*
8111 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008112 * Create "act" object.
8113 *
8114 */
8115
8116 /* This table entry is the object "act" base. */
8117 lua_newtable(gL.T);
8118
8119 /* push action return constants */
8120 hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT);
8121 hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP);
8122 hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD);
8123 hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR);
8124 hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE);
8125 hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY);
8126 hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT);
8127 hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV);
8128
Christopher Faulet501465d2020-02-26 14:54:16 +01008129 hlua_class_function(gL.T, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008130
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008131 lua_setglobal(gL.T, "act");
8132
8133 /*
8134 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008135 * Register class Map
8136 *
8137 */
8138
8139 /* This table entry is the object "Map" base. */
8140 lua_newtable(gL.T);
8141
8142 /* register pattern types. */
8143 for (i=0; i<PAT_MATCH_NUM; i++)
8144 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008145 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008146 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8147 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008148 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008149
8150 /* register constructor. */
8151 hlua_class_function(gL.T, "new", hlua_map_new);
8152
8153 /* Create and fill the metatable. */
8154 lua_newtable(gL.T);
8155
Ilya Shipitsind4259502020-04-08 01:07:56 +05008156 /* Create and fill the __index entry. */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008157 lua_pushstring(gL.T, "__index");
8158 lua_newtable(gL.T);
8159
8160 /* Register . */
8161 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8162 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8163
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008164 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008165
Thierry Fournier45e78d72016-02-19 18:34:46 +01008166 /* Register previous table in the registry with reference and named entry.
8167 * The function hlua_register_metatable() pops the stack, so we
8168 * previously create a copy of the table.
8169 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008170 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008171 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008172
8173 /* Assign the metatable to the mai Map object. */
8174 lua_setmetatable(gL.T, -2);
8175
8176 /* Set a name to the table. */
8177 lua_setglobal(gL.T, "Map");
8178
8179 /*
8180 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008181 * Register class Channel
8182 *
8183 */
8184
8185 /* Create and fill the metatable. */
8186 lua_newtable(gL.T);
8187
Ilya Shipitsind4259502020-04-08 01:07:56 +05008188 /* Create and fill the __index entry. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008189 lua_pushstring(gL.T, "__index");
8190 lua_newtable(gL.T);
8191
8192 /* Register . */
8193 hlua_class_function(gL.T, "get", hlua_channel_get);
8194 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8195 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8196 hlua_class_function(gL.T, "set", hlua_channel_set);
8197 hlua_class_function(gL.T, "append", hlua_channel_append);
8198 hlua_class_function(gL.T, "send", hlua_channel_send);
8199 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8200 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8201 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008202 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01008203 hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008204
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008205 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008206
8207 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008208 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008209
8210 /*
8211 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008212 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008213 *
8214 */
8215
8216 /* Create and fill the metatable. */
8217 lua_newtable(gL.T);
8218
Ilya Shipitsind4259502020-04-08 01:07:56 +05008219 /* Create and fill the __index entry. */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008220 lua_pushstring(gL.T, "__index");
8221 lua_newtable(gL.T);
8222
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008223 /* Browse existing fetches and create the associated
8224 * object method.
8225 */
8226 sf = NULL;
8227 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8228
8229 /* Dont register the keywork if the arguments check function are
8230 * not safe during the runtime.
8231 */
8232 if ((sf->val_args != NULL) &&
8233 (sf->val_args != val_payload_lv) &&
8234 (sf->val_args != val_hdr))
8235 continue;
8236
8237 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8238 * by an underscore.
8239 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008240 strncpy(trash.area, sf->kw, trash.size);
8241 trash.area[trash.size - 1] = '\0';
8242 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008243 if (*p == '.' || *p == '-' || *p == '+')
8244 *p = '_';
8245
8246 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008247 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008248 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008249 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008250 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008251 }
8252
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008253 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008254
8255 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008256 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008257
8258 /*
8259 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008260 * Register class Converters
8261 *
8262 */
8263
8264 /* Create and fill the metatable. */
8265 lua_newtable(gL.T);
8266
8267 /* Create and fill the __index entry. */
8268 lua_pushstring(gL.T, "__index");
8269 lua_newtable(gL.T);
8270
8271 /* Browse existing converters and create the associated
8272 * object method.
8273 */
8274 sc = NULL;
8275 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8276 /* Dont register the keywork if the arguments check function are
8277 * not safe during the runtime.
8278 */
8279 if (sc->val_args != NULL)
8280 continue;
8281
8282 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8283 * by an underscore.
8284 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008285 strncpy(trash.area, sc->kw, trash.size);
8286 trash.area[trash.size - 1] = '\0';
8287 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008288 if (*p == '.' || *p == '-' || *p == '+')
8289 *p = '_';
8290
8291 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008292 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008293 lua_pushlightuserdata(gL.T, sc);
8294 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008295 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008296 }
8297
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008298 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008299
8300 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008301 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008302
8303 /*
8304 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008305 * Register class HTTP
8306 *
8307 */
8308
8309 /* Create and fill the metatable. */
8310 lua_newtable(gL.T);
8311
Ilya Shipitsind4259502020-04-08 01:07:56 +05008312 /* Create and fill the __index entry. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008313 lua_pushstring(gL.T, "__index");
8314 lua_newtable(gL.T);
8315
8316 /* Register Lua functions. */
8317 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8318 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8319 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8320 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8321 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8322 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8323 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8324 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8325 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8326 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8327
8328 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8329 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8330 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8331 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8332 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8333 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008334 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008335
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008336 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008337
8338 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008339 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008340
8341 /*
8342 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008343 * Register class AppletTCP
8344 *
8345 */
8346
8347 /* Create and fill the metatable. */
8348 lua_newtable(gL.T);
8349
Ilya Shipitsind4259502020-04-08 01:07:56 +05008350 /* Create and fill the __index entry. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008351 lua_pushstring(gL.T, "__index");
8352 lua_newtable(gL.T);
8353
8354 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008355 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8356 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8357 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8358 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8359 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008360 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8361 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8362 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008363
8364 lua_settable(gL.T, -3);
8365
8366 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008367 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008368
8369 /*
8370 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008371 * Register class AppletHTTP
8372 *
8373 */
8374
8375 /* Create and fill the metatable. */
8376 lua_newtable(gL.T);
8377
Ilya Shipitsind4259502020-04-08 01:07:56 +05008378 /* Create and fill the __index entry. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008379 lua_pushstring(gL.T, "__index");
8380 lua_newtable(gL.T);
8381
8382 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008383 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8384 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008385 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8386 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8387 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008388 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8389 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8390 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8391 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8392 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8393 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8394
8395 lua_settable(gL.T, -3);
8396
8397 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008398 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008399
8400 /*
8401 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008402 * Register class TXN
8403 *
8404 */
8405
8406 /* Create and fill the metatable. */
8407 lua_newtable(gL.T);
8408
Ilya Shipitsind4259502020-04-08 01:07:56 +05008409 /* Create and fill the __index entry. */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008410 lua_pushstring(gL.T, "__index");
8411 lua_newtable(gL.T);
8412
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008413 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008414 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8415 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8416 hlua_class_function(gL.T, "set_var", hlua_set_var);
8417 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8418 hlua_class_function(gL.T, "get_var", hlua_get_var);
8419 hlua_class_function(gL.T, "done", hlua_txn_done);
Christopher Faulet700d9e82020-01-31 12:21:52 +01008420 hlua_class_function(gL.T, "reply", hlua_txn_reply_new);
Patrick Hemmer268a7072018-05-11 12:52:31 -04008421 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8422 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8423 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8424 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8425 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8426 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8427 hlua_class_function(gL.T, "log", hlua_txn_log);
8428 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8429 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8430 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8431 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008432
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008433 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008434
8435 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008436 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008437
8438 /*
8439 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01008440 * Register class reply
8441 *
8442 */
8443 lua_newtable(gL.T);
8444 lua_pushstring(gL.T, "__index");
8445 lua_newtable(gL.T);
8446 hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status);
8447 hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header);
8448 hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header);
8449 hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body);
8450 lua_settable(gL.T, -3); /* Sets the __index entry. */
8451 class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
8452
8453
8454 /*
8455 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008456 * Register class Socket
8457 *
8458 */
8459
8460 /* Create and fill the metatable. */
8461 lua_newtable(gL.T);
8462
Ilya Shipitsind4259502020-04-08 01:07:56 +05008463 /* Create and fill the __index entry. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008464 lua_pushstring(gL.T, "__index");
8465 lua_newtable(gL.T);
8466
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008467#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008468 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008469#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008470 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8471 hlua_class_function(gL.T, "send", hlua_socket_send);
8472 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8473 hlua_class_function(gL.T, "close", hlua_socket_close);
8474 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8475 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8476 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8477 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8478
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008479 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008480
8481 /* Register the garbage collector entry. */
8482 lua_pushstring(gL.T, "__gc");
8483 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008484 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008485
8486 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008487 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008488
8489 /* Proxy and server configuration initialisation. */
8490 memset(&socket_proxy, 0, sizeof(socket_proxy));
8491 init_new_proxy(&socket_proxy);
8492 socket_proxy.parent = NULL;
8493 socket_proxy.last_change = now.tv_sec;
8494 socket_proxy.id = "LUA-SOCKET";
8495 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8496 socket_proxy.maxconn = 0;
8497 socket_proxy.accept = NULL;
8498 socket_proxy.options2 |= PR_O2_INDEPSTR;
8499 socket_proxy.srv = NULL;
8500 socket_proxy.conn_retries = 0;
8501 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8502
8503 /* Init TCP server: unchanged parameters */
8504 memset(&socket_tcp, 0, sizeof(socket_tcp));
8505 socket_tcp.next = NULL;
8506 socket_tcp.proxy = &socket_proxy;
8507 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8508 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008509 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008510 socket_tcp.idle_conns = NULL;
8511 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008512 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008513 socket_tcp.last_change = 0;
8514 socket_tcp.id = "LUA-TCP-CONN";
8515 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8516 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8517 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8518
8519 /* XXX: Copy default parameter from default server,
8520 * but the default server is not initialized.
8521 */
8522 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8523 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8524 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8525 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8526 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8527 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8528 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8529 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8530 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8531 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8532
8533 socket_tcp.check.status = HCHK_STATUS_INI;
8534 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8535 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8536 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8537 socket_tcp.check.server = &socket_tcp;
8538
8539 socket_tcp.agent.status = HCHK_STATUS_INI;
8540 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8541 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8542 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8543 socket_tcp.agent.server = &socket_tcp;
8544
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008545 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008546
8547#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008548 /* Init TCP server: unchanged parameters */
8549 memset(&socket_ssl, 0, sizeof(socket_ssl));
8550 socket_ssl.next = NULL;
8551 socket_ssl.proxy = &socket_proxy;
8552 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8553 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008554 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008555 socket_ssl.idle_conns = NULL;
8556 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008557 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008558 socket_ssl.last_change = 0;
8559 socket_ssl.id = "LUA-SSL-CONN";
8560 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8561 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8562 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8563
8564 /* XXX: Copy default parameter from default server,
8565 * but the default server is not initialized.
8566 */
8567 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8568 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8569 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8570 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8571 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8572 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8573 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8574 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8575 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8576 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8577
8578 socket_ssl.check.status = HCHK_STATUS_INI;
8579 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8580 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8581 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8582 socket_ssl.check.server = &socket_ssl;
8583
8584 socket_ssl.agent.status = HCHK_STATUS_INI;
8585 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8586 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8587 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8588 socket_ssl.agent.server = &socket_ssl;
8589
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008590 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008591 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008592
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008593 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008594 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8595 /*
8596 *
8597 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008598 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008599 * features like client certificates and ssl_verify.
8600 *
8601 */
8602 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8603 if (tmp_error != 0) {
8604 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8605 abort(); /* This must be never arrives because the command line
8606 not editable by the user. */
8607 }
8608 idx += kw->skip;
8609 }
8610 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008611#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008612
8613 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008614}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008615
Willy Tarreau80713382018-11-26 10:19:54 +01008616static void hlua_register_build_options(void)
8617{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008618 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008619
Willy Tarreaubb57d942016-12-21 19:04:56 +01008620 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8621 hap_register_build_opts(ptr, 1);
8622}
Willy Tarreau80713382018-11-26 10:19:54 +01008623
8624INITCALL0(STG_REGISTER, hlua_register_build_options);