blob: bb82d956970156a67020795d296b47c22139126b [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>
Mark Lakes56cc1252018-03-27 09:48:06 +020014#include <limits.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020015#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010016
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010017#include <lauxlib.h>
18#include <lua.h>
19#include <lualib.h>
20
Thierry FOURNIER463119c2015-03-10 00:35:36 +010021#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
22#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010023#endif
24
Thierry FOURNIER380d0932015-01-23 14:27:52 +010025#include <ebpttree.h>
26
27#include <common/cfgparse.h>
Willy Tarreaub059b892018-10-16 17:57:36 +020028#include <common/compiler.h>
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +020029#include <common/hathreads.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010030#include <common/initcall.h>
31#include <common/xref.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010032#include <common/h1.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010033
William Lallemand9ed62032016-11-21 17:49:11 +010034#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010035#include <types/hlua.h>
36#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010037#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010038
Thierry FOURNIER55da1652015-01-23 11:36:30 +010039#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010041#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010042#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010043#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010045#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010046#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010047#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020048#include <proto/http_fetch.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010049#include <proto/http_htx.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020050#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020051#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010052#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040053#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010054#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010055#include <proto/payload.h>
56#include <proto/proto_http.h>
57#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010058#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020059#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020060#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010061#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010062#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010063#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020064#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010065
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010066/* Lua uses longjmp to perform yield or throwing errors. This
67 * macro is used only for identifying the function that can
68 * not return because a longjmp is executed.
69 * __LJMP marks a prototype of hlua file that can use longjmp.
70 * WILL_LJMP() marks an lua function that will use longjmp.
71 * MAY_LJMP() marks an lua function that may use longjmp.
72 */
73#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020074#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010075#define MAY_LJMP(func) func
76
Thierry FOURNIERbabae282015-09-17 11:36:37 +020077/* This couple of function executes securely some Lua calls outside of
78 * the lua runtime environment. Each Lua call can return a longjmp
79 * if it encounter a memory error.
80 *
81 * Lua documentation extract:
82 *
83 * If an error happens outside any protected environment, Lua calls
84 * a panic function (see lua_atpanic) and then calls abort, thus
85 * exiting the host application. Your panic function can avoid this
86 * exit by never returning (e.g., doing a long jump to your own
87 * recovery point outside Lua).
88 *
89 * The panic function runs as if it were a message handler (see
90 * §2.3); in particular, the error message is at the top of the
91 * stack. However, there is no guarantee about stack space. To push
92 * anything on the stack, the panic function must first check the
93 * available space (see §4.2).
94 *
95 * We must check all the Lua entry point. This includes:
96 * - The include/proto/hlua.h exported functions
97 * - the task wrapper function
98 * - The action wrapper function
99 * - The converters wrapper function
100 * - The sample-fetch wrapper functions
101 *
102 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800103 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200104 *
105 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
106 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
107 * because they must be exists in the program stack when the longjmp
108 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200109 *
110 * Note that the Lua processing is not really thread safe. It provides
111 * heavy system which consists to add our own lock function in the Lua
112 * code and recompile the library. This system will probably not accepted
113 * by maintainers of various distribs.
114 *
115 * Our main excution point of the Lua is the function lua_resume(). A
116 * quick looking on the Lua sources displays a lua_lock() a the start
117 * of function and a lua_unlock() at the end of the function. So I
118 * conclude that the Lua thread safe mode just perform a mutex around
119 * all execution. So I prefer to do this in the HAProxy code, it will be
120 * easier for distro maintainers.
121 *
122 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
123 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
124 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200125 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100126__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200127THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128static int hlua_panic_safe(lua_State *L) { return 0; }
129static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
130
131#define SET_SAFE_LJMP(__L) \
132 ({ \
133 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100134 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200135 if (setjmp(safe_ljmp_env) != 0) { \
136 lua_atpanic(__L, hlua_panic_safe); \
137 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100138 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200139 } else { \
140 lua_atpanic(__L, hlua_panic_ljmp); \
141 ret = 1; \
142 } \
143 ret; \
144 })
145
146/* If we are the last function catching Lua errors, we
147 * must reset the panic function.
148 */
149#define RESET_SAFE_LJMP(__L) \
150 do { \
151 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100152 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200153 } while(0)
154
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200155/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200156#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100157/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200158#define APPLET_HDR_SENT 0x04 /* Response header sent. */
159#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
160#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100161#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100162#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200163
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100164/* The main Lua execution context. */
165struct hlua gL;
166
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100167/* This is the memory pool containing struct lua for applets
168 * (including cli).
169 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100170DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100171
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100172/* Used for Socket connection. */
173static struct proxy socket_proxy;
174static struct server socket_tcp;
175#ifdef USE_OPENSSL
176static struct server socket_ssl;
177#endif
178
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100179/* List head of the function called at the initialisation time. */
180struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
181
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100182/* The following variables contains the reference of the different
183 * Lua classes. These references are useful for identify metadata
184 * associated with an object.
185 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100186static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100187static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100188static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100189static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100190static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100191static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200192static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200193static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200194static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100195
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100196/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200197 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100198 * short timeout. Lua linked with tasks doesn't have a timeout
199 * because a task may remain alive during all the haproxy execution.
200 */
201static unsigned int hlua_timeout_session = 4000; /* session timeout. */
202static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200203static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100204
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100205/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
206 * it is used for preventing infinite loops.
207 *
208 * I test the scheer with an infinite loop containing one incrementation
209 * and one test. I run this loop between 10 seconds, I raise a ceil of
210 * 710M loops from one interrupt each 9000 instructions, so I fix the value
211 * to one interrupt each 10 000 instructions.
212 *
213 * configured | Number of
214 * instructions | loops executed
215 * between two | in milions
216 * forced yields |
217 * ---------------+---------------
218 * 10 | 160
219 * 500 | 670
220 * 1000 | 680
221 * 5000 | 700
222 * 7000 | 700
223 * 8000 | 700
224 * 9000 | 710 <- ceil
225 * 10000 | 710
226 * 100000 | 710
227 * 1000000 | 710
228 *
229 */
230static unsigned int hlua_nb_instruction = 10000;
231
Willy Tarreau32f61e22015-03-18 17:54:59 +0100232/* Descriptor for the memory allocation state. If limit is not null, it will
233 * be enforced on any memory allocation.
234 */
235struct hlua_mem_allocator {
236 size_t allocated;
237 size_t limit;
238};
239
240static struct hlua_mem_allocator hlua_global_allocator;
241
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200242static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200243 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200244 "Cache-Control: no-cache\r\n"
245 "Connection: close\r\n"
246 "Content-Type: text/html\r\n"
247 "\r\n"
Joseph Herlant7fe15772018-11-15 10:07:32 -0800248 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200249
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100250/* These functions converts types between HAProxy internal args or
251 * sample and LUA types. Another function permits to check if the
252 * LUA stack contains arguments according with an required ARG_T
253 * format.
254 */
255static int hlua_arg2lua(lua_State *L, const struct arg *arg);
256static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100257__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100258 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100259static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100260static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100261static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
262
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200263__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
264
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200265#define SEND_ERR(__be, __fmt, __args...) \
266 do { \
267 send_log(__be, LOG_ERR, __fmt, ## __args); \
268 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100269 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200270 } while (0)
271
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100272/* Used to check an Lua function type in the stack. It creates and
273 * returns a reference of the function. This function throws an
274 * error if the rgument is not a "function".
275 */
276__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
277{
278 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100279 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100280 WILL_LJMP(luaL_argerror(L, argno, msg));
281 }
282 lua_pushvalue(L, argno);
283 return luaL_ref(L, LUA_REGISTRYINDEX);
284}
285
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200286/* Return the string that is of the top of the stack. */
287const char *hlua_get_top_error_string(lua_State *L)
288{
289 if (lua_gettop(L) < 1)
290 return "unknown error";
291 if (lua_type(L, -1) != LUA_TSTRING)
292 return "unknown error";
293 return lua_tostring(L, -1);
294}
295
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200296__LJMP static const char *hlua_traceback(lua_State *L)
297{
298 lua_Debug ar;
299 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200300 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200301 int filled = 0;
302
303 while (lua_getstack(L, level++, &ar)) {
304
305 /* Add separator */
306 if (filled)
307 chunk_appendf(msg, ", ");
308 filled = 1;
309
310 /* Fill fields:
311 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
312 * 'l': fills in the field currentline;
313 * 'n': fills in the field name and namewhat;
314 * 't': fills in the field istailcall;
315 */
316 lua_getinfo(L, "Slnt", &ar);
317
318 /* Append code localisation */
319 if (ar.currentline > 0)
320 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
321 else
322 chunk_appendf(msg, "%s ", ar.short_src);
323
324 /*
325 * Get function name
326 *
327 * if namewhat is no empty, name is defined.
328 * what contains "Lua" for Lua function, "C" for C function,
329 * or "main" for main code.
330 */
331 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
332 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
333
334 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
335 chunk_appendf(msg, "main chunk");
336
337 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
338 chunk_appendf(msg, "C function line %d", ar.linedefined);
339
340 else /* nothing left... */
341 chunk_appendf(msg, "?");
342
343
344 /* Display tailed call */
345 if (ar.istailcall)
346 chunk_appendf(msg, " ...");
347 }
348
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200349 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200350}
351
352
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100353/* This function check the number of arguments available in the
354 * stack. If the number of arguments available is not the same
355 * then <nb> an error is throwed.
356 */
357__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
358{
359 if (lua_gettop(L) == nb)
360 return;
361 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
362}
363
Mark Lakes22154b42018-01-29 14:38:40 -0800364/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100365 * and the line number where the error is encountered.
366 */
367static int hlua_pusherror(lua_State *L, const char *fmt, ...)
368{
369 va_list argp;
370 va_start(argp, fmt);
371 luaL_where(L, 1);
372 lua_pushvfstring(L, fmt, argp);
373 va_end(argp);
374 lua_concat(L, 2);
375 return 1;
376}
377
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100378/* This functions is used with sample fetch and converters. It
379 * converts the HAProxy configuration argument in a lua stack
380 * values.
381 *
382 * It takes an array of "arg", and each entry of the array is
383 * converted and pushed in the LUA stack.
384 */
385static int hlua_arg2lua(lua_State *L, const struct arg *arg)
386{
387 switch (arg->type) {
388 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100389 case ARGT_TIME:
390 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100391 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100392 break;
393
394 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200395 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100396 break;
397
398 case ARGT_IPV4:
399 case ARGT_IPV6:
400 case ARGT_MSK4:
401 case ARGT_MSK6:
402 case ARGT_FE:
403 case ARGT_BE:
404 case ARGT_TAB:
405 case ARGT_SRV:
406 case ARGT_USR:
407 case ARGT_MAP:
408 default:
409 lua_pushnil(L);
410 break;
411 }
412 return 1;
413}
414
415/* This function take one entrie in an LUA stack at the index "ud",
416 * and try to convert it in an HAProxy argument entry. This is useful
417 * with sample fetch wrappers. The input arguments are gived to the
418 * lua wrapper and converted as arg list by thi function.
419 */
420static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
421{
422 switch (lua_type(L, ud)) {
423
424 case LUA_TNUMBER:
425 case LUA_TBOOLEAN:
426 arg->type = ARGT_SINT;
427 arg->data.sint = lua_tointeger(L, ud);
428 break;
429
430 case LUA_TSTRING:
431 arg->type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200432 arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data);
Tim Duesterhusf371b832019-09-29 23:03:07 +0200433 /* We don't know the actual size of the underlying allocation, so be conservative. */
434 arg->data.str.size = arg->data.str.data;
435 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100436 break;
437
438 case LUA_TUSERDATA:
439 case LUA_TNIL:
440 case LUA_TTABLE:
441 case LUA_TFUNCTION:
442 case LUA_TTHREAD:
443 case LUA_TLIGHTUSERDATA:
444 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200445 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 break;
447 }
448 return 1;
449}
450
451/* the following functions are used to convert a struct sample
452 * in Lua type. This useful to convert the return of the
453 * fetchs or converters.
454 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100455static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100456{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200457 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100458 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100459 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200460 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100461 break;
462
463 case SMP_T_BIN:
464 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200465 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100466 break;
467
468 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200469 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100470 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
471 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
472 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
473 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
474 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
475 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
476 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
477 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
478 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100480 break;
481 default:
482 lua_pushnil(L);
483 break;
484 }
485 break;
486
487 case SMP_T_IPV4:
488 case SMP_T_IPV6:
489 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200490 if (sample_casts[smp->data.type][SMP_T_STR] &&
491 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200492 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100493 else
494 lua_pushnil(L);
495 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100496 default:
497 lua_pushnil(L);
498 break;
499 }
500 return 1;
501}
502
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100503/* the following functions are used to convert a struct sample
504 * in Lua strings. This is useful to convert the return of the
505 * fetchs or converters.
506 */
507static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
508{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200509 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100510
511 case SMP_T_BIN:
512 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200513 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100514 break;
515
516 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200517 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100518 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
519 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
520 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
521 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
522 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
523 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
524 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
525 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
526 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200527 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100528 break;
529 default:
530 lua_pushstring(L, "");
531 break;
532 }
533 break;
534
535 case SMP_T_SINT:
536 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100537 case SMP_T_IPV4:
538 case SMP_T_IPV6:
539 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200540 if (sample_casts[smp->data.type][SMP_T_STR] &&
541 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200542 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100543 else
544 lua_pushstring(L, "");
545 break;
546 default:
547 lua_pushstring(L, "");
548 break;
549 }
550 return 1;
551}
552
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100553/* the following functions are used to convert an Lua type in a
554 * struct sample. This is useful to provide data from a converter
555 * to the LUA code.
556 */
557static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
558{
559 switch (lua_type(L, ud)) {
560
561 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200562 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200563 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100564 break;
565
566
567 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200568 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200569 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 break;
571
572 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200573 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100574 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200575 smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data);
Tim Duesterhusf371b832019-09-29 23:03:07 +0200576 /* We don't know the actual size of the underlying allocation, so be conservative. */
577 smp->data.u.str.size = smp->data.u.str.data;
578 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100579 break;
580
581 case LUA_TUSERDATA:
582 case LUA_TNIL:
583 case LUA_TTABLE:
584 case LUA_TFUNCTION:
585 case LUA_TTHREAD:
586 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200587 case LUA_TNONE:
588 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200589 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200590 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100591 break;
592 }
593 return 1;
594}
595
596/* This function check the "argp" builded by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800597 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100598 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100599 *
600 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
601 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100602 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100603__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100604 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100605{
606 int min_arg;
607 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100608 struct proxy *px;
609 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100610
611 idx = 0;
612 min_arg = ARGM(mask);
613 mask >>= ARGM_BITS;
614
615 while (1) {
616
617 /* Check oversize. */
618 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100619 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100620 }
621
622 /* Check for mandatory arguments. */
623 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100624 if (idx < min_arg) {
625
626 /* If miss other argument than the first one, we return an error. */
627 if (idx > 0)
628 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
629
630 /* If first argument have a certain type, some default values
631 * may be used. See the function smp_resolve_args().
632 */
633 switch (mask & ARGT_MASK) {
634
635 case ARGT_FE:
636 if (!(p->cap & PR_CAP_FE))
637 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
638 argp[idx].data.prx = p;
639 argp[idx].type = ARGT_FE;
640 argp[idx+1].type = ARGT_STOP;
641 break;
642
643 case ARGT_BE:
644 if (!(p->cap & PR_CAP_BE))
645 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
646 argp[idx].data.prx = p;
647 argp[idx].type = ARGT_BE;
648 argp[idx+1].type = ARGT_STOP;
649 break;
650
651 case ARGT_TAB:
652 argp[idx].data.prx = p;
653 argp[idx].type = ARGT_TAB;
654 argp[idx+1].type = ARGT_STOP;
655 break;
656
657 default:
658 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
659 break;
660 }
661 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100662 return 0;
663 }
664
665 /* Check for exceed the number of requiered argument. */
666 if ((mask & ARGT_MASK) == ARGT_STOP &&
667 argp[idx].type != ARGT_STOP) {
668 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
669 }
670
671 if ((mask & ARGT_MASK) == ARGT_STOP &&
672 argp[idx].type == ARGT_STOP) {
673 return 0;
674 }
675
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100676 /* Convert some argument types. */
677 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100678 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100679 if (argp[idx].type != ARGT_SINT)
680 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
681 argp[idx].type = ARGT_SINT;
682 break;
683
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100684 case ARGT_TIME:
685 if (argp[idx].type != ARGT_SINT)
686 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200687 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100688 break;
689
690 case ARGT_SIZE:
691 if (argp[idx].type != ARGT_SINT)
692 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200693 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100694 break;
695
696 case ARGT_FE:
697 if (argp[idx].type != ARGT_STR)
698 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200699 memcpy(trash.area, argp[idx].data.str.area,
700 argp[idx].data.str.data);
701 trash.area[argp[idx].data.str.data] = 0;
702 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100703 if (!argp[idx].data.prx)
704 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
705 argp[idx].type = ARGT_FE;
706 break;
707
708 case ARGT_BE:
709 if (argp[idx].type != ARGT_STR)
710 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200711 memcpy(trash.area, argp[idx].data.str.area,
712 argp[idx].data.str.data);
713 trash.area[argp[idx].data.str.data] = 0;
714 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100715 if (!argp[idx].data.prx)
716 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
717 argp[idx].type = ARGT_BE;
718 break;
719
720 case ARGT_TAB:
721 if (argp[idx].type != ARGT_STR)
722 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200723 memcpy(trash.area, argp[idx].data.str.area,
724 argp[idx].data.str.data);
725 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhuse351e762019-09-29 23:03:09 +0200726 argp[idx].data.t = stktable_find_by_name(trash.area);
727 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100728 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
729 argp[idx].type = ARGT_TAB;
730 break;
731
732 case ARGT_SRV:
733 if (argp[idx].type != ARGT_STR)
734 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200735 memcpy(trash.area, argp[idx].data.str.area,
736 argp[idx].data.str.data);
737 trash.area[argp[idx].data.str.data] = 0;
738 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100739 if (sname) {
740 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200741 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200742 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100743 if (!px)
744 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
745 }
746 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200747 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100748 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100749 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100750 argp[idx].data.srv = findserver(px, sname);
751 if (!argp[idx].data.srv)
752 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
753 argp[idx].type = ARGT_SRV;
754 break;
755
756 case ARGT_IPV4:
Christopher Faulete7f1fcf2020-08-07 09:07:26 +0200757 if (argp[idx].type != ARGT_STR)
758 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 memcpy(trash.area, argp[idx].data.str.area,
760 argp[idx].data.str.data);
761 trash.area[argp[idx].data.str.data] = 0;
762 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100763 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
764 argp[idx].type = ARGT_IPV4;
765 break;
766
767 case ARGT_MSK4:
Christopher Fauletff8de2c2020-08-07 09:11:22 +0200768 if (argp[idx].type == ARGT_SINT)
769 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
770 else if (argp[idx].type == ARGT_STR) {
771 memcpy(trash.area, argp[idx].data.str.area,
772 argp[idx].data.str.data);
773 trash.area[argp[idx].data.str.data] = 0;
774 if (!str2mask(trash.area, &argp[idx].data.ipv4))
775 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
776 }
777 else
778 WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected"));
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100779 argp[idx].type = ARGT_MSK4;
780 break;
781
782 case ARGT_IPV6:
Christopher Faulete7f1fcf2020-08-07 09:07:26 +0200783 if (argp[idx].type != ARGT_STR)
784 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200785 memcpy(trash.area, argp[idx].data.str.area,
786 argp[idx].data.str.data);
787 trash.area[argp[idx].data.str.data] = 0;
788 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100789 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
790 argp[idx].type = ARGT_IPV6;
791 break;
792
793 case ARGT_MSK6:
Christopher Fauletff8de2c2020-08-07 09:11:22 +0200794 if (argp[idx].type == ARGT_SINT)
795 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
796 else if (argp[idx].type == ARGT_STR) {
797 memcpy(trash.area, argp[idx].data.str.area,
798 argp[idx].data.str.data);
799 trash.area[argp[idx].data.str.data] = 0;
800 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
801 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
802 }
803 else
804 WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected"));
Tim Duesterhusb814da62018-01-25 16:24:50 +0100805 argp[idx].type = ARGT_MSK6;
806 break;
807
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100808 case ARGT_MAP:
809 case ARGT_REG:
810 case ARGT_USR:
811 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100812 break;
813 }
814
815 /* Check for type of argument. */
816 if ((mask & ARGT_MASK) != argp[idx].type) {
817 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
818 arg_type_names[(mask & ARGT_MASK)],
819 arg_type_names[argp[idx].type & ARGT_MASK]);
820 WILL_LJMP(luaL_argerror(L, first + idx, msg));
821 }
822
823 /* Next argument. */
824 mask >>= ARGT_BITS;
825 idx++;
826 }
827}
828
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100829/*
830 * The following functions are used to make correspondance between the the
831 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100832 *
833 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100834 * - hlua_sethlua : create the association between hlua context and lua_state.
835 */
836static inline struct hlua *hlua_gethlua(lua_State *L)
837{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100838 struct hlua **hlua = lua_getextraspace(L);
839 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100840}
841static inline void hlua_sethlua(struct hlua *hlua)
842{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100843 struct hlua **hlua_store = lua_getextraspace(hlua->T);
844 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100845}
846
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100847/* This function is used to send logs. It try to send on screen (stderr)
848 * and on the default syslog server.
849 */
850static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
851{
852 struct tm tm;
853 char *p;
854
855 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200856 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100857 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200858 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200859 /* Break the message if exceed the buffer size. */
860 *(p-4) = ' ';
861 *(p-3) = '.';
862 *(p-2) = '.';
863 *(p-1) = '.';
864 break;
865 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100866 if (isprint(*msg))
867 *p = *msg;
868 else
869 *p = '.';
870 }
871 *p = '\0';
872
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200873 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100874 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletb290edf2020-10-02 18:13:52 +0200875 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
876 return;
877
Willy Tarreaua678b432015-08-28 10:14:59 +0200878 get_localtime(date.tv_sec, &tm);
879 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100880 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200881 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100882 fflush(stderr);
883 }
884}
885
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100886/* This function just ensure that the yield will be always
887 * returned with a timeout and permit to set some flags
888 */
889__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100890 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100891{
892 struct hlua *hlua = hlua_gethlua(L);
893
894 /* Set the wake timeout. If timeout is required, we set
895 * the expiration time.
896 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200897 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100898
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100899 hlua->flags |= flags;
900
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100901 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200902 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100903}
904
Willy Tarreau87b09662015-04-03 00:22:06 +0200905/* This function initialises the Lua environment stored in the stream.
906 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200908 *
909 * This function is particular. it initialises a new Lua thread. If the
910 * initialisation fails (example: out of memory error), the lua function
911 * throws an error (longjmp).
912 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100913 * In some case (at least one), this function can be called from safe
914 * environement, so we must not initialise it. While the support of
915 * threads appear, the safe environment set a lock to ensure only one
916 * Lua execution at a time. If we initialize safe environment in another
917 * safe environmenet, we have a dead lock.
918 *
919 * set "already_safe" true if the context is initialized form safe
920 * Lua fonction.
921 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800922 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200923 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800924 * MUST NOT manipulate the created thread stack state, because it is not
925 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100926 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100927int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100928{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100929 if (!already_safe) {
930 if (!SET_SAFE_LJMP(gL.T)) {
931 lua->Tref = LUA_REFNIL;
932 return 0;
933 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200934 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100935 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100936 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100937 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100938 lua->T = lua_newthread(gL.T);
939 if (!lua->T) {
940 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100941 if (!already_safe)
942 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100943 return 0;
944 }
945 hlua_sethlua(lua);
946 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
947 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100948 if (!already_safe)
949 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100950 return 1;
951}
952
Willy Tarreau87b09662015-04-03 00:22:06 +0200953/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100954 * is destroyed. The destroy also the memory context. The struct "lua"
955 * is not freed.
956 */
957void hlua_ctx_destroy(struct hlua *lua)
958{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100959 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100960 return;
961
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100962 if (!lua->T)
963 goto end;
964
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100965 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200966 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100967
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200968 if (!SET_SAFE_LJMP(lua->T))
969 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100970 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200971 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200972
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200973 if (!SET_SAFE_LJMP(gL.T))
974 return;
975 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
976 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200977 /* Forces a garbage collecting process. If the Lua program is finished
978 * without error, we run the GC on the thread pointer. Its freed all
979 * the unused memory.
980 * If the thread is finnish with an error or is currently yielded,
981 * it seems that the GC applied on the thread doesn't clean anything,
982 * so e run the GC on the main thread.
983 * NOTE: maybe this action locks all the Lua threads untiml the en of
984 * the garbage collection.
985 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200986 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200987 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200988 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200989 lua_gc(gL.T, LUA_GCCOLLECT, 0);
990 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200991 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200992
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200993 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100994
995end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100996 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100997}
998
999/* This function is used to restore the Lua context when a coroutine
1000 * fails. This function copy the common memory between old coroutine
1001 * and the new coroutine. The old coroutine is destroyed, and its
1002 * replaced by the new coroutine.
1003 * If the flag "keep_msg" is set, the last entry of the old is assumed
1004 * as string error message and it is copied in the new stack.
1005 */
1006static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1007{
1008 lua_State *T;
1009 int new_ref;
1010
1011 /* Renew the main LUA stack doesn't have sense. */
1012 if (lua == &gL)
1013 return 0;
1014
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001015 /* New Lua coroutine. */
1016 T = lua_newthread(gL.T);
1017 if (!T)
1018 return 0;
1019
1020 /* Copy last error message. */
1021 if (keep_msg)
1022 lua_xmove(lua->T, T, 1);
1023
1024 /* Copy data between the coroutines. */
1025 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1026 lua_xmove(lua->T, T, 1);
1027 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
1028
1029 /* Destroy old data. */
1030 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1031
1032 /* The thread is garbage collected by Lua. */
1033 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1034
1035 /* Fill the struct with the new coroutine values. */
1036 lua->Mref = new_ref;
1037 lua->T = T;
1038 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1039
1040 /* Set context. */
1041 hlua_sethlua(lua);
1042
1043 return 1;
1044}
1045
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001046void hlua_hook(lua_State *L, lua_Debug *ar)
1047{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001048 struct hlua *hlua = hlua_gethlua(L);
1049
1050 /* Lua cannot yield when its returning from a function,
1051 * so, we can fix the interrupt hook to 1 instruction,
1052 * expecting that the function is finnished.
1053 */
1054 if (lua_gethookmask(L) & LUA_MASKRET) {
1055 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1056 return;
1057 }
1058
1059 /* restore the interrupt condition. */
1060 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1061
1062 /* If we interrupt the Lua processing in yieldable state, we yield.
1063 * If the state is not yieldable, trying yield causes an error.
1064 */
1065 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001066 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001067
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001068 /* If we cannot yield, update the clock and check the timeout. */
1069 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001070 hlua->run_time += now_ms - hlua->start_time;
1071 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001072 lua_pushfstring(L, "execution timeout");
1073 WILL_LJMP(lua_error(L));
1074 }
1075
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001076 /* Update the start time. */
1077 hlua->start_time = now_ms;
1078
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001079 /* Try to interrupt the process at the end of the current
1080 * unyieldable function.
1081 */
1082 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001083}
1084
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001085/* This function start or resumes the Lua stack execution. If the flag
1086 * "yield_allowed" if no set and the LUA stack execution returns a yield
1087 * The function return an error.
1088 *
1089 * The function can returns 4 values:
1090 * - HLUA_E_OK : The execution is terminated without any errors.
1091 * - HLUA_E_AGAIN : The execution must continue at the next associated
1092 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001093 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001094 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001095 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001096 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001097 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001098 * LUA code.
1099 */
1100static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1101{
Christopher Faulet20699902020-07-28 10:33:25 +02001102#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1103 int nres;
1104#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001105 int ret;
1106 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001107 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001108
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001109 /* Initialise run time counter. */
1110 if (!HLUA_IS_RUNNING(lua))
1111 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001112
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001113 /* Lock the whole Lua execution. This lock must be before the
1114 * label "resume_execution".
1115 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001116 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001117
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001118resume_execution:
1119
1120 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1121 * instructions. it is used for preventing infinite loops.
1122 */
1123 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1124
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001125 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001126 HLUA_SET_RUN(lua);
1127 HLUA_CLR_CTRLYIELD(lua);
1128 HLUA_CLR_WAKERESWR(lua);
1129 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001130
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001131 /* Update the start time. */
1132 lua->start_time = now_ms;
1133
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001134 /* Call the function. */
Christopher Faulet20699902020-07-28 10:33:25 +02001135#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1136 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1137#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001138 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet20699902020-07-28 10:33:25 +02001139#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001140 switch (ret) {
1141
1142 case LUA_OK:
1143 ret = HLUA_E_OK;
1144 break;
1145
1146 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001147 /* Check if the execution timeout is expired. It it is the case, we
1148 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001149 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001150 tv_update_date(0, 1);
1151 lua->run_time += now_ms - lua->start_time;
1152 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001153 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001154 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001155 break;
1156 }
1157 /* Process the forced yield. if the general yield is not allowed or
1158 * if no task were associated this the current Lua execution
1159 * coroutine, we resume the execution. Else we want to return in the
1160 * scheduler and we want to be waked up again, to continue the
1161 * current Lua execution. So we schedule our own task.
1162 */
1163 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001164 if (!yield_allowed || !lua->task)
1165 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001166 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001167 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001168 if (!yield_allowed) {
1169 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001170 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001171 break;
1172 }
1173 ret = HLUA_E_AGAIN;
1174 break;
1175
1176 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001177
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001178 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001179 * because the errors ares the only one mean to return immediately
1180 * from and lua execution.
1181 */
1182 if (lua->flags & HLUA_EXIT) {
1183 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001184 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001185 break;
1186 }
1187
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001188 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001189 if (!lua_checkstack(lua->T, 1)) {
1190 ret = HLUA_E_ERR;
1191 break;
1192 }
1193 msg = lua_tostring(lua->T, -1);
1194 lua_settop(lua->T, 0); /* Empty the stack. */
1195 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001196 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001197 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001198 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001199 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001200 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001201 ret = HLUA_E_ERRMSG;
1202 break;
1203
1204 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001205 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001206 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001207 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001208 break;
1209
1210 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001211 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001212 if (!lua_checkstack(lua->T, 1)) {
1213 ret = HLUA_E_ERR;
1214 break;
1215 }
1216 msg = lua_tostring(lua->T, -1);
1217 lua_settop(lua->T, 0); /* Empty the stack. */
1218 lua_pop(lua->T, 1);
1219 if (msg)
1220 lua_pushfstring(lua->T, "message handler error: %s", msg);
1221 else
1222 lua_pushfstring(lua->T, "message handler error");
1223 ret = HLUA_E_ERRMSG;
1224 break;
1225
1226 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001227 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001228 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001229 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001230 break;
1231 }
1232
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001233 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001234 if (lua->flags & HLUA_MUST_GC &&
1235 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001236 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1237
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001238 switch (ret) {
1239 case HLUA_E_AGAIN:
1240 break;
1241
1242 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001243 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001244 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001245 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001246 break;
1247
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001248 case HLUA_E_ETMOUT:
1249 case HLUA_E_NOMEM:
1250 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001251 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001252 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001253 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001254 hlua_ctx_renew(lua, 0);
1255 break;
1256
1257 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001258 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001259 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001260 break;
1261 }
1262
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001263 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001264 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001265
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001266 return ret;
1267}
1268
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001269/* This function exit the current code. */
1270__LJMP static int hlua_done(lua_State *L)
1271{
1272 struct hlua *hlua = hlua_gethlua(L);
1273
1274 hlua->flags |= HLUA_EXIT;
1275 WILL_LJMP(lua_error(L));
1276
1277 return 0;
1278}
1279
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001280/* This function is an LUA binding. It provides a function
1281 * for deleting ACL from a referenced ACL file.
1282 */
1283__LJMP static int hlua_del_acl(lua_State *L)
1284{
1285 const char *name;
1286 const char *key;
1287 struct pat_ref *ref;
1288
1289 MAY_LJMP(check_args(L, 2, "del_acl"));
1290
1291 name = MAY_LJMP(luaL_checkstring(L, 1));
1292 key = MAY_LJMP(luaL_checkstring(L, 2));
1293
1294 ref = pat_ref_lookup(name);
1295 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001296 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001297
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001298 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001299 pat_ref_delete(ref, key);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001300 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001301 return 0;
1302}
1303
1304/* This function is an LUA binding. It provides a function
1305 * for deleting map entry from a referenced map file.
1306 */
1307static int hlua_del_map(lua_State *L)
1308{
1309 const char *name;
1310 const char *key;
1311 struct pat_ref *ref;
1312
1313 MAY_LJMP(check_args(L, 2, "del_map"));
1314
1315 name = MAY_LJMP(luaL_checkstring(L, 1));
1316 key = MAY_LJMP(luaL_checkstring(L, 2));
1317
1318 ref = pat_ref_lookup(name);
1319 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001320 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001321
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001322 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001323 pat_ref_delete(ref, key);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001324 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001325 return 0;
1326}
1327
1328/* This function is an LUA binding. It provides a function
1329 * for adding ACL pattern from a referenced ACL file.
1330 */
1331static int hlua_add_acl(lua_State *L)
1332{
1333 const char *name;
1334 const char *key;
1335 struct pat_ref *ref;
1336
1337 MAY_LJMP(check_args(L, 2, "add_acl"));
1338
1339 name = MAY_LJMP(luaL_checkstring(L, 1));
1340 key = MAY_LJMP(luaL_checkstring(L, 2));
1341
1342 ref = pat_ref_lookup(name);
1343 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001344 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001345
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001346 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001347 if (pat_ref_find_elt(ref, key) == NULL)
1348 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001349 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001350 return 0;
1351}
1352
1353/* This function is an LUA binding. It provides a function
1354 * for setting map pattern and sample from a referenced map
1355 * file.
1356 */
1357static int hlua_set_map(lua_State *L)
1358{
1359 const char *name;
1360 const char *key;
1361 const char *value;
1362 struct pat_ref *ref;
1363
1364 MAY_LJMP(check_args(L, 3, "set_map"));
1365
1366 name = MAY_LJMP(luaL_checkstring(L, 1));
1367 key = MAY_LJMP(luaL_checkstring(L, 2));
1368 value = MAY_LJMP(luaL_checkstring(L, 3));
1369
1370 ref = pat_ref_lookup(name);
1371 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001372 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001373
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001374 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001375 if (pat_ref_find_elt(ref, key) != NULL)
1376 pat_ref_set(ref, key, value, NULL);
1377 else
1378 pat_ref_add(ref, key, value, NULL);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001379 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001380 return 0;
1381}
1382
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001383/* A class is a lot of memory that contain data. This data can be a table,
1384 * an integer or user data. This data is associated with a metatable. This
1385 * metatable have an original version registred in the global context with
1386 * the name of the object (_G[<name>] = <metable> ).
1387 *
1388 * A metable is a table that modify the standard behavior of a standard
1389 * access to the associated data. The entries of this new metatable are
1390 * defined as is:
1391 *
1392 * http://lua-users.org/wiki/MetatableEvents
1393 *
1394 * __index
1395 *
1396 * we access an absent field in a table, the result is nil. This is
1397 * true, but it is not the whole truth. Actually, such access triggers
1398 * the interpreter to look for an __index metamethod: If there is no
1399 * such method, as usually happens, then the access results in nil;
1400 * otherwise, the metamethod will provide the result.
1401 *
1402 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1403 * the key does not appear in the table, but the metatable has an __index
1404 * property:
1405 *
1406 * - if the value is a function, the function is called, passing in the
1407 * table and the key; the return value of that function is returned as
1408 * the result.
1409 *
1410 * - if the value is another table, the value of the key in that table is
1411 * asked for and returned (and if it doesn't exist in that table, but that
1412 * table's metatable has an __index property, then it continues on up)
1413 *
1414 * - Use "rawget(myTable,key)" to skip this metamethod.
1415 *
1416 * http://www.lua.org/pil/13.4.1.html
1417 *
1418 * __newindex
1419 *
1420 * Like __index, but control property assignment.
1421 *
1422 * __mode - Control weak references. A string value with one or both
1423 * of the characters 'k' and 'v' which specifies that the the
1424 * keys and/or values in the table are weak references.
1425 *
1426 * __call - Treat a table like a function. When a table is followed by
1427 * parenthesis such as "myTable( 'foo' )" and the metatable has
1428 * a __call key pointing to a function, that function is invoked
1429 * (passing any specified arguments) and the return value is
1430 * returned.
1431 *
1432 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1433 * called, if the metatable for myTable has a __metatable
1434 * key, the value of that key is returned instead of the
1435 * actual metatable.
1436 *
1437 * __tostring - Control string representation. When the builtin
1438 * "tostring( myTable )" function is called, if the metatable
1439 * for myTable has a __tostring property set to a function,
1440 * that function is invoked (passing myTable to it) and the
1441 * return value is used as the string representation.
1442 *
1443 * __len - Control table length. When the table length is requested using
1444 * the length operator ( '#' ), if the metatable for myTable has
1445 * a __len key pointing to a function, that function is invoked
1446 * (passing myTable to it) and the return value used as the value
1447 * of "#myTable".
1448 *
1449 * __gc - Userdata finalizer code. When userdata is set to be garbage
1450 * collected, if the metatable has a __gc field pointing to a
1451 * function, that function is first invoked, passing the userdata
1452 * to it. The __gc metamethod is not called for tables.
1453 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1454 *
1455 * Special metamethods for redefining standard operators:
1456 * http://www.lua.org/pil/13.1.html
1457 *
1458 * __add "+"
1459 * __sub "-"
1460 * __mul "*"
1461 * __div "/"
1462 * __unm "!"
1463 * __pow "^"
1464 * __concat ".."
1465 *
1466 * Special methods for redfining standar relations
1467 * http://www.lua.org/pil/13.2.html
1468 *
1469 * __eq "=="
1470 * __lt "<"
1471 * __le "<="
1472 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001473
1474/*
1475 *
1476 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001477 * Class Map
1478 *
1479 *
1480 */
1481
1482/* Returns a struct hlua_map if the stack entry "ud" is
1483 * a class session, otherwise it throws an error.
1484 */
1485__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1486{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001487 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001488}
1489
1490/* This function is the map constructor. It don't need
1491 * the class Map object. It creates and return a new Map
1492 * object. It must be called only during "body" or "init"
1493 * context because it process some filesystem accesses.
1494 */
1495__LJMP static int hlua_map_new(struct lua_State *L)
1496{
1497 const char *fn;
1498 int match = PAT_MATCH_STR;
1499 struct sample_conv conv;
1500 const char *file = "";
1501 int line = 0;
1502 lua_Debug ar;
1503 char *err = NULL;
1504 struct arg args[2];
1505
1506 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1507 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1508
1509 fn = MAY_LJMP(luaL_checkstring(L, 1));
1510
1511 if (lua_gettop(L) >= 2) {
1512 match = MAY_LJMP(luaL_checkinteger(L, 2));
1513 if (match < 0 || match >= PAT_MATCH_NUM)
1514 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1515 }
1516
1517 /* Get Lua filename and line number. */
1518 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1519 lua_getinfo(L, "Sl", &ar); /* get info about it */
1520 if (ar.currentline > 0) { /* is there info? */
1521 file = ar.short_src;
1522 line = ar.currentline;
1523 }
1524 }
1525
1526 /* fill fake sample_conv struct. */
1527 conv.kw = ""; /* unused. */
1528 conv.process = NULL; /* unused. */
1529 conv.arg_mask = 0; /* unused. */
1530 conv.val_args = NULL; /* unused. */
1531 conv.out_type = SMP_T_STR;
1532 conv.private = (void *)(long)match;
1533 switch (match) {
1534 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1535 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1536 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1537 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1538 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1539 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1540 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001541 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001542 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1543 default:
1544 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1545 }
1546
1547 /* fill fake args. */
1548 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001549 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001550 args[1].type = ARGT_STOP;
1551
1552 /* load the map. */
1553 if (!sample_load_map(args, &conv, file, line, &err)) {
1554 /* error case: we cant use luaL_error because we must
1555 * free the err variable.
1556 */
1557 luaL_where(L, 1);
1558 lua_pushfstring(L, "'new': %s.", err);
1559 lua_concat(L, 2);
1560 free(err);
1561 WILL_LJMP(lua_error(L));
1562 }
1563
1564 /* create the lua object. */
1565 lua_newtable(L);
1566 lua_pushlightuserdata(L, args[0].data.map);
1567 lua_rawseti(L, -2, 0);
1568
1569 /* Pop a class Map metatable and affect it to the userdata. */
1570 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1571 lua_setmetatable(L, -2);
1572
1573
1574 return 1;
1575}
1576
1577__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1578{
1579 struct map_descriptor *desc;
1580 struct pattern *pat;
1581 struct sample smp;
1582
1583 MAY_LJMP(check_args(L, 2, "lookup"));
1584 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001585 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001586 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001587 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001588 }
1589 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001590 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001591 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001592 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 +02001593 }
1594
1595 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001596 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001597 if (str)
1598 lua_pushstring(L, "");
1599 else
1600 lua_pushnil(L);
1601 return 1;
1602 }
1603
1604 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001605 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001606 return 1;
1607}
1608
1609__LJMP static int hlua_map_lookup(struct lua_State *L)
1610{
1611 return _hlua_map_lookup(L, 0);
1612}
1613
1614__LJMP static int hlua_map_slookup(struct lua_State *L)
1615{
1616 return _hlua_map_lookup(L, 1);
1617}
1618
1619/*
1620 *
1621 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001622 * Class Socket
1623 *
1624 *
1625 */
1626
1627__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1628{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001629 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001630}
1631
1632/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001633 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001634 * received.
1635 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001636static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001637{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001638 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001639 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001640
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001641 if (appctx->ctx.hlua_cosocket.die) {
1642 si_shutw(si);
1643 si_shutr(si);
1644 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001645 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1646 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001647 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1648 }
1649
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001650 /* If the connection object is not available, close all the
1651 * streams and wakeup everything waiting for.
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001652 */
1653 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001654 si_shutw(si);
1655 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001656 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001657 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1658 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001659 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001660 }
1661
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001662 /* If we cant write, wakeup the pending write signals. */
1663 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001664 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001665
1666 /* If we cant read, wakeup the pending read signals. */
1667 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001668 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001669
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001670 /* if the connection is not estabkished, inform the stream that we want
1671 * to be notified whenever the connection completes.
1672 */
1673 if (!(c->flags & CO_FL_CONNECTED)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001674 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001675 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001676 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001677 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001678 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001679
1680 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001681 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001682
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001683 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001684 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001685 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001686
1687 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001688 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001689 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001690
1691 /* Some data were injected in the buffer, notify the stream
1692 * interface.
1693 */
1694 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001695 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001696
1697 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001698 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001699 */
1700 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001701 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001702}
1703
Willy Tarreau87b09662015-04-03 00:22:06 +02001704/* This function is called when the "struct stream" is destroyed.
1705 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001706 * Wake all the pending signals.
1707 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001708static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001709{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001710 struct xref *peer;
1711
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001712 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001713 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1714 if (peer)
1715 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001716
1717 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001718 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1719 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001720}
1721
1722/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001723 * uses this object. If the stream does not exists, just quit.
1724 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001725 * pending signal can rest in the read and write lists. destroy
1726 * it.
1727 */
1728__LJMP static int hlua_socket_gc(lua_State *L)
1729{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001730 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001731 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001732 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001733
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001734 MAY_LJMP(check_args(L, 1, "__gc"));
1735
1736 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001737 peer = xref_get_peer_and_lock(&socket->xref);
1738 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001739 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001740 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001741
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001742 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001743 appctx->ctx.hlua_cosocket.die = 1;
1744 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001745
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001746 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001747 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001748 return 0;
1749}
1750
1751/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001752 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001753 */
sada05ed3302018-05-11 11:48:18 -07001754__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001755{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001756 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001757 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001758 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001759
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001760 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001761
1762 /* Check if we run on the same thread than the xreator thread.
1763 * We cannot access to the socket if the thread is different.
1764 */
1765 if (socket->tid != tid)
1766 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1767
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001768 peer = xref_get_peer_and_lock(&socket->xref);
1769 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001770 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001771 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001772
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001773 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001774 appctx->ctx.hlua_cosocket.die = 1;
1775 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001776
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001777 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001778 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001779 return 0;
1780}
1781
sada05ed3302018-05-11 11:48:18 -07001782/* The close function calls close_helper.
1783 */
1784__LJMP static int hlua_socket_close(lua_State *L)
1785{
1786 MAY_LJMP(check_args(L, 1, "close"));
1787 return hlua_socket_close_helper(L);
1788}
1789
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001790/* This Lua function assumes that the stack contain three parameters.
1791 * 1 - USERDATA containing a struct socket
1792 * 2 - INTEGER with values of the macro defined below
1793 * If the integer is -1, we must read at most one line.
1794 * If the integer is -2, we ust read all the data until the
1795 * end of the stream.
1796 * If the integer is positive value, we must read a number of
1797 * bytes corresponding to this value.
1798 */
1799#define HLSR_READ_LINE (-1)
1800#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001801__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001802{
1803 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1804 int wanted = lua_tointeger(L, 2);
1805 struct hlua *hlua = hlua_gethlua(L);
1806 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001807 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001808 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001809 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001810 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001811 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001812 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001813 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001814 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001815 struct stream_interface *si;
1816 struct stream *s;
1817 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001818 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001819
1820 /* Check if this lua stack is schedulable. */
1821 if (!hlua || !hlua->task)
1822 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1823 "'frontend', 'backend' or 'task'"));
1824
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001825 /* Check if we run on the same thread than the xreator thread.
1826 * We cannot access to the socket if the thread is different.
1827 */
1828 if (socket->tid != tid)
1829 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1830
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001831 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001832 peer = xref_get_peer_and_lock(&socket->xref);
1833 if (!peer)
1834 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001835 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1836 si = appctx->owner;
1837 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001838
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001839 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001840 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001841 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001842 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001843 if (nblk < 0) /* Connection close. */
1844 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001845 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001846 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001847
1848 /* remove final \r\n. */
1849 if (nblk == 1) {
1850 if (blk1[len1-1] == '\n') {
1851 len1--;
1852 skip_at_end++;
1853 if (blk1[len1-1] == '\r') {
1854 len1--;
1855 skip_at_end++;
1856 }
1857 }
1858 }
1859 else {
1860 if (blk2[len2-1] == '\n') {
1861 len2--;
1862 skip_at_end++;
1863 if (blk2[len2-1] == '\r') {
1864 len2--;
1865 skip_at_end++;
1866 }
1867 }
1868 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001869 }
1870
1871 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001872 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001873 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001874 if (nblk < 0) /* Connection close. */
1875 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001876 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001877 goto connection_empty;
1878 }
1879
1880 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001881 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001882 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001883 if (nblk < 0) /* Connection close. */
1884 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001885 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001886 goto connection_empty;
1887
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001888 missing_bytes = wanted - socket->b.n;
1889 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001890 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001891 len1 = missing_bytes;
1892 } if (nblk == 2 && len1 + len2 > missing_bytes)
1893 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001894 }
1895
1896 len = len1;
1897
1898 luaL_addlstring(&socket->b, blk1, len1);
1899 if (nblk == 2) {
1900 len += len2;
1901 luaL_addlstring(&socket->b, blk2, len2);
1902 }
1903
1904 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001905 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906
1907 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001908 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909
1910 /* If the pattern reclaim to read all the data
1911 * in the connection, got out.
1912 */
1913 if (wanted == HLSR_READ_ALL)
1914 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001915 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001916 goto connection_empty;
1917
1918 /* Return result. */
1919 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001920 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001921 return 1;
1922
1923connection_closed:
1924
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001925 xref_unlock(&socket->xref, peer);
1926
1927no_peer:
1928
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001929 /* If the buffer containds data. */
1930 if (socket->b.n > 0) {
1931 luaL_pushresult(&socket->b);
1932 return 1;
1933 }
1934 lua_pushnil(L);
1935 lua_pushstring(L, "connection closed.");
1936 return 2;
1937
1938connection_empty:
1939
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001940 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1941 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001942 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001943 }
1944 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001945 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001946 return 0;
1947}
1948
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001949/* This Lua function gets two parameters. The first one can be string
1950 * or a number. If the string is "*l", the user requires one line. If
1951 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001952 * If the value is a number, the user require a number of bytes equal
1953 * to the value. The default value is "*l" (a line).
1954 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001955 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001956 * integer takes this values:
1957 * -1 : read a line
1958 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001959 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001960 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001961 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962 * concatenated with the read data.
1963 */
1964__LJMP static int hlua_socket_receive(struct lua_State *L)
1965{
1966 int wanted = HLSR_READ_LINE;
1967 const char *pattern;
1968 int type;
1969 char *error;
1970 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001971 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001972
1973 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1974 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1975
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001976 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001977
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001978 /* Check if we run on the same thread than the xreator thread.
1979 * We cannot access to the socket if the thread is different.
1980 */
1981 if (socket->tid != tid)
1982 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1983
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001984 /* check for pattern. */
1985 if (lua_gettop(L) >= 2) {
1986 type = lua_type(L, 2);
1987 if (type == LUA_TSTRING) {
1988 pattern = lua_tostring(L, 2);
1989 if (strcmp(pattern, "*a") == 0)
1990 wanted = HLSR_READ_ALL;
1991 else if (strcmp(pattern, "*l") == 0)
1992 wanted = HLSR_READ_LINE;
1993 else {
1994 wanted = strtoll(pattern, &error, 10);
1995 if (*error != '\0')
1996 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1997 }
1998 }
1999 else if (type == LUA_TNUMBER) {
2000 wanted = lua_tointeger(L, 2);
2001 if (wanted < 0)
2002 WILL_LJMP(luaL_error(L, "Unsupported size."));
2003 }
2004 }
2005
2006 /* Set pattern. */
2007 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002008
2009 /* Check if we would replace the top by itself. */
2010 if (lua_gettop(L) != 2)
2011 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002012
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002013 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002014 luaL_buffinit(L, &socket->b);
2015
2016 /* Check prefix. */
2017 if (lua_gettop(L) >= 3) {
2018 if (lua_type(L, 3) != LUA_TSTRING)
2019 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2020 pattern = lua_tolstring(L, 3, &len);
2021 luaL_addlstring(&socket->b, pattern, len);
2022 }
2023
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002024 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002025}
2026
2027/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002028 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002029 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002030static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002031{
2032 struct hlua_socket *socket;
2033 struct hlua *hlua = hlua_gethlua(L);
2034 struct appctx *appctx;
2035 size_t buf_len;
2036 const char *buf;
2037 int len;
2038 int send_len;
2039 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002040 struct xref *peer;
2041 struct stream_interface *si;
2042 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002043
2044 /* Check if this lua stack is schedulable. */
2045 if (!hlua || !hlua->task)
2046 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2047 "'frontend', 'backend' or 'task'"));
2048
2049 /* Get object */
2050 socket = MAY_LJMP(hlua_checksocket(L, 1));
2051 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002052 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002053
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002054 /* Check if we run on the same thread than the xreator thread.
2055 * We cannot access to the socket if the thread is different.
2056 */
2057 if (socket->tid != tid)
2058 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2059
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002060 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002061 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002062 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002063 lua_pushinteger(L, -1);
2064 return 1;
2065 }
2066 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2067 si = appctx->owner;
2068 s = si_strm(si);
2069
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002070 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002071 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002072 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002073 lua_pushinteger(L, -1);
2074 return 1;
2075 }
2076
2077 /* Update the input buffer data. */
2078 buf += sent;
2079 send_len = buf_len - sent;
2080
2081 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002082 if (sent >= buf_len) {
2083 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002084 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002085 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002086
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002087 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002088 * the request buffer if its not required.
2089 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002090 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002091 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002092 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002093 }
2094
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002095 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002096 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002097 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002098 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002099 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002100
2101 /* send data */
2102 if (len < send_len)
2103 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002104 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002105
2106 /* "Not enough space" (-1), "Buffer too little to contain
2107 * the data" (-2) are not expected because the available length
2108 * is tested.
2109 * Other unknown error are also not expected.
2110 */
2111 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002112 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002113 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002114
sada05ed3302018-05-11 11:48:18 -07002115 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002116 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002117 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002118 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002119 return 1;
2120 }
2121
2122 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002123 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002124
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002125 s->req.rex = TICK_ETERNITY;
2126 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002127
2128 /* Update length sent. */
2129 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002130 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002131
2132 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002133 if (sent + len >= buf_len) {
2134 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002135 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002136 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002137
2138hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002139 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2140 xref_unlock(&socket->xref, peer);
2141 WILL_LJMP(luaL_error(L, "out of memory"));
2142 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002143 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002144 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002145 return 0;
2146}
2147
2148/* This function initiate the send of data. It just check the input
2149 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002150 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002151 * "hlua_socket_write_yield" that can yield.
2152 *
2153 * The Lua function gets between 3 and 4 parameters. The first one is
2154 * the associated object. The second is a string buffer. The third is
2155 * a facultative integer that represents where is the buffer position
2156 * of the start of the data that can send. The first byte is the
2157 * position "1". The default value is "1". The fourth argument is a
2158 * facultative integer that represents where is the buffer position
2159 * of the end of the data that can send. The default is the last byte.
2160 */
2161static int hlua_socket_send(struct lua_State *L)
2162{
2163 int i;
2164 int j;
2165 const char *buf;
2166 size_t buf_len;
2167
2168 /* Check number of arguments. */
2169 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2170 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2171
2172 /* Get the string. */
2173 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2174
2175 /* Get and check j. */
2176 if (lua_gettop(L) == 4) {
2177 j = MAY_LJMP(luaL_checkinteger(L, 4));
2178 if (j < 0)
2179 j = buf_len + j + 1;
2180 if (j > buf_len)
2181 j = buf_len + 1;
2182 lua_pop(L, 1);
2183 }
2184 else
2185 j = buf_len;
2186
2187 /* Get and check i. */
2188 if (lua_gettop(L) == 3) {
2189 i = MAY_LJMP(luaL_checkinteger(L, 3));
2190 if (i < 0)
2191 i = buf_len + i + 1;
2192 if (i > buf_len)
2193 i = buf_len + 1;
2194 lua_pop(L, 1);
2195 } else
2196 i = 1;
2197
2198 /* Check bth i and j. */
2199 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002200 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002201 return 1;
2202 }
2203 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002204 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002205 return 1;
2206 }
2207 if (i == 0)
2208 i = 1;
2209 if (j == 0)
2210 j = 1;
2211
2212 /* Pop the string. */
2213 lua_pop(L, 1);
2214
2215 /* Update the buffer length. */
2216 buf += i - 1;
2217 buf_len = j - i + 1;
2218 lua_pushlstring(L, buf, buf_len);
2219
2220 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002221 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002222
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002223 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002224}
2225
Willy Tarreau22b0a682015-06-17 19:43:49 +02002226#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002227__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2228{
2229 static char buffer[SOCKET_INFO_MAX_LEN];
2230 int ret;
2231 int len;
2232 char *p;
2233
2234 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2235 if (ret <= 0) {
2236 lua_pushnil(L);
2237 return 1;
2238 }
2239
2240 if (ret == AF_UNIX) {
2241 lua_pushstring(L, buffer+1);
2242 return 1;
2243 }
2244 else if (ret == AF_INET6) {
2245 buffer[0] = '[';
2246 len = strlen(buffer);
2247 buffer[len] = ']';
2248 len++;
2249 buffer[len] = ':';
2250 len++;
2251 p = buffer;
2252 }
2253 else if (ret == AF_INET) {
2254 p = buffer + 1;
2255 len = strlen(p);
2256 p[len] = ':';
2257 len++;
2258 }
2259 else {
2260 lua_pushnil(L);
2261 return 1;
2262 }
2263
2264 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2265 lua_pushnil(L);
2266 return 1;
2267 }
2268
2269 lua_pushstring(L, p);
2270 return 1;
2271}
2272
2273/* Returns information about the peer of the connection. */
2274__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2275{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002276 struct hlua_socket *socket;
2277 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002278 struct xref *peer;
2279 struct appctx *appctx;
2280 struct stream_interface *si;
2281 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002282 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002283
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002284 MAY_LJMP(check_args(L, 1, "getpeername"));
2285
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002286 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002287
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002288 /* Check if we run on the same thread than the xreator thread.
2289 * We cannot access to the socket if the thread is different.
2290 */
2291 if (socket->tid != tid)
2292 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2293
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002294 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002295 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002296 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297 lua_pushnil(L);
2298 return 1;
2299 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002300 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2301 si = appctx->owner;
2302 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002304 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002305 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002306 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307 lua_pushnil(L);
2308 return 1;
2309 }
2310
Willy Tarreaua71f6422016-11-16 17:00:14 +01002311 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002312 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002313 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002314 lua_pushnil(L);
2315 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002316 }
2317
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002318 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2319 xref_unlock(&socket->xref, peer);
2320 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002321}
2322
2323/* Returns information about my connection side. */
2324static int hlua_socket_getsockname(struct lua_State *L)
2325{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002326 struct hlua_socket *socket;
2327 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002328 struct appctx *appctx;
2329 struct xref *peer;
2330 struct stream_interface *si;
2331 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002332 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002333
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002334 MAY_LJMP(check_args(L, 1, "getsockname"));
2335
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002336 socket = MAY_LJMP(hlua_checksocket(L, 1));
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 */
2341 if (socket->tid != tid)
2342 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2343
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002344 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002345 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002346 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002347 lua_pushnil(L);
2348 return 1;
2349 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002350 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2351 si = appctx->owner;
2352 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002353
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002354 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002355 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002356 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002357 lua_pushnil(L);
2358 return 1;
2359 }
2360
Willy Tarreaua71f6422016-11-16 17:00:14 +01002361 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002362 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002363 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002364 lua_pushnil(L);
2365 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002366 }
2367
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002368 ret = hlua_socket_info(L, &conn->addr.from);
2369 xref_unlock(&socket->xref, peer);
2370 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002371}
2372
2373/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002374static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002375 .obj_type = OBJ_TYPE_APPLET,
2376 .name = "<LUA_TCP>",
2377 .fct = hlua_socket_handler,
2378 .release = hlua_socket_release,
2379};
2380
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002381__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002382{
2383 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2384 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002385 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002386 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002387 struct stream_interface *si;
2388 struct stream *s;
2389
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002390 /* Check if we run on the same thread than the xreator thread.
2391 * We cannot access to the socket if the thread is different.
2392 */
2393 if (socket->tid != tid)
2394 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2395
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002396 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002397 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002398 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002399 lua_pushnil(L);
2400 lua_pushstring(L, "Can't connect");
2401 return 2;
2402 }
2403 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2404 si = appctx->owner;
2405 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002406
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002407 /* Check if we run on the same thread than the xreator thread.
2408 * We cannot access to the socket if the thread is different.
2409 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002410 if (socket->tid != tid) {
2411 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002412 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002413 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002414
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002415 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002416 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002417 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002418 lua_pushnil(L);
2419 lua_pushstring(L, "Can't connect");
2420 return 2;
2421 }
2422
Willy Tarreaue09101e2018-10-16 17:37:12 +02002423 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002424
2425 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002426 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002427 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002428 lua_pushinteger(L, 1);
2429 return 1;
2430 }
2431
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002432 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2433 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002434 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002435 }
2436 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002437 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002438 return 0;
2439}
2440
2441/* This function fail or initite the connection. */
2442__LJMP static int hlua_socket_connect(struct lua_State *L)
2443{
2444 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002445 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002446 const char *ip;
2447 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002448 struct hlua *hlua;
2449 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002450 int low, high;
2451 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002452 struct xref *peer;
2453 struct stream_interface *si;
2454 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002455
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002456 if (lua_gettop(L) < 2)
2457 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002458
2459 /* Get args. */
2460 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002461
2462 /* Check if we run on the same thread than the xreator thread.
2463 * We cannot access to the socket if the thread is different.
2464 */
2465 if (socket->tid != tid)
2466 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2467
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002468 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002469 if (lua_gettop(L) >= 3) {
2470 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002471 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002472
Tim Duesterhus6edab862018-01-06 19:04:45 +01002473 /* Force the ip to end with a colon, to support IPv6 addresses
2474 * that are not enclosed within square brackets.
2475 */
2476 if (port > 0) {
2477 luaL_buffinit(L, &b);
2478 luaL_addstring(&b, ip);
2479 luaL_addchar(&b, ':');
2480 luaL_pushresult(&b);
2481 ip = lua_tolstring(L, lua_gettop(L), NULL);
2482 }
2483 }
2484
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002485 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002486 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002487 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002488 lua_pushnil(L);
2489 return 1;
2490 }
2491 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2492 si = appctx->owner;
2493 s = si_strm(si);
2494
2495 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002496 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002497 if (!conn) {
2498 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002499 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002500 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002501
Willy Tarreau3adac082015-09-26 17:51:09 +02002502 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002503 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002504
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002505 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002506 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002507 if (!addr) {
2508 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002509 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002510 }
2511 if (low != high) {
2512 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002513 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002514 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002515 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002516
2517 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002518 if (low == 0) {
2519 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002520 if (port == -1) {
2521 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002522 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002523 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002524 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2525 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002526 if (port == -1) {
2527 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002528 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002529 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002530 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2531 }
2532 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002533
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002534 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002535 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002536
2537 /* inform the stream that we want to be notified whenever the
2538 * connection completes.
2539 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002540 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002541 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002542 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002543
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002544 hlua->flags |= HLUA_MUST_GC;
2545
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002546 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2547 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002548 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002549 }
2550 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002551
2552 task_wakeup(s->task, TASK_WOKEN_INIT);
2553 /* Return yield waiting for connection. */
2554
Willy Tarreau9635e032018-10-16 17:52:55 +02002555 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002556
2557 return 0;
2558}
2559
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002560#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002561__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2562{
2563 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002564 struct xref *peer;
2565 struct appctx *appctx;
2566 struct stream_interface *si;
2567 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002568
2569 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2570 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002571
2572 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002573 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002574 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002575 lua_pushnil(L);
2576 return 1;
2577 }
2578 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2579 si = appctx->owner;
2580 s = si_strm(si);
2581
2582 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002583 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002584 return MAY_LJMP(hlua_socket_connect(L));
2585}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002586#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002587
2588__LJMP static int hlua_socket_setoption(struct lua_State *L)
2589{
2590 return 0;
2591}
2592
2593__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2594{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002595 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002596 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002597 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002598 struct xref *peer;
2599 struct appctx *appctx;
2600 struct stream_interface *si;
2601 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002602
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002603 MAY_LJMP(check_args(L, 2, "settimeout"));
2604
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002605 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002606
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002607 /* convert the timeout to millis */
2608 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002609
Thierry Fournier17a921b2018-03-08 09:59:02 +01002610 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002611 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002612 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2613
Mark Lakes56cc1252018-03-27 09:48:06 +02002614 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002615 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002616
2617 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002618 if (tmout == 0)
2619 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002620
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002621 /* Check if we run on the same thread than the xreator thread.
2622 * We cannot access to the socket if the thread is different.
2623 */
2624 if (socket->tid != tid)
2625 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2626
Mark Lakes56cc1252018-03-27 09:48:06 +02002627 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002628 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002629 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002630 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2631 WILL_LJMP(lua_error(L));
2632 return 0;
2633 }
2634 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2635 si = appctx->owner;
2636 s = si_strm(si);
2637
Cyril Bonté7bb63452018-08-17 23:51:02 +02002638 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002639 s->req.rto = tmout;
2640 s->req.wto = tmout;
2641 s->res.rto = tmout;
2642 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002643 s->req.rex = tick_add_ifset(now_ms, tmout);
2644 s->req.wex = tick_add_ifset(now_ms, tmout);
2645 s->res.rex = tick_add_ifset(now_ms, tmout);
2646 s->res.wex = tick_add_ifset(now_ms, tmout);
2647
2648 s->task->expire = tick_add_ifset(now_ms, tmout);
2649 task_queue(s->task);
2650
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002651 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002652
Thierry Fourniere9636f12018-03-08 09:54:32 +01002653 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002654 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002655}
2656
2657__LJMP static int hlua_socket_new(lua_State *L)
2658{
2659 struct hlua_socket *socket;
2660 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002661 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002662 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002663
2664 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002665 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002666 hlua_pusherror(L, "socket: full stack");
2667 goto out_fail_conf;
2668 }
2669
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002670 /* Create the object: obj[0] = userdata. */
2671 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002672 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002673 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002674 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002675 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002676
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002677 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002678 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002679 hlua_pusherror(L, "socket: uninitialized pools.");
2680 goto out_fail_conf;
2681 }
2682
Willy Tarreau87b09662015-04-03 00:22:06 +02002683 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002684 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2685 lua_setmetatable(L, -2);
2686
Willy Tarreaud420a972015-04-06 00:39:18 +02002687 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002688 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002689 if (!appctx) {
2690 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002691 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002692 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002693
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002694 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002695 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002696 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2697 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002698
Willy Tarreaud420a972015-04-06 00:39:18 +02002699 /* Now create a session, task and stream for this applet */
2700 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2701 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002702 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002703 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002704 }
2705
Willy Tarreau87787ac2017-08-28 16:22:54 +02002706 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002707 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002708 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002709 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002710 }
2711
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002712 /* Initialise cross reference between stream and Lua socket object. */
2713 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002714
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002715 /* Configure "right" stream interface. this "si" is used to connect
2716 * and retrieve data from the server. The connection is initialized
2717 * with the "struct server".
2718 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002719 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002720
2721 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002722 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2723 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002724
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002725 return 1;
2726
Willy Tarreaud420a972015-04-06 00:39:18 +02002727 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002728 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002729 out_fail_sess:
2730 appctx_free(appctx);
2731 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002732 WILL_LJMP(lua_error(L));
2733 return 0;
2734}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002735
2736/*
2737 *
2738 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002739 * Class Channel
2740 *
2741 *
2742 */
2743
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002744/* This function is called before the Lua execution. It stores
2745 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002746 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002747static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002748{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002749 c->mode = stream->be->mode;
2750 switch (c->mode) {
2751 case PR_MODE_HTTP:
2752 c->data.http.dir = opt & SMP_OPT_DIR;
2753 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2754 c->data.http.state = stream->txn->req.msg_state;
2755 else
2756 c->data.http.state = stream->txn->rsp.msg_state;
2757 break;
2758 default:
2759 break;
2760 }
2761}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002762
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002763/* This function is called after the Lua execution. it
2764 * returns true if the parser state is consistent, otherwise,
2765 * it return false.
2766 *
2767 * In HTTP mode, the parser state must be in the same state
2768 * or greater when we exit the function. Even if we do a
2769 * control yield. This prevent to break the HTTP message
2770 * from the Lua code.
2771 */
2772static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2773{
2774 if (c->mode != stream->be->mode)
2775 return 0;
2776
2777 switch (c->mode) {
2778 case PR_MODE_HTTP:
2779 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002780 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002781 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2782 return stream->txn->req.msg_state >= c->data.http.state;
2783 else
2784 return stream->txn->rsp.msg_state >= c->data.http.state;
2785 default:
2786 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002787 }
2788 return 1;
2789}
2790
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002791/* Returns the struct hlua_channel join to the class channel in the
2792 * stack entry "ud" or throws an argument error.
2793 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002794__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002795{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002796 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002797}
2798
Willy Tarreau47860ed2015-03-10 14:07:50 +01002799/* Pushes the channel onto the top of the stack. If the stask does not have a
2800 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002801 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002802static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002803{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002804 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002805 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002806 return 0;
2807
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002808 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002809 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002810 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002811
2812 /* Pop a class sesison metatable and affect it to the userdata. */
2813 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2814 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002815 return 1;
2816}
2817
2818/* Duplicate all the data present in the input channel and put it
2819 * in a string LUA variables. Returns -1 and push a nil value in
2820 * the stack if the channel is closed and all the data are consumed,
2821 * returns 0 if no data are available, otherwise it returns the length
2822 * of the builded string.
2823 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002824static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002825{
2826 char *blk1;
2827 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002828 size_t len1;
2829 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002830 int ret;
2831 luaL_Buffer b;
2832
Willy Tarreau06d80a92017-10-19 14:32:15 +02002833 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002834 if (unlikely(ret == 0))
2835 return 0;
2836
2837 if (unlikely(ret < 0)) {
2838 lua_pushnil(L);
2839 return -1;
2840 }
2841
2842 luaL_buffinit(L, &b);
2843 luaL_addlstring(&b, blk1, len1);
2844 if (unlikely(ret == 2))
2845 luaL_addlstring(&b, blk2, len2);
2846 luaL_pushresult(&b);
2847
2848 if (unlikely(ret == 2))
2849 return len1 + len2;
2850 return len1;
2851}
2852
2853/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2854 * a yield. This function keep the data in the buffer.
2855 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002856__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002857{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002858 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002859
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002860 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2861
Christopher Faulet3f829a42018-12-13 21:56:45 +01002862 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2863 WILL_LJMP(lua_error(L));
2864
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002865 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002866 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002867 return 1;
2868}
2869
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002870/* Check arguments for the function "hlua_channel_dup_yield". */
2871__LJMP static int hlua_channel_dup(lua_State *L)
2872{
2873 MAY_LJMP(check_args(L, 1, "dup"));
2874 MAY_LJMP(hlua_checkchannel(L, 1));
2875 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2876}
2877
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002878/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2879 * a yield. This function consumes the data in the buffer. It returns
2880 * a string containing the data or a nil pointer if no data are available
2881 * and the channel is closed.
2882 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002883__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002884{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002885 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002886 int ret;
2887
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002888 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002889
Christopher Faulet3f829a42018-12-13 21:56:45 +01002890 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2891 WILL_LJMP(lua_error(L));
2892
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002893 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002894 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002895 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002896
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897 if (unlikely(ret == -1))
2898 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002899
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002900 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901 return 1;
2902}
2903
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002904/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002905__LJMP static int hlua_channel_get(lua_State *L)
2906{
2907 MAY_LJMP(check_args(L, 1, "get"));
2908 MAY_LJMP(hlua_checkchannel(L, 1));
2909 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2910}
2911
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912/* This functions consumes and returns one line. If the channel is closed,
2913 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002914 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002915 * value.
2916 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002917__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002919 char *blk1;
2920 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002921 size_t len1;
2922 size_t len2;
2923 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002924 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002925 int ret;
2926 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002927
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002928 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2929
Christopher Faulet3f829a42018-12-13 21:56:45 +01002930 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2931 WILL_LJMP(lua_error(L));
2932
Willy Tarreau06d80a92017-10-19 14:32:15 +02002933 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002934 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002935 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002936
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937 if (ret == -1) {
2938 lua_pushnil(L);
2939 return 1;
2940 }
2941
2942 luaL_buffinit(L, &b);
2943 luaL_addlstring(&b, blk1, len1);
2944 len = len1;
2945 if (unlikely(ret == 2)) {
2946 luaL_addlstring(&b, blk2, len2);
2947 len += len2;
2948 }
2949 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002950 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002951 return 1;
2952}
2953
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002954/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002955__LJMP static int hlua_channel_getline(lua_State *L)
2956{
2957 MAY_LJMP(check_args(L, 1, "getline"));
2958 MAY_LJMP(hlua_checkchannel(L, 1));
2959 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2960}
2961
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002962/* This function takes a string as input, and append it at the
2963 * input side of channel. If the data is too big, but a space
2964 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002965 * yields. If the data is bigger than the buffer, or if the
2966 * channel is closed, it returns -1. Otherwise, it returns the
2967 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002968 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002969__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002970{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002971 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002972 size_t len;
2973 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2974 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2975 int ret;
2976 int max;
2977
Christopher Faulet3f829a42018-12-13 21:56:45 +01002978 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2979 WILL_LJMP(lua_error(L));
2980
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002981 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002982 * the request buffer if its not required.
2983 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002984 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002985 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002986 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002987 }
2988
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002989 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002990 if (max > len - l)
2991 max = len - l;
2992
Willy Tarreau06d80a92017-10-19 14:32:15 +02002993 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994 if (ret == -2 || ret == -3) {
2995 lua_pushinteger(L, -1);
2996 return 1;
2997 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002998 if (ret == -1) {
2999 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02003000 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01003001 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003002 l += ret;
3003 lua_pop(L, 1);
3004 lua_pushinteger(L, l);
3005
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003006 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003007 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003008 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003009 * in this case, we cannot add more data, so we cannot yield,
3010 * we return the amount of copyied data.
3011 */
3012 return 1;
3013 }
3014 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003015 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003016 return 1;
3017}
3018
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003019/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3020 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003021 * buffer size is too little for the data.
3022 */
3023__LJMP static int hlua_channel_append(lua_State *L)
3024{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003025 size_t len;
3026
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003027 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003028 MAY_LJMP(hlua_checkchannel(L, 1));
3029 MAY_LJMP(luaL_checklstring(L, 2, &len));
3030 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031 lua_pushinteger(L, 0);
3032
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003033 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003034}
3035
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003036/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003037 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003038 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003039 * or -1 if the channel is closed or if the buffer size is too
3040 * little for the data.
3041 */
3042__LJMP static int hlua_channel_set(lua_State *L)
3043{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003044 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003045
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003046 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003047 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003048 lua_pushinteger(L, 0);
3049
Christopher Faulet3f829a42018-12-13 21:56:45 +01003050 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3051 WILL_LJMP(lua_error(L));
3052
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003053 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003054
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003055 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003056}
3057
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003058/* Append data in the output side of the buffer. This data is immediately
3059 * sent. The function returns the amount of data written. If the buffer
3060 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003061 * if the channel is closed.
3062 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003063__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003064{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003065 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003066 size_t len;
3067 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3068 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3069 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003070 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003071
Christopher Faulet3f829a42018-12-13 21:56:45 +01003072 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3073 WILL_LJMP(lua_error(L));
3074
Willy Tarreau47860ed2015-03-10 14:07:50 +01003075 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003076 lua_pushinteger(L, -1);
3077 return 1;
3078 }
3079
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003080 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003081 * the request buffer if its not required.
3082 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003083 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003084 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003085 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003086 }
3087
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003088 /* The written data will be immediately sent, so we can check
3089 * the available space without taking in account the reserve.
3090 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003091 * data, because the buffer will be flushed.
3092 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003093 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003094
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003095 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003096 * in this case, we cannot add more data, so we cannot yield,
3097 * we return the amount of copyied data.
3098 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003099 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003100 return 1;
3101
3102 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003103 if (max > len - l)
3104 max = len - l;
3105
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003106 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003107 * detects a non contiguous buffer and realign it.
3108 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003109 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003110 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003111
3112 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003113 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003114
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003115 /* buffer replace considers that the input part is filled.
3116 * so, I must forward these new data in the output part.
3117 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003118 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003119
3120 l += max;
3121 lua_pop(L, 1);
3122 lua_pushinteger(L, l);
3123
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003124 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003125 * in this case, we cannot add more data, so we cannot yield,
3126 * we return the amount of copyied data.
3127 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003128 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003129 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003130 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003131
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003132 if (l < len) {
3133 /* If we are waiting for space in the response buffer, we
3134 * must set the flag WAKERESWR. This flag required the task
3135 * wake up if any activity is detected on the response buffer.
3136 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003137 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003138 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003139 else
3140 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003141 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003142 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003143
3144 return 1;
3145}
3146
3147/* Just a wraper of "_hlua_channel_send". This wrapper permits
3148 * yield the LUA process, and resume it without checking the
3149 * input arguments.
3150 */
3151__LJMP static int hlua_channel_send(lua_State *L)
3152{
3153 MAY_LJMP(check_args(L, 2, "send"));
3154 lua_pushinteger(L, 0);
3155
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003156 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003157}
3158
3159/* This function forward and amount of butes. The data pass from
3160 * the input side of the buffer to the output side, and can be
3161 * forwarded. This function never fails.
3162 *
3163 * The Lua function takes an amount of bytes to be forwarded in
3164 * imput. It returns the number of bytes forwarded.
3165 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003166__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003167{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003168 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003169 int len;
3170 int l;
3171 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003172 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003173
3174 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003175
3176 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3177 WILL_LJMP(lua_error(L));
3178
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003179 len = MAY_LJMP(luaL_checkinteger(L, 2));
3180 l = MAY_LJMP(luaL_checkinteger(L, -1));
3181
3182 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003183 if (max > ci_data(chn))
3184 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003185 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003186 l += max;
3187
3188 lua_pop(L, 1);
3189 lua_pushinteger(L, l);
3190
3191 /* Check if it miss bytes to forward. */
3192 if (l < len) {
3193 /* The the input channel or the output channel are closed, we
3194 * must return the amount of data forwarded.
3195 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003196 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003197 return 1;
3198
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003199 /* If we are waiting for space data in the response buffer, we
3200 * must set the flag WAKERESWR. This flag required the task
3201 * wake up if any activity is detected on the response buffer.
3202 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003203 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003204 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003205 else
3206 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003207
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003208 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003209 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003210 }
3211
3212 return 1;
3213}
3214
3215/* Just check the input and prepare the stack for the previous
3216 * function "hlua_channel_forward_yield"
3217 */
3218__LJMP static int hlua_channel_forward(lua_State *L)
3219{
3220 MAY_LJMP(check_args(L, 2, "forward"));
3221 MAY_LJMP(hlua_checkchannel(L, 1));
3222 MAY_LJMP(luaL_checkinteger(L, 2));
3223
3224 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003225 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003226}
3227
3228/* Just returns the number of bytes available in the input
3229 * side of the buffer. This function never fails.
3230 */
3231__LJMP static int hlua_channel_get_in_len(lua_State *L)
3232{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003233 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003234
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003235 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003236 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003237 if (IS_HTX_STRM(chn_strm(chn))) {
3238 struct htx *htx = htxbuf(&chn->buf);
3239 lua_pushinteger(L, htx->data - co_data(chn));
3240 }
3241 else
3242 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003243 return 1;
3244}
3245
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003246/* Returns true if the channel is full. */
3247__LJMP static int hlua_channel_is_full(lua_State *L)
3248{
3249 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003250
3251 MAY_LJMP(check_args(L, 1, "is_full"));
3252 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0136ebb2020-02-26 11:59:19 +01003253 /* ignore the reserve, we are not on a producer side (ie in an
3254 * applet).
3255 */
3256 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003257 return 1;
3258}
3259
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003260/* Just returns the number of bytes available in the output
3261 * side of the buffer. This function never fails.
3262 */
3263__LJMP static int hlua_channel_get_out_len(lua_State *L)
3264{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003265 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003266
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003267 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003268 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003269 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003270 return 1;
3271}
3272
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003273/*
3274 *
3275 *
3276 * Class Fetches
3277 *
3278 *
3279 */
3280
3281/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003282 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003283 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003284__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003285{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003286 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003287}
3288
3289/* This function creates and push in the stack a fetch object according
3290 * with a current TXN.
3291 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003292static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003293{
Willy Tarreau7073c472015-04-06 11:15:40 +02003294 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003295
3296 /* Check stack size. */
3297 if (!lua_checkstack(L, 3))
3298 return 0;
3299
3300 /* Create the object: obj[0] = userdata.
3301 * Note that the base of the Fetches object is the
3302 * transaction object.
3303 */
3304 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003305 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003306 lua_rawseti(L, -2, 0);
3307
Willy Tarreau7073c472015-04-06 11:15:40 +02003308 hsmp->s = txn->s;
3309 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003310 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003311 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003312
3313 /* Pop a class sesison metatable and affect it to the userdata. */
3314 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3315 lua_setmetatable(L, -2);
3316
3317 return 1;
3318}
3319
3320/* This function is an LUA binding. It is called with each sample-fetch.
3321 * It uses closure argument to store the associated sample-fetch. It
3322 * returns only one argument or throws an error. An error is thrown
3323 * only if an error is encountered during the argument parsing. If
3324 * the "sample-fetch" function fails, nil is returned.
3325 */
3326__LJMP static int hlua_run_sample_fetch(lua_State *L)
3327{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003328 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003329 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003330 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003331 int i;
3332 struct sample smp;
3333
3334 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003335 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003336
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003337 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003338 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003339
Thierry FOURNIERca988662015-12-20 18:43:03 +01003340 /* Check execution authorization. */
3341 if (f->use & SMP_USE_HTTP_ANY &&
3342 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3343 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3344 "is not available in Lua services", f->kw);
3345 WILL_LJMP(lua_error(L));
3346 }
3347
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003348 /* Get extra arguments. */
3349 for (i = 0; i < lua_gettop(L) - 1; i++) {
3350 if (i >= ARGM_NBARGS)
3351 break;
3352 hlua_lua2arg(L, i + 2, &args[i]);
3353 }
3354 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003355 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003356
3357 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003358 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003359
3360 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003361 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003362 lua_pushfstring(L, "error in arguments");
3363 WILL_LJMP(lua_error(L));
3364 }
3365
3366 /* Initialise the sample. */
3367 memset(&smp, 0, sizeof(smp));
3368
3369 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003370 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003371 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003372 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003373 lua_pushstring(L, "");
3374 else
3375 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003376 return 1;
3377 }
3378
3379 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003380 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003381 hlua_smp2lua_str(L, &smp);
3382 else
3383 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003384 return 1;
3385}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003386
3387/*
3388 *
3389 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003390 * Class Converters
3391 *
3392 *
3393 */
3394
3395/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003396 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003397 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003398__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003399{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003400 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003401}
3402
3403/* This function creates and push in the stack a Converters object
3404 * according with a current TXN.
3405 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003406static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003407{
Willy Tarreau7073c472015-04-06 11:15:40 +02003408 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003409
3410 /* Check stack size. */
3411 if (!lua_checkstack(L, 3))
3412 return 0;
3413
3414 /* Create the object: obj[0] = userdata.
3415 * Note that the base of the Converters object is the
3416 * same than the TXN object.
3417 */
3418 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003419 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003420 lua_rawseti(L, -2, 0);
3421
Willy Tarreau7073c472015-04-06 11:15:40 +02003422 hsmp->s = txn->s;
3423 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003424 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003425 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003426
Willy Tarreau87b09662015-04-03 00:22:06 +02003427 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003428 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3429 lua_setmetatable(L, -2);
3430
3431 return 1;
3432}
3433
3434/* This function is an LUA binding. It is called with each converter.
3435 * It uses closure argument to store the associated converter. It
3436 * returns only one argument or throws an error. An error is thrown
3437 * only if an error is encountered during the argument parsing. If
3438 * the converter function function fails, nil is returned.
3439 */
3440__LJMP static int hlua_run_sample_conv(lua_State *L)
3441{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003442 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003443 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003444 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003445 int i;
3446 struct sample smp;
3447
3448 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003449 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003450
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003451 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003452 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003453
3454 /* Get extra arguments. */
3455 for (i = 0; i < lua_gettop(L) - 2; i++) {
3456 if (i >= ARGM_NBARGS)
3457 break;
3458 hlua_lua2arg(L, i + 3, &args[i]);
3459 }
3460 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003461 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003462
3463 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003464 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003465
3466 /* Run the special args checker. */
3467 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3468 hlua_pusherror(L, "error in arguments");
3469 WILL_LJMP(lua_error(L));
3470 }
3471
3472 /* Initialise the sample. */
3473 if (!hlua_lua2smp(L, 2, &smp)) {
3474 hlua_pusherror(L, "error in the input argument");
3475 WILL_LJMP(lua_error(L));
3476 }
3477
Willy Tarreau1777ea62016-03-10 16:15:46 +01003478 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3479
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003480 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003481 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003482 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003483 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003484 WILL_LJMP(lua_error(L));
3485 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003486 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3487 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003488 hlua_pusherror(L, "error during the input argument casting");
3489 WILL_LJMP(lua_error(L));
3490 }
3491
3492 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003493 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003494 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003495 lua_pushstring(L, "");
3496 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003497 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003498 return 1;
3499 }
3500
3501 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003502 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003503 hlua_smp2lua_str(L, &smp);
3504 else
3505 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003506 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003507}
3508
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003509/*
3510 *
3511 *
3512 * Class AppletTCP
3513 *
3514 *
3515 */
3516
3517/* Returns a struct hlua_txn if the stack entry "ud" is
3518 * a class stream, otherwise it throws an error.
3519 */
3520__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3521{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003522 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003523}
3524
3525/* This function creates and push in the stack an Applet object
3526 * according with a current TXN.
3527 */
3528static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3529{
3530 struct hlua_appctx *appctx;
3531 struct stream_interface *si = ctx->owner;
3532 struct stream *s = si_strm(si);
3533 struct proxy *p = s->be;
3534
3535 /* Check stack size. */
3536 if (!lua_checkstack(L, 3))
3537 return 0;
3538
3539 /* Create the object: obj[0] = userdata.
3540 * Note that the base of the Converters object is the
3541 * same than the TXN object.
3542 */
3543 lua_newtable(L);
3544 appctx = lua_newuserdata(L, sizeof(*appctx));
3545 lua_rawseti(L, -2, 0);
3546 appctx->appctx = ctx;
3547 appctx->htxn.s = s;
3548 appctx->htxn.p = p;
3549
3550 /* Create the "f" field that contains a list of fetches. */
3551 lua_pushstring(L, "f");
3552 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3553 return 0;
3554 lua_settable(L, -3);
3555
3556 /* Create the "sf" field that contains a list of stringsafe fetches. */
3557 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003558 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003559 return 0;
3560 lua_settable(L, -3);
3561
3562 /* Create the "c" field that contains a list of converters. */
3563 lua_pushstring(L, "c");
3564 if (!hlua_converters_new(L, &appctx->htxn, 0))
3565 return 0;
3566 lua_settable(L, -3);
3567
3568 /* Create the "sc" field that contains a list of stringsafe converters. */
3569 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003570 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003571 return 0;
3572 lua_settable(L, -3);
3573
3574 /* Pop a class stream metatable and affect it to the table. */
3575 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3576 lua_setmetatable(L, -2);
3577
3578 return 1;
3579}
3580
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003581__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3582{
3583 struct hlua_appctx *appctx;
3584 struct stream *s;
3585 const char *name;
3586 size_t len;
3587 struct sample smp;
3588
3589 MAY_LJMP(check_args(L, 3, "set_var"));
3590
3591 /* It is useles to retrieve the stream, but this function
3592 * runs only in a stream context.
3593 */
3594 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3595 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3596 s = appctx->htxn.s;
3597
3598 /* Converts the third argument in a sample. */
3599 hlua_lua2smp(L, 3, &smp);
3600
3601 /* Store the sample in a variable. */
3602 smp_set_owner(&smp, s->be, s->sess, s, 0);
3603 vars_set_by_name(name, len, &smp);
3604 return 0;
3605}
3606
3607__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3608{
3609 struct hlua_appctx *appctx;
3610 struct stream *s;
3611 const char *name;
3612 size_t len;
3613 struct sample smp;
3614
3615 MAY_LJMP(check_args(L, 2, "unset_var"));
3616
3617 /* It is useles to retrieve the stream, but this function
3618 * runs only in a stream context.
3619 */
3620 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3621 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3622 s = appctx->htxn.s;
3623
3624 /* Unset the variable. */
3625 smp_set_owner(&smp, s->be, s->sess, s, 0);
3626 vars_unset_by_name(name, len, &smp);
3627 return 0;
3628}
3629
3630__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3631{
3632 struct hlua_appctx *appctx;
3633 struct stream *s;
3634 const char *name;
3635 size_t len;
3636 struct sample smp;
3637
3638 MAY_LJMP(check_args(L, 2, "get_var"));
3639
3640 /* It is useles to retrieve the stream, but this function
3641 * runs only in a stream context.
3642 */
3643 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3644 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3645 s = appctx->htxn.s;
3646
3647 smp_set_owner(&smp, s->be, s->sess, s, 0);
3648 if (!vars_get_by_name(name, len, &smp)) {
3649 lua_pushnil(L);
3650 return 1;
3651 }
3652
3653 return hlua_smp2lua(L, &smp);
3654}
3655
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003656__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3657{
3658 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3659 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003660 struct hlua *hlua;
3661
3662 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003663 if (!s->hlua)
3664 return 0;
3665 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003666
3667 MAY_LJMP(check_args(L, 2, "set_priv"));
3668
3669 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003670 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003671
3672 /* Get and store new value. */
3673 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3674 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3675
3676 return 0;
3677}
3678
3679__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3680{
3681 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3682 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003683 struct hlua *hlua;
3684
3685 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003686 if (!s->hlua) {
3687 lua_pushnil(L);
3688 return 1;
3689 }
3690 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003691
3692 /* Push configuration index in the stack. */
3693 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3694
3695 return 1;
3696}
3697
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003698/* If expected data not yet available, it returns a yield. This function
3699 * consumes the data in the buffer. It returns a string containing the
3700 * data. This string can be empty.
3701 */
3702__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3703{
3704 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3705 struct stream_interface *si = appctx->appctx->owner;
3706 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003707 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003708 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003709 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003710 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003711
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003712 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003713 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003714
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003715 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003716 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003717 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003718 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003719 }
3720
3721 /* End of data: commit the total strings and return. */
3722 if (ret < 0) {
3723 luaL_pushresult(&appctx->b);
3724 return 1;
3725 }
3726
3727 /* Ensure that the block 2 length is usable. */
3728 if (ret == 1)
3729 len2 = 0;
3730
3731 /* dont check the max length read and dont check. */
3732 luaL_addlstring(&appctx->b, blk1, len1);
3733 luaL_addlstring(&appctx->b, blk2, len2);
3734
3735 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003736 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003737 luaL_pushresult(&appctx->b);
3738 return 1;
3739}
3740
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003741/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003742__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3743{
3744 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3745
3746 /* Initialise the string catenation. */
3747 luaL_buffinit(L, &appctx->b);
3748
3749 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3750}
3751
3752/* If expected data not yet available, it returns a yield. This function
3753 * consumes the data in the buffer. It returns a string containing the
3754 * data. This string can be empty.
3755 */
3756__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3757{
3758 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3759 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003760 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003761 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003762 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003763 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003764 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003765 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003766
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003767 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003768 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003769
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003770 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003771 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003772 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003773 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003774 }
3775
3776 /* End of data: commit the total strings and return. */
3777 if (ret < 0) {
3778 luaL_pushresult(&appctx->b);
3779 return 1;
3780 }
3781
3782 /* Ensure that the block 2 length is usable. */
3783 if (ret == 1)
3784 len2 = 0;
3785
3786 if (len == -1) {
3787
3788 /* If len == -1, catenate all the data avalaile and
3789 * yield because we want to get all the data until
3790 * the end of data stream.
3791 */
3792 luaL_addlstring(&appctx->b, blk1, len1);
3793 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003794 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003795 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003796 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003797
3798 } else {
3799
3800 /* Copy the fisrt block caping to the length required. */
3801 if (len1 > len)
3802 len1 = len;
3803 luaL_addlstring(&appctx->b, blk1, len1);
3804 len -= len1;
3805
3806 /* Copy the second block. */
3807 if (len2 > len)
3808 len2 = len;
3809 luaL_addlstring(&appctx->b, blk2, len2);
3810 len -= len2;
3811
3812 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003813 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003814
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003815 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003816 if (len > 0) {
3817 lua_pushinteger(L, len);
3818 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003819 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003820 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003821 }
3822
3823 /* return the result. */
3824 luaL_pushresult(&appctx->b);
3825 return 1;
3826 }
3827
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003828 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003829 hlua_pusherror(L, "Lua: internal error");
3830 WILL_LJMP(lua_error(L));
3831 return 0;
3832}
3833
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003834/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003835__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3836{
3837 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3838 int len = -1;
3839
3840 if (lua_gettop(L) > 2)
3841 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3842 if (lua_gettop(L) >= 2) {
3843 len = MAY_LJMP(luaL_checkinteger(L, 2));
3844 lua_pop(L, 1);
3845 }
3846
3847 /* Confirm or set the required length */
3848 lua_pushinteger(L, len);
3849
3850 /* Initialise the string catenation. */
3851 luaL_buffinit(L, &appctx->b);
3852
3853 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3854}
3855
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003856/* Append data in the output side of the buffer. This data is immediately
3857 * sent. The function returns the amount of data written. If the buffer
3858 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003859 * if the channel is closed.
3860 */
3861__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3862{
3863 size_t len;
3864 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3865 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3866 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3867 struct stream_interface *si = appctx->appctx->owner;
3868 struct channel *chn = si_ic(si);
3869 int max;
3870
3871 /* Get the max amount of data which can write as input in the channel. */
3872 max = channel_recv_max(chn);
3873 if (max > (len - l))
3874 max = len - l;
3875
3876 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003877 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003878
3879 /* update counters. */
3880 l += max;
3881 lua_pop(L, 1);
3882 lua_pushinteger(L, l);
3883
3884 /* If some data is not send, declares the situation to the
3885 * applet, and returns a yield.
3886 */
3887 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003888 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003889 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003890 }
3891
3892 return 1;
3893}
3894
3895/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3896 * yield the LUA process, and resume it without checking the
3897 * input arguments.
3898 */
3899__LJMP static int hlua_applet_tcp_send(lua_State *L)
3900{
3901 MAY_LJMP(check_args(L, 2, "send"));
3902 lua_pushinteger(L, 0);
3903
3904 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3905}
3906
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003907/*
3908 *
3909 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003910 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003911 *
3912 *
3913 */
3914
3915/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003916 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003917 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003918__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003919{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003920 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003921}
3922
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003923/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003924 * according with a current TXN.
3925 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003926static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003927{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003928 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003929 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003930 struct stream_interface *si = ctx->owner;
3931 struct stream *s = si_strm(si);
3932 struct proxy *px = s->be;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003933
3934 /* Check stack size. */
3935 if (!lua_checkstack(L, 3))
3936 return 0;
3937
3938 /* Create the object: obj[0] = userdata.
3939 * Note that the base of the Converters object is the
3940 * same than the TXN object.
3941 */
3942 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003943 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003944 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003945 appctx->appctx = ctx;
3946 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003947 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003948 appctx->htxn.s = s;
3949 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003950
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003951 /* Create the "f" field that contains a list of fetches. */
3952 lua_pushstring(L, "f");
3953 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3954 return 0;
3955 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003956
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003957 /* Create the "sf" field that contains a list of stringsafe fetches. */
3958 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003959 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003960 return 0;
3961 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003962
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003963 /* Create the "c" field that contains a list of converters. */
3964 lua_pushstring(L, "c");
3965 if (!hlua_converters_new(L, &appctx->htxn, 0))
3966 return 0;
3967 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003968
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003969 /* Create the "sc" field that contains a list of stringsafe converters. */
3970 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003971 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003972 return 0;
3973 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003974
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003975 if (IS_HTX_STRM(s)) {
3976 /* HTX version */
3977 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet29f17582019-05-23 11:03:26 +02003978 struct htx_blk *blk;
3979 struct htx_sl *sl;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003980 struct ist path;
3981 unsigned long long len = 0;
3982 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003983
Christopher Faulet29f17582019-05-23 11:03:26 +02003984 blk = htx_get_first_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +01003985 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Faulet29f17582019-05-23 11:03:26 +02003986 sl = htx_get_blk_ptr(htx, blk);
3987
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003988 /* Stores the request method. */
3989 lua_pushstring(L, "method");
3990 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3991 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003992
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003993 /* Stores the http version. */
3994 lua_pushstring(L, "version");
3995 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3996 lua_settable(L, -3);
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003997
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003998 /* creates an array of headers. hlua_http_get_headers() crates and push
3999 * the array on the top of the stack.
4000 */
4001 lua_pushstring(L, "headers");
4002 htxn.s = s;
4003 htxn.p = px;
4004 htxn.dir = SMP_OPT_DIR_REQ;
4005 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
4006 return 0;
4007 lua_settable(L, -3);
4008
4009 path = http_get_path(htx_sl_req_uri(sl));
4010 if (path.ptr) {
4011 char *p, *q, *end;
4012
4013 p = path.ptr;
4014 end = path.ptr + path.len;
4015 q = p;
4016 while (q < end && *q != '?')
4017 q++;
4018
4019 /* Stores the request path. */
4020 lua_pushstring(L, "path");
4021 lua_pushlstring(L, p, q - p);
4022 lua_settable(L, -3);
4023
4024 /* Stores the query string. */
4025 lua_pushstring(L, "qs");
4026 if (*q == '?')
4027 q++;
4028 lua_pushlstring(L, q, end - q);
4029 lua_settable(L, -3);
4030 }
4031
Christopher Fauleta3f15502019-05-13 15:27:23 +02004032 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004033 struct htx_blk *blk = htx_get_blk(htx, pos);
4034 enum htx_blk_type type = htx_get_blk_type(blk);
4035
Christopher Faulet54b5e212019-06-04 10:08:28 +02004036 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004037 break;
4038 if (type == HTX_BLK_DATA)
4039 len += htx_get_blksz(blk);
4040 }
4041 if (htx->extra != ULLONG_MAX)
4042 len += htx->extra;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004043
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004044 /* Stores the request path. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004045 lua_pushstring(L, "length");
4046 lua_pushinteger(L, len);
4047 lua_settable(L, -3);
4048 }
4049 else {
4050 /* Legacy HTTP version */
4051 struct http_txn *txn = s->txn;
4052 const char *path;
4053 const char *end;
4054 const char *p;
4055
4056 /* Stores the request method. */
4057 lua_pushstring(L, "method");
4058 lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004059 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004060
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004061 /* Stores the http version. */
4062 lua_pushstring(L, "version");
4063 lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004064 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004065
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004066 /* creates an array of headers. hlua_http_get_headers() crates and push
4067 * the array on the top of the stack.
4068 */
4069 lua_pushstring(L, "headers");
4070 htxn.s = s;
4071 htxn.p = px;
4072 htxn.dir = SMP_OPT_DIR_REQ;
4073 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
4074 return 0;
4075 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004076
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004077 /* Get path and qs */
4078 path = http_txn_get_path(txn);
4079 if (path) {
4080 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4081 p = path;
4082 while (p < end && *p != '?')
4083 p++;
4084
4085 /* Stores the request path. */
4086 lua_pushstring(L, "path");
4087 lua_pushlstring(L, path, p - path);
4088 lua_settable(L, -3);
4089
4090 /* Stores the query string. */
4091 lua_pushstring(L, "qs");
4092 if (*p == '?')
4093 p++;
4094 lua_pushlstring(L, p, end - p);
4095 lua_settable(L, -3);
4096 }
4097
4098 /* Stores the request path. */
4099 lua_pushstring(L, "length");
4100 lua_pushinteger(L, txn->req.body_len);
4101 lua_settable(L, -3);
4102 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004103
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004104 /* Create an empty array of HTTP request headers. */
4105 lua_pushstring(L, "response");
4106 lua_newtable(L);
4107 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004108
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004109 /* Pop a class stream metatable and affect it to the table. */
4110 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4111 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004112
4113 return 1;
4114}
4115
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004116__LJMP static int hlua_applet_http_set_var(lua_State *L)
4117{
4118 struct hlua_appctx *appctx;
4119 struct stream *s;
4120 const char *name;
4121 size_t len;
4122 struct sample smp;
4123
4124 MAY_LJMP(check_args(L, 3, "set_var"));
4125
4126 /* It is useles to retrieve the stream, but this function
4127 * runs only in a stream context.
4128 */
4129 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4130 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4131 s = appctx->htxn.s;
4132
4133 /* Converts the third argument in a sample. */
4134 hlua_lua2smp(L, 3, &smp);
4135
4136 /* Store the sample in a variable. */
4137 smp_set_owner(&smp, s->be, s->sess, s, 0);
4138 vars_set_by_name(name, len, &smp);
4139 return 0;
4140}
4141
4142__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4143{
4144 struct hlua_appctx *appctx;
4145 struct stream *s;
4146 const char *name;
4147 size_t len;
4148 struct sample smp;
4149
4150 MAY_LJMP(check_args(L, 2, "unset_var"));
4151
4152 /* It is useles to retrieve the stream, but this function
4153 * runs only in a stream context.
4154 */
4155 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4156 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4157 s = appctx->htxn.s;
4158
4159 /* Unset the variable. */
4160 smp_set_owner(&smp, s->be, s->sess, s, 0);
4161 vars_unset_by_name(name, len, &smp);
4162 return 0;
4163}
4164
4165__LJMP static int hlua_applet_http_get_var(lua_State *L)
4166{
4167 struct hlua_appctx *appctx;
4168 struct stream *s;
4169 const char *name;
4170 size_t len;
4171 struct sample smp;
4172
4173 MAY_LJMP(check_args(L, 2, "get_var"));
4174
4175 /* It is useles to retrieve the stream, but this function
4176 * runs only in a stream context.
4177 */
4178 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4179 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4180 s = appctx->htxn.s;
4181
4182 smp_set_owner(&smp, s->be, s->sess, s, 0);
4183 if (!vars_get_by_name(name, len, &smp)) {
4184 lua_pushnil(L);
4185 return 1;
4186 }
4187
4188 return hlua_smp2lua(L, &smp);
4189}
4190
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004191__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4192{
4193 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4194 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004195 struct hlua *hlua;
4196
4197 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004198 if (!s->hlua)
4199 return 0;
4200 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004201
4202 MAY_LJMP(check_args(L, 2, "set_priv"));
4203
4204 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004205 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004206
4207 /* Get and store new value. */
4208 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4209 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4210
4211 return 0;
4212}
4213
4214__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4215{
4216 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4217 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004218 struct hlua *hlua;
4219
4220 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004221 if (!s->hlua) {
4222 lua_pushnil(L);
4223 return 1;
4224 }
4225 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004226
4227 /* Push configuration index in the stack. */
4228 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4229
4230 return 1;
4231}
4232
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004233/* If expected data not yet available, it returns a yield. This function
4234 * consumes the data in the buffer. It returns a string containing the
4235 * data. This string can be empty.
4236 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004237__LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx)
4238{
4239 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4240 struct stream_interface *si = appctx->appctx->owner;
4241 struct channel *req = si_oc(si);
4242 struct htx *htx;
4243 struct htx_blk *blk;
4244 size_t count;
4245 int stop = 0;
4246
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004247 htx = htx_from_buf(&req->buf);
4248 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004249 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004250
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004251 while (count && !stop && blk) {
4252 enum htx_blk_type type = htx_get_blk_type(blk);
4253 uint32_t sz = htx_get_blksz(blk);
4254 struct ist v;
4255 uint32_t vlen;
4256 char *nl;
4257
Christopher Faulete461e342018-12-18 16:43:35 +01004258 if (type == HTX_BLK_EOM) {
4259 stop = 1;
4260 break;
4261 }
4262
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004263 vlen = sz;
4264 if (vlen > count) {
4265 if (type != HTX_BLK_DATA)
4266 break;
4267 vlen = count;
4268 }
4269
4270 switch (type) {
4271 case HTX_BLK_UNUSED:
4272 break;
4273
4274 case HTX_BLK_DATA:
4275 v = htx_get_blk_value(htx, blk);
4276 v.len = vlen;
4277 nl = istchr(v, '\n');
4278 if (nl != NULL) {
4279 stop = 1;
4280 vlen = nl - v.ptr + 1;
4281 }
4282 luaL_addlstring(&appctx->b, v.ptr, vlen);
4283 break;
4284
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004285 case HTX_BLK_TLR:
4286 case HTX_BLK_EOM:
4287 stop = 1;
4288 break;
4289
4290 default:
4291 break;
4292 }
4293
4294 co_set_data(req, co_data(req) - vlen);
4295 count -= vlen;
4296 if (sz == vlen)
4297 blk = htx_remove_blk(htx, blk);
4298 else {
4299 htx_cut_data_blk(htx, blk, vlen);
4300 break;
4301 }
4302 }
4303
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004304 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004305 if (!stop) {
4306 si_cant_get(si);
4307 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0));
4308 }
4309
4310 /* return the result. */
4311 luaL_pushresult(&appctx->b);
4312 return 1;
4313}
4314
4315
4316/* If expected data not yet available, it returns a yield. This function
4317 * consumes the data in the buffer. It returns a string containing the
4318 * data. This string can be empty.
4319 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004320__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004321{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004322 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4323 struct stream_interface *si = appctx->appctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004324 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004325 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004326 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004327 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004328 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004329
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004330 /* Check for the end of the data. */
4331 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4332 luaL_pushresult(&appctx->b);
4333 return 1;
4334 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004335
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004336 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004337 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004338
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004339 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004340 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004341 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004342 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004343 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004344
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004345 /* End of data: commit the total strings and return. */
4346 if (ret < 0) {
4347 luaL_pushresult(&appctx->b);
4348 return 1;
4349 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004350
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004351 /* Ensure that the block 2 length is usable. */
4352 if (ret == 1)
4353 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004354
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004355 /* Copy the fisrt block caping to the length required. */
4356 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4357 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4358 luaL_addlstring(&appctx->b, blk1, len1);
4359 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004360
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004361 /* Copy the second block. */
4362 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4363 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4364 luaL_addlstring(&appctx->b, blk2, len2);
4365 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004366
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004367 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004368 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004369 luaL_pushresult(&appctx->b);
4370 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004371}
4372
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004373/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004374__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004375{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004376 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004377
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004378 /* Initialise the string catenation. */
4379 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004380
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004381 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
4382 return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0));
4383 else
4384 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004385}
4386
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004387/* If expected data not yet available, it returns a yield. This function
4388 * consumes the data in the buffer. It returns a string containing the
4389 * data. This string can be empty.
4390 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004391__LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx)
4392{
4393 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4394 struct stream_interface *si = appctx->appctx->owner;
4395 struct channel *req = si_oc(si);
4396 struct htx *htx;
4397 struct htx_blk *blk;
4398 size_t count;
4399 int len;
4400
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004401 htx = htx_from_buf(&req->buf);
4402 len = MAY_LJMP(luaL_checkinteger(L, 2));
4403 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004404 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004405 while (count && len && blk) {
4406 enum htx_blk_type type = htx_get_blk_type(blk);
4407 uint32_t sz = htx_get_blksz(blk);
4408 struct ist v;
4409 uint32_t vlen;
4410
Christopher Faulete461e342018-12-18 16:43:35 +01004411 if (type == HTX_BLK_EOM) {
4412 len = 0;
4413 break;
4414 }
4415
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004416 vlen = sz;
4417 if (len > 0 && vlen > len)
4418 vlen = len;
4419 if (vlen > count) {
4420 if (type != HTX_BLK_DATA)
4421 break;
4422 vlen = count;
4423 }
4424
4425 switch (type) {
4426 case HTX_BLK_UNUSED:
4427 break;
4428
4429 case HTX_BLK_DATA:
4430 v = htx_get_blk_value(htx, blk);
4431 luaL_addlstring(&appctx->b, v.ptr, vlen);
4432 break;
4433
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004434 case HTX_BLK_TLR:
4435 case HTX_BLK_EOM:
4436 len = 0;
4437 break;
4438
4439 default:
4440 break;
4441 }
4442
4443 co_set_data(req, co_data(req) - vlen);
4444 count -= vlen;
4445 if (len > 0)
4446 len -= vlen;
4447 if (sz == vlen)
4448 blk = htx_remove_blk(htx, blk);
4449 else {
4450 htx_cut_data_blk(htx, blk, vlen);
4451 break;
4452 }
4453 }
4454
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004455 htx_to_buf(htx, &req->buf);
4456
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004457 /* If we are no other data available, yield waiting for new data. */
4458 if (len) {
4459 if (len > 0) {
4460 lua_pushinteger(L, len);
4461 lua_replace(L, 2);
4462 }
4463 si_cant_get(si);
4464 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0));
4465 }
4466
4467 /* return the result. */
4468 luaL_pushresult(&appctx->b);
4469 return 1;
4470}
4471
4472/* If expected data not yet available, it returns a yield. This function
4473 * consumes the data in the buffer. It returns a string containing the
4474 * data. This string can be empty.
4475 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004476__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004477{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004478 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4479 struct stream_interface *si = appctx->appctx->owner;
4480 int len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004481 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004482 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004483 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004484 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004485 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004486
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004487 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004488 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004489
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004490 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004491 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004492 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004493 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004494 }
4495
4496 /* End of data: commit the total strings and return. */
4497 if (ret < 0) {
4498 luaL_pushresult(&appctx->b);
4499 return 1;
4500 }
4501
4502 /* Ensure that the block 2 length is usable. */
4503 if (ret == 1)
4504 len2 = 0;
4505
4506 /* Copy the fisrt block caping to the length required. */
4507 if (len1 > len)
4508 len1 = len;
4509 luaL_addlstring(&appctx->b, blk1, len1);
4510 len -= len1;
4511
4512 /* Copy the second block. */
4513 if (len2 > len)
4514 len2 = len;
4515 luaL_addlstring(&appctx->b, blk2, len2);
4516 len -= len2;
4517
4518 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004519 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004520 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4521 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4522
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004523 /* If we are no other data available, yield waiting for new data. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004524 if (len > 0) {
4525 lua_pushinteger(L, len);
4526 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004527 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004528 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004529 }
4530
4531 /* return the result. */
4532 luaL_pushresult(&appctx->b);
4533 return 1;
4534}
4535
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004536/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004537__LJMP static int hlua_applet_http_recv(lua_State *L)
4538{
4539 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4540 int len = -1;
4541
4542 /* Check arguments. */
4543 if (lua_gettop(L) > 2)
4544 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4545 if (lua_gettop(L) >= 2) {
4546 len = MAY_LJMP(luaL_checkinteger(L, 2));
4547 lua_pop(L, 1);
4548 }
4549
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004550 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4551 /* HTX version */
4552 lua_pushinteger(L, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004553
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004554 /* Initialise the string catenation. */
4555 luaL_buffinit(L, &appctx->b);
4556
4557 return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0));
4558 }
4559 else {
4560 /* Legacy HTTP version */
4561 /* Check the required length */
4562 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4563 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4564 lua_pushinteger(L, len);
4565
4566 /* Initialise the string catenation. */
4567 luaL_buffinit(L, &appctx->b);
4568
4569 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4570 }
4571}
4572
4573/* Append data in the output side of the buffer. This data is immediately
4574 * sent. The function returns the amount of data written. If the buffer
4575 * cannot contain the data, the function yields. The function returns -1
4576 * if the channel is closed.
4577 */
4578__LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx)
4579{
4580 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4581 struct stream_interface *si = appctx->appctx->owner;
4582 struct channel *res = si_ic(si);
4583 struct htx *htx = htx_from_buf(&res->buf);
4584 const char *data;
4585 size_t len;
4586 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4587 int max;
4588
Christopher Faulet4e963012019-07-03 11:39:30 +02004589 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004590 if (!max)
4591 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004592
4593 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4594
4595 /* Get the max amount of data which can write as input in the channel. */
4596 if (max > (len - l))
4597 max = len - l;
4598
4599 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004600 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004601 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004602
4603 /* update counters. */
4604 l += max;
4605 lua_pop(L, 1);
4606 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004607
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004608 /* If some data is not send, declares the situation to the
4609 * applet, and returns a yield.
4610 */
4611 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004612 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004613 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004614 si_rx_room_blk(si);
4615 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0));
4616 }
4617
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004618 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004619 return 1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004620}
4621
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004622/* Append data in the output side of the buffer. This data is immediately
4623 * sent. The function returns the amount of data written. If the buffer
4624 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004625 * if the channel is closed.
4626 */
4627__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4628{
4629 size_t len;
4630 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4631 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4632 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4633 struct stream_interface *si = appctx->appctx->owner;
4634 struct channel *chn = si_ic(si);
4635 int max;
4636
4637 /* Get the max amount of data which can write as input in the channel. */
4638 max = channel_recv_max(chn);
4639 if (max > (len - l))
4640 max = len - l;
4641
4642 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004643 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004644
4645 /* update counters. */
4646 l += max;
4647 lua_pop(L, 1);
4648 lua_pushinteger(L, l);
4649
4650 /* If some data is not send, declares the situation to the
4651 * applet, and returns a yield.
4652 */
4653 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004654 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004655 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004656 }
4657
4658 return 1;
4659}
4660
4661/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4662 * yield the LUA process, and resume it without checking the
4663 * input arguments.
4664 */
4665__LJMP static int hlua_applet_http_send(lua_State *L)
4666{
4667 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004668
4669 /* We want to send some data. Headers must be sent. */
4670 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4671 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4672 WILL_LJMP(lua_error(L));
4673 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004674
4675 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4676 /* HTX version */
4677 /* This interger is used for followinf the amount of data sent. */
4678 lua_pushinteger(L, 0);
4679
4680 return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0));
4681 }
4682 else {
4683 /* Legacy HTTP version */
4684 size_t len;
4685 char hex[10];
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004686
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004687 MAY_LJMP(luaL_checklstring(L, 2, &len));
4688
4689 /* If transfer encoding chunked is selected, we surround the data
4690 * by chunk data.
4691 */
4692 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4693 snprintf(hex, 9, "%x", (unsigned int)len);
4694 lua_pushfstring(L, "%s\r\n", hex);
4695 lua_insert(L, 2); /* swap the last 2 entries. */
4696 lua_pushstring(L, "\r\n");
4697 lua_concat(L, 3);
4698 }
4699
4700 /* This interger is used for followinf the amount of data sent. */
4701 lua_pushinteger(L, 0);
4702
4703 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4704 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004705}
4706
4707__LJMP static int hlua_applet_http_addheader(lua_State *L)
4708{
4709 const char *name;
4710 int ret;
4711
4712 MAY_LJMP(hlua_checkapplet_http(L, 1));
4713 name = MAY_LJMP(luaL_checkstring(L, 2));
4714 MAY_LJMP(luaL_checkstring(L, 3));
4715
4716 /* Push in the stack the "response" entry. */
4717 ret = lua_getfield(L, 1, "response");
4718 if (ret != LUA_TTABLE) {
4719 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4720 "is expected as an array. %s found", lua_typename(L, ret));
4721 WILL_LJMP(lua_error(L));
4722 }
4723
4724 /* check if the header is already registered if it is not
4725 * the case, register it.
4726 */
4727 ret = lua_getfield(L, -1, name);
4728 if (ret == LUA_TNIL) {
4729
4730 /* Entry not found. */
4731 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4732
4733 /* Insert the new header name in the array in the top of the stack.
4734 * It left the new array in the top of the stack.
4735 */
4736 lua_newtable(L);
4737 lua_pushvalue(L, 2);
4738 lua_pushvalue(L, -2);
4739 lua_settable(L, -4);
4740
4741 } else if (ret != LUA_TTABLE) {
4742
4743 /* corruption error. */
4744 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4745 "is expected as an array. %s found", name, lua_typename(L, ret));
4746 WILL_LJMP(lua_error(L));
4747 }
4748
4749 /* Now the top od thestack is an array of values. We push
4750 * the header value as new entry.
4751 */
4752 lua_pushvalue(L, 3);
4753 ret = lua_rawlen(L, -2);
4754 lua_rawseti(L, -2, ret + 1);
4755 lua_pushboolean(L, 1);
4756 return 1;
4757}
4758
4759__LJMP static int hlua_applet_http_status(lua_State *L)
4760{
4761 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4762 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004763 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004764
4765 if (status < 100 || status > 599) {
4766 lua_pushboolean(L, 0);
4767 return 1;
4768 }
4769
4770 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004771 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004772 lua_pushboolean(L, 1);
4773 return 1;
4774}
4775
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004776
4777__LJMP static int hlua_applet_htx_send_response(lua_State *L)
4778{
4779 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4780 struct stream_interface *si = appctx->appctx->owner;
4781 struct channel *res = si_ic(si);
4782 struct htx *htx;
4783 struct htx_sl *sl;
4784 struct h1m h1m;
4785 const char *status, *reason;
4786 const char *name, *value;
4787 size_t nlen, vlen;
4788 unsigned int flags;
4789
4790 /* Send the message at once. */
4791 htx = htx_from_buf(&res->buf);
4792 h1m_init_res(&h1m);
4793
4794 /* Use the same http version than the request. */
4795 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4796 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4797 if (reason == NULL)
4798 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4799 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4800 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4801 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4802 }
4803 else {
4804 flags = HTX_SL_F_IS_RESP;
4805 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4806 }
4807 if (!sl) {
4808 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4809 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4810 WILL_LJMP(lua_error(L));
4811 }
4812 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4813
4814 /* Get the array associated to the field "response" in the object AppletHTTP. */
4815 lua_pushvalue(L, 0);
4816 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4817 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4818 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4819 WILL_LJMP(lua_error(L));
4820 }
4821
4822 /* Browse the list of headers. */
4823 lua_pushnil(L);
4824 while(lua_next(L, -2) != 0) {
4825 /* We expect a string as -2. */
4826 if (lua_type(L, -2) != LUA_TSTRING) {
4827 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4828 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4829 lua_typename(L, lua_type(L, -2)));
4830 WILL_LJMP(lua_error(L));
4831 }
4832 name = lua_tolstring(L, -2, &nlen);
4833
4834 /* We expect an array as -1. */
4835 if (lua_type(L, -1) != LUA_TTABLE) {
4836 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4837 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4838 name,
4839 lua_typename(L, lua_type(L, -1)));
4840 WILL_LJMP(lua_error(L));
4841 }
4842
4843 /* Browse the table who is on the top of the stack. */
4844 lua_pushnil(L);
4845 while(lua_next(L, -2) != 0) {
4846 int id;
4847
4848 /* We expect a number as -2. */
4849 if (lua_type(L, -2) != LUA_TNUMBER) {
4850 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4851 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4852 name,
4853 lua_typename(L, lua_type(L, -2)));
4854 WILL_LJMP(lua_error(L));
4855 }
4856 id = lua_tointeger(L, -2);
4857
4858 /* We expect a string as -2. */
4859 if (lua_type(L, -1) != LUA_TSTRING) {
4860 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4861 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4862 name, id,
4863 lua_typename(L, lua_type(L, -1)));
4864 WILL_LJMP(lua_error(L));
4865 }
4866 value = lua_tolstring(L, -1, &vlen);
4867
4868 /* Simple Protocol checks. */
4869 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
4870 h1_parse_xfer_enc_header(&h1m, ist2(name, nlen));
4871 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4872 struct ist v = ist2(value, vlen);
4873 int ret;
4874
4875 ret = h1_parse_cont_len_header(&h1m, &v);
4876 if (ret < 0) {
4877 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4878 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4879 name);
4880 WILL_LJMP(lua_error(L));
4881 }
4882 else if (ret == 0)
4883 goto next; /* Skip it */
4884 }
4885
4886 /* Add a new header */
4887 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4888 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4889 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4890 name);
4891 WILL_LJMP(lua_error(L));
4892 }
4893 next:
4894 /* Remove the array from the stack, and get next element with a remaining string. */
4895 lua_pop(L, 1);
4896 }
4897
4898 /* Remove the array from the stack, and get next element with a remaining string. */
4899 lua_pop(L, 1);
4900 }
4901
4902 if (h1m.flags & H1_MF_CHNK)
4903 h1m.flags &= ~H1_MF_CLEN;
4904 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4905 h1m.flags |= H1_MF_XFER_LEN;
4906
4907 /* Uset HTX start-line flags */
4908 if (h1m.flags & H1_MF_XFER_ENC)
4909 flags |= HTX_SL_F_XFER_ENC;
4910 if (h1m.flags & H1_MF_XFER_LEN) {
4911 flags |= HTX_SL_F_XFER_LEN;
4912 if (h1m.flags & H1_MF_CHNK)
4913 flags |= HTX_SL_F_CHNK;
4914 else if (h1m.flags & H1_MF_CLEN)
4915 flags |= HTX_SL_F_CLEN;
4916 if (h1m.body_len == 0)
4917 flags |= HTX_SL_F_BODYLESS;
4918 }
4919 sl->flags |= flags;
4920
4921 /* If we dont have a content-length set, and the HTTP version is 1.1
4922 * and the status code implies the presence of a message body, we must
4923 * announce a transfer encoding chunked. This is required by haproxy
4924 * for the keepalive compliance. If the applet annouces a transfer-encoding
4925 * chunked itslef, don't do anything.
4926 */
4927 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4928 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4929 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4930 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4931 /* Add a new header */
4932 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4933 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4934 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4935 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4936 WILL_LJMP(lua_error(L));
4937 }
4938 }
4939
4940 /* Finalize headers. */
4941 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4942 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4943 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4944 WILL_LJMP(lua_error(L));
4945 }
4946
4947 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4948 b_reset(&res->buf);
4949 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4950 WILL_LJMP(lua_error(L));
4951 }
4952
4953 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004954 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004955
4956 /* Headers sent, set the flag. */
4957 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4958 return 0;
4959
4960}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004961/* We will build the status line and the headers of the HTTP response.
4962 * We will try send at once if its not possible, we give back the hand
4963 * waiting for more room.
4964 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004965__LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4966{
4967 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4968 struct stream_interface *si = appctx->appctx->owner;
4969 struct channel *res = si_ic(si);
4970
4971 if (co_data(res)) {
4972 si_rx_room_blk(si);
4973 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0));
4974 }
4975 return MAY_LJMP(hlua_applet_htx_send_response(L));
4976}
4977
4978
4979__LJMP static int hlua_applet_htx_start_response(lua_State *L)
4980{
4981 return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0));
4982}
4983
4984/* We will build the status line and the headers of the HTTP response.
4985 * We will try send at once if its not possible, we give back the hand
4986 * waiting for more room.
4987 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004988__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4989{
4990 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4991 struct stream_interface *si = appctx->appctx->owner;
4992 struct channel *chn = si_ic(si);
4993 int ret;
4994 size_t len;
4995 const char *msg;
4996
4997 /* Get the message as the first argument on the stack. */
4998 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4999
5000 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02005001 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005002
5003 /* if ret == -2 or -3 the channel closed or the message si too
5004 * big for the buffers.
5005 */
5006 if (ret == -2 || ret == -3) {
5007 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
5008 WILL_LJMP(lua_error(L));
5009 }
5010
5011 /* If ret is -1, we dont have room in the buffer, so we yield. */
5012 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01005013 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02005014 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005015 }
5016
5017 /* Headers sent, set the flag. */
5018 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
5019 return 0;
5020}
5021
5022__LJMP static int hlua_applet_http_start_response(lua_State *L)
5023{
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005024 struct buffer *tmp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005025 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005026 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02005027 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005028 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02005029 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005030 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005031 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005032 int hdr_chunked = 0;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005033 const char *reason;
5034
5035 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
5036 return MAY_LJMP(hlua_applet_htx_start_response(L));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005037
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005038 reason = appctx->appctx->ctx.hlua_apphttp.reason;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005039 if (reason == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02005040 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005041
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005042 tmp = get_trash_chunk();
5043
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005044 /* Use the same http version than the request. */
5045 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01005046 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005047 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005048 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005049
5050 /* Get the array associated to the field "response" in the object AppletHTTP. */
5051 lua_pushvalue(L, 0);
5052 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
5053 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
5054 appctx->appctx->rule->arg.hlua_rule->fcn.name);
5055 WILL_LJMP(lua_error(L));
5056 }
5057
5058 /* Browse the list of headers. */
5059 lua_pushnil(L);
5060 while(lua_next(L, -2) != 0) {
5061
5062 /* We expect a string as -2. */
5063 if (lua_type(L, -2) != LUA_TSTRING) {
5064 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
5065 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5066 lua_typename(L, lua_type(L, -2)));
5067 WILL_LJMP(lua_error(L));
5068 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005069 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005070
5071 /* We expect an array as -1. */
5072 if (lua_type(L, -1) != LUA_TTABLE) {
5073 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
5074 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5075 name,
5076 lua_typename(L, lua_type(L, -1)));
5077 WILL_LJMP(lua_error(L));
5078 }
5079
5080 /* Browse the table who is on the top of the stack. */
5081 lua_pushnil(L);
5082 while(lua_next(L, -2) != 0) {
5083
5084 /* We expect a number as -2. */
5085 if (lua_type(L, -2) != LUA_TNUMBER) {
5086 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
5087 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5088 name,
5089 lua_typename(L, lua_type(L, -2)));
5090 WILL_LJMP(lua_error(L));
5091 }
5092 id = lua_tointeger(L, -2);
5093
5094 /* We expect a string as -2. */
5095 if (lua_type(L, -1) != LUA_TSTRING) {
5096 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
5097 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5098 name, id,
5099 lua_typename(L, lua_type(L, -1)));
5100 WILL_LJMP(lua_error(L));
5101 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005102 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005103
5104 /* Catenate a new header. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005105 if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) {
5106 memcpy(tmp->area + tmp->data, name, name_len);
5107 tmp->data += name_len;
5108 tmp->area[tmp->data++] = ':';
5109 tmp->area[tmp->data++] = ' ';
Willy Tarreaua3294632017-08-23 11:24:47 +02005110
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005111 memcpy(tmp->area + tmp->data, value,
5112 value_len);
5113 tmp->data += value_len;
5114 tmp->area[tmp->data++] = '\r';
5115 tmp->area[tmp->data++] = '\n';
Willy Tarreaua3294632017-08-23 11:24:47 +02005116 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005117
5118 /* Protocol checks. */
5119
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005120 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005121 * is done without control. If it contains a bad value,
5122 * the content-length remains negative so that we can
5123 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005124 */
Willy Tarreaua3294632017-08-23 11:24:47 +02005125 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005126 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005127
5128 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02005129 if (name_len == 17 && value_len == 7 &&
5130 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005131 strcasecmp("chunked", value) == 0)
5132 hdr_chunked = 1;
5133
5134 /* Remove the array from the stack, and get next element with a remaining string. */
5135 lua_pop(L, 1);
5136 }
5137
5138 /* Remove the array from the stack, and get next element with a remaining string. */
5139 lua_pop(L, 1);
5140 }
5141
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005142 /* If we dont have a content-length set, and the HTTP version is 1.1
5143 * and the status code implies the presence of a message body, we must
5144 * announce a transfer encoding chunked. This is required by haproxy
5145 * for the keepalive compliance. If the applet annouces a transfer-encoding
5146 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005147 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005148 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005149 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
5150 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
5151 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
5152 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005153 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
5154 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
5155 }
5156
5157 /* Finalize headers. */
5158 chunk_appendf(tmp, "\r\n");
5159
5160 /* Remove the last entry and the array of headers */
5161 lua_pop(L, 2);
5162
5163 /* Push the headers block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005164 lua_pushlstring(L, tmp->area, tmp->data);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005165
5166 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
5167}
5168
5169/*
5170 *
5171 *
5172 * Class HTTP
5173 *
5174 *
5175 */
5176
5177/* Returns a struct hlua_txn if the stack entry "ud" is
5178 * a class stream, otherwise it throws an error.
5179 */
5180__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
5181{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005182 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005183}
5184
5185/* This function creates and push in the stack a HTTP object
5186 * according with a current TXN.
5187 */
5188static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
5189{
5190 struct hlua_txn *htxn;
5191
5192 /* Check stack size. */
5193 if (!lua_checkstack(L, 3))
5194 return 0;
5195
5196 /* Create the object: obj[0] = userdata.
5197 * Note that the base of the Converters object is the
5198 * same than the TXN object.
5199 */
5200 lua_newtable(L);
5201 htxn = lua_newuserdata(L, sizeof(*htxn));
5202 lua_rawseti(L, -2, 0);
5203
5204 htxn->s = txn->s;
5205 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02005206 htxn->dir = txn->dir;
5207 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005208
5209 /* Pop a class stream metatable and affect it to the table. */
5210 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5211 lua_setmetatable(L, -2);
5212
5213 return 1;
5214}
5215
5216/* This function creates ans returns an array of HTTP headers.
5217 * This function does not fails. It is used as wrapper with the
5218 * 2 following functions.
5219 */
5220__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5221{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005222 /* Create the table. */
5223 lua_newtable(L);
5224
5225 if (!htxn->s->txn)
5226 return 1;
5227
Christopher Faulet724a12c2018-12-13 22:12:15 +01005228 if (IS_HTX_STRM(htxn->s)) {
5229 /* HTX version */
5230 struct htx *htx = htxbuf(&msg->chn->buf);
5231 int32_t pos;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005232
Christopher Fauleta3f15502019-05-13 15:27:23 +02005233 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet724a12c2018-12-13 22:12:15 +01005234 struct htx_blk *blk = htx_get_blk(htx, pos);
5235 enum htx_blk_type type = htx_get_blk_type(blk);
5236 struct ist n, v;
5237 int len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005238
Christopher Faulet724a12c2018-12-13 22:12:15 +01005239 if (type == HTX_BLK_HDR) {
5240 n = htx_get_blk_name(htx,blk);
5241 v = htx_get_blk_value(htx, blk);
5242 }
5243 else if (type == HTX_BLK_EOH)
5244 break;
5245 else
5246 continue;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005247
Christopher Faulet724a12c2018-12-13 22:12:15 +01005248 /* Check for existing entry:
5249 * assume that the table is on the top of the stack, and
5250 * push the key in the stack, the function lua_gettable()
5251 * perform the lookup.
5252 */
5253 lua_pushlstring(L, n.ptr, n.len);
5254 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005255
Christopher Faulet724a12c2018-12-13 22:12:15 +01005256 switch (lua_type(L, -1)) {
5257 case LUA_TNIL:
5258 /* Table not found, create it. */
5259 lua_pop(L, 1); /* remove the nil value. */
5260 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5261 lua_newtable(L); /* create and push empty table. */
5262 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5263 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5264 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5265 break;
5266
5267 case LUA_TTABLE:
5268 /* Entry found: push the value in the table. */
5269 len = lua_rawlen(L, -1);
5270 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5271 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5272 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5273 break;
5274
5275 default:
5276 /* Other cases are errors. */
5277 hlua_pusherror(L, "internal error during the parsing of headers.");
5278 WILL_LJMP(lua_error(L));
5279 }
5280 }
5281 }
5282 else {
5283 /* Legacy HTTP version */
5284 const char *cur_ptr, *cur_next, *p;
5285 int old_idx, cur_idx;
5286 struct hdr_idx_elem *cur_hdr;
5287 const char *hn, *hv;
5288 int hnl, hvl;
5289 const char *in;
5290 char *out;
5291 int len;
5292
5293 /* Build array of headers. */
5294 old_idx = 0;
5295 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
5296
5297 while (1) {
5298 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
5299 if (!cur_idx)
5300 break;
5301 old_idx = cur_idx;
5302
5303 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
5304 cur_ptr = cur_next;
5305 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
5306
5307 /* Now we have one full header at cur_ptr of len cur_hdr->len,
5308 * and the next header starts at cur_next. We'll check
5309 * this header in the list as well as against the default
5310 * rule.
5311 */
5312
5313 /* look for ': *'. */
5314 hn = cur_ptr;
5315 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
5316 if (p >= cur_ptr+cur_hdr->len)
5317 continue;
5318 hnl = p - hn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005319 p++;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005320 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
5321 p++;
5322 if (p >= cur_ptr+cur_hdr->len)
5323 continue;
5324 hv = p;
5325 hvl = cur_ptr+cur_hdr->len-p;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005326
Christopher Faulet724a12c2018-12-13 22:12:15 +01005327 /* Lowercase the key. Don't check the size of trash, it have
5328 * the size of one buffer and the input data contains in one
5329 * buffer.
5330 */
5331 out = trash.area;
5332 for (in=hn; in<hn+hnl; in++, out++)
5333 *out = tolower(*in);
5334 *out = '\0';
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005335
Christopher Faulet724a12c2018-12-13 22:12:15 +01005336 /* Check for existing entry:
5337 * assume that the table is on the top of the stack, and
5338 * push the key in the stack, the function lua_gettable()
5339 * perform the lookup.
5340 */
5341 lua_pushlstring(L, trash.area, hnl);
5342 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005343
Christopher Faulet724a12c2018-12-13 22:12:15 +01005344 switch (lua_type(L, -1)) {
5345 case LUA_TNIL:
5346 /* Table not found, create it. */
5347 lua_pop(L, 1); /* remove the nil value. */
5348 lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */
5349 lua_newtable(L); /* create and push empty table. */
5350 lua_pushlstring(L, hv, hvl); /* push header value. */
5351 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5352 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5353 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005354
Christopher Faulet724a12c2018-12-13 22:12:15 +01005355 case LUA_TTABLE:
5356 /* Entry found: push the value in the table. */
5357 len = lua_rawlen(L, -1);
5358 lua_pushlstring(L, hv, hvl); /* push header value. */
5359 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5360 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5361 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005362
Christopher Faulet724a12c2018-12-13 22:12:15 +01005363 default:
5364 /* Other cases are errors. */
5365 hlua_pusherror(L, "internal error during the parsing of headers.");
5366 WILL_LJMP(lua_error(L));
5367 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005368 }
5369 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005370 return 1;
5371}
5372
5373__LJMP static int hlua_http_req_get_headers(lua_State *L)
5374{
5375 struct hlua_txn *htxn;
5376
5377 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5378 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5379
Christopher Faulet2351ca22019-07-26 16:31:34 +02005380 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005381 WILL_LJMP(lua_error(L));
5382
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005383 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
5384}
5385
5386__LJMP static int hlua_http_res_get_headers(lua_State *L)
5387{
5388 struct hlua_txn *htxn;
5389
5390 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5391 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5392
Christopher Faulet2351ca22019-07-26 16:31:34 +02005393 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005394 WILL_LJMP(lua_error(L));
5395
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005396 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
5397}
5398
5399/* This function replace full header, or just a value in
5400 * the request or in the response. It is a wrapper fir the
5401 * 4 following functions.
5402 */
5403__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
5404 struct http_msg *msg, int action)
5405{
5406 size_t name_len;
5407 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5408 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5409 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Dragan Dosen26743032019-04-30 15:54:36 +02005410 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005411
Dragan Dosen26743032019-04-30 15:54:36 +02005412 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005413 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5414
Christopher Faulet5a8549e2019-06-17 13:36:06 +02005415 if (IS_HTX_STRM(htxn->s)) {
5416 struct htx *htx = htxbuf(&msg->chn->buf);
5417
5418 htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action);
5419 }
5420 else
5421 http_transform_header_str(htxn->s, msg, name, name_len, value, re, action);
Dragan Dosen26743032019-04-30 15:54:36 +02005422 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005423 return 0;
5424}
5425
5426__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5427{
5428 struct hlua_txn *htxn;
5429
5430 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5431 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5432
Christopher Faulet2351ca22019-07-26 16:31:34 +02005433 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005434 WILL_LJMP(lua_error(L));
5435
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005436 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
5437}
5438
5439__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5440{
5441 struct hlua_txn *htxn;
5442
5443 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5444 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5445
Christopher Faulet2351ca22019-07-26 16:31:34 +02005446 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005447 WILL_LJMP(lua_error(L));
5448
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005449 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
5450}
5451
5452__LJMP static int hlua_http_req_rep_val(lua_State *L)
5453{
5454 struct hlua_txn *htxn;
5455
5456 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5457 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5458
Christopher Faulet2351ca22019-07-26 16:31:34 +02005459 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005460 WILL_LJMP(lua_error(L));
5461
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005462 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
5463}
5464
5465__LJMP static int hlua_http_res_rep_val(lua_State *L)
5466{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005467 struct hlua_txn *htxn;
5468
5469 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5470 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5471
Christopher Faulet2351ca22019-07-26 16:31:34 +02005472 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005473 WILL_LJMP(lua_error(L));
5474
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02005475 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005476}
5477
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005478/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005479 * It is a wrapper for the 2 following functions.
5480 */
5481__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5482{
5483 size_t len;
5484 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005485
Christopher Faulet724a12c2018-12-13 22:12:15 +01005486 if (IS_HTX_STRM(htxn->s)) {
5487 /* HTX version */
5488 struct htx *htx = htxbuf(&msg->chn->buf);
5489 struct http_hdr_ctx ctx;
5490
5491 ctx.blk = NULL;
5492 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5493 http_remove_header(htx, &ctx);
5494 }
5495 else {
5496 /* Legacy HTTP version */
5497 struct hdr_ctx ctx;
5498 struct http_txn *txn = htxn->s->txn;
5499
5500 ctx.idx = 0;
5501 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
5502 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5503 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005504 return 0;
5505}
5506
5507__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5508{
5509 struct hlua_txn *htxn;
5510
5511 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5512 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5513
Christopher Faulet2351ca22019-07-26 16:31:34 +02005514 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005515 WILL_LJMP(lua_error(L));
5516
Willy Tarreaueee5b512015-04-03 23:46:31 +02005517 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005518}
5519
5520__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5521{
5522 struct hlua_txn *htxn;
5523
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005524 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005525 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5526
Christopher Faulet2351ca22019-07-26 16:31:34 +02005527 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005528 WILL_LJMP(lua_error(L));
5529
Willy Tarreaueee5b512015-04-03 23:46:31 +02005530 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005531}
5532
5533/* This function adds an header. It is a wrapper used by
5534 * the 2 following functions.
5535 */
5536__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5537{
5538 size_t name_len;
5539 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5540 size_t value_len;
5541 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
5542 char *p;
5543
Christopher Faulet724a12c2018-12-13 22:12:15 +01005544 if (IS_HTX_STRM(htxn->s)) {
5545 /* HTX version */
5546 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005547
Christopher Faulet724a12c2018-12-13 22:12:15 +01005548 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5549 ist2(value, value_len)));
5550 }
5551 else {
5552 /* Legacy HTTP version */
5553 /* Check length. */
5554 trash.data = value_len + name_len + 2;
5555 if (trash.data > trash.size)
5556 return 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005557
Christopher Faulet724a12c2018-12-13 22:12:15 +01005558 /* Creates the header string. */
5559 p = trash.area;
5560 memcpy(p, name, name_len);
5561 p += name_len;
5562 *p = ':';
5563 p++;
5564 *p = ' ';
5565 p++;
5566 memcpy(p, value, value_len);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005567
Christopher Faulet724a12c2018-12-13 22:12:15 +01005568 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
5569 trash.area, trash.data) != 0);
5570 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005571 return 0;
5572}
5573
5574__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5575{
5576 struct hlua_txn *htxn;
5577
5578 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5579 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5580
Christopher Faulet2351ca22019-07-26 16:31:34 +02005581 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005582 WILL_LJMP(lua_error(L));
5583
Willy Tarreaueee5b512015-04-03 23:46:31 +02005584 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005585}
5586
5587__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5588{
5589 struct hlua_txn *htxn;
5590
5591 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5592 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5593
Christopher Faulet2351ca22019-07-26 16:31:34 +02005594 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005595 WILL_LJMP(lua_error(L));
5596
Willy Tarreaueee5b512015-04-03 23:46:31 +02005597 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005598}
5599
5600static int hlua_http_req_set_hdr(lua_State *L)
5601{
5602 struct hlua_txn *htxn;
5603
5604 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5605 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5606
Christopher Faulet2351ca22019-07-26 16:31:34 +02005607 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005608 WILL_LJMP(lua_error(L));
5609
Willy Tarreaueee5b512015-04-03 23:46:31 +02005610 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
5611 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005612}
5613
5614static int hlua_http_res_set_hdr(lua_State *L)
5615{
5616 struct hlua_txn *htxn;
5617
5618 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5619 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5620
Christopher Faulet2351ca22019-07-26 16:31:34 +02005621 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005622 WILL_LJMP(lua_error(L));
5623
Willy Tarreaueee5b512015-04-03 23:46:31 +02005624 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
5625 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005626}
5627
5628/* This function set the method. */
5629static int hlua_http_req_set_meth(lua_State *L)
5630{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005631 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005632 size_t name_len;
5633 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005634
Christopher Faulet2351ca22019-07-26 16:31:34 +02005635 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005636 WILL_LJMP(lua_error(L));
5637
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005638 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005639 return 1;
5640}
5641
5642/* This function set the method. */
5643static int hlua_http_req_set_path(lua_State *L)
5644{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005645 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005646 size_t name_len;
5647 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005648
Christopher Faulet2351ca22019-07-26 16:31:34 +02005649 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005650 WILL_LJMP(lua_error(L));
5651
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005652 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005653 return 1;
5654}
5655
5656/* This function set the query-string. */
5657static int hlua_http_req_set_query(lua_State *L)
5658{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005659 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005660 size_t name_len;
5661 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005662
Christopher Faulet2351ca22019-07-26 16:31:34 +02005663 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005664 WILL_LJMP(lua_error(L));
5665
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005666 /* Check length. */
5667 if (name_len > trash.size - 1) {
5668 lua_pushboolean(L, 0);
5669 return 1;
5670 }
5671
5672 /* Add the mark question as prefix. */
5673 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005674 trash.area[trash.data++] = '?';
5675 memcpy(trash.area + trash.data, name, name_len);
5676 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005677
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005678 lua_pushboolean(L,
5679 http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005680 return 1;
5681}
5682
5683/* This function set the uri. */
5684static int hlua_http_req_set_uri(lua_State *L)
5685{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005686 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005687 size_t name_len;
5688 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005689
Christopher Faulet2351ca22019-07-26 16:31:34 +02005690 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005691 WILL_LJMP(lua_error(L));
5692
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005693 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005694 return 1;
5695}
5696
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005697/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005698static int hlua_http_res_set_status(lua_State *L)
5699{
5700 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5701 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005702 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005703
Christopher Faulet2351ca22019-07-26 16:31:34 +02005704 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005705 WILL_LJMP(lua_error(L));
5706
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005707 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005708 return 0;
5709}
5710
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005711/*
5712 *
5713 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005714 * Class TXN
5715 *
5716 *
5717 */
5718
5719/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005720 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005721 */
5722__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5723{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005724 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005725}
5726
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005727__LJMP static int hlua_set_var(lua_State *L)
5728{
5729 struct hlua_txn *htxn;
5730 const char *name;
5731 size_t len;
5732 struct sample smp;
5733
5734 MAY_LJMP(check_args(L, 3, "set_var"));
5735
5736 /* It is useles to retrieve the stream, but this function
5737 * runs only in a stream context.
5738 */
5739 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5740 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5741
5742 /* Converts the third argument in a sample. */
5743 hlua_lua2smp(L, 3, &smp);
5744
5745 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005746 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005747 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005748 return 0;
5749}
5750
Christopher Faulet85d79c92016-11-09 16:54:56 +01005751__LJMP static int hlua_unset_var(lua_State *L)
5752{
5753 struct hlua_txn *htxn;
5754 const char *name;
5755 size_t len;
5756 struct sample smp;
5757
5758 MAY_LJMP(check_args(L, 2, "unset_var"));
5759
5760 /* It is useles to retrieve the stream, but this function
5761 * runs only in a stream context.
5762 */
5763 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5764 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5765
5766 /* Unset the variable. */
5767 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5768 vars_unset_by_name(name, len, &smp);
5769 return 0;
5770}
5771
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005772__LJMP static int hlua_get_var(lua_State *L)
5773{
5774 struct hlua_txn *htxn;
5775 const char *name;
5776 size_t len;
5777 struct sample smp;
5778
5779 MAY_LJMP(check_args(L, 2, "get_var"));
5780
5781 /* It is useles to retrieve the stream, but this function
5782 * runs only in a stream context.
5783 */
5784 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5785 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5786
Willy Tarreau7560dd42016-03-10 16:28:58 +01005787 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005788 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005789 lua_pushnil(L);
5790 return 1;
5791 }
5792
5793 return hlua_smp2lua(L, &smp);
5794}
5795
Willy Tarreau59551662015-03-10 14:23:13 +01005796__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005797{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005798 struct hlua *hlua;
5799
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005800 MAY_LJMP(check_args(L, 2, "set_priv"));
5801
Willy Tarreau87b09662015-04-03 00:22:06 +02005802 /* It is useles to retrieve the stream, but this function
5803 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005804 */
5805 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005806 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005807
5808 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005809 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005810
5811 /* Get and store new value. */
5812 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5813 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5814
5815 return 0;
5816}
5817
Willy Tarreau59551662015-03-10 14:23:13 +01005818__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005819{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005820 struct hlua *hlua;
5821
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005822 MAY_LJMP(check_args(L, 1, "get_priv"));
5823
Willy Tarreau87b09662015-04-03 00:22:06 +02005824 /* It is useles to retrieve the stream, but this function
5825 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005826 */
5827 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005828 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005829
5830 /* Push configuration index in the stack. */
5831 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5832
5833 return 1;
5834}
5835
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005836/* Create stack entry containing a class TXN. This function
5837 * return 0 if the stack does not contains free slots,
5838 * otherwise it returns 1.
5839 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005840static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005841{
Willy Tarreaude491382015-04-06 11:04:28 +02005842 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005843
5844 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005845 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005846 return 0;
5847
5848 /* NOTE: The allocation never fails. The failure
5849 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005850 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005851 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005852 /* Create the object: obj[0] = userdata. */
5853 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005854 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005855 lua_rawseti(L, -2, 0);
5856
Willy Tarreaude491382015-04-06 11:04:28 +02005857 htxn->s = s;
5858 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005859 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005860 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005861
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005862 /* Create the "f" field that contains a list of fetches. */
5863 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005864 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005865 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005866 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005867
5868 /* Create the "sf" field that contains a list of stringsafe fetches. */
5869 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005870 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005871 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005872 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005873
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005874 /* Create the "c" field that contains a list of converters. */
5875 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005876 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005877 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005878 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005879
5880 /* Create the "sc" field that contains a list of stringsafe converters. */
5881 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005882 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005883 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005884 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005885
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005886 /* Create the "req" field that contains the request channel object. */
5887 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005888 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005889 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005890 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005891
5892 /* Create the "res" field that contains the response channel object. */
5893 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005894 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005895 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005896 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005897
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005898 /* Creates the HTTP object is the current proxy allows http. */
5899 lua_pushstring(L, "http");
5900 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005901 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005902 return 0;
5903 }
5904 else
5905 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005906 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005907
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005908 /* Pop a class sesison metatable and affect it to the userdata. */
5909 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5910 lua_setmetatable(L, -2);
5911
5912 return 1;
5913}
5914
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005915__LJMP static int hlua_txn_deflog(lua_State *L)
5916{
5917 const char *msg;
5918 struct hlua_txn *htxn;
5919
5920 MAY_LJMP(check_args(L, 2, "deflog"));
5921 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5922 msg = MAY_LJMP(luaL_checkstring(L, 2));
5923
5924 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5925 return 0;
5926}
5927
5928__LJMP static int hlua_txn_log(lua_State *L)
5929{
5930 int level;
5931 const char *msg;
5932 struct hlua_txn *htxn;
5933
5934 MAY_LJMP(check_args(L, 3, "log"));
5935 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5936 level = MAY_LJMP(luaL_checkinteger(L, 2));
5937 msg = MAY_LJMP(luaL_checkstring(L, 3));
5938
5939 if (level < 0 || level >= NB_LOG_LEVELS)
5940 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5941
5942 hlua_sendlog(htxn->s->be, level, msg);
5943 return 0;
5944}
5945
5946__LJMP static int hlua_txn_log_debug(lua_State *L)
5947{
5948 const char *msg;
5949 struct hlua_txn *htxn;
5950
5951 MAY_LJMP(check_args(L, 2, "Debug"));
5952 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5953 msg = MAY_LJMP(luaL_checkstring(L, 2));
5954 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5955 return 0;
5956}
5957
5958__LJMP static int hlua_txn_log_info(lua_State *L)
5959{
5960 const char *msg;
5961 struct hlua_txn *htxn;
5962
5963 MAY_LJMP(check_args(L, 2, "Info"));
5964 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5965 msg = MAY_LJMP(luaL_checkstring(L, 2));
5966 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5967 return 0;
5968}
5969
5970__LJMP static int hlua_txn_log_warning(lua_State *L)
5971{
5972 const char *msg;
5973 struct hlua_txn *htxn;
5974
5975 MAY_LJMP(check_args(L, 2, "Warning"));
5976 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5977 msg = MAY_LJMP(luaL_checkstring(L, 2));
5978 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5979 return 0;
5980}
5981
5982__LJMP static int hlua_txn_log_alert(lua_State *L)
5983{
5984 const char *msg;
5985 struct hlua_txn *htxn;
5986
5987 MAY_LJMP(check_args(L, 2, "Alert"));
5988 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5989 msg = MAY_LJMP(luaL_checkstring(L, 2));
5990 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5991 return 0;
5992}
5993
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005994__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5995{
5996 struct hlua_txn *htxn;
5997 int ll;
5998
5999 MAY_LJMP(check_args(L, 2, "set_loglevel"));
6000 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6001 ll = MAY_LJMP(luaL_checkinteger(L, 2));
6002
6003 if (ll < 0 || ll > 7)
6004 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
6005
6006 htxn->s->logs.level = ll;
6007 return 0;
6008}
6009
6010__LJMP static int hlua_txn_set_tos(lua_State *L)
6011{
6012 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006013 int tos;
6014
6015 MAY_LJMP(check_args(L, 2, "set_tos"));
6016 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6017 tos = MAY_LJMP(luaL_checkinteger(L, 2));
6018
Willy Tarreau1a18b542018-12-11 16:37:42 +01006019 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006020 return 0;
6021}
6022
6023__LJMP static int hlua_txn_set_mark(lua_State *L)
6024{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006025 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006026 int mark;
6027
6028 MAY_LJMP(check_args(L, 2, "set_mark"));
6029 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6030 mark = MAY_LJMP(luaL_checkinteger(L, 2));
6031
Lukas Tribusb59291a2019-08-11 18:03:45 +02006032 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006033 return 0;
6034}
6035
Patrick Hemmer268a7072018-05-11 12:52:31 -04006036__LJMP static int hlua_txn_set_priority_class(lua_State *L)
6037{
6038 struct hlua_txn *htxn;
6039
6040 MAY_LJMP(check_args(L, 2, "set_priority_class"));
6041 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6042 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
6043 return 0;
6044}
6045
6046__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
6047{
6048 struct hlua_txn *htxn;
6049
6050 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
6051 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6052 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
6053 return 0;
6054}
6055
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006056/* This function is an Lua binding that send pending data
6057 * to the client, and close the stream interface.
6058 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006059__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006060{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006061 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006062 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01006063 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006064
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006065 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006066 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006067 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006068
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006069 /* If the flags NOTERM is set, we cannot terminate the http
6070 * session, so we just end the execution of the current
6071 * lua code.
6072 */
6073 if (htxn->flags & HLUA_TXN_NOTERM) {
6074 WILL_LJMP(hlua_done(L));
6075 return 0;
6076 }
6077
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006078 ic = &htxn->s->req;
6079 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01006080
Christopher Faulet72c69272019-07-26 16:40:24 +02006081 if (IS_HTX_STRM(htxn->s)) {
6082 htxn->s->txn->status = 0;
6083 http_reply_and_close(htxn->s, 0, NULL);
6084 ic->analysers &= AN_REQ_FLT_END;
6085 oc->analysers &= AN_RES_FLT_END;
6086 }
Christopher Fauletb58fb792019-07-16 10:52:40 +02006087 else {
6088 if (htxn->s->txn) {
6089 /* HTTP mode, let's stay in sync with the stream */
6090 b_del(&ic->buf, htxn->s->txn->req.sov);
6091 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
6092 htxn->s->txn->req.sov = 0;
6093
6094 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
6095 oc->analysers = AN_RES_HTTP_XFER_BODY;
6096 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
6097 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreau630ef452015-08-28 10:06:15 +02006098
Willy Tarreau630ef452015-08-28 10:06:15 +02006099 /* Note that if we want to support keep-alive, we need
6100 * to bypass the close/shutr_now calls below, but that
6101 * may only be done if the HTTP request was already
6102 * processed and the connection header is known (ie
6103 * not during TCP rules).
6104 */
Christopher Fauletb58fb792019-07-16 10:52:40 +02006105 }
Willy Tarreau630ef452015-08-28 10:06:15 +02006106
Christopher Fauletb58fb792019-07-16 10:52:40 +02006107 channel_auto_read(ic);
6108 channel_abort(ic);
6109 channel_auto_close(ic);
6110 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02006111
Christopher Fauletb58fb792019-07-16 10:52:40 +02006112 oc->wex = tick_add_ifset(now_ms, oc->wto);
6113 channel_auto_read(oc);
6114 channel_auto_close(oc);
6115 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006116
Christopher Fauletb58fb792019-07-16 10:52:40 +02006117 ic->analysers = 0;
6118 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006119
Christopher Faulet72c69272019-07-26 16:40:24 +02006120 if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is
6121 htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
6122
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006123 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006124 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006125 return 0;
6126}
6127
6128__LJMP static int hlua_log(lua_State *L)
6129{
6130 int level;
6131 const char *msg;
6132
6133 MAY_LJMP(check_args(L, 2, "log"));
6134 level = MAY_LJMP(luaL_checkinteger(L, 1));
6135 msg = MAY_LJMP(luaL_checkstring(L, 2));
6136
6137 if (level < 0 || level >= NB_LOG_LEVELS)
6138 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6139
6140 hlua_sendlog(NULL, level, msg);
6141 return 0;
6142}
6143
6144__LJMP static int hlua_log_debug(lua_State *L)
6145{
6146 const char *msg;
6147
6148 MAY_LJMP(check_args(L, 1, "debug"));
6149 msg = MAY_LJMP(luaL_checkstring(L, 1));
6150 hlua_sendlog(NULL, LOG_DEBUG, msg);
6151 return 0;
6152}
6153
6154__LJMP static int hlua_log_info(lua_State *L)
6155{
6156 const char *msg;
6157
6158 MAY_LJMP(check_args(L, 1, "info"));
6159 msg = MAY_LJMP(luaL_checkstring(L, 1));
6160 hlua_sendlog(NULL, LOG_INFO, msg);
6161 return 0;
6162}
6163
6164__LJMP static int hlua_log_warning(lua_State *L)
6165{
6166 const char *msg;
6167
6168 MAY_LJMP(check_args(L, 1, "warning"));
6169 msg = MAY_LJMP(luaL_checkstring(L, 1));
6170 hlua_sendlog(NULL, LOG_WARNING, msg);
6171 return 0;
6172}
6173
6174__LJMP static int hlua_log_alert(lua_State *L)
6175{
6176 const char *msg;
6177
6178 MAY_LJMP(check_args(L, 1, "alert"));
6179 msg = MAY_LJMP(luaL_checkstring(L, 1));
6180 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006181 return 0;
6182}
6183
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006184__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006185{
6186 int wakeup_ms = lua_tointeger(L, -1);
6187 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006188 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006189 return 0;
6190}
6191
6192__LJMP static int hlua_sleep(lua_State *L)
6193{
6194 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006195 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006196
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006197 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006198
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006199 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006200 wakeup_ms = tick_add(now_ms, delay);
6201 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006202
Willy Tarreau9635e032018-10-16 17:52:55 +02006203 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006204 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006205}
6206
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006207__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006208{
6209 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006210 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006211
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006212 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006213
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006214 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006215 wakeup_ms = tick_add(now_ms, delay);
6216 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006217
Willy Tarreau9635e032018-10-16 17:52:55 +02006218 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006219 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006220}
6221
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006222/* This functionis an LUA binding. it permits to give back
6223 * the hand at the HAProxy scheduler. It is used when the
6224 * LUA processing consumes a lot of time.
6225 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006226__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006227{
6228 return 0;
6229}
6230
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006231__LJMP static int hlua_yield(lua_State *L)
6232{
Willy Tarreau9635e032018-10-16 17:52:55 +02006233 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006234 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006235}
6236
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006237/* This function change the nice of the currently executed
6238 * task. It is used set low or high priority at the current
6239 * task.
6240 */
Willy Tarreau59551662015-03-10 14:23:13 +01006241__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006242{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006243 struct hlua *hlua;
6244 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006245
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006246 MAY_LJMP(check_args(L, 1, "set_nice"));
6247 hlua = hlua_gethlua(L);
6248 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006249
6250 /* If he task is not set, I'm in a start mode. */
6251 if (!hlua || !hlua->task)
6252 return 0;
6253
6254 if (nice < -1024)
6255 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006256 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006257 nice = 1024;
6258
6259 hlua->task->nice = nice;
6260 return 0;
6261}
6262
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006263/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006264 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6265 * return an E_AGAIN signal, the emmiter of this signal must set a
6266 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006267 *
6268 * Task wrapper are longjmp safe because the only one Lua code
6269 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006270 */
Willy Tarreau83a5ff42019-08-21 14:14:50 +02006271struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006272{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006273 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006274 enum hlua_exec status;
6275
Christopher Faulet5bc99722018-04-25 10:34:45 +02006276 if (task->thread_mask == MAX_THREADS_MASK)
6277 task_set_affinity(task, tid_bit);
6278
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006279 /* If it is the first call to the task, we must initialize the
6280 * execution timeouts.
6281 */
6282 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006283 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006284
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006285 /* Execute the Lua code. */
6286 status = hlua_ctx_resume(hlua, 1);
6287
6288 switch (status) {
6289 /* finished or yield */
6290 case HLUA_E_OK:
6291 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006292 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006293 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006294 break;
6295
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006296 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006297 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006298 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006299 break;
6300
6301 /* finished with error. */
6302 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006303 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006304 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006305 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006306 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006307 break;
6308
6309 case HLUA_E_ERR:
6310 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006311 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006312 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006313 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006314 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006315 break;
6316 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006317 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006318}
6319
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006320/* This function is an LUA binding that register LUA function to be
6321 * executed after the HAProxy configuration parsing and before the
6322 * HAProxy scheduler starts. This function expect only one LUA
6323 * argument that is a function. This function returns nothing, but
6324 * throws if an error is encountered.
6325 */
6326__LJMP static int hlua_register_init(lua_State *L)
6327{
6328 struct hlua_init_function *init;
6329 int ref;
6330
6331 MAY_LJMP(check_args(L, 1, "register_init"));
6332
6333 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6334
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006335 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006336 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006337 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006338
6339 init->function_ref = ref;
6340 LIST_ADDQ(&hlua_init_functions, &init->l);
6341 return 0;
6342}
6343
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006344/* This functio is an LUA binding. It permits to register a task
6345 * executed in parallel of the main HAroxy activity. The task is
6346 * created and it is set in the HAProxy scheduler. It can be called
6347 * from the "init" section, "post init" or during the runtime.
6348 *
6349 * Lua prototype:
6350 *
6351 * <none> core.register_task(<function>)
6352 */
6353static int hlua_register_task(lua_State *L)
6354{
6355 struct hlua *hlua;
6356 struct task *task;
6357 int ref;
6358
6359 MAY_LJMP(check_args(L, 1, "register_task"));
6360
6361 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6362
Willy Tarreaubafbe012017-11-24 17:34:44 +01006363 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006364 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006365 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006366
Emeric Brunc60def82017-09-27 14:59:38 +02006367 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006368 if (!task)
6369 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6370
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006371 task->context = hlua;
6372 task->process = hlua_process_task;
6373
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006374 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006375 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006376
6377 /* Restore the function in the stack. */
6378 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6379 hlua->nargs = 0;
6380
6381 /* Schedule task. */
6382 task_schedule(task, now_ms);
6383
6384 return 0;
6385}
6386
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006387/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6388 * doesn't allow "yield" functions because the HAProxy engine cannot
6389 * resume converters.
6390 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006391static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006392{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006393 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006394 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006395 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006396
Willy Tarreaube508f12016-03-10 11:47:01 +01006397 if (!stream)
6398 return 0;
6399
Willy Tarreau87b09662015-04-03 00:22:06 +02006400 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006401 * Lua context can be not initialized. This behavior
6402 * permits to save performances because a systematic
6403 * Lua initialization cause 5% performances loss.
6404 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006405 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006406 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006407 if (!stream->hlua) {
6408 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6409 return 0;
6410 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006411 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006412 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6413 return 0;
6414 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006415 }
6416
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006417 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006418 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006419
6420 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006421 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6422 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6423 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006424 else
6425 error = "critical error";
6426 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006427 return 0;
6428 }
6429
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006430 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006431 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006432 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006433 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006434 return 0;
6435 }
6436
6437 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006438 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006439
6440 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006441 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006442 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006443 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006444 return 0;
6445 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006446 hlua_smp2lua(stream->hlua->T, smp);
6447 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006448
6449 /* push keywords in the stack. */
6450 if (arg_p) {
6451 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006452 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006453 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006454 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006455 return 0;
6456 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006457 hlua_arg2lua(stream->hlua->T, arg_p);
6458 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006459 }
6460 }
6461
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006462 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006463 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006464
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006465 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006466 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006467 }
6468
6469 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006470 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006471 /* finished. */
6472 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006473 /* If the stack is empty, the function fails. */
6474 if (lua_gettop(stream->hlua->T) <= 0)
6475 return 0;
6476
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006477 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006478 hlua_lua2smp(stream->hlua->T, -1, smp);
6479 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006480 return 1;
6481
6482 /* yield. */
6483 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006484 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006485 return 0;
6486
6487 /* finished with error. */
6488 case HLUA_E_ERRMSG:
6489 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006490 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006491 fcn->name, lua_tostring(stream->hlua->T, -1));
6492 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006493 return 0;
6494
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006495 case HLUA_E_ETMOUT:
6496 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6497 return 0;
6498
6499 case HLUA_E_NOMEM:
6500 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6501 return 0;
6502
6503 case HLUA_E_YIELD:
6504 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6505 return 0;
6506
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006507 case HLUA_E_ERR:
6508 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006509 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006510
6511 default:
6512 return 0;
6513 }
6514}
6515
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006516/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6517 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006518 * resume sample-fetches. This function will be called by the sample
6519 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006520 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006521static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6522 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006523{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006524 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006525 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006526 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006527 const struct buffer msg = { };
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006528 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006529
Willy Tarreaube508f12016-03-10 11:47:01 +01006530 if (!stream)
6531 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006532
Willy Tarreau87b09662015-04-03 00:22:06 +02006533 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006534 * Lua context can be not initialized. This behavior
6535 * permits to save performances because a systematic
6536 * Lua initialization cause 5% performances loss.
6537 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006538 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006539 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006540 if (!stream->hlua) {
6541 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6542 return 0;
6543 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006544 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006545 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6546 return 0;
6547 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006548 }
6549
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006550 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006551
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006552 if (stream->be->mode == PR_MODE_HTTP) {
6553 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
6554 hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
6555 else
6556 hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
6557 }
6558
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006559 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006560 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006561
6562 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006563 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6564 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6565 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006566 else
6567 error = "critical error";
6568 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006569 return 0;
6570 }
6571
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006572 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006573 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006574 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006575 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006576 return 0;
6577 }
6578
6579 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006580 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006581
6582 /* push arguments in the stack. */
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006583 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006584 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006585 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006586 return 0;
6587 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006588 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006589
6590 /* push keywords in the stack. */
6591 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6592 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006593 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006594 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006595 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006596 return 0;
6597 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006598 hlua_arg2lua(stream->hlua->T, arg_p);
6599 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006600 }
6601
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006602 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006603 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006604
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006605 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006606 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006607 }
6608
6609 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006610 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006611 /* finished. */
6612 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006613 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006614 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006615 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006616 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006617 /* If the stack is empty, the function fails. */
6618 if (lua_gettop(stream->hlua->T) <= 0)
6619 return 0;
6620
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006621 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006622 hlua_lua2smp(stream->hlua->T, -1, smp);
6623 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006624
6625 /* Set the end of execution flag. */
6626 smp->flags &= ~SMP_F_MAY_CHANGE;
6627 return 1;
6628
6629 /* yield. */
6630 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006631 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006632 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006633 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006634 return 0;
6635
6636 /* finished with error. */
6637 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006638 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006639 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006640 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006641 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006642 fcn->name, lua_tostring(stream->hlua->T, -1));
6643 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006644 return 0;
6645
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006646 case HLUA_E_ETMOUT:
6647 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006648 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006649 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6650 return 0;
6651
6652 case HLUA_E_NOMEM:
6653 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006654 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006655 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6656 return 0;
6657
6658 case HLUA_E_YIELD:
6659 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006660 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006661 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6662 return 0;
6663
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006664 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006665 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006666 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006667 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006668 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006669
6670 default:
6671 return 0;
6672 }
6673}
6674
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006675/* This function is an LUA binding used for registering
6676 * "sample-conv" functions. It expects a converter name used
6677 * in the haproxy configuration file, and an LUA function.
6678 */
6679__LJMP static int hlua_register_converters(lua_State *L)
6680{
6681 struct sample_conv_kw_list *sck;
6682 const char *name;
6683 int ref;
6684 int len;
6685 struct hlua_function *fcn;
6686
6687 MAY_LJMP(check_args(L, 2, "register_converters"));
6688
6689 /* First argument : converter name. */
6690 name = MAY_LJMP(luaL_checkstring(L, 1));
6691
6692 /* Second argument : lua function. */
6693 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6694
6695 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006696 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006697 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006698 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006699 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006700 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006701 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006702
6703 /* Fill fcn. */
6704 fcn->name = strdup(name);
6705 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006706 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006707 fcn->function_ref = ref;
6708
6709 /* List head */
6710 sck->list.n = sck->list.p = NULL;
6711
6712 /* converter keyword. */
6713 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006714 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006715 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006716 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006717
6718 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6719 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006720 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 +01006721 sck->kw[0].val_args = NULL;
6722 sck->kw[0].in_type = SMP_T_STR;
6723 sck->kw[0].out_type = SMP_T_STR;
6724 sck->kw[0].private = fcn;
6725
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006726 /* Register this new converter */
6727 sample_register_convs(sck);
6728
6729 return 0;
6730}
6731
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006732/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006733 * "sample-fetch" functions. It expects a converter name used
6734 * in the haproxy configuration file, and an LUA function.
6735 */
6736__LJMP static int hlua_register_fetches(lua_State *L)
6737{
6738 const char *name;
6739 int ref;
6740 int len;
6741 struct sample_fetch_kw_list *sfk;
6742 struct hlua_function *fcn;
6743
6744 MAY_LJMP(check_args(L, 2, "register_fetches"));
6745
6746 /* First argument : sample-fetch name. */
6747 name = MAY_LJMP(luaL_checkstring(L, 1));
6748
6749 /* Second argument : lua function. */
6750 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6751
6752 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006753 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006754 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006755 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006756 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006757 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006758 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006759
6760 /* Fill fcn. */
6761 fcn->name = strdup(name);
6762 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006763 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006764 fcn->function_ref = ref;
6765
6766 /* List head */
6767 sfk->list.n = sfk->list.p = NULL;
6768
6769 /* sample-fetch keyword. */
6770 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006771 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006772 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006773 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006774
6775 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6776 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006777 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 +01006778 sfk->kw[0].val_args = NULL;
6779 sfk->kw[0].out_type = SMP_T_STR;
6780 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6781 sfk->kw[0].val = 0;
6782 sfk->kw[0].private = fcn;
6783
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006784 /* Register this new fetch. */
6785 sample_register_fetches(sfk);
6786
6787 return 0;
6788}
6789
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006790/* This function is a wrapper to execute each LUA function declared
6791 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006792 * return ACT_RET_CONT if the processing is finished (with or without
6793 * error) and return ACT_RET_YIELD if the function must be called again
6794 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006795 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006796static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006797 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006798{
6799 char **arg;
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006800 unsigned int hflags = 0;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006801 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006802 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006803 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006804
6805 switch (rule->from) {
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006806 case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break;
6807 case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break;
6808 case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break;
6809 case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006810 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006811 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006812 return ACT_RET_CONT;
6813 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006814
Willy Tarreau87b09662015-04-03 00:22:06 +02006815 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006816 * Lua context can be not initialized. This behavior
6817 * permits to save performances because a systematic
6818 * Lua initialization cause 5% performances loss.
6819 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006820 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006821 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006822 if (!s->hlua) {
6823 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6824 rule->arg.hlua_rule->fcn.name);
6825 return ACT_RET_CONT;
6826 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006827 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006828 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6829 rule->arg.hlua_rule->fcn.name);
6830 return ACT_RET_CONT;
6831 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006832 }
6833
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006834 consistency_set(s, dir, &s->hlua->cons);
6835
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006836 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006837 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006838
6839 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006840 if (!SET_SAFE_LJMP(s->hlua->T)) {
6841 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6842 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006843 else
6844 error = "critical error";
6845 SEND_ERR(px, "Lua function '%s': %s.\n",
6846 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006847 return ACT_RET_CONT;
6848 }
6849
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006850 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006851 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006852 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006853 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006854 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006855 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006856 }
6857
6858 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006859 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006860
Willy Tarreau87b09662015-04-03 00:22:06 +02006861 /* Create and and push object stream in the stack. */
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006862 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006863 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006864 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006865 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006866 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006867 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006868 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006869
6870 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006871 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006872 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006873 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006874 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006875 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006876 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006877 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006878 lua_pushstring(s->hlua->T, *arg);
6879 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006880 }
6881
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006882 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006883 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006884
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006885 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006886 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006887 }
6888
Christopher Faulet719ddc82020-06-02 18:46:07 +02006889 /* Always reset the analyse expiration timeout for the corresponding
6890 * channel in case the lua script yield, to be sure to not keep an
6891 * expired timeout.
6892 */
6893 if (dir == SMP_OPT_DIR_REQ)
6894 s->req.analyse_exp = TICK_ETERNITY;
6895 else
6896 s->res.analyse_exp = TICK_ETERNITY;
6897
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006898 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006899 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006900 /* finished. */
6901 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006902 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006903 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006904 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006905 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006906 if (s->hlua->flags & HLUA_STOP)
Christopher Faulet4629d082019-07-04 11:27:15 +02006907 return ACT_RET_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006908 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006909
6910 /* yield. */
6911 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006912 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006913 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Fauletea0b0e22019-08-14 23:19:45 +02006914 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006915 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006916 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006917 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006918 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006919 /* Some actions can be wake up when a "write" event
6920 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006921 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006922 */
Christopher Fauletb22f6502019-07-26 14:54:52 +02006923 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006924 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006925 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006926 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006927 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006928 * because HAProxy is not able to manipulate data, it
6929 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006930 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006931
6932 /* finished with error. */
6933 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006934 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006935 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006936 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006937 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006938 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006939 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006940 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6941 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006942 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006943
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006944 case HLUA_E_ETMOUT:
6945 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006946 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006947 return ACT_RET_ERR;
6948 }
6949 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6950 return 0;
6951
6952 case HLUA_E_NOMEM:
6953 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006954 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006955 return ACT_RET_ERR;
6956 }
6957 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6958 return 0;
6959
6960 case HLUA_E_YIELD:
6961 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006962 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006963 return ACT_RET_ERR;
6964 }
6965 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6966 rule->arg.hlua_rule->fcn.name);
6967 return 0;
6968
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006969 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006970 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006971 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006972 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006973 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006974 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006975 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006976 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006977
6978 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006979 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006980 }
6981}
6982
Olivier Houchard9f6af332018-05-25 14:04:04 +02006983struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006984{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006985 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006986
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006987 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006988 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006989 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006990}
6991
6992static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6993{
6994 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006995 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006996 struct task *task;
6997 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006998 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006999
Willy Tarreaubafbe012017-11-24 17:34:44 +01007000 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007001 if (!hlua) {
7002 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
7003 ctx->rule->arg.hlua_rule->fcn.name);
7004 return 0;
7005 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007006 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007007 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007008 ctx->ctx.hlua_apptcp.flags = 0;
7009
7010 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007011 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007012 if (!task) {
7013 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
7014 ctx->rule->arg.hlua_rule->fcn.name);
7015 return 0;
7016 }
7017 task->nice = 0;
7018 task->context = ctx;
7019 task->process = hlua_applet_wakeup;
7020 ctx->ctx.hlua_apptcp.task = task;
7021
7022 /* In the execution wrappers linked with a stream, the
7023 * Lua context can be not initialized. This behavior
7024 * permits to save performances because a systematic
7025 * Lua initialization cause 5% performances loss.
7026 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007027 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007028 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
7029 ctx->rule->arg.hlua_rule->fcn.name);
7030 return 0;
7031 }
7032
7033 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007034 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007035
7036 /* The following Lua calls can fail. */
7037 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007038 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7039 error = lua_tostring(hlua->T, -1);
7040 else
7041 error = "critical error";
7042 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7043 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007044 return 0;
7045 }
7046
7047 /* Check stack available size. */
7048 if (!lua_checkstack(hlua->T, 1)) {
7049 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7050 ctx->rule->arg.hlua_rule->fcn.name);
7051 RESET_SAFE_LJMP(hlua->T);
7052 return 0;
7053 }
7054
7055 /* Restore the function in the stack. */
7056 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7057
7058 /* Create and and push object stream in the stack. */
7059 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7060 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7061 ctx->rule->arg.hlua_rule->fcn.name);
7062 RESET_SAFE_LJMP(hlua->T);
7063 return 0;
7064 }
7065 hlua->nargs = 1;
7066
7067 /* push keywords in the stack. */
7068 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7069 if (!lua_checkstack(hlua->T, 1)) {
7070 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7071 ctx->rule->arg.hlua_rule->fcn.name);
7072 RESET_SAFE_LJMP(hlua->T);
7073 return 0;
7074 }
7075 lua_pushstring(hlua->T, *arg);
7076 hlua->nargs++;
7077 }
7078
7079 RESET_SAFE_LJMP(hlua->T);
7080
7081 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007082 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007083 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007084
7085 return 1;
7086}
7087
Willy Tarreau83a5ff42019-08-21 14:14:50 +02007088void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007089{
7090 struct stream_interface *si = ctx->owner;
7091 struct stream *strm = si_strm(si);
7092 struct channel *res = si_ic(si);
7093 struct act_rule *rule = ctx->rule;
7094 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007095 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007096
7097 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007098 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7099 /* eat the whole request */
7100 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007101 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007102 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007103
7104 /* If the stream is disconnect or closed, ldo nothing. */
7105 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7106 return;
7107
7108 /* Execute the function. */
7109 switch (hlua_ctx_resume(hlua, 1)) {
7110 /* finished. */
7111 case HLUA_E_OK:
7112 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7113
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007114 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007115 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007116 res->flags |= CF_READ_NULL;
7117 si_shutr(si);
7118 return;
7119
7120 /* yield. */
7121 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007122 if (hlua->wake_time != TICK_ETERNITY)
7123 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007124 return;
7125
7126 /* finished with error. */
7127 case HLUA_E_ERRMSG:
7128 /* Display log. */
7129 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7130 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7131 lua_pop(hlua->T, 1);
7132 goto error;
7133
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007134 case HLUA_E_ETMOUT:
7135 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
7136 rule->arg.hlua_rule->fcn.name);
7137 goto error;
7138
7139 case HLUA_E_NOMEM:
7140 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
7141 rule->arg.hlua_rule->fcn.name);
7142 goto error;
7143
7144 case HLUA_E_YIELD: /* unexpected */
7145 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
7146 rule->arg.hlua_rule->fcn.name);
7147 goto error;
7148
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007149 case HLUA_E_ERR:
7150 /* Display log. */
7151 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
7152 rule->arg.hlua_rule->fcn.name);
7153 goto error;
7154
7155 default:
7156 goto error;
7157 }
7158
7159error:
7160
7161 /* For all other cases, just close the stream. */
7162 si_shutw(si);
7163 si_shutr(si);
7164 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7165}
7166
7167static void hlua_applet_tcp_release(struct appctx *ctx)
7168{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007169 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007170 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007171 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007172 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007173}
7174
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007175/* The function returns 1 if the initialisation is complete, 0 if
7176 * an errors occurs and -1 if more data are required for initializing
7177 * the applet.
7178 */
7179static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7180{
7181 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007182 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007183 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007184 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007185 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007186 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007187
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007188 txn = strm->txn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007189
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007190 /* We want two things in HTTP mode :
7191 * - enforce server-close mode if we were in keep-alive, so that the
7192 * applet is released after each response ;
7193 * - enable request body transfer to the applet in order to resync
7194 * with the response body.
7195 */
7196 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
7197 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007198
Willy Tarreaubafbe012017-11-24 17:34:44 +01007199 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007200 if (!hlua) {
7201 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7202 ctx->rule->arg.hlua_rule->fcn.name);
7203 return 0;
7204 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007205 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007206 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007207 ctx->ctx.hlua_apphttp.left_bytes = -1;
7208 ctx->ctx.hlua_apphttp.flags = 0;
7209
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007210 if (txn->req.flags & HTTP_MSGF_VER_11)
7211 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7212
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007213 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007214 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007215 if (!task) {
7216 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7217 ctx->rule->arg.hlua_rule->fcn.name);
7218 return 0;
7219 }
7220 task->nice = 0;
7221 task->context = ctx;
7222 task->process = hlua_applet_wakeup;
7223 ctx->ctx.hlua_apphttp.task = task;
7224
7225 /* In the execution wrappers linked with a stream, the
7226 * Lua context can be not initialized. This behavior
7227 * permits to save performances because a systematic
7228 * Lua initialization cause 5% performances loss.
7229 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007230 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007231 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
7232 ctx->rule->arg.hlua_rule->fcn.name);
7233 return 0;
7234 }
7235
7236 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007237 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007238
7239 /* The following Lua calls can fail. */
7240 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007241 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7242 error = lua_tostring(hlua->T, -1);
7243 else
7244 error = "critical error";
7245 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7246 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007247 return 0;
7248 }
7249
7250 /* Check stack available size. */
7251 if (!lua_checkstack(hlua->T, 1)) {
7252 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7253 ctx->rule->arg.hlua_rule->fcn.name);
7254 RESET_SAFE_LJMP(hlua->T);
7255 return 0;
7256 }
7257
7258 /* Restore the function in the stack. */
7259 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7260
7261 /* Create and and push object stream in the stack. */
7262 if (!hlua_applet_http_new(hlua->T, ctx)) {
7263 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7264 ctx->rule->arg.hlua_rule->fcn.name);
7265 RESET_SAFE_LJMP(hlua->T);
7266 return 0;
7267 }
7268 hlua->nargs = 1;
7269
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007270 /* push keywords in the stack. */
7271 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7272 if (!lua_checkstack(hlua->T, 1)) {
7273 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7274 ctx->rule->arg.hlua_rule->fcn.name);
7275 RESET_SAFE_LJMP(hlua->T);
7276 return 0;
7277 }
7278 lua_pushstring(hlua->T, *arg);
7279 hlua->nargs++;
7280 }
7281
7282 RESET_SAFE_LJMP(hlua->T);
7283
7284 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007285 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007286
7287 return 1;
7288}
7289
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007290static void hlua_applet_htx_fct(struct appctx *ctx)
7291{
7292 struct stream_interface *si = ctx->owner;
7293 struct stream *strm = si_strm(si);
7294 struct channel *req = si_oc(si);
7295 struct channel *res = si_ic(si);
7296 struct act_rule *rule = ctx->rule;
7297 struct proxy *px = strm->be;
7298 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7299 struct htx *req_htx, *res_htx;
7300
7301 res_htx = htx_from_buf(&res->buf);
7302
7303 /* If the stream is disconnect or closed, ldo nothing. */
7304 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7305 goto out;
7306
7307 /* Check if the input buffer is avalaible. */
7308 if (!b_size(&res->buf)) {
7309 si_rx_room_blk(si);
7310 goto out;
7311 }
7312 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007313 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007314 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7315
7316 /* Set the currently running flag. */
7317 if (!HLUA_IS_RUNNING(hlua) &&
7318 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7319 struct htx_blk *blk;
7320 size_t count = co_data(req);
7321
7322 if (!count) {
7323 si_cant_get(si);
7324 goto out;
7325 }
7326
7327 /* We need to flush the request header. This left the body for
7328 * the Lua.
7329 */
7330 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007331 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007332 while (count && blk) {
7333 enum htx_blk_type type = htx_get_blk_type(blk);
7334 uint32_t sz = htx_get_blksz(blk);
7335
7336 if (sz > count) {
7337 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007338 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007339 goto out;
7340 }
7341
7342 count -= sz;
7343 co_set_data(req, co_data(req) - sz);
7344 blk = htx_remove_blk(req_htx, blk);
7345
7346 if (type == HTX_BLK_EOH)
7347 break;
7348 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007349 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007350 }
7351
7352 /* Executes The applet if it is not done. */
7353 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7354
7355 /* Execute the function. */
7356 switch (hlua_ctx_resume(hlua, 1)) {
7357 /* finished. */
7358 case HLUA_E_OK:
7359 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7360 break;
7361
7362 /* yield. */
7363 case HLUA_E_AGAIN:
7364 if (hlua->wake_time != TICK_ETERNITY)
7365 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007366 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007367
7368 /* finished with error. */
7369 case HLUA_E_ERRMSG:
7370 /* Display log. */
7371 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7372 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7373 lua_pop(hlua->T, 1);
7374 goto error;
7375
7376 case HLUA_E_ETMOUT:
7377 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7378 rule->arg.hlua_rule->fcn.name);
7379 goto error;
7380
7381 case HLUA_E_NOMEM:
7382 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7383 rule->arg.hlua_rule->fcn.name);
7384 goto error;
7385
7386 case HLUA_E_YIELD: /* unexpected */
7387 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7388 rule->arg.hlua_rule->fcn.name);
7389 goto error;
7390
7391 case HLUA_E_ERR:
7392 /* Display log. */
7393 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7394 rule->arg.hlua_rule->fcn.name);
7395 goto error;
7396
7397 default:
7398 goto error;
7399 }
7400 }
7401
7402 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007403 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7404 goto done;
7405
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007406 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7407 goto error;
7408
Christopher Faulet54b5e212019-06-04 10:08:28 +02007409 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007410 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7411 si_rx_room_blk(si);
7412 goto out;
7413 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007414 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007415 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7416 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007417 }
7418
7419 done:
7420 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007421 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007422 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007423 si_shutr(si);
7424 }
7425
7426 /* eat the whole request */
7427 if (co_data(req)) {
7428 req_htx = htx_from_buf(&req->buf);
7429 co_htx_skip(req, req_htx, co_data(req));
7430 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007431 }
7432 }
7433
7434 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007435 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007436 return;
7437
7438 error:
7439
7440 /* If we are in HTTP mode, and we are not send any
7441 * data, return a 500 server error in best effort:
7442 * if there is no room available in the buffer,
7443 * just close the connection.
7444 */
7445 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7446 struct buffer *err = &htx_err_chunks[HTTP_ERR_500];
7447
7448 channel_erase(res);
7449 res->buf.data = b_data(err);
7450 memcpy(res->buf.area, b_head(err), b_data(err));
7451 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007452 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007453 }
7454 if (!(strm->flags & SF_ERR_MASK))
7455 strm->flags |= SF_ERR_RESOURCE;
7456 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7457 goto done;
7458}
7459
Willy Tarreau83a5ff42019-08-21 14:14:50 +02007460void hlua_applet_http_fct(struct appctx *ctx)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007461{
7462 struct stream_interface *si = ctx->owner;
7463 struct stream *strm = si_strm(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007464 struct channel *req = si_oc(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007465 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007466 struct act_rule *rule = ctx->rule;
7467 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007468 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02007469 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007470 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02007471 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007472 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007473 int ret;
7474
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007475 if (IS_HTX_STRM(strm))
7476 return hlua_applet_htx_fct(ctx);
7477
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007478 /* If the stream is disconnect or closed, ldo nothing. */
7479 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007480 goto out;
7481
7482 /* Check if the input buffer is avalaible. */
7483 if (!b_size(&res->buf)) {
7484 si_rx_room_blk(si);
7485 goto out;
7486 }
7487 /* check that the output is not closed */
7488 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
7489 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007490
7491 /* Set the currently running flag. */
7492 if (!HLUA_IS_RUNNING(hlua) &&
7493 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007494 /* Store the max amount of bytes that we can read. */
7495 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
7496
7497 /* We need to flush the request header. This left the body
7498 * for the Lua.
7499 */
7500
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007501 /* Read the maximum amount of data available. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007502 ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007503 if (ret == -1)
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007504 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007505
7506 /* No data available, ask for more data. */
7507 if (ret == 1)
7508 len2 = 0;
7509 if (ret == 0)
7510 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02007511 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007512 si_cant_get(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007513 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007514 }
7515
7516 /* skip the requests bytes. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007517 co_skip(req, strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007518 }
7519
7520 /* Executes The applet if it is not done. */
7521 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7522
7523 /* Execute the function. */
7524 switch (hlua_ctx_resume(hlua, 1)) {
7525 /* finished. */
7526 case HLUA_E_OK:
7527 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7528 break;
7529
7530 /* yield. */
7531 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007532 if (hlua->wake_time != TICK_ETERNITY)
7533 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007534 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007535
7536 /* finished with error. */
7537 case HLUA_E_ERRMSG:
7538 /* Display log. */
7539 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7540 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7541 lua_pop(hlua->T, 1);
7542 goto error;
7543
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007544 case HLUA_E_ETMOUT:
7545 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7546 rule->arg.hlua_rule->fcn.name);
7547 goto error;
7548
7549 case HLUA_E_NOMEM:
7550 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7551 rule->arg.hlua_rule->fcn.name);
7552 goto error;
7553
7554 case HLUA_E_YIELD: /* unexpected */
7555 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7556 rule->arg.hlua_rule->fcn.name);
7557 goto error;
7558
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007559 case HLUA_E_ERR:
7560 /* Display log. */
7561 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7562 rule->arg.hlua_rule->fcn.name);
7563 goto error;
7564
7565 default:
7566 goto error;
7567 }
7568 }
7569
7570 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007571 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7572 goto done;
7573
Christopher Fauletcc26b132018-12-18 21:20:57 +01007574 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7575 goto error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007576
7577 /* We must send the final chunk. */
7578 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
7579 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
7580
7581 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02007582 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007583
7584 /* critical error. */
7585 if (ret == -2 || ret == -3) {
7586 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
7587 rule->arg.hlua_rule->fcn.name);
7588 goto error;
7589 }
7590
7591 /* no enough space error. */
7592 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01007593 si_rx_room_blk(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007594 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007595 }
7596
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007597 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7598 ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007599 }
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007600 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007601
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007602 done:
7603 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
7604 if (!(res->flags & CF_SHUTR)) {
7605 res->flags |= CF_READ_NULL;
7606 si_shutr(si);
7607 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007608
7609 /* eat the whole request */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007610 if (co_data(req))
7611 co_skip(req, co_data(req));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007612 }
7613
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007614 out:
7615 return;
7616
7617 error:
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007618
7619 /* If we are in HTTP mode, and we are not send any
7620 * data, return a 500 server error in best effort:
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007621 * if there is no room available in the buffer,
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007622 * just close the connection.
7623 */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007624 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7625 channel_erase(res);
7626 ci_putblk(res, error_500, strlen(error_500));
7627 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007628 if (!(strm->flags & SF_ERR_MASK))
7629 strm->flags |= SF_ERR_RESOURCE;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007630 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007631 goto done;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007632}
7633
7634static void hlua_applet_http_release(struct appctx *ctx)
7635{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007636 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007637 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007638 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007639 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007640}
7641
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007642/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
7643 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007644 *
7645 * This function can fail with an abort() due to an Lua critical error.
7646 * We are in the configuration parsing process of HAProxy, this abort() is
7647 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007648 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007649static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7650 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007651{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007652 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007653 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007654
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007655 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007656 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007657 if (!rule->arg.hlua_rule) {
7658 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007659 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007660 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007661
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007662 /* Memory for arguments. */
7663 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7664 if (!rule->arg.hlua_rule->args) {
7665 memprintf(err, "out of memory error");
7666 return ACT_RET_PRS_ERR;
7667 }
7668
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007669 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007670 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007671
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007672 /* Expect some arguments */
7673 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007674 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007675 memprintf(err, "expect %d arguments", fcn->nargs);
7676 return ACT_RET_PRS_ERR;
7677 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007678 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007679 if (!rule->arg.hlua_rule->args[i]) {
7680 memprintf(err, "out of memory error");
7681 return ACT_RET_PRS_ERR;
7682 }
7683 (*cur_arg)++;
7684 }
7685 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007686
Thierry FOURNIER42148732015-09-02 17:17:33 +02007687 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007688 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007689 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007690}
7691
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007692static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7693 struct act_rule *rule, char **err)
7694{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007695 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007696
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007697 /* HTTP applets are forbidden in tcp-request rules.
7698 * HTTP applet request requires everything initilized by
7699 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
7700 * The applet will be immediately initilized, but its before
7701 * the call of this analyzer.
7702 */
7703 if (rule->from != ACT_F_HTTP_REQ) {
7704 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7705 return ACT_RET_PRS_ERR;
7706 }
7707
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007708 /* Memory for the rule. */
7709 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7710 if (!rule->arg.hlua_rule) {
7711 memprintf(err, "out of memory error");
7712 return ACT_RET_PRS_ERR;
7713 }
7714
7715 /* Reference the Lua function and store the reference. */
7716 rule->arg.hlua_rule->fcn = *fcn;
7717
7718 /* TODO: later accept arguments. */
7719 rule->arg.hlua_rule->args = NULL;
7720
7721 /* Add applet pointer in the rule. */
7722 rule->applet.obj_type = OBJ_TYPE_APPLET;
7723 rule->applet.name = fcn->name;
7724 rule->applet.init = hlua_applet_http_init;
7725 rule->applet.fct = hlua_applet_http_fct;
7726 rule->applet.release = hlua_applet_http_release;
7727 rule->applet.timeout = hlua_timeout_applet;
7728
7729 return ACT_RET_PRS_OK;
7730}
7731
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007732/* This function is an LUA binding used for registering
7733 * "sample-conv" functions. It expects a converter name used
7734 * in the haproxy configuration file, and an LUA function.
7735 */
7736__LJMP static int hlua_register_action(lua_State *L)
7737{
7738 struct action_kw_list *akl;
7739 const char *name;
7740 int ref;
7741 int len;
7742 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007743 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007744
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007745 /* Initialise the number of expected arguments at 0. */
7746 nargs = 0;
7747
7748 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7749 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007750
7751 /* First argument : converter name. */
7752 name = MAY_LJMP(luaL_checkstring(L, 1));
7753
7754 /* Second argument : environment. */
7755 if (lua_type(L, 2) != LUA_TTABLE)
7756 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7757
7758 /* Third argument : lua function. */
7759 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7760
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007761 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007762 if (lua_gettop(L) >= 4)
7763 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7764
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007765 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007766 lua_pushnil(L);
7767 while (lua_next(L, 2) != 0) {
7768 if (lua_type(L, -1) != LUA_TSTRING)
7769 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7770
7771 /* Check required environment. Only accepted "http" or "tcp". */
7772 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007773 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007774 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007775 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007776 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007777 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007778 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007779
7780 /* Fill fcn. */
7781 fcn->name = strdup(name);
7782 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007783 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007784 fcn->function_ref = ref;
7785
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007786 /* Set the expected number od arguments. */
7787 fcn->nargs = nargs;
7788
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007789 /* List head */
7790 akl->list.n = akl->list.p = NULL;
7791
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007792 /* action keyword. */
7793 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007794 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007795 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007796 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007797
7798 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7799
7800 akl->kw[0].match_pfx = 0;
7801 akl->kw[0].private = fcn;
7802 akl->kw[0].parse = action_register_lua;
7803
7804 /* select the action registering point. */
7805 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7806 tcp_req_cont_keywords_register(akl);
7807 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7808 tcp_res_cont_keywords_register(akl);
7809 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7810 http_req_keywords_register(akl);
7811 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7812 http_res_keywords_register(akl);
7813 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007814 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007815 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7816 "are expected.", lua_tostring(L, -1)));
7817
7818 /* pop the environment string. */
7819 lua_pop(L, 1);
7820 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007821 return ACT_RET_PRS_OK;
7822}
7823
7824static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7825 struct act_rule *rule, char **err)
7826{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007827 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007828
Christopher Fauleteb709802019-04-11 22:04:08 +02007829 if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) {
Christopher Fauletafd8f102018-11-08 11:34:21 +01007830 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
7831 return ACT_RET_PRS_ERR;
7832 }
7833
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007834 /* Memory for the rule. */
7835 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7836 if (!rule->arg.hlua_rule) {
7837 memprintf(err, "out of memory error");
7838 return ACT_RET_PRS_ERR;
7839 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007840
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007841 /* Reference the Lua function and store the reference. */
7842 rule->arg.hlua_rule->fcn = *fcn;
7843
7844 /* TODO: later accept arguments. */
7845 rule->arg.hlua_rule->args = NULL;
7846
7847 /* Add applet pointer in the rule. */
7848 rule->applet.obj_type = OBJ_TYPE_APPLET;
7849 rule->applet.name = fcn->name;
7850 rule->applet.init = hlua_applet_tcp_init;
7851 rule->applet.fct = hlua_applet_tcp_fct;
7852 rule->applet.release = hlua_applet_tcp_release;
7853 rule->applet.timeout = hlua_timeout_applet;
7854
7855 return 0;
7856}
7857
7858/* This function is an LUA binding used for registering
7859 * "sample-conv" functions. It expects a converter name used
7860 * in the haproxy configuration file, and an LUA function.
7861 */
7862__LJMP static int hlua_register_service(lua_State *L)
7863{
7864 struct action_kw_list *akl;
7865 const char *name;
7866 const char *env;
7867 int ref;
7868 int len;
7869 struct hlua_function *fcn;
7870
7871 MAY_LJMP(check_args(L, 3, "register_service"));
7872
7873 /* First argument : converter name. */
7874 name = MAY_LJMP(luaL_checkstring(L, 1));
7875
7876 /* Second argument : environment. */
7877 env = MAY_LJMP(luaL_checkstring(L, 2));
7878
7879 /* Third argument : lua function. */
7880 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7881
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007882 /* Allocate and fill the sample fetch keyword struct. */
7883 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7884 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007885 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007886 fcn = calloc(1, sizeof(*fcn));
7887 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007888 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007889
7890 /* Fill fcn. */
7891 len = strlen("<lua.>") + strlen(name) + 1;
7892 fcn->name = calloc(1, len);
7893 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007894 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007895 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7896 fcn->function_ref = ref;
7897
7898 /* List head */
7899 akl->list.n = akl->list.p = NULL;
7900
7901 /* converter keyword. */
7902 len = strlen("lua.") + strlen(name) + 1;
7903 akl->kw[0].kw = calloc(1, len);
7904 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007905 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007906
7907 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7908
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007909 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007910 if (strcmp(env, "tcp") == 0)
7911 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007912 else if (strcmp(env, "http") == 0)
7913 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007914 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007915 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007916 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007917
7918 akl->kw[0].match_pfx = 0;
7919 akl->kw[0].private = fcn;
7920
7921 /* End of array. */
7922 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7923
7924 /* Register this new converter */
7925 service_keywords_register(akl);
7926
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007927 return 0;
7928}
7929
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007930/* This function initialises Lua cli handler. It copies the
7931 * arguments in the Lua stack and create channel IO objects.
7932 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007933static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007934{
7935 struct hlua *hlua;
7936 struct hlua_function *fcn;
7937 int i;
7938 const char *error;
7939
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007940 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007941 appctx->ctx.hlua_cli.fcn = private;
7942
Willy Tarreaubafbe012017-11-24 17:34:44 +01007943 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007944 if (!hlua) {
7945 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007946 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007947 }
7948 HLUA_INIT(hlua);
7949 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007950
7951 /* Create task used by signal to wakeup applets.
7952 * We use the same wakeup fonction than the Lua applet_tcp and
7953 * applet_http. It is absolutely compatible.
7954 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007955 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007956 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007957 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007958 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007959 }
7960 appctx->ctx.hlua_cli.task->nice = 0;
7961 appctx->ctx.hlua_cli.task->context = appctx;
7962 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7963
7964 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007965 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007966 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007967 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007968 }
7969
7970 /* The following Lua calls can fail. */
7971 if (!SET_SAFE_LJMP(hlua->T)) {
7972 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7973 error = lua_tostring(hlua->T, -1);
7974 else
7975 error = "critical error";
7976 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7977 goto error;
7978 }
7979
7980 /* Check stack available size. */
7981 if (!lua_checkstack(hlua->T, 2)) {
7982 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7983 goto error;
7984 }
7985
7986 /* Restore the function in the stack. */
7987 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7988
7989 /* Once the arguments parsed, the CLI is like an AppletTCP,
7990 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007991 */
7992 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7993 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7994 goto error;
7995 }
7996 hlua->nargs = 1;
7997
7998 /* push keywords in the stack. */
7999 for (i = 0; *args[i]; i++) {
8000 /* Check stack available size. */
8001 if (!lua_checkstack(hlua->T, 1)) {
8002 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8003 goto error;
8004 }
8005 lua_pushstring(hlua->T, args[i]);
8006 hlua->nargs++;
8007 }
8008
8009 /* We must initialize the execution timeouts. */
8010 hlua->max_time = hlua_timeout_session;
8011
8012 /* At this point the execution is safe. */
8013 RESET_SAFE_LJMP(hlua->T);
8014
8015 /* It's ok */
8016 return 0;
8017
8018 /* It's not ok. */
8019error:
8020 RESET_SAFE_LJMP(hlua->T);
8021 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008022 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008023 return 1;
8024}
8025
8026static int hlua_cli_io_handler_fct(struct appctx *appctx)
8027{
8028 struct hlua *hlua;
8029 struct stream_interface *si;
8030 struct hlua_function *fcn;
8031
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008032 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008033 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008034 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008035
8036 /* If the stream is disconnect or closed, ldo nothing. */
8037 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8038 return 1;
8039
8040 /* Execute the function. */
8041 switch (hlua_ctx_resume(hlua, 1)) {
8042
8043 /* finished. */
8044 case HLUA_E_OK:
8045 return 1;
8046
8047 /* yield. */
8048 case HLUA_E_AGAIN:
8049 /* We want write. */
8050 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008051 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008052 /* Set the timeout. */
8053 if (hlua->wake_time != TICK_ETERNITY)
8054 task_schedule(hlua->task, hlua->wake_time);
8055 return 0;
8056
8057 /* finished with error. */
8058 case HLUA_E_ERRMSG:
8059 /* Display log. */
8060 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8061 fcn->name, lua_tostring(hlua->T, -1));
8062 lua_pop(hlua->T, 1);
8063 return 1;
8064
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008065 case HLUA_E_ETMOUT:
8066 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8067 fcn->name);
8068 return 1;
8069
8070 case HLUA_E_NOMEM:
8071 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8072 fcn->name);
8073 return 1;
8074
8075 case HLUA_E_YIELD: /* unexpected */
8076 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8077 fcn->name);
8078 return 1;
8079
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008080 case HLUA_E_ERR:
8081 /* Display log. */
8082 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8083 fcn->name);
8084 return 1;
8085
8086 default:
8087 return 1;
8088 }
8089
8090 return 1;
8091}
8092
8093static void hlua_cli_io_release_fct(struct appctx *appctx)
8094{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008095 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008096 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008097}
8098
8099/* This function is an LUA binding used for registering
8100 * new keywords in the cli. It expects a list of keywords
8101 * which are the "path". It is limited to 5 keywords. A
8102 * description of the command, a function to be executed
8103 * for the parsing and a function for io handlers.
8104 */
8105__LJMP static int hlua_register_cli(lua_State *L)
8106{
8107 struct cli_kw_list *cli_kws;
8108 const char *message;
8109 int ref_io;
8110 int len;
8111 struct hlua_function *fcn;
8112 int index;
8113 int i;
8114
8115 MAY_LJMP(check_args(L, 3, "register_cli"));
8116
8117 /* First argument : an array of maximum 5 keywords. */
8118 if (!lua_istable(L, 1))
8119 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8120
8121 /* Second argument : string with contextual message. */
8122 message = MAY_LJMP(luaL_checkstring(L, 2));
8123
8124 /* Third and fourth argument : lua function. */
8125 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8126
8127 /* Allocate and fill the sample fetch keyword struct. */
8128 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
8129 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008130 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008131 fcn = calloc(1, sizeof(*fcn));
8132 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008133 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008134
8135 /* Fill path. */
8136 index = 0;
8137 lua_pushnil(L);
8138 while(lua_next(L, 1) != 0) {
8139 if (index >= 5)
8140 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8141 if (lua_type(L, -1) != LUA_TSTRING)
8142 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8143 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
8144 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008145 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008146 index++;
8147 lua_pop(L, 1);
8148 }
8149
8150 /* Copy help message. */
8151 cli_kws->kw[0].usage = strdup(message);
8152 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008153 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008154
8155 /* Fill fcn io handler. */
8156 len = strlen("<lua.cli>") + 1;
8157 for (i = 0; i < index; i++)
8158 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8159 fcn->name = calloc(1, len);
8160 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008161 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008162 strncat((char *)fcn->name, "<lua.cli", len);
8163 for (i = 0; i < index; i++) {
8164 strncat((char *)fcn->name, ".", len);
8165 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8166 }
8167 strncat((char *)fcn->name, ">", len);
8168 fcn->function_ref = ref_io;
8169
8170 /* Fill last entries. */
8171 cli_kws->kw[0].private = fcn;
8172 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8173 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8174 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8175
8176 /* Register this new converter */
8177 cli_register_kw(cli_kws);
8178
8179 return 0;
8180}
8181
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008182static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
8183 struct proxy *defpx, const char *file, int line,
8184 char **err, unsigned int *timeout)
8185{
8186 const char *error;
8187
8188 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008189 if (error == PARSE_TIME_OVER) {
8190 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8191 args[1], args[0]);
8192 return -1;
8193 }
8194 else if (error == PARSE_TIME_UNDER) {
8195 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8196 args[1], args[0]);
8197 return -1;
8198 }
8199 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008200 memprintf(err, "%s: invalid timeout", args[0]);
8201 return -1;
8202 }
8203 return 0;
8204}
8205
8206static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
8207 struct proxy *defpx, const char *file, int line,
8208 char **err)
8209{
8210 return hlua_read_timeout(args, section_type, curpx, defpx,
8211 file, line, err, &hlua_timeout_session);
8212}
8213
8214static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8215 struct proxy *defpx, const char *file, int line,
8216 char **err)
8217{
8218 return hlua_read_timeout(args, section_type, curpx, defpx,
8219 file, line, err, &hlua_timeout_task);
8220}
8221
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008222static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8223 struct proxy *defpx, const char *file, int line,
8224 char **err)
8225{
8226 return hlua_read_timeout(args, section_type, curpx, defpx,
8227 file, line, err, &hlua_timeout_applet);
8228}
8229
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008230static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
8231 struct proxy *defpx, const char *file, int line,
8232 char **err)
8233{
8234 char *error;
8235
8236 hlua_nb_instruction = strtoll(args[1], &error, 10);
8237 if (*error != '\0') {
8238 memprintf(err, "%s: invalid number", args[0]);
8239 return -1;
8240 }
8241 return 0;
8242}
8243
Willy Tarreau32f61e22015-03-18 17:54:59 +01008244static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8245 struct proxy *defpx, const char *file, int line,
8246 char **err)
8247{
8248 char *error;
8249
8250 if (*(args[1]) == 0) {
8251 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8252 return -1;
8253 }
8254 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8255 if (*error != '\0') {
8256 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8257 return -1;
8258 }
8259 return 0;
8260}
8261
8262
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008263/* This function is called by the main configuration key "lua-load". It loads and
8264 * execute an lua file during the parsing of the HAProxy configuration file. It is
8265 * the main lua entry point.
8266 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008267 * This function runs with the HAProxy keywords API. It returns -1 if an error
8268 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008269 *
8270 * In some error case, LUA set an error message in top of the stack. This function
8271 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008272 *
8273 * This function can fail with an abort() due to an Lua critical error.
8274 * We are in the configuration parsing process of HAProxy, this abort() is
8275 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008276 */
8277static int hlua_load(char **args, int section_type, struct proxy *curpx,
8278 struct proxy *defpx, const char *file, int line,
8279 char **err)
8280{
8281 int error;
8282
8283 /* Just load and compile the file. */
8284 error = luaL_loadfile(gL.T, args[1]);
8285 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008286 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008287 lua_pop(gL.T, 1);
8288 return -1;
8289 }
8290
8291 /* If no syntax error where detected, execute the code. */
8292 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
8293 switch (error) {
8294 case LUA_OK:
8295 break;
8296 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008297 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008298 lua_pop(gL.T, 1);
8299 return -1;
8300 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008301 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008302 return -1;
8303 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008304 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008305 lua_pop(gL.T, 1);
8306 return -1;
Christopher Faulet20699902020-07-28 10:33:25 +02008307#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008308 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008309 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008310 lua_pop(gL.T, 1);
8311 return -1;
Christopher Faulet20699902020-07-28 10:33:25 +02008312#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008313 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008314 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008315 lua_pop(gL.T, 1);
8316 return -1;
8317 }
8318
8319 return 0;
8320}
8321
8322/* configuration keywords declaration */
8323static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008324 { CFG_GLOBAL, "lua-load", hlua_load },
8325 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8326 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008327 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008328 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008329 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008330 { 0, NULL, NULL },
8331}};
8332
Willy Tarreau0108d902018-11-25 19:14:37 +01008333INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8334
Christopher Fauletafd8f102018-11-08 11:34:21 +01008335
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008336/* This function can fail with an abort() due to an Lua critical error.
8337 * We are in the initialisation process of HAProxy, this abort() is
8338 * tolerated.
8339 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008340int hlua_post_init()
8341{
8342 struct hlua_init_function *init;
8343 const char *msg;
8344 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008345 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008346
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008347 /* Call post initialisation function in safe environement. */
8348 if (!SET_SAFE_LJMP(gL.T)) {
8349 if (lua_type(gL.T, -1) == LUA_TSTRING)
8350 error = lua_tostring(gL.T, -1);
8351 else
8352 error = "critical error";
8353 fprintf(stderr, "Lua post-init: %s.\n", error);
8354 exit(1);
8355 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008356
8357#if USE_OPENSSL
8358 /* Initialize SSL server. */
8359 if (socket_ssl.xprt->prepare_srv) {
8360 int saved_used_backed = global.ssl_used_backend;
8361 // don't affect maxconn automatic computation
8362 socket_ssl.xprt->prepare_srv(&socket_ssl);
8363 global.ssl_used_backend = saved_used_backed;
8364 }
8365#endif
8366
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008367 hlua_fcn_post_init(gL.T);
8368 RESET_SAFE_LJMP(gL.T);
8369
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008370 list_for_each_entry(init, &hlua_init_functions, l) {
8371 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
8372 ret = hlua_ctx_resume(&gL, 0);
8373 switch (ret) {
8374 case HLUA_E_OK:
8375 lua_pop(gL.T, -1);
8376 return 1;
8377 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008378 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008379 return 0;
8380 case HLUA_E_ERRMSG:
8381 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008382 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008383 return 0;
8384 case HLUA_E_ERR:
8385 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008386 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008387 return 0;
8388 }
8389 }
8390 return 1;
8391}
8392
Willy Tarreau32f61e22015-03-18 17:54:59 +01008393/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8394 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8395 * is the previously allocated size or the kind of object in case of a new
8396 * allocation. <nsize> is the requested new size.
8397 */
8398static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8399{
8400 struct hlua_mem_allocator *zone = ud;
8401
8402 if (nsize == 0) {
8403 /* it's a free */
8404 if (ptr)
8405 zone->allocated -= osize;
8406 free(ptr);
8407 return NULL;
8408 }
8409
8410 if (!ptr) {
8411 /* it's a new allocation */
8412 if (zone->limit && zone->allocated + nsize > zone->limit)
8413 return NULL;
8414
8415 ptr = malloc(nsize);
8416 if (ptr)
8417 zone->allocated += nsize;
8418 return ptr;
8419 }
8420
8421 /* it's a realloc */
8422 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8423 return NULL;
8424
8425 ptr = realloc(ptr, nsize);
8426 if (ptr)
8427 zone->allocated += nsize - osize;
8428 return ptr;
8429}
8430
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008431/* Ithis function can fail with an abort() due to an Lua critical error.
8432 * We are in the initialisation process of HAProxy, this abort() is
8433 * tolerated.
8434 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008435void hlua_init(void)
8436{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008437 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008438 int idx;
8439 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008440 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008441 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008442 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008443#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008444 struct srv_kw *kw;
8445 int tmp_error;
8446 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008447 char *args[] = { /* SSL client configuration. */
8448 "ssl",
8449 "verify",
8450 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008451 NULL
8452 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008453#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008454
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008455 /* Init main lua stack. */
8456 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008457 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008458 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008459 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008460 hlua_sethlua(&gL);
8461 gL.Tref = LUA_REFNIL;
8462 gL.task = NULL;
8463
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008464 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008465 * the Lua function can fail with an abort. We are in the initialisation
8466 * process of HAProxy, this abort() is tolerated.
8467 */
8468
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008469 /* Initialise lua. */
8470 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008471
Thierry Fournier75933d42016-01-21 09:30:18 +01008472 /* Set safe environment for the initialisation. */
8473 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008474 if (lua_type(gL.T, -1) == LUA_TSTRING)
8475 error_msg = lua_tostring(gL.T, -1);
8476 else
8477 error_msg = "critical error";
8478 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008479 exit(1);
8480 }
8481
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008482 /*
8483 *
8484 * Create "core" object.
8485 *
8486 */
8487
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008488 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008489 lua_newtable(gL.T);
8490
8491 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008492 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008493 hlua_class_const_int(gL.T, log_levels[i], i);
8494
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008495 /* Register special functions. */
8496 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008497 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008498 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008499 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008500 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008501 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008502 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008503 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008504 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008505 hlua_class_function(gL.T, "sleep", hlua_sleep);
8506 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008507 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8508 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8509 hlua_class_function(gL.T, "set_map", hlua_set_map);
8510 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008511 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008512 hlua_class_function(gL.T, "log", hlua_log);
8513 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8514 hlua_class_function(gL.T, "Info", hlua_log_info);
8515 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8516 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008517 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008518 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008519
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008520 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008521
8522 /*
8523 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008524 * Register class Map
8525 *
8526 */
8527
8528 /* This table entry is the object "Map" base. */
8529 lua_newtable(gL.T);
8530
8531 /* register pattern types. */
8532 for (i=0; i<PAT_MATCH_NUM; i++)
8533 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008534 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008535 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8536 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008537 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008538
8539 /* register constructor. */
8540 hlua_class_function(gL.T, "new", hlua_map_new);
8541
8542 /* Create and fill the metatable. */
8543 lua_newtable(gL.T);
8544
8545 /* Create and fille the __index entry. */
8546 lua_pushstring(gL.T, "__index");
8547 lua_newtable(gL.T);
8548
8549 /* Register . */
8550 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8551 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8552
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008553 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008554
Thierry Fournier45e78d72016-02-19 18:34:46 +01008555 /* Register previous table in the registry with reference and named entry.
8556 * The function hlua_register_metatable() pops the stack, so we
8557 * previously create a copy of the table.
8558 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008559 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008560 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008561
8562 /* Assign the metatable to the mai Map object. */
8563 lua_setmetatable(gL.T, -2);
8564
8565 /* Set a name to the table. */
8566 lua_setglobal(gL.T, "Map");
8567
8568 /*
8569 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008570 * Register class Channel
8571 *
8572 */
8573
8574 /* Create and fill the metatable. */
8575 lua_newtable(gL.T);
8576
8577 /* Create and fille the __index entry. */
8578 lua_pushstring(gL.T, "__index");
8579 lua_newtable(gL.T);
8580
8581 /* Register . */
8582 hlua_class_function(gL.T, "get", hlua_channel_get);
8583 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8584 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8585 hlua_class_function(gL.T, "set", hlua_channel_set);
8586 hlua_class_function(gL.T, "append", hlua_channel_append);
8587 hlua_class_function(gL.T, "send", hlua_channel_send);
8588 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8589 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8590 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008591 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008592
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008593 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008594
8595 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008596 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008597
8598 /*
8599 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008600 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008601 *
8602 */
8603
8604 /* Create and fill the metatable. */
8605 lua_newtable(gL.T);
8606
8607 /* Create and fille the __index entry. */
8608 lua_pushstring(gL.T, "__index");
8609 lua_newtable(gL.T);
8610
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008611 /* Browse existing fetches and create the associated
8612 * object method.
8613 */
8614 sf = NULL;
8615 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8616
8617 /* Dont register the keywork if the arguments check function are
8618 * not safe during the runtime.
8619 */
8620 if ((sf->val_args != NULL) &&
8621 (sf->val_args != val_payload_lv) &&
8622 (sf->val_args != val_hdr))
8623 continue;
8624
8625 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8626 * by an underscore.
8627 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008628 strncpy(trash.area, sf->kw, trash.size);
8629 trash.area[trash.size - 1] = '\0';
8630 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008631 if (*p == '.' || *p == '-' || *p == '+')
8632 *p = '_';
8633
8634 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008635 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008636 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008637 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008638 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008639 }
8640
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008641 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008642
8643 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008644 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008645
8646 /*
8647 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008648 * Register class Converters
8649 *
8650 */
8651
8652 /* Create and fill the metatable. */
8653 lua_newtable(gL.T);
8654
8655 /* Create and fill the __index entry. */
8656 lua_pushstring(gL.T, "__index");
8657 lua_newtable(gL.T);
8658
8659 /* Browse existing converters and create the associated
8660 * object method.
8661 */
8662 sc = NULL;
8663 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8664 /* Dont register the keywork if the arguments check function are
8665 * not safe during the runtime.
8666 */
8667 if (sc->val_args != NULL)
8668 continue;
8669
8670 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8671 * by an underscore.
8672 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008673 strncpy(trash.area, sc->kw, trash.size);
8674 trash.area[trash.size - 1] = '\0';
8675 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008676 if (*p == '.' || *p == '-' || *p == '+')
8677 *p = '_';
8678
8679 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008680 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008681 lua_pushlightuserdata(gL.T, sc);
8682 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008683 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008684 }
8685
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008686 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008687
8688 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008689 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008690
8691 /*
8692 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008693 * Register class HTTP
8694 *
8695 */
8696
8697 /* Create and fill the metatable. */
8698 lua_newtable(gL.T);
8699
8700 /* Create and fille the __index entry. */
8701 lua_pushstring(gL.T, "__index");
8702 lua_newtable(gL.T);
8703
8704 /* Register Lua functions. */
8705 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8706 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8707 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8708 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8709 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8710 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8711 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8712 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8713 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8714 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8715
8716 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8717 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8718 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8719 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8720 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8721 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008722 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008723
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008724 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008725
8726 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008727 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008728
8729 /*
8730 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008731 * Register class AppletTCP
8732 *
8733 */
8734
8735 /* Create and fill the metatable. */
8736 lua_newtable(gL.T);
8737
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008738 /* Create and fille the __index entry. */
8739 lua_pushstring(gL.T, "__index");
8740 lua_newtable(gL.T);
8741
8742 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008743 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8744 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8745 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8746 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8747 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008748 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8749 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8750 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008751
8752 lua_settable(gL.T, -3);
8753
8754 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008755 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008756
8757 /*
8758 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008759 * Register class AppletHTTP
8760 *
8761 */
8762
8763 /* Create and fill the metatable. */
8764 lua_newtable(gL.T);
8765
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008766 /* Create and fille the __index entry. */
8767 lua_pushstring(gL.T, "__index");
8768 lua_newtable(gL.T);
8769
8770 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008771 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8772 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008773 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8774 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8775 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008776 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8777 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8778 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8779 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8780 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8781 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8782
8783 lua_settable(gL.T, -3);
8784
8785 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008786 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008787
8788 /*
8789 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008790 * Register class TXN
8791 *
8792 */
8793
8794 /* Create and fill the metatable. */
8795 lua_newtable(gL.T);
8796
8797 /* Create and fille the __index entry. */
8798 lua_pushstring(gL.T, "__index");
8799 lua_newtable(gL.T);
8800
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008801 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008802 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8803 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8804 hlua_class_function(gL.T, "set_var", hlua_set_var);
8805 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8806 hlua_class_function(gL.T, "get_var", hlua_get_var);
8807 hlua_class_function(gL.T, "done", hlua_txn_done);
8808 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8809 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8810 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8811 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8812 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8813 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8814 hlua_class_function(gL.T, "log", hlua_txn_log);
8815 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8816 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8817 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8818 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008819
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008820 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008821
8822 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008823 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008824
8825 /*
8826 *
8827 * Register class Socket
8828 *
8829 */
8830
8831 /* Create and fill the metatable. */
8832 lua_newtable(gL.T);
8833
8834 /* Create and fille the __index entry. */
8835 lua_pushstring(gL.T, "__index");
8836 lua_newtable(gL.T);
8837
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008838#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008839 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008840#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008841 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8842 hlua_class_function(gL.T, "send", hlua_socket_send);
8843 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8844 hlua_class_function(gL.T, "close", hlua_socket_close);
8845 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8846 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8847 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8848 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8849
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008850 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008851
8852 /* Register the garbage collector entry. */
8853 lua_pushstring(gL.T, "__gc");
8854 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008855 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008856
8857 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008858 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008859
8860 /* Proxy and server configuration initialisation. */
8861 memset(&socket_proxy, 0, sizeof(socket_proxy));
8862 init_new_proxy(&socket_proxy);
8863 socket_proxy.parent = NULL;
8864 socket_proxy.last_change = now.tv_sec;
8865 socket_proxy.id = "LUA-SOCKET";
8866 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8867 socket_proxy.maxconn = 0;
8868 socket_proxy.accept = NULL;
8869 socket_proxy.options2 |= PR_O2_INDEPSTR;
8870 socket_proxy.srv = NULL;
8871 socket_proxy.conn_retries = 0;
8872 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8873
8874 /* Init TCP server: unchanged parameters */
8875 memset(&socket_tcp, 0, sizeof(socket_tcp));
8876 socket_tcp.next = NULL;
8877 socket_tcp.proxy = &socket_proxy;
8878 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8879 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008880 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008881 socket_tcp.priv_conns = NULL;
8882 socket_tcp.idle_conns = NULL;
8883 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008884 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008885 socket_tcp.last_change = 0;
8886 socket_tcp.id = "LUA-TCP-CONN";
8887 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8888 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8889 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8890
8891 /* XXX: Copy default parameter from default server,
8892 * but the default server is not initialized.
8893 */
8894 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8895 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8896 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8897 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8898 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8899 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8900 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8901 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8902 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8903 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8904
8905 socket_tcp.check.status = HCHK_STATUS_INI;
8906 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8907 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8908 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8909 socket_tcp.check.server = &socket_tcp;
8910
8911 socket_tcp.agent.status = HCHK_STATUS_INI;
8912 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8913 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8914 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8915 socket_tcp.agent.server = &socket_tcp;
8916
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008917 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008918
8919#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008920 /* Init TCP server: unchanged parameters */
8921 memset(&socket_ssl, 0, sizeof(socket_ssl));
8922 socket_ssl.next = NULL;
8923 socket_ssl.proxy = &socket_proxy;
8924 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8925 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008926 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008927 socket_ssl.priv_conns = NULL;
8928 socket_ssl.idle_conns = NULL;
8929 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008930 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008931 socket_ssl.last_change = 0;
8932 socket_ssl.id = "LUA-SSL-CONN";
8933 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8934 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8935 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8936
8937 /* XXX: Copy default parameter from default server,
8938 * but the default server is not initialized.
8939 */
8940 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8941 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8942 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8943 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8944 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8945 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8946 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8947 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8948 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8949 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8950
8951 socket_ssl.check.status = HCHK_STATUS_INI;
8952 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8953 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8954 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8955 socket_ssl.check.server = &socket_ssl;
8956
8957 socket_ssl.agent.status = HCHK_STATUS_INI;
8958 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8959 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8960 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8961 socket_ssl.agent.server = &socket_ssl;
8962
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008963 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008964 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008965
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008966 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008967 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8968 /*
8969 *
8970 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008971 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008972 * features like client certificates and ssl_verify.
8973 *
8974 */
8975 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8976 if (tmp_error != 0) {
8977 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8978 abort(); /* This must be never arrives because the command line
8979 not editable by the user. */
8980 }
8981 idx += kw->skip;
8982 }
8983 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008984#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008985
8986 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008987}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008988
Willy Tarreau80713382018-11-26 10:19:54 +01008989static void hlua_register_build_options(void)
8990{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008991 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008992
Willy Tarreaubb57d942016-12-21 19:04:56 +01008993 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8994 hap_register_build_opts(ptr, 1);
8995}
Willy Tarreau80713382018-11-26 10:19:54 +01008996
8997INITCALL0(STG_REGISTER, hlua_register_build_options);