blob: d073bfc6ef5b48898337095bd919dbf410c51468 [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua unsafe core engine
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010013#include <ctype.h>
Mark Lakes56cc1252018-03-27 09:48:06 +020014#include <limits.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020015#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010016
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010017#include <lauxlib.h>
18#include <lua.h>
19#include <lualib.h>
20
Thierry FOURNIER463119c2015-03-10 00:35:36 +010021#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
22#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010023#endif
24
Thierry FOURNIER380d0932015-01-23 14:27:52 +010025#include <ebpttree.h>
26
27#include <common/cfgparse.h>
Willy Tarreaub059b892018-10-16 17:57:36 +020028#include <common/compiler.h>
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +020029#include <common/hathreads.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010030#include <common/initcall.h>
31#include <common/xref.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010032#include <common/h1.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010033
William Lallemand9ed62032016-11-21 17:49:11 +010034#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010035#include <types/hlua.h>
36#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010037#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010038
Thierry FOURNIER55da1652015-01-23 11:36:30 +010039#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010041#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010042#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010043#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010045#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010046#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010047#include <proto/hlua_fcn.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020048#include <proto/http_fetch.h>
Christopher Faulet724a12c2018-12-13 22:12:15 +010049#include <proto/http_htx.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020050#include <proto/http_rules.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020051#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010052#include <proto/obj_type.h>
Patrick Hemmer268a7072018-05-11 12:52:31 -040053#include <proto/queue.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010054#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010055#include <proto/payload.h>
56#include <proto/proto_http.h>
57#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010058#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020059#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020060#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010061#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010062#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010063#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020064#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010065
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010066/* Lua uses longjmp to perform yield or throwing errors. This
67 * macro is used only for identifying the function that can
68 * not return because a longjmp is executed.
69 * __LJMP marks a prototype of hlua file that can use longjmp.
70 * WILL_LJMP() marks an lua function that will use longjmp.
71 * MAY_LJMP() marks an lua function that may use longjmp.
72 */
73#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020074#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010075#define MAY_LJMP(func) func
76
Thierry FOURNIERbabae282015-09-17 11:36:37 +020077/* This couple of function executes securely some Lua calls outside of
78 * the lua runtime environment. Each Lua call can return a longjmp
79 * if it encounter a memory error.
80 *
81 * Lua documentation extract:
82 *
83 * If an error happens outside any protected environment, Lua calls
84 * a panic function (see lua_atpanic) and then calls abort, thus
85 * exiting the host application. Your panic function can avoid this
86 * exit by never returning (e.g., doing a long jump to your own
87 * recovery point outside Lua).
88 *
89 * The panic function runs as if it were a message handler (see
90 * §2.3); in particular, the error message is at the top of the
91 * stack. However, there is no guarantee about stack space. To push
92 * anything on the stack, the panic function must first check the
93 * available space (see §4.2).
94 *
95 * We must check all the Lua entry point. This includes:
96 * - The include/proto/hlua.h exported functions
97 * - the task wrapper function
98 * - The action wrapper function
99 * - The converters wrapper function
100 * - The sample-fetch wrapper functions
101 *
102 * It is tolerated that the initilisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800103 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200104 *
105 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
106 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
107 * because they must be exists in the program stack when the longjmp
108 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200109 *
110 * Note that the Lua processing is not really thread safe. It provides
111 * heavy system which consists to add our own lock function in the Lua
112 * code and recompile the library. This system will probably not accepted
113 * by maintainers of various distribs.
114 *
115 * Our main excution point of the Lua is the function lua_resume(). A
116 * quick looking on the Lua sources displays a lua_lock() a the start
117 * of function and a lua_unlock() at the end of the function. So I
118 * conclude that the Lua thread safe mode just perform a mutex around
119 * all execution. So I prefer to do this in the HAProxy code, it will be
120 * easier for distro maintainers.
121 *
122 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
123 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
124 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200125 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100126__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200127THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128static int hlua_panic_safe(lua_State *L) { return 0; }
129static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
130
131#define SET_SAFE_LJMP(__L) \
132 ({ \
133 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100134 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200135 if (setjmp(safe_ljmp_env) != 0) { \
136 lua_atpanic(__L, hlua_panic_safe); \
137 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100138 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200139 } else { \
140 lua_atpanic(__L, hlua_panic_ljmp); \
141 ret = 1; \
142 } \
143 ret; \
144 })
145
146/* If we are the last function catching Lua errors, we
147 * must reset the panic function.
148 */
149#define RESET_SAFE_LJMP(__L) \
150 do { \
151 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100152 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200153 } while(0)
154
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200155/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200156#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100157/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200158#define APPLET_HDR_SENT 0x04 /* Response header sent. */
159#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
160#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100161#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100162#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200163
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100164/* The main Lua execution context. */
165struct hlua gL;
166
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100167/* This is the memory pool containing struct lua for applets
168 * (including cli).
169 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100170DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100171
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100172/* Used for Socket connection. */
173static struct proxy socket_proxy;
174static struct server socket_tcp;
175#ifdef USE_OPENSSL
176static struct server socket_ssl;
177#endif
178
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100179/* List head of the function called at the initialisation time. */
180struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
181
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100182/* The following variables contains the reference of the different
183 * Lua classes. These references are useful for identify metadata
184 * associated with an object.
185 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100186static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100187static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100188static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100189static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100190static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100191static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200192static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200193static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200194static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100195
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100196/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200197 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100198 * short timeout. Lua linked with tasks doesn't have a timeout
199 * because a task may remain alive during all the haproxy execution.
200 */
201static unsigned int hlua_timeout_session = 4000; /* session timeout. */
202static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200203static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100204
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100205/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
206 * it is used for preventing infinite loops.
207 *
208 * I test the scheer with an infinite loop containing one incrementation
209 * and one test. I run this loop between 10 seconds, I raise a ceil of
210 * 710M loops from one interrupt each 9000 instructions, so I fix the value
211 * to one interrupt each 10 000 instructions.
212 *
213 * configured | Number of
214 * instructions | loops executed
215 * between two | in milions
216 * forced yields |
217 * ---------------+---------------
218 * 10 | 160
219 * 500 | 670
220 * 1000 | 680
221 * 5000 | 700
222 * 7000 | 700
223 * 8000 | 700
224 * 9000 | 710 <- ceil
225 * 10000 | 710
226 * 100000 | 710
227 * 1000000 | 710
228 *
229 */
230static unsigned int hlua_nb_instruction = 10000;
231
Willy Tarreau32f61e22015-03-18 17:54:59 +0100232/* Descriptor for the memory allocation state. If limit is not null, it will
233 * be enforced on any memory allocation.
234 */
235struct hlua_mem_allocator {
236 size_t allocated;
237 size_t limit;
238};
239
240static struct hlua_mem_allocator hlua_global_allocator;
241
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200242static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200243 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200244 "Cache-Control: no-cache\r\n"
245 "Connection: close\r\n"
246 "Content-Type: text/html\r\n"
247 "\r\n"
Joseph Herlant7fe15772018-11-15 10:07:32 -0800248 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200249
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100250/* These functions converts types between HAProxy internal args or
251 * sample and LUA types. Another function permits to check if the
252 * LUA stack contains arguments according with an required ARG_T
253 * format.
254 */
255static int hlua_arg2lua(lua_State *L, const struct arg *arg);
256static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100257__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100258 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100259static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100260static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100261static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
262
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200263__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
264
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200265#define SEND_ERR(__be, __fmt, __args...) \
266 do { \
267 send_log(__be, LOG_ERR, __fmt, ## __args); \
268 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100269 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200270 } while (0)
271
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100272/* Used to check an Lua function type in the stack. It creates and
273 * returns a reference of the function. This function throws an
274 * error if the rgument is not a "function".
275 */
276__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
277{
278 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100279 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100280 WILL_LJMP(luaL_argerror(L, argno, msg));
281 }
282 lua_pushvalue(L, argno);
283 return luaL_ref(L, LUA_REGISTRYINDEX);
284}
285
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200286/* Return the string that is of the top of the stack. */
287const char *hlua_get_top_error_string(lua_State *L)
288{
289 if (lua_gettop(L) < 1)
290 return "unknown error";
291 if (lua_type(L, -1) != LUA_TSTRING)
292 return "unknown error";
293 return lua_tostring(L, -1);
294}
295
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200296__LJMP static const char *hlua_traceback(lua_State *L)
297{
298 lua_Debug ar;
299 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200300 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200301 int filled = 0;
302
303 while (lua_getstack(L, level++, &ar)) {
304
305 /* Add separator */
306 if (filled)
307 chunk_appendf(msg, ", ");
308 filled = 1;
309
310 /* Fill fields:
311 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
312 * 'l': fills in the field currentline;
313 * 'n': fills in the field name and namewhat;
314 * 't': fills in the field istailcall;
315 */
316 lua_getinfo(L, "Slnt", &ar);
317
318 /* Append code localisation */
319 if (ar.currentline > 0)
320 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
321 else
322 chunk_appendf(msg, "%s ", ar.short_src);
323
324 /*
325 * Get function name
326 *
327 * if namewhat is no empty, name is defined.
328 * what contains "Lua" for Lua function, "C" for C function,
329 * or "main" for main code.
330 */
331 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
332 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
333
334 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
335 chunk_appendf(msg, "main chunk");
336
337 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
338 chunk_appendf(msg, "C function line %d", ar.linedefined);
339
340 else /* nothing left... */
341 chunk_appendf(msg, "?");
342
343
344 /* Display tailed call */
345 if (ar.istailcall)
346 chunk_appendf(msg, " ...");
347 }
348
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200349 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200350}
351
352
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100353/* This function check the number of arguments available in the
354 * stack. If the number of arguments available is not the same
355 * then <nb> an error is throwed.
356 */
357__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
358{
359 if (lua_gettop(L) == nb)
360 return;
361 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
362}
363
Mark Lakes22154b42018-01-29 14:38:40 -0800364/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100365 * and the line number where the error is encountered.
366 */
367static int hlua_pusherror(lua_State *L, const char *fmt, ...)
368{
369 va_list argp;
370 va_start(argp, fmt);
371 luaL_where(L, 1);
372 lua_pushvfstring(L, fmt, argp);
373 va_end(argp);
374 lua_concat(L, 2);
375 return 1;
376}
377
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100378/* This functions is used with sample fetch and converters. It
379 * converts the HAProxy configuration argument in a lua stack
380 * values.
381 *
382 * It takes an array of "arg", and each entry of the array is
383 * converted and pushed in the LUA stack.
384 */
385static int hlua_arg2lua(lua_State *L, const struct arg *arg)
386{
387 switch (arg->type) {
388 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100389 case ARGT_TIME:
390 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100391 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100392 break;
393
394 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200395 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100396 break;
397
398 case ARGT_IPV4:
399 case ARGT_IPV6:
400 case ARGT_MSK4:
401 case ARGT_MSK6:
402 case ARGT_FE:
403 case ARGT_BE:
404 case ARGT_TAB:
405 case ARGT_SRV:
406 case ARGT_USR:
407 case ARGT_MAP:
408 default:
409 lua_pushnil(L);
410 break;
411 }
412 return 1;
413}
414
415/* This function take one entrie in an LUA stack at the index "ud",
416 * and try to convert it in an HAProxy argument entry. This is useful
417 * with sample fetch wrappers. The input arguments are gived to the
418 * lua wrapper and converted as arg list by thi function.
419 */
420static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
421{
422 switch (lua_type(L, ud)) {
423
424 case LUA_TNUMBER:
425 case LUA_TBOOLEAN:
426 arg->type = ARGT_SINT;
427 arg->data.sint = lua_tointeger(L, ud);
428 break;
429
430 case LUA_TSTRING:
431 arg->type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200432 arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data);
Tim Duesterhusf371b832019-09-29 23:03:07 +0200433 /* We don't know the actual size of the underlying allocation, so be conservative. */
434 arg->data.str.size = arg->data.str.data;
435 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100436 break;
437
438 case LUA_TUSERDATA:
439 case LUA_TNIL:
440 case LUA_TTABLE:
441 case LUA_TFUNCTION:
442 case LUA_TTHREAD:
443 case LUA_TLIGHTUSERDATA:
444 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200445 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 break;
447 }
448 return 1;
449}
450
451/* the following functions are used to convert a struct sample
452 * in Lua type. This useful to convert the return of the
453 * fetchs or converters.
454 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100455static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100456{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200457 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100458 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100459 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200460 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100461 break;
462
463 case SMP_T_BIN:
464 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200465 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100466 break;
467
468 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200469 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100470 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
471 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
472 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
473 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
474 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
475 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
476 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
477 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
478 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100480 break;
481 default:
482 lua_pushnil(L);
483 break;
484 }
485 break;
486
487 case SMP_T_IPV4:
488 case SMP_T_IPV6:
489 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200490 if (sample_casts[smp->data.type][SMP_T_STR] &&
491 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200492 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100493 else
494 lua_pushnil(L);
495 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100496 default:
497 lua_pushnil(L);
498 break;
499 }
500 return 1;
501}
502
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100503/* the following functions are used to convert a struct sample
504 * in Lua strings. This is useful to convert the return of the
505 * fetchs or converters.
506 */
507static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
508{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200509 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100510
511 case SMP_T_BIN:
512 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200513 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100514 break;
515
516 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200517 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100518 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
519 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
520 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
521 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
522 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
523 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
524 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
525 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
526 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200527 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100528 break;
529 default:
530 lua_pushstring(L, "");
531 break;
532 }
533 break;
534
535 case SMP_T_SINT:
536 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100537 case SMP_T_IPV4:
538 case SMP_T_IPV6:
539 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200540 if (sample_casts[smp->data.type][SMP_T_STR] &&
541 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200542 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100543 else
544 lua_pushstring(L, "");
545 break;
546 default:
547 lua_pushstring(L, "");
548 break;
549 }
550 return 1;
551}
552
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100553/* the following functions are used to convert an Lua type in a
554 * struct sample. This is useful to provide data from a converter
555 * to the LUA code.
556 */
557static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
558{
559 switch (lua_type(L, ud)) {
560
561 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200562 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200563 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100564 break;
565
566
567 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200568 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200569 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 break;
571
572 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200573 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100574 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200575 smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data);
Tim Duesterhusf371b832019-09-29 23:03:07 +0200576 /* We don't know the actual size of the underlying allocation, so be conservative. */
577 smp->data.u.str.size = smp->data.u.str.data;
578 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100579 break;
580
581 case LUA_TUSERDATA:
582 case LUA_TNIL:
583 case LUA_TTABLE:
584 case LUA_TFUNCTION:
585 case LUA_TTHREAD:
586 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200587 case LUA_TNONE:
588 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200589 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200590 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100591 break;
592 }
593 return 1;
594}
595
596/* This function check the "argp" builded by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800597 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100598 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100599 *
600 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
601 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100602 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100603__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100604 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100605{
606 int min_arg;
607 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100608 struct proxy *px;
609 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100610
611 idx = 0;
612 min_arg = ARGM(mask);
613 mask >>= ARGM_BITS;
614
615 while (1) {
616
617 /* Check oversize. */
618 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100619 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100620 }
621
622 /* Check for mandatory arguments. */
623 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100624 if (idx < min_arg) {
625
626 /* If miss other argument than the first one, we return an error. */
627 if (idx > 0)
628 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
629
630 /* If first argument have a certain type, some default values
631 * may be used. See the function smp_resolve_args().
632 */
633 switch (mask & ARGT_MASK) {
634
635 case ARGT_FE:
636 if (!(p->cap & PR_CAP_FE))
637 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
638 argp[idx].data.prx = p;
639 argp[idx].type = ARGT_FE;
640 argp[idx+1].type = ARGT_STOP;
641 break;
642
643 case ARGT_BE:
644 if (!(p->cap & PR_CAP_BE))
645 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
646 argp[idx].data.prx = p;
647 argp[idx].type = ARGT_BE;
648 argp[idx+1].type = ARGT_STOP;
649 break;
650
651 case ARGT_TAB:
652 argp[idx].data.prx = p;
653 argp[idx].type = ARGT_TAB;
654 argp[idx+1].type = ARGT_STOP;
655 break;
656
657 default:
658 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
659 break;
660 }
661 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100662 return 0;
663 }
664
665 /* Check for exceed the number of requiered argument. */
666 if ((mask & ARGT_MASK) == ARGT_STOP &&
667 argp[idx].type != ARGT_STOP) {
668 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
669 }
670
671 if ((mask & ARGT_MASK) == ARGT_STOP &&
672 argp[idx].type == ARGT_STOP) {
673 return 0;
674 }
675
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100676 /* Convert some argument types. */
677 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100678 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100679 if (argp[idx].type != ARGT_SINT)
680 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
681 argp[idx].type = ARGT_SINT;
682 break;
683
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100684 case ARGT_TIME:
685 if (argp[idx].type != ARGT_SINT)
686 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200687 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100688 break;
689
690 case ARGT_SIZE:
691 if (argp[idx].type != ARGT_SINT)
692 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200693 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100694 break;
695
696 case ARGT_FE:
697 if (argp[idx].type != ARGT_STR)
698 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200699 memcpy(trash.area, argp[idx].data.str.area,
700 argp[idx].data.str.data);
701 trash.area[argp[idx].data.str.data] = 0;
702 argp[idx].data.prx = proxy_fe_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100703 if (!argp[idx].data.prx)
704 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
705 argp[idx].type = ARGT_FE;
706 break;
707
708 case ARGT_BE:
709 if (argp[idx].type != ARGT_STR)
710 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200711 memcpy(trash.area, argp[idx].data.str.area,
712 argp[idx].data.str.data);
713 trash.area[argp[idx].data.str.data] = 0;
714 argp[idx].data.prx = proxy_be_by_name(trash.area);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100715 if (!argp[idx].data.prx)
716 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
717 argp[idx].type = ARGT_BE;
718 break;
719
720 case ARGT_TAB:
721 if (argp[idx].type != ARGT_STR)
722 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200723 memcpy(trash.area, argp[idx].data.str.area,
724 argp[idx].data.str.data);
725 trash.area[argp[idx].data.str.data] = 0;
Tim Duesterhuse351e762019-09-29 23:03:09 +0200726 argp[idx].data.t = stktable_find_by_name(trash.area);
727 if (!argp[idx].data.t)
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100728 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
729 argp[idx].type = ARGT_TAB;
730 break;
731
732 case ARGT_SRV:
733 if (argp[idx].type != ARGT_STR)
734 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200735 memcpy(trash.area, argp[idx].data.str.area,
736 argp[idx].data.str.data);
737 trash.area[argp[idx].data.str.data] = 0;
738 sname = strrchr(trash.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100739 if (sname) {
740 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200741 pname = trash.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200742 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100743 if (!px)
744 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
745 }
746 else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200747 sname = trash.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100748 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100749 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100750 argp[idx].data.srv = findserver(px, sname);
751 if (!argp[idx].data.srv)
752 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
753 argp[idx].type = ARGT_SRV;
754 break;
755
756 case ARGT_IPV4:
Christopher Faulete7f1fcf2020-08-07 09:07:26 +0200757 if (argp[idx].type != ARGT_STR)
758 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 memcpy(trash.area, argp[idx].data.str.area,
760 argp[idx].data.str.data);
761 trash.area[argp[idx].data.str.data] = 0;
762 if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100763 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
764 argp[idx].type = ARGT_IPV4;
765 break;
766
767 case ARGT_MSK4:
Christopher Fauletff8de2c2020-08-07 09:11:22 +0200768 if (argp[idx].type == ARGT_SINT)
769 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
770 else if (argp[idx].type == ARGT_STR) {
771 memcpy(trash.area, argp[idx].data.str.area,
772 argp[idx].data.str.data);
773 trash.area[argp[idx].data.str.data] = 0;
774 if (!str2mask(trash.area, &argp[idx].data.ipv4))
775 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
776 }
777 else
778 WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected"));
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100779 argp[idx].type = ARGT_MSK4;
780 break;
781
782 case ARGT_IPV6:
Christopher Faulete7f1fcf2020-08-07 09:07:26 +0200783 if (argp[idx].type != ARGT_STR)
784 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200785 memcpy(trash.area, argp[idx].data.str.area,
786 argp[idx].data.str.data);
787 trash.area[argp[idx].data.str.data] = 0;
788 if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6))
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100789 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
790 argp[idx].type = ARGT_IPV6;
791 break;
792
793 case ARGT_MSK6:
Christopher Fauletff8de2c2020-08-07 09:11:22 +0200794 if (argp[idx].type == ARGT_SINT)
795 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
796 else if (argp[idx].type == ARGT_STR) {
797 memcpy(trash.area, argp[idx].data.str.area,
798 argp[idx].data.str.data);
799 trash.area[argp[idx].data.str.data] = 0;
800 if (!str2mask6(trash.area, &argp[idx].data.ipv6))
801 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
802 }
803 else
804 WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected"));
Tim Duesterhusb814da62018-01-25 16:24:50 +0100805 argp[idx].type = ARGT_MSK6;
806 break;
807
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100808 case ARGT_MAP:
809 case ARGT_REG:
810 case ARGT_USR:
811 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100812 break;
813 }
814
815 /* Check for type of argument. */
816 if ((mask & ARGT_MASK) != argp[idx].type) {
817 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
818 arg_type_names[(mask & ARGT_MASK)],
819 arg_type_names[argp[idx].type & ARGT_MASK]);
820 WILL_LJMP(luaL_argerror(L, first + idx, msg));
821 }
822
823 /* Next argument. */
824 mask >>= ARGT_BITS;
825 idx++;
826 }
827}
828
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100829/*
830 * The following functions are used to make correspondance between the the
831 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100832 *
833 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100834 * - hlua_sethlua : create the association between hlua context and lua_state.
835 */
836static inline struct hlua *hlua_gethlua(lua_State *L)
837{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100838 struct hlua **hlua = lua_getextraspace(L);
839 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100840}
841static inline void hlua_sethlua(struct hlua *hlua)
842{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100843 struct hlua **hlua_store = lua_getextraspace(hlua->T);
844 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100845}
846
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100847/* This function is used to send logs. It try to send on screen (stderr)
848 * and on the default syslog server.
849 */
850static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
851{
852 struct tm tm;
853 char *p;
854
855 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200856 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100857 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200858 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200859 /* Break the message if exceed the buffer size. */
860 *(p-4) = ' ';
861 *(p-3) = '.';
862 *(p-2) = '.';
863 *(p-1) = '.';
864 break;
865 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100866 if (isprint(*msg))
867 *p = *msg;
868 else
869 *p = '.';
870 }
871 *p = '\0';
872
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200873 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100874 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletb290edf2020-10-02 18:13:52 +0200875 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
876 return;
877
Willy Tarreaua678b432015-08-28 10:14:59 +0200878 get_localtime(date.tv_sec, &tm);
879 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100880 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200881 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100882 fflush(stderr);
883 }
884}
885
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100886/* This function just ensure that the yield will be always
887 * returned with a timeout and permit to set some flags
888 */
889__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100890 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100891{
892 struct hlua *hlua = hlua_gethlua(L);
893
894 /* Set the wake timeout. If timeout is required, we set
895 * the expiration time.
896 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200897 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100898
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100899 hlua->flags |= flags;
900
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100901 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +0200902 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100903}
904
Willy Tarreau87b09662015-04-03 00:22:06 +0200905/* This function initialises the Lua environment stored in the stream.
906 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200908 *
909 * This function is particular. it initialises a new Lua thread. If the
910 * initialisation fails (example: out of memory error), the lua function
911 * throws an error (longjmp).
912 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100913 * In some case (at least one), this function can be called from safe
914 * environement, so we must not initialise it. While the support of
915 * threads appear, the safe environment set a lock to ensure only one
916 * Lua execution at a time. If we initialize safe environment in another
917 * safe environmenet, we have a dead lock.
918 *
919 * set "already_safe" true if the context is initialized form safe
920 * Lua fonction.
921 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800922 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200923 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800924 * MUST NOT manipulate the created thread stack state, because it is not
925 * proctected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100926 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100927int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100928{
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100929 if (!already_safe) {
930 if (!SET_SAFE_LJMP(gL.T)) {
931 lua->Tref = LUA_REFNIL;
932 return 0;
933 }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200934 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100935 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100936 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100937 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100938 lua->T = lua_newthread(gL.T);
939 if (!lua->T) {
940 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100941 if (!already_safe)
942 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100943 return 0;
944 }
945 hlua_sethlua(lua);
946 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
947 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +0100948 if (!already_safe)
949 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100950 return 1;
951}
952
Willy Tarreau87b09662015-04-03 00:22:06 +0200953/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100954 * is destroyed. The destroy also the memory context. The struct "lua"
955 * is not freed.
956 */
957void hlua_ctx_destroy(struct hlua *lua)
958{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100959 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100960 return;
961
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100962 if (!lua->T)
963 goto end;
964
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100965 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200966 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100967
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200968 if (!SET_SAFE_LJMP(lua->T))
969 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100970 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200971 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200972
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200973 if (!SET_SAFE_LJMP(gL.T))
974 return;
975 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
976 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200977 /* Forces a garbage collecting process. If the Lua program is finished
978 * without error, we run the GC on the thread pointer. Its freed all
979 * the unused memory.
980 * If the thread is finnish with an error or is currently yielded,
981 * it seems that the GC applied on the thread doesn't clean anything,
982 * so e run the GC on the main thread.
983 * NOTE: maybe this action locks all the Lua threads untiml the en of
984 * the garbage collection.
985 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200986 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200987 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200988 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200989 lua_gc(gL.T, LUA_GCCOLLECT, 0);
990 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200991 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200992
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200993 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100994
995end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100996 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100997}
998
999/* This function is used to restore the Lua context when a coroutine
1000 * fails. This function copy the common memory between old coroutine
1001 * and the new coroutine. The old coroutine is destroyed, and its
1002 * replaced by the new coroutine.
1003 * If the flag "keep_msg" is set, the last entry of the old is assumed
1004 * as string error message and it is copied in the new stack.
1005 */
1006static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1007{
1008 lua_State *T;
1009 int new_ref;
1010
1011 /* Renew the main LUA stack doesn't have sense. */
1012 if (lua == &gL)
1013 return 0;
1014
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001015 /* New Lua coroutine. */
1016 T = lua_newthread(gL.T);
1017 if (!T)
1018 return 0;
1019
1020 /* Copy last error message. */
1021 if (keep_msg)
1022 lua_xmove(lua->T, T, 1);
1023
1024 /* Copy data between the coroutines. */
1025 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1026 lua_xmove(lua->T, T, 1);
1027 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
1028
1029 /* Destroy old data. */
1030 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1031
1032 /* The thread is garbage collected by Lua. */
1033 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
1034
1035 /* Fill the struct with the new coroutine values. */
1036 lua->Mref = new_ref;
1037 lua->T = T;
1038 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
1039
1040 /* Set context. */
1041 hlua_sethlua(lua);
1042
1043 return 1;
1044}
1045
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001046void hlua_hook(lua_State *L, lua_Debug *ar)
1047{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001048 struct hlua *hlua = hlua_gethlua(L);
1049
1050 /* Lua cannot yield when its returning from a function,
1051 * so, we can fix the interrupt hook to 1 instruction,
1052 * expecting that the function is finnished.
1053 */
1054 if (lua_gethookmask(L) & LUA_MASKRET) {
1055 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1056 return;
1057 }
1058
1059 /* restore the interrupt condition. */
1060 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1061
1062 /* If we interrupt the Lua processing in yieldable state, we yield.
1063 * If the state is not yieldable, trying yield causes an error.
1064 */
1065 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001066 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001067
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001068 /* If we cannot yield, update the clock and check the timeout. */
1069 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001070 hlua->run_time += now_ms - hlua->start_time;
1071 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001072 lua_pushfstring(L, "execution timeout");
1073 WILL_LJMP(lua_error(L));
1074 }
1075
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001076 /* Update the start time. */
1077 hlua->start_time = now_ms;
1078
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001079 /* Try to interrupt the process at the end of the current
1080 * unyieldable function.
1081 */
1082 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001083}
1084
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001085/* This function start or resumes the Lua stack execution. If the flag
1086 * "yield_allowed" if no set and the LUA stack execution returns a yield
1087 * The function return an error.
1088 *
1089 * The function can returns 4 values:
1090 * - HLUA_E_OK : The execution is terminated without any errors.
1091 * - HLUA_E_AGAIN : The execution must continue at the next associated
1092 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001093 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001094 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001095 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001096 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001097 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001098 * LUA code.
1099 */
1100static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1101{
Christopher Faulet20699902020-07-28 10:33:25 +02001102#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1103 int nres;
1104#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001105 int ret;
1106 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001107 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001108
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001109 /* Initialise run time counter. */
1110 if (!HLUA_IS_RUNNING(lua))
1111 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001112
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001113 /* Lock the whole Lua execution. This lock must be before the
1114 * label "resume_execution".
1115 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001116 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001117
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001118resume_execution:
1119
1120 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1121 * instructions. it is used for preventing infinite loops.
1122 */
1123 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1124
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001125 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001126 HLUA_SET_RUN(lua);
1127 HLUA_CLR_CTRLYIELD(lua);
1128 HLUA_CLR_WAKERESWR(lua);
1129 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001130
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001131 /* Update the start time. */
1132 lua->start_time = now_ms;
1133
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001134 /* Call the function. */
Christopher Faulet20699902020-07-28 10:33:25 +02001135#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1136 ret = lua_resume(lua->T, gL.T, lua->nargs, &nres);
1137#else
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001138 ret = lua_resume(lua->T, gL.T, lua->nargs);
Christopher Faulet20699902020-07-28 10:33:25 +02001139#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001140 switch (ret) {
1141
1142 case LUA_OK:
1143 ret = HLUA_E_OK;
1144 break;
1145
1146 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001147 /* Check if the execution timeout is expired. It it is the case, we
1148 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001149 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001150 tv_update_date(0, 1);
1151 lua->run_time += now_ms - lua->start_time;
1152 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001153 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001154 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001155 break;
1156 }
1157 /* Process the forced yield. if the general yield is not allowed or
1158 * if no task were associated this the current Lua execution
1159 * coroutine, we resume the execution. Else we want to return in the
1160 * scheduler and we want to be waked up again, to continue the
1161 * current Lua execution. So we schedule our own task.
1162 */
1163 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001164 if (!yield_allowed || !lua->task)
1165 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001166 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001167 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001168 if (!yield_allowed) {
1169 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001170 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001171 break;
1172 }
1173 ret = HLUA_E_AGAIN;
1174 break;
1175
1176 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001177
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001178 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001179 * because the errors ares the only one mean to return immediately
1180 * from and lua execution.
1181 */
1182 if (lua->flags & HLUA_EXIT) {
1183 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001184 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001185 break;
1186 }
1187
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001188 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001189 if (!lua_checkstack(lua->T, 1)) {
1190 ret = HLUA_E_ERR;
1191 break;
1192 }
1193 msg = lua_tostring(lua->T, -1);
1194 lua_settop(lua->T, 0); /* Empty the stack. */
1195 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001196 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001197 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001198 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001199 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001200 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001201 ret = HLUA_E_ERRMSG;
1202 break;
1203
1204 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001205 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001206 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001207 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001208 break;
1209
1210 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001211 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001212 if (!lua_checkstack(lua->T, 1)) {
1213 ret = HLUA_E_ERR;
1214 break;
1215 }
1216 msg = lua_tostring(lua->T, -1);
1217 lua_settop(lua->T, 0); /* Empty the stack. */
1218 lua_pop(lua->T, 1);
1219 if (msg)
1220 lua_pushfstring(lua->T, "message handler error: %s", msg);
1221 else
1222 lua_pushfstring(lua->T, "message handler error");
1223 ret = HLUA_E_ERRMSG;
1224 break;
1225
1226 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001227 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001228 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001229 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001230 break;
1231 }
1232
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001233 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001234 if (lua->flags & HLUA_MUST_GC &&
1235 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001236 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1237
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001238 switch (ret) {
1239 case HLUA_E_AGAIN:
1240 break;
1241
1242 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001243 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001244 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001245 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001246 break;
1247
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001248 case HLUA_E_ETMOUT:
1249 case HLUA_E_NOMEM:
1250 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001251 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001252 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001253 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001254 hlua_ctx_renew(lua, 0);
1255 break;
1256
1257 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001258 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001259 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001260 break;
1261 }
1262
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001263 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001264 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001265
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001266 return ret;
1267}
1268
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001269/* This function exit the current code. */
1270__LJMP static int hlua_done(lua_State *L)
1271{
1272 struct hlua *hlua = hlua_gethlua(L);
1273
1274 hlua->flags |= HLUA_EXIT;
1275 WILL_LJMP(lua_error(L));
1276
1277 return 0;
1278}
1279
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001280/* This function is an LUA binding. It provides a function
1281 * for deleting ACL from a referenced ACL file.
1282 */
1283__LJMP static int hlua_del_acl(lua_State *L)
1284{
1285 const char *name;
1286 const char *key;
1287 struct pat_ref *ref;
1288
1289 MAY_LJMP(check_args(L, 2, "del_acl"));
1290
1291 name = MAY_LJMP(luaL_checkstring(L, 1));
1292 key = MAY_LJMP(luaL_checkstring(L, 2));
1293
1294 ref = pat_ref_lookup(name);
1295 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001296 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001297
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001298 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001299 pat_ref_delete(ref, key);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001300 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001301 return 0;
1302}
1303
1304/* This function is an LUA binding. It provides a function
1305 * for deleting map entry from a referenced map file.
1306 */
1307static int hlua_del_map(lua_State *L)
1308{
1309 const char *name;
1310 const char *key;
1311 struct pat_ref *ref;
1312
1313 MAY_LJMP(check_args(L, 2, "del_map"));
1314
1315 name = MAY_LJMP(luaL_checkstring(L, 1));
1316 key = MAY_LJMP(luaL_checkstring(L, 2));
1317
1318 ref = pat_ref_lookup(name);
1319 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001320 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001321
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001322 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001323 pat_ref_delete(ref, key);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001324 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001325 return 0;
1326}
1327
1328/* This function is an LUA binding. It provides a function
1329 * for adding ACL pattern from a referenced ACL file.
1330 */
1331static int hlua_add_acl(lua_State *L)
1332{
1333 const char *name;
1334 const char *key;
1335 struct pat_ref *ref;
1336
1337 MAY_LJMP(check_args(L, 2, "add_acl"));
1338
1339 name = MAY_LJMP(luaL_checkstring(L, 1));
1340 key = MAY_LJMP(luaL_checkstring(L, 2));
1341
1342 ref = pat_ref_lookup(name);
1343 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001344 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001345
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001346 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001347 if (pat_ref_find_elt(ref, key) == NULL)
1348 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001349 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001350 return 0;
1351}
1352
1353/* This function is an LUA binding. It provides a function
1354 * for setting map pattern and sample from a referenced map
1355 * file.
1356 */
1357static int hlua_set_map(lua_State *L)
1358{
1359 const char *name;
1360 const char *key;
1361 const char *value;
1362 struct pat_ref *ref;
1363
1364 MAY_LJMP(check_args(L, 3, "set_map"));
1365
1366 name = MAY_LJMP(luaL_checkstring(L, 1));
1367 key = MAY_LJMP(luaL_checkstring(L, 2));
1368 value = MAY_LJMP(luaL_checkstring(L, 3));
1369
1370 ref = pat_ref_lookup(name);
1371 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001372 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001373
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001374 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001375 if (pat_ref_find_elt(ref, key) != NULL)
1376 pat_ref_set(ref, key, value, NULL);
1377 else
1378 pat_ref_add(ref, key, value, NULL);
Christopher Faulet9fa1f4e2020-06-03 18:39:16 +02001379 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001380 return 0;
1381}
1382
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001383/* A class is a lot of memory that contain data. This data can be a table,
1384 * an integer or user data. This data is associated with a metatable. This
1385 * metatable have an original version registred in the global context with
1386 * the name of the object (_G[<name>] = <metable> ).
1387 *
1388 * A metable is a table that modify the standard behavior of a standard
1389 * access to the associated data. The entries of this new metatable are
1390 * defined as is:
1391 *
1392 * http://lua-users.org/wiki/MetatableEvents
1393 *
1394 * __index
1395 *
1396 * we access an absent field in a table, the result is nil. This is
1397 * true, but it is not the whole truth. Actually, such access triggers
1398 * the interpreter to look for an __index metamethod: If there is no
1399 * such method, as usually happens, then the access results in nil;
1400 * otherwise, the metamethod will provide the result.
1401 *
1402 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1403 * the key does not appear in the table, but the metatable has an __index
1404 * property:
1405 *
1406 * - if the value is a function, the function is called, passing in the
1407 * table and the key; the return value of that function is returned as
1408 * the result.
1409 *
1410 * - if the value is another table, the value of the key in that table is
1411 * asked for and returned (and if it doesn't exist in that table, but that
1412 * table's metatable has an __index property, then it continues on up)
1413 *
1414 * - Use "rawget(myTable,key)" to skip this metamethod.
1415 *
1416 * http://www.lua.org/pil/13.4.1.html
1417 *
1418 * __newindex
1419 *
1420 * Like __index, but control property assignment.
1421 *
1422 * __mode - Control weak references. A string value with one or both
1423 * of the characters 'k' and 'v' which specifies that the the
1424 * keys and/or values in the table are weak references.
1425 *
1426 * __call - Treat a table like a function. When a table is followed by
1427 * parenthesis such as "myTable( 'foo' )" and the metatable has
1428 * a __call key pointing to a function, that function is invoked
1429 * (passing any specified arguments) and the return value is
1430 * returned.
1431 *
1432 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1433 * called, if the metatable for myTable has a __metatable
1434 * key, the value of that key is returned instead of the
1435 * actual metatable.
1436 *
1437 * __tostring - Control string representation. When the builtin
1438 * "tostring( myTable )" function is called, if the metatable
1439 * for myTable has a __tostring property set to a function,
1440 * that function is invoked (passing myTable to it) and the
1441 * return value is used as the string representation.
1442 *
1443 * __len - Control table length. When the table length is requested using
1444 * the length operator ( '#' ), if the metatable for myTable has
1445 * a __len key pointing to a function, that function is invoked
1446 * (passing myTable to it) and the return value used as the value
1447 * of "#myTable".
1448 *
1449 * __gc - Userdata finalizer code. When userdata is set to be garbage
1450 * collected, if the metatable has a __gc field pointing to a
1451 * function, that function is first invoked, passing the userdata
1452 * to it. The __gc metamethod is not called for tables.
1453 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1454 *
1455 * Special metamethods for redefining standard operators:
1456 * http://www.lua.org/pil/13.1.html
1457 *
1458 * __add "+"
1459 * __sub "-"
1460 * __mul "*"
1461 * __div "/"
1462 * __unm "!"
1463 * __pow "^"
1464 * __concat ".."
1465 *
1466 * Special methods for redfining standar relations
1467 * http://www.lua.org/pil/13.2.html
1468 *
1469 * __eq "=="
1470 * __lt "<"
1471 * __le "<="
1472 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001473
1474/*
1475 *
1476 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001477 * Class Map
1478 *
1479 *
1480 */
1481
1482/* Returns a struct hlua_map if the stack entry "ud" is
1483 * a class session, otherwise it throws an error.
1484 */
1485__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1486{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001487 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001488}
1489
1490/* This function is the map constructor. It don't need
1491 * the class Map object. It creates and return a new Map
1492 * object. It must be called only during "body" or "init"
1493 * context because it process some filesystem accesses.
1494 */
1495__LJMP static int hlua_map_new(struct lua_State *L)
1496{
1497 const char *fn;
1498 int match = PAT_MATCH_STR;
1499 struct sample_conv conv;
1500 const char *file = "";
1501 int line = 0;
1502 lua_Debug ar;
1503 char *err = NULL;
1504 struct arg args[2];
1505
1506 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1507 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1508
1509 fn = MAY_LJMP(luaL_checkstring(L, 1));
1510
1511 if (lua_gettop(L) >= 2) {
1512 match = MAY_LJMP(luaL_checkinteger(L, 2));
1513 if (match < 0 || match >= PAT_MATCH_NUM)
1514 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1515 }
1516
1517 /* Get Lua filename and line number. */
1518 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1519 lua_getinfo(L, "Sl", &ar); /* get info about it */
1520 if (ar.currentline > 0) { /* is there info? */
1521 file = ar.short_src;
1522 line = ar.currentline;
1523 }
1524 }
1525
1526 /* fill fake sample_conv struct. */
1527 conv.kw = ""; /* unused. */
1528 conv.process = NULL; /* unused. */
1529 conv.arg_mask = 0; /* unused. */
1530 conv.val_args = NULL; /* unused. */
1531 conv.out_type = SMP_T_STR;
1532 conv.private = (void *)(long)match;
1533 switch (match) {
1534 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1535 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1536 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1537 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1538 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1539 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1540 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001541 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001542 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1543 default:
1544 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1545 }
1546
1547 /* fill fake args. */
1548 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001549 args[0].data.str.area = (char *)fn;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001550 args[1].type = ARGT_STOP;
1551
1552 /* load the map. */
1553 if (!sample_load_map(args, &conv, file, line, &err)) {
1554 /* error case: we cant use luaL_error because we must
1555 * free the err variable.
1556 */
1557 luaL_where(L, 1);
1558 lua_pushfstring(L, "'new': %s.", err);
1559 lua_concat(L, 2);
1560 free(err);
1561 WILL_LJMP(lua_error(L));
1562 }
1563
1564 /* create the lua object. */
1565 lua_newtable(L);
1566 lua_pushlightuserdata(L, args[0].data.map);
1567 lua_rawseti(L, -2, 0);
1568
1569 /* Pop a class Map metatable and affect it to the userdata. */
1570 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1571 lua_setmetatable(L, -2);
1572
1573
1574 return 1;
1575}
1576
1577__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1578{
1579 struct map_descriptor *desc;
1580 struct pattern *pat;
1581 struct sample smp;
1582
1583 MAY_LJMP(check_args(L, 2, "lookup"));
1584 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001585 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001586 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001587 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001588 }
1589 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001590 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001591 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001592 smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001593 }
1594
1595 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001596 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001597 if (str)
1598 lua_pushstring(L, "");
1599 else
1600 lua_pushnil(L);
1601 return 1;
1602 }
1603
1604 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001605 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001606 return 1;
1607}
1608
1609__LJMP static int hlua_map_lookup(struct lua_State *L)
1610{
1611 return _hlua_map_lookup(L, 0);
1612}
1613
1614__LJMP static int hlua_map_slookup(struct lua_State *L)
1615{
1616 return _hlua_map_lookup(L, 1);
1617}
1618
1619/*
1620 *
1621 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001622 * Class Socket
1623 *
1624 *
1625 */
1626
1627__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1628{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001629 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001630}
1631
1632/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001633 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001634 * received.
1635 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001636static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001637{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001638 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001639 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001640
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001641 if (appctx->ctx.hlua_cosocket.die) {
1642 si_shutw(si);
1643 si_shutr(si);
1644 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001645 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1646 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001647 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1648 }
1649
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001650 /* If the connection object is not available, close all the
1651 * streams and wakeup everything waiting for.
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001652 */
1653 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001654 si_shutw(si);
1655 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001656 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001657 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1658 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001659 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001660 }
1661
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001662 /* If we cant write, wakeup the pending write signals. */
1663 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001664 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001665
1666 /* If we cant read, wakeup the pending read signals. */
1667 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001668 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001669
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001670 /* if the connection is not estabkished, inform the stream that we want
1671 * to be notified whenever the connection completes.
1672 */
1673 if (!(c->flags & CO_FL_CONNECTED)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001674 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001675 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001676 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001677 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001678 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001679
1680 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001681 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001682
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001683 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001684 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001685 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001686
1687 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001688 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001689 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001690
1691 /* Some data were injected in the buffer, notify the stream
1692 * interface.
1693 */
1694 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001695 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001696
1697 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001698 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001699 */
1700 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001701 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001702}
1703
Willy Tarreau87b09662015-04-03 00:22:06 +02001704/* This function is called when the "struct stream" is destroyed.
1705 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001706 * Wake all the pending signals.
1707 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001708static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001709{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001710 struct xref *peer;
1711
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001712 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001713 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1714 if (peer)
1715 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001716
1717 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001718 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1719 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001720}
1721
1722/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001723 * uses this object. If the stream does not exists, just quit.
1724 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001725 * pending signal can rest in the read and write lists. destroy
1726 * it.
1727 */
1728__LJMP static int hlua_socket_gc(lua_State *L)
1729{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001730 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001731 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001732 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001733
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001734 MAY_LJMP(check_args(L, 1, "__gc"));
1735
1736 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001737 peer = xref_get_peer_and_lock(&socket->xref);
1738 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001739 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001740 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001741
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001742 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001743 appctx->ctx.hlua_cosocket.die = 1;
1744 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001745
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001746 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001747 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001748 return 0;
1749}
1750
1751/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001752 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001753 */
sada05ed3302018-05-11 11:48:18 -07001754__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001755{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001756 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001757 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001758 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001759
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001760 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001761
1762 /* Check if we run on the same thread than the xreator thread.
1763 * We cannot access to the socket if the thread is different.
1764 */
1765 if (socket->tid != tid)
1766 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1767
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001768 peer = xref_get_peer_and_lock(&socket->xref);
1769 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001770 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001771 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001772
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001773 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001774 appctx->ctx.hlua_cosocket.die = 1;
1775 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001776
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001777 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001778 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001779 return 0;
1780}
1781
sada05ed3302018-05-11 11:48:18 -07001782/* The close function calls close_helper.
1783 */
1784__LJMP static int hlua_socket_close(lua_State *L)
1785{
1786 MAY_LJMP(check_args(L, 1, "close"));
1787 return hlua_socket_close_helper(L);
1788}
1789
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001790/* This Lua function assumes that the stack contain three parameters.
1791 * 1 - USERDATA containing a struct socket
1792 * 2 - INTEGER with values of the macro defined below
1793 * If the integer is -1, we must read at most one line.
1794 * If the integer is -2, we ust read all the data until the
1795 * end of the stream.
1796 * If the integer is positive value, we must read a number of
1797 * bytes corresponding to this value.
1798 */
1799#define HLSR_READ_LINE (-1)
1800#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001801__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001802{
1803 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1804 int wanted = lua_tointeger(L, 2);
1805 struct hlua *hlua = hlua_gethlua(L);
1806 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001807 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001808 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001809 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001810 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001811 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001812 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001813 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001814 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001815 struct stream_interface *si;
1816 struct stream *s;
1817 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001818 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001819
1820 /* Check if this lua stack is schedulable. */
1821 if (!hlua || !hlua->task)
1822 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1823 "'frontend', 'backend' or 'task'"));
1824
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001825 /* Check if we run on the same thread than the xreator thread.
1826 * We cannot access to the socket if the thread is different.
1827 */
1828 if (socket->tid != tid)
1829 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1830
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001831 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001832 peer = xref_get_peer_and_lock(&socket->xref);
1833 if (!peer)
1834 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001835 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1836 si = appctx->owner;
1837 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001838
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001839 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001840 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001841 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001842 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001843 if (nblk < 0) /* Connection close. */
1844 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001845 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001846 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001847
1848 /* remove final \r\n. */
1849 if (nblk == 1) {
1850 if (blk1[len1-1] == '\n') {
1851 len1--;
1852 skip_at_end++;
1853 if (blk1[len1-1] == '\r') {
1854 len1--;
1855 skip_at_end++;
1856 }
1857 }
1858 }
1859 else {
1860 if (blk2[len2-1] == '\n') {
1861 len2--;
1862 skip_at_end++;
1863 if (blk2[len2-1] == '\r') {
1864 len2--;
1865 skip_at_end++;
1866 }
1867 }
1868 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001869 }
1870
1871 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001872 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001873 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001874 if (nblk < 0) /* Connection close. */
1875 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001876 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001877 goto connection_empty;
1878 }
1879
1880 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001881 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001882 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001883 if (nblk < 0) /* Connection close. */
1884 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001885 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001886 goto connection_empty;
1887
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001888 missing_bytes = wanted - socket->b.n;
1889 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001890 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001891 len1 = missing_bytes;
1892 } if (nblk == 2 && len1 + len2 > missing_bytes)
1893 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001894 }
1895
1896 len = len1;
1897
1898 luaL_addlstring(&socket->b, blk1, len1);
1899 if (nblk == 2) {
1900 len += len2;
1901 luaL_addlstring(&socket->b, blk2, len2);
1902 }
1903
1904 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001905 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906
1907 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001908 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909
1910 /* If the pattern reclaim to read all the data
1911 * in the connection, got out.
1912 */
1913 if (wanted == HLSR_READ_ALL)
1914 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001915 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001916 goto connection_empty;
1917
1918 /* Return result. */
1919 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001920 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001921 return 1;
1922
1923connection_closed:
1924
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001925 xref_unlock(&socket->xref, peer);
1926
1927no_peer:
1928
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001929 /* If the buffer containds data. */
1930 if (socket->b.n > 0) {
1931 luaL_pushresult(&socket->b);
1932 return 1;
1933 }
1934 lua_pushnil(L);
1935 lua_pushstring(L, "connection closed.");
1936 return 2;
1937
1938connection_empty:
1939
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001940 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1941 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001942 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001943 }
1944 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02001945 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001946 return 0;
1947}
1948
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001949/* This Lua function gets two parameters. The first one can be string
1950 * or a number. If the string is "*l", the user requires one line. If
1951 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001952 * If the value is a number, the user require a number of bytes equal
1953 * to the value. The default value is "*l" (a line).
1954 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001955 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001956 * integer takes this values:
1957 * -1 : read a line
1958 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001959 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001960 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001961 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962 * concatenated with the read data.
1963 */
1964__LJMP static int hlua_socket_receive(struct lua_State *L)
1965{
1966 int wanted = HLSR_READ_LINE;
1967 const char *pattern;
1968 int type;
1969 char *error;
1970 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001971 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001972
1973 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1974 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1975
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001976 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001977
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001978 /* Check if we run on the same thread than the xreator thread.
1979 * We cannot access to the socket if the thread is different.
1980 */
1981 if (socket->tid != tid)
1982 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1983
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001984 /* check for pattern. */
1985 if (lua_gettop(L) >= 2) {
1986 type = lua_type(L, 2);
1987 if (type == LUA_TSTRING) {
1988 pattern = lua_tostring(L, 2);
1989 if (strcmp(pattern, "*a") == 0)
1990 wanted = HLSR_READ_ALL;
1991 else if (strcmp(pattern, "*l") == 0)
1992 wanted = HLSR_READ_LINE;
1993 else {
1994 wanted = strtoll(pattern, &error, 10);
1995 if (*error != '\0')
1996 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1997 }
1998 }
1999 else if (type == LUA_TNUMBER) {
2000 wanted = lua_tointeger(L, 2);
2001 if (wanted < 0)
2002 WILL_LJMP(luaL_error(L, "Unsupported size."));
2003 }
2004 }
2005
2006 /* Set pattern. */
2007 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002008
2009 /* Check if we would replace the top by itself. */
2010 if (lua_gettop(L) != 2)
2011 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002012
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002013 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002014 luaL_buffinit(L, &socket->b);
2015
2016 /* Check prefix. */
2017 if (lua_gettop(L) >= 3) {
2018 if (lua_type(L, 3) != LUA_TSTRING)
2019 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2020 pattern = lua_tolstring(L, 3, &len);
2021 luaL_addlstring(&socket->b, pattern, len);
2022 }
2023
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002024 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002025}
2026
2027/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002028 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002029 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002030static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002031{
2032 struct hlua_socket *socket;
2033 struct hlua *hlua = hlua_gethlua(L);
2034 struct appctx *appctx;
2035 size_t buf_len;
2036 const char *buf;
2037 int len;
2038 int send_len;
2039 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002040 struct xref *peer;
2041 struct stream_interface *si;
2042 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002043
2044 /* Check if this lua stack is schedulable. */
2045 if (!hlua || !hlua->task)
2046 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2047 "'frontend', 'backend' or 'task'"));
2048
2049 /* Get object */
2050 socket = MAY_LJMP(hlua_checksocket(L, 1));
2051 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002052 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002053
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002054 /* Check if we run on the same thread than the xreator thread.
2055 * We cannot access to the socket if the thread is different.
2056 */
2057 if (socket->tid != tid)
2058 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2059
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002060 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002061 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002062 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002063 lua_pushinteger(L, -1);
2064 return 1;
2065 }
2066 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2067 si = appctx->owner;
2068 s = si_strm(si);
2069
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002070 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002071 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002072 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002073 lua_pushinteger(L, -1);
2074 return 1;
2075 }
2076
2077 /* Update the input buffer data. */
2078 buf += sent;
2079 send_len = buf_len - sent;
2080
2081 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002082 if (sent >= buf_len) {
2083 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002084 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002085 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002086
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002087 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002088 * the request buffer if its not required.
2089 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002090 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002091 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002092 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002093 }
2094
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002095 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002096 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002097 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002098 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002099 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002100
2101 /* send data */
2102 if (len < send_len)
2103 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002104 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002105
2106 /* "Not enough space" (-1), "Buffer too little to contain
2107 * the data" (-2) are not expected because the available length
2108 * is tested.
2109 * Other unknown error are also not expected.
2110 */
2111 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002112 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002113 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002114
sada05ed3302018-05-11 11:48:18 -07002115 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002116 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002117 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002118 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002119 return 1;
2120 }
2121
2122 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002123 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002124
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002125 s->req.rex = TICK_ETERNITY;
2126 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002127
2128 /* Update length sent. */
2129 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002130 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002131
2132 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002133 if (sent + len >= buf_len) {
2134 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002135 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002136 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002137
2138hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002139 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2140 xref_unlock(&socket->xref, peer);
2141 WILL_LJMP(luaL_error(L, "out of memory"));
2142 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002143 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002144 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002145 return 0;
2146}
2147
2148/* This function initiate the send of data. It just check the input
2149 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002150 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002151 * "hlua_socket_write_yield" that can yield.
2152 *
2153 * The Lua function gets between 3 and 4 parameters. The first one is
2154 * the associated object. The second is a string buffer. The third is
2155 * a facultative integer that represents where is the buffer position
2156 * of the start of the data that can send. The first byte is the
2157 * position "1". The default value is "1". The fourth argument is a
2158 * facultative integer that represents where is the buffer position
2159 * of the end of the data that can send. The default is the last byte.
2160 */
2161static int hlua_socket_send(struct lua_State *L)
2162{
2163 int i;
2164 int j;
2165 const char *buf;
2166 size_t buf_len;
2167
2168 /* Check number of arguments. */
2169 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2170 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2171
2172 /* Get the string. */
2173 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2174
2175 /* Get and check j. */
2176 if (lua_gettop(L) == 4) {
2177 j = MAY_LJMP(luaL_checkinteger(L, 4));
2178 if (j < 0)
2179 j = buf_len + j + 1;
2180 if (j > buf_len)
2181 j = buf_len + 1;
2182 lua_pop(L, 1);
2183 }
2184 else
2185 j = buf_len;
2186
2187 /* Get and check i. */
2188 if (lua_gettop(L) == 3) {
2189 i = MAY_LJMP(luaL_checkinteger(L, 3));
2190 if (i < 0)
2191 i = buf_len + i + 1;
2192 if (i > buf_len)
2193 i = buf_len + 1;
2194 lua_pop(L, 1);
2195 } else
2196 i = 1;
2197
2198 /* Check bth i and j. */
2199 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002200 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002201 return 1;
2202 }
2203 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002204 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002205 return 1;
2206 }
2207 if (i == 0)
2208 i = 1;
2209 if (j == 0)
2210 j = 1;
2211
2212 /* Pop the string. */
2213 lua_pop(L, 1);
2214
2215 /* Update the buffer length. */
2216 buf += i - 1;
2217 buf_len = j - i + 1;
2218 lua_pushlstring(L, buf, buf_len);
2219
2220 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002221 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002222
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002223 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002224}
2225
Willy Tarreau22b0a682015-06-17 19:43:49 +02002226#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002227__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2228{
2229 static char buffer[SOCKET_INFO_MAX_LEN];
2230 int ret;
2231 int len;
2232 char *p;
2233
2234 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2235 if (ret <= 0) {
2236 lua_pushnil(L);
2237 return 1;
2238 }
2239
2240 if (ret == AF_UNIX) {
2241 lua_pushstring(L, buffer+1);
2242 return 1;
2243 }
2244 else if (ret == AF_INET6) {
2245 buffer[0] = '[';
2246 len = strlen(buffer);
2247 buffer[len] = ']';
2248 len++;
2249 buffer[len] = ':';
2250 len++;
2251 p = buffer;
2252 }
2253 else if (ret == AF_INET) {
2254 p = buffer + 1;
2255 len = strlen(p);
2256 p[len] = ':';
2257 len++;
2258 }
2259 else {
2260 lua_pushnil(L);
2261 return 1;
2262 }
2263
2264 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2265 lua_pushnil(L);
2266 return 1;
2267 }
2268
2269 lua_pushstring(L, p);
2270 return 1;
2271}
2272
2273/* Returns information about the peer of the connection. */
2274__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2275{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002276 struct hlua_socket *socket;
2277 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002278 struct xref *peer;
2279 struct appctx *appctx;
2280 struct stream_interface *si;
2281 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002282 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002283
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002284 MAY_LJMP(check_args(L, 1, "getpeername"));
2285
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002286 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002287
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002288 /* Check if we run on the same thread than the xreator thread.
2289 * We cannot access to the socket if the thread is different.
2290 */
2291 if (socket->tid != tid)
2292 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2293
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002294 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002295 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002296 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297 lua_pushnil(L);
2298 return 1;
2299 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002300 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2301 si = appctx->owner;
2302 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002304 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002305 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002306 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307 lua_pushnil(L);
2308 return 1;
2309 }
2310
Willy Tarreaua71f6422016-11-16 17:00:14 +01002311 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002312 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002313 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002314 lua_pushnil(L);
2315 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002316 }
2317
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002318 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2319 xref_unlock(&socket->xref, peer);
2320 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002321}
2322
2323/* Returns information about my connection side. */
2324static int hlua_socket_getsockname(struct lua_State *L)
2325{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002326 struct hlua_socket *socket;
2327 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002328 struct appctx *appctx;
2329 struct xref *peer;
2330 struct stream_interface *si;
2331 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002332 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002333
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002334 MAY_LJMP(check_args(L, 1, "getsockname"));
2335
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002336 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002337
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002338 /* Check if we run on the same thread than the xreator thread.
2339 * We cannot access to the socket if the thread is different.
2340 */
2341 if (socket->tid != tid)
2342 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2343
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002344 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002345 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002346 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002347 lua_pushnil(L);
2348 return 1;
2349 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002350 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2351 si = appctx->owner;
2352 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002353
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002354 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002355 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002356 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002357 lua_pushnil(L);
2358 return 1;
2359 }
2360
Willy Tarreaua71f6422016-11-16 17:00:14 +01002361 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002362 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002363 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002364 lua_pushnil(L);
2365 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002366 }
2367
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002368 ret = hlua_socket_info(L, &conn->addr.from);
2369 xref_unlock(&socket->xref, peer);
2370 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002371}
2372
2373/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002374static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002375 .obj_type = OBJ_TYPE_APPLET,
2376 .name = "<LUA_TCP>",
2377 .fct = hlua_socket_handler,
2378 .release = hlua_socket_release,
2379};
2380
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002381__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002382{
2383 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2384 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002385 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002386 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002387 struct stream_interface *si;
2388 struct stream *s;
2389
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002390 /* Check if we run on the same thread than the xreator thread.
2391 * We cannot access to the socket if the thread is different.
2392 */
2393 if (socket->tid != tid)
2394 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2395
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002396 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002397 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002398 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002399 lua_pushnil(L);
2400 lua_pushstring(L, "Can't connect");
2401 return 2;
2402 }
2403 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2404 si = appctx->owner;
2405 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002406
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002407 /* Check if we run on the same thread than the xreator thread.
2408 * We cannot access to the socket if the thread is different.
2409 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002410 if (socket->tid != tid) {
2411 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002412 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002413 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002414
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002415 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002416 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002417 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002418 lua_pushnil(L);
2419 lua_pushstring(L, "Can't connect");
2420 return 2;
2421 }
2422
Willy Tarreaue09101e2018-10-16 17:37:12 +02002423 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002424
2425 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002426 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002427 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002428 lua_pushinteger(L, 1);
2429 return 1;
2430 }
2431
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002432 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2433 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002434 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002435 }
2436 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002437 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002438 return 0;
2439}
2440
2441/* This function fail or initite the connection. */
2442__LJMP static int hlua_socket_connect(struct lua_State *L)
2443{
2444 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002445 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002446 const char *ip;
2447 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002448 struct hlua *hlua;
2449 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002450 int low, high;
2451 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002452 struct xref *peer;
2453 struct stream_interface *si;
2454 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002455
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002456 if (lua_gettop(L) < 2)
2457 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002458
2459 /* Get args. */
2460 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002461
2462 /* Check if we run on the same thread than the xreator thread.
2463 * We cannot access to the socket if the thread is different.
2464 */
2465 if (socket->tid != tid)
2466 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2467
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002468 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002469 if (lua_gettop(L) >= 3) {
2470 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002471 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002472
Tim Duesterhus6edab862018-01-06 19:04:45 +01002473 /* Force the ip to end with a colon, to support IPv6 addresses
2474 * that are not enclosed within square brackets.
2475 */
2476 if (port > 0) {
2477 luaL_buffinit(L, &b);
2478 luaL_addstring(&b, ip);
2479 luaL_addchar(&b, ':');
2480 luaL_pushresult(&b);
2481 ip = lua_tolstring(L, lua_gettop(L), NULL);
2482 }
2483 }
2484
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002485 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002486 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002487 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002488 lua_pushnil(L);
2489 return 1;
2490 }
2491 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2492 si = appctx->owner;
2493 s = si_strm(si);
2494
2495 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002496 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002497 if (!conn) {
2498 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002499 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002500 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002501
Willy Tarreau3adac082015-09-26 17:51:09 +02002502 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002503 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002504
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002505 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002506 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002507 if (!addr) {
2508 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002509 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002510 }
2511 if (low != high) {
2512 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002513 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002514 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002515 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002516
2517 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002518 if (low == 0) {
2519 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002520 if (port == -1) {
2521 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002522 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002523 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002524 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2525 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002526 if (port == -1) {
2527 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002528 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002529 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002530 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2531 }
2532 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002533
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002534 hlua = hlua_gethlua(L);
Willy Tarreaue09101e2018-10-16 17:37:12 +02002535 appctx = __objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002536
2537 /* inform the stream that we want to be notified whenever the
2538 * connection completes.
2539 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002540 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002541 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002542 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002543
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002544 hlua->flags |= HLUA_MUST_GC;
2545
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002546 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2547 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002548 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002549 }
2550 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002551
2552 task_wakeup(s->task, TASK_WOKEN_INIT);
2553 /* Return yield waiting for connection. */
2554
Willy Tarreau9635e032018-10-16 17:52:55 +02002555 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002556
2557 return 0;
2558}
2559
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002560#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002561__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2562{
2563 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002564 struct xref *peer;
2565 struct appctx *appctx;
2566 struct stream_interface *si;
2567 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002568
2569 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2570 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002571
2572 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002573 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002574 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002575 lua_pushnil(L);
2576 return 1;
2577 }
2578 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2579 si = appctx->owner;
2580 s = si_strm(si);
2581
2582 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002583 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002584 return MAY_LJMP(hlua_socket_connect(L));
2585}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002586#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002587
2588__LJMP static int hlua_socket_setoption(struct lua_State *L)
2589{
2590 return 0;
2591}
2592
2593__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2594{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002595 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002596 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002597 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002598 struct xref *peer;
2599 struct appctx *appctx;
2600 struct stream_interface *si;
2601 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002602
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002603 MAY_LJMP(check_args(L, 2, "settimeout"));
2604
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002605 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002606
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002607 /* convert the timeout to millis */
2608 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002609
Thierry Fournier17a921b2018-03-08 09:59:02 +01002610 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002611 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002612 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2613
Mark Lakes56cc1252018-03-27 09:48:06 +02002614 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002615 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002616
2617 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002618 if (tmout == 0)
2619 tmout++; /* very small timeouts are adjusted to a minium of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002620
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002621 /* Check if we run on the same thread than the xreator thread.
2622 * We cannot access to the socket if the thread is different.
2623 */
2624 if (socket->tid != tid)
2625 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2626
Mark Lakes56cc1252018-03-27 09:48:06 +02002627 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002628 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002629 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002630 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2631 WILL_LJMP(lua_error(L));
2632 return 0;
2633 }
2634 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2635 si = appctx->owner;
2636 s = si_strm(si);
2637
Cyril Bonté7bb63452018-08-17 23:51:02 +02002638 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002639 s->req.rto = tmout;
2640 s->req.wto = tmout;
2641 s->res.rto = tmout;
2642 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002643 s->req.rex = tick_add_ifset(now_ms, tmout);
2644 s->req.wex = tick_add_ifset(now_ms, tmout);
2645 s->res.rex = tick_add_ifset(now_ms, tmout);
2646 s->res.wex = tick_add_ifset(now_ms, tmout);
2647
2648 s->task->expire = tick_add_ifset(now_ms, tmout);
2649 task_queue(s->task);
2650
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002651 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002652
Thierry Fourniere9636f12018-03-08 09:54:32 +01002653 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002654 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002655}
2656
2657__LJMP static int hlua_socket_new(lua_State *L)
2658{
2659 struct hlua_socket *socket;
2660 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002661 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002662 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002663
2664 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002665 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002666 hlua_pusherror(L, "socket: full stack");
2667 goto out_fail_conf;
2668 }
2669
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002670 /* Create the object: obj[0] = userdata. */
2671 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002672 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002673 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002674 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002675 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002676
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002677 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002678 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002679 hlua_pusherror(L, "socket: uninitialized pools.");
2680 goto out_fail_conf;
2681 }
2682
Willy Tarreau87b09662015-04-03 00:22:06 +02002683 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002684 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2685 lua_setmetatable(L, -2);
2686
Willy Tarreaud420a972015-04-06 00:39:18 +02002687 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002688 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002689 if (!appctx) {
2690 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002691 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002692 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002693
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002694 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002695 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002696 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2697 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002698
Willy Tarreaud420a972015-04-06 00:39:18 +02002699 /* Now create a session, task and stream for this applet */
2700 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2701 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002702 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002703 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002704 }
2705
Willy Tarreau87787ac2017-08-28 16:22:54 +02002706 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002707 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002708 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002709 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002710 }
2711
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002712 /* Initialise cross reference between stream and Lua socket object. */
2713 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002714
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002715 /* Configure "right" stream interface. this "si" is used to connect
2716 * and retrieve data from the server. The connection is initialized
2717 * with the "struct server".
2718 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002719 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002720
2721 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002722 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2723 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002724
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002725 return 1;
2726
Willy Tarreaud420a972015-04-06 00:39:18 +02002727 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002728 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002729 out_fail_sess:
2730 appctx_free(appctx);
2731 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002732 WILL_LJMP(lua_error(L));
2733 return 0;
2734}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002735
2736/*
2737 *
2738 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002739 * Class Channel
2740 *
2741 *
2742 */
2743
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002744/* This function is called before the Lua execution. It stores
2745 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002746 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002747static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002748{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002749 c->mode = stream->be->mode;
2750 switch (c->mode) {
2751 case PR_MODE_HTTP:
2752 c->data.http.dir = opt & SMP_OPT_DIR;
2753 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2754 c->data.http.state = stream->txn->req.msg_state;
2755 else
2756 c->data.http.state = stream->txn->rsp.msg_state;
2757 break;
2758 default:
2759 break;
2760 }
2761}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002762
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002763/* This function is called after the Lua execution. it
2764 * returns true if the parser state is consistent, otherwise,
2765 * it return false.
2766 *
2767 * In HTTP mode, the parser state must be in the same state
2768 * or greater when we exit the function. Even if we do a
2769 * control yield. This prevent to break the HTTP message
2770 * from the Lua code.
2771 */
2772static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2773{
2774 if (c->mode != stream->be->mode)
2775 return 0;
2776
2777 switch (c->mode) {
2778 case PR_MODE_HTTP:
2779 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002780 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002781 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2782 return stream->txn->req.msg_state >= c->data.http.state;
2783 else
2784 return stream->txn->rsp.msg_state >= c->data.http.state;
2785 default:
2786 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002787 }
2788 return 1;
2789}
2790
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002791/* Returns the struct hlua_channel join to the class channel in the
2792 * stack entry "ud" or throws an argument error.
2793 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002794__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002795{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002796 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002797}
2798
Willy Tarreau47860ed2015-03-10 14:07:50 +01002799/* Pushes the channel onto the top of the stack. If the stask does not have a
2800 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002801 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002802static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002803{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002804 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002805 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002806 return 0;
2807
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002808 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002809 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002810 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002811
2812 /* Pop a class sesison metatable and affect it to the userdata. */
2813 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2814 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002815 return 1;
2816}
2817
2818/* Duplicate all the data present in the input channel and put it
2819 * in a string LUA variables. Returns -1 and push a nil value in
2820 * the stack if the channel is closed and all the data are consumed,
2821 * returns 0 if no data are available, otherwise it returns the length
2822 * of the builded string.
2823 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002824static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002825{
2826 char *blk1;
2827 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002828 size_t len1;
2829 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002830 int ret;
2831 luaL_Buffer b;
2832
Willy Tarreau06d80a92017-10-19 14:32:15 +02002833 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002834 if (unlikely(ret == 0))
2835 return 0;
2836
2837 if (unlikely(ret < 0)) {
2838 lua_pushnil(L);
2839 return -1;
2840 }
2841
2842 luaL_buffinit(L, &b);
2843 luaL_addlstring(&b, blk1, len1);
2844 if (unlikely(ret == 2))
2845 luaL_addlstring(&b, blk2, len2);
2846 luaL_pushresult(&b);
2847
2848 if (unlikely(ret == 2))
2849 return len1 + len2;
2850 return len1;
2851}
2852
2853/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2854 * a yield. This function keep the data in the buffer.
2855 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002856__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002857{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002858 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002859
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002860 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2861
Christopher Faulet3f829a42018-12-13 21:56:45 +01002862 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2863 WILL_LJMP(lua_error(L));
2864
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002865 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002866 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002867 return 1;
2868}
2869
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002870/* Check arguments for the function "hlua_channel_dup_yield". */
2871__LJMP static int hlua_channel_dup(lua_State *L)
2872{
2873 MAY_LJMP(check_args(L, 1, "dup"));
2874 MAY_LJMP(hlua_checkchannel(L, 1));
2875 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2876}
2877
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002878/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2879 * a yield. This function consumes the data in the buffer. It returns
2880 * a string containing the data or a nil pointer if no data are available
2881 * and the channel is closed.
2882 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002883__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002884{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002885 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002886 int ret;
2887
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002888 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002889
Christopher Faulet3f829a42018-12-13 21:56:45 +01002890 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2891 WILL_LJMP(lua_error(L));
2892
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002893 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002894 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002895 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002896
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897 if (unlikely(ret == -1))
2898 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002899
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002900 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901 return 1;
2902}
2903
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002904/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002905__LJMP static int hlua_channel_get(lua_State *L)
2906{
2907 MAY_LJMP(check_args(L, 1, "get"));
2908 MAY_LJMP(hlua_checkchannel(L, 1));
2909 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2910}
2911
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912/* This functions consumes and returns one line. If the channel is closed,
2913 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002914 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002915 * value.
2916 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002917__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002919 char *blk1;
2920 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002921 size_t len1;
2922 size_t len2;
2923 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002924 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002925 int ret;
2926 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002927
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002928 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2929
Christopher Faulet3f829a42018-12-13 21:56:45 +01002930 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2931 WILL_LJMP(lua_error(L));
2932
Willy Tarreau06d80a92017-10-19 14:32:15 +02002933 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002934 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002935 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002936
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937 if (ret == -1) {
2938 lua_pushnil(L);
2939 return 1;
2940 }
2941
2942 luaL_buffinit(L, &b);
2943 luaL_addlstring(&b, blk1, len1);
2944 len = len1;
2945 if (unlikely(ret == 2)) {
2946 luaL_addlstring(&b, blk2, len2);
2947 len += len2;
2948 }
2949 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002950 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002951 return 1;
2952}
2953
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002954/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002955__LJMP static int hlua_channel_getline(lua_State *L)
2956{
2957 MAY_LJMP(check_args(L, 1, "getline"));
2958 MAY_LJMP(hlua_checkchannel(L, 1));
2959 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2960}
2961
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002962/* This function takes a string as input, and append it at the
2963 * input side of channel. If the data is too big, but a space
2964 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002965 * yields. If the data is bigger than the buffer, or if the
2966 * channel is closed, it returns -1. Otherwise, it returns the
2967 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002968 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002969__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002970{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002971 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002972 size_t len;
2973 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2974 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2975 int ret;
2976 int max;
2977
Christopher Faulet3f829a42018-12-13 21:56:45 +01002978 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
2979 WILL_LJMP(lua_error(L));
2980
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002981 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002982 * the request buffer if its not required.
2983 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002984 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01002985 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02002986 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002987 }
2988
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002989 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002990 if (max > len - l)
2991 max = len - l;
2992
Willy Tarreau06d80a92017-10-19 14:32:15 +02002993 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994 if (ret == -2 || ret == -3) {
2995 lua_pushinteger(L, -1);
2996 return 1;
2997 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002998 if (ret == -1) {
2999 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02003000 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01003001 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003002 l += ret;
3003 lua_pop(L, 1);
3004 lua_pushinteger(L, l);
3005
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003006 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003007 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003008 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003009 * in this case, we cannot add more data, so we cannot yield,
3010 * we return the amount of copyied data.
3011 */
3012 return 1;
3013 }
3014 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003015 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003016 return 1;
3017}
3018
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003019/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3020 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003021 * buffer size is too little for the data.
3022 */
3023__LJMP static int hlua_channel_append(lua_State *L)
3024{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003025 size_t len;
3026
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003027 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003028 MAY_LJMP(hlua_checkchannel(L, 1));
3029 MAY_LJMP(luaL_checklstring(L, 2, &len));
3030 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031 lua_pushinteger(L, 0);
3032
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003033 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003034}
3035
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003036/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003037 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003038 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003039 * or -1 if the channel is closed or if the buffer size is too
3040 * little for the data.
3041 */
3042__LJMP static int hlua_channel_set(lua_State *L)
3043{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003044 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003045
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003046 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003047 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003048 lua_pushinteger(L, 0);
3049
Christopher Faulet3f829a42018-12-13 21:56:45 +01003050 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3051 WILL_LJMP(lua_error(L));
3052
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003053 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003054
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003055 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003056}
3057
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003058/* Append data in the output side of the buffer. This data is immediately
3059 * sent. The function returns the amount of data written. If the buffer
3060 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003061 * if the channel is closed.
3062 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003063__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003064{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003065 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003066 size_t len;
3067 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3068 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3069 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003070 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003071
Christopher Faulet3f829a42018-12-13 21:56:45 +01003072 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3073 WILL_LJMP(lua_error(L));
3074
Willy Tarreau47860ed2015-03-10 14:07:50 +01003075 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003076 lua_pushinteger(L, -1);
3077 return 1;
3078 }
3079
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003080 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003081 * the request buffer if its not required.
3082 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003083 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003084 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003085 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003086 }
3087
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003088 /* The written data will be immediately sent, so we can check
3089 * the available space without taking in account the reserve.
3090 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003091 * data, because the buffer will be flushed.
3092 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003093 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003094
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003095 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003096 * in this case, we cannot add more data, so we cannot yield,
3097 * we return the amount of copyied data.
3098 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003099 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003100 return 1;
3101
3102 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003103 if (max > len - l)
3104 max = len - l;
3105
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003106 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003107 * detects a non contiguous buffer and realign it.
3108 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003109 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003110 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003111
3112 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003113 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003114
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003115 /* buffer replace considers that the input part is filled.
3116 * so, I must forward these new data in the output part.
3117 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003118 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003119
3120 l += max;
3121 lua_pop(L, 1);
3122 lua_pushinteger(L, l);
3123
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003124 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003125 * in this case, we cannot add more data, so we cannot yield,
3126 * we return the amount of copyied data.
3127 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003128 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003129 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003130 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003131
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003132 if (l < len) {
3133 /* If we are waiting for space in the response buffer, we
3134 * must set the flag WAKERESWR. This flag required the task
3135 * wake up if any activity is detected on the response buffer.
3136 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003137 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003138 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003139 else
3140 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003141 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003142 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003143
3144 return 1;
3145}
3146
3147/* Just a wraper of "_hlua_channel_send". This wrapper permits
3148 * yield the LUA process, and resume it without checking the
3149 * input arguments.
3150 */
3151__LJMP static int hlua_channel_send(lua_State *L)
3152{
3153 MAY_LJMP(check_args(L, 2, "send"));
3154 lua_pushinteger(L, 0);
3155
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003156 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003157}
3158
3159/* This function forward and amount of butes. The data pass from
3160 * the input side of the buffer to the output side, and can be
3161 * forwarded. This function never fails.
3162 *
3163 * The Lua function takes an amount of bytes to be forwarded in
3164 * imput. It returns the number of bytes forwarded.
3165 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003166__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003167{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003168 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003169 int len;
3170 int l;
3171 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003172 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003173
3174 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003175
3176 if (chn_strm(chn)->be->mode == PR_MODE_HTTP)
3177 WILL_LJMP(lua_error(L));
3178
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003179 len = MAY_LJMP(luaL_checkinteger(L, 2));
3180 l = MAY_LJMP(luaL_checkinteger(L, -1));
3181
3182 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003183 if (max > ci_data(chn))
3184 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003185 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003186 l += max;
3187
3188 lua_pop(L, 1);
3189 lua_pushinteger(L, l);
3190
3191 /* Check if it miss bytes to forward. */
3192 if (l < len) {
3193 /* The the input channel or the output channel are closed, we
3194 * must return the amount of data forwarded.
3195 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003196 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003197 return 1;
3198
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003199 /* If we are waiting for space data in the response buffer, we
3200 * must set the flag WAKERESWR. This flag required the task
3201 * wake up if any activity is detected on the response buffer.
3202 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003203 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003204 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003205 else
3206 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003207
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003208 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003209 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003210 }
3211
3212 return 1;
3213}
3214
3215/* Just check the input and prepare the stack for the previous
3216 * function "hlua_channel_forward_yield"
3217 */
3218__LJMP static int hlua_channel_forward(lua_State *L)
3219{
3220 MAY_LJMP(check_args(L, 2, "forward"));
3221 MAY_LJMP(hlua_checkchannel(L, 1));
3222 MAY_LJMP(luaL_checkinteger(L, 2));
3223
3224 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003225 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003226}
3227
3228/* Just returns the number of bytes available in the input
3229 * side of the buffer. This function never fails.
3230 */
3231__LJMP static int hlua_channel_get_in_len(lua_State *L)
3232{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003233 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003234
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003235 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003236 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003237 if (IS_HTX_STRM(chn_strm(chn))) {
3238 struct htx *htx = htxbuf(&chn->buf);
3239 lua_pushinteger(L, htx->data - co_data(chn));
3240 }
3241 else
3242 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003243 return 1;
3244}
3245
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003246/* Returns true if the channel is full. */
3247__LJMP static int hlua_channel_is_full(lua_State *L)
3248{
3249 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003250
3251 MAY_LJMP(check_args(L, 1, "is_full"));
3252 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0136ebb2020-02-26 11:59:19 +01003253 /* ignore the reserve, we are not on a producer side (ie in an
3254 * applet).
3255 */
3256 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003257 return 1;
3258}
3259
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003260/* Just returns the number of bytes available in the output
3261 * side of the buffer. This function never fails.
3262 */
3263__LJMP static int hlua_channel_get_out_len(lua_State *L)
3264{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003265 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003266
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003267 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003268 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003269 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003270 return 1;
3271}
3272
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003273/*
3274 *
3275 *
3276 * Class Fetches
3277 *
3278 *
3279 */
3280
3281/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003282 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003283 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003284__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003285{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003286 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003287}
3288
3289/* This function creates and push in the stack a fetch object according
3290 * with a current TXN.
3291 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003292static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003293{
Willy Tarreau7073c472015-04-06 11:15:40 +02003294 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003295
3296 /* Check stack size. */
3297 if (!lua_checkstack(L, 3))
3298 return 0;
3299
3300 /* Create the object: obj[0] = userdata.
3301 * Note that the base of the Fetches object is the
3302 * transaction object.
3303 */
3304 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003305 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003306 lua_rawseti(L, -2, 0);
3307
Willy Tarreau7073c472015-04-06 11:15:40 +02003308 hsmp->s = txn->s;
3309 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003310 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003311 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003312
3313 /* Pop a class sesison metatable and affect it to the userdata. */
3314 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3315 lua_setmetatable(L, -2);
3316
3317 return 1;
3318}
3319
3320/* This function is an LUA binding. It is called with each sample-fetch.
3321 * It uses closure argument to store the associated sample-fetch. It
3322 * returns only one argument or throws an error. An error is thrown
3323 * only if an error is encountered during the argument parsing. If
3324 * the "sample-fetch" function fails, nil is returned.
3325 */
3326__LJMP static int hlua_run_sample_fetch(lua_State *L)
3327{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003328 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003329 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003330 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003331 int i;
3332 struct sample smp;
3333
3334 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003335 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003336
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003337 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003338 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003339
Thierry FOURNIERca988662015-12-20 18:43:03 +01003340 /* Check execution authorization. */
3341 if (f->use & SMP_USE_HTTP_ANY &&
3342 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3343 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3344 "is not available in Lua services", f->kw);
3345 WILL_LJMP(lua_error(L));
3346 }
3347
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003348 /* Get extra arguments. */
3349 for (i = 0; i < lua_gettop(L) - 1; i++) {
3350 if (i >= ARGM_NBARGS)
3351 break;
3352 hlua_lua2arg(L, i + 2, &args[i]);
3353 }
3354 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003355 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003356
3357 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003358 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003359
3360 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003361 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003362 lua_pushfstring(L, "error in arguments");
3363 WILL_LJMP(lua_error(L));
3364 }
3365
3366 /* Initialise the sample. */
3367 memset(&smp, 0, sizeof(smp));
3368
3369 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003370 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003371 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003372 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003373 lua_pushstring(L, "");
3374 else
3375 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003376 return 1;
3377 }
3378
3379 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003380 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003381 hlua_smp2lua_str(L, &smp);
3382 else
3383 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003384 return 1;
3385}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003386
3387/*
3388 *
3389 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003390 * Class Converters
3391 *
3392 *
3393 */
3394
3395/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003396 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003397 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003398__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003399{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003400 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003401}
3402
3403/* This function creates and push in the stack a Converters object
3404 * according with a current TXN.
3405 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003406static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003407{
Willy Tarreau7073c472015-04-06 11:15:40 +02003408 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003409
3410 /* Check stack size. */
3411 if (!lua_checkstack(L, 3))
3412 return 0;
3413
3414 /* Create the object: obj[0] = userdata.
3415 * Note that the base of the Converters object is the
3416 * same than the TXN object.
3417 */
3418 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003419 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003420 lua_rawseti(L, -2, 0);
3421
Willy Tarreau7073c472015-04-06 11:15:40 +02003422 hsmp->s = txn->s;
3423 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003424 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003425 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003426
Willy Tarreau87b09662015-04-03 00:22:06 +02003427 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003428 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3429 lua_setmetatable(L, -2);
3430
3431 return 1;
3432}
3433
3434/* This function is an LUA binding. It is called with each converter.
3435 * It uses closure argument to store the associated converter. It
3436 * returns only one argument or throws an error. An error is thrown
3437 * only if an error is encountered during the argument parsing. If
3438 * the converter function function fails, nil is returned.
3439 */
3440__LJMP static int hlua_run_sample_conv(lua_State *L)
3441{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003442 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003443 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003444 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003445 int i;
3446 struct sample smp;
3447
3448 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003449 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003450
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003451 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003452 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003453
3454 /* Get extra arguments. */
3455 for (i = 0; i < lua_gettop(L) - 2; i++) {
3456 if (i >= ARGM_NBARGS)
3457 break;
3458 hlua_lua2arg(L, i + 3, &args[i]);
3459 }
3460 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003461 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003462
3463 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003464 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003465
3466 /* Run the special args checker. */
3467 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3468 hlua_pusherror(L, "error in arguments");
3469 WILL_LJMP(lua_error(L));
3470 }
3471
3472 /* Initialise the sample. */
Amaury Denoyelleca492f32020-10-29 17:21:20 +01003473 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003474 if (!hlua_lua2smp(L, 2, &smp)) {
3475 hlua_pusherror(L, "error in the input argument");
3476 WILL_LJMP(lua_error(L));
3477 }
3478
Willy Tarreau1777ea62016-03-10 16:15:46 +01003479 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3480
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003481 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003482 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003483 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003484 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003485 WILL_LJMP(lua_error(L));
3486 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003487 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3488 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003489 hlua_pusherror(L, "error during the input argument casting");
3490 WILL_LJMP(lua_error(L));
3491 }
3492
3493 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003494 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003495 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003496 lua_pushstring(L, "");
3497 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003498 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003499 return 1;
3500 }
3501
3502 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003503 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003504 hlua_smp2lua_str(L, &smp);
3505 else
3506 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003507 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003508}
3509
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003510/*
3511 *
3512 *
3513 * Class AppletTCP
3514 *
3515 *
3516 */
3517
3518/* Returns a struct hlua_txn if the stack entry "ud" is
3519 * a class stream, otherwise it throws an error.
3520 */
3521__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3522{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003523 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003524}
3525
3526/* This function creates and push in the stack an Applet object
3527 * according with a current TXN.
3528 */
3529static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3530{
3531 struct hlua_appctx *appctx;
3532 struct stream_interface *si = ctx->owner;
3533 struct stream *s = si_strm(si);
3534 struct proxy *p = s->be;
3535
3536 /* Check stack size. */
3537 if (!lua_checkstack(L, 3))
3538 return 0;
3539
3540 /* Create the object: obj[0] = userdata.
3541 * Note that the base of the Converters object is the
3542 * same than the TXN object.
3543 */
3544 lua_newtable(L);
3545 appctx = lua_newuserdata(L, sizeof(*appctx));
3546 lua_rawseti(L, -2, 0);
3547 appctx->appctx = ctx;
3548 appctx->htxn.s = s;
3549 appctx->htxn.p = p;
3550
3551 /* Create the "f" field that contains a list of fetches. */
3552 lua_pushstring(L, "f");
3553 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3554 return 0;
3555 lua_settable(L, -3);
3556
3557 /* Create the "sf" field that contains a list of stringsafe fetches. */
3558 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003559 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003560 return 0;
3561 lua_settable(L, -3);
3562
3563 /* Create the "c" field that contains a list of converters. */
3564 lua_pushstring(L, "c");
3565 if (!hlua_converters_new(L, &appctx->htxn, 0))
3566 return 0;
3567 lua_settable(L, -3);
3568
3569 /* Create the "sc" field that contains a list of stringsafe converters. */
3570 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003571 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003572 return 0;
3573 lua_settable(L, -3);
3574
3575 /* Pop a class stream metatable and affect it to the table. */
3576 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3577 lua_setmetatable(L, -2);
3578
3579 return 1;
3580}
3581
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003582__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3583{
3584 struct hlua_appctx *appctx;
3585 struct stream *s;
3586 const char *name;
3587 size_t len;
3588 struct sample smp;
3589
3590 MAY_LJMP(check_args(L, 3, "set_var"));
3591
3592 /* It is useles to retrieve the stream, but this function
3593 * runs only in a stream context.
3594 */
3595 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3596 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3597 s = appctx->htxn.s;
3598
3599 /* Converts the third argument in a sample. */
Amaury Denoyelleca492f32020-10-29 17:21:20 +01003600 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003601 hlua_lua2smp(L, 3, &smp);
3602
3603 /* Store the sample in a variable. */
3604 smp_set_owner(&smp, s->be, s->sess, s, 0);
3605 vars_set_by_name(name, len, &smp);
3606 return 0;
3607}
3608
3609__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3610{
3611 struct hlua_appctx *appctx;
3612 struct stream *s;
3613 const char *name;
3614 size_t len;
3615 struct sample smp;
3616
3617 MAY_LJMP(check_args(L, 2, "unset_var"));
3618
3619 /* It is useles to retrieve the stream, but this function
3620 * runs only in a stream context.
3621 */
3622 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3623 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3624 s = appctx->htxn.s;
3625
3626 /* Unset the variable. */
3627 smp_set_owner(&smp, s->be, s->sess, s, 0);
3628 vars_unset_by_name(name, len, &smp);
3629 return 0;
3630}
3631
3632__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3633{
3634 struct hlua_appctx *appctx;
3635 struct stream *s;
3636 const char *name;
3637 size_t len;
3638 struct sample smp;
3639
3640 MAY_LJMP(check_args(L, 2, "get_var"));
3641
3642 /* It is useles to retrieve the stream, but this function
3643 * runs only in a stream context.
3644 */
3645 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3646 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3647 s = appctx->htxn.s;
3648
3649 smp_set_owner(&smp, s->be, s->sess, s, 0);
3650 if (!vars_get_by_name(name, len, &smp)) {
3651 lua_pushnil(L);
3652 return 1;
3653 }
3654
3655 return hlua_smp2lua(L, &smp);
3656}
3657
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003658__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3659{
3660 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3661 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003662 struct hlua *hlua;
3663
3664 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003665 if (!s->hlua)
3666 return 0;
3667 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003668
3669 MAY_LJMP(check_args(L, 2, "set_priv"));
3670
3671 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003672 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003673
3674 /* Get and store new value. */
3675 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3676 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3677
3678 return 0;
3679}
3680
3681__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3682{
3683 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3684 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003685 struct hlua *hlua;
3686
3687 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003688 if (!s->hlua) {
3689 lua_pushnil(L);
3690 return 1;
3691 }
3692 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003693
3694 /* Push configuration index in the stack. */
3695 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3696
3697 return 1;
3698}
3699
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003700/* If expected data not yet available, it returns a yield. This function
3701 * consumes the data in the buffer. It returns a string containing the
3702 * data. This string can be empty.
3703 */
3704__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3705{
3706 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3707 struct stream_interface *si = appctx->appctx->owner;
3708 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003709 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003710 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003711 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003712 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003713
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003714 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003715 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003716
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003717 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003718 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003719 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003720 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003721 }
3722
3723 /* End of data: commit the total strings and return. */
3724 if (ret < 0) {
3725 luaL_pushresult(&appctx->b);
3726 return 1;
3727 }
3728
3729 /* Ensure that the block 2 length is usable. */
3730 if (ret == 1)
3731 len2 = 0;
3732
3733 /* dont check the max length read and dont check. */
3734 luaL_addlstring(&appctx->b, blk1, len1);
3735 luaL_addlstring(&appctx->b, blk2, len2);
3736
3737 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003738 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003739 luaL_pushresult(&appctx->b);
3740 return 1;
3741}
3742
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003743/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003744__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3745{
3746 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3747
3748 /* Initialise the string catenation. */
3749 luaL_buffinit(L, &appctx->b);
3750
3751 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3752}
3753
3754/* If expected data not yet available, it returns a yield. This function
3755 * consumes the data in the buffer. It returns a string containing the
3756 * data. This string can be empty.
3757 */
3758__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3759{
3760 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3761 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003762 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003763 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003764 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003765 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003766 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003767 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003768
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003769 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003770 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003771
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003772 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003773 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003774 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003775 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003776 }
3777
3778 /* End of data: commit the total strings and return. */
3779 if (ret < 0) {
3780 luaL_pushresult(&appctx->b);
3781 return 1;
3782 }
3783
3784 /* Ensure that the block 2 length is usable. */
3785 if (ret == 1)
3786 len2 = 0;
3787
3788 if (len == -1) {
3789
3790 /* If len == -1, catenate all the data avalaile and
3791 * yield because we want to get all the data until
3792 * the end of data stream.
3793 */
3794 luaL_addlstring(&appctx->b, blk1, len1);
3795 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003796 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003797 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003798 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003799
3800 } else {
3801
3802 /* Copy the fisrt block caping to the length required. */
3803 if (len1 > len)
3804 len1 = len;
3805 luaL_addlstring(&appctx->b, blk1, len1);
3806 len -= len1;
3807
3808 /* Copy the second block. */
3809 if (len2 > len)
3810 len2 = len;
3811 luaL_addlstring(&appctx->b, blk2, len2);
3812 len -= len2;
3813
3814 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003815 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003816
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003817 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003818 if (len > 0) {
3819 lua_pushinteger(L, len);
3820 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003821 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003822 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003823 }
3824
3825 /* return the result. */
3826 luaL_pushresult(&appctx->b);
3827 return 1;
3828 }
3829
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003830 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003831 hlua_pusherror(L, "Lua: internal error");
3832 WILL_LJMP(lua_error(L));
3833 return 0;
3834}
3835
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003836/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003837__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3838{
3839 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3840 int len = -1;
3841
3842 if (lua_gettop(L) > 2)
3843 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3844 if (lua_gettop(L) >= 2) {
3845 len = MAY_LJMP(luaL_checkinteger(L, 2));
3846 lua_pop(L, 1);
3847 }
3848
3849 /* Confirm or set the required length */
3850 lua_pushinteger(L, len);
3851
3852 /* Initialise the string catenation. */
3853 luaL_buffinit(L, &appctx->b);
3854
3855 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3856}
3857
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003858/* Append data in the output side of the buffer. This data is immediately
3859 * sent. The function returns the amount of data written. If the buffer
3860 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003861 * if the channel is closed.
3862 */
3863__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3864{
3865 size_t len;
3866 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3867 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3868 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3869 struct stream_interface *si = appctx->appctx->owner;
3870 struct channel *chn = si_ic(si);
3871 int max;
3872
3873 /* Get the max amount of data which can write as input in the channel. */
3874 max = channel_recv_max(chn);
3875 if (max > (len - l))
3876 max = len - l;
3877
3878 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003879 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003880
3881 /* update counters. */
3882 l += max;
3883 lua_pop(L, 1);
3884 lua_pushinteger(L, l);
3885
3886 /* If some data is not send, declares the situation to the
3887 * applet, and returns a yield.
3888 */
3889 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003890 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003891 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003892 }
3893
3894 return 1;
3895}
3896
3897/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3898 * yield the LUA process, and resume it without checking the
3899 * input arguments.
3900 */
3901__LJMP static int hlua_applet_tcp_send(lua_State *L)
3902{
3903 MAY_LJMP(check_args(L, 2, "send"));
3904 lua_pushinteger(L, 0);
3905
3906 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3907}
3908
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003909/*
3910 *
3911 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003912 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003913 *
3914 *
3915 */
3916
3917/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003918 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003919 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003920__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003921{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003922 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003923}
3924
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003925/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003926 * according with a current TXN.
3927 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003928static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003929{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003930 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003931 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003932 struct stream_interface *si = ctx->owner;
3933 struct stream *s = si_strm(si);
3934 struct proxy *px = s->be;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003935
3936 /* Check stack size. */
3937 if (!lua_checkstack(L, 3))
3938 return 0;
3939
3940 /* Create the object: obj[0] = userdata.
3941 * Note that the base of the Converters object is the
3942 * same than the TXN object.
3943 */
3944 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003945 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003946 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003947 appctx->appctx = ctx;
3948 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003949 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003950 appctx->htxn.s = s;
3951 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003952
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003953 /* Create the "f" field that contains a list of fetches. */
3954 lua_pushstring(L, "f");
3955 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3956 return 0;
3957 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003958
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003959 /* Create the "sf" field that contains a list of stringsafe fetches. */
3960 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003961 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003962 return 0;
3963 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003964
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003965 /* Create the "c" field that contains a list of converters. */
3966 lua_pushstring(L, "c");
3967 if (!hlua_converters_new(L, &appctx->htxn, 0))
3968 return 0;
3969 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003970
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003971 /* Create the "sc" field that contains a list of stringsafe converters. */
3972 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003973 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003974 return 0;
3975 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003976
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003977 if (IS_HTX_STRM(s)) {
3978 /* HTX version */
3979 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet29f17582019-05-23 11:03:26 +02003980 struct htx_blk *blk;
3981 struct htx_sl *sl;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003982 struct ist path;
3983 unsigned long long len = 0;
3984 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003985
Christopher Faulet29f17582019-05-23 11:03:26 +02003986 blk = htx_get_first_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +01003987 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Faulet29f17582019-05-23 11:03:26 +02003988 sl = htx_get_blk_ptr(htx, blk);
3989
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003990 /* Stores the request method. */
3991 lua_pushstring(L, "method");
3992 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
3993 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003994
Christopher Faulet9c832fc2018-12-14 13:34:05 +01003995 /* Stores the http version. */
3996 lua_pushstring(L, "version");
3997 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
3998 lua_settable(L, -3);
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003999
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004000 /* creates an array of headers. hlua_http_get_headers() crates and push
4001 * the array on the top of the stack.
4002 */
4003 lua_pushstring(L, "headers");
4004 htxn.s = s;
4005 htxn.p = px;
4006 htxn.dir = SMP_OPT_DIR_REQ;
4007 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
4008 return 0;
4009 lua_settable(L, -3);
4010
4011 path = http_get_path(htx_sl_req_uri(sl));
4012 if (path.ptr) {
4013 char *p, *q, *end;
4014
4015 p = path.ptr;
4016 end = path.ptr + path.len;
4017 q = p;
4018 while (q < end && *q != '?')
4019 q++;
4020
4021 /* Stores the request path. */
4022 lua_pushstring(L, "path");
4023 lua_pushlstring(L, p, q - p);
4024 lua_settable(L, -3);
4025
4026 /* Stores the query string. */
4027 lua_pushstring(L, "qs");
4028 if (*q == '?')
4029 q++;
4030 lua_pushlstring(L, q, end - q);
4031 lua_settable(L, -3);
4032 }
4033
Christopher Fauleta3f15502019-05-13 15:27:23 +02004034 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004035 struct htx_blk *blk = htx_get_blk(htx, pos);
4036 enum htx_blk_type type = htx_get_blk_type(blk);
4037
Christopher Faulet54b5e212019-06-04 10:08:28 +02004038 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004039 break;
4040 if (type == HTX_BLK_DATA)
4041 len += htx_get_blksz(blk);
4042 }
4043 if (htx->extra != ULLONG_MAX)
4044 len += htx->extra;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004045
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004046 /* Stores the request path. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004047 lua_pushstring(L, "length");
4048 lua_pushinteger(L, len);
4049 lua_settable(L, -3);
4050 }
4051 else {
4052 /* Legacy HTTP version */
4053 struct http_txn *txn = s->txn;
4054 const char *path;
4055 const char *end;
4056 const char *p;
4057
4058 /* Stores the request method. */
4059 lua_pushstring(L, "method");
4060 lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004061 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004062
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004063 /* Stores the http version. */
4064 lua_pushstring(L, "version");
4065 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 +01004066 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004067
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004068 /* creates an array of headers. hlua_http_get_headers() crates and push
4069 * the array on the top of the stack.
4070 */
4071 lua_pushstring(L, "headers");
4072 htxn.s = s;
4073 htxn.p = px;
4074 htxn.dir = SMP_OPT_DIR_REQ;
4075 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
4076 return 0;
4077 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004078
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004079 /* Get path and qs */
4080 path = http_txn_get_path(txn);
4081 if (path) {
4082 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4083 p = path;
4084 while (p < end && *p != '?')
4085 p++;
4086
4087 /* Stores the request path. */
4088 lua_pushstring(L, "path");
4089 lua_pushlstring(L, path, p - path);
4090 lua_settable(L, -3);
4091
4092 /* Stores the query string. */
4093 lua_pushstring(L, "qs");
4094 if (*p == '?')
4095 p++;
4096 lua_pushlstring(L, p, end - p);
4097 lua_settable(L, -3);
4098 }
4099
4100 /* Stores the request path. */
4101 lua_pushstring(L, "length");
4102 lua_pushinteger(L, txn->req.body_len);
4103 lua_settable(L, -3);
4104 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004105
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004106 /* Create an empty array of HTTP request headers. */
4107 lua_pushstring(L, "response");
4108 lua_newtable(L);
4109 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004110
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004111 /* Pop a class stream metatable and affect it to the table. */
4112 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4113 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004114
4115 return 1;
4116}
4117
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004118__LJMP static int hlua_applet_http_set_var(lua_State *L)
4119{
4120 struct hlua_appctx *appctx;
4121 struct stream *s;
4122 const char *name;
4123 size_t len;
4124 struct sample smp;
4125
4126 MAY_LJMP(check_args(L, 3, "set_var"));
4127
4128 /* It is useles to retrieve the stream, but this function
4129 * runs only in a stream context.
4130 */
4131 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4132 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4133 s = appctx->htxn.s;
4134
4135 /* Converts the third argument in a sample. */
Amaury Denoyelleca492f32020-10-29 17:21:20 +01004136 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004137 hlua_lua2smp(L, 3, &smp);
4138
4139 /* Store the sample in a variable. */
4140 smp_set_owner(&smp, s->be, s->sess, s, 0);
4141 vars_set_by_name(name, len, &smp);
4142 return 0;
4143}
4144
4145__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4146{
4147 struct hlua_appctx *appctx;
4148 struct stream *s;
4149 const char *name;
4150 size_t len;
4151 struct sample smp;
4152
4153 MAY_LJMP(check_args(L, 2, "unset_var"));
4154
4155 /* It is useles to retrieve the stream, but this function
4156 * runs only in a stream context.
4157 */
4158 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4159 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4160 s = appctx->htxn.s;
4161
4162 /* Unset the variable. */
4163 smp_set_owner(&smp, s->be, s->sess, s, 0);
4164 vars_unset_by_name(name, len, &smp);
4165 return 0;
4166}
4167
4168__LJMP static int hlua_applet_http_get_var(lua_State *L)
4169{
4170 struct hlua_appctx *appctx;
4171 struct stream *s;
4172 const char *name;
4173 size_t len;
4174 struct sample smp;
4175
4176 MAY_LJMP(check_args(L, 2, "get_var"));
4177
4178 /* It is useles to retrieve the stream, but this function
4179 * runs only in a stream context.
4180 */
4181 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4182 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4183 s = appctx->htxn.s;
4184
4185 smp_set_owner(&smp, s->be, s->sess, s, 0);
4186 if (!vars_get_by_name(name, len, &smp)) {
4187 lua_pushnil(L);
4188 return 1;
4189 }
4190
4191 return hlua_smp2lua(L, &smp);
4192}
4193
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004194__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4195{
4196 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4197 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004198 struct hlua *hlua;
4199
4200 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004201 if (!s->hlua)
4202 return 0;
4203 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004204
4205 MAY_LJMP(check_args(L, 2, "set_priv"));
4206
4207 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004208 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004209
4210 /* Get and store new value. */
4211 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4212 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4213
4214 return 0;
4215}
4216
4217__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4218{
4219 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4220 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004221 struct hlua *hlua;
4222
4223 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004224 if (!s->hlua) {
4225 lua_pushnil(L);
4226 return 1;
4227 }
4228 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004229
4230 /* Push configuration index in the stack. */
4231 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4232
4233 return 1;
4234}
4235
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004236/* If expected data not yet available, it returns a yield. This function
4237 * consumes the data in the buffer. It returns a string containing the
4238 * data. This string can be empty.
4239 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004240__LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx)
4241{
4242 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4243 struct stream_interface *si = appctx->appctx->owner;
4244 struct channel *req = si_oc(si);
4245 struct htx *htx;
4246 struct htx_blk *blk;
4247 size_t count;
4248 int stop = 0;
4249
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004250 htx = htx_from_buf(&req->buf);
4251 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004252 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004253
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004254 while (count && !stop && blk) {
4255 enum htx_blk_type type = htx_get_blk_type(blk);
4256 uint32_t sz = htx_get_blksz(blk);
4257 struct ist v;
4258 uint32_t vlen;
4259 char *nl;
4260
Christopher Faulete461e342018-12-18 16:43:35 +01004261 if (type == HTX_BLK_EOM) {
4262 stop = 1;
4263 break;
4264 }
4265
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004266 vlen = sz;
4267 if (vlen > count) {
4268 if (type != HTX_BLK_DATA)
4269 break;
4270 vlen = count;
4271 }
4272
4273 switch (type) {
4274 case HTX_BLK_UNUSED:
4275 break;
4276
4277 case HTX_BLK_DATA:
4278 v = htx_get_blk_value(htx, blk);
4279 v.len = vlen;
4280 nl = istchr(v, '\n');
4281 if (nl != NULL) {
4282 stop = 1;
4283 vlen = nl - v.ptr + 1;
4284 }
4285 luaL_addlstring(&appctx->b, v.ptr, vlen);
4286 break;
4287
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004288 case HTX_BLK_TLR:
4289 case HTX_BLK_EOM:
4290 stop = 1;
4291 break;
4292
4293 default:
4294 break;
4295 }
4296
4297 co_set_data(req, co_data(req) - vlen);
4298 count -= vlen;
4299 if (sz == vlen)
4300 blk = htx_remove_blk(htx, blk);
4301 else {
4302 htx_cut_data_blk(htx, blk, vlen);
4303 break;
4304 }
4305 }
4306
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004307 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004308 if (!stop) {
4309 si_cant_get(si);
4310 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0));
4311 }
4312
4313 /* return the result. */
4314 luaL_pushresult(&appctx->b);
4315 return 1;
4316}
4317
4318
4319/* If expected data not yet available, it returns a yield. This function
4320 * consumes the data in the buffer. It returns a string containing the
4321 * data. This string can be empty.
4322 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004323__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004324{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004325 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4326 struct stream_interface *si = appctx->appctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004327 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004328 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004329 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004330 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004331 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004332
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004333 /* Check for the end of the data. */
4334 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4335 luaL_pushresult(&appctx->b);
4336 return 1;
4337 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004338
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004339 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004340 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004341
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004342 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004343 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004344 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004345 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004346 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004347
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004348 /* End of data: commit the total strings and return. */
4349 if (ret < 0) {
4350 luaL_pushresult(&appctx->b);
4351 return 1;
4352 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004353
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004354 /* Ensure that the block 2 length is usable. */
4355 if (ret == 1)
4356 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004357
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004358 /* Copy the fisrt block caping to the length required. */
4359 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4360 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4361 luaL_addlstring(&appctx->b, blk1, len1);
4362 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004363
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004364 /* Copy the second block. */
4365 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4366 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4367 luaL_addlstring(&appctx->b, blk2, len2);
4368 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004369
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004370 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004371 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004372 luaL_pushresult(&appctx->b);
4373 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004374}
4375
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004376/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004377__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004378{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004379 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004380
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004381 /* Initialise the string catenation. */
4382 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004383
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004384 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
4385 return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0));
4386 else
4387 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004388}
4389
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004390/* If expected data not yet available, it returns a yield. This function
4391 * consumes the data in the buffer. It returns a string containing the
4392 * data. This string can be empty.
4393 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004394__LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx)
4395{
4396 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4397 struct stream_interface *si = appctx->appctx->owner;
4398 struct channel *req = si_oc(si);
4399 struct htx *htx;
4400 struct htx_blk *blk;
4401 size_t count;
4402 int len;
4403
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004404 htx = htx_from_buf(&req->buf);
4405 len = MAY_LJMP(luaL_checkinteger(L, 2));
4406 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004407 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004408 while (count && len && blk) {
4409 enum htx_blk_type type = htx_get_blk_type(blk);
4410 uint32_t sz = htx_get_blksz(blk);
4411 struct ist v;
4412 uint32_t vlen;
4413
Christopher Faulete461e342018-12-18 16:43:35 +01004414 if (type == HTX_BLK_EOM) {
4415 len = 0;
4416 break;
4417 }
4418
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004419 vlen = sz;
4420 if (len > 0 && vlen > len)
4421 vlen = len;
4422 if (vlen > count) {
4423 if (type != HTX_BLK_DATA)
4424 break;
4425 vlen = count;
4426 }
4427
4428 switch (type) {
4429 case HTX_BLK_UNUSED:
4430 break;
4431
4432 case HTX_BLK_DATA:
4433 v = htx_get_blk_value(htx, blk);
4434 luaL_addlstring(&appctx->b, v.ptr, vlen);
4435 break;
4436
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004437 case HTX_BLK_TLR:
4438 case HTX_BLK_EOM:
4439 len = 0;
4440 break;
4441
4442 default:
4443 break;
4444 }
4445
4446 co_set_data(req, co_data(req) - vlen);
4447 count -= vlen;
4448 if (len > 0)
4449 len -= vlen;
4450 if (sz == vlen)
4451 blk = htx_remove_blk(htx, blk);
4452 else {
4453 htx_cut_data_blk(htx, blk, vlen);
4454 break;
4455 }
4456 }
4457
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004458 htx_to_buf(htx, &req->buf);
4459
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004460 /* If we are no other data available, yield waiting for new data. */
4461 if (len) {
4462 if (len > 0) {
4463 lua_pushinteger(L, len);
4464 lua_replace(L, 2);
4465 }
4466 si_cant_get(si);
4467 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0));
4468 }
4469
4470 /* return the result. */
4471 luaL_pushresult(&appctx->b);
4472 return 1;
4473}
4474
4475/* If expected data not yet available, it returns a yield. This function
4476 * consumes the data in the buffer. It returns a string containing the
4477 * data. This string can be empty.
4478 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004479__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004480{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004481 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4482 struct stream_interface *si = appctx->appctx->owner;
4483 int len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004484 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02004485 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004486 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02004487 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004488 size_t len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004489
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004490 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004491 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004492
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004493 /* Data not yet available. return yield. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004494 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004495 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004496 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004497 }
4498
4499 /* End of data: commit the total strings and return. */
4500 if (ret < 0) {
4501 luaL_pushresult(&appctx->b);
4502 return 1;
4503 }
4504
4505 /* Ensure that the block 2 length is usable. */
4506 if (ret == 1)
4507 len2 = 0;
4508
4509 /* Copy the fisrt block caping to the length required. */
4510 if (len1 > len)
4511 len1 = len;
4512 luaL_addlstring(&appctx->b, blk1, len1);
4513 len -= len1;
4514
4515 /* Copy the second block. */
4516 if (len2 > len)
4517 len2 = len;
4518 luaL_addlstring(&appctx->b, blk2, len2);
4519 len -= len2;
4520
4521 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004522 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004523 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4524 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4525
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004526 /* If we are no other data available, yield waiting for new data. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004527 if (len > 0) {
4528 lua_pushinteger(L, len);
4529 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01004530 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004531 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004532 }
4533
4534 /* return the result. */
4535 luaL_pushresult(&appctx->b);
4536 return 1;
4537}
4538
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004539/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004540__LJMP static int hlua_applet_http_recv(lua_State *L)
4541{
4542 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4543 int len = -1;
4544
4545 /* Check arguments. */
4546 if (lua_gettop(L) > 2)
4547 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4548 if (lua_gettop(L) >= 2) {
4549 len = MAY_LJMP(luaL_checkinteger(L, 2));
4550 lua_pop(L, 1);
4551 }
4552
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004553 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4554 /* HTX version */
4555 lua_pushinteger(L, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004556
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004557 /* Initialise the string catenation. */
4558 luaL_buffinit(L, &appctx->b);
4559
4560 return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0));
4561 }
4562 else {
4563 /* Legacy HTTP version */
4564 /* Check the required length */
4565 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4566 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4567 lua_pushinteger(L, len);
4568
4569 /* Initialise the string catenation. */
4570 luaL_buffinit(L, &appctx->b);
4571
4572 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4573 }
4574}
4575
4576/* Append data in the output side of the buffer. This data is immediately
4577 * sent. The function returns the amount of data written. If the buffer
4578 * cannot contain the data, the function yields. The function returns -1
4579 * if the channel is closed.
4580 */
4581__LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx)
4582{
4583 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4584 struct stream_interface *si = appctx->appctx->owner;
4585 struct channel *res = si_ic(si);
4586 struct htx *htx = htx_from_buf(&res->buf);
4587 const char *data;
4588 size_t len;
4589 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4590 int max;
4591
Christopher Faulet4e963012019-07-03 11:39:30 +02004592 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004593 if (!max)
4594 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004595
4596 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4597
4598 /* Get the max amount of data which can write as input in the channel. */
4599 if (max > (len - l))
4600 max = len - l;
4601
4602 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004603 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004604 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004605
4606 /* update counters. */
4607 l += max;
4608 lua_pop(L, 1);
4609 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004610
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004611 /* If some data is not send, declares the situation to the
4612 * applet, and returns a yield.
4613 */
4614 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004615 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004616 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004617 si_rx_room_blk(si);
4618 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0));
4619 }
4620
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004621 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004622 return 1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004623}
4624
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004625/* Append data in the output side of the buffer. This data is immediately
4626 * sent. The function returns the amount of data written. If the buffer
4627 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004628 * if the channel is closed.
4629 */
4630__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4631{
4632 size_t len;
4633 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4634 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4635 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4636 struct stream_interface *si = appctx->appctx->owner;
4637 struct channel *chn = si_ic(si);
4638 int max;
4639
4640 /* Get the max amount of data which can write as input in the channel. */
4641 max = channel_recv_max(chn);
4642 if (max > (len - l))
4643 max = len - l;
4644
4645 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004646 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004647
4648 /* update counters. */
4649 l += max;
4650 lua_pop(L, 1);
4651 lua_pushinteger(L, l);
4652
4653 /* If some data is not send, declares the situation to the
4654 * applet, and returns a yield.
4655 */
4656 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004657 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004658 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004659 }
4660
4661 return 1;
4662}
4663
4664/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4665 * yield the LUA process, and resume it without checking the
4666 * input arguments.
4667 */
4668__LJMP static int hlua_applet_http_send(lua_State *L)
4669{
4670 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004671
4672 /* We want to send some data. Headers must be sent. */
4673 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4674 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4675 WILL_LJMP(lua_error(L));
4676 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004677
4678 if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) {
4679 /* HTX version */
4680 /* This interger is used for followinf the amount of data sent. */
4681 lua_pushinteger(L, 0);
4682
4683 return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0));
4684 }
4685 else {
4686 /* Legacy HTTP version */
4687 size_t len;
4688 char hex[10];
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004689
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004690 MAY_LJMP(luaL_checklstring(L, 2, &len));
4691
4692 /* If transfer encoding chunked is selected, we surround the data
4693 * by chunk data.
4694 */
4695 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4696 snprintf(hex, 9, "%x", (unsigned int)len);
4697 lua_pushfstring(L, "%s\r\n", hex);
4698 lua_insert(L, 2); /* swap the last 2 entries. */
4699 lua_pushstring(L, "\r\n");
4700 lua_concat(L, 3);
4701 }
4702
4703 /* This interger is used for followinf the amount of data sent. */
4704 lua_pushinteger(L, 0);
4705
4706 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4707 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004708}
4709
4710__LJMP static int hlua_applet_http_addheader(lua_State *L)
4711{
4712 const char *name;
4713 int ret;
4714
4715 MAY_LJMP(hlua_checkapplet_http(L, 1));
4716 name = MAY_LJMP(luaL_checkstring(L, 2));
4717 MAY_LJMP(luaL_checkstring(L, 3));
4718
4719 /* Push in the stack the "response" entry. */
4720 ret = lua_getfield(L, 1, "response");
4721 if (ret != LUA_TTABLE) {
4722 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4723 "is expected as an array. %s found", lua_typename(L, ret));
4724 WILL_LJMP(lua_error(L));
4725 }
4726
4727 /* check if the header is already registered if it is not
4728 * the case, register it.
4729 */
4730 ret = lua_getfield(L, -1, name);
4731 if (ret == LUA_TNIL) {
4732
4733 /* Entry not found. */
4734 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4735
4736 /* Insert the new header name in the array in the top of the stack.
4737 * It left the new array in the top of the stack.
4738 */
4739 lua_newtable(L);
4740 lua_pushvalue(L, 2);
4741 lua_pushvalue(L, -2);
4742 lua_settable(L, -4);
4743
4744 } else if (ret != LUA_TTABLE) {
4745
4746 /* corruption error. */
4747 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4748 "is expected as an array. %s found", name, lua_typename(L, ret));
4749 WILL_LJMP(lua_error(L));
4750 }
4751
4752 /* Now the top od thestack is an array of values. We push
4753 * the header value as new entry.
4754 */
4755 lua_pushvalue(L, 3);
4756 ret = lua_rawlen(L, -2);
4757 lua_rawseti(L, -2, ret + 1);
4758 lua_pushboolean(L, 1);
4759 return 1;
4760}
4761
4762__LJMP static int hlua_applet_http_status(lua_State *L)
4763{
4764 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4765 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004766 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004767
4768 if (status < 100 || status > 599) {
4769 lua_pushboolean(L, 0);
4770 return 1;
4771 }
4772
4773 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004774 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004775 lua_pushboolean(L, 1);
4776 return 1;
4777}
4778
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004779
4780__LJMP static int hlua_applet_htx_send_response(lua_State *L)
4781{
4782 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4783 struct stream_interface *si = appctx->appctx->owner;
4784 struct channel *res = si_ic(si);
4785 struct htx *htx;
4786 struct htx_sl *sl;
4787 struct h1m h1m;
4788 const char *status, *reason;
4789 const char *name, *value;
4790 size_t nlen, vlen;
4791 unsigned int flags;
4792
4793 /* Send the message at once. */
4794 htx = htx_from_buf(&res->buf);
4795 h1m_init_res(&h1m);
4796
4797 /* Use the same http version than the request. */
4798 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4799 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4800 if (reason == NULL)
4801 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4802 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4803 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4804 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4805 }
4806 else {
4807 flags = HTX_SL_F_IS_RESP;
4808 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4809 }
4810 if (!sl) {
4811 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
4812 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4813 WILL_LJMP(lua_error(L));
4814 }
4815 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4816
4817 /* Get the array associated to the field "response" in the object AppletHTTP. */
4818 lua_pushvalue(L, 0);
4819 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4820 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4821 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4822 WILL_LJMP(lua_error(L));
4823 }
4824
4825 /* Browse the list of headers. */
4826 lua_pushnil(L);
4827 while(lua_next(L, -2) != 0) {
4828 /* We expect a string as -2. */
4829 if (lua_type(L, -2) != LUA_TSTRING) {
4830 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4831 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4832 lua_typename(L, lua_type(L, -2)));
4833 WILL_LJMP(lua_error(L));
4834 }
4835 name = lua_tolstring(L, -2, &nlen);
4836
4837 /* We expect an array as -1. */
4838 if (lua_type(L, -1) != LUA_TTABLE) {
4839 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4840 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4841 name,
4842 lua_typename(L, lua_type(L, -1)));
4843 WILL_LJMP(lua_error(L));
4844 }
4845
4846 /* Browse the table who is on the top of the stack. */
4847 lua_pushnil(L);
4848 while(lua_next(L, -2) != 0) {
4849 int id;
4850
4851 /* We expect a number as -2. */
4852 if (lua_type(L, -2) != LUA_TNUMBER) {
4853 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4854 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4855 name,
4856 lua_typename(L, lua_type(L, -2)));
4857 WILL_LJMP(lua_error(L));
4858 }
4859 id = lua_tointeger(L, -2);
4860
4861 /* We expect a string as -2. */
4862 if (lua_type(L, -1) != LUA_TSTRING) {
4863 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4864 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4865 name, id,
4866 lua_typename(L, lua_type(L, -1)));
4867 WILL_LJMP(lua_error(L));
4868 }
4869 value = lua_tolstring(L, -1, &vlen);
4870
4871 /* Simple Protocol checks. */
4872 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
4873 h1_parse_xfer_enc_header(&h1m, ist2(name, nlen));
4874 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4875 struct ist v = ist2(value, vlen);
4876 int ret;
4877
4878 ret = h1_parse_cont_len_header(&h1m, &v);
4879 if (ret < 0) {
4880 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
4881 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4882 name);
4883 WILL_LJMP(lua_error(L));
4884 }
4885 else if (ret == 0)
4886 goto next; /* Skip it */
4887 }
4888
4889 /* Add a new header */
4890 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4891 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
4892 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4893 name);
4894 WILL_LJMP(lua_error(L));
4895 }
4896 next:
4897 /* Remove the array from the stack, and get next element with a remaining string. */
4898 lua_pop(L, 1);
4899 }
4900
4901 /* Remove the array from the stack, and get next element with a remaining string. */
4902 lua_pop(L, 1);
4903 }
4904
4905 if (h1m.flags & H1_MF_CHNK)
4906 h1m.flags &= ~H1_MF_CLEN;
4907 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4908 h1m.flags |= H1_MF_XFER_LEN;
4909
4910 /* Uset HTX start-line flags */
4911 if (h1m.flags & H1_MF_XFER_ENC)
4912 flags |= HTX_SL_F_XFER_ENC;
4913 if (h1m.flags & H1_MF_XFER_LEN) {
4914 flags |= HTX_SL_F_XFER_LEN;
4915 if (h1m.flags & H1_MF_CHNK)
4916 flags |= HTX_SL_F_CHNK;
4917 else if (h1m.flags & H1_MF_CLEN)
4918 flags |= HTX_SL_F_CLEN;
4919 if (h1m.body_len == 0)
4920 flags |= HTX_SL_F_BODYLESS;
4921 }
4922 sl->flags |= flags;
4923
4924 /* If we dont have a content-length set, and the HTTP version is 1.1
4925 * and the status code implies the presence of a message body, we must
4926 * announce a transfer encoding chunked. This is required by haproxy
4927 * for the keepalive compliance. If the applet annouces a transfer-encoding
4928 * chunked itslef, don't do anything.
4929 */
4930 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4931 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4932 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4933 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4934 /* Add a new header */
4935 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4936 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4937 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
4938 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4939 WILL_LJMP(lua_error(L));
4940 }
4941 }
4942
4943 /* Finalize headers. */
4944 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4945 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
4946 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4947 WILL_LJMP(lua_error(L));
4948 }
4949
4950 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4951 b_reset(&res->buf);
4952 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4953 WILL_LJMP(lua_error(L));
4954 }
4955
4956 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004957 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004958
4959 /* Headers sent, set the flag. */
4960 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4961 return 0;
4962
4963}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004964/* We will build the status line and the headers of the HTTP response.
4965 * We will try send at once if its not possible, we give back the hand
4966 * waiting for more room.
4967 */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004968__LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4969{
4970 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4971 struct stream_interface *si = appctx->appctx->owner;
4972 struct channel *res = si_ic(si);
4973
4974 if (co_data(res)) {
4975 si_rx_room_blk(si);
4976 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0));
4977 }
4978 return MAY_LJMP(hlua_applet_htx_send_response(L));
4979}
4980
4981
4982__LJMP static int hlua_applet_htx_start_response(lua_State *L)
4983{
4984 return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0));
4985}
4986
4987/* We will build the status line and the headers of the HTTP response.
4988 * We will try send at once if its not possible, we give back the hand
4989 * waiting for more room.
4990 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004991__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4992{
4993 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4994 struct stream_interface *si = appctx->appctx->owner;
4995 struct channel *chn = si_ic(si);
4996 int ret;
4997 size_t len;
4998 const char *msg;
4999
5000 /* Get the message as the first argument on the stack. */
5001 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
5002
5003 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02005004 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005005
5006 /* if ret == -2 or -3 the channel closed or the message si too
5007 * big for the buffers.
5008 */
5009 if (ret == -2 || ret == -3) {
5010 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
5011 WILL_LJMP(lua_error(L));
5012 }
5013
5014 /* If ret is -1, we dont have room in the buffer, so we yield. */
5015 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01005016 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02005017 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005018 }
5019
5020 /* Headers sent, set the flag. */
5021 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
5022 return 0;
5023}
5024
5025__LJMP static int hlua_applet_http_start_response(lua_State *L)
5026{
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005027 struct buffer *tmp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005028 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005029 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02005030 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005031 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02005032 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005033 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005034 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005035 int hdr_chunked = 0;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005036 const char *reason;
5037
5038 if (IS_HTX_STRM(si_strm(appctx->appctx->owner)))
5039 return MAY_LJMP(hlua_applet_htx_start_response(L));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005040
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005041 reason = appctx->appctx->ctx.hlua_apphttp.reason;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005042 if (reason == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02005043 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005044
Christopher Faulet9c832fc2018-12-14 13:34:05 +01005045 tmp = get_trash_chunk();
5046
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005047 /* Use the same http version than the request. */
5048 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01005049 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005050 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005051 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005052
5053 /* Get the array associated to the field "response" in the object AppletHTTP. */
5054 lua_pushvalue(L, 0);
5055 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
5056 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
5057 appctx->appctx->rule->arg.hlua_rule->fcn.name);
5058 WILL_LJMP(lua_error(L));
5059 }
5060
5061 /* Browse the list of headers. */
5062 lua_pushnil(L);
5063 while(lua_next(L, -2) != 0) {
5064
5065 /* We expect a string as -2. */
5066 if (lua_type(L, -2) != LUA_TSTRING) {
5067 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
5068 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5069 lua_typename(L, lua_type(L, -2)));
5070 WILL_LJMP(lua_error(L));
5071 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005072 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005073
5074 /* We expect an array as -1. */
5075 if (lua_type(L, -1) != LUA_TTABLE) {
5076 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
5077 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5078 name,
5079 lua_typename(L, lua_type(L, -1)));
5080 WILL_LJMP(lua_error(L));
5081 }
5082
5083 /* Browse the table who is on the top of the stack. */
5084 lua_pushnil(L);
5085 while(lua_next(L, -2) != 0) {
5086
5087 /* We expect a number as -2. */
5088 if (lua_type(L, -2) != LUA_TNUMBER) {
5089 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
5090 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5091 name,
5092 lua_typename(L, lua_type(L, -2)));
5093 WILL_LJMP(lua_error(L));
5094 }
5095 id = lua_tointeger(L, -2);
5096
5097 /* We expect a string as -2. */
5098 if (lua_type(L, -1) != LUA_TSTRING) {
5099 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
5100 appctx->appctx->rule->arg.hlua_rule->fcn.name,
5101 name, id,
5102 lua_typename(L, lua_type(L, -1)));
5103 WILL_LJMP(lua_error(L));
5104 }
Willy Tarreaua3294632017-08-23 11:24:47 +02005105 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005106
5107 /* Catenate a new header. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005108 if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) {
5109 memcpy(tmp->area + tmp->data, name, name_len);
5110 tmp->data += name_len;
5111 tmp->area[tmp->data++] = ':';
5112 tmp->area[tmp->data++] = ' ';
Willy Tarreaua3294632017-08-23 11:24:47 +02005113
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005114 memcpy(tmp->area + tmp->data, value,
5115 value_len);
5116 tmp->data += value_len;
5117 tmp->area[tmp->data++] = '\r';
5118 tmp->area[tmp->data++] = '\n';
Willy Tarreaua3294632017-08-23 11:24:47 +02005119 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005120
5121 /* Protocol checks. */
5122
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005123 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005124 * is done without control. If it contains a bad value,
5125 * the content-length remains negative so that we can
5126 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005127 */
Willy Tarreaua3294632017-08-23 11:24:47 +02005128 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005129 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005130
5131 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02005132 if (name_len == 17 && value_len == 7 &&
5133 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005134 strcasecmp("chunked", value) == 0)
5135 hdr_chunked = 1;
5136
5137 /* Remove the array from the stack, and get next element with a remaining string. */
5138 lua_pop(L, 1);
5139 }
5140
5141 /* Remove the array from the stack, and get next element with a remaining string. */
5142 lua_pop(L, 1);
5143 }
5144
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005145 /* If we dont have a content-length set, and the HTTP version is 1.1
5146 * and the status code implies the presence of a message body, we must
5147 * announce a transfer encoding chunked. This is required by haproxy
5148 * for the keepalive compliance. If the applet annouces a transfer-encoding
5149 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005150 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02005151 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02005152 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
5153 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
5154 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
5155 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005156 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
5157 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
5158 }
5159
5160 /* Finalize headers. */
5161 chunk_appendf(tmp, "\r\n");
5162
5163 /* Remove the last entry and the array of headers */
5164 lua_pop(L, 2);
5165
5166 /* Push the headers block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005167 lua_pushlstring(L, tmp->area, tmp->data);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005168
5169 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
5170}
5171
5172/*
5173 *
5174 *
5175 * Class HTTP
5176 *
5177 *
5178 */
5179
5180/* Returns a struct hlua_txn if the stack entry "ud" is
5181 * a class stream, otherwise it throws an error.
5182 */
5183__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
5184{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005185 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005186}
5187
5188/* This function creates and push in the stack a HTTP object
5189 * according with a current TXN.
5190 */
5191static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
5192{
5193 struct hlua_txn *htxn;
5194
5195 /* Check stack size. */
5196 if (!lua_checkstack(L, 3))
5197 return 0;
5198
5199 /* Create the object: obj[0] = userdata.
5200 * Note that the base of the Converters object is the
5201 * same than the TXN object.
5202 */
5203 lua_newtable(L);
5204 htxn = lua_newuserdata(L, sizeof(*htxn));
5205 lua_rawseti(L, -2, 0);
5206
5207 htxn->s = txn->s;
5208 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02005209 htxn->dir = txn->dir;
5210 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005211
5212 /* Pop a class stream metatable and affect it to the table. */
5213 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
5214 lua_setmetatable(L, -2);
5215
5216 return 1;
5217}
5218
5219/* This function creates ans returns an array of HTTP headers.
5220 * This function does not fails. It is used as wrapper with the
5221 * 2 following functions.
5222 */
5223__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5224{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005225 /* Create the table. */
5226 lua_newtable(L);
5227
5228 if (!htxn->s->txn)
5229 return 1;
5230
Christopher Faulet724a12c2018-12-13 22:12:15 +01005231 if (IS_HTX_STRM(htxn->s)) {
5232 /* HTX version */
5233 struct htx *htx = htxbuf(&msg->chn->buf);
5234 int32_t pos;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005235
Christopher Fauleta3f15502019-05-13 15:27:23 +02005236 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet724a12c2018-12-13 22:12:15 +01005237 struct htx_blk *blk = htx_get_blk(htx, pos);
5238 enum htx_blk_type type = htx_get_blk_type(blk);
5239 struct ist n, v;
5240 int len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005241
Christopher Faulet724a12c2018-12-13 22:12:15 +01005242 if (type == HTX_BLK_HDR) {
5243 n = htx_get_blk_name(htx,blk);
5244 v = htx_get_blk_value(htx, blk);
5245 }
5246 else if (type == HTX_BLK_EOH)
5247 break;
5248 else
5249 continue;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005250
Christopher Faulet724a12c2018-12-13 22:12:15 +01005251 /* Check for existing entry:
5252 * assume that the table is on the top of the stack, and
5253 * push the key in the stack, the function lua_gettable()
5254 * perform the lookup.
5255 */
5256 lua_pushlstring(L, n.ptr, n.len);
5257 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005258
Christopher Faulet724a12c2018-12-13 22:12:15 +01005259 switch (lua_type(L, -1)) {
5260 case LUA_TNIL:
5261 /* Table not found, create it. */
5262 lua_pop(L, 1); /* remove the nil value. */
5263 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
5264 lua_newtable(L); /* create and push empty table. */
5265 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5266 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5267 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5268 break;
5269
5270 case LUA_TTABLE:
5271 /* Entry found: push the value in the table. */
5272 len = lua_rawlen(L, -1);
5273 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
5274 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5275 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5276 break;
5277
5278 default:
5279 /* Other cases are errors. */
5280 hlua_pusherror(L, "internal error during the parsing of headers.");
5281 WILL_LJMP(lua_error(L));
5282 }
5283 }
5284 }
5285 else {
5286 /* Legacy HTTP version */
5287 const char *cur_ptr, *cur_next, *p;
5288 int old_idx, cur_idx;
5289 struct hdr_idx_elem *cur_hdr;
5290 const char *hn, *hv;
5291 int hnl, hvl;
5292 const char *in;
5293 char *out;
5294 int len;
5295
5296 /* Build array of headers. */
5297 old_idx = 0;
5298 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
5299
5300 while (1) {
5301 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
5302 if (!cur_idx)
5303 break;
5304 old_idx = cur_idx;
5305
5306 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
5307 cur_ptr = cur_next;
5308 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
5309
5310 /* Now we have one full header at cur_ptr of len cur_hdr->len,
5311 * and the next header starts at cur_next. We'll check
5312 * this header in the list as well as against the default
5313 * rule.
5314 */
5315
5316 /* look for ': *'. */
5317 hn = cur_ptr;
5318 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
5319 if (p >= cur_ptr+cur_hdr->len)
5320 continue;
5321 hnl = p - hn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005322 p++;
Christopher Faulet724a12c2018-12-13 22:12:15 +01005323 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
5324 p++;
5325 if (p >= cur_ptr+cur_hdr->len)
5326 continue;
5327 hv = p;
5328 hvl = cur_ptr+cur_hdr->len-p;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005329
Christopher Faulet724a12c2018-12-13 22:12:15 +01005330 /* Lowercase the key. Don't check the size of trash, it have
5331 * the size of one buffer and the input data contains in one
5332 * buffer.
5333 */
5334 out = trash.area;
5335 for (in=hn; in<hn+hnl; in++, out++)
5336 *out = tolower(*in);
5337 *out = '\0';
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005338
Christopher Faulet724a12c2018-12-13 22:12:15 +01005339 /* Check for existing entry:
5340 * assume that the table is on the top of the stack, and
5341 * push the key in the stack, the function lua_gettable()
5342 * perform the lookup.
5343 */
5344 lua_pushlstring(L, trash.area, hnl);
5345 lua_gettable(L, -2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005346
Christopher Faulet724a12c2018-12-13 22:12:15 +01005347 switch (lua_type(L, -1)) {
5348 case LUA_TNIL:
5349 /* Table not found, create it. */
5350 lua_pop(L, 1); /* remove the nil value. */
5351 lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */
5352 lua_newtable(L); /* create and push empty table. */
5353 lua_pushlstring(L, hv, hvl); /* push header value. */
5354 lua_rawseti(L, -2, 0); /* index header value (pop it). */
5355 lua_rawset(L, -3); /* index new table with header name (pop the values). */
5356 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005357
Christopher Faulet724a12c2018-12-13 22:12:15 +01005358 case LUA_TTABLE:
5359 /* Entry found: push the value in the table. */
5360 len = lua_rawlen(L, -1);
5361 lua_pushlstring(L, hv, hvl); /* push header value. */
5362 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
5363 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5364 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005365
Christopher Faulet724a12c2018-12-13 22:12:15 +01005366 default:
5367 /* Other cases are errors. */
5368 hlua_pusherror(L, "internal error during the parsing of headers.");
5369 WILL_LJMP(lua_error(L));
5370 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005371 }
5372 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005373 return 1;
5374}
5375
5376__LJMP static int hlua_http_req_get_headers(lua_State *L)
5377{
5378 struct hlua_txn *htxn;
5379
5380 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5381 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5382
Christopher Faulet2351ca22019-07-26 16:31:34 +02005383 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005384 WILL_LJMP(lua_error(L));
5385
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005386 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
5387}
5388
5389__LJMP static int hlua_http_res_get_headers(lua_State *L)
5390{
5391 struct hlua_txn *htxn;
5392
5393 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5394 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5395
Christopher Faulet2351ca22019-07-26 16:31:34 +02005396 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005397 WILL_LJMP(lua_error(L));
5398
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005399 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
5400}
5401
5402/* This function replace full header, or just a value in
5403 * the request or in the response. It is a wrapper fir the
5404 * 4 following functions.
5405 */
5406__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
5407 struct http_msg *msg, int action)
5408{
5409 size_t name_len;
5410 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5411 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5412 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Dragan Dosen26743032019-04-30 15:54:36 +02005413 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005414
Dragan Dosen26743032019-04-30 15:54:36 +02005415 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005416 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5417
Christopher Faulet5a8549e2019-06-17 13:36:06 +02005418 if (IS_HTX_STRM(htxn->s)) {
5419 struct htx *htx = htxbuf(&msg->chn->buf);
5420
5421 htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action);
5422 }
5423 else
5424 http_transform_header_str(htxn->s, msg, name, name_len, value, re, action);
Dragan Dosen26743032019-04-30 15:54:36 +02005425 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005426 return 0;
5427}
5428
5429__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5430{
5431 struct hlua_txn *htxn;
5432
5433 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5434 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5435
Christopher Faulet2351ca22019-07-26 16:31:34 +02005436 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005437 WILL_LJMP(lua_error(L));
5438
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005439 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
5440}
5441
5442__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5443{
5444 struct hlua_txn *htxn;
5445
5446 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5447 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5448
Christopher Faulet2351ca22019-07-26 16:31:34 +02005449 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005450 WILL_LJMP(lua_error(L));
5451
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005452 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
5453}
5454
5455__LJMP static int hlua_http_req_rep_val(lua_State *L)
5456{
5457 struct hlua_txn *htxn;
5458
5459 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5460 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5461
Christopher Faulet2351ca22019-07-26 16:31:34 +02005462 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005463 WILL_LJMP(lua_error(L));
5464
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005465 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
5466}
5467
5468__LJMP static int hlua_http_res_rep_val(lua_State *L)
5469{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005470 struct hlua_txn *htxn;
5471
5472 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5473 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5474
Christopher Faulet2351ca22019-07-26 16:31:34 +02005475 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005476 WILL_LJMP(lua_error(L));
5477
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02005478 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005479}
5480
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005481/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005482 * It is a wrapper for the 2 following functions.
5483 */
5484__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5485{
5486 size_t len;
5487 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005488
Christopher Faulet724a12c2018-12-13 22:12:15 +01005489 if (IS_HTX_STRM(htxn->s)) {
5490 /* HTX version */
5491 struct htx *htx = htxbuf(&msg->chn->buf);
5492 struct http_hdr_ctx ctx;
5493
5494 ctx.blk = NULL;
5495 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5496 http_remove_header(htx, &ctx);
5497 }
5498 else {
5499 /* Legacy HTTP version */
5500 struct hdr_ctx ctx;
5501 struct http_txn *txn = htxn->s->txn;
5502
5503 ctx.idx = 0;
5504 while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx))
5505 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5506 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005507 return 0;
5508}
5509
5510__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5511{
5512 struct hlua_txn *htxn;
5513
5514 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5515 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5516
Christopher Faulet2351ca22019-07-26 16:31:34 +02005517 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005518 WILL_LJMP(lua_error(L));
5519
Willy Tarreaueee5b512015-04-03 23:46:31 +02005520 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005521}
5522
5523__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5524{
5525 struct hlua_txn *htxn;
5526
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005527 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005528 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5529
Christopher Faulet2351ca22019-07-26 16:31:34 +02005530 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005531 WILL_LJMP(lua_error(L));
5532
Willy Tarreaueee5b512015-04-03 23:46:31 +02005533 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005534}
5535
5536/* This function adds an header. It is a wrapper used by
5537 * the 2 following functions.
5538 */
5539__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
5540{
5541 size_t name_len;
5542 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5543 size_t value_len;
5544 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
5545 char *p;
5546
Christopher Faulet724a12c2018-12-13 22:12:15 +01005547 if (IS_HTX_STRM(htxn->s)) {
5548 /* HTX version */
5549 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005550
Christopher Faulet724a12c2018-12-13 22:12:15 +01005551 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5552 ist2(value, value_len)));
5553 }
5554 else {
5555 /* Legacy HTTP version */
5556 /* Check length. */
5557 trash.data = value_len + name_len + 2;
5558 if (trash.data > trash.size)
5559 return 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005560
Christopher Faulet724a12c2018-12-13 22:12:15 +01005561 /* Creates the header string. */
5562 p = trash.area;
5563 memcpy(p, name, name_len);
5564 p += name_len;
5565 *p = ':';
5566 p++;
5567 *p = ' ';
5568 p++;
5569 memcpy(p, value, value_len);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005570
Christopher Faulet724a12c2018-12-13 22:12:15 +01005571 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
5572 trash.area, trash.data) != 0);
5573 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005574 return 0;
5575}
5576
5577__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5578{
5579 struct hlua_txn *htxn;
5580
5581 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5582 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5583
Christopher Faulet2351ca22019-07-26 16:31:34 +02005584 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005585 WILL_LJMP(lua_error(L));
5586
Willy Tarreaueee5b512015-04-03 23:46:31 +02005587 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005588}
5589
5590__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5591{
5592 struct hlua_txn *htxn;
5593
5594 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5595 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5596
Christopher Faulet2351ca22019-07-26 16:31:34 +02005597 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005598 WILL_LJMP(lua_error(L));
5599
Willy Tarreaueee5b512015-04-03 23:46:31 +02005600 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005601}
5602
5603static int hlua_http_req_set_hdr(lua_State *L)
5604{
5605 struct hlua_txn *htxn;
5606
5607 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5608 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5609
Christopher Faulet2351ca22019-07-26 16:31:34 +02005610 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005611 WILL_LJMP(lua_error(L));
5612
Willy Tarreaueee5b512015-04-03 23:46:31 +02005613 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
5614 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005615}
5616
5617static int hlua_http_res_set_hdr(lua_State *L)
5618{
5619 struct hlua_txn *htxn;
5620
5621 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5622 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5623
Christopher Faulet2351ca22019-07-26 16:31:34 +02005624 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005625 WILL_LJMP(lua_error(L));
5626
Willy Tarreaueee5b512015-04-03 23:46:31 +02005627 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
5628 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005629}
5630
5631/* This function set the method. */
5632static int hlua_http_req_set_meth(lua_State *L)
5633{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005634 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005635 size_t name_len;
5636 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005637
Christopher Faulet2351ca22019-07-26 16:31:34 +02005638 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005639 WILL_LJMP(lua_error(L));
5640
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005641 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005642 return 1;
5643}
5644
5645/* This function set the method. */
5646static int hlua_http_req_set_path(lua_State *L)
5647{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005648 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005649 size_t name_len;
5650 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005651
Christopher Faulet2351ca22019-07-26 16:31:34 +02005652 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005653 WILL_LJMP(lua_error(L));
5654
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005655 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005656 return 1;
5657}
5658
5659/* This function set the query-string. */
5660static int hlua_http_req_set_query(lua_State *L)
5661{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005662 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005663 size_t name_len;
5664 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005665
Christopher Faulet2351ca22019-07-26 16:31:34 +02005666 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005667 WILL_LJMP(lua_error(L));
5668
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005669 /* Check length. */
5670 if (name_len > trash.size - 1) {
5671 lua_pushboolean(L, 0);
5672 return 1;
5673 }
5674
5675 /* Add the mark question as prefix. */
5676 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005677 trash.area[trash.data++] = '?';
5678 memcpy(trash.area + trash.data, name, name_len);
5679 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005680
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005681 lua_pushboolean(L,
5682 http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005683 return 1;
5684}
5685
5686/* This function set the uri. */
5687static int hlua_http_req_set_uri(lua_State *L)
5688{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005689 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005690 size_t name_len;
5691 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005692
Christopher Faulet2351ca22019-07-26 16:31:34 +02005693 if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005694 WILL_LJMP(lua_error(L));
5695
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005696 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005697 return 1;
5698}
5699
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005700/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005701static int hlua_http_res_set_status(lua_State *L)
5702{
5703 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5704 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005705 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005706
Christopher Faulet2351ca22019-07-26 16:31:34 +02005707 if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY))
Christopher Fauletdc2ee272019-07-26 16:17:01 +02005708 WILL_LJMP(lua_error(L));
5709
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005710 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005711 return 0;
5712}
5713
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005714/*
5715 *
5716 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005717 * Class TXN
5718 *
5719 *
5720 */
5721
5722/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005723 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005724 */
5725__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5726{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005727 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005728}
5729
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005730__LJMP static int hlua_set_var(lua_State *L)
5731{
5732 struct hlua_txn *htxn;
5733 const char *name;
5734 size_t len;
5735 struct sample smp;
5736
5737 MAY_LJMP(check_args(L, 3, "set_var"));
5738
5739 /* It is useles to retrieve the stream, but this function
5740 * runs only in a stream context.
5741 */
5742 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5743 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5744
5745 /* Converts the third argument in a sample. */
Amaury Denoyelleca492f32020-10-29 17:21:20 +01005746 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005747 hlua_lua2smp(L, 3, &smp);
5748
5749 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005750 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005751 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005752 return 0;
5753}
5754
Christopher Faulet85d79c92016-11-09 16:54:56 +01005755__LJMP static int hlua_unset_var(lua_State *L)
5756{
5757 struct hlua_txn *htxn;
5758 const char *name;
5759 size_t len;
5760 struct sample smp;
5761
5762 MAY_LJMP(check_args(L, 2, "unset_var"));
5763
5764 /* It is useles to retrieve the stream, but this function
5765 * runs only in a stream context.
5766 */
5767 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5768 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5769
5770 /* Unset the variable. */
5771 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5772 vars_unset_by_name(name, len, &smp);
5773 return 0;
5774}
5775
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005776__LJMP static int hlua_get_var(lua_State *L)
5777{
5778 struct hlua_txn *htxn;
5779 const char *name;
5780 size_t len;
5781 struct sample smp;
5782
5783 MAY_LJMP(check_args(L, 2, "get_var"));
5784
5785 /* It is useles to retrieve the stream, but this function
5786 * runs only in a stream context.
5787 */
5788 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5789 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5790
Willy Tarreau7560dd42016-03-10 16:28:58 +01005791 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005792 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005793 lua_pushnil(L);
5794 return 1;
5795 }
5796
5797 return hlua_smp2lua(L, &smp);
5798}
5799
Willy Tarreau59551662015-03-10 14:23:13 +01005800__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005801{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005802 struct hlua *hlua;
5803
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005804 MAY_LJMP(check_args(L, 2, "set_priv"));
5805
Willy Tarreau87b09662015-04-03 00:22:06 +02005806 /* It is useles to retrieve the stream, but this function
5807 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005808 */
5809 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005810 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005811
5812 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005813 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005814
5815 /* Get and store new value. */
5816 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5817 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5818
5819 return 0;
5820}
5821
Willy Tarreau59551662015-03-10 14:23:13 +01005822__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005823{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005824 struct hlua *hlua;
5825
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005826 MAY_LJMP(check_args(L, 1, "get_priv"));
5827
Willy Tarreau87b09662015-04-03 00:22:06 +02005828 /* It is useles to retrieve the stream, but this function
5829 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005830 */
5831 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005832 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005833
5834 /* Push configuration index in the stack. */
5835 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5836
5837 return 1;
5838}
5839
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005840/* Create stack entry containing a class TXN. This function
5841 * return 0 if the stack does not contains free slots,
5842 * otherwise it returns 1.
5843 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005844static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005845{
Willy Tarreaude491382015-04-06 11:04:28 +02005846 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005847
5848 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005849 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005850 return 0;
5851
5852 /* NOTE: The allocation never fails. The failure
5853 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005854 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005855 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005856 /* Create the object: obj[0] = userdata. */
5857 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005858 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005859 lua_rawseti(L, -2, 0);
5860
Willy Tarreaude491382015-04-06 11:04:28 +02005861 htxn->s = s;
5862 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005863 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005864 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005865
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005866 /* Create the "f" field that contains a list of fetches. */
5867 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005868 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005869 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005870 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005871
5872 /* Create the "sf" field that contains a list of stringsafe fetches. */
5873 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005874 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005875 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005876 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005877
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005878 /* Create the "c" field that contains a list of converters. */
5879 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005880 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005881 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005882 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005883
5884 /* Create the "sc" field that contains a list of stringsafe converters. */
5885 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005886 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005887 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005888 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005889
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005890 /* Create the "req" field that contains the request channel object. */
5891 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005892 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005893 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005894 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005895
5896 /* Create the "res" field that contains the response channel object. */
5897 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005898 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005899 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005900 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005901
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005902 /* Creates the HTTP object is the current proxy allows http. */
5903 lua_pushstring(L, "http");
5904 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005905 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005906 return 0;
5907 }
5908 else
5909 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005910 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005911
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005912 /* Pop a class sesison metatable and affect it to the userdata. */
5913 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5914 lua_setmetatable(L, -2);
5915
5916 return 1;
5917}
5918
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005919__LJMP static int hlua_txn_deflog(lua_State *L)
5920{
5921 const char *msg;
5922 struct hlua_txn *htxn;
5923
5924 MAY_LJMP(check_args(L, 2, "deflog"));
5925 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5926 msg = MAY_LJMP(luaL_checkstring(L, 2));
5927
5928 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5929 return 0;
5930}
5931
5932__LJMP static int hlua_txn_log(lua_State *L)
5933{
5934 int level;
5935 const char *msg;
5936 struct hlua_txn *htxn;
5937
5938 MAY_LJMP(check_args(L, 3, "log"));
5939 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5940 level = MAY_LJMP(luaL_checkinteger(L, 2));
5941 msg = MAY_LJMP(luaL_checkstring(L, 3));
5942
5943 if (level < 0 || level >= NB_LOG_LEVELS)
5944 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5945
5946 hlua_sendlog(htxn->s->be, level, msg);
5947 return 0;
5948}
5949
5950__LJMP static int hlua_txn_log_debug(lua_State *L)
5951{
5952 const char *msg;
5953 struct hlua_txn *htxn;
5954
5955 MAY_LJMP(check_args(L, 2, "Debug"));
5956 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5957 msg = MAY_LJMP(luaL_checkstring(L, 2));
5958 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5959 return 0;
5960}
5961
5962__LJMP static int hlua_txn_log_info(lua_State *L)
5963{
5964 const char *msg;
5965 struct hlua_txn *htxn;
5966
5967 MAY_LJMP(check_args(L, 2, "Info"));
5968 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5969 msg = MAY_LJMP(luaL_checkstring(L, 2));
5970 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5971 return 0;
5972}
5973
5974__LJMP static int hlua_txn_log_warning(lua_State *L)
5975{
5976 const char *msg;
5977 struct hlua_txn *htxn;
5978
5979 MAY_LJMP(check_args(L, 2, "Warning"));
5980 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5981 msg = MAY_LJMP(luaL_checkstring(L, 2));
5982 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5983 return 0;
5984}
5985
5986__LJMP static int hlua_txn_log_alert(lua_State *L)
5987{
5988 const char *msg;
5989 struct hlua_txn *htxn;
5990
5991 MAY_LJMP(check_args(L, 2, "Alert"));
5992 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5993 msg = MAY_LJMP(luaL_checkstring(L, 2));
5994 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5995 return 0;
5996}
5997
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005998__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5999{
6000 struct hlua_txn *htxn;
6001 int ll;
6002
6003 MAY_LJMP(check_args(L, 2, "set_loglevel"));
6004 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6005 ll = MAY_LJMP(luaL_checkinteger(L, 2));
6006
6007 if (ll < 0 || ll > 7)
6008 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
6009
6010 htxn->s->logs.level = ll;
6011 return 0;
6012}
6013
6014__LJMP static int hlua_txn_set_tos(lua_State *L)
6015{
6016 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006017 int tos;
6018
6019 MAY_LJMP(check_args(L, 2, "set_tos"));
6020 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6021 tos = MAY_LJMP(luaL_checkinteger(L, 2));
6022
Willy Tarreau1a18b542018-12-11 16:37:42 +01006023 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006024 return 0;
6025}
6026
6027__LJMP static int hlua_txn_set_mark(lua_State *L)
6028{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006029 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006030 int mark;
6031
6032 MAY_LJMP(check_args(L, 2, "set_mark"));
6033 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6034 mark = MAY_LJMP(luaL_checkinteger(L, 2));
6035
Lukas Tribusb59291a2019-08-11 18:03:45 +02006036 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01006037 return 0;
6038}
6039
Patrick Hemmer268a7072018-05-11 12:52:31 -04006040__LJMP static int hlua_txn_set_priority_class(lua_State *L)
6041{
6042 struct hlua_txn *htxn;
6043
6044 MAY_LJMP(check_args(L, 2, "set_priority_class"));
6045 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6046 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
6047 return 0;
6048}
6049
6050__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
6051{
6052 struct hlua_txn *htxn;
6053
6054 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
6055 htxn = MAY_LJMP(hlua_checktxn(L, 1));
6056 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
6057 return 0;
6058}
6059
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006060/* This function is an Lua binding that send pending data
6061 * to the client, and close the stream interface.
6062 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006063__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006064{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006065 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006066 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01006067 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006068
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006069 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006070 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006071 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006072
Thierry FOURNIERab00df62016-07-14 11:42:37 +02006073 /* If the flags NOTERM is set, we cannot terminate the http
6074 * session, so we just end the execution of the current
6075 * lua code.
6076 */
6077 if (htxn->flags & HLUA_TXN_NOTERM) {
6078 WILL_LJMP(hlua_done(L));
6079 return 0;
6080 }
6081
Willy Tarreaub2ccb562015-04-06 11:11:15 +02006082 ic = &htxn->s->req;
6083 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01006084
Christopher Faulet72c69272019-07-26 16:40:24 +02006085 if (IS_HTX_STRM(htxn->s)) {
6086 htxn->s->txn->status = 0;
6087 http_reply_and_close(htxn->s, 0, NULL);
6088 ic->analysers &= AN_REQ_FLT_END;
6089 oc->analysers &= AN_RES_FLT_END;
6090 }
Christopher Fauletb58fb792019-07-16 10:52:40 +02006091 else {
6092 if (htxn->s->txn) {
6093 /* HTTP mode, let's stay in sync with the stream */
6094 b_del(&ic->buf, htxn->s->txn->req.sov);
6095 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
6096 htxn->s->txn->req.sov = 0;
6097
6098 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
6099 oc->analysers = AN_RES_HTTP_XFER_BODY;
6100 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
6101 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreau630ef452015-08-28 10:06:15 +02006102
Willy Tarreau630ef452015-08-28 10:06:15 +02006103 /* Note that if we want to support keep-alive, we need
6104 * to bypass the close/shutr_now calls below, but that
6105 * may only be done if the HTTP request was already
6106 * processed and the connection header is known (ie
6107 * not during TCP rules).
6108 */
Christopher Fauletb58fb792019-07-16 10:52:40 +02006109 }
Willy Tarreau630ef452015-08-28 10:06:15 +02006110
Christopher Fauletb58fb792019-07-16 10:52:40 +02006111 channel_auto_read(ic);
6112 channel_abort(ic);
6113 channel_auto_close(ic);
6114 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02006115
Christopher Fauletb58fb792019-07-16 10:52:40 +02006116 oc->wex = tick_add_ifset(now_ms, oc->wto);
6117 channel_auto_read(oc);
6118 channel_auto_close(oc);
6119 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006120
Christopher Fauletb58fb792019-07-16 10:52:40 +02006121 ic->analysers = 0;
6122 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006123
Christopher Faulet72c69272019-07-26 16:40:24 +02006124 if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is
6125 htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
6126
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006127 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02006128 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006129 return 0;
6130}
6131
6132__LJMP static int hlua_log(lua_State *L)
6133{
6134 int level;
6135 const char *msg;
6136
6137 MAY_LJMP(check_args(L, 2, "log"));
6138 level = MAY_LJMP(luaL_checkinteger(L, 1));
6139 msg = MAY_LJMP(luaL_checkstring(L, 2));
6140
6141 if (level < 0 || level >= NB_LOG_LEVELS)
6142 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6143
6144 hlua_sendlog(NULL, level, msg);
6145 return 0;
6146}
6147
6148__LJMP static int hlua_log_debug(lua_State *L)
6149{
6150 const char *msg;
6151
6152 MAY_LJMP(check_args(L, 1, "debug"));
6153 msg = MAY_LJMP(luaL_checkstring(L, 1));
6154 hlua_sendlog(NULL, LOG_DEBUG, msg);
6155 return 0;
6156}
6157
6158__LJMP static int hlua_log_info(lua_State *L)
6159{
6160 const char *msg;
6161
6162 MAY_LJMP(check_args(L, 1, "info"));
6163 msg = MAY_LJMP(luaL_checkstring(L, 1));
6164 hlua_sendlog(NULL, LOG_INFO, msg);
6165 return 0;
6166}
6167
6168__LJMP static int hlua_log_warning(lua_State *L)
6169{
6170 const char *msg;
6171
6172 MAY_LJMP(check_args(L, 1, "warning"));
6173 msg = MAY_LJMP(luaL_checkstring(L, 1));
6174 hlua_sendlog(NULL, LOG_WARNING, msg);
6175 return 0;
6176}
6177
6178__LJMP static int hlua_log_alert(lua_State *L)
6179{
6180 const char *msg;
6181
6182 MAY_LJMP(check_args(L, 1, "alert"));
6183 msg = MAY_LJMP(luaL_checkstring(L, 1));
6184 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006185 return 0;
6186}
6187
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006188__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006189{
6190 int wakeup_ms = lua_tointeger(L, -1);
6191 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006192 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006193 return 0;
6194}
6195
6196__LJMP static int hlua_sleep(lua_State *L)
6197{
6198 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006199 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006200
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006201 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006202
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006203 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006204 wakeup_ms = tick_add(now_ms, delay);
6205 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006206
Willy Tarreau9635e032018-10-16 17:52:55 +02006207 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006208 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006209}
6210
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006211__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006212{
6213 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006214 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006215
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006216 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006217
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006218 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006219 wakeup_ms = tick_add(now_ms, delay);
6220 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006221
Willy Tarreau9635e032018-10-16 17:52:55 +02006222 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006223 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006224}
6225
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006226/* This functionis an LUA binding. it permits to give back
6227 * the hand at the HAProxy scheduler. It is used when the
6228 * LUA processing consumes a lot of time.
6229 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006230__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006231{
6232 return 0;
6233}
6234
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006235__LJMP static int hlua_yield(lua_State *L)
6236{
Willy Tarreau9635e032018-10-16 17:52:55 +02006237 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006238 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006239}
6240
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006241/* This function change the nice of the currently executed
6242 * task. It is used set low or high priority at the current
6243 * task.
6244 */
Willy Tarreau59551662015-03-10 14:23:13 +01006245__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006246{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006247 struct hlua *hlua;
6248 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006249
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006250 MAY_LJMP(check_args(L, 1, "set_nice"));
6251 hlua = hlua_gethlua(L);
6252 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006253
6254 /* If he task is not set, I'm in a start mode. */
6255 if (!hlua || !hlua->task)
6256 return 0;
6257
6258 if (nice < -1024)
6259 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006260 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006261 nice = 1024;
6262
6263 hlua->task->nice = nice;
6264 return 0;
6265}
6266
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006267/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006268 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6269 * return an E_AGAIN signal, the emmiter of this signal must set a
6270 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006271 *
6272 * Task wrapper are longjmp safe because the only one Lua code
6273 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006274 */
Willy Tarreau83a5ff42019-08-21 14:14:50 +02006275struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006276{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006277 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006278 enum hlua_exec status;
6279
Christopher Faulet5bc99722018-04-25 10:34:45 +02006280 if (task->thread_mask == MAX_THREADS_MASK)
6281 task_set_affinity(task, tid_bit);
6282
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006283 /* If it is the first call to the task, we must initialize the
6284 * execution timeouts.
6285 */
6286 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006287 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006288
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006289 /* Execute the Lua code. */
6290 status = hlua_ctx_resume(hlua, 1);
6291
6292 switch (status) {
6293 /* finished or yield */
6294 case HLUA_E_OK:
6295 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006296 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006297 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006298 break;
6299
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006300 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006301 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006302 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006303 break;
6304
6305 /* finished with error. */
6306 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006307 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006308 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006309 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006310 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006311 break;
6312
6313 case HLUA_E_ERR:
6314 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006315 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006316 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006317 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006318 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006319 break;
6320 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006321 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006322}
6323
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006324/* This function is an LUA binding that register LUA function to be
6325 * executed after the HAProxy configuration parsing and before the
6326 * HAProxy scheduler starts. This function expect only one LUA
6327 * argument that is a function. This function returns nothing, but
6328 * throws if an error is encountered.
6329 */
6330__LJMP static int hlua_register_init(lua_State *L)
6331{
6332 struct hlua_init_function *init;
6333 int ref;
6334
6335 MAY_LJMP(check_args(L, 1, "register_init"));
6336
6337 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6338
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006339 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006340 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006341 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006342
6343 init->function_ref = ref;
6344 LIST_ADDQ(&hlua_init_functions, &init->l);
6345 return 0;
6346}
6347
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006348/* This functio is an LUA binding. It permits to register a task
6349 * executed in parallel of the main HAroxy activity. The task is
6350 * created and it is set in the HAProxy scheduler. It can be called
6351 * from the "init" section, "post init" or during the runtime.
6352 *
6353 * Lua prototype:
6354 *
6355 * <none> core.register_task(<function>)
6356 */
6357static int hlua_register_task(lua_State *L)
6358{
6359 struct hlua *hlua;
6360 struct task *task;
6361 int ref;
6362
6363 MAY_LJMP(check_args(L, 1, "register_task"));
6364
6365 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6366
Willy Tarreaubafbe012017-11-24 17:34:44 +01006367 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006368 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006369 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006370
Emeric Brunc60def82017-09-27 14:59:38 +02006371 task = task_new(MAX_THREADS_MASK);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006372 if (!task)
6373 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6374
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006375 task->context = hlua;
6376 task->process = hlua_process_task;
6377
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006378 if (!hlua_ctx_init(hlua, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006379 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006380
6381 /* Restore the function in the stack. */
6382 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6383 hlua->nargs = 0;
6384
6385 /* Schedule task. */
6386 task_schedule(task, now_ms);
6387
6388 return 0;
6389}
6390
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006391/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6392 * doesn't allow "yield" functions because the HAProxy engine cannot
6393 * resume converters.
6394 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006395static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006396{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006397 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006398 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006399 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006400
Willy Tarreaube508f12016-03-10 11:47:01 +01006401 if (!stream)
6402 return 0;
6403
Willy Tarreau87b09662015-04-03 00:22:06 +02006404 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006405 * Lua context can be not initialized. This behavior
6406 * permits to save performances because a systematic
6407 * Lua initialization cause 5% performances loss.
6408 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006409 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006410 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006411 if (!stream->hlua) {
6412 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6413 return 0;
6414 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006415 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006416 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6417 return 0;
6418 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006419 }
6420
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006421 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006422 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006423
6424 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006425 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6426 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6427 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006428 else
6429 error = "critical error";
6430 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006431 return 0;
6432 }
6433
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006434 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006435 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006436 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006437 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006438 return 0;
6439 }
6440
6441 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006442 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006443
6444 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006445 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006446 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006447 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006448 return 0;
6449 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006450 hlua_smp2lua(stream->hlua->T, smp);
6451 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006452
6453 /* push keywords in the stack. */
6454 if (arg_p) {
6455 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006456 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006457 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006458 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006459 return 0;
6460 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006461 hlua_arg2lua(stream->hlua->T, arg_p);
6462 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006463 }
6464 }
6465
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006466 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006467 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006468
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006469 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006470 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006471 }
6472
6473 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006474 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006475 /* finished. */
6476 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006477 /* If the stack is empty, the function fails. */
6478 if (lua_gettop(stream->hlua->T) <= 0)
6479 return 0;
6480
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006481 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006482 hlua_lua2smp(stream->hlua->T, -1, smp);
6483 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006484 return 1;
6485
6486 /* yield. */
6487 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006488 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006489 return 0;
6490
6491 /* finished with error. */
6492 case HLUA_E_ERRMSG:
6493 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006494 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006495 fcn->name, lua_tostring(stream->hlua->T, -1));
6496 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006497 return 0;
6498
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006499 case HLUA_E_ETMOUT:
6500 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6501 return 0;
6502
6503 case HLUA_E_NOMEM:
6504 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6505 return 0;
6506
6507 case HLUA_E_YIELD:
6508 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6509 return 0;
6510
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006511 case HLUA_E_ERR:
6512 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006513 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006514
6515 default:
6516 return 0;
6517 }
6518}
6519
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006520/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6521 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006522 * resume sample-fetches. This function will be called by the sample
6523 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006524 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006525static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6526 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006527{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006528 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006529 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006530 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006531 const struct buffer msg = { };
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006532 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006533
Willy Tarreaube508f12016-03-10 11:47:01 +01006534 if (!stream)
6535 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006536
Willy Tarreau87b09662015-04-03 00:22:06 +02006537 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006538 * Lua context can be not initialized. This behavior
6539 * permits to save performances because a systematic
6540 * Lua initialization cause 5% performances loss.
6541 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006542 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006543 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006544 if (!stream->hlua) {
6545 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6546 return 0;
6547 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006548 if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006549 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6550 return 0;
6551 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006552 }
6553
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006554 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006555
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006556 if (stream->be->mode == PR_MODE_HTTP) {
6557 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
6558 hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
6559 else
6560 hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY);
6561 }
6562
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006563 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006564 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006565
6566 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006567 if (!SET_SAFE_LJMP(stream->hlua->T)) {
6568 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6569 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006570 else
6571 error = "critical error";
6572 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006573 return 0;
6574 }
6575
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006576 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006577 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006578 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006579 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006580 return 0;
6581 }
6582
6583 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006584 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006585
6586 /* push arguments in the stack. */
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006587 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006588 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006589 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006590 return 0;
6591 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006592 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006593
6594 /* push keywords in the stack. */
6595 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6596 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006597 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006598 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006599 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006600 return 0;
6601 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006602 hlua_arg2lua(stream->hlua->T, arg_p);
6603 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006604 }
6605
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006606 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006607 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006608
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006609 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006610 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006611 }
6612
6613 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006614 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006615 /* finished. */
6616 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006617 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006618 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006619 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006620 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006621 /* If the stack is empty, the function fails. */
6622 if (lua_gettop(stream->hlua->T) <= 0)
6623 return 0;
6624
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006625 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006626 hlua_lua2smp(stream->hlua->T, -1, smp);
6627 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006628
6629 /* Set the end of execution flag. */
6630 smp->flags &= ~SMP_F_MAY_CHANGE;
6631 return 1;
6632
6633 /* yield. */
6634 case HLUA_E_AGAIN:
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 FOURNIER23bc3752015-09-11 19:15:43 +02006637 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006638 return 0;
6639
6640 /* finished with error. */
6641 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006642 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006643 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006644 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006645 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006646 fcn->name, lua_tostring(stream->hlua->T, -1));
6647 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006648 return 0;
6649
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006650 case HLUA_E_ETMOUT:
6651 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006652 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006653 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6654 return 0;
6655
6656 case HLUA_E_NOMEM:
6657 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006658 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006659 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6660 return 0;
6661
6662 case HLUA_E_YIELD:
6663 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006664 si_retnclose(&stream->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006665 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6666 return 0;
6667
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006668 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006669 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006670 si_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006671 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006672 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006673
6674 default:
6675 return 0;
6676 }
6677}
6678
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006679/* This function is an LUA binding used for registering
6680 * "sample-conv" functions. It expects a converter name used
6681 * in the haproxy configuration file, and an LUA function.
6682 */
6683__LJMP static int hlua_register_converters(lua_State *L)
6684{
6685 struct sample_conv_kw_list *sck;
6686 const char *name;
6687 int ref;
6688 int len;
6689 struct hlua_function *fcn;
6690
6691 MAY_LJMP(check_args(L, 2, "register_converters"));
6692
6693 /* First argument : converter name. */
6694 name = MAY_LJMP(luaL_checkstring(L, 1));
6695
6696 /* Second argument : lua function. */
6697 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6698
6699 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006700 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006701 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006702 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006703 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006704 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006705 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006706
6707 /* Fill fcn. */
6708 fcn->name = strdup(name);
6709 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006710 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006711 fcn->function_ref = ref;
6712
6713 /* List head */
6714 sck->list.n = sck->list.p = NULL;
6715
6716 /* converter keyword. */
6717 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006718 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006719 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006720 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006721
6722 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6723 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006724 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 +01006725 sck->kw[0].val_args = NULL;
6726 sck->kw[0].in_type = SMP_T_STR;
6727 sck->kw[0].out_type = SMP_T_STR;
6728 sck->kw[0].private = fcn;
6729
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006730 /* Register this new converter */
6731 sample_register_convs(sck);
6732
6733 return 0;
6734}
6735
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006736/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006737 * "sample-fetch" functions. It expects a converter name used
6738 * in the haproxy configuration file, and an LUA function.
6739 */
6740__LJMP static int hlua_register_fetches(lua_State *L)
6741{
6742 const char *name;
6743 int ref;
6744 int len;
6745 struct sample_fetch_kw_list *sfk;
6746 struct hlua_function *fcn;
6747
6748 MAY_LJMP(check_args(L, 2, "register_fetches"));
6749
6750 /* First argument : sample-fetch name. */
6751 name = MAY_LJMP(luaL_checkstring(L, 1));
6752
6753 /* Second argument : lua function. */
6754 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6755
6756 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006757 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006758 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006759 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006760 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006761 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006762 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006763
6764 /* Fill fcn. */
6765 fcn->name = strdup(name);
6766 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006767 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006768 fcn->function_ref = ref;
6769
6770 /* List head */
6771 sfk->list.n = sfk->list.p = NULL;
6772
6773 /* sample-fetch keyword. */
6774 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006775 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006776 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006777 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006778
6779 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6780 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006781 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 +01006782 sfk->kw[0].val_args = NULL;
6783 sfk->kw[0].out_type = SMP_T_STR;
6784 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6785 sfk->kw[0].val = 0;
6786 sfk->kw[0].private = fcn;
6787
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006788 /* Register this new fetch. */
6789 sample_register_fetches(sfk);
6790
6791 return 0;
6792}
6793
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006794/* This function is a wrapper to execute each LUA function declared
6795 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006796 * return ACT_RET_CONT if the processing is finished (with or without
6797 * error) and return ACT_RET_YIELD if the function must be called again
6798 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006799 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006800static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006801 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006802{
6803 char **arg;
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006804 unsigned int hflags = 0;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006805 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006806 const char *error;
Willy Tarreau83061a82018-07-13 11:56:34 +02006807 const struct buffer msg = { };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006808
6809 switch (rule->from) {
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006810 case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break;
6811 case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break;
6812 case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break;
6813 case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006814 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006815 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006816 return ACT_RET_CONT;
6817 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006818
Willy Tarreau87b09662015-04-03 00:22:06 +02006819 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006820 * Lua context can be not initialized. This behavior
6821 * permits to save performances because a systematic
6822 * Lua initialization cause 5% performances loss.
6823 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006824 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006825 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006826 if (!s->hlua) {
6827 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6828 rule->arg.hlua_rule->fcn.name);
6829 return ACT_RET_CONT;
6830 }
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01006831 if (!hlua_ctx_init(s->hlua, s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006832 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6833 rule->arg.hlua_rule->fcn.name);
6834 return ACT_RET_CONT;
6835 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006836 }
6837
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006838 consistency_set(s, dir, &s->hlua->cons);
6839
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006840 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006841 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006842
6843 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006844 if (!SET_SAFE_LJMP(s->hlua->T)) {
6845 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6846 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006847 else
6848 error = "critical error";
6849 SEND_ERR(px, "Lua function '%s': %s.\n",
6850 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006851 return ACT_RET_CONT;
6852 }
6853
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006854 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006855 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006856 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006857 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006858 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006859 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006860 }
6861
6862 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006863 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006864
Willy Tarreau87b09662015-04-03 00:22:06 +02006865 /* Create and and push object stream in the stack. */
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006866 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006867 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006868 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006869 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006870 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006871 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006872 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006873
6874 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006875 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006876 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006877 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006878 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006879 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006880 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006881 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006882 lua_pushstring(s->hlua->T, *arg);
6883 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006884 }
6885
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006886 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006887 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006888
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006889 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006890 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006891 }
6892
Christopher Faulet719ddc82020-06-02 18:46:07 +02006893 /* Always reset the analyse expiration timeout for the corresponding
6894 * channel in case the lua script yield, to be sure to not keep an
6895 * expired timeout.
6896 */
6897 if (dir == SMP_OPT_DIR_REQ)
6898 s->req.analyse_exp = TICK_ETERNITY;
6899 else
6900 s->res.analyse_exp = TICK_ETERNITY;
6901
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006902 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006903 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006904 /* finished. */
6905 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006906 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006907 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006908 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006909 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006910 if (s->hlua->flags & HLUA_STOP)
Christopher Faulet4629d082019-07-04 11:27:15 +02006911 return ACT_RET_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006912 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006913
6914 /* yield. */
6915 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006916 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006917 if (s->hlua->wake_time != TICK_ETERNITY) {
Christopher Fauletea0b0e22019-08-14 23:19:45 +02006918 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006919 s->req.analyse_exp = s->hlua->wake_time;
Christopher Fauletff96b8b2019-07-26 15:09:53 +02006920 else
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006921 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006922 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006923 /* Some actions can be wake up when a "write" event
6924 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006925 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006926 */
Christopher Fauletb22f6502019-07-26 14:54:52 +02006927 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006928 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006929 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006930 s->req.flags |= CF_WAKE_WRITE;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006931 /* We can quit the function without consistency check
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006932 * because HAProxy is not able to manipulate data, it
6933 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006934 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006935
6936 /* finished with error. */
6937 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006938 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006939 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006940 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006941 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006942 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006943 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006944 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6945 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006946 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006947
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006948 case HLUA_E_ETMOUT:
6949 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006950 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006951 return ACT_RET_ERR;
6952 }
6953 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6954 return 0;
6955
6956 case HLUA_E_NOMEM:
6957 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006958 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006959 return ACT_RET_ERR;
6960 }
6961 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6962 return 0;
6963
6964 case HLUA_E_YIELD:
6965 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006966 si_retnclose(&s->si[0], &msg);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006967 return ACT_RET_ERR;
6968 }
6969 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6970 rule->arg.hlua_rule->fcn.name);
6971 return 0;
6972
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006973 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006974 if (!consistency_check(s, dir, &s->hlua->cons)) {
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01006975 si_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006976 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006977 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006978 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006979 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006980 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006981
6982 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006983 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006984 }
6985}
6986
Olivier Houchard9f6af332018-05-25 14:04:04 +02006987struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006988{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006989 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006990
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006991 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006992 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006993 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006994}
6995
6996static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6997{
6998 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006999 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007000 struct task *task;
7001 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007002 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007003
Willy Tarreaubafbe012017-11-24 17:34:44 +01007004 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007005 if (!hlua) {
7006 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
7007 ctx->rule->arg.hlua_rule->fcn.name);
7008 return 0;
7009 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007010 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007011 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007012 ctx->ctx.hlua_apptcp.flags = 0;
7013
7014 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007015 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007016 if (!task) {
7017 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
7018 ctx->rule->arg.hlua_rule->fcn.name);
7019 return 0;
7020 }
7021 task->nice = 0;
7022 task->context = ctx;
7023 task->process = hlua_applet_wakeup;
7024 ctx->ctx.hlua_apptcp.task = task;
7025
7026 /* In the execution wrappers linked with a stream, the
7027 * Lua context can be not initialized. This behavior
7028 * permits to save performances because a systematic
7029 * Lua initialization cause 5% performances loss.
7030 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007031 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007032 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
7033 ctx->rule->arg.hlua_rule->fcn.name);
7034 return 0;
7035 }
7036
7037 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007038 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007039
7040 /* The following Lua calls can fail. */
7041 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007042 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7043 error = lua_tostring(hlua->T, -1);
7044 else
7045 error = "critical error";
7046 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7047 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007048 return 0;
7049 }
7050
7051 /* Check stack available size. */
7052 if (!lua_checkstack(hlua->T, 1)) {
7053 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7054 ctx->rule->arg.hlua_rule->fcn.name);
7055 RESET_SAFE_LJMP(hlua->T);
7056 return 0;
7057 }
7058
7059 /* Restore the function in the stack. */
7060 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7061
7062 /* Create and and push object stream in the stack. */
7063 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7064 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7065 ctx->rule->arg.hlua_rule->fcn.name);
7066 RESET_SAFE_LJMP(hlua->T);
7067 return 0;
7068 }
7069 hlua->nargs = 1;
7070
7071 /* push keywords in the stack. */
7072 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7073 if (!lua_checkstack(hlua->T, 1)) {
7074 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
7075 ctx->rule->arg.hlua_rule->fcn.name);
7076 RESET_SAFE_LJMP(hlua->T);
7077 return 0;
7078 }
7079 lua_pushstring(hlua->T, *arg);
7080 hlua->nargs++;
7081 }
7082
7083 RESET_SAFE_LJMP(hlua->T);
7084
7085 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007086 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007087 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007088
7089 return 1;
7090}
7091
Willy Tarreau83a5ff42019-08-21 14:14:50 +02007092void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007093{
7094 struct stream_interface *si = ctx->owner;
7095 struct stream *strm = si_strm(si);
7096 struct channel *res = si_ic(si);
7097 struct act_rule *rule = ctx->rule;
7098 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007099 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007100
7101 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007102 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7103 /* eat the whole request */
7104 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007105 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007106 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007107
7108 /* If the stream is disconnect or closed, ldo nothing. */
7109 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7110 return;
7111
7112 /* Execute the function. */
7113 switch (hlua_ctx_resume(hlua, 1)) {
7114 /* finished. */
7115 case HLUA_E_OK:
7116 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7117
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007118 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007119 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007120 res->flags |= CF_READ_NULL;
7121 si_shutr(si);
7122 return;
7123
7124 /* yield. */
7125 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007126 if (hlua->wake_time != TICK_ETERNITY)
7127 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007128 return;
7129
7130 /* finished with error. */
7131 case HLUA_E_ERRMSG:
7132 /* Display log. */
7133 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
7134 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7135 lua_pop(hlua->T, 1);
7136 goto error;
7137
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007138 case HLUA_E_ETMOUT:
7139 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
7140 rule->arg.hlua_rule->fcn.name);
7141 goto error;
7142
7143 case HLUA_E_NOMEM:
7144 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
7145 rule->arg.hlua_rule->fcn.name);
7146 goto error;
7147
7148 case HLUA_E_YIELD: /* unexpected */
7149 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
7150 rule->arg.hlua_rule->fcn.name);
7151 goto error;
7152
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007153 case HLUA_E_ERR:
7154 /* Display log. */
7155 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
7156 rule->arg.hlua_rule->fcn.name);
7157 goto error;
7158
7159 default:
7160 goto error;
7161 }
7162
7163error:
7164
7165 /* For all other cases, just close the stream. */
7166 si_shutw(si);
7167 si_shutr(si);
7168 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7169}
7170
7171static void hlua_applet_tcp_release(struct appctx *ctx)
7172{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007173 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007174 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007175 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007176 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007177}
7178
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007179/* The function returns 1 if the initialisation is complete, 0 if
7180 * an errors occurs and -1 if more data are required for initializing
7181 * the applet.
7182 */
7183static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7184{
7185 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007186 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007187 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007188 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007189 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007190 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007191
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007192 txn = strm->txn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007193
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007194 /* We want two things in HTTP mode :
7195 * - enforce server-close mode if we were in keep-alive, so that the
7196 * applet is released after each response ;
7197 * - enable request body transfer to the applet in order to resync
7198 * with the response body.
7199 */
7200 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
7201 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02007202
Willy Tarreaubafbe012017-11-24 17:34:44 +01007203 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007204 if (!hlua) {
7205 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7206 ctx->rule->arg.hlua_rule->fcn.name);
7207 return 0;
7208 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007209 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007210 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007211 ctx->ctx.hlua_apphttp.left_bytes = -1;
7212 ctx->ctx.hlua_apphttp.flags = 0;
7213
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007214 if (txn->req.flags & HTTP_MSGF_VER_11)
7215 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7216
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007217 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007218 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007219 if (!task) {
7220 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
7221 ctx->rule->arg.hlua_rule->fcn.name);
7222 return 0;
7223 }
7224 task->nice = 0;
7225 task->context = ctx;
7226 task->process = hlua_applet_wakeup;
7227 ctx->ctx.hlua_apphttp.task = task;
7228
7229 /* In the execution wrappers linked with a stream, the
7230 * Lua context can be not initialized. This behavior
7231 * permits to save performances because a systematic
7232 * Lua initialization cause 5% performances loss.
7233 */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007234 if (!hlua_ctx_init(hlua, task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007235 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
7236 ctx->rule->arg.hlua_rule->fcn.name);
7237 return 0;
7238 }
7239
7240 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007241 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007242
7243 /* The following Lua calls can fail. */
7244 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007245 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7246 error = lua_tostring(hlua->T, -1);
7247 else
7248 error = "critical error";
7249 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7250 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007251 return 0;
7252 }
7253
7254 /* Check stack available size. */
7255 if (!lua_checkstack(hlua->T, 1)) {
7256 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7257 ctx->rule->arg.hlua_rule->fcn.name);
7258 RESET_SAFE_LJMP(hlua->T);
7259 return 0;
7260 }
7261
7262 /* Restore the function in the stack. */
7263 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
7264
7265 /* Create and and push object stream in the stack. */
7266 if (!hlua_applet_http_new(hlua->T, ctx)) {
7267 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7268 ctx->rule->arg.hlua_rule->fcn.name);
7269 RESET_SAFE_LJMP(hlua->T);
7270 return 0;
7271 }
7272 hlua->nargs = 1;
7273
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007274 /* push keywords in the stack. */
7275 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7276 if (!lua_checkstack(hlua->T, 1)) {
7277 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
7278 ctx->rule->arg.hlua_rule->fcn.name);
7279 RESET_SAFE_LJMP(hlua->T);
7280 return 0;
7281 }
7282 lua_pushstring(hlua->T, *arg);
7283 hlua->nargs++;
7284 }
7285
7286 RESET_SAFE_LJMP(hlua->T);
7287
7288 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007289 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007290
7291 return 1;
7292}
7293
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007294static void hlua_applet_htx_fct(struct appctx *ctx)
7295{
7296 struct stream_interface *si = ctx->owner;
7297 struct stream *strm = si_strm(si);
7298 struct channel *req = si_oc(si);
7299 struct channel *res = si_ic(si);
7300 struct act_rule *rule = ctx->rule;
7301 struct proxy *px = strm->be;
7302 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7303 struct htx *req_htx, *res_htx;
7304
7305 res_htx = htx_from_buf(&res->buf);
7306
7307 /* If the stream is disconnect or closed, ldo nothing. */
7308 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7309 goto out;
7310
7311 /* Check if the input buffer is avalaible. */
7312 if (!b_size(&res->buf)) {
7313 si_rx_room_blk(si);
7314 goto out;
7315 }
7316 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007317 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007318 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7319
7320 /* Set the currently running flag. */
7321 if (!HLUA_IS_RUNNING(hlua) &&
7322 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7323 struct htx_blk *blk;
7324 size_t count = co_data(req);
7325
7326 if (!count) {
7327 si_cant_get(si);
7328 goto out;
7329 }
7330
7331 /* We need to flush the request header. This left the body for
7332 * the Lua.
7333 */
7334 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007335 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007336 while (count && blk) {
7337 enum htx_blk_type type = htx_get_blk_type(blk);
7338 uint32_t sz = htx_get_blksz(blk);
7339
7340 if (sz > count) {
7341 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007342 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007343 goto out;
7344 }
7345
7346 count -= sz;
7347 co_set_data(req, co_data(req) - sz);
7348 blk = htx_remove_blk(req_htx, blk);
7349
7350 if (type == HTX_BLK_EOH)
7351 break;
7352 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007353 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007354 }
7355
7356 /* Executes The applet if it is not done. */
7357 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7358
7359 /* Execute the function. */
7360 switch (hlua_ctx_resume(hlua, 1)) {
7361 /* finished. */
7362 case HLUA_E_OK:
7363 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7364 break;
7365
7366 /* yield. */
7367 case HLUA_E_AGAIN:
7368 if (hlua->wake_time != TICK_ETERNITY)
7369 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007370 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007371
7372 /* finished with error. */
7373 case HLUA_E_ERRMSG:
7374 /* Display log. */
7375 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7376 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7377 lua_pop(hlua->T, 1);
7378 goto error;
7379
7380 case HLUA_E_ETMOUT:
7381 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7382 rule->arg.hlua_rule->fcn.name);
7383 goto error;
7384
7385 case HLUA_E_NOMEM:
7386 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7387 rule->arg.hlua_rule->fcn.name);
7388 goto error;
7389
7390 case HLUA_E_YIELD: /* unexpected */
7391 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7392 rule->arg.hlua_rule->fcn.name);
7393 goto error;
7394
7395 case HLUA_E_ERR:
7396 /* Display log. */
7397 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7398 rule->arg.hlua_rule->fcn.name);
7399 goto error;
7400
7401 default:
7402 goto error;
7403 }
7404 }
7405
7406 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007407 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7408 goto done;
7409
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007410 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7411 goto error;
7412
Christopher Faulet54b5e212019-06-04 10:08:28 +02007413 /* Don't add TLR because mux-h1 will take care of it */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007414 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
7415 si_rx_room_blk(si);
7416 goto out;
7417 }
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007418 channel_add_input(res, 1);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007419 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7420 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007421 }
7422
7423 done:
7424 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007425 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007426 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007427 si_shutr(si);
7428 }
7429
7430 /* eat the whole request */
7431 if (co_data(req)) {
7432 req_htx = htx_from_buf(&req->buf);
7433 co_htx_skip(req, req_htx, co_data(req));
7434 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007435 }
7436 }
7437
7438 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007439 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007440 return;
7441
7442 error:
7443
7444 /* If we are in HTTP mode, and we are not send any
7445 * data, return a 500 server error in best effort:
7446 * if there is no room available in the buffer,
7447 * just close the connection.
7448 */
7449 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7450 struct buffer *err = &htx_err_chunks[HTTP_ERR_500];
7451
7452 channel_erase(res);
7453 res->buf.data = b_data(err);
7454 memcpy(res->buf.area, b_head(err), b_data(err));
7455 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007456 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007457 }
7458 if (!(strm->flags & SF_ERR_MASK))
7459 strm->flags |= SF_ERR_RESOURCE;
7460 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7461 goto done;
7462}
7463
Willy Tarreau83a5ff42019-08-21 14:14:50 +02007464void hlua_applet_http_fct(struct appctx *ctx)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007465{
7466 struct stream_interface *si = ctx->owner;
7467 struct stream *strm = si_strm(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007468 struct channel *req = si_oc(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007469 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007470 struct act_rule *rule = ctx->rule;
7471 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007472 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Willy Tarreau206ba832018-06-14 15:27:31 +02007473 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007474 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02007475 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02007476 size_t len2;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007477 int ret;
7478
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007479 if (IS_HTX_STRM(strm))
7480 return hlua_applet_htx_fct(ctx);
7481
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007482 /* If the stream is disconnect or closed, ldo nothing. */
7483 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007484 goto out;
7485
7486 /* Check if the input buffer is avalaible. */
7487 if (!b_size(&res->buf)) {
7488 si_rx_room_blk(si);
7489 goto out;
7490 }
7491 /* check that the output is not closed */
7492 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
7493 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007494
7495 /* Set the currently running flag. */
7496 if (!HLUA_IS_RUNNING(hlua) &&
7497 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007498 /* Store the max amount of bytes that we can read. */
7499 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
7500
7501 /* We need to flush the request header. This left the body
7502 * for the Lua.
7503 */
7504
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007505 /* Read the maximum amount of data available. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007506 ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007507 if (ret == -1)
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007508 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007509
7510 /* No data available, ask for more data. */
7511 if (ret == 1)
7512 len2 = 0;
7513 if (ret == 0)
7514 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02007515 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007516 si_cant_get(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007517 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007518 }
7519
7520 /* skip the requests bytes. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007521 co_skip(req, strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007522 }
7523
7524 /* Executes The applet if it is not done. */
7525 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7526
7527 /* Execute the function. */
7528 switch (hlua_ctx_resume(hlua, 1)) {
7529 /* finished. */
7530 case HLUA_E_OK:
7531 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7532 break;
7533
7534 /* yield. */
7535 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007536 if (hlua->wake_time != TICK_ETERNITY)
7537 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007538 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007539
7540 /* finished with error. */
7541 case HLUA_E_ERRMSG:
7542 /* Display log. */
7543 SEND_ERR(px, "Lua applet http '%s': %s.\n",
7544 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
7545 lua_pop(hlua->T, 1);
7546 goto error;
7547
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007548 case HLUA_E_ETMOUT:
7549 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
7550 rule->arg.hlua_rule->fcn.name);
7551 goto error;
7552
7553 case HLUA_E_NOMEM:
7554 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
7555 rule->arg.hlua_rule->fcn.name);
7556 goto error;
7557
7558 case HLUA_E_YIELD: /* unexpected */
7559 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
7560 rule->arg.hlua_rule->fcn.name);
7561 goto error;
7562
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007563 case HLUA_E_ERR:
7564 /* Display log. */
7565 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
7566 rule->arg.hlua_rule->fcn.name);
7567 goto error;
7568
7569 default:
7570 goto error;
7571 }
7572 }
7573
7574 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007575 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7576 goto done;
7577
Christopher Fauletcc26b132018-12-18 21:20:57 +01007578 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7579 goto error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007580
7581 /* We must send the final chunk. */
7582 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
7583 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
7584
7585 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02007586 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007587
7588 /* critical error. */
7589 if (ret == -2 || ret == -3) {
7590 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
7591 rule->arg.hlua_rule->fcn.name);
7592 goto error;
7593 }
7594
7595 /* no enough space error. */
7596 if (ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01007597 si_rx_room_blk(si);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007598 goto out;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007599 }
7600
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007601 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7602 ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007603 }
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007604 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007605
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007606 done:
7607 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
7608 if (!(res->flags & CF_SHUTR)) {
7609 res->flags |= CF_READ_NULL;
7610 si_shutr(si);
7611 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007612
7613 /* eat the whole request */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007614 if (co_data(req))
7615 co_skip(req, co_data(req));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007616 }
7617
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007618 out:
7619 return;
7620
7621 error:
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007622
7623 /* If we are in HTTP mode, and we are not send any
7624 * data, return a 500 server error in best effort:
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007625 * if there is no room available in the buffer,
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007626 * just close the connection.
7627 */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007628 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
7629 channel_erase(res);
7630 ci_putblk(res, error_500, strlen(error_500));
7631 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007632 if (!(strm->flags & SF_ERR_MASK))
7633 strm->flags |= SF_ERR_RESOURCE;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007634 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007635 goto done;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007636}
7637
7638static void hlua_applet_http_release(struct appctx *ctx)
7639{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007640 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007641 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007642 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007643 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007644}
7645
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007646/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
7647 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007648 *
7649 * This function can fail with an abort() due to an Lua critical error.
7650 * We are in the configuration parsing process of HAProxy, this abort() is
7651 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007652 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007653static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7654 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007655{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007656 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007657 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007658
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007659 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007660 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007661 if (!rule->arg.hlua_rule) {
7662 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007663 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007664 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007665
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007666 /* Memory for arguments. */
7667 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
7668 if (!rule->arg.hlua_rule->args) {
7669 memprintf(err, "out of memory error");
7670 return ACT_RET_PRS_ERR;
7671 }
7672
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007673 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007674 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007675
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007676 /* Expect some arguments */
7677 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007678 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007679 memprintf(err, "expect %d arguments", fcn->nargs);
7680 return ACT_RET_PRS_ERR;
7681 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007682 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007683 if (!rule->arg.hlua_rule->args[i]) {
7684 memprintf(err, "out of memory error");
7685 return ACT_RET_PRS_ERR;
7686 }
7687 (*cur_arg)++;
7688 }
7689 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007690
Thierry FOURNIER42148732015-09-02 17:17:33 +02007691 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007692 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007693 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007694}
7695
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007696static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7697 struct act_rule *rule, char **err)
7698{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007699 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007700
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007701 /* HTTP applets are forbidden in tcp-request rules.
7702 * HTTP applet request requires everything initilized by
7703 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
7704 * The applet will be immediately initilized, but its before
7705 * the call of this analyzer.
7706 */
7707 if (rule->from != ACT_F_HTTP_REQ) {
7708 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7709 return ACT_RET_PRS_ERR;
7710 }
7711
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007712 /* Memory for the rule. */
7713 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7714 if (!rule->arg.hlua_rule) {
7715 memprintf(err, "out of memory error");
7716 return ACT_RET_PRS_ERR;
7717 }
7718
7719 /* Reference the Lua function and store the reference. */
7720 rule->arg.hlua_rule->fcn = *fcn;
7721
7722 /* TODO: later accept arguments. */
7723 rule->arg.hlua_rule->args = NULL;
7724
7725 /* Add applet pointer in the rule. */
7726 rule->applet.obj_type = OBJ_TYPE_APPLET;
7727 rule->applet.name = fcn->name;
7728 rule->applet.init = hlua_applet_http_init;
7729 rule->applet.fct = hlua_applet_http_fct;
7730 rule->applet.release = hlua_applet_http_release;
7731 rule->applet.timeout = hlua_timeout_applet;
7732
7733 return ACT_RET_PRS_OK;
7734}
7735
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007736/* This function is an LUA binding used for registering
7737 * "sample-conv" functions. It expects a converter name used
7738 * in the haproxy configuration file, and an LUA function.
7739 */
7740__LJMP static int hlua_register_action(lua_State *L)
7741{
7742 struct action_kw_list *akl;
7743 const char *name;
7744 int ref;
7745 int len;
7746 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007747 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007748
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007749 /* Initialise the number of expected arguments at 0. */
7750 nargs = 0;
7751
7752 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7753 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007754
7755 /* First argument : converter name. */
7756 name = MAY_LJMP(luaL_checkstring(L, 1));
7757
7758 /* Second argument : environment. */
7759 if (lua_type(L, 2) != LUA_TTABLE)
7760 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7761
7762 /* Third argument : lua function. */
7763 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7764
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007765 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007766 if (lua_gettop(L) >= 4)
7767 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7768
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007769 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007770 lua_pushnil(L);
7771 while (lua_next(L, 2) != 0) {
7772 if (lua_type(L, -1) != LUA_TSTRING)
7773 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7774
7775 /* Check required environment. Only accepted "http" or "tcp". */
7776 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007777 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007778 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007779 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007780 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007781 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007782 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007783
7784 /* Fill fcn. */
7785 fcn->name = strdup(name);
7786 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007787 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007788 fcn->function_ref = ref;
7789
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007790 /* Set the expected number od arguments. */
7791 fcn->nargs = nargs;
7792
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007793 /* List head */
7794 akl->list.n = akl->list.p = NULL;
7795
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007796 /* action keyword. */
7797 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007798 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007799 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007800 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007801
7802 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7803
7804 akl->kw[0].match_pfx = 0;
7805 akl->kw[0].private = fcn;
7806 akl->kw[0].parse = action_register_lua;
7807
7808 /* select the action registering point. */
7809 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7810 tcp_req_cont_keywords_register(akl);
7811 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7812 tcp_res_cont_keywords_register(akl);
7813 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7814 http_req_keywords_register(akl);
7815 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7816 http_res_keywords_register(akl);
7817 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007818 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007819 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7820 "are expected.", lua_tostring(L, -1)));
7821
7822 /* pop the environment string. */
7823 lua_pop(L, 1);
7824 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007825 return ACT_RET_PRS_OK;
7826}
7827
7828static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7829 struct act_rule *rule, char **err)
7830{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007831 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007832
Christopher Fauleteb709802019-04-11 22:04:08 +02007833 if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) {
Christopher Fauletafd8f102018-11-08 11:34:21 +01007834 memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled");
7835 return ACT_RET_PRS_ERR;
7836 }
7837
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007838 /* Memory for the rule. */
7839 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7840 if (!rule->arg.hlua_rule) {
7841 memprintf(err, "out of memory error");
7842 return ACT_RET_PRS_ERR;
7843 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007844
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007845 /* Reference the Lua function and store the reference. */
7846 rule->arg.hlua_rule->fcn = *fcn;
7847
7848 /* TODO: later accept arguments. */
7849 rule->arg.hlua_rule->args = NULL;
7850
7851 /* Add applet pointer in the rule. */
7852 rule->applet.obj_type = OBJ_TYPE_APPLET;
7853 rule->applet.name = fcn->name;
7854 rule->applet.init = hlua_applet_tcp_init;
7855 rule->applet.fct = hlua_applet_tcp_fct;
7856 rule->applet.release = hlua_applet_tcp_release;
7857 rule->applet.timeout = hlua_timeout_applet;
7858
7859 return 0;
7860}
7861
7862/* This function is an LUA binding used for registering
7863 * "sample-conv" functions. It expects a converter name used
7864 * in the haproxy configuration file, and an LUA function.
7865 */
7866__LJMP static int hlua_register_service(lua_State *L)
7867{
7868 struct action_kw_list *akl;
7869 const char *name;
7870 const char *env;
7871 int ref;
7872 int len;
7873 struct hlua_function *fcn;
7874
7875 MAY_LJMP(check_args(L, 3, "register_service"));
7876
7877 /* First argument : converter name. */
7878 name = MAY_LJMP(luaL_checkstring(L, 1));
7879
7880 /* Second argument : environment. */
7881 env = MAY_LJMP(luaL_checkstring(L, 2));
7882
7883 /* Third argument : lua function. */
7884 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7885
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007886 /* Allocate and fill the sample fetch keyword struct. */
7887 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7888 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007889 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007890 fcn = calloc(1, sizeof(*fcn));
7891 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007892 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007893
7894 /* Fill fcn. */
7895 len = strlen("<lua.>") + strlen(name) + 1;
7896 fcn->name = calloc(1, len);
7897 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007898 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007899 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7900 fcn->function_ref = ref;
7901
7902 /* List head */
7903 akl->list.n = akl->list.p = NULL;
7904
7905 /* converter keyword. */
7906 len = strlen("lua.") + strlen(name) + 1;
7907 akl->kw[0].kw = calloc(1, len);
7908 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007909 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007910
7911 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7912
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007913 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007914 if (strcmp(env, "tcp") == 0)
7915 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007916 else if (strcmp(env, "http") == 0)
7917 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007918 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007919 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007920 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007921
7922 akl->kw[0].match_pfx = 0;
7923 akl->kw[0].private = fcn;
7924
7925 /* End of array. */
7926 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7927
7928 /* Register this new converter */
7929 service_keywords_register(akl);
7930
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007931 return 0;
7932}
7933
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007934/* This function initialises Lua cli handler. It copies the
7935 * arguments in the Lua stack and create channel IO objects.
7936 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007937static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007938{
7939 struct hlua *hlua;
7940 struct hlua_function *fcn;
7941 int i;
7942 const char *error;
7943
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007944 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007945 appctx->ctx.hlua_cli.fcn = private;
7946
Willy Tarreaubafbe012017-11-24 17:34:44 +01007947 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007948 if (!hlua) {
7949 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007950 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007951 }
7952 HLUA_INIT(hlua);
7953 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007954
7955 /* Create task used by signal to wakeup applets.
7956 * We use the same wakeup fonction than the Lua applet_tcp and
7957 * applet_http. It is absolutely compatible.
7958 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007959 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007960 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007961 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007962 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007963 }
7964 appctx->ctx.hlua_cli.task->nice = 0;
7965 appctx->ctx.hlua_cli.task->context = appctx;
7966 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7967
7968 /* Initialises the Lua context */
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01007969 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007970 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007971 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007972 }
7973
7974 /* The following Lua calls can fail. */
7975 if (!SET_SAFE_LJMP(hlua->T)) {
7976 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7977 error = lua_tostring(hlua->T, -1);
7978 else
7979 error = "critical error";
7980 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7981 goto error;
7982 }
7983
7984 /* Check stack available size. */
7985 if (!lua_checkstack(hlua->T, 2)) {
7986 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7987 goto error;
7988 }
7989
7990 /* Restore the function in the stack. */
7991 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7992
7993 /* Once the arguments parsed, the CLI is like an AppletTCP,
7994 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007995 */
7996 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7997 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7998 goto error;
7999 }
8000 hlua->nargs = 1;
8001
8002 /* push keywords in the stack. */
8003 for (i = 0; *args[i]; i++) {
8004 /* Check stack available size. */
8005 if (!lua_checkstack(hlua->T, 1)) {
8006 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
8007 goto error;
8008 }
8009 lua_pushstring(hlua->T, args[i]);
8010 hlua->nargs++;
8011 }
8012
8013 /* We must initialize the execution timeouts. */
8014 hlua->max_time = hlua_timeout_session;
8015
8016 /* At this point the execution is safe. */
8017 RESET_SAFE_LJMP(hlua->T);
8018
8019 /* It's ok */
8020 return 0;
8021
8022 /* It's not ok. */
8023error:
8024 RESET_SAFE_LJMP(hlua->T);
8025 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01008026 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008027 return 1;
8028}
8029
8030static int hlua_cli_io_handler_fct(struct appctx *appctx)
8031{
8032 struct hlua *hlua;
8033 struct stream_interface *si;
8034 struct hlua_function *fcn;
8035
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008036 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008037 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01008038 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008039
8040 /* If the stream is disconnect or closed, ldo nothing. */
8041 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
8042 return 1;
8043
8044 /* Execute the function. */
8045 switch (hlua_ctx_resume(hlua, 1)) {
8046
8047 /* finished. */
8048 case HLUA_E_OK:
8049 return 1;
8050
8051 /* yield. */
8052 case HLUA_E_AGAIN:
8053 /* We want write. */
8054 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01008055 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008056 /* Set the timeout. */
8057 if (hlua->wake_time != TICK_ETERNITY)
8058 task_schedule(hlua->task, hlua->wake_time);
8059 return 0;
8060
8061 /* finished with error. */
8062 case HLUA_E_ERRMSG:
8063 /* Display log. */
8064 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
8065 fcn->name, lua_tostring(hlua->T, -1));
8066 lua_pop(hlua->T, 1);
8067 return 1;
8068
Thierry Fournierd5b073c2018-05-21 19:42:47 +02008069 case HLUA_E_ETMOUT:
8070 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
8071 fcn->name);
8072 return 1;
8073
8074 case HLUA_E_NOMEM:
8075 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
8076 fcn->name);
8077 return 1;
8078
8079 case HLUA_E_YIELD: /* unexpected */
8080 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8081 fcn->name);
8082 return 1;
8083
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008084 case HLUA_E_ERR:
8085 /* Display log. */
8086 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8087 fcn->name);
8088 return 1;
8089
8090 default:
8091 return 1;
8092 }
8093
8094 return 1;
8095}
8096
8097static void hlua_cli_io_release_fct(struct appctx *appctx)
8098{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008099 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008100 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008101}
8102
8103/* This function is an LUA binding used for registering
8104 * new keywords in the cli. It expects a list of keywords
8105 * which are the "path". It is limited to 5 keywords. A
8106 * description of the command, a function to be executed
8107 * for the parsing and a function for io handlers.
8108 */
8109__LJMP static int hlua_register_cli(lua_State *L)
8110{
8111 struct cli_kw_list *cli_kws;
8112 const char *message;
8113 int ref_io;
8114 int len;
8115 struct hlua_function *fcn;
8116 int index;
8117 int i;
8118
8119 MAY_LJMP(check_args(L, 3, "register_cli"));
8120
8121 /* First argument : an array of maximum 5 keywords. */
8122 if (!lua_istable(L, 1))
8123 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8124
8125 /* Second argument : string with contextual message. */
8126 message = MAY_LJMP(luaL_checkstring(L, 2));
8127
8128 /* Third and fourth argument : lua function. */
8129 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8130
8131 /* Allocate and fill the sample fetch keyword struct. */
8132 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
8133 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008134 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008135 fcn = calloc(1, sizeof(*fcn));
8136 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008137 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008138
8139 /* Fill path. */
8140 index = 0;
8141 lua_pushnil(L);
8142 while(lua_next(L, 1) != 0) {
8143 if (index >= 5)
8144 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8145 if (lua_type(L, -1) != LUA_TSTRING)
8146 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8147 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
8148 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008149 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008150 index++;
8151 lua_pop(L, 1);
8152 }
8153
8154 /* Copy help message. */
8155 cli_kws->kw[0].usage = strdup(message);
8156 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008157 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008158
8159 /* Fill fcn io handler. */
8160 len = strlen("<lua.cli>") + 1;
8161 for (i = 0; i < index; i++)
8162 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8163 fcn->name = calloc(1, len);
8164 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008165 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008166 strncat((char *)fcn->name, "<lua.cli", len);
8167 for (i = 0; i < index; i++) {
8168 strncat((char *)fcn->name, ".", len);
8169 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8170 }
8171 strncat((char *)fcn->name, ">", len);
8172 fcn->function_ref = ref_io;
8173
8174 /* Fill last entries. */
8175 cli_kws->kw[0].private = fcn;
8176 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8177 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8178 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8179
8180 /* Register this new converter */
8181 cli_register_kw(cli_kws);
8182
8183 return 0;
8184}
8185
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008186static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
8187 struct proxy *defpx, const char *file, int line,
8188 char **err, unsigned int *timeout)
8189{
8190 const char *error;
8191
8192 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008193 if (error == PARSE_TIME_OVER) {
8194 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8195 args[1], args[0]);
8196 return -1;
8197 }
8198 else if (error == PARSE_TIME_UNDER) {
8199 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8200 args[1], args[0]);
8201 return -1;
8202 }
8203 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008204 memprintf(err, "%s: invalid timeout", args[0]);
8205 return -1;
8206 }
8207 return 0;
8208}
8209
8210static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
8211 struct proxy *defpx, const char *file, int line,
8212 char **err)
8213{
8214 return hlua_read_timeout(args, section_type, curpx, defpx,
8215 file, line, err, &hlua_timeout_session);
8216}
8217
8218static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
8219 struct proxy *defpx, const char *file, int line,
8220 char **err)
8221{
8222 return hlua_read_timeout(args, section_type, curpx, defpx,
8223 file, line, err, &hlua_timeout_task);
8224}
8225
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008226static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
8227 struct proxy *defpx, const char *file, int line,
8228 char **err)
8229{
8230 return hlua_read_timeout(args, section_type, curpx, defpx,
8231 file, line, err, &hlua_timeout_applet);
8232}
8233
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008234static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
8235 struct proxy *defpx, const char *file, int line,
8236 char **err)
8237{
8238 char *error;
8239
8240 hlua_nb_instruction = strtoll(args[1], &error, 10);
8241 if (*error != '\0') {
8242 memprintf(err, "%s: invalid number", args[0]);
8243 return -1;
8244 }
8245 return 0;
8246}
8247
Willy Tarreau32f61e22015-03-18 17:54:59 +01008248static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
8249 struct proxy *defpx, const char *file, int line,
8250 char **err)
8251{
8252 char *error;
8253
8254 if (*(args[1]) == 0) {
8255 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8256 return -1;
8257 }
8258 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8259 if (*error != '\0') {
8260 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8261 return -1;
8262 }
8263 return 0;
8264}
8265
8266
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008267/* This function is called by the main configuration key "lua-load". It loads and
8268 * execute an lua file during the parsing of the HAProxy configuration file. It is
8269 * the main lua entry point.
8270 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008271 * This function runs with the HAProxy keywords API. It returns -1 if an error
8272 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008273 *
8274 * In some error case, LUA set an error message in top of the stack. This function
8275 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008276 *
8277 * This function can fail with an abort() due to an Lua critical error.
8278 * We are in the configuration parsing process of HAProxy, this abort() is
8279 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008280 */
8281static int hlua_load(char **args, int section_type, struct proxy *curpx,
8282 struct proxy *defpx, const char *file, int line,
8283 char **err)
8284{
8285 int error;
8286
8287 /* Just load and compile the file. */
8288 error = luaL_loadfile(gL.T, args[1]);
8289 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008290 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008291 lua_pop(gL.T, 1);
8292 return -1;
8293 }
8294
8295 /* If no syntax error where detected, execute the code. */
8296 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
8297 switch (error) {
8298 case LUA_OK:
8299 break;
8300 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008301 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008302 lua_pop(gL.T, 1);
8303 return -1;
8304 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008305 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008306 return -1;
8307 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008308 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008309 lua_pop(gL.T, 1);
8310 return -1;
Christopher Faulet20699902020-07-28 10:33:25 +02008311#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008312 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008313 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008314 lua_pop(gL.T, 1);
8315 return -1;
Christopher Faulet20699902020-07-28 10:33:25 +02008316#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008317 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008318 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008319 lua_pop(gL.T, 1);
8320 return -1;
8321 }
8322
8323 return 0;
8324}
8325
8326/* configuration keywords declaration */
8327static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008328 { CFG_GLOBAL, "lua-load", hlua_load },
8329 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8330 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008331 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008332 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008333 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008334 { 0, NULL, NULL },
8335}};
8336
Willy Tarreau0108d902018-11-25 19:14:37 +01008337INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8338
Christopher Fauletafd8f102018-11-08 11:34:21 +01008339
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008340/* This function can fail with an abort() due to an Lua critical error.
8341 * We are in the initialisation process of HAProxy, this abort() is
8342 * tolerated.
8343 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008344int hlua_post_init()
8345{
8346 struct hlua_init_function *init;
8347 const char *msg;
8348 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008349 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008350
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008351 /* Call post initialisation function in safe environement. */
8352 if (!SET_SAFE_LJMP(gL.T)) {
8353 if (lua_type(gL.T, -1) == LUA_TSTRING)
8354 error = lua_tostring(gL.T, -1);
8355 else
8356 error = "critical error";
8357 fprintf(stderr, "Lua post-init: %s.\n", error);
8358 exit(1);
8359 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008360
8361#if USE_OPENSSL
8362 /* Initialize SSL server. */
8363 if (socket_ssl.xprt->prepare_srv) {
8364 int saved_used_backed = global.ssl_used_backend;
8365 // don't affect maxconn automatic computation
8366 socket_ssl.xprt->prepare_srv(&socket_ssl);
8367 global.ssl_used_backend = saved_used_backed;
8368 }
8369#endif
8370
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008371 hlua_fcn_post_init(gL.T);
8372 RESET_SAFE_LJMP(gL.T);
8373
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008374 list_for_each_entry(init, &hlua_init_functions, l) {
8375 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
8376 ret = hlua_ctx_resume(&gL, 0);
8377 switch (ret) {
8378 case HLUA_E_OK:
8379 lua_pop(gL.T, -1);
8380 return 1;
8381 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008382 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008383 return 0;
8384 case HLUA_E_ERRMSG:
8385 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008386 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008387 return 0;
8388 case HLUA_E_ERR:
8389 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008390 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008391 return 0;
8392 }
8393 }
8394 return 1;
8395}
8396
Willy Tarreau32f61e22015-03-18 17:54:59 +01008397/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8398 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8399 * is the previously allocated size or the kind of object in case of a new
8400 * allocation. <nsize> is the requested new size.
8401 */
8402static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8403{
8404 struct hlua_mem_allocator *zone = ud;
8405
8406 if (nsize == 0) {
8407 /* it's a free */
8408 if (ptr)
8409 zone->allocated -= osize;
8410 free(ptr);
8411 return NULL;
8412 }
8413
8414 if (!ptr) {
8415 /* it's a new allocation */
8416 if (zone->limit && zone->allocated + nsize > zone->limit)
8417 return NULL;
8418
8419 ptr = malloc(nsize);
8420 if (ptr)
8421 zone->allocated += nsize;
8422 return ptr;
8423 }
8424
8425 /* it's a realloc */
8426 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
8427 return NULL;
8428
8429 ptr = realloc(ptr, nsize);
8430 if (ptr)
8431 zone->allocated += nsize - osize;
8432 return ptr;
8433}
8434
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008435/* Ithis function can fail with an abort() due to an Lua critical error.
8436 * We are in the initialisation process of HAProxy, this abort() is
8437 * tolerated.
8438 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008439void hlua_init(void)
8440{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008441 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008442 int idx;
8443 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008444 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008445 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008446 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008447#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008448 struct srv_kw *kw;
8449 int tmp_error;
8450 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008451 char *args[] = { /* SSL client configuration. */
8452 "ssl",
8453 "verify",
8454 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008455 NULL
8456 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008457#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008458
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008459 /* Init main lua stack. */
8460 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01008461 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01008462 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02008463 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008464 hlua_sethlua(&gL);
8465 gL.Tref = LUA_REFNIL;
8466 gL.task = NULL;
8467
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008468 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008469 * the Lua function can fail with an abort. We are in the initialisation
8470 * process of HAProxy, this abort() is tolerated.
8471 */
8472
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008473 /* Initialise lua. */
8474 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008475
Thierry Fournier75933d42016-01-21 09:30:18 +01008476 /* Set safe environment for the initialisation. */
8477 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01008478 if (lua_type(gL.T, -1) == LUA_TSTRING)
8479 error_msg = lua_tostring(gL.T, -1);
8480 else
8481 error_msg = "critical error";
8482 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01008483 exit(1);
8484 }
8485
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008486 /*
8487 *
8488 * Create "core" object.
8489 *
8490 */
8491
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008492 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008493 lua_newtable(gL.T);
8494
8495 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008496 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008497 hlua_class_const_int(gL.T, log_levels[i], i);
8498
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008499 /* Register special functions. */
8500 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01008501 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01008502 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01008503 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02008504 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008505 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008506 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01008507 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01008508 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01008509 hlua_class_function(gL.T, "sleep", hlua_sleep);
8510 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01008511 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
8512 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
8513 hlua_class_function(gL.T, "set_map", hlua_set_map);
8514 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008515 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01008516 hlua_class_function(gL.T, "log", hlua_log);
8517 hlua_class_function(gL.T, "Debug", hlua_log_debug);
8518 hlua_class_function(gL.T, "Info", hlua_log_info);
8519 hlua_class_function(gL.T, "Warning", hlua_log_warning);
8520 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02008521 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01008522 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008523
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008524 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008525
8526 /*
8527 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008528 * Register class Map
8529 *
8530 */
8531
8532 /* This table entry is the object "Map" base. */
8533 lua_newtable(gL.T);
8534
8535 /* register pattern types. */
8536 for (i=0; i<PAT_MATCH_NUM; i++)
8537 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008538 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008539 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
8540 hlua_class_const_int(gL.T, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008541 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008542
8543 /* register constructor. */
8544 hlua_class_function(gL.T, "new", hlua_map_new);
8545
8546 /* Create and fill the metatable. */
8547 lua_newtable(gL.T);
8548
8549 /* Create and fille the __index entry. */
8550 lua_pushstring(gL.T, "__index");
8551 lua_newtable(gL.T);
8552
8553 /* Register . */
8554 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
8555 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
8556
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008557 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008558
Thierry Fournier45e78d72016-02-19 18:34:46 +01008559 /* Register previous table in the registry with reference and named entry.
8560 * The function hlua_register_metatable() pops the stack, so we
8561 * previously create a copy of the table.
8562 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008563 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008564 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008565
8566 /* Assign the metatable to the mai Map object. */
8567 lua_setmetatable(gL.T, -2);
8568
8569 /* Set a name to the table. */
8570 lua_setglobal(gL.T, "Map");
8571
8572 /*
8573 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008574 * Register class Channel
8575 *
8576 */
8577
8578 /* Create and fill the metatable. */
8579 lua_newtable(gL.T);
8580
8581 /* Create and fille the __index entry. */
8582 lua_pushstring(gL.T, "__index");
8583 lua_newtable(gL.T);
8584
8585 /* Register . */
8586 hlua_class_function(gL.T, "get", hlua_channel_get);
8587 hlua_class_function(gL.T, "dup", hlua_channel_dup);
8588 hlua_class_function(gL.T, "getline", hlua_channel_getline);
8589 hlua_class_function(gL.T, "set", hlua_channel_set);
8590 hlua_class_function(gL.T, "append", hlua_channel_append);
8591 hlua_class_function(gL.T, "send", hlua_channel_send);
8592 hlua_class_function(gL.T, "forward", hlua_channel_forward);
8593 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
8594 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01008595 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008596
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008597 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008598
8599 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008600 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008601
8602 /*
8603 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008604 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008605 *
8606 */
8607
8608 /* Create and fill the metatable. */
8609 lua_newtable(gL.T);
8610
8611 /* Create and fille the __index entry. */
8612 lua_pushstring(gL.T, "__index");
8613 lua_newtable(gL.T);
8614
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008615 /* Browse existing fetches and create the associated
8616 * object method.
8617 */
8618 sf = NULL;
8619 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
8620
8621 /* Dont register the keywork if the arguments check function are
8622 * not safe during the runtime.
8623 */
8624 if ((sf->val_args != NULL) &&
8625 (sf->val_args != val_payload_lv) &&
8626 (sf->val_args != val_hdr))
8627 continue;
8628
8629 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8630 * by an underscore.
8631 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008632 strncpy(trash.area, sf->kw, trash.size);
8633 trash.area[trash.size - 1] = '\0';
8634 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008635 if (*p == '.' || *p == '-' || *p == '+')
8636 *p = '_';
8637
8638 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008639 lua_pushstring(gL.T, trash.area);
Willy Tarreau2ec22742015-03-10 14:27:20 +01008640 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008641 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008642 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008643 }
8644
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008645 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008646
8647 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008648 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008649
8650 /*
8651 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008652 * Register class Converters
8653 *
8654 */
8655
8656 /* Create and fill the metatable. */
8657 lua_newtable(gL.T);
8658
8659 /* Create and fill the __index entry. */
8660 lua_pushstring(gL.T, "__index");
8661 lua_newtable(gL.T);
8662
8663 /* Browse existing converters and create the associated
8664 * object method.
8665 */
8666 sc = NULL;
8667 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
8668 /* Dont register the keywork if the arguments check function are
8669 * not safe during the runtime.
8670 */
8671 if (sc->val_args != NULL)
8672 continue;
8673
8674 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8675 * by an underscore.
8676 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008677 strncpy(trash.area, sc->kw, trash.size);
8678 trash.area[trash.size - 1] = '\0';
8679 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008680 if (*p == '.' || *p == '-' || *p == '+')
8681 *p = '_';
8682
8683 /* Register the function. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008684 lua_pushstring(gL.T, trash.area);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008685 lua_pushlightuserdata(gL.T, sc);
8686 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008687 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008688 }
8689
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008690 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008691
8692 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008693 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008694
8695 /*
8696 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008697 * Register class HTTP
8698 *
8699 */
8700
8701 /* Create and fill the metatable. */
8702 lua_newtable(gL.T);
8703
8704 /* Create and fille the __index entry. */
8705 lua_pushstring(gL.T, "__index");
8706 lua_newtable(gL.T);
8707
8708 /* Register Lua functions. */
8709 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
8710 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
8711 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
8712 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
8713 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
8714 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
8715 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
8716 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
8717 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
8718 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
8719
8720 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
8721 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
8722 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
8723 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
8724 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
8725 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02008726 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008727
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008728 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008729
8730 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008731 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008732
8733 /*
8734 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008735 * Register class AppletTCP
8736 *
8737 */
8738
8739 /* Create and fill the metatable. */
8740 lua_newtable(gL.T);
8741
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008742 /* Create and fille the __index entry. */
8743 lua_pushstring(gL.T, "__index");
8744 lua_newtable(gL.T);
8745
8746 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01008747 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
8748 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
8749 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
8750 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
8751 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008752 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
8753 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
8754 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008755
8756 lua_settable(gL.T, -3);
8757
8758 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008759 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008760
8761 /*
8762 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008763 * Register class AppletHTTP
8764 *
8765 */
8766
8767 /* Create and fill the metatable. */
8768 lua_newtable(gL.T);
8769
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008770 /* Create and fille the __index entry. */
8771 lua_pushstring(gL.T, "__index");
8772 lua_newtable(gL.T);
8773
8774 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01008775 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
8776 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01008777 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
8778 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
8779 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008780 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
8781 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
8782 hlua_class_function(gL.T, "send", hlua_applet_http_send);
8783 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
8784 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
8785 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
8786
8787 lua_settable(gL.T, -3);
8788
8789 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008790 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02008791
8792 /*
8793 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008794 * Register class TXN
8795 *
8796 */
8797
8798 /* Create and fill the metatable. */
8799 lua_newtable(gL.T);
8800
8801 /* Create and fille the __index entry. */
8802 lua_pushstring(gL.T, "__index");
8803 lua_newtable(gL.T);
8804
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008805 /* Register Lua functions. */
Patrick Hemmer268a7072018-05-11 12:52:31 -04008806 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
8807 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
8808 hlua_class_function(gL.T, "set_var", hlua_set_var);
8809 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
8810 hlua_class_function(gL.T, "get_var", hlua_get_var);
8811 hlua_class_function(gL.T, "done", hlua_txn_done);
8812 hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel);
8813 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
8814 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
8815 hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class);
8816 hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset);
8817 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
8818 hlua_class_function(gL.T, "log", hlua_txn_log);
8819 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
8820 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
8821 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
8822 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01008823
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008824 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008825
8826 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008827 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008828
8829 /*
8830 *
8831 * Register class Socket
8832 *
8833 */
8834
8835 /* Create and fill the metatable. */
8836 lua_newtable(gL.T);
8837
8838 /* Create and fille the __index entry. */
8839 lua_pushstring(gL.T, "__index");
8840 lua_newtable(gL.T);
8841
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008842#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008843 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01008844#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008845 hlua_class_function(gL.T, "connect", hlua_socket_connect);
8846 hlua_class_function(gL.T, "send", hlua_socket_send);
8847 hlua_class_function(gL.T, "receive", hlua_socket_receive);
8848 hlua_class_function(gL.T, "close", hlua_socket_close);
8849 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
8850 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
8851 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
8852 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
8853
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008854 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008855
8856 /* Register the garbage collector entry. */
8857 lua_pushstring(gL.T, "__gc");
8858 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02008859 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008860
8861 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01008862 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008863
8864 /* Proxy and server configuration initialisation. */
8865 memset(&socket_proxy, 0, sizeof(socket_proxy));
8866 init_new_proxy(&socket_proxy);
8867 socket_proxy.parent = NULL;
8868 socket_proxy.last_change = now.tv_sec;
8869 socket_proxy.id = "LUA-SOCKET";
8870 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
8871 socket_proxy.maxconn = 0;
8872 socket_proxy.accept = NULL;
8873 socket_proxy.options2 |= PR_O2_INDEPSTR;
8874 socket_proxy.srv = NULL;
8875 socket_proxy.conn_retries = 0;
8876 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
8877
8878 /* Init TCP server: unchanged parameters */
8879 memset(&socket_tcp, 0, sizeof(socket_tcp));
8880 socket_tcp.next = NULL;
8881 socket_tcp.proxy = &socket_proxy;
8882 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8883 LIST_INIT(&socket_tcp.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008884 socket_tcp.pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02008885 socket_tcp.priv_conns = NULL;
8886 socket_tcp.idle_conns = NULL;
8887 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008888 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008889 socket_tcp.last_change = 0;
8890 socket_tcp.id = "LUA-TCP-CONN";
8891 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8892 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8893 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8894
8895 /* XXX: Copy default parameter from default server,
8896 * but the default server is not initialized.
8897 */
8898 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8899 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8900 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8901 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8902 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8903 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8904 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8905 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8906 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8907 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8908
8909 socket_tcp.check.status = HCHK_STATUS_INI;
8910 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8911 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8912 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8913 socket_tcp.check.server = &socket_tcp;
8914
8915 socket_tcp.agent.status = HCHK_STATUS_INI;
8916 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8917 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8918 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8919 socket_tcp.agent.server = &socket_tcp;
8920
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008921 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008922
8923#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008924 /* Init TCP server: unchanged parameters */
8925 memset(&socket_ssl, 0, sizeof(socket_ssl));
8926 socket_ssl.next = NULL;
8927 socket_ssl.proxy = &socket_proxy;
8928 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8929 LIST_INIT(&socket_ssl.actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04008930 socket_ssl.pendconns = EB_ROOT;
Willy Tarreaub784b352019-02-07 14:48:24 +01008931 socket_ssl.priv_conns = NULL;
8932 socket_ssl.idle_conns = NULL;
8933 socket_ssl.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008934 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008935 socket_ssl.last_change = 0;
8936 socket_ssl.id = "LUA-SSL-CONN";
8937 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8938 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8939 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8940
8941 /* XXX: Copy default parameter from default server,
8942 * but the default server is not initialized.
8943 */
8944 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8945 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8946 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8947 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8948 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8949 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8950 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8951 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8952 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8953 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8954
8955 socket_ssl.check.status = HCHK_STATUS_INI;
8956 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8957 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8958 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8959 socket_ssl.check.server = &socket_ssl;
8960
8961 socket_ssl.agent.status = HCHK_STATUS_INI;
8962 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8963 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8964 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8965 socket_ssl.agent.server = &socket_ssl;
8966
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008967 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008968 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008969
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008970 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008971 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8972 /*
8973 *
8974 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008975 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008976 * features like client certificates and ssl_verify.
8977 *
8978 */
8979 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8980 if (tmp_error != 0) {
8981 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8982 abort(); /* This must be never arrives because the command line
8983 not editable by the user. */
8984 }
8985 idx += kw->skip;
8986 }
8987 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008988#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008989
8990 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008991}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008992
Willy Tarreau80713382018-11-26 10:19:54 +01008993static void hlua_register_build_options(void)
8994{
Willy Tarreaubb57d942016-12-21 19:04:56 +01008995 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01008996
Willy Tarreaubb57d942016-12-21 19:04:56 +01008997 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8998 hap_register_build_opts(ptr, 1);
8999}
Willy Tarreau80713382018-11-26 10:19:54 +01009000
9001INITCALL0(STG_REGISTER, hlua_register_build_options);