blob: 1e70a4be10f1e6eff72dc20d0caf5ddb3f6092d9 [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))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200875 get_localtime(date.tv_sec, &tm);
876 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100877 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200878 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100879 fflush(stderr);
880 }
881}
882
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100883/* This function just ensure that the yield will be always
884 * returned with a timeout and permit to set some flags
885 */
886__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100887 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100888{
889 struct hlua *hlua = hlua_gethlua(L);
890
891 /* Set the wake timeout. If timeout is required, we set
892 * the expiration time.
893 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200894 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100895
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100896 hlua->flags |= flags;
897
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100898 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200899 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100900}
901
Willy Tarreau87b09662015-04-03 00:22:06 +0200902/* This function initialises the Lua environment stored in the stream.
903 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100904 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200905 *
906 * This function is particular. it initialises a new Lua thread. If the
907 * initialisation fails (example: out of memory error), the lua function
908 * throws an error (longjmp).
909 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100910 * In some case (at least one), this function can be called from safe
911 * environement, so we must not initialise it. While the support of
912 * threads appear, the safe environment set a lock to ensure only one
913 * Lua execution at a time. If we initialize safe environment in another
914 * safe environmenet, we have a dead lock.
915 *
916 * set "already_safe" true if the context is initialized form safe
917 * Lua fonction.
918 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800919 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200920 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800921 * MUST NOT manipulate the created thread stack state, because it is not
922 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100923 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100924int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100925{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100926 if (!already_safe) {
927 if (!SET_SAFE_LJMP(gL.T)) {
928 lua->Tref = LUA_REFNIL;
929 return 0;
930 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200931 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100932 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100933 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100934 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100935 lua->T = lua_newthread(gL.T);
936 if (!lua->T) {
937 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100938 if (!already_safe)
939 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100940 return 0;
941 }
942 hlua_sethlua(lua);
943 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
944 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100945 if (!already_safe)
946 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100947 return 1;
948}
949
Willy Tarreau87b09662015-04-03 00:22:06 +0200950/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100951 * is destroyed. The destroy also the memory context. The struct "lua"
952 * is not freed.
953 */
954void hlua_ctx_destroy(struct hlua *lua)
955{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100956 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100957 return;
958
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100959 if (!lua->T)
960 goto end;
961
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100962 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200963 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100964
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200965 if (!SET_SAFE_LJMP(lua->T))
966 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100967 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200968 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200969
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200970 if (!SET_SAFE_LJMP(gL.T))
971 return;
972 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
973 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200974 /* Forces a garbage collecting process. If the Lua program is finished
975 * without error, we run the GC on the thread pointer. Its freed all
976 * the unused memory.
977 * If the thread is finnish with an error or is currently yielded,
978 * it seems that the GC applied on the thread doesn't clean anything,
979 * so e run the GC on the main thread.
980 * NOTE: maybe this action locks all the Lua threads untiml the en of
981 * the garbage collection.
982 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200983 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200984 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200985 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200986 lua_gc(gL.T, LUA_GCCOLLECT, 0);
987 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200988 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200989
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200990 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100991
992end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100993 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100994}
995
996/* This function is used to restore the Lua context when a coroutine
997 * fails. This function copy the common memory between old coroutine
998 * and the new coroutine. The old coroutine is destroyed, and its
999 * replaced by the new coroutine.
1000 * If the flag "keep_msg" is set, the last entry of the old is assumed
1001 * as string error message and it is copied in the new stack.
1002 */
1003static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1004{
1005 lua_State *T;
1006 int new_ref;
1007
1008 /* Renew the main LUA stack doesn't have sense. */
1009 if (lua == &gL)
1010 return 0;
1011
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001012 /* New Lua coroutine. */
1013 T = lua_newthread(gL.T);
1014 if (!T)
1015 return 0;
1016
1017 /* Copy last error message. */
1018 if (keep_msg)
1019 lua_xmove(lua->T, T, 1);
1020
1021 /* Copy data between the coroutines. */
1022 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1023 lua_xmove(lua->T, T, 1);
1024 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
1025
1026 /* Destroy old data. */
1027 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1028
1029 /* The thread is garbage collected by Lua. */
1030 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1031
1032 /* Fill the struct with the new coroutine values. */
1033 lua->Mref = new_ref;
1034 lua->T = T;
1035 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1036
1037 /* Set context. */
1038 hlua_sethlua(lua);
1039
1040 return 1;
1041}
1042
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001043void hlua_hook(lua_State *L, lua_Debug *ar)
1044{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001045 struct hlua *hlua = hlua_gethlua(L);
1046
1047 /* Lua cannot yield when its returning from a function,
1048 * so, we can fix the interrupt hook to 1 instruction,
1049 * expecting that the function is finnished.
1050 */
1051 if (lua_gethookmask(L) & LUA_MASKRET) {
1052 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1053 return;
1054 }
1055
1056 /* restore the interrupt condition. */
1057 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1058
1059 /* If we interrupt the Lua processing in yieldable state, we yield.
1060 * If the state is not yieldable, trying yield causes an error.
1061 */
1062 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001063 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001064
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001065 /* If we cannot yield, update the clock and check the timeout. */
1066 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001067 hlua->run_time += now_ms - hlua->start_time;
1068 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001069 lua_pushfstring(L, "execution timeout");
1070 WILL_LJMP(lua_error(L));
1071 }
1072
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001073 /* Update the start time. */
1074 hlua->start_time = now_ms;
1075
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001076 /* Try to interrupt the process at the end of the current
1077 * unyieldable function.
1078 */
1079 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001080}
1081
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001082/* This function start or resumes the Lua stack execution. If the flag
1083 * "yield_allowed" if no set and the LUA stack execution returns a yield
1084 * The function return an error.
1085 *
1086 * The function can returns 4 values:
1087 * - HLUA_E_OK : The execution is terminated without any errors.
1088 * - HLUA_E_AGAIN : The execution must continue at the next associated
1089 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001090 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001091 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001092 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001093 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001094 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001095 * LUA code.
1096 */
1097static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1098{
Christopher Faulet20699902020-07-28 10:33:25 +02001099#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1100 int nres;
1101#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001102 int ret;
1103 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001104 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001105
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001106 /* Initialise run time counter. */
1107 if (!HLUA_IS_RUNNING(lua))
1108 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001109
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001110 /* Lock the whole Lua execution. This lock must be before the
1111 * label "resume_execution".
1112 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001113 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001114
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001115resume_execution:
1116
1117 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1118 * instructions. it is used for preventing infinite loops.
1119 */
1120 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1121
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001122 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001123 HLUA_SET_RUN(lua);
1124 HLUA_CLR_CTRLYIELD(lua);
1125 HLUA_CLR_WAKERESWR(lua);
1126 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001127
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001128 /* Update the start time. */
1129 lua->start_time = now_ms;
1130
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001131 /* Call the function. */
Christopher Faulet20699902020-07-28 10:33:25 +02001132#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1133 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1134#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001135 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet20699902020-07-28 10:33:25 +02001136#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001137 switch (ret) {
1138
1139 case LUA_OK:
1140 ret = HLUA_E_OK;
1141 break;
1142
1143 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001144 /* Check if the execution timeout is expired. It it is the case, we
1145 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001146 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001147 tv_update_date(0, 1);
1148 lua->run_time += now_ms - lua->start_time;
1149 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001150 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001151 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001152 break;
1153 }
1154 /* Process the forced yield. if the general yield is not allowed or
1155 * if no task were associated this the current Lua execution
1156 * coroutine, we resume the execution. Else we want to return in the
1157 * scheduler and we want to be waked up again, to continue the
1158 * current Lua execution. So we schedule our own task.
1159 */
1160 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001161 if (!yield_allowed || !lua->task)
1162 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001163 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001164 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001165 if (!yield_allowed) {
1166 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001167 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001168 break;
1169 }
1170 ret = HLUA_E_AGAIN;
1171 break;
1172
1173 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001174
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001175 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001176 * because the errors ares the only one mean to return immediately
1177 * from and lua execution.
1178 */
1179 if (lua->flags & HLUA_EXIT) {
1180 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001181 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001182 break;
1183 }
1184
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001185 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001186 if (!lua_checkstack(lua->T, 1)) {
1187 ret = HLUA_E_ERR;
1188 break;
1189 }
1190 msg = lua_tostring(lua->T, -1);
1191 lua_settop(lua->T, 0); /* Empty the stack. */
1192 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001193 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001194 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001195 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001196 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001197 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001198 ret = HLUA_E_ERRMSG;
1199 break;
1200
1201 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001202 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001203 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001204 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001205 break;
1206
1207 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001208 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001209 if (!lua_checkstack(lua->T, 1)) {
1210 ret = HLUA_E_ERR;
1211 break;
1212 }
1213 msg = lua_tostring(lua->T, -1);
1214 lua_settop(lua->T, 0); /* Empty the stack. */
1215 lua_pop(lua->T, 1);
1216 if (msg)
1217 lua_pushfstring(lua->T, "message handler error: %s", msg);
1218 else
1219 lua_pushfstring(lua->T, "message handler error");
1220 ret = HLUA_E_ERRMSG;
1221 break;
1222
1223 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001224 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001225 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001226 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001227 break;
1228 }
1229
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001230 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001231 if (lua->flags & HLUA_MUST_GC &&
1232 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001233 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1234
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001235 switch (ret) {
1236 case HLUA_E_AGAIN:
1237 break;
1238
1239 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001240 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001241 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001242 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001243 break;
1244
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001245 case HLUA_E_ETMOUT:
1246 case HLUA_E_NOMEM:
1247 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001248 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001249 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001250 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001251 hlua_ctx_renew(lua, 0);
1252 break;
1253
1254 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001255 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001256 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001257 break;
1258 }
1259
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001260 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001261 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001262
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001263 return ret;
1264}
1265
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001266/* This function exit the current code. */
1267__LJMP static int hlua_done(lua_State *L)
1268{
1269 struct hlua *hlua = hlua_gethlua(L);
1270
1271 hlua->flags |= HLUA_EXIT;
1272 WILL_LJMP(lua_error(L));
1273
1274 return 0;
1275}
1276
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001277/* This function is an LUA binding. It provides a function
1278 * for deleting ACL from a referenced ACL file.
1279 */
1280__LJMP static int hlua_del_acl(lua_State *L)
1281{
1282 const char *name;
1283 const char *key;
1284 struct pat_ref *ref;
1285
1286 MAY_LJMP(check_args(L, 2, "del_acl"));
1287
1288 name = MAY_LJMP(luaL_checkstring(L, 1));
1289 key = MAY_LJMP(luaL_checkstring(L, 2));
1290
1291 ref = pat_ref_lookup(name);
1292 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001293 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001294
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001295 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001296 pat_ref_delete(ref, key);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001297 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001298 return 0;
1299}
1300
1301/* This function is an LUA binding. It provides a function
1302 * for deleting map entry from a referenced map file.
1303 */
1304static int hlua_del_map(lua_State *L)
1305{
1306 const char *name;
1307 const char *key;
1308 struct pat_ref *ref;
1309
1310 MAY_LJMP(check_args(L, 2, "del_map"));
1311
1312 name = MAY_LJMP(luaL_checkstring(L, 1));
1313 key = MAY_LJMP(luaL_checkstring(L, 2));
1314
1315 ref = pat_ref_lookup(name);
1316 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001317 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001318
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001319 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001320 pat_ref_delete(ref, key);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001321 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001322 return 0;
1323}
1324
1325/* This function is an LUA binding. It provides a function
1326 * for adding ACL pattern from a referenced ACL file.
1327 */
1328static int hlua_add_acl(lua_State *L)
1329{
1330 const char *name;
1331 const char *key;
1332 struct pat_ref *ref;
1333
1334 MAY_LJMP(check_args(L, 2, "add_acl"));
1335
1336 name = MAY_LJMP(luaL_checkstring(L, 1));
1337 key = MAY_LJMP(luaL_checkstring(L, 2));
1338
1339 ref = pat_ref_lookup(name);
1340 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001341 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001342
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001343 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001344 if (pat_ref_find_elt(ref, key) == NULL)
1345 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001346 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001347 return 0;
1348}
1349
1350/* This function is an LUA binding. It provides a function
1351 * for setting map pattern and sample from a referenced map
1352 * file.
1353 */
1354static int hlua_set_map(lua_State *L)
1355{
1356 const char *name;
1357 const char *key;
1358 const char *value;
1359 struct pat_ref *ref;
1360
1361 MAY_LJMP(check_args(L, 3, "set_map"));
1362
1363 name = MAY_LJMP(luaL_checkstring(L, 1));
1364 key = MAY_LJMP(luaL_checkstring(L, 2));
1365 value = MAY_LJMP(luaL_checkstring(L, 3));
1366
1367 ref = pat_ref_lookup(name);
1368 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001369 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001370
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001371 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001372 if (pat_ref_find_elt(ref, key) != NULL)
1373 pat_ref_set(ref, key, value, NULL);
1374 else
1375 pat_ref_add(ref, key, value, NULL);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001376 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001377 return 0;
1378}
1379
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001380/* A class is a lot of memory that contain data. This data can be a table,
1381 * an integer or user data. This data is associated with a metatable. This
1382 * metatable have an original version registred in the global context with
1383 * the name of the object (_G[<name>] = <metable> ).
1384 *
1385 * A metable is a table that modify the standard behavior of a standard
1386 * access to the associated data. The entries of this new metatable are
1387 * defined as is:
1388 *
1389 * http://lua-users.org/wiki/MetatableEvents
1390 *
1391 * __index
1392 *
1393 * we access an absent field in a table, the result is nil. This is
1394 * true, but it is not the whole truth. Actually, such access triggers
1395 * the interpreter to look for an __index metamethod: If there is no
1396 * such method, as usually happens, then the access results in nil;
1397 * otherwise, the metamethod will provide the result.
1398 *
1399 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1400 * the key does not appear in the table, but the metatable has an __index
1401 * property:
1402 *
1403 * - if the value is a function, the function is called, passing in the
1404 * table and the key; the return value of that function is returned as
1405 * the result.
1406 *
1407 * - if the value is another table, the value of the key in that table is
1408 * asked for and returned (and if it doesn't exist in that table, but that
1409 * table's metatable has an __index property, then it continues on up)
1410 *
1411 * - Use "rawget(myTable,key)" to skip this metamethod.
1412 *
1413 * http://www.lua.org/pil/13.4.1.html
1414 *
1415 * __newindex
1416 *
1417 * Like __index, but control property assignment.
1418 *
1419 * __mode - Control weak references. A string value with one or both
1420 * of the characters 'k' and 'v' which specifies that the the
1421 * keys and/or values in the table are weak references.
1422 *
1423 * __call - Treat a table like a function. When a table is followed by
1424 * parenthesis such as "myTable( 'foo' )" and the metatable has
1425 * a __call key pointing to a function, that function is invoked
1426 * (passing any specified arguments) and the return value is
1427 * returned.
1428 *
1429 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1430 * called, if the metatable for myTable has a __metatable
1431 * key, the value of that key is returned instead of the
1432 * actual metatable.
1433 *
1434 * __tostring - Control string representation. When the builtin
1435 * "tostring( myTable )" function is called, if the metatable
1436 * for myTable has a __tostring property set to a function,
1437 * that function is invoked (passing myTable to it) and the
1438 * return value is used as the string representation.
1439 *
1440 * __len - Control table length. When the table length is requested using
1441 * the length operator ( '#' ), if the metatable for myTable has
1442 * a __len key pointing to a function, that function is invoked
1443 * (passing myTable to it) and the return value used as the value
1444 * of "#myTable".
1445 *
1446 * __gc - Userdata finalizer code. When userdata is set to be garbage
1447 * collected, if the metatable has a __gc field pointing to a
1448 * function, that function is first invoked, passing the userdata
1449 * to it. The __gc metamethod is not called for tables.
1450 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1451 *
1452 * Special metamethods for redefining standard operators:
1453 * http://www.lua.org/pil/13.1.html
1454 *
1455 * __add "+"
1456 * __sub "-"
1457 * __mul "*"
1458 * __div "/"
1459 * __unm "!"
1460 * __pow "^"
1461 * __concat ".."
1462 *
1463 * Special methods for redfining standar relations
1464 * http://www.lua.org/pil/13.2.html
1465 *
1466 * __eq "=="
1467 * __lt "<"
1468 * __le "<="
1469 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001470
1471/*
1472 *
1473 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001474 * Class Map
1475 *
1476 *
1477 */
1478
1479/* Returns a struct hlua_map if the stack entry "ud" is
1480 * a class session, otherwise it throws an error.
1481 */
1482__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1483{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001484 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001485}
1486
1487/* This function is the map constructor. It don't need
1488 * the class Map object. It creates and return a new Map
1489 * object. It must be called only during "body" or "init"
1490 * context because it process some filesystem accesses.
1491 */
1492__LJMP static int hlua_map_new(struct lua_State *L)
1493{
1494 const char *fn;
1495 int match = PAT_MATCH_STR;
1496 struct sample_conv conv;
1497 const char *file = "";
1498 int line = 0;
1499 lua_Debug ar;
1500 char *err = NULL;
1501 struct arg args[2];
1502
1503 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1504 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1505
1506 fn = MAY_LJMP(luaL_checkstring(L, 1));
1507
1508 if (lua_gettop(L) >= 2) {
1509 match = MAY_LJMP(luaL_checkinteger(L, 2));
1510 if (match < 0 || match >= PAT_MATCH_NUM)
1511 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1512 }
1513
1514 /* Get Lua filename and line number. */
1515 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1516 lua_getinfo(L, "Sl", &ar); /* get info about it */
1517 if (ar.currentline > 0) { /* is there info? */
1518 file = ar.short_src;
1519 line = ar.currentline;
1520 }
1521 }
1522
1523 /* fill fake sample_conv struct. */
1524 conv.kw = ""; /* unused. */
1525 conv.process = NULL; /* unused. */
1526 conv.arg_mask = 0; /* unused. */
1527 conv.val_args = NULL; /* unused. */
1528 conv.out_type = SMP_T_STR;
1529 conv.private = (void *)(long)match;
1530 switch (match) {
1531 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1532 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1533 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1534 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1535 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1536 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1537 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001538 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001539 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1540 default:
1541 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1542 }
1543
1544 /* fill fake args. */
1545 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001546 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001547 args[1].type = ARGT_STOP;
1548
1549 /* load the map. */
1550 if (!sample_load_map(args, &conv, file, line, &err)) {
1551 /* error case: we cant use luaL_error because we must
1552 * free the err variable.
1553 */
1554 luaL_where(L, 1);
1555 lua_pushfstring(L, "'new': %s.", err);
1556 lua_concat(L, 2);
1557 free(err);
1558 WILL_LJMP(lua_error(L));
1559 }
1560
1561 /* create the lua object. */
1562 lua_newtable(L);
1563 lua_pushlightuserdata(L, args[0].data.map);
1564 lua_rawseti(L, -2, 0);
1565
1566 /* Pop a class Map metatable and affect it to the userdata. */
1567 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1568 lua_setmetatable(L, -2);
1569
1570
1571 return 1;
1572}
1573
1574__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1575{
1576 struct map_descriptor *desc;
1577 struct pattern *pat;
1578 struct sample smp;
1579
1580 MAY_LJMP(check_args(L, 2, "lookup"));
1581 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001582 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001583 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001584 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001585 }
1586 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001587 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001588 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001589 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 +02001590 }
1591
1592 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001593 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001594 if (str)
1595 lua_pushstring(L, "");
1596 else
1597 lua_pushnil(L);
1598 return 1;
1599 }
1600
1601 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001602 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001603 return 1;
1604}
1605
1606__LJMP static int hlua_map_lookup(struct lua_State *L)
1607{
1608 return _hlua_map_lookup(L, 0);
1609}
1610
1611__LJMP static int hlua_map_slookup(struct lua_State *L)
1612{
1613 return _hlua_map_lookup(L, 1);
1614}
1615
1616/*
1617 *
1618 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001619 * Class Socket
1620 *
1621 *
1622 */
1623
1624__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1625{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001626 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001627}
1628
1629/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001630 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001631 * received.
1632 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001633static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001634{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001635 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001636 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001637
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001638 if (appctx->ctx.hlua_cosocket.die) {
1639 si_shutw(si);
1640 si_shutr(si);
1641 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001642 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1643 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001644 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1645 }
1646
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001647 /* If the connection object is not available, close all the
1648 * streams and wakeup everything waiting for.
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001649 */
1650 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001651 si_shutw(si);
1652 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001653 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001654 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1655 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001656 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001657 }
1658
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001659 /* If we cant write, wakeup the pending write signals. */
1660 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001661 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001662
1663 /* If we cant read, wakeup the pending read signals. */
1664 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001665 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001666
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001667 /* if the connection is not estabkished, inform the stream that we want
1668 * to be notified whenever the connection completes.
1669 */
1670 if (!(c->flags & CO_FL_CONNECTED)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001671 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001672 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001673 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001674 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001675 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001676
1677 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001678 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001679
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001680 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001681 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001682 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001683
1684 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001685 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001686 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001687
1688 /* Some data were injected in the buffer, notify the stream
1689 * interface.
1690 */
1691 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001692 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001693
1694 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001695 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001696 */
1697 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001698 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699}
1700
Willy Tarreau87b09662015-04-03 00:22:06 +02001701/* This function is called when the "struct stream" is destroyed.
1702 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703 * Wake all the pending signals.
1704 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001705static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001706{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001707 struct xref *peer;
1708
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001709 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001710 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1711 if (peer)
1712 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001713
1714 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001715 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1716 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001717}
1718
1719/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001720 * uses this object. If the stream does not exists, just quit.
1721 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001722 * pending signal can rest in the read and write lists. destroy
1723 * it.
1724 */
1725__LJMP static int hlua_socket_gc(lua_State *L)
1726{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001727 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001728 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001729 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001730
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001731 MAY_LJMP(check_args(L, 1, "__gc"));
1732
1733 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001734 peer = xref_get_peer_and_lock(&socket->xref);
1735 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001736 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001737 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001738
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001739 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001740 appctx->ctx.hlua_cosocket.die = 1;
1741 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001742
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001743 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001744 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001745 return 0;
1746}
1747
1748/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001749 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001750 */
sada05ed3302018-05-11 11:48:18 -07001751__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001752{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001753 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001754 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001755 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001756
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001757 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001758
1759 /* Check if we run on the same thread than the xreator thread.
1760 * We cannot access to the socket if the thread is different.
1761 */
1762 if (socket->tid != tid)
1763 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1764
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001765 peer = xref_get_peer_and_lock(&socket->xref);
1766 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001767 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001768 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001769
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001770 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001771 appctx->ctx.hlua_cosocket.die = 1;
1772 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001773
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001774 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001775 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001776 return 0;
1777}
1778
sada05ed3302018-05-11 11:48:18 -07001779/* The close function calls close_helper.
1780 */
1781__LJMP static int hlua_socket_close(lua_State *L)
1782{
1783 MAY_LJMP(check_args(L, 1, "close"));
1784 return hlua_socket_close_helper(L);
1785}
1786
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001787/* This Lua function assumes that the stack contain three parameters.
1788 * 1 - USERDATA containing a struct socket
1789 * 2 - INTEGER with values of the macro defined below
1790 * If the integer is -1, we must read at most one line.
1791 * If the integer is -2, we ust read all the data until the
1792 * end of the stream.
1793 * If the integer is positive value, we must read a number of
1794 * bytes corresponding to this value.
1795 */
1796#define HLSR_READ_LINE (-1)
1797#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001798__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001799{
1800 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1801 int wanted = lua_tointeger(L, 2);
1802 struct hlua *hlua = hlua_gethlua(L);
1803 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001804 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001805 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001806 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001807 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001808 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001809 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001810 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001811 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001812 struct stream_interface *si;
1813 struct stream *s;
1814 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001815 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001816
1817 /* Check if this lua stack is schedulable. */
1818 if (!hlua || !hlua->task)
1819 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1820 "'frontend', 'backend' or 'task'"));
1821
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001822 /* Check if we run on the same thread than the xreator thread.
1823 * We cannot access to the socket if the thread is different.
1824 */
1825 if (socket->tid != tid)
1826 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1827
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001828 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001829 peer = xref_get_peer_and_lock(&socket->xref);
1830 if (!peer)
1831 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001832 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1833 si = appctx->owner;
1834 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001835
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001836 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001837 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001838 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001839 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001840 if (nblk < 0) /* Connection close. */
1841 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001842 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001843 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001844
1845 /* remove final \r\n. */
1846 if (nblk == 1) {
1847 if (blk1[len1-1] == '\n') {
1848 len1--;
1849 skip_at_end++;
1850 if (blk1[len1-1] == '\r') {
1851 len1--;
1852 skip_at_end++;
1853 }
1854 }
1855 }
1856 else {
1857 if (blk2[len2-1] == '\n') {
1858 len2--;
1859 skip_at_end++;
1860 if (blk2[len2-1] == '\r') {
1861 len2--;
1862 skip_at_end++;
1863 }
1864 }
1865 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001866 }
1867
1868 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001869 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001870 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001871 if (nblk < 0) /* Connection close. */
1872 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001873 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001874 goto connection_empty;
1875 }
1876
1877 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001878 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001879 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001880 if (nblk < 0) /* Connection close. */
1881 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001882 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001883 goto connection_empty;
1884
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001885 missing_bytes = wanted - socket->b.n;
1886 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001887 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001888 len1 = missing_bytes;
1889 } if (nblk == 2 && len1 + len2 > missing_bytes)
1890 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001891 }
1892
1893 len = len1;
1894
1895 luaL_addlstring(&socket->b, blk1, len1);
1896 if (nblk == 2) {
1897 len += len2;
1898 luaL_addlstring(&socket->b, blk2, len2);
1899 }
1900
1901 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001902 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001903
1904 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001905 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906
1907 /* If the pattern reclaim to read all the data
1908 * in the connection, got out.
1909 */
1910 if (wanted == HLSR_READ_ALL)
1911 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001912 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001913 goto connection_empty;
1914
1915 /* Return result. */
1916 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001917 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001918 return 1;
1919
1920connection_closed:
1921
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001922 xref_unlock(&socket->xref, peer);
1923
1924no_peer:
1925
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001926 /* If the buffer containds data. */
1927 if (socket->b.n > 0) {
1928 luaL_pushresult(&socket->b);
1929 return 1;
1930 }
1931 lua_pushnil(L);
1932 lua_pushstring(L, "connection closed.");
1933 return 2;
1934
1935connection_empty:
1936
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001937 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1938 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001939 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001940 }
1941 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001942 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001943 return 0;
1944}
1945
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001946/* This Lua function gets two parameters. The first one can be string
1947 * or a number. If the string is "*l", the user requires one line. If
1948 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001949 * If the value is a number, the user require a number of bytes equal
1950 * to the value. The default value is "*l" (a line).
1951 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001952 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001953 * integer takes this values:
1954 * -1 : read a line
1955 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001956 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001957 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001958 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001959 * concatenated with the read data.
1960 */
1961__LJMP static int hlua_socket_receive(struct lua_State *L)
1962{
1963 int wanted = HLSR_READ_LINE;
1964 const char *pattern;
1965 int type;
1966 char *error;
1967 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001968 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001969
1970 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1971 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1972
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001973 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001974
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001975 /* Check if we run on the same thread than the xreator thread.
1976 * We cannot access to the socket if the thread is different.
1977 */
1978 if (socket->tid != tid)
1979 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1980
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001981 /* check for pattern. */
1982 if (lua_gettop(L) >= 2) {
1983 type = lua_type(L, 2);
1984 if (type == LUA_TSTRING) {
1985 pattern = lua_tostring(L, 2);
1986 if (strcmp(pattern, "*a") == 0)
1987 wanted = HLSR_READ_ALL;
1988 else if (strcmp(pattern, "*l") == 0)
1989 wanted = HLSR_READ_LINE;
1990 else {
1991 wanted = strtoll(pattern, &error, 10);
1992 if (*error != '\0')
1993 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1994 }
1995 }
1996 else if (type == LUA_TNUMBER) {
1997 wanted = lua_tointeger(L, 2);
1998 if (wanted < 0)
1999 WILL_LJMP(luaL_error(L, "Unsupported size."));
2000 }
2001 }
2002
2003 /* Set pattern. */
2004 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002005
2006 /* Check if we would replace the top by itself. */
2007 if (lua_gettop(L) != 2)
2008 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002009
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002010 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002011 luaL_buffinit(L, &socket->b);
2012
2013 /* Check prefix. */
2014 if (lua_gettop(L) >= 3) {
2015 if (lua_type(L, 3) != LUA_TSTRING)
2016 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2017 pattern = lua_tolstring(L, 3, &len);
2018 luaL_addlstring(&socket->b, pattern, len);
2019 }
2020
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002021 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002022}
2023
2024/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002025 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002026 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002027static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002028{
2029 struct hlua_socket *socket;
2030 struct hlua *hlua = hlua_gethlua(L);
2031 struct appctx *appctx;
2032 size_t buf_len;
2033 const char *buf;
2034 int len;
2035 int send_len;
2036 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002037 struct xref *peer;
2038 struct stream_interface *si;
2039 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002040
2041 /* Check if this lua stack is schedulable. */
2042 if (!hlua || !hlua->task)
2043 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2044 "'frontend', 'backend' or 'task'"));
2045
2046 /* Get object */
2047 socket = MAY_LJMP(hlua_checksocket(L, 1));
2048 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002049 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002050
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002051 /* Check if we run on the same thread than the xreator thread.
2052 * We cannot access to the socket if the thread is different.
2053 */
2054 if (socket->tid != tid)
2055 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2056
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002057 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002058 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002059 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002060 lua_pushinteger(L, -1);
2061 return 1;
2062 }
2063 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2064 si = appctx->owner;
2065 s = si_strm(si);
2066
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002067 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002068 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002069 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002070 lua_pushinteger(L, -1);
2071 return 1;
2072 }
2073
2074 /* Update the input buffer data. */
2075 buf += sent;
2076 send_len = buf_len - sent;
2077
2078 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002079 if (sent >= buf_len) {
2080 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002081 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002082 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002083
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002084 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002085 * the request buffer if its not required.
2086 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002087 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002088 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002089 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002090 }
2091
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002092 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002093 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002094 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002095 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002096 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002097
2098 /* send data */
2099 if (len < send_len)
2100 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002101 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002102
2103 /* "Not enough space" (-1), "Buffer too little to contain
2104 * the data" (-2) are not expected because the available length
2105 * is tested.
2106 * Other unknown error are also not expected.
2107 */
2108 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002109 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002110 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002111
sada05ed3302018-05-11 11:48:18 -07002112 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002113 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002114 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002115 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002116 return 1;
2117 }
2118
2119 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002120 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002121
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002122 s->req.rex = TICK_ETERNITY;
2123 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002124
2125 /* Update length sent. */
2126 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002127 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002128
2129 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002130 if (sent + len >= buf_len) {
2131 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002132 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002133 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002134
2135hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002136 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2137 xref_unlock(&socket->xref, peer);
2138 WILL_LJMP(luaL_error(L, "out of memory"));
2139 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002140 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002141 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002142 return 0;
2143}
2144
2145/* This function initiate the send of data. It just check the input
2146 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002147 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002148 * "hlua_socket_write_yield" that can yield.
2149 *
2150 * The Lua function gets between 3 and 4 parameters. The first one is
2151 * the associated object. The second is a string buffer. The third is
2152 * a facultative integer that represents where is the buffer position
2153 * of the start of the data that can send. The first byte is the
2154 * position "1". The default value is "1". The fourth argument is a
2155 * facultative integer that represents where is the buffer position
2156 * of the end of the data that can send. The default is the last byte.
2157 */
2158static int hlua_socket_send(struct lua_State *L)
2159{
2160 int i;
2161 int j;
2162 const char *buf;
2163 size_t buf_len;
2164
2165 /* Check number of arguments. */
2166 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2167 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2168
2169 /* Get the string. */
2170 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2171
2172 /* Get and check j. */
2173 if (lua_gettop(L) == 4) {
2174 j = MAY_LJMP(luaL_checkinteger(L, 4));
2175 if (j < 0)
2176 j = buf_len + j + 1;
2177 if (j > buf_len)
2178 j = buf_len + 1;
2179 lua_pop(L, 1);
2180 }
2181 else
2182 j = buf_len;
2183
2184 /* Get and check i. */
2185 if (lua_gettop(L) == 3) {
2186 i = MAY_LJMP(luaL_checkinteger(L, 3));
2187 if (i < 0)
2188 i = buf_len + i + 1;
2189 if (i > buf_len)
2190 i = buf_len + 1;
2191 lua_pop(L, 1);
2192 } else
2193 i = 1;
2194
2195 /* Check bth i and j. */
2196 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002197 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002198 return 1;
2199 }
2200 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002201 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002202 return 1;
2203 }
2204 if (i == 0)
2205 i = 1;
2206 if (j == 0)
2207 j = 1;
2208
2209 /* Pop the string. */
2210 lua_pop(L, 1);
2211
2212 /* Update the buffer length. */
2213 buf += i - 1;
2214 buf_len = j - i + 1;
2215 lua_pushlstring(L, buf, buf_len);
2216
2217 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002218 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002219
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002220 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002221}
2222
Willy Tarreau22b0a682015-06-17 19:43:49 +02002223#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002224__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2225{
2226 static char buffer[SOCKET_INFO_MAX_LEN];
2227 int ret;
2228 int len;
2229 char *p;
2230
2231 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2232 if (ret <= 0) {
2233 lua_pushnil(L);
2234 return 1;
2235 }
2236
2237 if (ret == AF_UNIX) {
2238 lua_pushstring(L, buffer+1);
2239 return 1;
2240 }
2241 else if (ret == AF_INET6) {
2242 buffer[0] = '[';
2243 len = strlen(buffer);
2244 buffer[len] = ']';
2245 len++;
2246 buffer[len] = ':';
2247 len++;
2248 p = buffer;
2249 }
2250 else if (ret == AF_INET) {
2251 p = buffer + 1;
2252 len = strlen(p);
2253 p[len] = ':';
2254 len++;
2255 }
2256 else {
2257 lua_pushnil(L);
2258 return 1;
2259 }
2260
2261 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2262 lua_pushnil(L);
2263 return 1;
2264 }
2265
2266 lua_pushstring(L, p);
2267 return 1;
2268}
2269
2270/* Returns information about the peer of the connection. */
2271__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2272{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002273 struct hlua_socket *socket;
2274 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002275 struct xref *peer;
2276 struct appctx *appctx;
2277 struct stream_interface *si;
2278 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002279 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002280
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002281 MAY_LJMP(check_args(L, 1, "getpeername"));
2282
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002283 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002284
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002285 /* Check if we run on the same thread than the xreator thread.
2286 * We cannot access to the socket if the thread is different.
2287 */
2288 if (socket->tid != tid)
2289 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2290
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002291 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002292 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002293 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294 lua_pushnil(L);
2295 return 1;
2296 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002297 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2298 si = appctx->owner;
2299 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002300
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002301 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002302 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002303 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002304 lua_pushnil(L);
2305 return 1;
2306 }
2307
Willy Tarreaua71f6422016-11-16 17:00:14 +01002308 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002309 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002310 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002311 lua_pushnil(L);
2312 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002313 }
2314
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002315 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2316 xref_unlock(&socket->xref, peer);
2317 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002318}
2319
2320/* Returns information about my connection side. */
2321static int hlua_socket_getsockname(struct lua_State *L)
2322{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002323 struct hlua_socket *socket;
2324 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002325 struct appctx *appctx;
2326 struct xref *peer;
2327 struct stream_interface *si;
2328 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002329 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002330
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002331 MAY_LJMP(check_args(L, 1, "getsockname"));
2332
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002333 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002334
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002335 /* Check if we run on the same thread than the xreator thread.
2336 * We cannot access to the socket if the thread is different.
2337 */
2338 if (socket->tid != tid)
2339 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2340
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002341 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002342 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002343 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002344 lua_pushnil(L);
2345 return 1;
2346 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002347 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2348 si = appctx->owner;
2349 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002351 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002352 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002353 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002354 lua_pushnil(L);
2355 return 1;
2356 }
2357
Willy Tarreaua71f6422016-11-16 17:00:14 +01002358 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002359 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002360 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002361 lua_pushnil(L);
2362 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002363 }
2364
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002365 ret = hlua_socket_info(L, &conn->addr.from);
2366 xref_unlock(&socket->xref, peer);
2367 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002368}
2369
2370/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002371static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002372 .obj_type = OBJ_TYPE_APPLET,
2373 .name = "<LUA_TCP>",
2374 .fct = hlua_socket_handler,
2375 .release = hlua_socket_release,
2376};
2377
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002378__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002379{
2380 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2381 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002382 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002383 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002384 struct stream_interface *si;
2385 struct stream *s;
2386
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002387 /* Check if we run on the same thread than the xreator thread.
2388 * We cannot access to the socket if the thread is different.
2389 */
2390 if (socket->tid != tid)
2391 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2392
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002393 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002394 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002395 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002396 lua_pushnil(L);
2397 lua_pushstring(L, "Can't connect");
2398 return 2;
2399 }
2400 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2401 si = appctx->owner;
2402 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002403
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002404 /* Check if we run on the same thread than the xreator thread.
2405 * We cannot access to the socket if the thread is different.
2406 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002407 if (socket->tid != tid) {
2408 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002409 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002410 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002411
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002412 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002413 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002414 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002415 lua_pushnil(L);
2416 lua_pushstring(L, "Can't connect");
2417 return 2;
2418 }
2419
Willy Tarreaue09101e2018-10-16 17:37:12 +02002420 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002421
2422 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002423 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002424 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002425 lua_pushinteger(L, 1);
2426 return 1;
2427 }
2428
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002429 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2430 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002431 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002432 }
2433 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002434 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002435 return 0;
2436}
2437
2438/* This function fail or initite the connection. */
2439__LJMP static int hlua_socket_connect(struct lua_State *L)
2440{
2441 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002442 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002443 const char *ip;
2444 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002445 struct hlua *hlua;
2446 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002447 int low, high;
2448 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002449 struct xref *peer;
2450 struct stream_interface *si;
2451 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002452
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002453 if (lua_gettop(L) < 2)
2454 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002455
2456 /* Get args. */
2457 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002458
2459 /* Check if we run on the same thread than the xreator thread.
2460 * We cannot access to the socket if the thread is different.
2461 */
2462 if (socket->tid != tid)
2463 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2464
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002465 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002466 if (lua_gettop(L) >= 3) {
2467 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002468 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002469
Tim Duesterhus6edab862018-01-06 19:04:45 +01002470 /* Force the ip to end with a colon, to support IPv6 addresses
2471 * that are not enclosed within square brackets.
2472 */
2473 if (port > 0) {
2474 luaL_buffinit(L, &b);
2475 luaL_addstring(&b, ip);
2476 luaL_addchar(&b, ':');
2477 luaL_pushresult(&b);
2478 ip = lua_tolstring(L, lua_gettop(L), NULL);
2479 }
2480 }
2481
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002482 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002483 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002484 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002485 lua_pushnil(L);
2486 return 1;
2487 }
2488 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2489 si = appctx->owner;
2490 s = si_strm(si);
2491
2492 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002493 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002494 if (!conn) {
2495 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002496 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002497 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002498
Willy Tarreau3adac082015-09-26 17:51:09 +02002499 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002500 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002501
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002502 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002503 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002504 if (!addr) {
2505 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002506 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002507 }
2508 if (low != high) {
2509 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002510 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002511 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002512 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002513
2514 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002515 if (low == 0) {
2516 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002517 if (port == -1) {
2518 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002519 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002520 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002521 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2522 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002523 if (port == -1) {
2524 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002525 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002526 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002527 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2528 }
2529 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002530
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002531 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002532 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002533
2534 /* inform the stream that we want to be notified whenever the
2535 * connection completes.
2536 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002537 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002538 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002539 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002540
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002541 hlua->flags |= HLUA_MUST_GC;
2542
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002543 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2544 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002545 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002546 }
2547 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002548
2549 task_wakeup(s->task, TASK_WOKEN_INIT);
2550 /* Return yield waiting for connection. */
2551
Willy Tarreau9635e032018-10-16 17:52:55 +02002552 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002553
2554 return 0;
2555}
2556
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002557#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002558__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2559{
2560 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002561 struct xref *peer;
2562 struct appctx *appctx;
2563 struct stream_interface *si;
2564 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002565
2566 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2567 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002568
2569 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002570 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002571 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002572 lua_pushnil(L);
2573 return 1;
2574 }
2575 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2576 si = appctx->owner;
2577 s = si_strm(si);
2578
2579 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002580 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002581 return MAY_LJMP(hlua_socket_connect(L));
2582}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002583#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002584
2585__LJMP static int hlua_socket_setoption(struct lua_State *L)
2586{
2587 return 0;
2588}
2589
2590__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2591{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002592 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002593 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002594 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002595 struct xref *peer;
2596 struct appctx *appctx;
2597 struct stream_interface *si;
2598 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002599
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002600 MAY_LJMP(check_args(L, 2, "settimeout"));
2601
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002602 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002603
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002604 /* convert the timeout to millis */
2605 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606
Thierry Fournier17a921b2018-03-08 09:59:02 +01002607 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002608 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002609 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2610
Mark Lakes56cc1252018-03-27 09:48:06 +02002611 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002612 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002613
2614 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002615 if (tmout == 0)
2616 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002617
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002618 /* Check if we run on the same thread than the xreator thread.
2619 * We cannot access to the socket if the thread is different.
2620 */
2621 if (socket->tid != tid)
2622 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2623
Mark Lakes56cc1252018-03-27 09:48:06 +02002624 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002625 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002626 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002627 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2628 WILL_LJMP(lua_error(L));
2629 return 0;
2630 }
2631 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2632 si = appctx->owner;
2633 s = si_strm(si);
2634
Cyril Bonté7bb63452018-08-17 23:51:02 +02002635 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002636 s->req.rto = tmout;
2637 s->req.wto = tmout;
2638 s->res.rto = tmout;
2639 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002640 s->req.rex = tick_add_ifset(now_ms, tmout);
2641 s->req.wex = tick_add_ifset(now_ms, tmout);
2642 s->res.rex = tick_add_ifset(now_ms, tmout);
2643 s->res.wex = tick_add_ifset(now_ms, tmout);
2644
2645 s->task->expire = tick_add_ifset(now_ms, tmout);
2646 task_queue(s->task);
2647
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002648 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002649
Thierry Fourniere9636f12018-03-08 09:54:32 +01002650 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002651 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002652}
2653
2654__LJMP static int hlua_socket_new(lua_State *L)
2655{
2656 struct hlua_socket *socket;
2657 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002658 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002659 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002660
2661 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002662 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002663 hlua_pusherror(L, "socket: full stack");
2664 goto out_fail_conf;
2665 }
2666
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002667 /* Create the object: obj[0] = userdata. */
2668 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002669 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002670 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002671 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002672 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002673
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002674 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002675 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002676 hlua_pusherror(L, "socket: uninitialized pools.");
2677 goto out_fail_conf;
2678 }
2679
Willy Tarreau87b09662015-04-03 00:22:06 +02002680 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002681 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2682 lua_setmetatable(L, -2);
2683
Willy Tarreaud420a972015-04-06 00:39:18 +02002684 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002685 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002686 if (!appctx) {
2687 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002688 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002689 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002690
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002691 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002692 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002693 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2694 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002695
Willy Tarreaud420a972015-04-06 00:39:18 +02002696 /* Now create a session, task and stream for this applet */
2697 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2698 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002699 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002700 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002701 }
2702
Willy Tarreau87787ac2017-08-28 16:22:54 +02002703 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002704 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002705 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002706 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002707 }
2708
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002709 /* Initialise cross reference between stream and Lua socket object. */
2710 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002711
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002712 /* Configure "right" stream interface. this "si" is used to connect
2713 * and retrieve data from the server. The connection is initialized
2714 * with the "struct server".
2715 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002716 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002717
2718 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002719 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2720 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002721
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002722 return 1;
2723
Willy Tarreaud420a972015-04-06 00:39:18 +02002724 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002725 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002726 out_fail_sess:
2727 appctx_free(appctx);
2728 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002729 WILL_LJMP(lua_error(L));
2730 return 0;
2731}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002732
2733/*
2734 *
2735 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002736 * Class Channel
2737 *
2738 *
2739 */
2740
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002741/* This function is called before the Lua execution. It stores
2742 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002743 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002744static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002745{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002746 c->mode = stream->be->mode;
2747 switch (c->mode) {
2748 case PR_MODE_HTTP:
2749 c->data.http.dir = opt & SMP_OPT_DIR;
2750 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2751 c->data.http.state = stream->txn->req.msg_state;
2752 else
2753 c->data.http.state = stream->txn->rsp.msg_state;
2754 break;
2755 default:
2756 break;
2757 }
2758}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002759
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002760/* This function is called after the Lua execution. it
2761 * returns true if the parser state is consistent, otherwise,
2762 * it return false.
2763 *
2764 * In HTTP mode, the parser state must be in the same state
2765 * or greater when we exit the function. Even if we do a
2766 * control yield. This prevent to break the HTTP message
2767 * from the Lua code.
2768 */
2769static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2770{
2771 if (c->mode != stream->be->mode)
2772 return 0;
2773
2774 switch (c->mode) {
2775 case PR_MODE_HTTP:
2776 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002777 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002778 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2779 return stream->txn->req.msg_state >= c->data.http.state;
2780 else
2781 return stream->txn->rsp.msg_state >= c->data.http.state;
2782 default:
2783 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002784 }
2785 return 1;
2786}
2787
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002788/* Returns the struct hlua_channel join to the class channel in the
2789 * stack entry "ud" or throws an argument error.
2790 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002791__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002792{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002793 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002794}
2795
Willy Tarreau47860ed2015-03-10 14:07:50 +01002796/* Pushes the channel onto the top of the stack. If the stask does not have a
2797 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002798 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002799static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002800{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002801 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002802 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002803 return 0;
2804
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002805 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002806 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002807 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002808
2809 /* Pop a class sesison metatable and affect it to the userdata. */
2810 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2811 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002812 return 1;
2813}
2814
2815/* Duplicate all the data present in the input channel and put it
2816 * in a string LUA variables. Returns -1 and push a nil value in
2817 * the stack if the channel is closed and all the data are consumed,
2818 * returns 0 if no data are available, otherwise it returns the length
2819 * of the builded string.
2820 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002821static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002822{
2823 char *blk1;
2824 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002825 size_t len1;
2826 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002827 int ret;
2828 luaL_Buffer b;
2829
Willy Tarreau06d80a92017-10-19 14:32:15 +02002830 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002831 if (unlikely(ret == 0))
2832 return 0;
2833
2834 if (unlikely(ret < 0)) {
2835 lua_pushnil(L);
2836 return -1;
2837 }
2838
2839 luaL_buffinit(L, &b);
2840 luaL_addlstring(&b, blk1, len1);
2841 if (unlikely(ret == 2))
2842 luaL_addlstring(&b, blk2, len2);
2843 luaL_pushresult(&b);
2844
2845 if (unlikely(ret == 2))
2846 return len1 + len2;
2847 return len1;
2848}
2849
2850/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2851 * a yield. This function keep the data in the buffer.
2852 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002853__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002854{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002855 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002856
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002857 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2858
Christopher Faulet3f829a42018-12-13 21:56:45 +01002859 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2860 WILL_LJMP(lua_error(L));
2861
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002862 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002863 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002864 return 1;
2865}
2866
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002867/* Check arguments for the function "hlua_channel_dup_yield". */
2868__LJMP static int hlua_channel_dup(lua_State *L)
2869{
2870 MAY_LJMP(check_args(L, 1, "dup"));
2871 MAY_LJMP(hlua_checkchannel(L, 1));
2872 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2873}
2874
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002875/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2876 * a yield. This function consumes the data in the buffer. It returns
2877 * a string containing the data or a nil pointer if no data are available
2878 * and the channel is closed.
2879 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002880__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002881{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002882 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002883 int ret;
2884
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002885 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002886
Christopher Faulet3f829a42018-12-13 21:56:45 +01002887 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2888 WILL_LJMP(lua_error(L));
2889
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002890 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002891 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002892 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002893
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002894 if (unlikely(ret == -1))
2895 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002896
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002897 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002898 return 1;
2899}
2900
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002901/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002902__LJMP static int hlua_channel_get(lua_State *L)
2903{
2904 MAY_LJMP(check_args(L, 1, "get"));
2905 MAY_LJMP(hlua_checkchannel(L, 1));
2906 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2907}
2908
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002909/* This functions consumes and returns one line. If the channel is closed,
2910 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002911 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912 * value.
2913 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002914__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002915{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002916 char *blk1;
2917 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002918 size_t len1;
2919 size_t len2;
2920 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002921 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002922 int ret;
2923 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002924
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002925 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2926
Christopher Faulet3f829a42018-12-13 21:56:45 +01002927 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2928 WILL_LJMP(lua_error(L));
2929
Willy Tarreau06d80a92017-10-19 14:32:15 +02002930 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002931 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002932 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002933
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002934 if (ret == -1) {
2935 lua_pushnil(L);
2936 return 1;
2937 }
2938
2939 luaL_buffinit(L, &b);
2940 luaL_addlstring(&b, blk1, len1);
2941 len = len1;
2942 if (unlikely(ret == 2)) {
2943 luaL_addlstring(&b, blk2, len2);
2944 len += len2;
2945 }
2946 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002947 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002948 return 1;
2949}
2950
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002951/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002952__LJMP static int hlua_channel_getline(lua_State *L)
2953{
2954 MAY_LJMP(check_args(L, 1, "getline"));
2955 MAY_LJMP(hlua_checkchannel(L, 1));
2956 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2957}
2958
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002959/* This function takes a string as input, and append it at the
2960 * input side of channel. If the data is too big, but a space
2961 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002962 * yields. If the data is bigger than the buffer, or if the
2963 * channel is closed, it returns -1. Otherwise, it returns the
2964 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002965 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002966__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002967{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002968 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002969 size_t len;
2970 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2971 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2972 int ret;
2973 int max;
2974
Christopher Faulet3f829a42018-12-13 21:56:45 +01002975 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2976 WILL_LJMP(lua_error(L));
2977
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002978 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002979 * the request buffer if its not required.
2980 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002981 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002982 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002983 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002984 }
2985
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002986 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002987 if (max > len - l)
2988 max = len - l;
2989
Willy Tarreau06d80a92017-10-19 14:32:15 +02002990 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002991 if (ret == -2 || ret == -3) {
2992 lua_pushinteger(L, -1);
2993 return 1;
2994 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002995 if (ret == -1) {
2996 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02002997 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002998 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002999 l += ret;
3000 lua_pop(L, 1);
3001 lua_pushinteger(L, l);
3002
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003003 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003004 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003005 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003006 * in this case, we cannot add more data, so we cannot yield,
3007 * we return the amount of copyied data.
3008 */
3009 return 1;
3010 }
3011 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003012 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003013 return 1;
3014}
3015
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003016/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3017 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003018 * buffer size is too little for the data.
3019 */
3020__LJMP static int hlua_channel_append(lua_State *L)
3021{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003022 size_t len;
3023
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003024 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003025 MAY_LJMP(hlua_checkchannel(L, 1));
3026 MAY_LJMP(luaL_checklstring(L, 2, &len));
3027 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003028 lua_pushinteger(L, 0);
3029
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003030 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031}
3032
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003033/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003034 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003035 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003036 * or -1 if the channel is closed or if the buffer size is too
3037 * little for the data.
3038 */
3039__LJMP static int hlua_channel_set(lua_State *L)
3040{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003041 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003042
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003043 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003044 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003045 lua_pushinteger(L, 0);
3046
Christopher Faulet3f829a42018-12-13 21:56:45 +01003047 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3048 WILL_LJMP(lua_error(L));
3049
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003050 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003051
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003052 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003053}
3054
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003055/* Append data in the output side of the buffer. This data is immediately
3056 * sent. The function returns the amount of data written. If the buffer
3057 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003058 * if the channel is closed.
3059 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003060__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003061{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003062 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003063 size_t len;
3064 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3065 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3066 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003067 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003068
Christopher Faulet3f829a42018-12-13 21:56:45 +01003069 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3070 WILL_LJMP(lua_error(L));
3071
Willy Tarreau47860ed2015-03-10 14:07:50 +01003072 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003073 lua_pushinteger(L, -1);
3074 return 1;
3075 }
3076
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003077 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003078 * the request buffer if its not required.
3079 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003080 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003081 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003082 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003083 }
3084
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003085 /* The written data will be immediately sent, so we can check
3086 * the available space without taking in account the reserve.
3087 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003088 * data, because the buffer will be flushed.
3089 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003090 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003091
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003092 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003093 * in this case, we cannot add more data, so we cannot yield,
3094 * we return the amount of copyied data.
3095 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003096 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003097 return 1;
3098
3099 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003100 if (max > len - l)
3101 max = len - l;
3102
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003103 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003104 * detects a non contiguous buffer and realign it.
3105 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003106 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003107 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003108
3109 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003110 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003111
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003112 /* buffer replace considers that the input part is filled.
3113 * so, I must forward these new data in the output part.
3114 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003115 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003116
3117 l += max;
3118 lua_pop(L, 1);
3119 lua_pushinteger(L, l);
3120
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003121 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003122 * in this case, we cannot add more data, so we cannot yield,
3123 * we return the amount of copyied data.
3124 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003125 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003126 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003127 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003128
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003129 if (l < len) {
3130 /* If we are waiting for space in the response buffer, we
3131 * must set the flag WAKERESWR. This flag required the task
3132 * wake up if any activity is detected on the response buffer.
3133 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003134 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003135 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003136 else
3137 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003138 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003139 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003140
3141 return 1;
3142}
3143
3144/* Just a wraper of "_hlua_channel_send". This wrapper permits
3145 * yield the LUA process, and resume it without checking the
3146 * input arguments.
3147 */
3148__LJMP static int hlua_channel_send(lua_State *L)
3149{
3150 MAY_LJMP(check_args(L, 2, "send"));
3151 lua_pushinteger(L, 0);
3152
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003153 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003154}
3155
3156/* This function forward and amount of butes. The data pass from
3157 * the input side of the buffer to the output side, and can be
3158 * forwarded. This function never fails.
3159 *
3160 * The Lua function takes an amount of bytes to be forwarded in
3161 * imput. It returns the number of bytes forwarded.
3162 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003163__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003164{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003165 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003166 int len;
3167 int l;
3168 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003169 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003170
3171 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003172
3173 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3174 WILL_LJMP(lua_error(L));
3175
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003176 len = MAY_LJMP(luaL_checkinteger(L, 2));
3177 l = MAY_LJMP(luaL_checkinteger(L, -1));
3178
3179 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003180 if (max > ci_data(chn))
3181 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003182 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003183 l += max;
3184
3185 lua_pop(L, 1);
3186 lua_pushinteger(L, l);
3187
3188 /* Check if it miss bytes to forward. */
3189 if (l < len) {
3190 /* The the input channel or the output channel are closed, we
3191 * must return the amount of data forwarded.
3192 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003193 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003194 return 1;
3195
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003196 /* If we are waiting for space data in the response buffer, we
3197 * must set the flag WAKERESWR. This flag required the task
3198 * wake up if any activity is detected on the response buffer.
3199 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003200 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003201 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003202 else
3203 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003204
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003205 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003206 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003207 }
3208
3209 return 1;
3210}
3211
3212/* Just check the input and prepare the stack for the previous
3213 * function "hlua_channel_forward_yield"
3214 */
3215__LJMP static int hlua_channel_forward(lua_State *L)
3216{
3217 MAY_LJMP(check_args(L, 2, "forward"));
3218 MAY_LJMP(hlua_checkchannel(L, 1));
3219 MAY_LJMP(luaL_checkinteger(L, 2));
3220
3221 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003222 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003223}
3224
3225/* Just returns the number of bytes available in the input
3226 * side of the buffer. This function never fails.
3227 */
3228__LJMP static int hlua_channel_get_in_len(lua_State *L)
3229{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003230 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003231
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003232 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003233 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003234 if (IS_HTX_STRM(chn_strm(chn))) {
3235 struct htx *htx = htxbuf(&chn->buf);
3236 lua_pushinteger(L, htx->data - co_data(chn));
3237 }
3238 else
3239 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003240 return 1;
3241}
3242
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003243/* Returns true if the channel is full. */
3244__LJMP static int hlua_channel_is_full(lua_State *L)
3245{
3246 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003247
3248 MAY_LJMP(check_args(L, 1, "is_full"));
3249 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0136ebb2020-02-26 11:59:19 +01003250 /* ignore the reserve, we are not on a producer side (ie in an
3251 * applet).
3252 */
3253 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003254 return 1;
3255}
3256
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003257/* Just returns the number of bytes available in the output
3258 * side of the buffer. This function never fails.
3259 */
3260__LJMP static int hlua_channel_get_out_len(lua_State *L)
3261{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003262 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003263
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003264 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003265 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003266 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003267 return 1;
3268}
3269
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003270/*
3271 *
3272 *
3273 * Class Fetches
3274 *
3275 *
3276 */
3277
3278/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003279 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003280 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003281__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003282{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003283 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003284}
3285
3286/* This function creates and push in the stack a fetch object according
3287 * with a current TXN.
3288 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003289static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003290{
Willy Tarreau7073c472015-04-06 11:15:40 +02003291 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003292
3293 /* Check stack size. */
3294 if (!lua_checkstack(L, 3))
3295 return 0;
3296
3297 /* Create the object: obj[0] = userdata.
3298 * Note that the base of the Fetches object is the
3299 * transaction object.
3300 */
3301 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003302 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003303 lua_rawseti(L, -2, 0);
3304
Willy Tarreau7073c472015-04-06 11:15:40 +02003305 hsmp->s = txn->s;
3306 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003307 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003308 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003309
3310 /* Pop a class sesison metatable and affect it to the userdata. */
3311 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3312 lua_setmetatable(L, -2);
3313
3314 return 1;
3315}
3316
3317/* This function is an LUA binding. It is called with each sample-fetch.
3318 * It uses closure argument to store the associated sample-fetch. It
3319 * returns only one argument or throws an error. An error is thrown
3320 * only if an error is encountered during the argument parsing. If
3321 * the "sample-fetch" function fails, nil is returned.
3322 */
3323__LJMP static int hlua_run_sample_fetch(lua_State *L)
3324{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003325 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003326 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003327 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003328 int i;
3329 struct sample smp;
3330
3331 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003332 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003333
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003334 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003335 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003336
Thierry FOURNIERca988662015-12-20 18:43:03 +01003337 /* Check execution authorization. */
3338 if (f->use & SMP_USE_HTTP_ANY &&
3339 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3340 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3341 "is not available in Lua services", f->kw);
3342 WILL_LJMP(lua_error(L));
3343 }
3344
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003345 /* Get extra arguments. */
3346 for (i = 0; i < lua_gettop(L) - 1; i++) {
3347 if (i >= ARGM_NBARGS)
3348 break;
3349 hlua_lua2arg(L, i + 2, &args[i]);
3350 }
3351 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003352 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003353
3354 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003355 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003356
3357 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003358 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003359 lua_pushfstring(L, "error in arguments");
3360 WILL_LJMP(lua_error(L));
3361 }
3362
3363 /* Initialise the sample. */
3364 memset(&smp, 0, sizeof(smp));
3365
3366 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003367 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003368 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003369 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003370 lua_pushstring(L, "");
3371 else
3372 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003373 return 1;
3374 }
3375
3376 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003377 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003378 hlua_smp2lua_str(L, &smp);
3379 else
3380 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003381 return 1;
3382}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003383
3384/*
3385 *
3386 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003387 * Class Converters
3388 *
3389 *
3390 */
3391
3392/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003393 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003394 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003395__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003396{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003397 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003398}
3399
3400/* This function creates and push in the stack a Converters object
3401 * according with a current TXN.
3402 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003403static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003404{
Willy Tarreau7073c472015-04-06 11:15:40 +02003405 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003406
3407 /* Check stack size. */
3408 if (!lua_checkstack(L, 3))
3409 return 0;
3410
3411 /* Create the object: obj[0] = userdata.
3412 * Note that the base of the Converters object is the
3413 * same than the TXN object.
3414 */
3415 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003416 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003417 lua_rawseti(L, -2, 0);
3418
Willy Tarreau7073c472015-04-06 11:15:40 +02003419 hsmp->s = txn->s;
3420 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003421 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003422 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003423
Willy Tarreau87b09662015-04-03 00:22:06 +02003424 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003425 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3426 lua_setmetatable(L, -2);
3427
3428 return 1;
3429}
3430
3431/* This function is an LUA binding. It is called with each converter.
3432 * It uses closure argument to store the associated converter. It
3433 * returns only one argument or throws an error. An error is thrown
3434 * only if an error is encountered during the argument parsing. If
3435 * the converter function function fails, nil is returned.
3436 */
3437__LJMP static int hlua_run_sample_conv(lua_State *L)
3438{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003439 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003440 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003441 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003442 int i;
3443 struct sample smp;
3444
3445 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003446 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003447
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003448 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003449 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003450
3451 /* Get extra arguments. */
3452 for (i = 0; i < lua_gettop(L) - 2; i++) {
3453 if (i >= ARGM_NBARGS)
3454 break;
3455 hlua_lua2arg(L, i + 3, &args[i]);
3456 }
3457 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003458 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003459
3460 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003461 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003462
3463 /* Run the special args checker. */
3464 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3465 hlua_pusherror(L, "error in arguments");
3466 WILL_LJMP(lua_error(L));
3467 }
3468
3469 /* Initialise the sample. */
3470 if (!hlua_lua2smp(L, 2, &smp)) {
3471 hlua_pusherror(L, "error in the input argument");
3472 WILL_LJMP(lua_error(L));
3473 }
3474
Willy Tarreau1777ea62016-03-10 16:15:46 +01003475 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3476
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003477 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003478 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003479 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003480 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003481 WILL_LJMP(lua_error(L));
3482 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003483 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3484 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003485 hlua_pusherror(L, "error during the input argument casting");
3486 WILL_LJMP(lua_error(L));
3487 }
3488
3489 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003490 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003491 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003492 lua_pushstring(L, "");
3493 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003494 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003495 return 1;
3496 }
3497
3498 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003499 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003500 hlua_smp2lua_str(L, &smp);
3501 else
3502 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003503 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003504}
3505
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003506/*
3507 *
3508 *
3509 * Class AppletTCP
3510 *
3511 *
3512 */
3513
3514/* Returns a struct hlua_txn if the stack entry "ud" is
3515 * a class stream, otherwise it throws an error.
3516 */
3517__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3518{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003519 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003520}
3521
3522/* This function creates and push in the stack an Applet object
3523 * according with a current TXN.
3524 */
3525static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3526{
3527 struct hlua_appctx *appctx;
3528 struct stream_interface *si = ctx->owner;
3529 struct stream *s = si_strm(si);
3530 struct proxy *p = s->be;
3531
3532 /* Check stack size. */
3533 if (!lua_checkstack(L, 3))
3534 return 0;
3535
3536 /* Create the object: obj[0] = userdata.
3537 * Note that the base of the Converters object is the
3538 * same than the TXN object.
3539 */
3540 lua_newtable(L);
3541 appctx = lua_newuserdata(L, sizeof(*appctx));
3542 lua_rawseti(L, -2, 0);
3543 appctx->appctx = ctx;
3544 appctx->htxn.s = s;
3545 appctx->htxn.p = p;
3546
3547 /* Create the "f" field that contains a list of fetches. */
3548 lua_pushstring(L, "f");
3549 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3550 return 0;
3551 lua_settable(L, -3);
3552
3553 /* Create the "sf" field that contains a list of stringsafe fetches. */
3554 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003555 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003556 return 0;
3557 lua_settable(L, -3);
3558
3559 /* Create the "c" field that contains a list of converters. */
3560 lua_pushstring(L, "c");
3561 if (!hlua_converters_new(L, &appctx->htxn, 0))
3562 return 0;
3563 lua_settable(L, -3);
3564
3565 /* Create the "sc" field that contains a list of stringsafe converters. */
3566 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003567 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003568 return 0;
3569 lua_settable(L, -3);
3570
3571 /* Pop a class stream metatable and affect it to the table. */
3572 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3573 lua_setmetatable(L, -2);
3574
3575 return 1;
3576}
3577
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003578__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3579{
3580 struct hlua_appctx *appctx;
3581 struct stream *s;
3582 const char *name;
3583 size_t len;
3584 struct sample smp;
3585
3586 MAY_LJMP(check_args(L, 3, "set_var"));
3587
3588 /* It is useles to retrieve the stream, but this function
3589 * runs only in a stream context.
3590 */
3591 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3592 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3593 s = appctx->htxn.s;
3594
3595 /* Converts the third argument in a sample. */
3596 hlua_lua2smp(L, 3, &smp);
3597
3598 /* Store the sample in a variable. */
3599 smp_set_owner(&smp, s->be, s->sess, s, 0);
3600 vars_set_by_name(name, len, &smp);
3601 return 0;
3602}
3603
3604__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3605{
3606 struct hlua_appctx *appctx;
3607 struct stream *s;
3608 const char *name;
3609 size_t len;
3610 struct sample smp;
3611
3612 MAY_LJMP(check_args(L, 2, "unset_var"));
3613
3614 /* It is useles to retrieve the stream, but this function
3615 * runs only in a stream context.
3616 */
3617 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3618 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3619 s = appctx->htxn.s;
3620
3621 /* Unset the variable. */
3622 smp_set_owner(&smp, s->be, s->sess, s, 0);
3623 vars_unset_by_name(name, len, &smp);
3624 return 0;
3625}
3626
3627__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3628{
3629 struct hlua_appctx *appctx;
3630 struct stream *s;
3631 const char *name;
3632 size_t len;
3633 struct sample smp;
3634
3635 MAY_LJMP(check_args(L, 2, "get_var"));
3636
3637 /* It is useles to retrieve the stream, but this function
3638 * runs only in a stream context.
3639 */
3640 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3641 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3642 s = appctx->htxn.s;
3643
3644 smp_set_owner(&smp, s->be, s->sess, s, 0);
3645 if (!vars_get_by_name(name, len, &smp)) {
3646 lua_pushnil(L);
3647 return 1;
3648 }
3649
3650 return hlua_smp2lua(L, &smp);
3651}
3652
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003653__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3654{
3655 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3656 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003657 struct hlua *hlua;
3658
3659 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003660 if (!s->hlua)
3661 return 0;
3662 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003663
3664 MAY_LJMP(check_args(L, 2, "set_priv"));
3665
3666 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003667 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003668
3669 /* Get and store new value. */
3670 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3671 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3672
3673 return 0;
3674}
3675
3676__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3677{
3678 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3679 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003680 struct hlua *hlua;
3681
3682 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003683 if (!s->hlua) {
3684 lua_pushnil(L);
3685 return 1;
3686 }
3687 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003688
3689 /* Push configuration index in the stack. */
3690 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3691
3692 return 1;
3693}
3694
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003695/* If expected data not yet available, it returns a yield. This function
3696 * consumes the data in the buffer. It returns a string containing the
3697 * data. This string can be empty.
3698 */
3699__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3700{
3701 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3702 struct stream_interface *si = appctx->appctx->owner;
3703 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003704 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003705 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003706 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003707 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003708
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003709 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003710 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003711
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003712 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003713 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003714 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003715 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003716 }
3717
3718 /* End of data: commit the total strings and return. */
3719 if (ret < 0) {
3720 luaL_pushresult(&appctx->b);
3721 return 1;
3722 }
3723
3724 /* Ensure that the block 2 length is usable. */
3725 if (ret == 1)
3726 len2 = 0;
3727
3728 /* dont check the max length read and dont check. */
3729 luaL_addlstring(&appctx->b, blk1, len1);
3730 luaL_addlstring(&appctx->b, blk2, len2);
3731
3732 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003733 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003734 luaL_pushresult(&appctx->b);
3735 return 1;
3736}
3737
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003738/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003739__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3740{
3741 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3742
3743 /* Initialise the string catenation. */
3744 luaL_buffinit(L, &appctx->b);
3745
3746 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3747}
3748
3749/* If expected data not yet available, it returns a yield. This function
3750 * consumes the data in the buffer. It returns a string containing the
3751 * data. This string can be empty.
3752 */
3753__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3754{
3755 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3756 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003757 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003758 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003759 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003760 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003761 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003762 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003763
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003764 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003765 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003766
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003767 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003768 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003769 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003770 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003771 }
3772
3773 /* End of data: commit the total strings and return. */
3774 if (ret < 0) {
3775 luaL_pushresult(&appctx->b);
3776 return 1;
3777 }
3778
3779 /* Ensure that the block 2 length is usable. */
3780 if (ret == 1)
3781 len2 = 0;
3782
3783 if (len == -1) {
3784
3785 /* If len == -1, catenate all the data avalaile and
3786 * yield because we want to get all the data until
3787 * the end of data stream.
3788 */
3789 luaL_addlstring(&appctx->b, blk1, len1);
3790 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003791 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003792 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003793 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003794
3795 } else {
3796
3797 /* Copy the fisrt block caping to the length required. */
3798 if (len1 > len)
3799 len1 = len;
3800 luaL_addlstring(&appctx->b, blk1, len1);
3801 len -= len1;
3802
3803 /* Copy the second block. */
3804 if (len2 > len)
3805 len2 = len;
3806 luaL_addlstring(&appctx->b, blk2, len2);
3807 len -= len2;
3808
3809 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003810 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003811
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003812 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003813 if (len > 0) {
3814 lua_pushinteger(L, len);
3815 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003816 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003817 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003818 }
3819
3820 /* return the result. */
3821 luaL_pushresult(&appctx->b);
3822 return 1;
3823 }
3824
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003825 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003826 hlua_pusherror(L, "Lua: internal error");
3827 WILL_LJMP(lua_error(L));
3828 return 0;
3829}
3830
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003831/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003832__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3833{
3834 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3835 int len = -1;
3836
3837 if (lua_gettop(L) > 2)
3838 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3839 if (lua_gettop(L) >= 2) {
3840 len = MAY_LJMP(luaL_checkinteger(L, 2));
3841 lua_pop(L, 1);
3842 }
3843
3844 /* Confirm or set the required length */
3845 lua_pushinteger(L, len);
3846
3847 /* Initialise the string catenation. */
3848 luaL_buffinit(L, &appctx->b);
3849
3850 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3851}
3852
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003853/* Append data in the output side of the buffer. This data is immediately
3854 * sent. The function returns the amount of data written. If the buffer
3855 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003856 * if the channel is closed.
3857 */
3858__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3859{
3860 size_t len;
3861 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3862 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3863 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3864 struct stream_interface *si = appctx->appctx->owner;
3865 struct channel *chn = si_ic(si);
3866 int max;
3867
3868 /* Get the max amount of data which can write as input in the channel. */
3869 max = channel_recv_max(chn);
3870 if (max > (len - l))
3871 max = len - l;
3872
3873 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003874 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003875
3876 /* update counters. */
3877 l += max;
3878 lua_pop(L, 1);
3879 lua_pushinteger(L, l);
3880
3881 /* If some data is not send, declares the situation to the
3882 * applet, and returns a yield.
3883 */
3884 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003885 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003886 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003887 }
3888
3889 return 1;
3890}
3891
3892/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3893 * yield the LUA process, and resume it without checking the
3894 * input arguments.
3895 */
3896__LJMP static int hlua_applet_tcp_send(lua_State *L)
3897{
3898 MAY_LJMP(check_args(L, 2, "send"));
3899 lua_pushinteger(L, 0);
3900
3901 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3902}
3903
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003904/*
3905 *
3906 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003907 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003908 *
3909 *
3910 */
3911
3912/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003913 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003914 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003915__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003916{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003917 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003918}
3919
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003920/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003921 * according with a current TXN.
3922 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003923static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003924{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003925 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003926 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003927 struct stream_interface *si = ctx->owner;
3928 struct stream *s = si_strm(si);
3929 struct proxy *px = s->be;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003930
3931 /* Check stack size. */
3932 if (!lua_checkstack(L, 3))
3933 return 0;
3934
3935 /* Create the object: obj[0] = userdata.
3936 * Note that the base of the Converters object is the
3937 * same than the TXN object.
3938 */
3939 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003940 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003941 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003942 appctx->appctx = ctx;
3943 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003944 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003945 appctx->htxn.s = s;
3946 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003947
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003948 /* Create the "f" field that contains a list of fetches. */
3949 lua_pushstring(L, "f");
3950 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3951 return 0;
3952 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003953
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003954 /* Create the "sf" field that contains a list of stringsafe fetches. */
3955 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003956 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003957 return 0;
3958 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003959
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003960 /* Create the "c" field that contains a list of converters. */
3961 lua_pushstring(L, "c");
3962 if (!hlua_converters_new(L, &appctx->htxn, 0))
3963 return 0;
3964 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003965
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003966 /* Create the "sc" field that contains a list of stringsafe converters. */
3967 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003968 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003969 return 0;
3970 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003971
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003972 if (IS_HTX_STRM(s)) {
3973 /* HTX version */
3974 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet29f17582019-05-23 11:03:26 +02003975 struct htx_blk *blk;
3976 struct htx_sl *sl;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003977 struct ist path;
3978 unsigned long long len = 0;
3979 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003980
Christopher Faulet29f17582019-05-23 11:03:26 +02003981 blk = htx_get_first_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +01003982 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Faulet29f17582019-05-23 11:03:26 +02003983 sl = htx_get_blk_ptr(htx, blk);
3984
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003985 /* Stores the request method. */
3986 lua_pushstring(L, "method");
3987 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3988 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003989
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003990 /* Stores the http version. */
3991 lua_pushstring(L, "version");
3992 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3993 lua_settable(L, -3);
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003994
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003995 /* creates an array of headers. hlua_http_get_headers() crates and push
3996 * the array on the top of the stack.
3997 */
3998 lua_pushstring(L, "headers");
3999 htxn.s = s;
4000 htxn.p = px;
4001 htxn.dir = SMP_OPT_DIR_REQ;
4002 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
4003 return 0;
4004 lua_settable(L, -3);
4005
4006 path = http_get_path(htx_sl_req_uri(sl));
4007 if (path.ptr) {
4008 char *p, *q, *end;
4009
4010 p = path.ptr;
4011 end = path.ptr + path.len;
4012 q = p;
4013 while (q < end && *q != '?')
4014 q++;
4015
4016 /* Stores the request path. */
4017 lua_pushstring(L, "path");
4018 lua_pushlstring(L, p, q - p);
4019 lua_settable(L, -3);
4020
4021 /* Stores the query string. */
4022 lua_pushstring(L, "qs");
4023 if (*q == '?')
4024 q++;
4025 lua_pushlstring(L, q, end - q);
4026 lua_settable(L, -3);
4027 }
4028
Christopher Fauleta3f15502019-05-13 15:27:23 +02004029 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004030 struct htx_blk *blk = htx_get_blk(htx, pos);
4031 enum htx_blk_type type = htx_get_blk_type(blk);
4032
Christopher Faulet54b5e212019-06-04 10:08:28 +02004033 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004034 break;
4035 if (type == HTX_BLK_DATA)
4036 len += htx_get_blksz(blk);
4037 }
4038 if (htx->extra != ULLONG_MAX)
4039 len += htx->extra;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004040
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004041 /* Stores the request path. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004042 lua_pushstring(L, "length");
4043 lua_pushinteger(L, len);
4044 lua_settable(L, -3);
4045 }
4046 else {
4047 /* Legacy HTTP version */
4048 struct http_txn *txn = s->txn;
4049 const char *path;
4050 const char *end;
4051 const char *p;
4052
4053 /* Stores the request method. */
4054 lua_pushstring(L, "method");
4055 lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004056 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004057
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004058 /* Stores the http version. */
4059 lua_pushstring(L, "version");
4060 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 +01004061 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004062
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004063 /* creates an array of headers. hlua_http_get_headers() crates and push
4064 * the array on the top of the stack.
4065 */
4066 lua_pushstring(L, "headers");
4067 htxn.s = s;
4068 htxn.p = px;
4069 htxn.dir = SMP_OPT_DIR_REQ;
4070 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
4071 return 0;
4072 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004073
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004074 /* Get path and qs */
4075 path = http_txn_get_path(txn);
4076 if (path) {
4077 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4078 p = path;
4079 while (p < end && *p != '?')
4080 p++;
4081
4082 /* Stores the request path. */
4083 lua_pushstring(L, "path");
4084 lua_pushlstring(L, path, p - path);
4085 lua_settable(L, -3);
4086
4087 /* Stores the query string. */
4088 lua_pushstring(L, "qs");
4089 if (*p == '?')
4090 p++;
4091 lua_pushlstring(L, p, end - p);
4092 lua_settable(L, -3);
4093 }
4094
4095 /* Stores the request path. */
4096 lua_pushstring(L, "length");
4097 lua_pushinteger(L, txn->req.body_len);
4098 lua_settable(L, -3);
4099 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004100
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004101 /* Create an empty array of HTTP request headers. */
4102 lua_pushstring(L, "response");
4103 lua_newtable(L);
4104 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004105
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004106 /* Pop a class stream metatable and affect it to the table. */
4107 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4108 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004109
4110 return 1;
4111}
4112
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004113__LJMP static int hlua_applet_http_set_var(lua_State *L)
4114{
4115 struct hlua_appctx *appctx;
4116 struct stream *s;
4117 const char *name;
4118 size_t len;
4119 struct sample smp;
4120
4121 MAY_LJMP(check_args(L, 3, "set_var"));
4122
4123 /* It is useles to retrieve the stream, but this function
4124 * runs only in a stream context.
4125 */
4126 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4127 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4128 s = appctx->htxn.s;
4129
4130 /* Converts the third argument in a sample. */
4131 hlua_lua2smp(L, 3, &smp);
4132
4133 /* Store the sample in a variable. */
4134 smp_set_owner(&smp, s->be, s->sess, s, 0);
4135 vars_set_by_name(name, len, &smp);
4136 return 0;
4137}
4138
4139__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4140{
4141 struct hlua_appctx *appctx;
4142 struct stream *s;
4143 const char *name;
4144 size_t len;
4145 struct sample smp;
4146
4147 MAY_LJMP(check_args(L, 2, "unset_var"));
4148
4149 /* It is useles to retrieve the stream, but this function
4150 * runs only in a stream context.
4151 */
4152 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4153 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4154 s = appctx->htxn.s;
4155
4156 /* Unset the variable. */
4157 smp_set_owner(&smp, s->be, s->sess, s, 0);
4158 vars_unset_by_name(name, len, &smp);
4159 return 0;
4160}
4161
4162__LJMP static int hlua_applet_http_get_var(lua_State *L)
4163{
4164 struct hlua_appctx *appctx;
4165 struct stream *s;
4166 const char *name;
4167 size_t len;
4168 struct sample smp;
4169
4170 MAY_LJMP(check_args(L, 2, "get_var"));
4171
4172 /* It is useles to retrieve the stream, but this function
4173 * runs only in a stream context.
4174 */
4175 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4176 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4177 s = appctx->htxn.s;
4178
4179 smp_set_owner(&smp, s->be, s->sess, s, 0);
4180 if (!vars_get_by_name(name, len, &smp)) {
4181 lua_pushnil(L);
4182 return 1;
4183 }
4184
4185 return hlua_smp2lua(L, &smp);
4186}
4187
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004188__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4189{
4190 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4191 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004192 struct hlua *hlua;
4193
4194 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004195 if (!s->hlua)
4196 return 0;
4197 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004198
4199 MAY_LJMP(check_args(L, 2, "set_priv"));
4200
4201 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004202 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004203
4204 /* Get and store new value. */
4205 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4206 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4207
4208 return 0;
4209}
4210
4211__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4212{
4213 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4214 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004215 struct hlua *hlua;
4216
4217 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004218 if (!s->hlua) {
4219 lua_pushnil(L);
4220 return 1;
4221 }
4222 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004223
4224 /* Push configuration index in the stack. */
4225 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4226
4227 return 1;
4228}
4229
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004230/* If expected data not yet available, it returns a yield. This function
4231 * consumes the data in the buffer. It returns a string containing the
4232 * data. This string can be empty.
4233 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004234__LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx)
4235{
4236 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4237 struct stream_interface *si = appctx->appctx->owner;
4238 struct channel *req = si_oc(si);
4239 struct htx *htx;
4240 struct htx_blk *blk;
4241 size_t count;
4242 int stop = 0;
4243
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004244 htx = htx_from_buf(&req->buf);
4245 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004246 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004247
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004248 while (count && !stop && blk) {
4249 enum htx_blk_type type = htx_get_blk_type(blk);
4250 uint32_t sz = htx_get_blksz(blk);
4251 struct ist v;
4252 uint32_t vlen;
4253 char *nl;
4254
Christopher Faulete461e342018-12-18 16:43:35 +01004255 if (type == HTX_BLK_EOM) {
4256 stop = 1;
4257 break;
4258 }
4259
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004260 vlen = sz;
4261 if (vlen > count) {
4262 if (type != HTX_BLK_DATA)
4263 break;
4264 vlen = count;
4265 }
4266
4267 switch (type) {
4268 case HTX_BLK_UNUSED:
4269 break;
4270
4271 case HTX_BLK_DATA:
4272 v = htx_get_blk_value(htx, blk);
4273 v.len = vlen;
4274 nl = istchr(v, '\n');
4275 if (nl != NULL) {
4276 stop = 1;
4277 vlen = nl - v.ptr + 1;
4278 }
4279 luaL_addlstring(&appctx->b, v.ptr, vlen);
4280 break;
4281
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004282 case HTX_BLK_TLR:
4283 case HTX_BLK_EOM:
4284 stop = 1;
4285 break;
4286
4287 default:
4288 break;
4289 }
4290
4291 co_set_data(req, co_data(req) - vlen);
4292 count -= vlen;
4293 if (sz == vlen)
4294 blk = htx_remove_blk(htx, blk);
4295 else {
4296 htx_cut_data_blk(htx, blk, vlen);
4297 break;
4298 }
4299 }
4300
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004301 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004302 if (!stop) {
4303 si_cant_get(si);
4304 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0));
4305 }
4306
4307 /* return the result. */
4308 luaL_pushresult(&appctx->b);
4309 return 1;
4310}
4311
4312
4313/* If expected data not yet available, it returns a yield. This function
4314 * consumes the data in the buffer. It returns a string containing the
4315 * data. This string can be empty.
4316 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004317__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004318{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004319 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4320 struct stream_interface *si = appctx->appctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004321 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004322 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004323 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004324 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004325 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004326
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004327 /* Check for the end of the data. */
4328 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4329 luaL_pushresult(&appctx->b);
4330 return 1;
4331 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004332
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004333 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004334 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004335
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004336 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004337 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004338 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004339 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004340 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004341
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004342 /* End of data: commit the total strings and return. */
4343 if (ret < 0) {
4344 luaL_pushresult(&appctx->b);
4345 return 1;
4346 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004347
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004348 /* Ensure that the block 2 length is usable. */
4349 if (ret == 1)
4350 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004351
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004352 /* Copy the fisrt block caping to the length required. */
4353 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4354 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4355 luaL_addlstring(&appctx->b, blk1, len1);
4356 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004357
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004358 /* Copy the second block. */
4359 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4360 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4361 luaL_addlstring(&appctx->b, blk2, len2);
4362 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004363
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004364 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004365 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004366 luaL_pushresult(&appctx->b);
4367 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004368}
4369
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004370/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004371__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004372{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004373 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004374
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004375 /* Initialise the string catenation. */
4376 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004377
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004378 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
4379 return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0));
4380 else
4381 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004382}
4383
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004384/* If expected data not yet available, it returns a yield. This function
4385 * consumes the data in the buffer. It returns a string containing the
4386 * data. This string can be empty.
4387 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004388__LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx)
4389{
4390 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4391 struct stream_interface *si = appctx->appctx->owner;
4392 struct channel *req = si_oc(si);
4393 struct htx *htx;
4394 struct htx_blk *blk;
4395 size_t count;
4396 int len;
4397
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004398 htx = htx_from_buf(&req->buf);
4399 len = MAY_LJMP(luaL_checkinteger(L, 2));
4400 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004401 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004402 while (count && len && blk) {
4403 enum htx_blk_type type = htx_get_blk_type(blk);
4404 uint32_t sz = htx_get_blksz(blk);
4405 struct ist v;
4406 uint32_t vlen;
4407
Christopher Faulete461e342018-12-18 16:43:35 +01004408 if (type == HTX_BLK_EOM) {
4409 len = 0;
4410 break;
4411 }
4412
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004413 vlen = sz;
4414 if (len > 0 && vlen > len)
4415 vlen = len;
4416 if (vlen > count) {
4417 if (type != HTX_BLK_DATA)
4418 break;
4419 vlen = count;
4420 }
4421
4422 switch (type) {
4423 case HTX_BLK_UNUSED:
4424 break;
4425
4426 case HTX_BLK_DATA:
4427 v = htx_get_blk_value(htx, blk);
4428 luaL_addlstring(&appctx->b, v.ptr, vlen);
4429 break;
4430
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004431 case HTX_BLK_TLR:
4432 case HTX_BLK_EOM:
4433 len = 0;
4434 break;
4435
4436 default:
4437 break;
4438 }
4439
4440 co_set_data(req, co_data(req) - vlen);
4441 count -= vlen;
4442 if (len > 0)
4443 len -= vlen;
4444 if (sz == vlen)
4445 blk = htx_remove_blk(htx, blk);
4446 else {
4447 htx_cut_data_blk(htx, blk, vlen);
4448 break;
4449 }
4450 }
4451
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004452 htx_to_buf(htx, &req->buf);
4453
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004454 /* If we are no other data available, yield waiting for new data. */
4455 if (len) {
4456 if (len > 0) {
4457 lua_pushinteger(L, len);
4458 lua_replace(L, 2);
4459 }
4460 si_cant_get(si);
4461 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0));
4462 }
4463
4464 /* return the result. */
4465 luaL_pushresult(&appctx->b);
4466 return 1;
4467}
4468
4469/* If expected data not yet available, it returns a yield. This function
4470 * consumes the data in the buffer. It returns a string containing the
4471 * data. This string can be empty.
4472 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004473__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004474{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004475 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4476 struct stream_interface *si = appctx->appctx->owner;
4477 int len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004478 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004479 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004480 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004481 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004482 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004483
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004484 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004485 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004486
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004487 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004488 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004489 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004490 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004491 }
4492
4493 /* End of data: commit the total strings and return. */
4494 if (ret < 0) {
4495 luaL_pushresult(&appctx->b);
4496 return 1;
4497 }
4498
4499 /* Ensure that the block 2 length is usable. */
4500 if (ret == 1)
4501 len2 = 0;
4502
4503 /* Copy the fisrt block caping to the length required. */
4504 if (len1 > len)
4505 len1 = len;
4506 luaL_addlstring(&appctx->b, blk1, len1);
4507 len -= len1;
4508
4509 /* Copy the second block. */
4510 if (len2 > len)
4511 len2 = len;
4512 luaL_addlstring(&appctx->b, blk2, len2);
4513 len -= len2;
4514
4515 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004516 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004517 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4518 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4519
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004520 /* If we are no other data available, yield waiting for new data. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004521 if (len > 0) {
4522 lua_pushinteger(L, len);
4523 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004524 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004525 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004526 }
4527
4528 /* return the result. */
4529 luaL_pushresult(&appctx->b);
4530 return 1;
4531}
4532
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004533/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004534__LJMP static int hlua_applet_http_recv(lua_State *L)
4535{
4536 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4537 int len = -1;
4538
4539 /* Check arguments. */
4540 if (lua_gettop(L) > 2)
4541 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4542 if (lua_gettop(L) >= 2) {
4543 len = MAY_LJMP(luaL_checkinteger(L, 2));
4544 lua_pop(L, 1);
4545 }
4546
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004547 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4548 /* HTX version */
4549 lua_pushinteger(L, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004550
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004551 /* Initialise the string catenation. */
4552 luaL_buffinit(L, &appctx->b);
4553
4554 return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0));
4555 }
4556 else {
4557 /* Legacy HTTP version */
4558 /* Check the required length */
4559 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4560 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4561 lua_pushinteger(L, len);
4562
4563 /* Initialise the string catenation. */
4564 luaL_buffinit(L, &appctx->b);
4565
4566 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4567 }
4568}
4569
4570/* Append data in the output side of the buffer. This data is immediately
4571 * sent. The function returns the amount of data written. If the buffer
4572 * cannot contain the data, the function yields. The function returns -1
4573 * if the channel is closed.
4574 */
4575__LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx)
4576{
4577 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4578 struct stream_interface *si = appctx->appctx->owner;
4579 struct channel *res = si_ic(si);
4580 struct htx *htx = htx_from_buf(&res->buf);
4581 const char *data;
4582 size_t len;
4583 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4584 int max;
4585
Christopher Faulet4e963012019-07-03 11:39:30 +02004586 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004587 if (!max)
4588 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004589
4590 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4591
4592 /* Get the max amount of data which can write as input in the channel. */
4593 if (max > (len - l))
4594 max = len - l;
4595
4596 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004597 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004598 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004599
4600 /* update counters. */
4601 l += max;
4602 lua_pop(L, 1);
4603 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004604
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004605 /* If some data is not send, declares the situation to the
4606 * applet, and returns a yield.
4607 */
4608 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004609 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004610 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004611 si_rx_room_blk(si);
4612 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0));
4613 }
4614
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004615 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004616 return 1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004617}
4618
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004619/* Append data in the output side of the buffer. This data is immediately
4620 * sent. The function returns the amount of data written. If the buffer
4621 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004622 * if the channel is closed.
4623 */
4624__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4625{
4626 size_t len;
4627 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4628 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4629 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4630 struct stream_interface *si = appctx->appctx->owner;
4631 struct channel *chn = si_ic(si);
4632 int max;
4633
4634 /* Get the max amount of data which can write as input in the channel. */
4635 max = channel_recv_max(chn);
4636 if (max > (len - l))
4637 max = len - l;
4638
4639 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004640 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004641
4642 /* update counters. */
4643 l += max;
4644 lua_pop(L, 1);
4645 lua_pushinteger(L, l);
4646
4647 /* If some data is not send, declares the situation to the
4648 * applet, and returns a yield.
4649 */
4650 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004651 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004652 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004653 }
4654
4655 return 1;
4656}
4657
4658/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4659 * yield the LUA process, and resume it without checking the
4660 * input arguments.
4661 */
4662__LJMP static int hlua_applet_http_send(lua_State *L)
4663{
4664 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004665
4666 /* We want to send some data. Headers must be sent. */
4667 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4668 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4669 WILL_LJMP(lua_error(L));
4670 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004671
4672 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4673 /* HTX version */
4674 /* This interger is used for followinf the amount of data sent. */
4675 lua_pushinteger(L, 0);
4676
4677 return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0));
4678 }
4679 else {
4680 /* Legacy HTTP version */
4681 size_t len;
4682 char hex[10];
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004683
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004684 MAY_LJMP(luaL_checklstring(L, 2, &len));
4685
4686 /* If transfer encoding chunked is selected, we surround the data
4687 * by chunk data.
4688 */
4689 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4690 snprintf(hex, 9, "%x", (unsigned int)len);
4691 lua_pushfstring(L, "%s\r\n", hex);
4692 lua_insert(L, 2); /* swap the last 2 entries. */
4693 lua_pushstring(L, "\r\n");
4694 lua_concat(L, 3);
4695 }
4696
4697 /* This interger is used for followinf the amount of data sent. */
4698 lua_pushinteger(L, 0);
4699
4700 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4701 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004702}
4703
4704__LJMP static int hlua_applet_http_addheader(lua_State *L)
4705{
4706 const char *name;
4707 int ret;
4708
4709 MAY_LJMP(hlua_checkapplet_http(L, 1));
4710 name = MAY_LJMP(luaL_checkstring(L, 2));
4711 MAY_LJMP(luaL_checkstring(L, 3));
4712
4713 /* Push in the stack the "response" entry. */
4714 ret = lua_getfield(L, 1, "response");
4715 if (ret != LUA_TTABLE) {
4716 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4717 "is expected as an array. %s found", lua_typename(L, ret));
4718 WILL_LJMP(lua_error(L));
4719 }
4720
4721 /* check if the header is already registered if it is not
4722 * the case, register it.
4723 */
4724 ret = lua_getfield(L, -1, name);
4725 if (ret == LUA_TNIL) {
4726
4727 /* Entry not found. */
4728 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4729
4730 /* Insert the new header name in the array in the top of the stack.
4731 * It left the new array in the top of the stack.
4732 */
4733 lua_newtable(L);
4734 lua_pushvalue(L, 2);
4735 lua_pushvalue(L, -2);
4736 lua_settable(L, -4);
4737
4738 } else if (ret != LUA_TTABLE) {
4739
4740 /* corruption error. */
4741 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4742 "is expected as an array. %s found", name, lua_typename(L, ret));
4743 WILL_LJMP(lua_error(L));
4744 }
4745
4746 /* Now the top od thestack is an array of values. We push
4747 * the header value as new entry.
4748 */
4749 lua_pushvalue(L, 3);
4750 ret = lua_rawlen(L, -2);
4751 lua_rawseti(L, -2, ret + 1);
4752 lua_pushboolean(L, 1);
4753 return 1;
4754}
4755
4756__LJMP static int hlua_applet_http_status(lua_State *L)
4757{
4758 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4759 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004760 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004761
4762 if (status < 100 || status > 599) {
4763 lua_pushboolean(L, 0);
4764 return 1;
4765 }
4766
4767 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004768 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004769 lua_pushboolean(L, 1);
4770 return 1;
4771}
4772
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004773
4774__LJMP static int hlua_applet_htx_send_response(lua_State *L)
4775{
4776 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4777 struct stream_interface *si = appctx->appctx->owner;
4778 struct channel *res = si_ic(si);
4779 struct htx *htx;
4780 struct htx_sl *sl;
4781 struct h1m h1m;
4782 const char *status, *reason;
4783 const char *name, *value;
4784 size_t nlen, vlen;
4785 unsigned int flags;
4786
4787 /* Send the message at once. */
4788 htx = htx_from_buf(&res->buf);
4789 h1m_init_res(&h1m);
4790
4791 /* Use the same http version than the request. */
4792 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4793 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4794 if (reason == NULL)
4795 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4796 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4797 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4798 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4799 }
4800 else {
4801 flags = HTX_SL_F_IS_RESP;
4802 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4803 }
4804 if (!sl) {
4805 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4806 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4807 WILL_LJMP(lua_error(L));
4808 }
4809 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4810
4811 /* Get the array associated to the field "response" in the object AppletHTTP. */
4812 lua_pushvalue(L, 0);
4813 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4814 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4815 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4816 WILL_LJMP(lua_error(L));
4817 }
4818
4819 /* Browse the list of headers. */
4820 lua_pushnil(L);
4821 while(lua_next(L, -2) != 0) {
4822 /* We expect a string as -2. */
4823 if (lua_type(L, -2) != LUA_TSTRING) {
4824 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4825 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4826 lua_typename(L, lua_type(L, -2)));
4827 WILL_LJMP(lua_error(L));
4828 }
4829 name = lua_tolstring(L, -2, &nlen);
4830
4831 /* We expect an array as -1. */
4832 if (lua_type(L, -1) != LUA_TTABLE) {
4833 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4834 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4835 name,
4836 lua_typename(L, lua_type(L, -1)));
4837 WILL_LJMP(lua_error(L));
4838 }
4839
4840 /* Browse the table who is on the top of the stack. */
4841 lua_pushnil(L);
4842 while(lua_next(L, -2) != 0) {
4843 int id;
4844
4845 /* We expect a number as -2. */
4846 if (lua_type(L, -2) != LUA_TNUMBER) {
4847 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4848 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4849 name,
4850 lua_typename(L, lua_type(L, -2)));
4851 WILL_LJMP(lua_error(L));
4852 }
4853 id = lua_tointeger(L, -2);
4854
4855 /* We expect a string as -2. */
4856 if (lua_type(L, -1) != LUA_TSTRING) {
4857 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4858 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4859 name, id,
4860 lua_typename(L, lua_type(L, -1)));
4861 WILL_LJMP(lua_error(L));
4862 }
4863 value = lua_tolstring(L, -1, &vlen);
4864
4865 /* Simple Protocol checks. */
4866 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
4867 h1_parse_xfer_enc_header(&h1m, ist2(name, nlen));
4868 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4869 struct ist v = ist2(value, vlen);
4870 int ret;
4871
4872 ret = h1_parse_cont_len_header(&h1m, &v);
4873 if (ret < 0) {
4874 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4875 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4876 name);
4877 WILL_LJMP(lua_error(L));
4878 }
4879 else if (ret == 0)
4880 goto next; /* Skip it */
4881 }
4882
4883 /* Add a new header */
4884 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4885 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4886 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4887 name);
4888 WILL_LJMP(lua_error(L));
4889 }
4890 next:
4891 /* Remove the array from the stack, and get next element with a remaining string. */
4892 lua_pop(L, 1);
4893 }
4894
4895 /* Remove the array from the stack, and get next element with a remaining string. */
4896 lua_pop(L, 1);
4897 }
4898
4899 if (h1m.flags & H1_MF_CHNK)
4900 h1m.flags &= ~H1_MF_CLEN;
4901 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4902 h1m.flags |= H1_MF_XFER_LEN;
4903
4904 /* Uset HTX start-line flags */
4905 if (h1m.flags & H1_MF_XFER_ENC)
4906 flags |= HTX_SL_F_XFER_ENC;
4907 if (h1m.flags & H1_MF_XFER_LEN) {
4908 flags |= HTX_SL_F_XFER_LEN;
4909 if (h1m.flags & H1_MF_CHNK)
4910 flags |= HTX_SL_F_CHNK;
4911 else if (h1m.flags & H1_MF_CLEN)
4912 flags |= HTX_SL_F_CLEN;
4913 if (h1m.body_len == 0)
4914 flags |= HTX_SL_F_BODYLESS;
4915 }
4916 sl->flags |= flags;
4917
4918 /* If we dont have a content-length set, and the HTTP version is 1.1
4919 * and the status code implies the presence of a message body, we must
4920 * announce a transfer encoding chunked. This is required by haproxy
4921 * for the keepalive compliance. If the applet annouces a transfer-encoding
4922 * chunked itslef, don't do anything.
4923 */
4924 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4925 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4926 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4927 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4928 /* Add a new header */
4929 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4930 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4931 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4932 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4933 WILL_LJMP(lua_error(L));
4934 }
4935 }
4936
4937 /* Finalize headers. */
4938 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4939 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4940 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4941 WILL_LJMP(lua_error(L));
4942 }
4943
4944 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4945 b_reset(&res->buf);
4946 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4947 WILL_LJMP(lua_error(L));
4948 }
4949
4950 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004951 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004952
4953 /* Headers sent, set the flag. */
4954 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4955 return 0;
4956
4957}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004958/* We will build the status line and the headers of the HTTP response.
4959 * We will try send at once if its not possible, we give back the hand
4960 * waiting for more room.
4961 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004962__LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4963{
4964 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4965 struct stream_interface *si = appctx->appctx->owner;
4966 struct channel *res = si_ic(si);
4967
4968 if (co_data(res)) {
4969 si_rx_room_blk(si);
4970 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0));
4971 }
4972 return MAY_LJMP(hlua_applet_htx_send_response(L));
4973}
4974
4975
4976__LJMP static int hlua_applet_htx_start_response(lua_State *L)
4977{
4978 return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0));
4979}
4980
4981/* We will build the status line and the headers of the HTTP response.
4982 * We will try send at once if its not possible, we give back the hand
4983 * waiting for more room.
4984 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004985__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4986{
4987 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4988 struct stream_interface *si = appctx->appctx->owner;
4989 struct channel *chn = si_ic(si);
4990 int ret;
4991 size_t len;
4992 const char *msg;
4993
4994 /* Get the message as the first argument on the stack. */
4995 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4996
4997 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004998 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004999
5000 /* if ret == -2 or -3 the channel closed or the message si too
5001 * big for the buffers.
5002 */
5003 if (ret == -2 || ret == -3) {
5004 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
5005 WILL_LJMP(lua_error(L));
5006 }
5007
5008 /* If ret is -1, we dont have room in the buffer, so we yield. */
5009 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01005010 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02005011 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005012 }
5013
5014 /* Headers sent, set the flag. */
5015 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
5016 return 0;
5017}
5018
5019__LJMP static int hlua_applet_http_start_response(lua_State *L)
5020{
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005021 struct buffer *tmp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005022 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005023 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02005024 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005025 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02005026 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005027 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005028 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005029 int hdr_chunked = 0;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005030 const char *reason;
5031
5032 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
5033 return MAY_LJMP(hlua_applet_htx_start_response(L));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005034
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005035 reason = appctx->appctx->ctx.hlua_apphttp.reason;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005036 if (reason == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02005037 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005038
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005039 tmp = get_trash_chunk();
5040
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005041 /* Use the same http version than the request. */
5042 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01005043 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005044 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005045 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005046
5047 /* Get the array associated to the field "response" in the object AppletHTTP. */
5048 lua_pushvalue(L, 0);
5049 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
5050 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
5051 appctx->appctx->rule->arg.hlua_rule->fcn.name);
5052 WILL_LJMP(lua_error(L));
5053 }
5054
5055 /* Browse the list of headers. */
5056 lua_pushnil(L);
5057 while(lua_next(L, -2) != 0) {
5058
5059 /* We expect a string as -2. */
5060 if (lua_type(L, -2) != LUA_TSTRING) {
5061 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
5062 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5063 lua_typename(L, lua_type(L, -2)));
5064 WILL_LJMP(lua_error(L));
5065 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005066 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005067
5068 /* We expect an array as -1. */
5069 if (lua_type(L, -1) != LUA_TTABLE) {
5070 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
5071 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5072 name,
5073 lua_typename(L, lua_type(L, -1)));
5074 WILL_LJMP(lua_error(L));
5075 }
5076
5077 /* Browse the table who is on the top of the stack. */
5078 lua_pushnil(L);
5079 while(lua_next(L, -2) != 0) {
5080
5081 /* We expect a number as -2. */
5082 if (lua_type(L, -2) != LUA_TNUMBER) {
5083 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
5084 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5085 name,
5086 lua_typename(L, lua_type(L, -2)));
5087 WILL_LJMP(lua_error(L));
5088 }
5089 id = lua_tointeger(L, -2);
5090
5091 /* We expect a string as -2. */
5092 if (lua_type(L, -1) != LUA_TSTRING) {
5093 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
5094 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5095 name, id,
5096 lua_typename(L, lua_type(L, -1)));
5097 WILL_LJMP(lua_error(L));
5098 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005099 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005100
5101 /* Catenate a new header. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005102 if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) {
5103 memcpy(tmp->area + tmp->data, name, name_len);
5104 tmp->data += name_len;
5105 tmp->area[tmp->data++] = ':';
5106 tmp->area[tmp->data++] = ' ';
Willy Tarreaua3294632017-08-23 11:24:47 +02005107
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005108 memcpy(tmp->area + tmp->data, value,
5109 value_len);
5110 tmp->data += value_len;
5111 tmp->area[tmp->data++] = '\r';
5112 tmp->area[tmp->data++] = '\n';
Willy Tarreaua3294632017-08-23 11:24:47 +02005113 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005114
5115 /* Protocol checks. */
5116
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005117 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005118 * is done without control. If it contains a bad value,
5119 * the content-length remains negative so that we can
5120 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005121 */
Willy Tarreaua3294632017-08-23 11:24:47 +02005122 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005123 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005124
5125 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02005126 if (name_len == 17 && value_len == 7 &&
5127 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005128 strcasecmp("chunked", value) == 0)
5129 hdr_chunked = 1;
5130
5131 /* Remove the array from the stack, and get next element with a remaining string. */
5132 lua_pop(L, 1);
5133 }
5134
5135 /* Remove the array from the stack, and get next element with a remaining string. */
5136 lua_pop(L, 1);
5137 }
5138
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005139 /* If we dont have a content-length set, and the HTTP version is 1.1
5140 * and the status code implies the presence of a message body, we must
5141 * announce a transfer encoding chunked. This is required by haproxy
5142 * for the keepalive compliance. If the applet annouces a transfer-encoding
5143 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005144 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005145 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005146 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
5147 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
5148 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
5149 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005150 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
5151 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
5152 }
5153
5154 /* Finalize headers. */
5155 chunk_appendf(tmp, "\r\n");
5156
5157 /* Remove the last entry and the array of headers */
5158 lua_pop(L, 2);
5159
5160 /* Push the headers block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005161 lua_pushlstring(L, tmp->area, tmp->data);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005162
5163 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
5164}
5165
5166/*
5167 *
5168 *
5169 * Class HTTP
5170 *
5171 *
5172 */
5173
5174/* Returns a struct hlua_txn if the stack entry "ud" is
5175 * a class stream, otherwise it throws an error.
5176 */
5177__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
5178{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005179 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005180}
5181
5182/* This function creates and push in the stack a HTTP object
5183 * according with a current TXN.
5184 */
5185static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
5186{
5187 struct hlua_txn *htxn;
5188
5189 /* Check stack size. */
5190 if (!lua_checkstack(L, 3))
5191 return 0;
5192
5193 /* Create the object: obj[0] = userdata.
5194 * Note that the base of the Converters object is the
5195 * same than the TXN object.
5196 */
5197 lua_newtable(L);
5198 htxn = lua_newuserdata(L, sizeof(*htxn));
5199 lua_rawseti(L, -2, 0);
5200
5201 htxn->s = txn->s;
5202 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02005203 htxn->dir = txn->dir;
5204 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005205
5206 /* Pop a class stream metatable and affect it to the table. */
5207 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5208 lua_setmetatable(L, -2);
5209
5210 return 1;
5211}
5212
5213/* This function creates ans returns an array of HTTP headers.
5214 * This function does not fails. It is used as wrapper with the
5215 * 2 following functions.
5216 */
5217__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5218{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005219 /* Create the table. */
5220 lua_newtable(L);
5221
5222 if (!htxn->s->txn)
5223 return 1;
5224
Christopher Faulet724a12c2018-12-13 22:12:15 +01005225 if (IS_HTX_STRM(htxn->s)) {
5226 /* HTX version */
5227 struct htx *htx = htxbuf(&msg->chn->buf);
5228 int32_t pos;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005229
Christopher Fauleta3f15502019-05-13 15:27:23 +02005230 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet724a12c2018-12-13 22:12:15 +01005231 struct htx_blk *blk = htx_get_blk(htx, pos);
5232 enum htx_blk_type type = htx_get_blk_type(blk);
5233 struct ist n, v;
5234 int len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005235
Christopher Faulet724a12c2018-12-13 22:12:15 +01005236 if (type == HTX_BLK_HDR) {
5237 n = htx_get_blk_name(htx,blk);
5238 v = htx_get_blk_value(htx, blk);
5239 }
5240 else if (type == HTX_BLK_EOH)
5241 break;
5242 else
5243 continue;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005244
Christopher Faulet724a12c2018-12-13 22:12:15 +01005245 /* Check for existing entry:
5246 * assume that the table is on the top of the stack, and
5247 * push the key in the stack, the function lua_gettable()
5248 * perform the lookup.
5249 */
5250 lua_pushlstring(L, n.ptr, n.len);
5251 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005252
Christopher Faulet724a12c2018-12-13 22:12:15 +01005253 switch (lua_type(L, -1)) {
5254 case LUA_TNIL:
5255 /* Table not found, create it. */
5256 lua_pop(L, 1); /* remove the nil value. */
5257 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5258 lua_newtable(L); /* create and push empty table. */
5259 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5260 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5261 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5262 break;
5263
5264 case LUA_TTABLE:
5265 /* Entry found: push the value in the table. */
5266 len = lua_rawlen(L, -1);
5267 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5268 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5269 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5270 break;
5271
5272 default:
5273 /* Other cases are errors. */
5274 hlua_pusherror(L, "internal error during the parsing of headers.");
5275 WILL_LJMP(lua_error(L));
5276 }
5277 }
5278 }
5279 else {
5280 /* Legacy HTTP version */
5281 const char *cur_ptr, *cur_next, *p;
5282 int old_idx, cur_idx;
5283 struct hdr_idx_elem *cur_hdr;
5284 const char *hn, *hv;
5285 int hnl, hvl;
5286 const char *in;
5287 char *out;
5288 int len;
5289
5290 /* Build array of headers. */
5291 old_idx = 0;
5292 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
5293
5294 while (1) {
5295 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
5296 if (!cur_idx)
5297 break;
5298 old_idx = cur_idx;
5299
5300 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
5301 cur_ptr = cur_next;
5302 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
5303
5304 /* Now we have one full header at cur_ptr of len cur_hdr->len,
5305 * and the next header starts at cur_next. We'll check
5306 * this header in the list as well as against the default
5307 * rule.
5308 */
5309
5310 /* look for ': *'. */
5311 hn = cur_ptr;
5312 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
5313 if (p >= cur_ptr+cur_hdr->len)
5314 continue;
5315 hnl = p - hn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005316 p++;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005317 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
5318 p++;
5319 if (p >= cur_ptr+cur_hdr->len)
5320 continue;
5321 hv = p;
5322 hvl = cur_ptr+cur_hdr->len-p;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005323
Christopher Faulet724a12c2018-12-13 22:12:15 +01005324 /* Lowercase the key. Don't check the size of trash, it have
5325 * the size of one buffer and the input data contains in one
5326 * buffer.
5327 */
5328 out = trash.area;
5329 for (in=hn; in<hn+hnl; in++, out++)
5330 *out = tolower(*in);
5331 *out = '\0';
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005332
Christopher Faulet724a12c2018-12-13 22:12:15 +01005333 /* Check for existing entry:
5334 * assume that the table is on the top of the stack, and
5335 * push the key in the stack, the function lua_gettable()
5336 * perform the lookup.
5337 */
5338 lua_pushlstring(L, trash.area, hnl);
5339 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005340
Christopher Faulet724a12c2018-12-13 22:12:15 +01005341 switch (lua_type(L, -1)) {
5342 case LUA_TNIL:
5343 /* Table not found, create it. */
5344 lua_pop(L, 1); /* remove the nil value. */
5345 lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */
5346 lua_newtable(L); /* create and push empty table. */
5347 lua_pushlstring(L, hv, hvl); /* push header value. */
5348 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5349 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5350 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005351
Christopher Faulet724a12c2018-12-13 22:12:15 +01005352 case LUA_TTABLE:
5353 /* Entry found: push the value in the table. */
5354 len = lua_rawlen(L, -1);
5355 lua_pushlstring(L, hv, hvl); /* push header value. */
5356 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5357 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5358 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005359
Christopher Faulet724a12c2018-12-13 22:12:15 +01005360 default:
5361 /* Other cases are errors. */
5362 hlua_pusherror(L, "internal error during the parsing of headers.");
5363 WILL_LJMP(lua_error(L));
5364 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005365 }
5366 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005367 return 1;
5368}
5369
5370__LJMP static int hlua_http_req_get_headers(lua_State *L)
5371{
5372 struct hlua_txn *htxn;
5373
5374 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5375 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5376
Christopher Faulet2351ca22019-07-26 16:31:34 +02005377 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005378 WILL_LJMP(lua_error(L));
5379
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005380 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
5381}
5382
5383__LJMP static int hlua_http_res_get_headers(lua_State *L)
5384{
5385 struct hlua_txn *htxn;
5386
5387 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5388 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5389
Christopher Faulet2351ca22019-07-26 16:31:34 +02005390 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005391 WILL_LJMP(lua_error(L));
5392
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005393 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
5394}
5395
5396/* This function replace full header, or just a value in
5397 * the request or in the response. It is a wrapper fir the
5398 * 4 following functions.
5399 */
5400__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
5401 struct http_msg *msg, int action)
5402{
5403 size_t name_len;
5404 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5405 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5406 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Dragan Dosen26743032019-04-30 15:54:36 +02005407 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005408
Dragan Dosen26743032019-04-30 15:54:36 +02005409 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005410 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5411
Christopher Faulet5a8549e2019-06-17 13:36:06 +02005412 if (IS_HTX_STRM(htxn->s)) {
5413 struct htx *htx = htxbuf(&msg->chn->buf);
5414
5415 htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action);
5416 }
5417 else
5418 http_transform_header_str(htxn->s, msg, name, name_len, value, re, action);
Dragan Dosen26743032019-04-30 15:54:36 +02005419 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005420 return 0;
5421}
5422
5423__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5424{
5425 struct hlua_txn *htxn;
5426
5427 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5428 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5429
Christopher Faulet2351ca22019-07-26 16:31:34 +02005430 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005431 WILL_LJMP(lua_error(L));
5432
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005433 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
5434}
5435
5436__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5437{
5438 struct hlua_txn *htxn;
5439
5440 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5441 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5442
Christopher Faulet2351ca22019-07-26 16:31:34 +02005443 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005444 WILL_LJMP(lua_error(L));
5445
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005446 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
5447}
5448
5449__LJMP static int hlua_http_req_rep_val(lua_State *L)
5450{
5451 struct hlua_txn *htxn;
5452
5453 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5454 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5455
Christopher Faulet2351ca22019-07-26 16:31:34 +02005456 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005457 WILL_LJMP(lua_error(L));
5458
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005459 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
5460}
5461
5462__LJMP static int hlua_http_res_rep_val(lua_State *L)
5463{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005464 struct hlua_txn *htxn;
5465
5466 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5467 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5468
Christopher Faulet2351ca22019-07-26 16:31:34 +02005469 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005470 WILL_LJMP(lua_error(L));
5471
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02005472 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005473}
5474
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005475/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005476 * It is a wrapper for the 2 following functions.
5477 */
5478__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5479{
5480 size_t len;
5481 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005482
Christopher Faulet724a12c2018-12-13 22:12:15 +01005483 if (IS_HTX_STRM(htxn->s)) {
5484 /* HTX version */
5485 struct htx *htx = htxbuf(&msg->chn->buf);
5486 struct http_hdr_ctx ctx;
5487
5488 ctx.blk = NULL;
5489 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5490 http_remove_header(htx, &ctx);
5491 }
5492 else {
5493 /* Legacy HTTP version */
5494 struct hdr_ctx ctx;
5495 struct http_txn *txn = htxn->s->txn;
5496
5497 ctx.idx = 0;
5498 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
5499 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5500 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005501 return 0;
5502}
5503
5504__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5505{
5506 struct hlua_txn *htxn;
5507
5508 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5509 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5510
Christopher Faulet2351ca22019-07-26 16:31:34 +02005511 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005512 WILL_LJMP(lua_error(L));
5513
Willy Tarreaueee5b512015-04-03 23:46:31 +02005514 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005515}
5516
5517__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5518{
5519 struct hlua_txn *htxn;
5520
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005521 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005522 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5523
Christopher Faulet2351ca22019-07-26 16:31:34 +02005524 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005525 WILL_LJMP(lua_error(L));
5526
Willy Tarreaueee5b512015-04-03 23:46:31 +02005527 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005528}
5529
5530/* This function adds an header. It is a wrapper used by
5531 * the 2 following functions.
5532 */
5533__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5534{
5535 size_t name_len;
5536 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5537 size_t value_len;
5538 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
5539 char *p;
5540
Christopher Faulet724a12c2018-12-13 22:12:15 +01005541 if (IS_HTX_STRM(htxn->s)) {
5542 /* HTX version */
5543 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005544
Christopher Faulet724a12c2018-12-13 22:12:15 +01005545 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5546 ist2(value, value_len)));
5547 }
5548 else {
5549 /* Legacy HTTP version */
5550 /* Check length. */
5551 trash.data = value_len + name_len + 2;
5552 if (trash.data > trash.size)
5553 return 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005554
Christopher Faulet724a12c2018-12-13 22:12:15 +01005555 /* Creates the header string. */
5556 p = trash.area;
5557 memcpy(p, name, name_len);
5558 p += name_len;
5559 *p = ':';
5560 p++;
5561 *p = ' ';
5562 p++;
5563 memcpy(p, value, value_len);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005564
Christopher Faulet724a12c2018-12-13 22:12:15 +01005565 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
5566 trash.area, trash.data) != 0);
5567 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005568 return 0;
5569}
5570
5571__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5572{
5573 struct hlua_txn *htxn;
5574
5575 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5576 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5577
Christopher Faulet2351ca22019-07-26 16:31:34 +02005578 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005579 WILL_LJMP(lua_error(L));
5580
Willy Tarreaueee5b512015-04-03 23:46:31 +02005581 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005582}
5583
5584__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5585{
5586 struct hlua_txn *htxn;
5587
5588 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5589 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5590
Christopher Faulet2351ca22019-07-26 16:31:34 +02005591 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005592 WILL_LJMP(lua_error(L));
5593
Willy Tarreaueee5b512015-04-03 23:46:31 +02005594 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005595}
5596
5597static int hlua_http_req_set_hdr(lua_State *L)
5598{
5599 struct hlua_txn *htxn;
5600
5601 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5602 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5603
Christopher Faulet2351ca22019-07-26 16:31:34 +02005604 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005605 WILL_LJMP(lua_error(L));
5606
Willy Tarreaueee5b512015-04-03 23:46:31 +02005607 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
5608 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005609}
5610
5611static int hlua_http_res_set_hdr(lua_State *L)
5612{
5613 struct hlua_txn *htxn;
5614
5615 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5616 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5617
Christopher Faulet2351ca22019-07-26 16:31:34 +02005618 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005619 WILL_LJMP(lua_error(L));
5620
Willy Tarreaueee5b512015-04-03 23:46:31 +02005621 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
5622 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005623}
5624
5625/* This function set the method. */
5626static int hlua_http_req_set_meth(lua_State *L)
5627{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005628 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005629 size_t name_len;
5630 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005631
Christopher Faulet2351ca22019-07-26 16:31:34 +02005632 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005633 WILL_LJMP(lua_error(L));
5634
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005635 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005636 return 1;
5637}
5638
5639/* This function set the method. */
5640static int hlua_http_req_set_path(lua_State *L)
5641{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005642 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005643 size_t name_len;
5644 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005645
Christopher Faulet2351ca22019-07-26 16:31:34 +02005646 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005647 WILL_LJMP(lua_error(L));
5648
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005649 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005650 return 1;
5651}
5652
5653/* This function set the query-string. */
5654static int hlua_http_req_set_query(lua_State *L)
5655{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005656 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005657 size_t name_len;
5658 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005659
Christopher Faulet2351ca22019-07-26 16:31:34 +02005660 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005661 WILL_LJMP(lua_error(L));
5662
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005663 /* Check length. */
5664 if (name_len > trash.size - 1) {
5665 lua_pushboolean(L, 0);
5666 return 1;
5667 }
5668
5669 /* Add the mark question as prefix. */
5670 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005671 trash.area[trash.data++] = '?';
5672 memcpy(trash.area + trash.data, name, name_len);
5673 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005674
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005675 lua_pushboolean(L,
5676 http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005677 return 1;
5678}
5679
5680/* This function set the uri. */
5681static int hlua_http_req_set_uri(lua_State *L)
5682{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005683 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005684 size_t name_len;
5685 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005686
Christopher Faulet2351ca22019-07-26 16:31:34 +02005687 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005688 WILL_LJMP(lua_error(L));
5689
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005690 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005691 return 1;
5692}
5693
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005694/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005695static int hlua_http_res_set_status(lua_State *L)
5696{
5697 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5698 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005699 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005700
Christopher Faulet2351ca22019-07-26 16:31:34 +02005701 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005702 WILL_LJMP(lua_error(L));
5703
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005704 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005705 return 0;
5706}
5707
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005708/*
5709 *
5710 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005711 * Class TXN
5712 *
5713 *
5714 */
5715
5716/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005717 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005718 */
5719__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5720{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005721 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005722}
5723
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005724__LJMP static int hlua_set_var(lua_State *L)
5725{
5726 struct hlua_txn *htxn;
5727 const char *name;
5728 size_t len;
5729 struct sample smp;
5730
5731 MAY_LJMP(check_args(L, 3, "set_var"));
5732
5733 /* It is useles to retrieve the stream, but this function
5734 * runs only in a stream context.
5735 */
5736 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5737 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5738
5739 /* Converts the third argument in a sample. */
5740 hlua_lua2smp(L, 3, &smp);
5741
5742 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005743 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005744 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005745 return 0;
5746}
5747
Christopher Faulet85d79c92016-11-09 16:54:56 +01005748__LJMP static int hlua_unset_var(lua_State *L)
5749{
5750 struct hlua_txn *htxn;
5751 const char *name;
5752 size_t len;
5753 struct sample smp;
5754
5755 MAY_LJMP(check_args(L, 2, "unset_var"));
5756
5757 /* It is useles to retrieve the stream, but this function
5758 * runs only in a stream context.
5759 */
5760 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5761 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5762
5763 /* Unset the variable. */
5764 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5765 vars_unset_by_name(name, len, &smp);
5766 return 0;
5767}
5768
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005769__LJMP static int hlua_get_var(lua_State *L)
5770{
5771 struct hlua_txn *htxn;
5772 const char *name;
5773 size_t len;
5774 struct sample smp;
5775
5776 MAY_LJMP(check_args(L, 2, "get_var"));
5777
5778 /* It is useles to retrieve the stream, but this function
5779 * runs only in a stream context.
5780 */
5781 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5782 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5783
Willy Tarreau7560dd42016-03-10 16:28:58 +01005784 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005785 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005786 lua_pushnil(L);
5787 return 1;
5788 }
5789
5790 return hlua_smp2lua(L, &smp);
5791}
5792
Willy Tarreau59551662015-03-10 14:23:13 +01005793__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005794{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005795 struct hlua *hlua;
5796
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005797 MAY_LJMP(check_args(L, 2, "set_priv"));
5798
Willy Tarreau87b09662015-04-03 00:22:06 +02005799 /* It is useles to retrieve the stream, but this function
5800 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005801 */
5802 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005803 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005804
5805 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005806 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005807
5808 /* Get and store new value. */
5809 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5810 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5811
5812 return 0;
5813}
5814
Willy Tarreau59551662015-03-10 14:23:13 +01005815__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005816{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005817 struct hlua *hlua;
5818
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005819 MAY_LJMP(check_args(L, 1, "get_priv"));
5820
Willy Tarreau87b09662015-04-03 00:22:06 +02005821 /* It is useles to retrieve the stream, but this function
5822 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005823 */
5824 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005825 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005826
5827 /* Push configuration index in the stack. */
5828 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5829
5830 return 1;
5831}
5832
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005833/* Create stack entry containing a class TXN. This function
5834 * return 0 if the stack does not contains free slots,
5835 * otherwise it returns 1.
5836 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005837static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005838{
Willy Tarreaude491382015-04-06 11:04:28 +02005839 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005840
5841 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005842 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005843 return 0;
5844
5845 /* NOTE: The allocation never fails. The failure
5846 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005847 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005848 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005849 /* Create the object: obj[0] = userdata. */
5850 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005851 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005852 lua_rawseti(L, -2, 0);
5853
Willy Tarreaude491382015-04-06 11:04:28 +02005854 htxn->s = s;
5855 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005856 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005857 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005858
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005859 /* Create the "f" field that contains a list of fetches. */
5860 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005861 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005862 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005863 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005864
5865 /* Create the "sf" field that contains a list of stringsafe fetches. */
5866 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005867 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005868 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005869 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005870
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005871 /* Create the "c" field that contains a list of converters. */
5872 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005873 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005874 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005875 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005876
5877 /* Create the "sc" field that contains a list of stringsafe converters. */
5878 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005879 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005880 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005881 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005882
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005883 /* Create the "req" field that contains the request channel object. */
5884 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005885 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005886 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005887 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005888
5889 /* Create the "res" field that contains the response channel object. */
5890 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005891 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005892 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005893 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005894
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005895 /* Creates the HTTP object is the current proxy allows http. */
5896 lua_pushstring(L, "http");
5897 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005898 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005899 return 0;
5900 }
5901 else
5902 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005903 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005904
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005905 /* Pop a class sesison metatable and affect it to the userdata. */
5906 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5907 lua_setmetatable(L, -2);
5908
5909 return 1;
5910}
5911
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005912__LJMP static int hlua_txn_deflog(lua_State *L)
5913{
5914 const char *msg;
5915 struct hlua_txn *htxn;
5916
5917 MAY_LJMP(check_args(L, 2, "deflog"));
5918 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5919 msg = MAY_LJMP(luaL_checkstring(L, 2));
5920
5921 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5922 return 0;
5923}
5924
5925__LJMP static int hlua_txn_log(lua_State *L)
5926{
5927 int level;
5928 const char *msg;
5929 struct hlua_txn *htxn;
5930
5931 MAY_LJMP(check_args(L, 3, "log"));
5932 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5933 level = MAY_LJMP(luaL_checkinteger(L, 2));
5934 msg = MAY_LJMP(luaL_checkstring(L, 3));
5935
5936 if (level < 0 || level >= NB_LOG_LEVELS)
5937 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5938
5939 hlua_sendlog(htxn->s->be, level, msg);
5940 return 0;
5941}
5942
5943__LJMP static int hlua_txn_log_debug(lua_State *L)
5944{
5945 const char *msg;
5946 struct hlua_txn *htxn;
5947
5948 MAY_LJMP(check_args(L, 2, "Debug"));
5949 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5950 msg = MAY_LJMP(luaL_checkstring(L, 2));
5951 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5952 return 0;
5953}
5954
5955__LJMP static int hlua_txn_log_info(lua_State *L)
5956{
5957 const char *msg;
5958 struct hlua_txn *htxn;
5959
5960 MAY_LJMP(check_args(L, 2, "Info"));
5961 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5962 msg = MAY_LJMP(luaL_checkstring(L, 2));
5963 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5964 return 0;
5965}
5966
5967__LJMP static int hlua_txn_log_warning(lua_State *L)
5968{
5969 const char *msg;
5970 struct hlua_txn *htxn;
5971
5972 MAY_LJMP(check_args(L, 2, "Warning"));
5973 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5974 msg = MAY_LJMP(luaL_checkstring(L, 2));
5975 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5976 return 0;
5977}
5978
5979__LJMP static int hlua_txn_log_alert(lua_State *L)
5980{
5981 const char *msg;
5982 struct hlua_txn *htxn;
5983
5984 MAY_LJMP(check_args(L, 2, "Alert"));
5985 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5986 msg = MAY_LJMP(luaL_checkstring(L, 2));
5987 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5988 return 0;
5989}
5990
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005991__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5992{
5993 struct hlua_txn *htxn;
5994 int ll;
5995
5996 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5997 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5998 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5999
6000 if (ll < 0 || ll > 7)
6001 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
6002
6003 htxn->s->logs.level = ll;
6004 return 0;
6005}
6006
6007__LJMP static int hlua_txn_set_tos(lua_State *L)
6008{
6009 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006010 int tos;
6011
6012 MAY_LJMP(check_args(L, 2, "set_tos"));
6013 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6014 tos = MAY_LJMP(luaL_checkinteger(L, 2));
6015
Willy Tarreau1a18b542018-12-11 16:37:42 +01006016 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006017 return 0;
6018}
6019
6020__LJMP static int hlua_txn_set_mark(lua_State *L)
6021{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006022 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006023 int mark;
6024
6025 MAY_LJMP(check_args(L, 2, "set_mark"));
6026 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6027 mark = MAY_LJMP(luaL_checkinteger(L, 2));
6028
Lukas Tribusb59291a2019-08-11 18:03:45 +02006029 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006030 return 0;
6031}
6032
Patrick Hemmer268a7072018-05-11 12:52:31 -04006033__LJMP static int hlua_txn_set_priority_class(lua_State *L)
6034{
6035 struct hlua_txn *htxn;
6036
6037 MAY_LJMP(check_args(L, 2, "set_priority_class"));
6038 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6039 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
6040 return 0;
6041}
6042
6043__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
6044{
6045 struct hlua_txn *htxn;
6046
6047 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
6048 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6049 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
6050 return 0;
6051}
6052
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006053/* This function is an Lua binding that send pending data
6054 * to the client, and close the stream interface.
6055 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006056__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006057{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006058 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006059 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01006060 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006061
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006062 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006063 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006064 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006065
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006066 /* If the flags NOTERM is set, we cannot terminate the http
6067 * session, so we just end the execution of the current
6068 * lua code.
6069 */
6070 if (htxn->flags & HLUA_TXN_NOTERM) {
6071 WILL_LJMP(hlua_done(L));
6072 return 0;
6073 }
6074
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006075 ic = &htxn->s->req;
6076 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01006077
Christopher Faulet72c69272019-07-26 16:40:24 +02006078 if (IS_HTX_STRM(htxn->s)) {
6079 htxn->s->txn->status = 0;
6080 http_reply_and_close(htxn->s, 0, NULL);
6081 ic->analysers &= AN_REQ_FLT_END;
6082 oc->analysers &= AN_RES_FLT_END;
6083 }
Christopher Fauletb58fb792019-07-16 10:52:40 +02006084 else {
6085 if (htxn->s->txn) {
6086 /* HTTP mode, let's stay in sync with the stream */
6087 b_del(&ic->buf, htxn->s->txn->req.sov);
6088 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
6089 htxn->s->txn->req.sov = 0;
6090
6091 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
6092 oc->analysers = AN_RES_HTTP_XFER_BODY;
6093 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
6094 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreau630ef452015-08-28 10:06:15 +02006095
Willy Tarreau630ef452015-08-28 10:06:15 +02006096 /* Note that if we want to support keep-alive, we need
6097 * to bypass the close/shutr_now calls below, but that
6098 * may only be done if the HTTP request was already
6099 * processed and the connection header is known (ie
6100 * not during TCP rules).
6101 */
Christopher Fauletb58fb792019-07-16 10:52:40 +02006102 }
Willy Tarreau630ef452015-08-28 10:06:15 +02006103
Christopher Fauletb58fb792019-07-16 10:52:40 +02006104 channel_auto_read(ic);
6105 channel_abort(ic);
6106 channel_auto_close(ic);
6107 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02006108
Christopher Fauletb58fb792019-07-16 10:52:40 +02006109 oc->wex = tick_add_ifset(now_ms, oc->wto);
6110 channel_auto_read(oc);
6111 channel_auto_close(oc);
6112 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006113
Christopher Fauletb58fb792019-07-16 10:52:40 +02006114 ic->analysers = 0;
6115 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006116
Christopher Faulet72c69272019-07-26 16:40:24 +02006117 if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is
6118 htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
6119
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006120 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006121 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006122 return 0;
6123}
6124
6125__LJMP static int hlua_log(lua_State *L)
6126{
6127 int level;
6128 const char *msg;
6129
6130 MAY_LJMP(check_args(L, 2, "log"));
6131 level = MAY_LJMP(luaL_checkinteger(L, 1));
6132 msg = MAY_LJMP(luaL_checkstring(L, 2));
6133
6134 if (level < 0 || level >= NB_LOG_LEVELS)
6135 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6136
6137 hlua_sendlog(NULL, level, msg);
6138 return 0;
6139}
6140
6141__LJMP static int hlua_log_debug(lua_State *L)
6142{
6143 const char *msg;
6144
6145 MAY_LJMP(check_args(L, 1, "debug"));
6146 msg = MAY_LJMP(luaL_checkstring(L, 1));
6147 hlua_sendlog(NULL, LOG_DEBUG, msg);
6148 return 0;
6149}
6150
6151__LJMP static int hlua_log_info(lua_State *L)
6152{
6153 const char *msg;
6154
6155 MAY_LJMP(check_args(L, 1, "info"));
6156 msg = MAY_LJMP(luaL_checkstring(L, 1));
6157 hlua_sendlog(NULL, LOG_INFO, msg);
6158 return 0;
6159}
6160
6161__LJMP static int hlua_log_warning(lua_State *L)
6162{
6163 const char *msg;
6164
6165 MAY_LJMP(check_args(L, 1, "warning"));
6166 msg = MAY_LJMP(luaL_checkstring(L, 1));
6167 hlua_sendlog(NULL, LOG_WARNING, msg);
6168 return 0;
6169}
6170
6171__LJMP static int hlua_log_alert(lua_State *L)
6172{
6173 const char *msg;
6174
6175 MAY_LJMP(check_args(L, 1, "alert"));
6176 msg = MAY_LJMP(luaL_checkstring(L, 1));
6177 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006178 return 0;
6179}
6180
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006181__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006182{
6183 int wakeup_ms = lua_tointeger(L, -1);
6184 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006185 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006186 return 0;
6187}
6188
6189__LJMP static int hlua_sleep(lua_State *L)
6190{
6191 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006192 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006193
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006194 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006195
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006196 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006197 wakeup_ms = tick_add(now_ms, delay);
6198 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006199
Willy Tarreau9635e032018-10-16 17:52:55 +02006200 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006201 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006202}
6203
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006204__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006205{
6206 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006207 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006208
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006209 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006210
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006211 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006212 wakeup_ms = tick_add(now_ms, delay);
6213 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006214
Willy Tarreau9635e032018-10-16 17:52:55 +02006215 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006216 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006217}
6218
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006219/* This functionis an LUA binding. it permits to give back
6220 * the hand at the HAProxy scheduler. It is used when the
6221 * LUA processing consumes a lot of time.
6222 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006223__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006224{
6225 return 0;
6226}
6227
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006228__LJMP static int hlua_yield(lua_State *L)
6229{
Willy Tarreau9635e032018-10-16 17:52:55 +02006230 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006231 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006232}
6233
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006234/* This function change the nice of the currently executed
6235 * task. It is used set low or high priority at the current
6236 * task.
6237 */
Willy Tarreau59551662015-03-10 14:23:13 +01006238__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006239{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006240 struct hlua *hlua;
6241 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006242
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006243 MAY_LJMP(check_args(L, 1, "set_nice"));
6244 hlua = hlua_gethlua(L);
6245 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006246
6247 /* If he task is not set, I'm in a start mode. */
6248 if (!hlua || !hlua->task)
6249 return 0;
6250
6251 if (nice < -1024)
6252 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006253 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006254 nice = 1024;
6255
6256 hlua->task->nice = nice;
6257 return 0;
6258}
6259
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006260/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006261 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6262 * return an E_AGAIN signal, the emmiter of this signal must set a
6263 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006264 *
6265 * Task wrapper are longjmp safe because the only one Lua code
6266 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006267 */
Willy Tarreau83a5ff42019-08-21 14:14:50 +02006268struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006269{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006270 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006271 enum hlua_exec status;
6272
Christopher Faulet5bc99722018-04-25 10:34:45 +02006273 if (task->thread_mask == MAX_THREADS_MASK)
6274 task_set_affinity(task, tid_bit);
6275
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006276 /* If it is the first call to the task, we must initialize the
6277 * execution timeouts.
6278 */
6279 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006280 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006281
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006282 /* Execute the Lua code. */
6283 status = hlua_ctx_resume(hlua, 1);
6284
6285 switch (status) {
6286 /* finished or yield */
6287 case HLUA_E_OK:
6288 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006289 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006290 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006291 break;
6292
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006293 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006294 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006295 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006296 break;
6297
6298 /* finished with error. */
6299 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006300 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006301 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006302 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006303 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006304 break;
6305
6306 case HLUA_E_ERR:
6307 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006308 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006309 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006310 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006311 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006312 break;
6313 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006314 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006315}
6316
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006317/* This function is an LUA binding that register LUA function to be
6318 * executed after the HAProxy configuration parsing and before the
6319 * HAProxy scheduler starts. This function expect only one LUA
6320 * argument that is a function. This function returns nothing, but
6321 * throws if an error is encountered.
6322 */
6323__LJMP static int hlua_register_init(lua_State *L)
6324{
6325 struct hlua_init_function *init;
6326 int ref;
6327
6328 MAY_LJMP(check_args(L, 1, "register_init"));
6329
6330 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6331
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006332 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006333 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006334 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006335
6336 init->function_ref = ref;
6337 LIST_ADDQ(&hlua_init_functions, &init->l);
6338 return 0;
6339}
6340
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006341/* This functio is an LUA binding. It permits to register a task
6342 * executed in parallel of the main HAroxy activity. The task is
6343 * created and it is set in the HAProxy scheduler. It can be called
6344 * from the "init" section, "post init" or during the runtime.
6345 *
6346 * Lua prototype:
6347 *
6348 * <none> core.register_task(<function>)
6349 */
6350static int hlua_register_task(lua_State *L)
6351{
6352 struct hlua *hlua;
6353 struct task *task;
6354 int ref;
6355
6356 MAY_LJMP(check_args(L, 1, "register_task"));
6357
6358 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6359
Willy Tarreaubafbe012017-11-24 17:34:44 +01006360 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006361 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006362 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006363
Emeric Brunc60def82017-09-27 14:59:38 +02006364 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006365 if (!task)
6366 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6367
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006368 task->context = hlua;
6369 task->process = hlua_process_task;
6370
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006371 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006372 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006373
6374 /* Restore the function in the stack. */
6375 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6376 hlua->nargs = 0;
6377
6378 /* Schedule task. */
6379 task_schedule(task, now_ms);
6380
6381 return 0;
6382}
6383
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006384/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6385 * doesn't allow "yield" functions because the HAProxy engine cannot
6386 * resume converters.
6387 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006388static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006389{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006390 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006391 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006392 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006393
Willy Tarreaube508f12016-03-10 11:47:01 +01006394 if (!stream)
6395 return 0;
6396
Willy Tarreau87b09662015-04-03 00:22:06 +02006397 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006398 * Lua context can be not initialized. This behavior
6399 * permits to save performances because a systematic
6400 * Lua initialization cause 5% performances loss.
6401 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006402 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006403 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006404 if (!stream->hlua) {
6405 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6406 return 0;
6407 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006408 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006409 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6410 return 0;
6411 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006412 }
6413
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006414 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006415 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006416
6417 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006418 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6419 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6420 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006421 else
6422 error = "critical error";
6423 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006424 return 0;
6425 }
6426
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006427 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006428 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006429 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006430 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006431 return 0;
6432 }
6433
6434 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006435 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006436
6437 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006438 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006439 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006440 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006441 return 0;
6442 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006443 hlua_smp2lua(stream->hlua->T, smp);
6444 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006445
6446 /* push keywords in the stack. */
6447 if (arg_p) {
6448 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006449 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006450 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006451 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006452 return 0;
6453 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006454 hlua_arg2lua(stream->hlua->T, arg_p);
6455 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006456 }
6457 }
6458
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006459 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006460 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006461
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006462 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006463 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006464 }
6465
6466 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006467 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006468 /* finished. */
6469 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006470 /* If the stack is empty, the function fails. */
6471 if (lua_gettop(stream->hlua->T) <= 0)
6472 return 0;
6473
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006474 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006475 hlua_lua2smp(stream->hlua->T, -1, smp);
6476 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006477 return 1;
6478
6479 /* yield. */
6480 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006481 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006482 return 0;
6483
6484 /* finished with error. */
6485 case HLUA_E_ERRMSG:
6486 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006487 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006488 fcn->name, lua_tostring(stream->hlua->T, -1));
6489 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006490 return 0;
6491
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006492 case HLUA_E_ETMOUT:
6493 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6494 return 0;
6495
6496 case HLUA_E_NOMEM:
6497 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6498 return 0;
6499
6500 case HLUA_E_YIELD:
6501 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6502 return 0;
6503
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006504 case HLUA_E_ERR:
6505 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006506 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006507
6508 default:
6509 return 0;
6510 }
6511}
6512
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006513/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6514 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006515 * resume sample-fetches. This function will be called by the sample
6516 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006517 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006518static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6519 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006520{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006521 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006522 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006523 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006524 const struct buffer msg = { };
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006525 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006526
Willy Tarreaube508f12016-03-10 11:47:01 +01006527 if (!stream)
6528 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006529
Willy Tarreau87b09662015-04-03 00:22:06 +02006530 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006531 * Lua context can be not initialized. This behavior
6532 * permits to save performances because a systematic
6533 * Lua initialization cause 5% performances loss.
6534 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006535 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006536 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006537 if (!stream->hlua) {
6538 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6539 return 0;
6540 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006541 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006542 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6543 return 0;
6544 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006545 }
6546
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006547 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006548
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006549 if (stream->be->mode == PR_MODE_HTTP) {
6550 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
6551 hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
6552 else
6553 hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
6554 }
6555
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006556 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006557 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006558
6559 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006560 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6561 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6562 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006563 else
6564 error = "critical error";
6565 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006566 return 0;
6567 }
6568
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006569 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006570 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006571 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006572 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006573 return 0;
6574 }
6575
6576 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006577 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006578
6579 /* push arguments in the stack. */
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006580 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006581 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006582 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006583 return 0;
6584 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006585 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006586
6587 /* push keywords in the stack. */
6588 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6589 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006590 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006591 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006592 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006593 return 0;
6594 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006595 hlua_arg2lua(stream->hlua->T, arg_p);
6596 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006597 }
6598
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006599 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006600 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006601
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006602 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006603 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006604 }
6605
6606 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006607 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006608 /* finished. */
6609 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006610 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006611 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006612 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006613 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006614 /* If the stack is empty, the function fails. */
6615 if (lua_gettop(stream->hlua->T) <= 0)
6616 return 0;
6617
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006618 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006619 hlua_lua2smp(stream->hlua->T, -1, smp);
6620 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006621
6622 /* Set the end of execution flag. */
6623 smp->flags &= ~SMP_F_MAY_CHANGE;
6624 return 1;
6625
6626 /* yield. */
6627 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006628 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006629 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006630 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006631 return 0;
6632
6633 /* finished with error. */
6634 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006635 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006636 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006637 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006638 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006639 fcn->name, lua_tostring(stream->hlua->T, -1));
6640 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006641 return 0;
6642
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006643 case HLUA_E_ETMOUT:
6644 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006645 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006646 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6647 return 0;
6648
6649 case HLUA_E_NOMEM:
6650 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006651 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006652 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6653 return 0;
6654
6655 case HLUA_E_YIELD:
6656 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006657 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006658 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6659 return 0;
6660
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006661 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006662 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006663 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006664 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006665 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006666
6667 default:
6668 return 0;
6669 }
6670}
6671
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006672/* This function is an LUA binding used for registering
6673 * "sample-conv" functions. It expects a converter name used
6674 * in the haproxy configuration file, and an LUA function.
6675 */
6676__LJMP static int hlua_register_converters(lua_State *L)
6677{
6678 struct sample_conv_kw_list *sck;
6679 const char *name;
6680 int ref;
6681 int len;
6682 struct hlua_function *fcn;
6683
6684 MAY_LJMP(check_args(L, 2, "register_converters"));
6685
6686 /* First argument : converter name. */
6687 name = MAY_LJMP(luaL_checkstring(L, 1));
6688
6689 /* Second argument : lua function. */
6690 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6691
6692 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006693 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006694 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006695 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006696 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006697 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006698 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006699
6700 /* Fill fcn. */
6701 fcn->name = strdup(name);
6702 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006703 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006704 fcn->function_ref = ref;
6705
6706 /* List head */
6707 sck->list.n = sck->list.p = NULL;
6708
6709 /* converter keyword. */
6710 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006711 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006712 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006713 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006714
6715 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6716 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006717 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 +01006718 sck->kw[0].val_args = NULL;
6719 sck->kw[0].in_type = SMP_T_STR;
6720 sck->kw[0].out_type = SMP_T_STR;
6721 sck->kw[0].private = fcn;
6722
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006723 /* Register this new converter */
6724 sample_register_convs(sck);
6725
6726 return 0;
6727}
6728
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006729/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006730 * "sample-fetch" functions. It expects a converter name used
6731 * in the haproxy configuration file, and an LUA function.
6732 */
6733__LJMP static int hlua_register_fetches(lua_State *L)
6734{
6735 const char *name;
6736 int ref;
6737 int len;
6738 struct sample_fetch_kw_list *sfk;
6739 struct hlua_function *fcn;
6740
6741 MAY_LJMP(check_args(L, 2, "register_fetches"));
6742
6743 /* First argument : sample-fetch name. */
6744 name = MAY_LJMP(luaL_checkstring(L, 1));
6745
6746 /* Second argument : lua function. */
6747 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6748
6749 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006750 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006751 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006752 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006753 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006754 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006755 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006756
6757 /* Fill fcn. */
6758 fcn->name = strdup(name);
6759 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006760 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006761 fcn->function_ref = ref;
6762
6763 /* List head */
6764 sfk->list.n = sfk->list.p = NULL;
6765
6766 /* sample-fetch keyword. */
6767 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006768 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006769 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006770 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006771
6772 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6773 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006774 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 +01006775 sfk->kw[0].val_args = NULL;
6776 sfk->kw[0].out_type = SMP_T_STR;
6777 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6778 sfk->kw[0].val = 0;
6779 sfk->kw[0].private = fcn;
6780
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006781 /* Register this new fetch. */
6782 sample_register_fetches(sfk);
6783
6784 return 0;
6785}
6786
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006787/* This function is a wrapper to execute each LUA function declared
6788 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006789 * return ACT_RET_CONT if the processing is finished (with or without
6790 * error) and return ACT_RET_YIELD if the function must be called again
6791 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006792 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006793static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006794 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006795{
6796 char **arg;
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006797 unsigned int hflags = 0;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006798 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006799 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006800 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006801
6802 switch (rule->from) {
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006803 case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break;
6804 case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break;
6805 case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break;
6806 case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006807 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006808 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006809 return ACT_RET_CONT;
6810 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006811
Willy Tarreau87b09662015-04-03 00:22:06 +02006812 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006813 * Lua context can be not initialized. This behavior
6814 * permits to save performances because a systematic
6815 * Lua initialization cause 5% performances loss.
6816 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006817 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006818 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006819 if (!s->hlua) {
6820 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6821 rule->arg.hlua_rule->fcn.name);
6822 return ACT_RET_CONT;
6823 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006824 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006825 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6826 rule->arg.hlua_rule->fcn.name);
6827 return ACT_RET_CONT;
6828 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006829 }
6830
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006831 consistency_set(s, dir, &s->hlua->cons);
6832
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006833 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006834 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006835
6836 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006837 if (!SET_SAFE_LJMP(s->hlua->T)) {
6838 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6839 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006840 else
6841 error = "critical error";
6842 SEND_ERR(px, "Lua function '%s': %s.\n",
6843 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006844 return ACT_RET_CONT;
6845 }
6846
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006847 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006848 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006849 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006850 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006851 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006852 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006853 }
6854
6855 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006856 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006857
Willy Tarreau87b09662015-04-03 00:22:06 +02006858 /* Create and and push object stream in the stack. */
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006859 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006860 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006861 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006862 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006863 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006864 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006865 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006866
6867 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006868 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006869 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006870 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006871 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006872 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006873 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006874 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006875 lua_pushstring(s->hlua->T, *arg);
6876 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006877 }
6878
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006879 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006880 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006881
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006882 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006883 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006884 }
6885
Christopher Faulet719ddc82020-06-02 18:46:07 +02006886 /* Always reset the analyse expiration timeout for the corresponding
6887 * channel in case the lua script yield, to be sure to not keep an
6888 * expired timeout.
6889 */
6890 if (dir == SMP_OPT_DIR_REQ)
6891 s->req.analyse_exp = TICK_ETERNITY;
6892 else
6893 s->res.analyse_exp = TICK_ETERNITY;
6894
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006895 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006896 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006897 /* finished. */
6898 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006899 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006900 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006901 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006902 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006903 if (s->hlua->flags & HLUA_STOP)
Christopher Faulet4629d082019-07-04 11:27:15 +02006904 return ACT_RET_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006905 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006906
6907 /* yield. */
6908 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006909 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006910 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Fauletea0b0e22019-08-14 23:19:45 +02006911 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006912 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006913 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006914 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006915 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006916 /* Some actions can be wake up when a "write" event
6917 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006918 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006919 */
Christopher Fauletb22f6502019-07-26 14:54:52 +02006920 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006921 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006922 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006923 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006924 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006925 * because HAProxy is not able to manipulate data, it
6926 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006927 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006928
6929 /* finished with error. */
6930 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006931 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006932 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006933 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006934 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006935 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006936 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006937 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6938 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006939 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006940
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006941 case HLUA_E_ETMOUT:
6942 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006943 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006944 return ACT_RET_ERR;
6945 }
6946 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6947 return 0;
6948
6949 case HLUA_E_NOMEM:
6950 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006951 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006952 return ACT_RET_ERR;
6953 }
6954 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6955 return 0;
6956
6957 case HLUA_E_YIELD:
6958 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006959 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006960 return ACT_RET_ERR;
6961 }
6962 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6963 rule->arg.hlua_rule->fcn.name);
6964 return 0;
6965
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006966 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006967 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006968 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006969 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006970 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006971 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006972 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006973 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006974
6975 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006976 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006977 }
6978}
6979
Olivier Houchard9f6af332018-05-25 14:04:04 +02006980struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006981{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006982 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006983
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006984 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006985 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006986 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006987}
6988
6989static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6990{
6991 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006992 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006993 struct task *task;
6994 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006995 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006996
Willy Tarreaubafbe012017-11-24 17:34:44 +01006997 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006998 if (!hlua) {
6999 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
7000 ctx->rule->arg.hlua_rule->fcn.name);
7001 return 0;
7002 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007003 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007004 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007005 ctx->ctx.hlua_apptcp.flags = 0;
7006
7007 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007008 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007009 if (!task) {
7010 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
7011 ctx->rule->arg.hlua_rule->fcn.name);
7012 return 0;
7013 }
7014 task->nice = 0;
7015 task->context = ctx;
7016 task->process = hlua_applet_wakeup;
7017 ctx->ctx.hlua_apptcp.task = task;
7018
7019 /* In the execution wrappers linked with a stream, the
7020 * Lua context can be not initialized. This behavior
7021 * permits to save performances because a systematic
7022 * Lua initialization cause 5% performances loss.
7023 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007024 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007025 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
7026 ctx->rule->arg.hlua_rule->fcn.name);
7027 return 0;
7028 }
7029
7030 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007031 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007032
7033 /* The following Lua calls can fail. */
7034 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007035 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7036 error = lua_tostring(hlua->T, -1);
7037 else
7038 error = "critical error";
7039 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7040 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007041 return 0;
7042 }
7043
7044 /* Check stack available size. */
7045 if (!lua_checkstack(hlua->T, 1)) {
7046 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7047 ctx->rule->arg.hlua_rule->fcn.name);
7048 RESET_SAFE_LJMP(hlua->T);
7049 return 0;
7050 }
7051
7052 /* Restore the function in the stack. */
7053 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7054
7055 /* Create and and push object stream in the stack. */
7056 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7057 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7058 ctx->rule->arg.hlua_rule->fcn.name);
7059 RESET_SAFE_LJMP(hlua->T);
7060 return 0;
7061 }
7062 hlua->nargs = 1;
7063
7064 /* push keywords in the stack. */
7065 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7066 if (!lua_checkstack(hlua->T, 1)) {
7067 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7068 ctx->rule->arg.hlua_rule->fcn.name);
7069 RESET_SAFE_LJMP(hlua->T);
7070 return 0;
7071 }
7072 lua_pushstring(hlua->T, *arg);
7073 hlua->nargs++;
7074 }
7075
7076 RESET_SAFE_LJMP(hlua->T);
7077
7078 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007079 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007080 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007081
7082 return 1;
7083}
7084
Willy Tarreau83a5ff42019-08-21 14:14:50 +02007085void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007086{
7087 struct stream_interface *si = ctx->owner;
7088 struct stream *strm = si_strm(si);
7089 struct channel *res = si_ic(si);
7090 struct act_rule *rule = ctx->rule;
7091 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007092 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007093
7094 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007095 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7096 /* eat the whole request */
7097 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007098 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007099 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007100
7101 /* If the stream is disconnect or closed, ldo nothing. */
7102 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7103 return;
7104
7105 /* Execute the function. */
7106 switch (hlua_ctx_resume(hlua, 1)) {
7107 /* finished. */
7108 case HLUA_E_OK:
7109 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7110
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007111 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007112 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007113 res->flags |= CF_READ_NULL;
7114 si_shutr(si);
7115 return;
7116
7117 /* yield. */
7118 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007119 if (hlua->wake_time != TICK_ETERNITY)
7120 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007121 return;
7122
7123 /* finished with error. */
7124 case HLUA_E_ERRMSG:
7125 /* Display log. */
7126 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7127 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7128 lua_pop(hlua->T, 1);
7129 goto error;
7130
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007131 case HLUA_E_ETMOUT:
7132 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
7133 rule->arg.hlua_rule->fcn.name);
7134 goto error;
7135
7136 case HLUA_E_NOMEM:
7137 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
7138 rule->arg.hlua_rule->fcn.name);
7139 goto error;
7140
7141 case HLUA_E_YIELD: /* unexpected */
7142 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
7143 rule->arg.hlua_rule->fcn.name);
7144 goto error;
7145
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007146 case HLUA_E_ERR:
7147 /* Display log. */
7148 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
7149 rule->arg.hlua_rule->fcn.name);
7150 goto error;
7151
7152 default:
7153 goto error;
7154 }
7155
7156error:
7157
7158 /* For all other cases, just close the stream. */
7159 si_shutw(si);
7160 si_shutr(si);
7161 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7162}
7163
7164static void hlua_applet_tcp_release(struct appctx *ctx)
7165{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007166 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007167 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007168 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007169 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007170}
7171
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007172/* The function returns 1 if the initialisation is complete, 0 if
7173 * an errors occurs and -1 if more data are required for initializing
7174 * the applet.
7175 */
7176static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7177{
7178 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007179 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007180 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007181 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007182 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007183 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007184
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007185 txn = strm->txn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007186
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007187 /* We want two things in HTTP mode :
7188 * - enforce server-close mode if we were in keep-alive, so that the
7189 * applet is released after each response ;
7190 * - enable request body transfer to the applet in order to resync
7191 * with the response body.
7192 */
7193 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
7194 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007195
Willy Tarreaubafbe012017-11-24 17:34:44 +01007196 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007197 if (!hlua) {
7198 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7199 ctx->rule->arg.hlua_rule->fcn.name);
7200 return 0;
7201 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007202 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007203 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007204 ctx->ctx.hlua_apphttp.left_bytes = -1;
7205 ctx->ctx.hlua_apphttp.flags = 0;
7206
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007207 if (txn->req.flags & HTTP_MSGF_VER_11)
7208 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7209
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007210 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007211 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007212 if (!task) {
7213 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7214 ctx->rule->arg.hlua_rule->fcn.name);
7215 return 0;
7216 }
7217 task->nice = 0;
7218 task->context = ctx;
7219 task->process = hlua_applet_wakeup;
7220 ctx->ctx.hlua_apphttp.task = task;
7221
7222 /* In the execution wrappers linked with a stream, the
7223 * Lua context can be not initialized. This behavior
7224 * permits to save performances because a systematic
7225 * Lua initialization cause 5% performances loss.
7226 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007227 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007228 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
7229 ctx->rule->arg.hlua_rule->fcn.name);
7230 return 0;
7231 }
7232
7233 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007234 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007235
7236 /* The following Lua calls can fail. */
7237 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007238 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7239 error = lua_tostring(hlua->T, -1);
7240 else
7241 error = "critical error";
7242 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7243 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007244 return 0;
7245 }
7246
7247 /* Check stack available size. */
7248 if (!lua_checkstack(hlua->T, 1)) {
7249 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7250 ctx->rule->arg.hlua_rule->fcn.name);
7251 RESET_SAFE_LJMP(hlua->T);
7252 return 0;
7253 }
7254
7255 /* Restore the function in the stack. */
7256 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7257
7258 /* Create and and push object stream in the stack. */
7259 if (!hlua_applet_http_new(hlua->T, ctx)) {
7260 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7261 ctx->rule->arg.hlua_rule->fcn.name);
7262 RESET_SAFE_LJMP(hlua->T);
7263 return 0;
7264 }
7265 hlua->nargs = 1;
7266
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007267 /* push keywords in the stack. */
7268 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7269 if (!lua_checkstack(hlua->T, 1)) {
7270 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7271 ctx->rule->arg.hlua_rule->fcn.name);
7272 RESET_SAFE_LJMP(hlua->T);
7273 return 0;
7274 }
7275 lua_pushstring(hlua->T, *arg);
7276 hlua->nargs++;
7277 }
7278
7279 RESET_SAFE_LJMP(hlua->T);
7280
7281 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007282 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007283
7284 return 1;
7285}
7286
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007287static void hlua_applet_htx_fct(struct appctx *ctx)
7288{
7289 struct stream_interface *si = ctx->owner;
7290 struct stream *strm = si_strm(si);
7291 struct channel *req = si_oc(si);
7292 struct channel *res = si_ic(si);
7293 struct act_rule *rule = ctx->rule;
7294 struct proxy *px = strm->be;
7295 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7296 struct htx *req_htx, *res_htx;
7297
7298 res_htx = htx_from_buf(&res->buf);
7299
7300 /* If the stream is disconnect or closed, ldo nothing. */
7301 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7302 goto out;
7303
7304 /* Check if the input buffer is avalaible. */
7305 if (!b_size(&res->buf)) {
7306 si_rx_room_blk(si);
7307 goto out;
7308 }
7309 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007310 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007311 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7312
7313 /* Set the currently running flag. */
7314 if (!HLUA_IS_RUNNING(hlua) &&
7315 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7316 struct htx_blk *blk;
7317 size_t count = co_data(req);
7318
7319 if (!count) {
7320 si_cant_get(si);
7321 goto out;
7322 }
7323
7324 /* We need to flush the request header. This left the body for
7325 * the Lua.
7326 */
7327 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007328 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007329 while (count && blk) {
7330 enum htx_blk_type type = htx_get_blk_type(blk);
7331 uint32_t sz = htx_get_blksz(blk);
7332
7333 if (sz > count) {
7334 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007335 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007336 goto out;
7337 }
7338
7339 count -= sz;
7340 co_set_data(req, co_data(req) - sz);
7341 blk = htx_remove_blk(req_htx, blk);
7342
7343 if (type == HTX_BLK_EOH)
7344 break;
7345 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007346 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007347 }
7348
7349 /* Executes The applet if it is not done. */
7350 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7351
7352 /* Execute the function. */
7353 switch (hlua_ctx_resume(hlua, 1)) {
7354 /* finished. */
7355 case HLUA_E_OK:
7356 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7357 break;
7358
7359 /* yield. */
7360 case HLUA_E_AGAIN:
7361 if (hlua->wake_time != TICK_ETERNITY)
7362 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007363 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007364
7365 /* finished with error. */
7366 case HLUA_E_ERRMSG:
7367 /* Display log. */
7368 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7369 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7370 lua_pop(hlua->T, 1);
7371 goto error;
7372
7373 case HLUA_E_ETMOUT:
7374 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7375 rule->arg.hlua_rule->fcn.name);
7376 goto error;
7377
7378 case HLUA_E_NOMEM:
7379 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7380 rule->arg.hlua_rule->fcn.name);
7381 goto error;
7382
7383 case HLUA_E_YIELD: /* unexpected */
7384 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7385 rule->arg.hlua_rule->fcn.name);
7386 goto error;
7387
7388 case HLUA_E_ERR:
7389 /* Display log. */
7390 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7391 rule->arg.hlua_rule->fcn.name);
7392 goto error;
7393
7394 default:
7395 goto error;
7396 }
7397 }
7398
7399 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007400 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7401 goto done;
7402
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007403 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7404 goto error;
7405
Christopher Faulet54b5e212019-06-04 10:08:28 +02007406 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007407 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7408 si_rx_room_blk(si);
7409 goto out;
7410 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007411 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007412 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7413 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007414 }
7415
7416 done:
7417 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007418 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007419 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007420 si_shutr(si);
7421 }
7422
7423 /* eat the whole request */
7424 if (co_data(req)) {
7425 req_htx = htx_from_buf(&req->buf);
7426 co_htx_skip(req, req_htx, co_data(req));
7427 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007428 }
7429 }
7430
7431 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007432 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007433 return;
7434
7435 error:
7436
7437 /* If we are in HTTP mode, and we are not send any
7438 * data, return a 500 server error in best effort:
7439 * if there is no room available in the buffer,
7440 * just close the connection.
7441 */
7442 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7443 struct buffer *err = &htx_err_chunks[HTTP_ERR_500];
7444
7445 channel_erase(res);
7446 res->buf.data = b_data(err);
7447 memcpy(res->buf.area, b_head(err), b_data(err));
7448 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007449 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007450 }
7451 if (!(strm->flags & SF_ERR_MASK))
7452 strm->flags |= SF_ERR_RESOURCE;
7453 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7454 goto done;
7455}
7456
Willy Tarreau83a5ff42019-08-21 14:14:50 +02007457void hlua_applet_http_fct(struct appctx *ctx)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007458{
7459 struct stream_interface *si = ctx->owner;
7460 struct stream *strm = si_strm(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007461 struct channel *req = si_oc(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007462 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007463 struct act_rule *rule = ctx->rule;
7464 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007465 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02007466 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007467 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02007468 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007469 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007470 int ret;
7471
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007472 if (IS_HTX_STRM(strm))
7473 return hlua_applet_htx_fct(ctx);
7474
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007475 /* If the stream is disconnect or closed, ldo nothing. */
7476 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007477 goto out;
7478
7479 /* Check if the input buffer is avalaible. */
7480 if (!b_size(&res->buf)) {
7481 si_rx_room_blk(si);
7482 goto out;
7483 }
7484 /* check that the output is not closed */
7485 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
7486 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007487
7488 /* Set the currently running flag. */
7489 if (!HLUA_IS_RUNNING(hlua) &&
7490 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007491 /* Store the max amount of bytes that we can read. */
7492 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
7493
7494 /* We need to flush the request header. This left the body
7495 * for the Lua.
7496 */
7497
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007498 /* Read the maximum amount of data available. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007499 ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007500 if (ret == -1)
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007501 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007502
7503 /* No data available, ask for more data. */
7504 if (ret == 1)
7505 len2 = 0;
7506 if (ret == 0)
7507 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02007508 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007509 si_cant_get(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007510 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007511 }
7512
7513 /* skip the requests bytes. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007514 co_skip(req, strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007515 }
7516
7517 /* Executes The applet if it is not done. */
7518 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7519
7520 /* Execute the function. */
7521 switch (hlua_ctx_resume(hlua, 1)) {
7522 /* finished. */
7523 case HLUA_E_OK:
7524 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7525 break;
7526
7527 /* yield. */
7528 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007529 if (hlua->wake_time != TICK_ETERNITY)
7530 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007531 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007532
7533 /* finished with error. */
7534 case HLUA_E_ERRMSG:
7535 /* Display log. */
7536 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7537 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7538 lua_pop(hlua->T, 1);
7539 goto error;
7540
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007541 case HLUA_E_ETMOUT:
7542 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7543 rule->arg.hlua_rule->fcn.name);
7544 goto error;
7545
7546 case HLUA_E_NOMEM:
7547 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7548 rule->arg.hlua_rule->fcn.name);
7549 goto error;
7550
7551 case HLUA_E_YIELD: /* unexpected */
7552 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7553 rule->arg.hlua_rule->fcn.name);
7554 goto error;
7555
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007556 case HLUA_E_ERR:
7557 /* Display log. */
7558 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7559 rule->arg.hlua_rule->fcn.name);
7560 goto error;
7561
7562 default:
7563 goto error;
7564 }
7565 }
7566
7567 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007568 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7569 goto done;
7570
Christopher Fauletcc26b132018-12-18 21:20:57 +01007571 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7572 goto error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007573
7574 /* We must send the final chunk. */
7575 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
7576 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
7577
7578 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02007579 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007580
7581 /* critical error. */
7582 if (ret == -2 || ret == -3) {
7583 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
7584 rule->arg.hlua_rule->fcn.name);
7585 goto error;
7586 }
7587
7588 /* no enough space error. */
7589 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01007590 si_rx_room_blk(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007591 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007592 }
7593
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007594 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7595 ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007596 }
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007597 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007598
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007599 done:
7600 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
7601 if (!(res->flags & CF_SHUTR)) {
7602 res->flags |= CF_READ_NULL;
7603 si_shutr(si);
7604 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007605
7606 /* eat the whole request */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007607 if (co_data(req))
7608 co_skip(req, co_data(req));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007609 }
7610
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007611 out:
7612 return;
7613
7614 error:
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007615
7616 /* If we are in HTTP mode, and we are not send any
7617 * data, return a 500 server error in best effort:
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007618 * if there is no room available in the buffer,
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007619 * just close the connection.
7620 */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007621 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7622 channel_erase(res);
7623 ci_putblk(res, error_500, strlen(error_500));
7624 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007625 if (!(strm->flags & SF_ERR_MASK))
7626 strm->flags |= SF_ERR_RESOURCE;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007627 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007628 goto done;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007629}
7630
7631static void hlua_applet_http_release(struct appctx *ctx)
7632{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007633 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007634 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007635 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007636 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007637}
7638
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007639/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
7640 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007641 *
7642 * This function can fail with an abort() due to an Lua critical error.
7643 * We are in the configuration parsing process of HAProxy, this abort() is
7644 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007645 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007646static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7647 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007648{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007649 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007650 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007651
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007652 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007653 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007654 if (!rule->arg.hlua_rule) {
7655 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007656 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007657 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007658
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007659 /* Memory for arguments. */
7660 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7661 if (!rule->arg.hlua_rule->args) {
7662 memprintf(err, "out of memory error");
7663 return ACT_RET_PRS_ERR;
7664 }
7665
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007666 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007667 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007668
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007669 /* Expect some arguments */
7670 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007671 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007672 memprintf(err, "expect %d arguments", fcn->nargs);
7673 return ACT_RET_PRS_ERR;
7674 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007675 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007676 if (!rule->arg.hlua_rule->args[i]) {
7677 memprintf(err, "out of memory error");
7678 return ACT_RET_PRS_ERR;
7679 }
7680 (*cur_arg)++;
7681 }
7682 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007683
Thierry FOURNIER42148732015-09-02 17:17:33 +02007684 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007685 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007686 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007687}
7688
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007689static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7690 struct act_rule *rule, char **err)
7691{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007692 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007693
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007694 /* HTTP applets are forbidden in tcp-request rules.
7695 * HTTP applet request requires everything initilized by
7696 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
7697 * The applet will be immediately initilized, but its before
7698 * the call of this analyzer.
7699 */
7700 if (rule->from != ACT_F_HTTP_REQ) {
7701 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7702 return ACT_RET_PRS_ERR;
7703 }
7704
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007705 /* Memory for the rule. */
7706 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7707 if (!rule->arg.hlua_rule) {
7708 memprintf(err, "out of memory error");
7709 return ACT_RET_PRS_ERR;
7710 }
7711
7712 /* Reference the Lua function and store the reference. */
7713 rule->arg.hlua_rule->fcn = *fcn;
7714
7715 /* TODO: later accept arguments. */
7716 rule->arg.hlua_rule->args = NULL;
7717
7718 /* Add applet pointer in the rule. */
7719 rule->applet.obj_type = OBJ_TYPE_APPLET;
7720 rule->applet.name = fcn->name;
7721 rule->applet.init = hlua_applet_http_init;
7722 rule->applet.fct = hlua_applet_http_fct;
7723 rule->applet.release = hlua_applet_http_release;
7724 rule->applet.timeout = hlua_timeout_applet;
7725
7726 return ACT_RET_PRS_OK;
7727}
7728
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007729/* This function is an LUA binding used for registering
7730 * "sample-conv" functions. It expects a converter name used
7731 * in the haproxy configuration file, and an LUA function.
7732 */
7733__LJMP static int hlua_register_action(lua_State *L)
7734{
7735 struct action_kw_list *akl;
7736 const char *name;
7737 int ref;
7738 int len;
7739 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007740 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007741
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007742 /* Initialise the number of expected arguments at 0. */
7743 nargs = 0;
7744
7745 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7746 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007747
7748 /* First argument : converter name. */
7749 name = MAY_LJMP(luaL_checkstring(L, 1));
7750
7751 /* Second argument : environment. */
7752 if (lua_type(L, 2) != LUA_TTABLE)
7753 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7754
7755 /* Third argument : lua function. */
7756 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7757
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007758 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007759 if (lua_gettop(L) >= 4)
7760 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7761
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007762 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007763 lua_pushnil(L);
7764 while (lua_next(L, 2) != 0) {
7765 if (lua_type(L, -1) != LUA_TSTRING)
7766 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7767
7768 /* Check required environment. Only accepted "http" or "tcp". */
7769 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007770 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007771 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007772 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007773 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007774 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007775 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007776
7777 /* Fill fcn. */
7778 fcn->name = strdup(name);
7779 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007780 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007781 fcn->function_ref = ref;
7782
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007783 /* Set the expected number od arguments. */
7784 fcn->nargs = nargs;
7785
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007786 /* List head */
7787 akl->list.n = akl->list.p = NULL;
7788
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007789 /* action keyword. */
7790 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007791 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007792 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007793 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007794
7795 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7796
7797 akl->kw[0].match_pfx = 0;
7798 akl->kw[0].private = fcn;
7799 akl->kw[0].parse = action_register_lua;
7800
7801 /* select the action registering point. */
7802 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7803 tcp_req_cont_keywords_register(akl);
7804 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7805 tcp_res_cont_keywords_register(akl);
7806 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7807 http_req_keywords_register(akl);
7808 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7809 http_res_keywords_register(akl);
7810 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007811 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007812 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7813 "are expected.", lua_tostring(L, -1)));
7814
7815 /* pop the environment string. */
7816 lua_pop(L, 1);
7817 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007818 return ACT_RET_PRS_OK;
7819}
7820
7821static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7822 struct act_rule *rule, char **err)
7823{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007824 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007825
Christopher Fauleteb709802019-04-11 22:04:08 +02007826 if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) {
Christopher Fauletafd8f102018-11-08 11:34:21 +01007827 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
7828 return ACT_RET_PRS_ERR;
7829 }
7830
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007831 /* Memory for the rule. */
7832 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7833 if (!rule->arg.hlua_rule) {
7834 memprintf(err, "out of memory error");
7835 return ACT_RET_PRS_ERR;
7836 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007837
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007838 /* Reference the Lua function and store the reference. */
7839 rule->arg.hlua_rule->fcn = *fcn;
7840
7841 /* TODO: later accept arguments. */
7842 rule->arg.hlua_rule->args = NULL;
7843
7844 /* Add applet pointer in the rule. */
7845 rule->applet.obj_type = OBJ_TYPE_APPLET;
7846 rule->applet.name = fcn->name;
7847 rule->applet.init = hlua_applet_tcp_init;
7848 rule->applet.fct = hlua_applet_tcp_fct;
7849 rule->applet.release = hlua_applet_tcp_release;
7850 rule->applet.timeout = hlua_timeout_applet;
7851
7852 return 0;
7853}
7854
7855/* This function is an LUA binding used for registering
7856 * "sample-conv" functions. It expects a converter name used
7857 * in the haproxy configuration file, and an LUA function.
7858 */
7859__LJMP static int hlua_register_service(lua_State *L)
7860{
7861 struct action_kw_list *akl;
7862 const char *name;
7863 const char *env;
7864 int ref;
7865 int len;
7866 struct hlua_function *fcn;
7867
7868 MAY_LJMP(check_args(L, 3, "register_service"));
7869
7870 /* First argument : converter name. */
7871 name = MAY_LJMP(luaL_checkstring(L, 1));
7872
7873 /* Second argument : environment. */
7874 env = MAY_LJMP(luaL_checkstring(L, 2));
7875
7876 /* Third argument : lua function. */
7877 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7878
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007879 /* Allocate and fill the sample fetch keyword struct. */
7880 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7881 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007882 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007883 fcn = calloc(1, sizeof(*fcn));
7884 if (!fcn)
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
7887 /* Fill fcn. */
7888 len = strlen("<lua.>") + strlen(name) + 1;
7889 fcn->name = calloc(1, len);
7890 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007891 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007892 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7893 fcn->function_ref = ref;
7894
7895 /* List head */
7896 akl->list.n = akl->list.p = NULL;
7897
7898 /* converter keyword. */
7899 len = strlen("lua.") + strlen(name) + 1;
7900 akl->kw[0].kw = calloc(1, len);
7901 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007902 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007903
7904 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7905
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007906 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007907 if (strcmp(env, "tcp") == 0)
7908 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007909 else if (strcmp(env, "http") == 0)
7910 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007911 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007912 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007913 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007914
7915 akl->kw[0].match_pfx = 0;
7916 akl->kw[0].private = fcn;
7917
7918 /* End of array. */
7919 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7920
7921 /* Register this new converter */
7922 service_keywords_register(akl);
7923
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007924 return 0;
7925}
7926
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007927/* This function initialises Lua cli handler. It copies the
7928 * arguments in the Lua stack and create channel IO objects.
7929 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007930static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007931{
7932 struct hlua *hlua;
7933 struct hlua_function *fcn;
7934 int i;
7935 const char *error;
7936
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007937 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007938 appctx->ctx.hlua_cli.fcn = private;
7939
Willy Tarreaubafbe012017-11-24 17:34:44 +01007940 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007941 if (!hlua) {
7942 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007943 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007944 }
7945 HLUA_INIT(hlua);
7946 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007947
7948 /* Create task used by signal to wakeup applets.
7949 * We use the same wakeup fonction than the Lua applet_tcp and
7950 * applet_http. It is absolutely compatible.
7951 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007952 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007953 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007954 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007955 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007956 }
7957 appctx->ctx.hlua_cli.task->nice = 0;
7958 appctx->ctx.hlua_cli.task->context = appctx;
7959 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7960
7961 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007962 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007963 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007964 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007965 }
7966
7967 /* The following Lua calls can fail. */
7968 if (!SET_SAFE_LJMP(hlua->T)) {
7969 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7970 error = lua_tostring(hlua->T, -1);
7971 else
7972 error = "critical error";
7973 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7974 goto error;
7975 }
7976
7977 /* Check stack available size. */
7978 if (!lua_checkstack(hlua->T, 2)) {
7979 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7980 goto error;
7981 }
7982
7983 /* Restore the function in the stack. */
7984 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7985
7986 /* Once the arguments parsed, the CLI is like an AppletTCP,
7987 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007988 */
7989 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7990 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7991 goto error;
7992 }
7993 hlua->nargs = 1;
7994
7995 /* push keywords in the stack. */
7996 for (i = 0; *args[i]; i++) {
7997 /* Check stack available size. */
7998 if (!lua_checkstack(hlua->T, 1)) {
7999 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8000 goto error;
8001 }
8002 lua_pushstring(hlua->T, args[i]);
8003 hlua->nargs++;
8004 }
8005
8006 /* We must initialize the execution timeouts. */
8007 hlua->max_time = hlua_timeout_session;
8008
8009 /* At this point the execution is safe. */
8010 RESET_SAFE_LJMP(hlua->T);
8011
8012 /* It's ok */
8013 return 0;
8014
8015 /* It's not ok. */
8016error:
8017 RESET_SAFE_LJMP(hlua->T);
8018 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008019 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008020 return 1;
8021}
8022
8023static int hlua_cli_io_handler_fct(struct appctx *appctx)
8024{
8025 struct hlua *hlua;
8026 struct stream_interface *si;
8027 struct hlua_function *fcn;
8028
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008029 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008030 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008031 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008032
8033 /* If the stream is disconnect or closed, ldo nothing. */
8034 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8035 return 1;
8036
8037 /* Execute the function. */
8038 switch (hlua_ctx_resume(hlua, 1)) {
8039
8040 /* finished. */
8041 case HLUA_E_OK:
8042 return 1;
8043
8044 /* yield. */
8045 case HLUA_E_AGAIN:
8046 /* We want write. */
8047 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008048 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008049 /* Set the timeout. */
8050 if (hlua->wake_time != TICK_ETERNITY)
8051 task_schedule(hlua->task, hlua->wake_time);
8052 return 0;
8053
8054 /* finished with error. */
8055 case HLUA_E_ERRMSG:
8056 /* Display log. */
8057 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8058 fcn->name, lua_tostring(hlua->T, -1));
8059 lua_pop(hlua->T, 1);
8060 return 1;
8061
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008062 case HLUA_E_ETMOUT:
8063 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8064 fcn->name);
8065 return 1;
8066
8067 case HLUA_E_NOMEM:
8068 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8069 fcn->name);
8070 return 1;
8071
8072 case HLUA_E_YIELD: /* unexpected */
8073 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8074 fcn->name);
8075 return 1;
8076
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008077 case HLUA_E_ERR:
8078 /* Display log. */
8079 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8080 fcn->name);
8081 return 1;
8082
8083 default:
8084 return 1;
8085 }
8086
8087 return 1;
8088}
8089
8090static void hlua_cli_io_release_fct(struct appctx *appctx)
8091{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008092 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008093 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008094}
8095
8096/* This function is an LUA binding used for registering
8097 * new keywords in the cli. It expects a list of keywords
8098 * which are the "path". It is limited to 5 keywords. A
8099 * description of the command, a function to be executed
8100 * for the parsing and a function for io handlers.
8101 */
8102__LJMP static int hlua_register_cli(lua_State *L)
8103{
8104 struct cli_kw_list *cli_kws;
8105 const char *message;
8106 int ref_io;
8107 int len;
8108 struct hlua_function *fcn;
8109 int index;
8110 int i;
8111
8112 MAY_LJMP(check_args(L, 3, "register_cli"));
8113
8114 /* First argument : an array of maximum 5 keywords. */
8115 if (!lua_istable(L, 1))
8116 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8117
8118 /* Second argument : string with contextual message. */
8119 message = MAY_LJMP(luaL_checkstring(L, 2));
8120
8121 /* Third and fourth argument : lua function. */
8122 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8123
8124 /* Allocate and fill the sample fetch keyword struct. */
8125 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
8126 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008127 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008128 fcn = calloc(1, sizeof(*fcn));
8129 if (!fcn)
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
8132 /* Fill path. */
8133 index = 0;
8134 lua_pushnil(L);
8135 while(lua_next(L, 1) != 0) {
8136 if (index >= 5)
8137 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8138 if (lua_type(L, -1) != LUA_TSTRING)
8139 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8140 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
8141 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008142 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008143 index++;
8144 lua_pop(L, 1);
8145 }
8146
8147 /* Copy help message. */
8148 cli_kws->kw[0].usage = strdup(message);
8149 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008150 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008151
8152 /* Fill fcn io handler. */
8153 len = strlen("<lua.cli>") + 1;
8154 for (i = 0; i < index; i++)
8155 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8156 fcn->name = calloc(1, len);
8157 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008158 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008159 strncat((char *)fcn->name, "<lua.cli", len);
8160 for (i = 0; i < index; i++) {
8161 strncat((char *)fcn->name, ".", len);
8162 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8163 }
8164 strncat((char *)fcn->name, ">", len);
8165 fcn->function_ref = ref_io;
8166
8167 /* Fill last entries. */
8168 cli_kws->kw[0].private = fcn;
8169 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8170 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8171 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8172
8173 /* Register this new converter */
8174 cli_register_kw(cli_kws);
8175
8176 return 0;
8177}
8178
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008179static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
8180 struct proxy *defpx, const char *file, int line,
8181 char **err, unsigned int *timeout)
8182{
8183 const char *error;
8184
8185 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008186 if (error == PARSE_TIME_OVER) {
8187 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8188 args[1], args[0]);
8189 return -1;
8190 }
8191 else if (error == PARSE_TIME_UNDER) {
8192 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8193 args[1], args[0]);
8194 return -1;
8195 }
8196 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008197 memprintf(err, "%s: invalid timeout", args[0]);
8198 return -1;
8199 }
8200 return 0;
8201}
8202
8203static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
8204 struct proxy *defpx, const char *file, int line,
8205 char **err)
8206{
8207 return hlua_read_timeout(args, section_type, curpx, defpx,
8208 file, line, err, &hlua_timeout_session);
8209}
8210
8211static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8212 struct proxy *defpx, const char *file, int line,
8213 char **err)
8214{
8215 return hlua_read_timeout(args, section_type, curpx, defpx,
8216 file, line, err, &hlua_timeout_task);
8217}
8218
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008219static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8220 struct proxy *defpx, const char *file, int line,
8221 char **err)
8222{
8223 return hlua_read_timeout(args, section_type, curpx, defpx,
8224 file, line, err, &hlua_timeout_applet);
8225}
8226
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008227static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
8228 struct proxy *defpx, const char *file, int line,
8229 char **err)
8230{
8231 char *error;
8232
8233 hlua_nb_instruction = strtoll(args[1], &error, 10);
8234 if (*error != '\0') {
8235 memprintf(err, "%s: invalid number", args[0]);
8236 return -1;
8237 }
8238 return 0;
8239}
8240
Willy Tarreau32f61e22015-03-18 17:54:59 +01008241static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8242 struct proxy *defpx, const char *file, int line,
8243 char **err)
8244{
8245 char *error;
8246
8247 if (*(args[1]) == 0) {
8248 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8249 return -1;
8250 }
8251 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8252 if (*error != '\0') {
8253 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8254 return -1;
8255 }
8256 return 0;
8257}
8258
8259
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008260/* This function is called by the main configuration key "lua-load". It loads and
8261 * execute an lua file during the parsing of the HAProxy configuration file. It is
8262 * the main lua entry point.
8263 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008264 * This function runs with the HAProxy keywords API. It returns -1 if an error
8265 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008266 *
8267 * In some error case, LUA set an error message in top of the stack. This function
8268 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008269 *
8270 * This function can fail with an abort() due to an Lua critical error.
8271 * We are in the configuration parsing process of HAProxy, this abort() is
8272 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008273 */
8274static int hlua_load(char **args, int section_type, struct proxy *curpx,
8275 struct proxy *defpx, const char *file, int line,
8276 char **err)
8277{
8278 int error;
8279
8280 /* Just load and compile the file. */
8281 error = luaL_loadfile(gL.T, args[1]);
8282 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008283 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008284 lua_pop(gL.T, 1);
8285 return -1;
8286 }
8287
8288 /* If no syntax error where detected, execute the code. */
8289 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
8290 switch (error) {
8291 case LUA_OK:
8292 break;
8293 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008294 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008295 lua_pop(gL.T, 1);
8296 return -1;
8297 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008298 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008299 return -1;
8300 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008301 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008302 lua_pop(gL.T, 1);
8303 return -1;
Christopher Faulet20699902020-07-28 10:33:25 +02008304#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008305 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008306 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008307 lua_pop(gL.T, 1);
8308 return -1;
Christopher Faulet20699902020-07-28 10:33:25 +02008309#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008310 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008311 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008312 lua_pop(gL.T, 1);
8313 return -1;
8314 }
8315
8316 return 0;
8317}
8318
8319/* configuration keywords declaration */
8320static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008321 { CFG_GLOBAL, "lua-load", hlua_load },
8322 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8323 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008324 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008325 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008326 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008327 { 0, NULL, NULL },
8328}};
8329
Willy Tarreau0108d902018-11-25 19:14:37 +01008330INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8331
Christopher Fauletafd8f102018-11-08 11:34:21 +01008332
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008333/* This function can fail with an abort() due to an Lua critical error.
8334 * We are in the initialisation process of HAProxy, this abort() is
8335 * tolerated.
8336 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008337int hlua_post_init()
8338{
8339 struct hlua_init_function *init;
8340 const char *msg;
8341 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008342 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008343
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008344 /* Call post initialisation function in safe environement. */
8345 if (!SET_SAFE_LJMP(gL.T)) {
8346 if (lua_type(gL.T, -1) == LUA_TSTRING)
8347 error = lua_tostring(gL.T, -1);
8348 else
8349 error = "critical error";
8350 fprintf(stderr, "Lua post-init: %s.\n", error);
8351 exit(1);
8352 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008353
8354#if USE_OPENSSL
8355 /* Initialize SSL server. */
8356 if (socket_ssl.xprt->prepare_srv) {
8357 int saved_used_backed = global.ssl_used_backend;
8358 // don't affect maxconn automatic computation
8359 socket_ssl.xprt->prepare_srv(&socket_ssl);
8360 global.ssl_used_backend = saved_used_backed;
8361 }
8362#endif
8363
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008364 hlua_fcn_post_init(gL.T);
8365 RESET_SAFE_LJMP(gL.T);
8366
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008367 list_for_each_entry(init, &hlua_init_functions, l) {
8368 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
8369 ret = hlua_ctx_resume(&gL, 0);
8370 switch (ret) {
8371 case HLUA_E_OK:
8372 lua_pop(gL.T, -1);
8373 return 1;
8374 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008375 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008376 return 0;
8377 case HLUA_E_ERRMSG:
8378 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008379 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008380 return 0;
8381 case HLUA_E_ERR:
8382 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008383 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008384 return 0;
8385 }
8386 }
8387 return 1;
8388}
8389
Willy Tarreau32f61e22015-03-18 17:54:59 +01008390/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8391 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8392 * is the previously allocated size or the kind of object in case of a new
8393 * allocation. <nsize> is the requested new size.
8394 */
8395static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8396{
8397 struct hlua_mem_allocator *zone = ud;
8398
8399 if (nsize == 0) {
8400 /* it's a free */
8401 if (ptr)
8402 zone->allocated -= osize;
8403 free(ptr);
8404 return NULL;
8405 }
8406
8407 if (!ptr) {
8408 /* it's a new allocation */
8409 if (zone->limit && zone->allocated + nsize > zone->limit)
8410 return NULL;
8411
8412 ptr = malloc(nsize);
8413 if (ptr)
8414 zone->allocated += nsize;
8415 return ptr;
8416 }
8417
8418 /* it's a realloc */
8419 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8420 return NULL;
8421
8422 ptr = realloc(ptr, nsize);
8423 if (ptr)
8424 zone->allocated += nsize - osize;
8425 return ptr;
8426}
8427
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008428/* Ithis function can fail with an abort() due to an Lua critical error.
8429 * We are in the initialisation process of HAProxy, this abort() is
8430 * tolerated.
8431 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008432void hlua_init(void)
8433{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008434 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008435 int idx;
8436 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008437 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008438 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008439 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008440#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008441 struct srv_kw *kw;
8442 int tmp_error;
8443 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008444 char *args[] = { /* SSL client configuration. */
8445 "ssl",
8446 "verify",
8447 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008448 NULL
8449 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008450#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008451
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008452 /* Init main lua stack. */
8453 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008454 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008455 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008456 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008457 hlua_sethlua(&gL);
8458 gL.Tref = LUA_REFNIL;
8459 gL.task = NULL;
8460
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008461 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008462 * the Lua function can fail with an abort. We are in the initialisation
8463 * process of HAProxy, this abort() is tolerated.
8464 */
8465
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008466 /* Initialise lua. */
8467 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008468
Thierry Fournier75933d42016-01-21 09:30:18 +01008469 /* Set safe environment for the initialisation. */
8470 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008471 if (lua_type(gL.T, -1) == LUA_TSTRING)
8472 error_msg = lua_tostring(gL.T, -1);
8473 else
8474 error_msg = "critical error";
8475 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008476 exit(1);
8477 }
8478
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008479 /*
8480 *
8481 * Create "core" object.
8482 *
8483 */
8484
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008485 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008486 lua_newtable(gL.T);
8487
8488 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008489 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008490 hlua_class_const_int(gL.T, log_levels[i], i);
8491
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008492 /* Register special functions. */
8493 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008494 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008495 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008496 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008497 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008498 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008499 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008500 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008501 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008502 hlua_class_function(gL.T, "sleep", hlua_sleep);
8503 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008504 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8505 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8506 hlua_class_function(gL.T, "set_map", hlua_set_map);
8507 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008508 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008509 hlua_class_function(gL.T, "log", hlua_log);
8510 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8511 hlua_class_function(gL.T, "Info", hlua_log_info);
8512 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8513 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008514 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008515 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008516
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008517 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008518
8519 /*
8520 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008521 * Register class Map
8522 *
8523 */
8524
8525 /* This table entry is the object "Map" base. */
8526 lua_newtable(gL.T);
8527
8528 /* register pattern types. */
8529 for (i=0; i<PAT_MATCH_NUM; i++)
8530 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008531 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008532 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8533 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008534 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008535
8536 /* register constructor. */
8537 hlua_class_function(gL.T, "new", hlua_map_new);
8538
8539 /* Create and fill the metatable. */
8540 lua_newtable(gL.T);
8541
8542 /* Create and fille the __index entry. */
8543 lua_pushstring(gL.T, "__index");
8544 lua_newtable(gL.T);
8545
8546 /* Register . */
8547 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8548 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8549
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008550 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008551
Thierry Fournier45e78d72016-02-19 18:34:46 +01008552 /* Register previous table in the registry with reference and named entry.
8553 * The function hlua_register_metatable() pops the stack, so we
8554 * previously create a copy of the table.
8555 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008556 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008557 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008558
8559 /* Assign the metatable to the mai Map object. */
8560 lua_setmetatable(gL.T, -2);
8561
8562 /* Set a name to the table. */
8563 lua_setglobal(gL.T, "Map");
8564
8565 /*
8566 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008567 * Register class Channel
8568 *
8569 */
8570
8571 /* Create and fill the metatable. */
8572 lua_newtable(gL.T);
8573
8574 /* Create and fille the __index entry. */
8575 lua_pushstring(gL.T, "__index");
8576 lua_newtable(gL.T);
8577
8578 /* Register . */
8579 hlua_class_function(gL.T, "get", hlua_channel_get);
8580 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8581 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8582 hlua_class_function(gL.T, "set", hlua_channel_set);
8583 hlua_class_function(gL.T, "append", hlua_channel_append);
8584 hlua_class_function(gL.T, "send", hlua_channel_send);
8585 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8586 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8587 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008588 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008589
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008590 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008591
8592 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008593 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008594
8595 /*
8596 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008597 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008598 *
8599 */
8600
8601 /* Create and fill the metatable. */
8602 lua_newtable(gL.T);
8603
8604 /* Create and fille the __index entry. */
8605 lua_pushstring(gL.T, "__index");
8606 lua_newtable(gL.T);
8607
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008608 /* Browse existing fetches and create the associated
8609 * object method.
8610 */
8611 sf = NULL;
8612 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8613
8614 /* Dont register the keywork if the arguments check function are
8615 * not safe during the runtime.
8616 */
8617 if ((sf->val_args != NULL) &&
8618 (sf->val_args != val_payload_lv) &&
8619 (sf->val_args != val_hdr))
8620 continue;
8621
8622 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8623 * by an underscore.
8624 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008625 strncpy(trash.area, sf->kw, trash.size);
8626 trash.area[trash.size - 1] = '\0';
8627 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008628 if (*p == '.' || *p == '-' || *p == '+')
8629 *p = '_';
8630
8631 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008632 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008633 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008634 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008635 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008636 }
8637
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008638 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008639
8640 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008641 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008642
8643 /*
8644 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008645 * Register class Converters
8646 *
8647 */
8648
8649 /* Create and fill the metatable. */
8650 lua_newtable(gL.T);
8651
8652 /* Create and fill the __index entry. */
8653 lua_pushstring(gL.T, "__index");
8654 lua_newtable(gL.T);
8655
8656 /* Browse existing converters and create the associated
8657 * object method.
8658 */
8659 sc = NULL;
8660 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8661 /* Dont register the keywork if the arguments check function are
8662 * not safe during the runtime.
8663 */
8664 if (sc->val_args != NULL)
8665 continue;
8666
8667 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8668 * by an underscore.
8669 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008670 strncpy(trash.area, sc->kw, trash.size);
8671 trash.area[trash.size - 1] = '\0';
8672 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008673 if (*p == '.' || *p == '-' || *p == '+')
8674 *p = '_';
8675
8676 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008677 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008678 lua_pushlightuserdata(gL.T, sc);
8679 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008680 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008681 }
8682
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008683 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008684
8685 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008686 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008687
8688 /*
8689 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008690 * Register class HTTP
8691 *
8692 */
8693
8694 /* Create and fill the metatable. */
8695 lua_newtable(gL.T);
8696
8697 /* Create and fille the __index entry. */
8698 lua_pushstring(gL.T, "__index");
8699 lua_newtable(gL.T);
8700
8701 /* Register Lua functions. */
8702 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8703 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8704 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8705 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8706 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8707 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8708 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8709 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8710 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8711 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8712
8713 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8714 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8715 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8716 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8717 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8718 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008719 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008720
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008721 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008722
8723 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008724 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008725
8726 /*
8727 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008728 * Register class AppletTCP
8729 *
8730 */
8731
8732 /* Create and fill the metatable. */
8733 lua_newtable(gL.T);
8734
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008735 /* Create and fille the __index entry. */
8736 lua_pushstring(gL.T, "__index");
8737 lua_newtable(gL.T);
8738
8739 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008740 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8741 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8742 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8743 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8744 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008745 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8746 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8747 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008748
8749 lua_settable(gL.T, -3);
8750
8751 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008752 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008753
8754 /*
8755 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008756 * Register class AppletHTTP
8757 *
8758 */
8759
8760 /* Create and fill the metatable. */
8761 lua_newtable(gL.T);
8762
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008763 /* Create and fille the __index entry. */
8764 lua_pushstring(gL.T, "__index");
8765 lua_newtable(gL.T);
8766
8767 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008768 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8769 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008770 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8771 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8772 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008773 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8774 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8775 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8776 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8777 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8778 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8779
8780 lua_settable(gL.T, -3);
8781
8782 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008783 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008784
8785 /*
8786 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008787 * Register class TXN
8788 *
8789 */
8790
8791 /* Create and fill the metatable. */
8792 lua_newtable(gL.T);
8793
8794 /* Create and fille the __index entry. */
8795 lua_pushstring(gL.T, "__index");
8796 lua_newtable(gL.T);
8797
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008798 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008799 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8800 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8801 hlua_class_function(gL.T, "set_var", hlua_set_var);
8802 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8803 hlua_class_function(gL.T, "get_var", hlua_get_var);
8804 hlua_class_function(gL.T, "done", hlua_txn_done);
8805 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8806 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8807 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8808 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8809 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8810 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8811 hlua_class_function(gL.T, "log", hlua_txn_log);
8812 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8813 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8814 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8815 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008816
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008817 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008818
8819 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008820 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008821
8822 /*
8823 *
8824 * Register class Socket
8825 *
8826 */
8827
8828 /* Create and fill the metatable. */
8829 lua_newtable(gL.T);
8830
8831 /* Create and fille the __index entry. */
8832 lua_pushstring(gL.T, "__index");
8833 lua_newtable(gL.T);
8834
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008835#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008836 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008837#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008838 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8839 hlua_class_function(gL.T, "send", hlua_socket_send);
8840 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8841 hlua_class_function(gL.T, "close", hlua_socket_close);
8842 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8843 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8844 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8845 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8846
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008847 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008848
8849 /* Register the garbage collector entry. */
8850 lua_pushstring(gL.T, "__gc");
8851 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008852 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008853
8854 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008855 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008856
8857 /* Proxy and server configuration initialisation. */
8858 memset(&socket_proxy, 0, sizeof(socket_proxy));
8859 init_new_proxy(&socket_proxy);
8860 socket_proxy.parent = NULL;
8861 socket_proxy.last_change = now.tv_sec;
8862 socket_proxy.id = "LUA-SOCKET";
8863 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8864 socket_proxy.maxconn = 0;
8865 socket_proxy.accept = NULL;
8866 socket_proxy.options2 |= PR_O2_INDEPSTR;
8867 socket_proxy.srv = NULL;
8868 socket_proxy.conn_retries = 0;
8869 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8870
8871 /* Init TCP server: unchanged parameters */
8872 memset(&socket_tcp, 0, sizeof(socket_tcp));
8873 socket_tcp.next = NULL;
8874 socket_tcp.proxy = &socket_proxy;
8875 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8876 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008877 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008878 socket_tcp.priv_conns = NULL;
8879 socket_tcp.idle_conns = NULL;
8880 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008881 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008882 socket_tcp.last_change = 0;
8883 socket_tcp.id = "LUA-TCP-CONN";
8884 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8885 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8886 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8887
8888 /* XXX: Copy default parameter from default server,
8889 * but the default server is not initialized.
8890 */
8891 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8892 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8893 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8894 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8895 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8896 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8897 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8898 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8899 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8900 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8901
8902 socket_tcp.check.status = HCHK_STATUS_INI;
8903 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8904 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8905 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8906 socket_tcp.check.server = &socket_tcp;
8907
8908 socket_tcp.agent.status = HCHK_STATUS_INI;
8909 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8910 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8911 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8912 socket_tcp.agent.server = &socket_tcp;
8913
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008914 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008915
8916#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008917 /* Init TCP server: unchanged parameters */
8918 memset(&socket_ssl, 0, sizeof(socket_ssl));
8919 socket_ssl.next = NULL;
8920 socket_ssl.proxy = &socket_proxy;
8921 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8922 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008923 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008924 socket_ssl.priv_conns = NULL;
8925 socket_ssl.idle_conns = NULL;
8926 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008927 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008928 socket_ssl.last_change = 0;
8929 socket_ssl.id = "LUA-SSL-CONN";
8930 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8931 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8932 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8933
8934 /* XXX: Copy default parameter from default server,
8935 * but the default server is not initialized.
8936 */
8937 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8938 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8939 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8940 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8941 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8942 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8943 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8944 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8945 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8946 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8947
8948 socket_ssl.check.status = HCHK_STATUS_INI;
8949 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8950 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8951 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8952 socket_ssl.check.server = &socket_ssl;
8953
8954 socket_ssl.agent.status = HCHK_STATUS_INI;
8955 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8956 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8957 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8958 socket_ssl.agent.server = &socket_ssl;
8959
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008960 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008961 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008962
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008963 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008964 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8965 /*
8966 *
8967 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008968 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008969 * features like client certificates and ssl_verify.
8970 *
8971 */
8972 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8973 if (tmp_error != 0) {
8974 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8975 abort(); /* This must be never arrives because the command line
8976 not editable by the user. */
8977 }
8978 idx += kw->skip;
8979 }
8980 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008981#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008982
8983 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008984}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008985
Willy Tarreau80713382018-11-26 10:19:54 +01008986static void hlua_register_build_options(void)
8987{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008988 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008989
Willy Tarreaubb57d942016-12-21 19:04:56 +01008990 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8991 hap_register_build_opts(ptr, 1);
8992}
Willy Tarreau80713382018-11-26 10:19:54 +01008993
8994INITCALL0(STG_REGISTER, hlua_register_build_options);