blob: 2f7fe99605a0027df83858075271076789d35fc6 [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>
Thierry FOURNIER2da788e2017-09-11 18:37:23 +020028#include <common/xref.h>
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +020029#include <common/hathreads.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010030
William Lallemand9ed62032016-11-21 17:49:11 +010031#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010032#include <types/hlua.h>
33#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010034#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010035
Thierry FOURNIER55da1652015-01-23 11:36:30 +010036#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020037#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010038#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010039#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010040#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010041#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010042#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010043#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010044#include <proto/hlua_fcn.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020045#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010046#include <proto/obj_type.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010047#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010048#include <proto/payload.h>
49#include <proto/proto_http.h>
50#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010051#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020052#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020053#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010054#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010055#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010056#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020057#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010058
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010059/* Lua uses longjmp to perform yield or throwing errors. This
60 * macro is used only for identifying the function that can
61 * not return because a longjmp is executed.
62 * __LJMP marks a prototype of hlua file that can use longjmp.
63 * WILL_LJMP() marks an lua function that will use longjmp.
64 * MAY_LJMP() marks an lua function that may use longjmp.
65 */
66#define __LJMP
67#define WILL_LJMP(func) func
68#define MAY_LJMP(func) func
69
Thierry FOURNIERbabae282015-09-17 11:36:37 +020070/* This couple of function executes securely some Lua calls outside of
71 * the lua runtime environment. Each Lua call can return a longjmp
72 * if it encounter a memory error.
73 *
74 * Lua documentation extract:
75 *
76 * If an error happens outside any protected environment, Lua calls
77 * a panic function (see lua_atpanic) and then calls abort, thus
78 * exiting the host application. Your panic function can avoid this
79 * exit by never returning (e.g., doing a long jump to your own
80 * recovery point outside Lua).
81 *
82 * The panic function runs as if it were a message handler (see
83 * §2.3); in particular, the error message is at the top of the
84 * stack. However, there is no guarantee about stack space. To push
85 * anything on the stack, the panic function must first check the
86 * available space (see §4.2).
87 *
88 * We must check all the Lua entry point. This includes:
89 * - The include/proto/hlua.h exported functions
90 * - the task wrapper function
91 * - The action wrapper function
92 * - The converters wrapper function
93 * - The sample-fetch wrapper functions
94 *
95 * It is tolerated that the initilisation function returns an abort.
96 * Before each Lua abort, an error message is writed on stderr.
97 *
98 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
99 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
100 * because they must be exists in the program stack when the longjmp
101 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200102 *
103 * Note that the Lua processing is not really thread safe. It provides
104 * heavy system which consists to add our own lock function in the Lua
105 * code and recompile the library. This system will probably not accepted
106 * by maintainers of various distribs.
107 *
108 * Our main excution point of the Lua is the function lua_resume(). A
109 * quick looking on the Lua sources displays a lua_lock() a the start
110 * of function and a lua_unlock() at the end of the function. So I
111 * conclude that the Lua thread safe mode just perform a mutex around
112 * all execution. So I prefer to do this in the HAProxy code, it will be
113 * easier for distro maintainers.
114 *
115 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
116 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
117 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200118 */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100119__decl_hathreads(HA_SPINLOCK_T hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200120THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200121static int hlua_panic_safe(lua_State *L) { return 0; }
122static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
123
124#define SET_SAFE_LJMP(__L) \
125 ({ \
126 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100127 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128 if (setjmp(safe_ljmp_env) != 0) { \
129 lua_atpanic(__L, hlua_panic_safe); \
130 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100131 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200132 } else { \
133 lua_atpanic(__L, hlua_panic_ljmp); \
134 ret = 1; \
135 } \
136 ret; \
137 })
138
139/* If we are the last function catching Lua errors, we
140 * must reset the panic function.
141 */
142#define RESET_SAFE_LJMP(__L) \
143 do { \
144 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100145 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200146 } while(0)
147
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200148/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200149#define APPLET_DONE 0x01 /* applet processing is done. */
150#define APPLET_100C 0x02 /* 100 continue expected. */
151#define APPLET_HDR_SENT 0x04 /* Response header sent. */
152#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
153#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100154#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200155
156#define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n"
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200157
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100158/* The main Lua execution context. */
159struct hlua gL;
160
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100161/* This is the memory pool containing struct lua for applets
162 * (including cli).
163 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100164struct pool_head *pool_head_hlua;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100165
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100166/* Used for Socket connection. */
167static struct proxy socket_proxy;
168static struct server socket_tcp;
169#ifdef USE_OPENSSL
170static struct server socket_ssl;
171#endif
172
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100173/* List head of the function called at the initialisation time. */
174struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
175
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100176/* The following variables contains the reference of the different
177 * Lua classes. These references are useful for identify metadata
178 * associated with an object.
179 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100180static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100181static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100182static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100183static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100184static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100185static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200186static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200187static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200188static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100189
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100190/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200191 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100192 * short timeout. Lua linked with tasks doesn't have a timeout
193 * because a task may remain alive during all the haproxy execution.
194 */
195static unsigned int hlua_timeout_session = 4000; /* session timeout. */
196static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200197static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100198
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100199/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
200 * it is used for preventing infinite loops.
201 *
202 * I test the scheer with an infinite loop containing one incrementation
203 * and one test. I run this loop between 10 seconds, I raise a ceil of
204 * 710M loops from one interrupt each 9000 instructions, so I fix the value
205 * to one interrupt each 10 000 instructions.
206 *
207 * configured | Number of
208 * instructions | loops executed
209 * between two | in milions
210 * forced yields |
211 * ---------------+---------------
212 * 10 | 160
213 * 500 | 670
214 * 1000 | 680
215 * 5000 | 700
216 * 7000 | 700
217 * 8000 | 700
218 * 9000 | 710 <- ceil
219 * 10000 | 710
220 * 100000 | 710
221 * 1000000 | 710
222 *
223 */
224static unsigned int hlua_nb_instruction = 10000;
225
Willy Tarreau32f61e22015-03-18 17:54:59 +0100226/* Descriptor for the memory allocation state. If limit is not null, it will
227 * be enforced on any memory allocation.
228 */
229struct hlua_mem_allocator {
230 size_t allocated;
231 size_t limit;
232};
233
234static struct hlua_mem_allocator hlua_global_allocator;
235
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200236static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200237 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200238 "Cache-Control: no-cache\r\n"
239 "Connection: close\r\n"
240 "Content-Type: text/html\r\n"
241 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200242 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200243
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100244/* These functions converts types between HAProxy internal args or
245 * sample and LUA types. Another function permits to check if the
246 * LUA stack contains arguments according with an required ARG_T
247 * format.
248 */
249static int hlua_arg2lua(lua_State *L, const struct arg *arg);
250static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100251__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100252 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100253static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100254static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100255static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
256
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200257__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
258
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200259#define SEND_ERR(__be, __fmt, __args...) \
260 do { \
261 send_log(__be, LOG_ERR, __fmt, ## __args); \
262 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100263 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200264 } while (0)
265
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100266/* Used to check an Lua function type in the stack. It creates and
267 * returns a reference of the function. This function throws an
268 * error if the rgument is not a "function".
269 */
270__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
271{
272 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100273 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100274 WILL_LJMP(luaL_argerror(L, argno, msg));
275 }
276 lua_pushvalue(L, argno);
277 return luaL_ref(L, LUA_REGISTRYINDEX);
278}
279
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200280/* Return the string that is of the top of the stack. */
281const char *hlua_get_top_error_string(lua_State *L)
282{
283 if (lua_gettop(L) < 1)
284 return "unknown error";
285 if (lua_type(L, -1) != LUA_TSTRING)
286 return "unknown error";
287 return lua_tostring(L, -1);
288}
289
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100290/* This function check the number of arguments available in the
291 * stack. If the number of arguments available is not the same
292 * then <nb> an error is throwed.
293 */
294__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
295{
296 if (lua_gettop(L) == nb)
297 return;
298 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
299}
300
Mark Lakes22154b42018-01-29 14:38:40 -0800301/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100302 * and the line number where the error is encountered.
303 */
304static int hlua_pusherror(lua_State *L, const char *fmt, ...)
305{
306 va_list argp;
307 va_start(argp, fmt);
308 luaL_where(L, 1);
309 lua_pushvfstring(L, fmt, argp);
310 va_end(argp);
311 lua_concat(L, 2);
312 return 1;
313}
314
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100315/* This functions is used with sample fetch and converters. It
316 * converts the HAProxy configuration argument in a lua stack
317 * values.
318 *
319 * It takes an array of "arg", and each entry of the array is
320 * converted and pushed in the LUA stack.
321 */
322static int hlua_arg2lua(lua_State *L, const struct arg *arg)
323{
324 switch (arg->type) {
325 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100326 case ARGT_TIME:
327 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100328 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100329 break;
330
331 case ARGT_STR:
332 lua_pushlstring(L, arg->data.str.str, arg->data.str.len);
333 break;
334
335 case ARGT_IPV4:
336 case ARGT_IPV6:
337 case ARGT_MSK4:
338 case ARGT_MSK6:
339 case ARGT_FE:
340 case ARGT_BE:
341 case ARGT_TAB:
342 case ARGT_SRV:
343 case ARGT_USR:
344 case ARGT_MAP:
345 default:
346 lua_pushnil(L);
347 break;
348 }
349 return 1;
350}
351
352/* This function take one entrie in an LUA stack at the index "ud",
353 * and try to convert it in an HAProxy argument entry. This is useful
354 * with sample fetch wrappers. The input arguments are gived to the
355 * lua wrapper and converted as arg list by thi function.
356 */
357static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
358{
359 switch (lua_type(L, ud)) {
360
361 case LUA_TNUMBER:
362 case LUA_TBOOLEAN:
363 arg->type = ARGT_SINT;
364 arg->data.sint = lua_tointeger(L, ud);
365 break;
366
367 case LUA_TSTRING:
368 arg->type = ARGT_STR;
369 arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len);
370 break;
371
372 case LUA_TUSERDATA:
373 case LUA_TNIL:
374 case LUA_TTABLE:
375 case LUA_TFUNCTION:
376 case LUA_TTHREAD:
377 case LUA_TLIGHTUSERDATA:
378 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200379 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100380 break;
381 }
382 return 1;
383}
384
385/* the following functions are used to convert a struct sample
386 * in Lua type. This useful to convert the return of the
387 * fetchs or converters.
388 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100389static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100390{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200391 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100392 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100393 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200394 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100395 break;
396
397 case SMP_T_BIN:
398 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200399 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100400 break;
401
402 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200403 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100404 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
405 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
406 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
407 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
408 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
409 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
410 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
411 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
412 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200413 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100414 break;
415 default:
416 lua_pushnil(L);
417 break;
418 }
419 break;
420
421 case SMP_T_IPV4:
422 case SMP_T_IPV6:
423 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200424 if (sample_casts[smp->data.type][SMP_T_STR] &&
425 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200426 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100427 else
428 lua_pushnil(L);
429 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100430 default:
431 lua_pushnil(L);
432 break;
433 }
434 return 1;
435}
436
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100437/* the following functions are used to convert a struct sample
438 * in Lua strings. This is useful to convert the return of the
439 * fetchs or converters.
440 */
441static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
442{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200443 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100444
445 case SMP_T_BIN:
446 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200447 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100448 break;
449
450 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200451 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100452 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
453 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
454 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
455 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
456 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
457 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
458 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
459 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
460 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200461 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100462 break;
463 default:
464 lua_pushstring(L, "");
465 break;
466 }
467 break;
468
469 case SMP_T_SINT:
470 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100471 case SMP_T_IPV4:
472 case SMP_T_IPV6:
473 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200474 if (sample_casts[smp->data.type][SMP_T_STR] &&
475 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200476 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100477 else
478 lua_pushstring(L, "");
479 break;
480 default:
481 lua_pushstring(L, "");
482 break;
483 }
484 return 1;
485}
486
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100487/* the following functions are used to convert an Lua type in a
488 * struct sample. This is useful to provide data from a converter
489 * to the LUA code.
490 */
491static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
492{
493 switch (lua_type(L, ud)) {
494
495 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200496 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200497 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100498 break;
499
500
501 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200502 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200503 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100504 break;
505
506 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200507 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100508 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200509 smp->data.u.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100510 break;
511
512 case LUA_TUSERDATA:
513 case LUA_TNIL:
514 case LUA_TTABLE:
515 case LUA_TFUNCTION:
516 case LUA_TTHREAD:
517 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200518 case LUA_TNONE:
519 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200520 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200521 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100522 break;
523 }
524 return 1;
525}
526
527/* This function check the "argp" builded by another conversion function
528 * is in accord with the expected argp defined by the "mask". The fucntion
529 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100530 *
531 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
532 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100533 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100534__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100535 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100536{
537 int min_arg;
538 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100539 struct proxy *px;
540 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100541
542 idx = 0;
543 min_arg = ARGM(mask);
544 mask >>= ARGM_BITS;
545
546 while (1) {
547
548 /* Check oversize. */
549 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100550 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100551 }
552
553 /* Check for mandatory arguments. */
554 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100555 if (idx < min_arg) {
556
557 /* If miss other argument than the first one, we return an error. */
558 if (idx > 0)
559 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
560
561 /* If first argument have a certain type, some default values
562 * may be used. See the function smp_resolve_args().
563 */
564 switch (mask & ARGT_MASK) {
565
566 case ARGT_FE:
567 if (!(p->cap & PR_CAP_FE))
568 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
569 argp[idx].data.prx = p;
570 argp[idx].type = ARGT_FE;
571 argp[idx+1].type = ARGT_STOP;
572 break;
573
574 case ARGT_BE:
575 if (!(p->cap & PR_CAP_BE))
576 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
577 argp[idx].data.prx = p;
578 argp[idx].type = ARGT_BE;
579 argp[idx+1].type = ARGT_STOP;
580 break;
581
582 case ARGT_TAB:
583 argp[idx].data.prx = p;
584 argp[idx].type = ARGT_TAB;
585 argp[idx+1].type = ARGT_STOP;
586 break;
587
588 default:
589 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
590 break;
591 }
592 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100593 return 0;
594 }
595
596 /* Check for exceed the number of requiered argument. */
597 if ((mask & ARGT_MASK) == ARGT_STOP &&
598 argp[idx].type != ARGT_STOP) {
599 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
600 }
601
602 if ((mask & ARGT_MASK) == ARGT_STOP &&
603 argp[idx].type == ARGT_STOP) {
604 return 0;
605 }
606
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100607 /* Convert some argument types. */
608 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100609 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100610 if (argp[idx].type != ARGT_SINT)
611 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
612 argp[idx].type = ARGT_SINT;
613 break;
614
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100615 case ARGT_TIME:
616 if (argp[idx].type != ARGT_SINT)
617 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200618 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100619 break;
620
621 case ARGT_SIZE:
622 if (argp[idx].type != ARGT_SINT)
623 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200624 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100625 break;
626
627 case ARGT_FE:
628 if (argp[idx].type != ARGT_STR)
629 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
630 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
631 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200632 argp[idx].data.prx = proxy_fe_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100633 if (!argp[idx].data.prx)
634 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
635 argp[idx].type = ARGT_FE;
636 break;
637
638 case ARGT_BE:
639 if (argp[idx].type != ARGT_STR)
640 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
641 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
642 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200643 argp[idx].data.prx = proxy_be_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100644 if (!argp[idx].data.prx)
645 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
646 argp[idx].type = ARGT_BE;
647 break;
648
649 case ARGT_TAB:
650 if (argp[idx].type != ARGT_STR)
651 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
652 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
653 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200654 argp[idx].data.prx = proxy_tbl_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100655 if (!argp[idx].data.prx)
656 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
657 argp[idx].type = ARGT_TAB;
658 break;
659
660 case ARGT_SRV:
661 if (argp[idx].type != ARGT_STR)
662 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
663 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
664 trash.str[argp[idx].data.str.len] = 0;
665 sname = strrchr(trash.str, '/');
666 if (sname) {
667 *sname++ = '\0';
668 pname = trash.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200669 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100670 if (!px)
671 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
672 }
673 else {
674 sname = trash.str;
675 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100676 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100677 argp[idx].data.srv = findserver(px, sname);
678 if (!argp[idx].data.srv)
679 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
680 argp[idx].type = ARGT_SRV;
681 break;
682
683 case ARGT_IPV4:
684 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
685 trash.str[argp[idx].data.str.len] = 0;
686 if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4))
687 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
688 argp[idx].type = ARGT_IPV4;
689 break;
690
691 case ARGT_MSK4:
692 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
693 trash.str[argp[idx].data.str.len] = 0;
694 if (!str2mask(trash.str, &argp[idx].data.ipv4))
695 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
696 argp[idx].type = ARGT_MSK4;
697 break;
698
699 case ARGT_IPV6:
700 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
701 trash.str[argp[idx].data.str.len] = 0;
702 if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6))
703 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
704 argp[idx].type = ARGT_IPV6;
705 break;
706
707 case ARGT_MSK6:
Tim Duesterhusb814da62018-01-25 16:24:50 +0100708 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
709 trash.str[argp[idx].data.str.len] = 0;
710 if (!str2mask6(trash.str, &argp[idx].data.ipv6))
711 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
712 argp[idx].type = ARGT_MSK6;
713 break;
714
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100715 case ARGT_MAP:
716 case ARGT_REG:
717 case ARGT_USR:
718 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100719 break;
720 }
721
722 /* Check for type of argument. */
723 if ((mask & ARGT_MASK) != argp[idx].type) {
724 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
725 arg_type_names[(mask & ARGT_MASK)],
726 arg_type_names[argp[idx].type & ARGT_MASK]);
727 WILL_LJMP(luaL_argerror(L, first + idx, msg));
728 }
729
730 /* Next argument. */
731 mask >>= ARGT_BITS;
732 idx++;
733 }
734}
735
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100736/*
737 * The following functions are used to make correspondance between the the
738 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100739 *
740 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100741 * - hlua_sethlua : create the association between hlua context and lua_state.
742 */
743static inline struct hlua *hlua_gethlua(lua_State *L)
744{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100745 struct hlua **hlua = lua_getextraspace(L);
746 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100747}
748static inline void hlua_sethlua(struct hlua *hlua)
749{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100750 struct hlua **hlua_store = lua_getextraspace(hlua->T);
751 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100752}
753
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100754/* This function is used to send logs. It try to send on screen (stderr)
755 * and on the default syslog server.
756 */
757static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
758{
759 struct tm tm;
760 char *p;
761
762 /* Cleanup the log message. */
763 p = trash.str;
764 for (; *msg != '\0'; msg++, p++) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200765 if (p >= trash.str + trash.size - 1) {
766 /* Break the message if exceed the buffer size. */
767 *(p-4) = ' ';
768 *(p-3) = '.';
769 *(p-2) = '.';
770 *(p-1) = '.';
771 break;
772 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100773 if (isprint(*msg))
774 *p = *msg;
775 else
776 *p = '.';
777 }
778 *p = '\0';
779
Thierry FOURNIER5554e292015-09-09 11:21:37 +0200780 send_log(px, level, "%s\n", trash.str);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100781 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200782 get_localtime(date.tv_sec, &tm);
783 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100784 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
785 (int)getpid(), trash.str);
786 fflush(stderr);
787 }
788}
789
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100790/* This function just ensure that the yield will be always
791 * returned with a timeout and permit to set some flags
792 */
793__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100794 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100795{
796 struct hlua *hlua = hlua_gethlua(L);
797
798 /* Set the wake timeout. If timeout is required, we set
799 * the expiration time.
800 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200801 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100802
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100803 hlua->flags |= flags;
804
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100805 /* Process the yield. */
806 WILL_LJMP(lua_yieldk(L, nresults, ctx, k));
807}
808
Willy Tarreau87b09662015-04-03 00:22:06 +0200809/* This function initialises the Lua environment stored in the stream.
810 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100811 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200812 *
813 * This function is particular. it initialises a new Lua thread. If the
814 * initialisation fails (example: out of memory error), the lua function
815 * throws an error (longjmp).
816 *
817 * This function manipulates two Lua stack: the main and the thread. Only
818 * the main stack can fail. The thread is not manipulated. This function
819 * MUST NOT manipulate the created thread stack state, because is not
820 * proctected agains error throwed by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100821 */
822int hlua_ctx_init(struct hlua *lua, struct task *task)
823{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200824 if (!SET_SAFE_LJMP(gL.T)) {
825 lua->Tref = LUA_REFNIL;
826 return 0;
827 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100828 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100829 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100830 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100831 lua->T = lua_newthread(gL.T);
832 if (!lua->T) {
833 lua->Tref = LUA_REFNIL;
Thierry FOURNIER0a976202017-07-12 11:18:00 +0200834 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100835 return 0;
836 }
837 hlua_sethlua(lua);
838 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
839 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200840 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100841 return 1;
842}
843
Willy Tarreau87b09662015-04-03 00:22:06 +0200844/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100845 * is destroyed. The destroy also the memory context. The struct "lua"
846 * is not freed.
847 */
848void hlua_ctx_destroy(struct hlua *lua)
849{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100850 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100851 return;
852
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100853 if (!lua->T)
854 goto end;
855
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100856 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200857 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100858
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200859 if (!SET_SAFE_LJMP(lua->T))
860 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100861 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200862 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200863
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200864 if (!SET_SAFE_LJMP(gL.T))
865 return;
866 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
867 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200868 /* Forces a garbage collecting process. If the Lua program is finished
869 * without error, we run the GC on the thread pointer. Its freed all
870 * the unused memory.
871 * If the thread is finnish with an error or is currently yielded,
872 * it seems that the GC applied on the thread doesn't clean anything,
873 * so e run the GC on the main thread.
874 * NOTE: maybe this action locks all the Lua threads untiml the en of
875 * the garbage collection.
876 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200877 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200878 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200879 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200880 lua_gc(gL.T, LUA_GCCOLLECT, 0);
881 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200882 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200883
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200884 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100885
886end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100887 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100888}
889
890/* This function is used to restore the Lua context when a coroutine
891 * fails. This function copy the common memory between old coroutine
892 * and the new coroutine. The old coroutine is destroyed, and its
893 * replaced by the new coroutine.
894 * If the flag "keep_msg" is set, the last entry of the old is assumed
895 * as string error message and it is copied in the new stack.
896 */
897static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
898{
899 lua_State *T;
900 int new_ref;
901
902 /* Renew the main LUA stack doesn't have sense. */
903 if (lua == &gL)
904 return 0;
905
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100906 /* New Lua coroutine. */
907 T = lua_newthread(gL.T);
908 if (!T)
909 return 0;
910
911 /* Copy last error message. */
912 if (keep_msg)
913 lua_xmove(lua->T, T, 1);
914
915 /* Copy data between the coroutines. */
916 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
917 lua_xmove(lua->T, T, 1);
918 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
919
920 /* Destroy old data. */
921 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
922
923 /* The thread is garbage collected by Lua. */
924 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
925
926 /* Fill the struct with the new coroutine values. */
927 lua->Mref = new_ref;
928 lua->T = T;
929 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
930
931 /* Set context. */
932 hlua_sethlua(lua);
933
934 return 1;
935}
936
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100937void hlua_hook(lua_State *L, lua_Debug *ar)
938{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100939 struct hlua *hlua = hlua_gethlua(L);
940
941 /* Lua cannot yield when its returning from a function,
942 * so, we can fix the interrupt hook to 1 instruction,
943 * expecting that the function is finnished.
944 */
945 if (lua_gethookmask(L) & LUA_MASKRET) {
946 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
947 return;
948 }
949
950 /* restore the interrupt condition. */
951 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
952
953 /* If we interrupt the Lua processing in yieldable state, we yield.
954 * If the state is not yieldable, trying yield causes an error.
955 */
956 if (lua_isyieldable(L))
957 WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
958
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +0100959 /* If we cannot yield, update the clock and check the timeout. */
960 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200961 hlua->run_time += now_ms - hlua->start_time;
962 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100963 lua_pushfstring(L, "execution timeout");
964 WILL_LJMP(lua_error(L));
965 }
966
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200967 /* Update the start time. */
968 hlua->start_time = now_ms;
969
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100970 /* Try to interrupt the process at the end of the current
971 * unyieldable function.
972 */
973 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100974}
975
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100976/* This function start or resumes the Lua stack execution. If the flag
977 * "yield_allowed" if no set and the LUA stack execution returns a yield
978 * The function return an error.
979 *
980 * The function can returns 4 values:
981 * - HLUA_E_OK : The execution is terminated without any errors.
982 * - HLUA_E_AGAIN : The execution must continue at the next associated
983 * task wakeup.
984 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
985 * the top of the stack.
986 * - HLUA_E_ERR : An error has occured without error message.
987 *
988 * If an error occured, the stack is renewed and it is ready to run new
989 * LUA code.
990 */
991static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
992{
993 int ret;
994 const char *msg;
995
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200996 /* Initialise run time counter. */
997 if (!HLUA_IS_RUNNING(lua))
998 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100999
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001000 /* Lock the whole Lua execution. This lock must be before the
1001 * label "resume_execution".
1002 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001003 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001004
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001005resume_execution:
1006
1007 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1008 * instructions. it is used for preventing infinite loops.
1009 */
1010 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1011
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001012 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001013 HLUA_SET_RUN(lua);
1014 HLUA_CLR_CTRLYIELD(lua);
1015 HLUA_CLR_WAKERESWR(lua);
1016 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001017
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001018 /* Update the start time. */
1019 lua->start_time = now_ms;
1020
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001021 /* Call the function. */
1022 ret = lua_resume(lua->T, gL.T, lua->nargs);
1023 switch (ret) {
1024
1025 case LUA_OK:
1026 ret = HLUA_E_OK;
1027 break;
1028
1029 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001030 /* Check if the execution timeout is expired. It it is the case, we
1031 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001032 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001033 tv_update_date(0, 1);
1034 lua->run_time += now_ms - lua->start_time;
1035 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001036 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001037 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001038 break;
1039 }
1040 /* Process the forced yield. if the general yield is not allowed or
1041 * if no task were associated this the current Lua execution
1042 * coroutine, we resume the execution. Else we want to return in the
1043 * scheduler and we want to be waked up again, to continue the
1044 * current Lua execution. So we schedule our own task.
1045 */
1046 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001047 if (!yield_allowed || !lua->task)
1048 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001049 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001050 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001051 if (!yield_allowed) {
1052 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001053 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001054 break;
1055 }
1056 ret = HLUA_E_AGAIN;
1057 break;
1058
1059 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001060
1061 /* Special exit case. The traditionnal exit is returned as an error
1062 * because the errors ares the only one mean to return immediately
1063 * from and lua execution.
1064 */
1065 if (lua->flags & HLUA_EXIT) {
1066 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001067 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001068 break;
1069 }
1070
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001071 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001072 if (!lua_checkstack(lua->T, 1)) {
1073 ret = HLUA_E_ERR;
1074 break;
1075 }
1076 msg = lua_tostring(lua->T, -1);
1077 lua_settop(lua->T, 0); /* Empty the stack. */
1078 lua_pop(lua->T, 1);
1079 if (msg)
1080 lua_pushfstring(lua->T, "runtime error: %s", msg);
1081 else
1082 lua_pushfstring(lua->T, "unknown runtime error");
1083 ret = HLUA_E_ERRMSG;
1084 break;
1085
1086 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001087 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001088 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001089 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001090 break;
1091
1092 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001093 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001094 if (!lua_checkstack(lua->T, 1)) {
1095 ret = HLUA_E_ERR;
1096 break;
1097 }
1098 msg = lua_tostring(lua->T, -1);
1099 lua_settop(lua->T, 0); /* Empty the stack. */
1100 lua_pop(lua->T, 1);
1101 if (msg)
1102 lua_pushfstring(lua->T, "message handler error: %s", msg);
1103 else
1104 lua_pushfstring(lua->T, "message handler error");
1105 ret = HLUA_E_ERRMSG;
1106 break;
1107
1108 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001109 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001110 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001111 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001112 break;
1113 }
1114
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001115 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001116 if (lua->flags & HLUA_MUST_GC &&
1117 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001118 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1119
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001120 switch (ret) {
1121 case HLUA_E_AGAIN:
1122 break;
1123
1124 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001125 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001126 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001127 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001128 break;
1129
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001130 case HLUA_E_ETMOUT:
1131 case HLUA_E_NOMEM:
1132 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001133 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001134 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001135 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001136 hlua_ctx_renew(lua, 0);
1137 break;
1138
1139 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001140 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001141 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001142 break;
1143 }
1144
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001145 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001146 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001147
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001148 return ret;
1149}
1150
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001151/* This function exit the current code. */
1152__LJMP static int hlua_done(lua_State *L)
1153{
1154 struct hlua *hlua = hlua_gethlua(L);
1155
1156 hlua->flags |= HLUA_EXIT;
1157 WILL_LJMP(lua_error(L));
1158
1159 return 0;
1160}
1161
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001162/* This function is an LUA binding. It provides a function
1163 * for deleting ACL from a referenced ACL file.
1164 */
1165__LJMP static int hlua_del_acl(lua_State *L)
1166{
1167 const char *name;
1168 const char *key;
1169 struct pat_ref *ref;
1170
1171 MAY_LJMP(check_args(L, 2, "del_acl"));
1172
1173 name = MAY_LJMP(luaL_checkstring(L, 1));
1174 key = MAY_LJMP(luaL_checkstring(L, 2));
1175
1176 ref = pat_ref_lookup(name);
1177 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001178 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001179
1180 pat_ref_delete(ref, key);
1181 return 0;
1182}
1183
1184/* This function is an LUA binding. It provides a function
1185 * for deleting map entry from a referenced map file.
1186 */
1187static int hlua_del_map(lua_State *L)
1188{
1189 const char *name;
1190 const char *key;
1191 struct pat_ref *ref;
1192
1193 MAY_LJMP(check_args(L, 2, "del_map"));
1194
1195 name = MAY_LJMP(luaL_checkstring(L, 1));
1196 key = MAY_LJMP(luaL_checkstring(L, 2));
1197
1198 ref = pat_ref_lookup(name);
1199 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001200 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001201
1202 pat_ref_delete(ref, key);
1203 return 0;
1204}
1205
1206/* This function is an LUA binding. It provides a function
1207 * for adding ACL pattern from a referenced ACL file.
1208 */
1209static int hlua_add_acl(lua_State *L)
1210{
1211 const char *name;
1212 const char *key;
1213 struct pat_ref *ref;
1214
1215 MAY_LJMP(check_args(L, 2, "add_acl"));
1216
1217 name = MAY_LJMP(luaL_checkstring(L, 1));
1218 key = MAY_LJMP(luaL_checkstring(L, 2));
1219
1220 ref = pat_ref_lookup(name);
1221 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001222 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001223
1224 if (pat_ref_find_elt(ref, key) == NULL)
1225 pat_ref_add(ref, key, NULL, NULL);
1226 return 0;
1227}
1228
1229/* This function is an LUA binding. It provides a function
1230 * for setting map pattern and sample from a referenced map
1231 * file.
1232 */
1233static int hlua_set_map(lua_State *L)
1234{
1235 const char *name;
1236 const char *key;
1237 const char *value;
1238 struct pat_ref *ref;
1239
1240 MAY_LJMP(check_args(L, 3, "set_map"));
1241
1242 name = MAY_LJMP(luaL_checkstring(L, 1));
1243 key = MAY_LJMP(luaL_checkstring(L, 2));
1244 value = MAY_LJMP(luaL_checkstring(L, 3));
1245
1246 ref = pat_ref_lookup(name);
1247 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001248 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001249
1250 if (pat_ref_find_elt(ref, key) != NULL)
1251 pat_ref_set(ref, key, value, NULL);
1252 else
1253 pat_ref_add(ref, key, value, NULL);
1254 return 0;
1255}
1256
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001257/* A class is a lot of memory that contain data. This data can be a table,
1258 * an integer or user data. This data is associated with a metatable. This
1259 * metatable have an original version registred in the global context with
1260 * the name of the object (_G[<name>] = <metable> ).
1261 *
1262 * A metable is a table that modify the standard behavior of a standard
1263 * access to the associated data. The entries of this new metatable are
1264 * defined as is:
1265 *
1266 * http://lua-users.org/wiki/MetatableEvents
1267 *
1268 * __index
1269 *
1270 * we access an absent field in a table, the result is nil. This is
1271 * true, but it is not the whole truth. Actually, such access triggers
1272 * the interpreter to look for an __index metamethod: If there is no
1273 * such method, as usually happens, then the access results in nil;
1274 * otherwise, the metamethod will provide the result.
1275 *
1276 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1277 * the key does not appear in the table, but the metatable has an __index
1278 * property:
1279 *
1280 * - if the value is a function, the function is called, passing in the
1281 * table and the key; the return value of that function is returned as
1282 * the result.
1283 *
1284 * - if the value is another table, the value of the key in that table is
1285 * asked for and returned (and if it doesn't exist in that table, but that
1286 * table's metatable has an __index property, then it continues on up)
1287 *
1288 * - Use "rawget(myTable,key)" to skip this metamethod.
1289 *
1290 * http://www.lua.org/pil/13.4.1.html
1291 *
1292 * __newindex
1293 *
1294 * Like __index, but control property assignment.
1295 *
1296 * __mode - Control weak references. A string value with one or both
1297 * of the characters 'k' and 'v' which specifies that the the
1298 * keys and/or values in the table are weak references.
1299 *
1300 * __call - Treat a table like a function. When a table is followed by
1301 * parenthesis such as "myTable( 'foo' )" and the metatable has
1302 * a __call key pointing to a function, that function is invoked
1303 * (passing any specified arguments) and the return value is
1304 * returned.
1305 *
1306 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1307 * called, if the metatable for myTable has a __metatable
1308 * key, the value of that key is returned instead of the
1309 * actual metatable.
1310 *
1311 * __tostring - Control string representation. When the builtin
1312 * "tostring( myTable )" function is called, if the metatable
1313 * for myTable has a __tostring property set to a function,
1314 * that function is invoked (passing myTable to it) and the
1315 * return value is used as the string representation.
1316 *
1317 * __len - Control table length. When the table length is requested using
1318 * the length operator ( '#' ), if the metatable for myTable has
1319 * a __len key pointing to a function, that function is invoked
1320 * (passing myTable to it) and the return value used as the value
1321 * of "#myTable".
1322 *
1323 * __gc - Userdata finalizer code. When userdata is set to be garbage
1324 * collected, if the metatable has a __gc field pointing to a
1325 * function, that function is first invoked, passing the userdata
1326 * to it. The __gc metamethod is not called for tables.
1327 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1328 *
1329 * Special metamethods for redefining standard operators:
1330 * http://www.lua.org/pil/13.1.html
1331 *
1332 * __add "+"
1333 * __sub "-"
1334 * __mul "*"
1335 * __div "/"
1336 * __unm "!"
1337 * __pow "^"
1338 * __concat ".."
1339 *
1340 * Special methods for redfining standar relations
1341 * http://www.lua.org/pil/13.2.html
1342 *
1343 * __eq "=="
1344 * __lt "<"
1345 * __le "<="
1346 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001347
1348/*
1349 *
1350 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001351 * Class Map
1352 *
1353 *
1354 */
1355
1356/* Returns a struct hlua_map if the stack entry "ud" is
1357 * a class session, otherwise it throws an error.
1358 */
1359__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1360{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001361 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001362}
1363
1364/* This function is the map constructor. It don't need
1365 * the class Map object. It creates and return a new Map
1366 * object. It must be called only during "body" or "init"
1367 * context because it process some filesystem accesses.
1368 */
1369__LJMP static int hlua_map_new(struct lua_State *L)
1370{
1371 const char *fn;
1372 int match = PAT_MATCH_STR;
1373 struct sample_conv conv;
1374 const char *file = "";
1375 int line = 0;
1376 lua_Debug ar;
1377 char *err = NULL;
1378 struct arg args[2];
1379
1380 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1381 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1382
1383 fn = MAY_LJMP(luaL_checkstring(L, 1));
1384
1385 if (lua_gettop(L) >= 2) {
1386 match = MAY_LJMP(luaL_checkinteger(L, 2));
1387 if (match < 0 || match >= PAT_MATCH_NUM)
1388 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1389 }
1390
1391 /* Get Lua filename and line number. */
1392 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1393 lua_getinfo(L, "Sl", &ar); /* get info about it */
1394 if (ar.currentline > 0) { /* is there info? */
1395 file = ar.short_src;
1396 line = ar.currentline;
1397 }
1398 }
1399
1400 /* fill fake sample_conv struct. */
1401 conv.kw = ""; /* unused. */
1402 conv.process = NULL; /* unused. */
1403 conv.arg_mask = 0; /* unused. */
1404 conv.val_args = NULL; /* unused. */
1405 conv.out_type = SMP_T_STR;
1406 conv.private = (void *)(long)match;
1407 switch (match) {
1408 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1409 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1410 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1411 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1412 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1413 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1414 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001415 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001416 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1417 default:
1418 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1419 }
1420
1421 /* fill fake args. */
1422 args[0].type = ARGT_STR;
1423 args[0].data.str.str = (char *)fn;
1424 args[1].type = ARGT_STOP;
1425
1426 /* load the map. */
1427 if (!sample_load_map(args, &conv, file, line, &err)) {
1428 /* error case: we cant use luaL_error because we must
1429 * free the err variable.
1430 */
1431 luaL_where(L, 1);
1432 lua_pushfstring(L, "'new': %s.", err);
1433 lua_concat(L, 2);
1434 free(err);
1435 WILL_LJMP(lua_error(L));
1436 }
1437
1438 /* create the lua object. */
1439 lua_newtable(L);
1440 lua_pushlightuserdata(L, args[0].data.map);
1441 lua_rawseti(L, -2, 0);
1442
1443 /* Pop a class Map metatable and affect it to the userdata. */
1444 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1445 lua_setmetatable(L, -2);
1446
1447
1448 return 1;
1449}
1450
1451__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1452{
1453 struct map_descriptor *desc;
1454 struct pattern *pat;
1455 struct sample smp;
1456
1457 MAY_LJMP(check_args(L, 2, "lookup"));
1458 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001459 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001460 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001461 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001462 }
1463 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001464 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001465 smp.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001466 smp.data.u.str.str = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.len));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001467 }
1468
1469 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001470 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001471 if (str)
1472 lua_pushstring(L, "");
1473 else
1474 lua_pushnil(L);
1475 return 1;
1476 }
1477
1478 /* The Lua pattern must return a string, so we can't check the returned type */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001479 lua_pushlstring(L, pat->data->u.str.str, pat->data->u.str.len);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001480 return 1;
1481}
1482
1483__LJMP static int hlua_map_lookup(struct lua_State *L)
1484{
1485 return _hlua_map_lookup(L, 0);
1486}
1487
1488__LJMP static int hlua_map_slookup(struct lua_State *L)
1489{
1490 return _hlua_map_lookup(L, 1);
1491}
1492
1493/*
1494 *
1495 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001496 * Class Socket
1497 *
1498 *
1499 */
1500
1501__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1502{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001503 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001504}
1505
1506/* This function is the handler called for each I/O on the established
1507 * connection. It is used for notify space avalaible to send or data
1508 * received.
1509 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001510static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001511{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001512 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001513 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001514
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001515 if (appctx->ctx.hlua_cosocket.die) {
1516 si_shutw(si);
1517 si_shutr(si);
1518 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001519 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1520 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001521 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1522 }
1523
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001524 /* If the connection object is not avalaible, close all the
1525 * streams and wakeup everithing waiting for.
1526 */
1527 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001528 si_shutw(si);
1529 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001530 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001531 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1532 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001533 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001534 }
1535
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001536 /* If we cant write, wakeup the pending write signals. */
1537 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001538 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001539
1540 /* If we cant read, wakeup the pending read signals. */
1541 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001542 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001543
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001544 /* if the connection is not estabkished, inform the stream that we want
1545 * to be notified whenever the connection completes.
1546 */
1547 if (!(c->flags & CO_FL_CONNECTED)) {
1548 si_applet_cant_get(si);
1549 si_applet_cant_put(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001550 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001551 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001552
1553 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001554 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001555
1556 /* Wake the tasks which wants to write if the buffer have avalaible space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001557 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001558 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001559
1560 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001561 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001562 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001563
1564 /* Some data were injected in the buffer, notify the stream
1565 * interface.
1566 */
1567 if (!channel_is_empty(si_ic(si)))
1568 stream_int_update(si);
1569
1570 /* If write notifications are registered, we considers we want
1571 * to write, so we set the flag cant put
1572 */
1573 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
1574 si_applet_cant_put(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001575}
1576
Willy Tarreau87b09662015-04-03 00:22:06 +02001577/* This function is called when the "struct stream" is destroyed.
1578 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001579 * Wake all the pending signals.
1580 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001581static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001582{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001583 struct xref *peer;
1584
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001585 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001586 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1587 if (peer)
1588 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001589
1590 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001591 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1592 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001593}
1594
1595/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001596 * uses this object. If the stream does not exists, just quit.
1597 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001598 * pending signal can rest in the read and write lists. destroy
1599 * it.
1600 */
1601__LJMP static int hlua_socket_gc(lua_State *L)
1602{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001603 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001604 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001605 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001606
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001607 MAY_LJMP(check_args(L, 1, "__gc"));
1608
1609 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001610 peer = xref_get_peer_and_lock(&socket->xref);
1611 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001612 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001613 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001614
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001615 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001616 appctx->ctx.hlua_cosocket.die = 1;
1617 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001618
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001619 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001620 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001621 return 0;
1622}
1623
1624/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001625 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001626 */
sada05ed3302018-05-11 11:48:18 -07001627__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001628{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001629 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001630 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001631 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001632
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001633 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001634
1635 /* Check if we run on the same thread than the xreator thread.
1636 * We cannot access to the socket if the thread is different.
1637 */
1638 if (socket->tid != tid)
1639 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1640
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001641 peer = xref_get_peer_and_lock(&socket->xref);
1642 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001643 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001644 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001645
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001646 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001647 appctx->ctx.hlua_cosocket.die = 1;
1648 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001649
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001650 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001651 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001652 return 0;
1653}
1654
sada05ed3302018-05-11 11:48:18 -07001655/* The close function calls close_helper.
1656 */
1657__LJMP static int hlua_socket_close(lua_State *L)
1658{
1659 MAY_LJMP(check_args(L, 1, "close"));
1660 return hlua_socket_close_helper(L);
1661}
1662
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001663/* This Lua function assumes that the stack contain three parameters.
1664 * 1 - USERDATA containing a struct socket
1665 * 2 - INTEGER with values of the macro defined below
1666 * If the integer is -1, we must read at most one line.
1667 * If the integer is -2, we ust read all the data until the
1668 * end of the stream.
1669 * If the integer is positive value, we must read a number of
1670 * bytes corresponding to this value.
1671 */
1672#define HLSR_READ_LINE (-1)
1673#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001674__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001675{
1676 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1677 int wanted = lua_tointeger(L, 2);
1678 struct hlua *hlua = hlua_gethlua(L);
1679 struct appctx *appctx;
1680 int len;
1681 int nblk;
1682 char *blk1;
1683 int len1;
1684 char *blk2;
1685 int len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001686 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001687 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001688 struct stream_interface *si;
1689 struct stream *s;
1690 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001691 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001692
1693 /* Check if this lua stack is schedulable. */
1694 if (!hlua || !hlua->task)
1695 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1696 "'frontend', 'backend' or 'task'"));
1697
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001698 /* Check if we run on the same thread than the xreator thread.
1699 * We cannot access to the socket if the thread is different.
1700 */
1701 if (socket->tid != tid)
1702 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1703
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001704 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001705 peer = xref_get_peer_and_lock(&socket->xref);
1706 if (!peer)
1707 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001708 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1709 si = appctx->owner;
1710 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001711
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001712 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001713 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001714 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001715 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001716 if (nblk < 0) /* Connection close. */
1717 goto connection_closed;
1718 if (nblk == 0) /* No data avalaible. */
1719 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001720
1721 /* remove final \r\n. */
1722 if (nblk == 1) {
1723 if (blk1[len1-1] == '\n') {
1724 len1--;
1725 skip_at_end++;
1726 if (blk1[len1-1] == '\r') {
1727 len1--;
1728 skip_at_end++;
1729 }
1730 }
1731 }
1732 else {
1733 if (blk2[len2-1] == '\n') {
1734 len2--;
1735 skip_at_end++;
1736 if (blk2[len2-1] == '\r') {
1737 len2--;
1738 skip_at_end++;
1739 }
1740 }
1741 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001742 }
1743
1744 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001745 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001746 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001747 if (nblk < 0) /* Connection close. */
1748 goto connection_closed;
1749 if (nblk == 0) /* No data avalaible. */
1750 goto connection_empty;
1751 }
1752
1753 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001754 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001755 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001756 if (nblk < 0) /* Connection close. */
1757 goto connection_closed;
1758 if (nblk == 0) /* No data avalaible. */
1759 goto connection_empty;
1760
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001761 missing_bytes = wanted - socket->b.n;
1762 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001763 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001764 len1 = missing_bytes;
1765 } if (nblk == 2 && len1 + len2 > missing_bytes)
1766 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001767 }
1768
1769 len = len1;
1770
1771 luaL_addlstring(&socket->b, blk1, len1);
1772 if (nblk == 2) {
1773 len += len2;
1774 luaL_addlstring(&socket->b, blk2, len2);
1775 }
1776
1777 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001778 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001779
1780 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001781 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001782
1783 /* If the pattern reclaim to read all the data
1784 * in the connection, got out.
1785 */
1786 if (wanted == HLSR_READ_ALL)
1787 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001788 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789 goto connection_empty;
1790
1791 /* Return result. */
1792 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001793 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001794 return 1;
1795
1796connection_closed:
1797
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001798 xref_unlock(&socket->xref, peer);
1799
1800no_peer:
1801
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001802 /* If the buffer containds data. */
1803 if (socket->b.n > 0) {
1804 luaL_pushresult(&socket->b);
1805 return 1;
1806 }
1807 lua_pushnil(L);
1808 lua_pushstring(L, "connection closed.");
1809 return 2;
1810
1811connection_empty:
1812
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001813 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1814 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001815 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001816 }
1817 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001818 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001819 return 0;
1820}
1821
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001822/* This Lua function gets two parameters. The first one can be string
1823 * or a number. If the string is "*l", the user requires one line. If
1824 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825 * If the value is a number, the user require a number of bytes equal
1826 * to the value. The default value is "*l" (a line).
1827 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001828 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001829 * integer takes this values:
1830 * -1 : read a line
1831 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001832 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001833 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001834 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001835 * concatenated with the read data.
1836 */
1837__LJMP static int hlua_socket_receive(struct lua_State *L)
1838{
1839 int wanted = HLSR_READ_LINE;
1840 const char *pattern;
1841 int type;
1842 char *error;
1843 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001844 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001845
1846 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1847 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1848
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001849 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001850
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001851 /* Check if we run on the same thread than the xreator thread.
1852 * We cannot access to the socket if the thread is different.
1853 */
1854 if (socket->tid != tid)
1855 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1856
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001857 /* check for pattern. */
1858 if (lua_gettop(L) >= 2) {
1859 type = lua_type(L, 2);
1860 if (type == LUA_TSTRING) {
1861 pattern = lua_tostring(L, 2);
1862 if (strcmp(pattern, "*a") == 0)
1863 wanted = HLSR_READ_ALL;
1864 else if (strcmp(pattern, "*l") == 0)
1865 wanted = HLSR_READ_LINE;
1866 else {
1867 wanted = strtoll(pattern, &error, 10);
1868 if (*error != '\0')
1869 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1870 }
1871 }
1872 else if (type == LUA_TNUMBER) {
1873 wanted = lua_tointeger(L, 2);
1874 if (wanted < 0)
1875 WILL_LJMP(luaL_error(L, "Unsupported size."));
1876 }
1877 }
1878
1879 /* Set pattern. */
1880 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001881
1882 /* Check if we would replace the top by itself. */
1883 if (lua_gettop(L) != 2)
1884 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001885
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001886 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001887 luaL_buffinit(L, &socket->b);
1888
1889 /* Check prefix. */
1890 if (lua_gettop(L) >= 3) {
1891 if (lua_type(L, 3) != LUA_TSTRING)
1892 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1893 pattern = lua_tolstring(L, 3, &len);
1894 luaL_addlstring(&socket->b, pattern, len);
1895 }
1896
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001897 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001898}
1899
1900/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001901 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001902 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001903static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904{
1905 struct hlua_socket *socket;
1906 struct hlua *hlua = hlua_gethlua(L);
1907 struct appctx *appctx;
1908 size_t buf_len;
1909 const char *buf;
1910 int len;
1911 int send_len;
1912 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001913 struct xref *peer;
1914 struct stream_interface *si;
1915 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001916
1917 /* Check if this lua stack is schedulable. */
1918 if (!hlua || !hlua->task)
1919 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1920 "'frontend', 'backend' or 'task'"));
1921
1922 /* Get object */
1923 socket = MAY_LJMP(hlua_checksocket(L, 1));
1924 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001925 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001926
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001927 /* Check if we run on the same thread than the xreator thread.
1928 * We cannot access to the socket if the thread is different.
1929 */
1930 if (socket->tid != tid)
1931 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1932
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001933 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001934 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001935 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001936 lua_pushinteger(L, -1);
1937 return 1;
1938 }
1939 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1940 si = appctx->owner;
1941 s = si_strm(si);
1942
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001943 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001944 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001945 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001946 lua_pushinteger(L, -1);
1947 return 1;
1948 }
1949
1950 /* Update the input buffer data. */
1951 buf += sent;
1952 send_len = buf_len - sent;
1953
1954 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001955 if (sent >= buf_len) {
1956 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001957 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001958 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001959
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001960 /* Check if the buffer is avalaible because HAProxy doesn't allocate
1961 * the request buffer if its not required.
1962 */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001963 if (s->req.buf->size == 0) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001964 if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01001965 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001966 }
1967
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001968 /* Check for avalaible space. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001969 len = buffer_total_space(s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01001970 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001971 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01001972 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001973
1974 /* send data */
1975 if (len < send_len)
1976 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02001977 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001978
1979 /* "Not enough space" (-1), "Buffer too little to contain
1980 * the data" (-2) are not expected because the available length
1981 * is tested.
1982 * Other unknown error are also not expected.
1983 */
1984 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01001985 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001986 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001987
sada05ed3302018-05-11 11:48:18 -07001988 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001989 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001990 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001991 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001992 return 1;
1993 }
1994
1995 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001996 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02001997
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001998 s->req.rex = TICK_ETERNITY;
1999 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002000
2001 /* Update length sent. */
2002 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002003 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002004
2005 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002006 if (sent + len >= buf_len) {
2007 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002008 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002009 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002010
2011hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002012 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2013 xref_unlock(&socket->xref, peer);
2014 WILL_LJMP(luaL_error(L, "out of memory"));
2015 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002016 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002017 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002018 return 0;
2019}
2020
2021/* This function initiate the send of data. It just check the input
2022 * parameters and push an integer in the Lua stack that contain the
2023 * amount of data writed in the buffer. This is used by the function
2024 * "hlua_socket_write_yield" that can yield.
2025 *
2026 * The Lua function gets between 3 and 4 parameters. The first one is
2027 * the associated object. The second is a string buffer. The third is
2028 * a facultative integer that represents where is the buffer position
2029 * of the start of the data that can send. The first byte is the
2030 * position "1". The default value is "1". The fourth argument is a
2031 * facultative integer that represents where is the buffer position
2032 * of the end of the data that can send. The default is the last byte.
2033 */
2034static int hlua_socket_send(struct lua_State *L)
2035{
2036 int i;
2037 int j;
2038 const char *buf;
2039 size_t buf_len;
2040
2041 /* Check number of arguments. */
2042 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2043 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2044
2045 /* Get the string. */
2046 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2047
2048 /* Get and check j. */
2049 if (lua_gettop(L) == 4) {
2050 j = MAY_LJMP(luaL_checkinteger(L, 4));
2051 if (j < 0)
2052 j = buf_len + j + 1;
2053 if (j > buf_len)
2054 j = buf_len + 1;
2055 lua_pop(L, 1);
2056 }
2057 else
2058 j = buf_len;
2059
2060 /* Get and check i. */
2061 if (lua_gettop(L) == 3) {
2062 i = MAY_LJMP(luaL_checkinteger(L, 3));
2063 if (i < 0)
2064 i = buf_len + i + 1;
2065 if (i > buf_len)
2066 i = buf_len + 1;
2067 lua_pop(L, 1);
2068 } else
2069 i = 1;
2070
2071 /* Check bth i and j. */
2072 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002073 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002074 return 1;
2075 }
2076 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002077 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002078 return 1;
2079 }
2080 if (i == 0)
2081 i = 1;
2082 if (j == 0)
2083 j = 1;
2084
2085 /* Pop the string. */
2086 lua_pop(L, 1);
2087
2088 /* Update the buffer length. */
2089 buf += i - 1;
2090 buf_len = j - i + 1;
2091 lua_pushlstring(L, buf, buf_len);
2092
2093 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002094 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002095
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002096 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002097}
2098
Willy Tarreau22b0a682015-06-17 19:43:49 +02002099#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002100__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2101{
2102 static char buffer[SOCKET_INFO_MAX_LEN];
2103 int ret;
2104 int len;
2105 char *p;
2106
2107 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2108 if (ret <= 0) {
2109 lua_pushnil(L);
2110 return 1;
2111 }
2112
2113 if (ret == AF_UNIX) {
2114 lua_pushstring(L, buffer+1);
2115 return 1;
2116 }
2117 else if (ret == AF_INET6) {
2118 buffer[0] = '[';
2119 len = strlen(buffer);
2120 buffer[len] = ']';
2121 len++;
2122 buffer[len] = ':';
2123 len++;
2124 p = buffer;
2125 }
2126 else if (ret == AF_INET) {
2127 p = buffer + 1;
2128 len = strlen(p);
2129 p[len] = ':';
2130 len++;
2131 }
2132 else {
2133 lua_pushnil(L);
2134 return 1;
2135 }
2136
2137 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2138 lua_pushnil(L);
2139 return 1;
2140 }
2141
2142 lua_pushstring(L, p);
2143 return 1;
2144}
2145
2146/* Returns information about the peer of the connection. */
2147__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2148{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002149 struct hlua_socket *socket;
2150 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002151 struct xref *peer;
2152 struct appctx *appctx;
2153 struct stream_interface *si;
2154 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002155 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002156
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002157 MAY_LJMP(check_args(L, 1, "getpeername"));
2158
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002159 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002160
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002161 /* Check if we run on the same thread than the xreator thread.
2162 * We cannot access to the socket if the thread is different.
2163 */
2164 if (socket->tid != tid)
2165 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2166
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002167 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002168 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002169 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002170 lua_pushnil(L);
2171 return 1;
2172 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002173 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2174 si = appctx->owner;
2175 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002176
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002177 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002178 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002179 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002180 lua_pushnil(L);
2181 return 1;
2182 }
2183
Willy Tarreaua71f6422016-11-16 17:00:14 +01002184 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002185 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002186 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002187 lua_pushnil(L);
2188 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002189 }
2190
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002191 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2192 xref_unlock(&socket->xref, peer);
2193 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002194}
2195
2196/* Returns information about my connection side. */
2197static int hlua_socket_getsockname(struct lua_State *L)
2198{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002199 struct hlua_socket *socket;
2200 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002201 struct appctx *appctx;
2202 struct xref *peer;
2203 struct stream_interface *si;
2204 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002205 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002206
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002207 MAY_LJMP(check_args(L, 1, "getsockname"));
2208
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002209 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002210
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002211 /* Check if we run on the same thread than the xreator thread.
2212 * We cannot access to the socket if the thread is different.
2213 */
2214 if (socket->tid != tid)
2215 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2216
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002217 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002218 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002219 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002220 lua_pushnil(L);
2221 return 1;
2222 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002223 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2224 si = appctx->owner;
2225 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002226
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002227 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002228 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002229 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002230 lua_pushnil(L);
2231 return 1;
2232 }
2233
Willy Tarreaua71f6422016-11-16 17:00:14 +01002234 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002235 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002236 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002237 lua_pushnil(L);
2238 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002239 }
2240
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002241 ret = hlua_socket_info(L, &conn->addr.from);
2242 xref_unlock(&socket->xref, peer);
2243 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002244}
2245
2246/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002247static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002248 .obj_type = OBJ_TYPE_APPLET,
2249 .name = "<LUA_TCP>",
2250 .fct = hlua_socket_handler,
2251 .release = hlua_socket_release,
2252};
2253
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002254__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002255{
2256 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2257 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002258 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002259 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002260 struct stream_interface *si;
2261 struct stream *s;
2262
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002263 /* Check if we run on the same thread than the xreator thread.
2264 * We cannot access to the socket if the thread is different.
2265 */
2266 if (socket->tid != tid)
2267 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2268
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002269 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002270 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002271 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002272 lua_pushnil(L);
2273 lua_pushstring(L, "Can't connect");
2274 return 2;
2275 }
2276 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2277 si = appctx->owner;
2278 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002279
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002280 /* Check if we run on the same thread than the xreator thread.
2281 * We cannot access to the socket if the thread is different.
2282 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002283 if (socket->tid != tid) {
2284 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002285 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002286 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002287
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002288 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002289 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002290 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002291 lua_pushnil(L);
2292 lua_pushstring(L, "Can't connect");
2293 return 2;
2294 }
2295
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002296 appctx = objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297
2298 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002299 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002300 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002301 lua_pushinteger(L, 1);
2302 return 1;
2303 }
2304
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002305 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2306 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002308 }
2309 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002310 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002311 return 0;
2312}
2313
2314/* This function fail or initite the connection. */
2315__LJMP static int hlua_socket_connect(struct lua_State *L)
2316{
2317 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002318 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002319 const char *ip;
2320 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002321 struct hlua *hlua;
2322 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002323 int low, high;
2324 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002325 struct xref *peer;
2326 struct stream_interface *si;
2327 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002328
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002329 if (lua_gettop(L) < 2)
2330 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002331
2332 /* Get args. */
2333 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002334
2335 /* Check if we run on the same thread than the xreator thread.
2336 * We cannot access to the socket if the thread is different.
2337 */
2338 if (socket->tid != tid)
2339 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2340
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002341 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002342 if (lua_gettop(L) >= 3) {
2343 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002344 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002345
Tim Duesterhus6edab862018-01-06 19:04:45 +01002346 /* Force the ip to end with a colon, to support IPv6 addresses
2347 * that are not enclosed within square brackets.
2348 */
2349 if (port > 0) {
2350 luaL_buffinit(L, &b);
2351 luaL_addstring(&b, ip);
2352 luaL_addchar(&b, ':');
2353 luaL_pushresult(&b);
2354 ip = lua_tolstring(L, lua_gettop(L), NULL);
2355 }
2356 }
2357
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002358 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002359 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002360 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002361 lua_pushnil(L);
2362 return 1;
2363 }
2364 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2365 si = appctx->owner;
2366 s = si_strm(si);
2367
2368 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002369 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002370 if (!conn) {
2371 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002372 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002373 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002374
Willy Tarreau3adac082015-09-26 17:51:09 +02002375 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002376 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002377
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002379 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002380 if (!addr) {
2381 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002382 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002383 }
2384 if (low != high) {
2385 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002386 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002387 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002388 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002389
2390 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002391 if (low == 0) {
2392 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002393 if (port == -1) {
2394 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002395 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002396 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002397 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2398 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002399 if (port == -1) {
2400 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002401 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002402 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002403 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2404 }
2405 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002406
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002407 hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002408 appctx = objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002409
2410 /* inform the stream that we want to be notified whenever the
2411 * connection completes.
2412 */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002413 si_applet_cant_get(&s->si[0]);
2414 si_applet_cant_put(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002415 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002416
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002417 hlua->flags |= HLUA_MUST_GC;
2418
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002419 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2420 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002421 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002422 }
2423 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002424
2425 task_wakeup(s->task, TASK_WOKEN_INIT);
2426 /* Return yield waiting for connection. */
2427
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002428 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002429
2430 return 0;
2431}
2432
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002433#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002434__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2435{
2436 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002437 struct xref *peer;
2438 struct appctx *appctx;
2439 struct stream_interface *si;
2440 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002441
2442 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2443 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002444
2445 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002446 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002447 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002448 lua_pushnil(L);
2449 return 1;
2450 }
2451 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2452 si = appctx->owner;
2453 s = si_strm(si);
2454
2455 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002456 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002457 return MAY_LJMP(hlua_socket_connect(L));
2458}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002459#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002460
2461__LJMP static int hlua_socket_setoption(struct lua_State *L)
2462{
2463 return 0;
2464}
2465
2466__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2467{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002468 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002469 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002470 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002471 struct xref *peer;
2472 struct appctx *appctx;
2473 struct stream_interface *si;
2474 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002475
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002476 MAY_LJMP(check_args(L, 2, "settimeout"));
2477
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002478 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002479
2480 /* round up for inputs that are fractions and convert to millis */
2481 dtmout = (0.5 + MAY_LJMP(luaL_checknumber(L, 2))) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002482
Thierry Fournier17a921b2018-03-08 09:59:02 +01002483 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002484 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002485 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2486
Mark Lakes56cc1252018-03-27 09:48:06 +02002487 if (dtmout > INT_MAX) /* overflow check */
2488 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d", INT_MAX));
2489
2490 tmout = MS_TO_TICKS((int)dtmout);
2491
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002492 /* Check if we run on the same thread than the xreator thread.
2493 * We cannot access to the socket if the thread is different.
2494 */
2495 if (socket->tid != tid)
2496 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2497
Mark Lakes56cc1252018-03-27 09:48:06 +02002498 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002499 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002500 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002501 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2502 WILL_LJMP(lua_error(L));
2503 return 0;
2504 }
2505 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2506 si = appctx->owner;
2507 s = si_strm(si);
2508
2509 s->req.rto = tmout;
2510 s->req.wto = tmout;
2511 s->res.rto = tmout;
2512 s->res.wto = tmout;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002513 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002514
Thierry Fourniere9636f12018-03-08 09:54:32 +01002515 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002516 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002517}
2518
2519__LJMP static int hlua_socket_new(lua_State *L)
2520{
2521 struct hlua_socket *socket;
2522 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002523 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002524 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002525
2526 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002527 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002528 hlua_pusherror(L, "socket: full stack");
2529 goto out_fail_conf;
2530 }
2531
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002532 /* Create the object: obj[0] = userdata. */
2533 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002534 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002535 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002536 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002537 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002538
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002539 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002540 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002541 hlua_pusherror(L, "socket: uninitialized pools.");
2542 goto out_fail_conf;
2543 }
2544
Willy Tarreau87b09662015-04-03 00:22:06 +02002545 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002546 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2547 lua_setmetatable(L, -2);
2548
Willy Tarreaud420a972015-04-06 00:39:18 +02002549 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002550 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002551 if (!appctx) {
2552 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002553 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002554 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002555
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002556 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002557 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002558 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2559 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002560
Willy Tarreaud420a972015-04-06 00:39:18 +02002561 /* Now create a session, task and stream for this applet */
2562 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2563 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002564 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002565 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002566 }
2567
Willy Tarreau87787ac2017-08-28 16:22:54 +02002568 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002569 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002570 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002571 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002572 }
2573
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002574 /* Initialise cross reference between stream and Lua socket object. */
2575 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002576
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002577 /* Configure "right" stream interface. this "si" is used to connect
2578 * and retrieve data from the server. The connection is initialized
2579 * with the "struct server".
2580 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002581 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002582
2583 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002584 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2585 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002586
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002587 return 1;
2588
Willy Tarreaud420a972015-04-06 00:39:18 +02002589 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002590 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002591 out_fail_sess:
2592 appctx_free(appctx);
2593 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002594 WILL_LJMP(lua_error(L));
2595 return 0;
2596}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002597
2598/*
2599 *
2600 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002601 * Class Channel
2602 *
2603 *
2604 */
2605
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002606/* The state between the channel data and the HTTP parser state can be
2607 * unconsistent, so reset the parser and call it again. Warning, this
2608 * action not revalidate the request and not send a 400 if the modified
2609 * resuest is not valid.
2610 *
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002611 * This function never fails. The direction is set using dir, which equals
2612 * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002613 */
2614static void hlua_resynchonize_proto(struct stream *stream, int dir)
2615{
2616 /* Protocol HTTP. */
2617 if (stream->be->mode == PR_MODE_HTTP) {
2618
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002619 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002620 http_txn_reset_req(stream->txn);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002621 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002622 http_txn_reset_res(stream->txn);
2623
2624 if (stream->txn->hdr_idx.v)
2625 hdr_idx_init(&stream->txn->hdr_idx);
2626
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002627 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002628 http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002629 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002630 http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx);
2631 }
2632}
2633
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002634/* This function is called before the Lua execution. It stores
2635 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002636 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002637static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002638{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002639 c->mode = stream->be->mode;
2640 switch (c->mode) {
2641 case PR_MODE_HTTP:
2642 c->data.http.dir = opt & SMP_OPT_DIR;
2643 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2644 c->data.http.state = stream->txn->req.msg_state;
2645 else
2646 c->data.http.state = stream->txn->rsp.msg_state;
2647 break;
2648 default:
2649 break;
2650 }
2651}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002652
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002653/* This function is called after the Lua execution. it
2654 * returns true if the parser state is consistent, otherwise,
2655 * it return false.
2656 *
2657 * In HTTP mode, the parser state must be in the same state
2658 * or greater when we exit the function. Even if we do a
2659 * control yield. This prevent to break the HTTP message
2660 * from the Lua code.
2661 */
2662static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2663{
2664 if (c->mode != stream->be->mode)
2665 return 0;
2666
2667 switch (c->mode) {
2668 case PR_MODE_HTTP:
2669 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002670 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002671 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2672 return stream->txn->req.msg_state >= c->data.http.state;
2673 else
2674 return stream->txn->rsp.msg_state >= c->data.http.state;
2675 default:
2676 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002677 }
2678 return 1;
2679}
2680
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002681/* Returns the struct hlua_channel join to the class channel in the
2682 * stack entry "ud" or throws an argument error.
2683 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002684__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002685{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002686 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002687}
2688
Willy Tarreau47860ed2015-03-10 14:07:50 +01002689/* Pushes the channel onto the top of the stack. If the stask does not have a
2690 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002691 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002692static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002693{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002694 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002695 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002696 return 0;
2697
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002698 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002699 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002700 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002701
2702 /* Pop a class sesison metatable and affect it to the userdata. */
2703 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2704 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002705 return 1;
2706}
2707
2708/* Duplicate all the data present in the input channel and put it
2709 * in a string LUA variables. Returns -1 and push a nil value in
2710 * the stack if the channel is closed and all the data are consumed,
2711 * returns 0 if no data are available, otherwise it returns the length
2712 * of the builded string.
2713 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002714static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002715{
2716 char *blk1;
2717 char *blk2;
2718 int len1;
2719 int len2;
2720 int ret;
2721 luaL_Buffer b;
2722
Willy Tarreau06d80a92017-10-19 14:32:15 +02002723 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002724 if (unlikely(ret == 0))
2725 return 0;
2726
2727 if (unlikely(ret < 0)) {
2728 lua_pushnil(L);
2729 return -1;
2730 }
2731
2732 luaL_buffinit(L, &b);
2733 luaL_addlstring(&b, blk1, len1);
2734 if (unlikely(ret == 2))
2735 luaL_addlstring(&b, blk2, len2);
2736 luaL_pushresult(&b);
2737
2738 if (unlikely(ret == 2))
2739 return len1 + len2;
2740 return len1;
2741}
2742
2743/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2744 * a yield. This function keep the data in the buffer.
2745 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002746__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002747{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002748 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002749
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002750 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2751
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002752 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002753 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002754 return 1;
2755}
2756
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002757/* Check arguments for the function "hlua_channel_dup_yield". */
2758__LJMP static int hlua_channel_dup(lua_State *L)
2759{
2760 MAY_LJMP(check_args(L, 1, "dup"));
2761 MAY_LJMP(hlua_checkchannel(L, 1));
2762 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2763}
2764
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002765/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2766 * a yield. This function consumes the data in the buffer. It returns
2767 * a string containing the data or a nil pointer if no data are available
2768 * and the channel is closed.
2769 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002770__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002771{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002772 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002773 int ret;
2774
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002775 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002776
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002777 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002778 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002779 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002780
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002781 if (unlikely(ret == -1))
2782 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002783
Willy Tarreau47860ed2015-03-10 14:07:50 +01002784 chn->buf->i -= ret;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002785 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002786 return 1;
2787}
2788
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002789/* Check arguments for the fucntion "hlua_channel_get_yield". */
2790__LJMP static int hlua_channel_get(lua_State *L)
2791{
2792 MAY_LJMP(check_args(L, 1, "get"));
2793 MAY_LJMP(hlua_checkchannel(L, 1));
2794 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2795}
2796
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002797/* This functions consumes and returns one line. If the channel is closed,
2798 * and the last data does not contains a final '\n', the data are returned
2799 * without the final '\n'. When no more data are avalaible, it returns nil
2800 * value.
2801 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002802__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002803{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002804 char *blk1;
2805 char *blk2;
2806 int len1;
2807 int len2;
2808 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002809 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002810 int ret;
2811 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002812
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002813 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2814
Willy Tarreau06d80a92017-10-19 14:32:15 +02002815 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002816 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002817 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002818
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002819 if (ret == -1) {
2820 lua_pushnil(L);
2821 return 1;
2822 }
2823
2824 luaL_buffinit(L, &b);
2825 luaL_addlstring(&b, blk1, len1);
2826 len = len1;
2827 if (unlikely(ret == 2)) {
2828 luaL_addlstring(&b, blk2, len2);
2829 len += len2;
2830 }
2831 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002832 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002833 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002834 return 1;
2835}
2836
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002837/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2838__LJMP static int hlua_channel_getline(lua_State *L)
2839{
2840 MAY_LJMP(check_args(L, 1, "getline"));
2841 MAY_LJMP(hlua_checkchannel(L, 1));
2842 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2843}
2844
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002845/* This function takes a string as input, and append it at the
2846 * input side of channel. If the data is too big, but a space
2847 * is probably available after sending some data, the function
2848 * yield. If the data is bigger than the buffer, or if the
2849 * channel is closed, it returns -1. otherwise, it returns the
2850 * amount of data writed.
2851 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002852__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002853{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002854 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002855 size_t len;
2856 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2857 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2858 int ret;
2859 int max;
2860
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002861 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2862 * the request buffer if its not required.
2863 */
2864 if (chn->buf->size == 0) {
2865 si_applet_cant_put(chn_prod(chn));
2866 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
2867 }
2868
Willy Tarreau47860ed2015-03-10 14:07:50 +01002869 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002870 if (max > len - l)
2871 max = len - l;
2872
Willy Tarreau06d80a92017-10-19 14:32:15 +02002873 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002874 if (ret == -2 || ret == -3) {
2875 lua_pushinteger(L, -1);
2876 return 1;
2877 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002878 if (ret == -1) {
2879 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002880 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002881 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002882 l += ret;
2883 lua_pop(L, 1);
2884 lua_pushinteger(L, l);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002885 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002886
Willy Tarreau47860ed2015-03-10 14:07:50 +01002887 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2888 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002889 /* There are no space avalaible, and the output buffer is empty.
2890 * in this case, we cannot add more data, so we cannot yield,
2891 * we return the amount of copyied data.
2892 */
2893 return 1;
2894 }
2895 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002896 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897 return 1;
2898}
2899
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002900/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901 * of the writed string, or -1 if the channel is closed or if the
2902 * buffer size is too little for the data.
2903 */
2904__LJMP static int hlua_channel_append(lua_State *L)
2905{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002906 size_t len;
2907
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002908 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002909 MAY_LJMP(hlua_checkchannel(L, 1));
2910 MAY_LJMP(luaL_checklstring(L, 2, &len));
2911 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912 lua_pushinteger(L, 0);
2913
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002914 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002915}
2916
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002917/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002918 * his process by cleaning the buffer. The result is a replacement
2919 * of the current data. It returns the length of the writed string,
2920 * or -1 if the channel is closed or if the buffer size is too
2921 * little for the data.
2922 */
2923__LJMP static int hlua_channel_set(lua_State *L)
2924{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002925 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002926
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002927 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002928 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002929 lua_pushinteger(L, 0);
2930
Willy Tarreau47860ed2015-03-10 14:07:50 +01002931 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002932
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002933 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002934}
2935
2936/* Append data in the output side of the buffer. This data is immediatly
2937 * sent. The fcuntion returns the ammount of data writed. If the buffer
2938 * cannot contains the data, the function yield. The function returns -1
2939 * if the channel is closed.
2940 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002941__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002942{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002943 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002944 size_t len;
2945 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2946 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2947 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002948 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002949
Willy Tarreau47860ed2015-03-10 14:07:50 +01002950 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002951 lua_pushinteger(L, -1);
2952 return 1;
2953 }
2954
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002955 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2956 * the request buffer if its not required.
2957 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002958 if (chn->buf->size == 0) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002959 si_applet_cant_put(chn_prod(chn));
2960 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002961 }
2962
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002963 /* the writed data will be immediatly sent, so we can check
2964 * the avalaible space without taking in account the reserve.
2965 * The reserve is guaranted for the processing of incoming
2966 * data, because the buffer will be flushed.
2967 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002968 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002969
2970 /* If there are no space avalaible, and the output buffer is empty.
2971 * in this case, we cannot add more data, so we cannot yield,
2972 * we return the amount of copyied data.
2973 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002974 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002975 return 1;
2976
2977 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002978 if (max > len - l)
2979 max = len - l;
2980
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002981 /* The buffer avalaible size may be not contiguous. This test
2982 * detects a non contiguous buffer and realign it.
2983 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002984 if (bi_space_for_replace(chn->buf) < max)
2985 buffer_slow_realign(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002986
2987 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002988 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002989
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002990 /* buffer replace considers that the input part is filled.
2991 * so, I must forward these new data in the output part.
2992 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002993 b_adv(chn->buf, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002994
2995 l += max;
2996 lua_pop(L, 1);
2997 lua_pushinteger(L, l);
2998
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002999 /* If there are no space avalaible, and the output buffer is empty.
3000 * in this case, we cannot add more data, so we cannot yield,
3001 * we return the amount of copyied data.
3002 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003003 max = chn->buf->size - buffer_len(chn->buf);
3004 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003005 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003006
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003007 if (l < len) {
3008 /* If we are waiting for space in the response buffer, we
3009 * must set the flag WAKERESWR. This flag required the task
3010 * wake up if any activity is detected on the response buffer.
3011 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003012 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003013 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003014 else
3015 HLUA_SET_WAKEREQWR(hlua);
3016 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003017 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003018
3019 return 1;
3020}
3021
3022/* Just a wraper of "_hlua_channel_send". This wrapper permits
3023 * yield the LUA process, and resume it without checking the
3024 * input arguments.
3025 */
3026__LJMP static int hlua_channel_send(lua_State *L)
3027{
3028 MAY_LJMP(check_args(L, 2, "send"));
3029 lua_pushinteger(L, 0);
3030
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003031 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003032}
3033
3034/* This function forward and amount of butes. The data pass from
3035 * the input side of the buffer to the output side, and can be
3036 * forwarded. This function never fails.
3037 *
3038 * The Lua function takes an amount of bytes to be forwarded in
3039 * imput. It returns the number of bytes forwarded.
3040 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003041__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003042{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003043 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003044 int len;
3045 int l;
3046 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003047 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003048
3049 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3050 len = MAY_LJMP(luaL_checkinteger(L, 2));
3051 l = MAY_LJMP(luaL_checkinteger(L, -1));
3052
3053 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003054 if (max > chn->buf->i)
3055 max = chn->buf->i;
3056 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003057 l += max;
3058
3059 lua_pop(L, 1);
3060 lua_pushinteger(L, l);
3061
3062 /* Check if it miss bytes to forward. */
3063 if (l < len) {
3064 /* The the input channel or the output channel are closed, we
3065 * must return the amount of data forwarded.
3066 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003067 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003068 return 1;
3069
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003070 /* If we are waiting for space data in the response buffer, we
3071 * must set the flag WAKERESWR. This flag required the task
3072 * wake up if any activity is detected on the response buffer.
3073 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003074 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003075 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003076 else
3077 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003078
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003079 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01003080 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003081 }
3082
3083 return 1;
3084}
3085
3086/* Just check the input and prepare the stack for the previous
3087 * function "hlua_channel_forward_yield"
3088 */
3089__LJMP static int hlua_channel_forward(lua_State *L)
3090{
3091 MAY_LJMP(check_args(L, 2, "forward"));
3092 MAY_LJMP(hlua_checkchannel(L, 1));
3093 MAY_LJMP(luaL_checkinteger(L, 2));
3094
3095 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003096 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003097}
3098
3099/* Just returns the number of bytes available in the input
3100 * side of the buffer. This function never fails.
3101 */
3102__LJMP static int hlua_channel_get_in_len(lua_State *L)
3103{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003104 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003105
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003106 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003107 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01003108 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003109 return 1;
3110}
3111
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003112/* Returns true if the channel is full. */
3113__LJMP static int hlua_channel_is_full(lua_State *L)
3114{
3115 struct channel *chn;
3116 int rem;
3117
3118 MAY_LJMP(check_args(L, 1, "is_full"));
3119 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3120
3121 rem = chn->buf->size;
3122 rem -= chn->buf->o; /* Output size */
3123 rem -= chn->buf->i; /* Input size */
3124 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3125
3126 lua_pushboolean(L, rem <= 0);
3127 return 1;
3128}
3129
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003130/* Just returns the number of bytes available in the output
3131 * side of the buffer. This function never fails.
3132 */
3133__LJMP static int hlua_channel_get_out_len(lua_State *L)
3134{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003135 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003136
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003137 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003138 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01003139 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003140 return 1;
3141}
3142
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003143/*
3144 *
3145 *
3146 * Class Fetches
3147 *
3148 *
3149 */
3150
3151/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003152 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003153 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003154__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003155{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003156 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003157}
3158
3159/* This function creates and push in the stack a fetch object according
3160 * with a current TXN.
3161 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003162static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003163{
Willy Tarreau7073c472015-04-06 11:15:40 +02003164 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003165
3166 /* Check stack size. */
3167 if (!lua_checkstack(L, 3))
3168 return 0;
3169
3170 /* Create the object: obj[0] = userdata.
3171 * Note that the base of the Fetches object is the
3172 * transaction object.
3173 */
3174 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003175 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003176 lua_rawseti(L, -2, 0);
3177
Willy Tarreau7073c472015-04-06 11:15:40 +02003178 hsmp->s = txn->s;
3179 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003180 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003181 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003182
3183 /* Pop a class sesison metatable and affect it to the userdata. */
3184 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3185 lua_setmetatable(L, -2);
3186
3187 return 1;
3188}
3189
3190/* This function is an LUA binding. It is called with each sample-fetch.
3191 * It uses closure argument to store the associated sample-fetch. It
3192 * returns only one argument or throws an error. An error is thrown
3193 * only if an error is encountered during the argument parsing. If
3194 * the "sample-fetch" function fails, nil is returned.
3195 */
3196__LJMP static int hlua_run_sample_fetch(lua_State *L)
3197{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003198 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003199 struct sample_fetch *f;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003200 struct arg args[ARGM_NBARGS + 1];
3201 int i;
3202 struct sample smp;
3203
3204 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003205 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003206
3207 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003208 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003209
Thierry FOURNIERca988662015-12-20 18:43:03 +01003210 /* Check execution authorization. */
3211 if (f->use & SMP_USE_HTTP_ANY &&
3212 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3213 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3214 "is not available in Lua services", f->kw);
3215 WILL_LJMP(lua_error(L));
3216 }
3217
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003218 /* Get extra arguments. */
3219 for (i = 0; i < lua_gettop(L) - 1; i++) {
3220 if (i >= ARGM_NBARGS)
3221 break;
3222 hlua_lua2arg(L, i + 2, &args[i]);
3223 }
3224 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003225 args[i].data.str.str = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003226
3227 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003228 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003229
3230 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003231 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003232 lua_pushfstring(L, "error in arguments");
3233 WILL_LJMP(lua_error(L));
3234 }
3235
3236 /* Initialise the sample. */
3237 memset(&smp, 0, sizeof(smp));
3238
3239 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003240 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003241 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003242 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003243 lua_pushstring(L, "");
3244 else
3245 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003246 return 1;
3247 }
3248
3249 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003250 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003251 hlua_smp2lua_str(L, &smp);
3252 else
3253 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003254 return 1;
3255}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003256
3257/*
3258 *
3259 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003260 * Class Converters
3261 *
3262 *
3263 */
3264
3265/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003266 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003267 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003268__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003269{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003270 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003271}
3272
3273/* This function creates and push in the stack a Converters object
3274 * according with a current TXN.
3275 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003276static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003277{
Willy Tarreau7073c472015-04-06 11:15:40 +02003278 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003279
3280 /* Check stack size. */
3281 if (!lua_checkstack(L, 3))
3282 return 0;
3283
3284 /* Create the object: obj[0] = userdata.
3285 * Note that the base of the Converters object is the
3286 * same than the TXN object.
3287 */
3288 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003289 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003290 lua_rawseti(L, -2, 0);
3291
Willy Tarreau7073c472015-04-06 11:15:40 +02003292 hsmp->s = txn->s;
3293 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003294 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003295 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003296
Willy Tarreau87b09662015-04-03 00:22:06 +02003297 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003298 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3299 lua_setmetatable(L, -2);
3300
3301 return 1;
3302}
3303
3304/* This function is an LUA binding. It is called with each converter.
3305 * It uses closure argument to store the associated converter. It
3306 * returns only one argument or throws an error. An error is thrown
3307 * only if an error is encountered during the argument parsing. If
3308 * the converter function function fails, nil is returned.
3309 */
3310__LJMP static int hlua_run_sample_conv(lua_State *L)
3311{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003312 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003313 struct sample_conv *conv;
3314 struct arg args[ARGM_NBARGS + 1];
3315 int i;
3316 struct sample smp;
3317
3318 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003319 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003320
3321 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003322 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003323
3324 /* Get extra arguments. */
3325 for (i = 0; i < lua_gettop(L) - 2; i++) {
3326 if (i >= ARGM_NBARGS)
3327 break;
3328 hlua_lua2arg(L, i + 3, &args[i]);
3329 }
3330 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003331 args[i].data.str.str = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003332
3333 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003334 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003335
3336 /* Run the special args checker. */
3337 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3338 hlua_pusherror(L, "error in arguments");
3339 WILL_LJMP(lua_error(L));
3340 }
3341
3342 /* Initialise the sample. */
3343 if (!hlua_lua2smp(L, 2, &smp)) {
3344 hlua_pusherror(L, "error in the input argument");
3345 WILL_LJMP(lua_error(L));
3346 }
3347
Willy Tarreau1777ea62016-03-10 16:15:46 +01003348 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3349
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003350 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003351 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003352 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003353 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003354 WILL_LJMP(lua_error(L));
3355 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003356 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3357 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003358 hlua_pusherror(L, "error during the input argument casting");
3359 WILL_LJMP(lua_error(L));
3360 }
3361
3362 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003363 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003364 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003365 lua_pushstring(L, "");
3366 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003367 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003368 return 1;
3369 }
3370
3371 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003372 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003373 hlua_smp2lua_str(L, &smp);
3374 else
3375 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003376 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003377}
3378
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003379/*
3380 *
3381 *
3382 * Class AppletTCP
3383 *
3384 *
3385 */
3386
3387/* Returns a struct hlua_txn if the stack entry "ud" is
3388 * a class stream, otherwise it throws an error.
3389 */
3390__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3391{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003392 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003393}
3394
3395/* This function creates and push in the stack an Applet object
3396 * according with a current TXN.
3397 */
3398static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3399{
3400 struct hlua_appctx *appctx;
3401 struct stream_interface *si = ctx->owner;
3402 struct stream *s = si_strm(si);
3403 struct proxy *p = s->be;
3404
3405 /* Check stack size. */
3406 if (!lua_checkstack(L, 3))
3407 return 0;
3408
3409 /* Create the object: obj[0] = userdata.
3410 * Note that the base of the Converters object is the
3411 * same than the TXN object.
3412 */
3413 lua_newtable(L);
3414 appctx = lua_newuserdata(L, sizeof(*appctx));
3415 lua_rawseti(L, -2, 0);
3416 appctx->appctx = ctx;
3417 appctx->htxn.s = s;
3418 appctx->htxn.p = p;
3419
3420 /* Create the "f" field that contains a list of fetches. */
3421 lua_pushstring(L, "f");
3422 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3423 return 0;
3424 lua_settable(L, -3);
3425
3426 /* Create the "sf" field that contains a list of stringsafe fetches. */
3427 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003428 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003429 return 0;
3430 lua_settable(L, -3);
3431
3432 /* Create the "c" field that contains a list of converters. */
3433 lua_pushstring(L, "c");
3434 if (!hlua_converters_new(L, &appctx->htxn, 0))
3435 return 0;
3436 lua_settable(L, -3);
3437
3438 /* Create the "sc" field that contains a list of stringsafe converters. */
3439 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003440 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003441 return 0;
3442 lua_settable(L, -3);
3443
3444 /* Pop a class stream metatable and affect it to the table. */
3445 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3446 lua_setmetatable(L, -2);
3447
3448 return 1;
3449}
3450
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003451__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3452{
3453 struct hlua_appctx *appctx;
3454 struct stream *s;
3455 const char *name;
3456 size_t len;
3457 struct sample smp;
3458
3459 MAY_LJMP(check_args(L, 3, "set_var"));
3460
3461 /* It is useles to retrieve the stream, but this function
3462 * runs only in a stream context.
3463 */
3464 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3465 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3466 s = appctx->htxn.s;
3467
3468 /* Converts the third argument in a sample. */
3469 hlua_lua2smp(L, 3, &smp);
3470
3471 /* Store the sample in a variable. */
3472 smp_set_owner(&smp, s->be, s->sess, s, 0);
3473 vars_set_by_name(name, len, &smp);
3474 return 0;
3475}
3476
3477__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3478{
3479 struct hlua_appctx *appctx;
3480 struct stream *s;
3481 const char *name;
3482 size_t len;
3483 struct sample smp;
3484
3485 MAY_LJMP(check_args(L, 2, "unset_var"));
3486
3487 /* It is useles to retrieve the stream, but this function
3488 * runs only in a stream context.
3489 */
3490 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3491 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3492 s = appctx->htxn.s;
3493
3494 /* Unset the variable. */
3495 smp_set_owner(&smp, s->be, s->sess, s, 0);
3496 vars_unset_by_name(name, len, &smp);
3497 return 0;
3498}
3499
3500__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3501{
3502 struct hlua_appctx *appctx;
3503 struct stream *s;
3504 const char *name;
3505 size_t len;
3506 struct sample smp;
3507
3508 MAY_LJMP(check_args(L, 2, "get_var"));
3509
3510 /* It is useles to retrieve the stream, but this function
3511 * runs only in a stream context.
3512 */
3513 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3514 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3515 s = appctx->htxn.s;
3516
3517 smp_set_owner(&smp, s->be, s->sess, s, 0);
3518 if (!vars_get_by_name(name, len, &smp)) {
3519 lua_pushnil(L);
3520 return 1;
3521 }
3522
3523 return hlua_smp2lua(L, &smp);
3524}
3525
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003526__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3527{
3528 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3529 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003530 struct hlua *hlua;
3531
3532 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003533 if (!s->hlua)
3534 return 0;
3535 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003536
3537 MAY_LJMP(check_args(L, 2, "set_priv"));
3538
3539 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003540 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003541
3542 /* Get and store new value. */
3543 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3544 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3545
3546 return 0;
3547}
3548
3549__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3550{
3551 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3552 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003553 struct hlua *hlua;
3554
3555 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003556 if (!s->hlua) {
3557 lua_pushnil(L);
3558 return 1;
3559 }
3560 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003561
3562 /* Push configuration index in the stack. */
3563 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3564
3565 return 1;
3566}
3567
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003568/* If expected data not yet available, it returns a yield. This function
3569 * consumes the data in the buffer. It returns a string containing the
3570 * data. This string can be empty.
3571 */
3572__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3573{
3574 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3575 struct stream_interface *si = appctx->appctx->owner;
3576 int ret;
3577 char *blk1;
3578 int len1;
3579 char *blk2;
3580 int len2;
3581
3582 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003583 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003584
3585 /* Data not yet avalaible. return yield. */
3586 if (ret == 0) {
3587 si_applet_cant_get(si);
3588 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
3589 }
3590
3591 /* End of data: commit the total strings and return. */
3592 if (ret < 0) {
3593 luaL_pushresult(&appctx->b);
3594 return 1;
3595 }
3596
3597 /* Ensure that the block 2 length is usable. */
3598 if (ret == 1)
3599 len2 = 0;
3600
3601 /* dont check the max length read and dont check. */
3602 luaL_addlstring(&appctx->b, blk1, len1);
3603 luaL_addlstring(&appctx->b, blk2, len2);
3604
3605 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003606 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003607 luaL_pushresult(&appctx->b);
3608 return 1;
3609}
3610
3611/* Check arguments for the fucntion "hlua_channel_get_yield". */
3612__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3613{
3614 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3615
3616 /* Initialise the string catenation. */
3617 luaL_buffinit(L, &appctx->b);
3618
3619 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3620}
3621
3622/* If expected data not yet available, it returns a yield. This function
3623 * consumes the data in the buffer. It returns a string containing the
3624 * data. This string can be empty.
3625 */
3626__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3627{
3628 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3629 struct stream_interface *si = appctx->appctx->owner;
3630 int len = MAY_LJMP(luaL_checkinteger(L, 2));
3631 int ret;
3632 char *blk1;
3633 int len1;
3634 char *blk2;
3635 int len2;
3636
3637 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003638 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003639
3640 /* Data not yet avalaible. return yield. */
3641 if (ret == 0) {
3642 si_applet_cant_get(si);
3643 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3644 }
3645
3646 /* End of data: commit the total strings and return. */
3647 if (ret < 0) {
3648 luaL_pushresult(&appctx->b);
3649 return 1;
3650 }
3651
3652 /* Ensure that the block 2 length is usable. */
3653 if (ret == 1)
3654 len2 = 0;
3655
3656 if (len == -1) {
3657
3658 /* If len == -1, catenate all the data avalaile and
3659 * yield because we want to get all the data until
3660 * the end of data stream.
3661 */
3662 luaL_addlstring(&appctx->b, blk1, len1);
3663 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003664 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003665 si_applet_cant_get(si);
3666 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3667
3668 } else {
3669
3670 /* Copy the fisrt block caping to the length required. */
3671 if (len1 > len)
3672 len1 = len;
3673 luaL_addlstring(&appctx->b, blk1, len1);
3674 len -= len1;
3675
3676 /* Copy the second block. */
3677 if (len2 > len)
3678 len2 = len;
3679 luaL_addlstring(&appctx->b, blk2, len2);
3680 len -= len2;
3681
3682 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003683 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003684
3685 /* If we are no other data avalaible, yield waiting for new data. */
3686 if (len > 0) {
3687 lua_pushinteger(L, len);
3688 lua_replace(L, 2);
3689 si_applet_cant_get(si);
3690 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3691 }
3692
3693 /* return the result. */
3694 luaL_pushresult(&appctx->b);
3695 return 1;
3696 }
3697
3698 /* we never executes this */
3699 hlua_pusherror(L, "Lua: internal error");
3700 WILL_LJMP(lua_error(L));
3701 return 0;
3702}
3703
3704/* Check arguments for the fucntion "hlua_channel_get_yield". */
3705__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3706{
3707 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3708 int len = -1;
3709
3710 if (lua_gettop(L) > 2)
3711 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3712 if (lua_gettop(L) >= 2) {
3713 len = MAY_LJMP(luaL_checkinteger(L, 2));
3714 lua_pop(L, 1);
3715 }
3716
3717 /* Confirm or set the required length */
3718 lua_pushinteger(L, len);
3719
3720 /* Initialise the string catenation. */
3721 luaL_buffinit(L, &appctx->b);
3722
3723 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3724}
3725
3726/* Append data in the output side of the buffer. This data is immediatly
3727 * sent. The fcuntion returns the ammount of data writed. If the buffer
3728 * cannot contains the data, the function yield. The function returns -1
3729 * if the channel is closed.
3730 */
3731__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3732{
3733 size_t len;
3734 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3735 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3736 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3737 struct stream_interface *si = appctx->appctx->owner;
3738 struct channel *chn = si_ic(si);
3739 int max;
3740
3741 /* Get the max amount of data which can write as input in the channel. */
3742 max = channel_recv_max(chn);
3743 if (max > (len - l))
3744 max = len - l;
3745
3746 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003747 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003748
3749 /* update counters. */
3750 l += max;
3751 lua_pop(L, 1);
3752 lua_pushinteger(L, l);
3753
3754 /* If some data is not send, declares the situation to the
3755 * applet, and returns a yield.
3756 */
3757 if (l < len) {
3758 si_applet_cant_put(si);
3759 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
3760 }
3761
3762 return 1;
3763}
3764
3765/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3766 * yield the LUA process, and resume it without checking the
3767 * input arguments.
3768 */
3769__LJMP static int hlua_applet_tcp_send(lua_State *L)
3770{
3771 MAY_LJMP(check_args(L, 2, "send"));
3772 lua_pushinteger(L, 0);
3773
3774 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3775}
3776
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003777/*
3778 *
3779 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003780 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003781 *
3782 *
3783 */
3784
3785/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003786 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003787 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003788__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003789{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003790 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003791}
3792
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003793/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003794 * according with a current TXN.
3795 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003796static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003797{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003798 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003799 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003800 struct stream_interface *si = ctx->owner;
3801 struct stream *s = si_strm(si);
3802 struct proxy *px = s->be;
3803 struct http_txn *txn = s->txn;
3804 const char *path;
3805 const char *end;
3806 const char *p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003807
3808 /* Check stack size. */
3809 if (!lua_checkstack(L, 3))
3810 return 0;
3811
3812 /* Create the object: obj[0] = userdata.
3813 * Note that the base of the Converters object is the
3814 * same than the TXN object.
3815 */
3816 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003817 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003818 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003819 appctx->appctx = ctx;
3820 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003821 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003822 appctx->htxn.s = s;
3823 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003824
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003825 /* Create the "f" field that contains a list of fetches. */
3826 lua_pushstring(L, "f");
3827 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3828 return 0;
3829 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003830
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003831 /* Create the "sf" field that contains a list of stringsafe fetches. */
3832 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003833 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003834 return 0;
3835 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003836
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003837 /* Create the "c" field that contains a list of converters. */
3838 lua_pushstring(L, "c");
3839 if (!hlua_converters_new(L, &appctx->htxn, 0))
3840 return 0;
3841 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003842
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003843 /* Create the "sc" field that contains a list of stringsafe converters. */
3844 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003845 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003846 return 0;
3847 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003848
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003849 /* Stores the request method. */
3850 lua_pushstring(L, "method");
3851 lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l);
3852 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003853
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003854 /* Stores the http version. */
3855 lua_pushstring(L, "version");
3856 lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
3857 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003858
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003859 /* creates an array of headers. hlua_http_get_headers() crates and push
3860 * the array on the top of the stack.
3861 */
3862 lua_pushstring(L, "headers");
3863 htxn.s = s;
3864 htxn.p = px;
3865 htxn.dir = SMP_OPT_DIR_REQ;
3866 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3867 return 0;
3868 lua_settable(L, -3);
3869
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003870 /* Get path and qs */
3871 path = http_get_path(txn);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003872 if (path) {
3873 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
3874 p = path;
3875 while (p < end && *p != '?')
3876 p++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003877
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003878 /* Stores the request path. */
3879 lua_pushstring(L, "path");
3880 lua_pushlstring(L, path, p - path);
3881 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003882
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003883 /* Stores the query string. */
3884 lua_pushstring(L, "qs");
3885 if (*p == '?')
3886 p++;
3887 lua_pushlstring(L, p, end - p);
3888 lua_settable(L, -3);
3889 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003890
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003891 /* Stores the request path. */
3892 lua_pushstring(L, "length");
3893 lua_pushinteger(L, txn->req.body_len);
3894 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003895
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003896 /* Create an array of HTTP request headers. */
3897 lua_pushstring(L, "headers");
3898 MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req));
3899 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003900
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003901 /* Create an empty array of HTTP request headers. */
3902 lua_pushstring(L, "response");
3903 lua_newtable(L);
3904 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003905
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003906 /* Pop a class stream metatable and affect it to the table. */
3907 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3908 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003909
3910 return 1;
3911}
3912
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003913__LJMP static int hlua_applet_http_set_var(lua_State *L)
3914{
3915 struct hlua_appctx *appctx;
3916 struct stream *s;
3917 const char *name;
3918 size_t len;
3919 struct sample smp;
3920
3921 MAY_LJMP(check_args(L, 3, "set_var"));
3922
3923 /* It is useles to retrieve the stream, but this function
3924 * runs only in a stream context.
3925 */
3926 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3927 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3928 s = appctx->htxn.s;
3929
3930 /* Converts the third argument in a sample. */
3931 hlua_lua2smp(L, 3, &smp);
3932
3933 /* Store the sample in a variable. */
3934 smp_set_owner(&smp, s->be, s->sess, s, 0);
3935 vars_set_by_name(name, len, &smp);
3936 return 0;
3937}
3938
3939__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3940{
3941 struct hlua_appctx *appctx;
3942 struct stream *s;
3943 const char *name;
3944 size_t len;
3945 struct sample smp;
3946
3947 MAY_LJMP(check_args(L, 2, "unset_var"));
3948
3949 /* It is useles to retrieve the stream, but this function
3950 * runs only in a stream context.
3951 */
3952 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3953 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3954 s = appctx->htxn.s;
3955
3956 /* Unset the variable. */
3957 smp_set_owner(&smp, s->be, s->sess, s, 0);
3958 vars_unset_by_name(name, len, &smp);
3959 return 0;
3960}
3961
3962__LJMP static int hlua_applet_http_get_var(lua_State *L)
3963{
3964 struct hlua_appctx *appctx;
3965 struct stream *s;
3966 const char *name;
3967 size_t len;
3968 struct sample smp;
3969
3970 MAY_LJMP(check_args(L, 2, "get_var"));
3971
3972 /* It is useles to retrieve the stream, but this function
3973 * runs only in a stream context.
3974 */
3975 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3976 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3977 s = appctx->htxn.s;
3978
3979 smp_set_owner(&smp, s->be, s->sess, s, 0);
3980 if (!vars_get_by_name(name, len, &smp)) {
3981 lua_pushnil(L);
3982 return 1;
3983 }
3984
3985 return hlua_smp2lua(L, &smp);
3986}
3987
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003988__LJMP static int hlua_applet_http_set_priv(lua_State *L)
3989{
3990 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3991 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003992 struct hlua *hlua;
3993
3994 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003995 if (!s->hlua)
3996 return 0;
3997 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003998
3999 MAY_LJMP(check_args(L, 2, "set_priv"));
4000
4001 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004002 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004003
4004 /* Get and store new value. */
4005 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4006 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4007
4008 return 0;
4009}
4010
4011__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4012{
4013 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4014 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004015 struct hlua *hlua;
4016
4017 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004018 if (!s->hlua) {
4019 lua_pushnil(L);
4020 return 1;
4021 }
4022 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004023
4024 /* Push configuration index in the stack. */
4025 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4026
4027 return 1;
4028}
4029
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004030/* If expected data not yet available, it returns a yield. This function
4031 * consumes the data in the buffer. It returns a string containing the
4032 * data. This string can be empty.
4033 */
4034__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004035{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004036 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4037 struct stream_interface *si = appctx->appctx->owner;
4038 struct channel *chn = si_ic(si);
4039 int ret;
4040 char *blk1;
4041 int len1;
4042 char *blk2;
4043 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004044
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004045 /* Maybe we cant send a 100-continue ? */
4046 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004047 ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004048 /* if ret == -2 or -3 the channel closed or the message si too
4049 * big for the buffers. We cant send anything. So, we ignoring
4050 * the error, considers that the 100-continue is sent, and try
4051 * to receive.
4052 * If ret is -1, we dont have room in the buffer, so we yield.
4053 */
4054 if (ret == -1) {
4055 si_applet_cant_put(si);
4056 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
4057 }
4058 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4059 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004060
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004061 /* Check for the end of the data. */
4062 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4063 luaL_pushresult(&appctx->b);
4064 return 1;
4065 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004066
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004067 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004068 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004069
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004070 /* Data not yet avalaible. return yield. */
4071 if (ret == 0) {
4072 si_applet_cant_get(si);
4073 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
4074 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004075
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004076 /* End of data: commit the total strings and return. */
4077 if (ret < 0) {
4078 luaL_pushresult(&appctx->b);
4079 return 1;
4080 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004081
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004082 /* Ensure that the block 2 length is usable. */
4083 if (ret == 1)
4084 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004085
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004086 /* Copy the fisrt block caping to the length required. */
4087 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4088 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4089 luaL_addlstring(&appctx->b, blk1, len1);
4090 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004091
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004092 /* Copy the second block. */
4093 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4094 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4095 luaL_addlstring(&appctx->b, blk2, len2);
4096 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004097
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004098 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004099 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004100 luaL_pushresult(&appctx->b);
4101 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004102}
4103
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004104/* Check arguments for the fucntion "hlua_channel_get_yield". */
4105__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004106{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004107 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004108
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004109 /* Initialise the string catenation. */
4110 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004111
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004112 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004113}
4114
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004115/* If expected data not yet available, it returns a yield. This function
4116 * consumes the data in the buffer. It returns a string containing the
4117 * data. This string can be empty.
4118 */
4119__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004120{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004121 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4122 struct stream_interface *si = appctx->appctx->owner;
4123 int len = MAY_LJMP(luaL_checkinteger(L, 2));
4124 struct channel *chn = si_ic(si);
4125 int ret;
4126 char *blk1;
4127 int len1;
4128 char *blk2;
4129 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004130
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004131 /* Maybe we cant send a 100-continue ? */
4132 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004133 ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004134 /* if ret == -2 or -3 the channel closed or the message si too
4135 * big for the buffers. We cant send anything. So, we ignoring
4136 * the error, considers that the 100-continue is sent, and try
4137 * to receive.
4138 * If ret is -1, we dont have room in the buffer, so we yield.
4139 */
4140 if (ret == -1) {
4141 si_applet_cant_put(si);
4142 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4143 }
4144 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4145 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004146
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004147 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004148 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004149
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004150 /* Data not yet avalaible. return yield. */
4151 if (ret == 0) {
4152 si_applet_cant_get(si);
4153 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4154 }
4155
4156 /* End of data: commit the total strings and return. */
4157 if (ret < 0) {
4158 luaL_pushresult(&appctx->b);
4159 return 1;
4160 }
4161
4162 /* Ensure that the block 2 length is usable. */
4163 if (ret == 1)
4164 len2 = 0;
4165
4166 /* Copy the fisrt block caping to the length required. */
4167 if (len1 > len)
4168 len1 = len;
4169 luaL_addlstring(&appctx->b, blk1, len1);
4170 len -= len1;
4171
4172 /* Copy the second block. */
4173 if (len2 > len)
4174 len2 = len;
4175 luaL_addlstring(&appctx->b, blk2, len2);
4176 len -= len2;
4177
4178 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004179 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004180 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4181 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4182
4183 /* If we are no other data avalaible, yield waiting for new data. */
4184 if (len > 0) {
4185 lua_pushinteger(L, len);
4186 lua_replace(L, 2);
4187 si_applet_cant_get(si);
4188 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4189 }
4190
4191 /* return the result. */
4192 luaL_pushresult(&appctx->b);
4193 return 1;
4194}
4195
4196/* Check arguments for the fucntion "hlua_channel_get_yield". */
4197__LJMP static int hlua_applet_http_recv(lua_State *L)
4198{
4199 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4200 int len = -1;
4201
4202 /* Check arguments. */
4203 if (lua_gettop(L) > 2)
4204 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4205 if (lua_gettop(L) >= 2) {
4206 len = MAY_LJMP(luaL_checkinteger(L, 2));
4207 lua_pop(L, 1);
4208 }
4209
4210 /* Check the required length */
4211 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4212 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4213 lua_pushinteger(L, len);
4214
4215 /* Initialise the string catenation. */
4216 luaL_buffinit(L, &appctx->b);
4217
4218 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4219}
4220
4221/* Append data in the output side of the buffer. This data is immediatly
4222 * sent. The fcuntion returns the ammount of data writed. If the buffer
4223 * cannot contains the data, the function yield. The function returns -1
4224 * if the channel is closed.
4225 */
4226__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4227{
4228 size_t len;
4229 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4230 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4231 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4232 struct stream_interface *si = appctx->appctx->owner;
4233 struct channel *chn = si_ic(si);
4234 int max;
4235
4236 /* Get the max amount of data which can write as input in the channel. */
4237 max = channel_recv_max(chn);
4238 if (max > (len - l))
4239 max = len - l;
4240
4241 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004242 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004243
4244 /* update counters. */
4245 l += max;
4246 lua_pop(L, 1);
4247 lua_pushinteger(L, l);
4248
4249 /* If some data is not send, declares the situation to the
4250 * applet, and returns a yield.
4251 */
4252 if (l < len) {
4253 si_applet_cant_put(si);
4254 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
4255 }
4256
4257 return 1;
4258}
4259
4260/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4261 * yield the LUA process, and resume it without checking the
4262 * input arguments.
4263 */
4264__LJMP static int hlua_applet_http_send(lua_State *L)
4265{
4266 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4267 size_t len;
4268 char hex[10];
4269
4270 MAY_LJMP(luaL_checklstring(L, 2, &len));
4271
4272 /* If transfer encoding chunked is selected, we surround the data
4273 * by chunk data.
4274 */
4275 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4276 snprintf(hex, 9, "%x", (unsigned int)len);
4277 lua_pushfstring(L, "%s\r\n", hex);
4278 lua_insert(L, 2); /* swap the last 2 entries. */
4279 lua_pushstring(L, "\r\n");
4280 lua_concat(L, 3);
4281 }
4282
4283 /* This interger is used for followinf the amount of data sent. */
4284 lua_pushinteger(L, 0);
4285
4286 /* We want to send some data. Headers must be sent. */
4287 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4288 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4289 WILL_LJMP(lua_error(L));
4290 }
4291
4292 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4293}
4294
4295__LJMP static int hlua_applet_http_addheader(lua_State *L)
4296{
4297 const char *name;
4298 int ret;
4299
4300 MAY_LJMP(hlua_checkapplet_http(L, 1));
4301 name = MAY_LJMP(luaL_checkstring(L, 2));
4302 MAY_LJMP(luaL_checkstring(L, 3));
4303
4304 /* Push in the stack the "response" entry. */
4305 ret = lua_getfield(L, 1, "response");
4306 if (ret != LUA_TTABLE) {
4307 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4308 "is expected as an array. %s found", lua_typename(L, ret));
4309 WILL_LJMP(lua_error(L));
4310 }
4311
4312 /* check if the header is already registered if it is not
4313 * the case, register it.
4314 */
4315 ret = lua_getfield(L, -1, name);
4316 if (ret == LUA_TNIL) {
4317
4318 /* Entry not found. */
4319 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4320
4321 /* Insert the new header name in the array in the top of the stack.
4322 * It left the new array in the top of the stack.
4323 */
4324 lua_newtable(L);
4325 lua_pushvalue(L, 2);
4326 lua_pushvalue(L, -2);
4327 lua_settable(L, -4);
4328
4329 } else if (ret != LUA_TTABLE) {
4330
4331 /* corruption error. */
4332 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4333 "is expected as an array. %s found", name, lua_typename(L, ret));
4334 WILL_LJMP(lua_error(L));
4335 }
4336
4337 /* Now the top od thestack is an array of values. We push
4338 * the header value as new entry.
4339 */
4340 lua_pushvalue(L, 3);
4341 ret = lua_rawlen(L, -2);
4342 lua_rawseti(L, -2, ret + 1);
4343 lua_pushboolean(L, 1);
4344 return 1;
4345}
4346
4347__LJMP static int hlua_applet_http_status(lua_State *L)
4348{
4349 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4350 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004351 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004352
4353 if (status < 100 || status > 599) {
4354 lua_pushboolean(L, 0);
4355 return 1;
4356 }
4357
4358 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004359 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004360 lua_pushboolean(L, 1);
4361 return 1;
4362}
4363
4364/* We will build the status line and the headers of the HTTP response.
4365 * We will try send at once if its not possible, we give back the hand
4366 * waiting for more room.
4367 */
4368__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4369{
4370 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4371 struct stream_interface *si = appctx->appctx->owner;
4372 struct channel *chn = si_ic(si);
4373 int ret;
4374 size_t len;
4375 const char *msg;
4376
4377 /* Get the message as the first argument on the stack. */
4378 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4379
4380 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004381 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004382
4383 /* if ret == -2 or -3 the channel closed or the message si too
4384 * big for the buffers.
4385 */
4386 if (ret == -2 || ret == -3) {
4387 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4388 WILL_LJMP(lua_error(L));
4389 }
4390
4391 /* If ret is -1, we dont have room in the buffer, so we yield. */
4392 if (ret == -1) {
4393 si_applet_cant_put(si);
4394 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
4395 }
4396
4397 /* Headers sent, set the flag. */
4398 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4399 return 0;
4400}
4401
4402__LJMP static int hlua_applet_http_start_response(lua_State *L)
4403{
4404 struct chunk *tmp = get_trash_chunk();
4405 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004406 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02004407 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004408 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02004409 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004410 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004411 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004412 int hdr_chunked = 0;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004413 const char *reason = appctx->appctx->ctx.hlua_apphttp.reason;
4414
4415 if (reason == NULL)
4416 reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004417
4418 /* Use the same http version than the request. */
4419 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004420 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004421 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004422 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004423
4424 /* Get the array associated to the field "response" in the object AppletHTTP. */
4425 lua_pushvalue(L, 0);
4426 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4427 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4428 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4429 WILL_LJMP(lua_error(L));
4430 }
4431
4432 /* Browse the list of headers. */
4433 lua_pushnil(L);
4434 while(lua_next(L, -2) != 0) {
4435
4436 /* We expect a string as -2. */
4437 if (lua_type(L, -2) != LUA_TSTRING) {
4438 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4439 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4440 lua_typename(L, lua_type(L, -2)));
4441 WILL_LJMP(lua_error(L));
4442 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004443 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004444
4445 /* We expect an array as -1. */
4446 if (lua_type(L, -1) != LUA_TTABLE) {
4447 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4448 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4449 name,
4450 lua_typename(L, lua_type(L, -1)));
4451 WILL_LJMP(lua_error(L));
4452 }
4453
4454 /* Browse the table who is on the top of the stack. */
4455 lua_pushnil(L);
4456 while(lua_next(L, -2) != 0) {
4457
4458 /* We expect a number as -2. */
4459 if (lua_type(L, -2) != LUA_TNUMBER) {
4460 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4461 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4462 name,
4463 lua_typename(L, lua_type(L, -2)));
4464 WILL_LJMP(lua_error(L));
4465 }
4466 id = lua_tointeger(L, -2);
4467
4468 /* We expect a string as -2. */
4469 if (lua_type(L, -1) != LUA_TSTRING) {
4470 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4471 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4472 name, id,
4473 lua_typename(L, lua_type(L, -1)));
4474 WILL_LJMP(lua_error(L));
4475 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004476 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004477
4478 /* Catenate a new header. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004479 if (tmp->len + name_len + 2 + value_len + 2 < tmp->size) {
4480 memcpy(tmp->str + tmp->len, name, name_len);
4481 tmp->len += name_len;
4482 tmp->str[tmp->len++] = ':';
4483 tmp->str[tmp->len++] = ' ';
4484
4485 memcpy(tmp->str + tmp->len, value, value_len);
4486 tmp->len += value_len;
4487 tmp->str[tmp->len++] = '\r';
4488 tmp->str[tmp->len++] = '\n';
4489 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004490
4491 /* Protocol checks. */
4492
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004493 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004494 * is done without control. If it contains a bad value,
4495 * the content-length remains negative so that we can
4496 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004497 */
Willy Tarreaua3294632017-08-23 11:24:47 +02004498 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004499 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004500
4501 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004502 if (name_len == 17 && value_len == 7 &&
4503 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004504 strcasecmp("chunked", value) == 0)
4505 hdr_chunked = 1;
4506
4507 /* Remove the array from the stack, and get next element with a remaining string. */
4508 lua_pop(L, 1);
4509 }
4510
4511 /* Remove the array from the stack, and get next element with a remaining string. */
4512 lua_pop(L, 1);
4513 }
4514
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004515 /* If we dont have a content-length set, and the HTTP version is 1.1
4516 * and the status code implies the presence of a message body, we must
4517 * announce a transfer encoding chunked. This is required by haproxy
4518 * for the keepalive compliance. If the applet annouces a transfer-encoding
4519 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004520 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004521 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004522 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
4523 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4524 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4525 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004526 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
4527 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
4528 }
4529
4530 /* Finalize headers. */
4531 chunk_appendf(tmp, "\r\n");
4532
4533 /* Remove the last entry and the array of headers */
4534 lua_pop(L, 2);
4535
4536 /* Push the headers block. */
4537 lua_pushlstring(L, tmp->str, tmp->len);
4538
4539 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
4540}
4541
4542/*
4543 *
4544 *
4545 * Class HTTP
4546 *
4547 *
4548 */
4549
4550/* Returns a struct hlua_txn if the stack entry "ud" is
4551 * a class stream, otherwise it throws an error.
4552 */
4553__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
4554{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004555 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004556}
4557
4558/* This function creates and push in the stack a HTTP object
4559 * according with a current TXN.
4560 */
4561static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4562{
4563 struct hlua_txn *htxn;
4564
4565 /* Check stack size. */
4566 if (!lua_checkstack(L, 3))
4567 return 0;
4568
4569 /* Create the object: obj[0] = userdata.
4570 * Note that the base of the Converters object is the
4571 * same than the TXN object.
4572 */
4573 lua_newtable(L);
4574 htxn = lua_newuserdata(L, sizeof(*htxn));
4575 lua_rawseti(L, -2, 0);
4576
4577 htxn->s = txn->s;
4578 htxn->p = txn->p;
4579
4580 /* Pop a class stream metatable and affect it to the table. */
4581 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4582 lua_setmetatable(L, -2);
4583
4584 return 1;
4585}
4586
4587/* This function creates ans returns an array of HTTP headers.
4588 * This function does not fails. It is used as wrapper with the
4589 * 2 following functions.
4590 */
4591__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4592{
4593 const char *cur_ptr, *cur_next, *p;
4594 int old_idx, cur_idx;
4595 struct hdr_idx_elem *cur_hdr;
4596 const char *hn, *hv;
4597 int hnl, hvl;
4598 int type;
4599 const char *in;
4600 char *out;
4601 int len;
4602
4603 /* Create the table. */
4604 lua_newtable(L);
4605
4606 if (!htxn->s->txn)
4607 return 1;
4608
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004609 /* Check if a valid response is parsed */
4610 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4611 return 1;
4612
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004613 /* Build array of headers. */
4614 old_idx = 0;
4615 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
4616
4617 while (1) {
4618 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
4619 if (!cur_idx)
4620 break;
4621 old_idx = cur_idx;
4622
4623 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
4624 cur_ptr = cur_next;
4625 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
4626
4627 /* Now we have one full header at cur_ptr of len cur_hdr->len,
4628 * and the next header starts at cur_next. We'll check
4629 * this header in the list as well as against the default
4630 * rule.
4631 */
4632
4633 /* look for ': *'. */
4634 hn = cur_ptr;
4635 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
4636 if (p >= cur_ptr+cur_hdr->len)
4637 continue;
4638 hnl = p - hn;
4639 p++;
4640 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
4641 p++;
4642 if (p >= cur_ptr+cur_hdr->len)
4643 continue;
4644 hv = p;
4645 hvl = cur_ptr+cur_hdr->len-p;
4646
4647 /* Lowercase the key. Don't check the size of trash, it have
4648 * the size of one buffer and the input data contains in one
4649 * buffer.
4650 */
4651 out = trash.str;
4652 for (in=hn; in<hn+hnl; in++, out++)
4653 *out = tolower(*in);
4654 *out = '\0';
4655
4656 /* Check for existing entry:
4657 * assume that the table is on the top of the stack, and
4658 * push the key in the stack, the function lua_gettable()
4659 * perform the lookup.
4660 */
4661 lua_pushlstring(L, trash.str, hnl);
4662 lua_gettable(L, -2);
4663 type = lua_type(L, -1);
4664
4665 switch (type) {
4666 case LUA_TNIL:
4667 /* Table not found, create it. */
4668 lua_pop(L, 1); /* remove the nil value. */
4669 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
4670 lua_newtable(L); /* create and push empty table. */
4671 lua_pushlstring(L, hv, hvl); /* push header value. */
4672 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4673 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4674 break;
4675
4676 case LUA_TTABLE:
4677 /* Entry found: push the value in the table. */
4678 len = lua_rawlen(L, -1);
4679 lua_pushlstring(L, hv, hvl); /* push header value. */
4680 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4681 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4682 break;
4683
4684 default:
4685 /* Other cases are errors. */
4686 hlua_pusherror(L, "internal error during the parsing of headers.");
4687 WILL_LJMP(lua_error(L));
4688 }
4689 }
4690
4691 return 1;
4692}
4693
4694__LJMP static int hlua_http_req_get_headers(lua_State *L)
4695{
4696 struct hlua_txn *htxn;
4697
4698 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4699 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4700
4701 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4702}
4703
4704__LJMP static int hlua_http_res_get_headers(lua_State *L)
4705{
4706 struct hlua_txn *htxn;
4707
4708 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4709 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4710
4711 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4712}
4713
4714/* This function replace full header, or just a value in
4715 * the request or in the response. It is a wrapper fir the
4716 * 4 following functions.
4717 */
4718__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4719 struct http_msg *msg, int action)
4720{
4721 size_t name_len;
4722 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4723 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4724 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
4725 struct my_regex re;
4726
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004727 /* Check if a valid response is parsed */
4728 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4729 return 0;
4730
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004731 if (!regex_comp(reg, &re, 1, 1, NULL))
4732 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4733
4734 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
4735 regex_free(&re);
4736 return 0;
4737}
4738
4739__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4740{
4741 struct hlua_txn *htxn;
4742
4743 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4744 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4745
4746 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4747}
4748
4749__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4750{
4751 struct hlua_txn *htxn;
4752
4753 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4754 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4755
4756 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4757}
4758
4759__LJMP static int hlua_http_req_rep_val(lua_State *L)
4760{
4761 struct hlua_txn *htxn;
4762
4763 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4764 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4765
4766 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4767}
4768
4769__LJMP static int hlua_http_res_rep_val(lua_State *L)
4770{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004771 struct hlua_txn *htxn;
4772
4773 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4774 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4775
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004776 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004777}
4778
4779/* This function deletes all the occurences of an header.
4780 * It is a wrapper for the 2 following functions.
4781 */
4782__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4783{
4784 size_t len;
4785 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4786 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004787 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004788
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004789 /* Check if a valid response is parsed */
4790 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4791 return 0;
4792
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004793 ctx.idx = 0;
4794 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
4795 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4796 return 0;
4797}
4798
4799__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4800{
4801 struct hlua_txn *htxn;
4802
4803 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4804 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4805
Willy Tarreaueee5b512015-04-03 23:46:31 +02004806 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004807}
4808
4809__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4810{
4811 struct hlua_txn *htxn;
4812
4813 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4814 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4815
Willy Tarreaueee5b512015-04-03 23:46:31 +02004816 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004817}
4818
4819/* This function adds an header. It is a wrapper used by
4820 * the 2 following functions.
4821 */
4822__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4823{
4824 size_t name_len;
4825 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4826 size_t value_len;
4827 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
4828 char *p;
4829
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004830 /* Check if a valid message is parsed */
4831 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4832 return 0;
4833
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004834 /* Check length. */
4835 trash.len = value_len + name_len + 2;
4836 if (trash.len > trash.size)
4837 return 0;
4838
4839 /* Creates the header string. */
4840 p = trash.str;
4841 memcpy(p, name, name_len);
4842 p += name_len;
4843 *p = ':';
4844 p++;
4845 *p = ' ';
4846 p++;
4847 memcpy(p, value, value_len);
4848
Willy Tarreaueee5b512015-04-03 23:46:31 +02004849 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004850 trash.str, trash.len) != 0);
4851
4852 return 0;
4853}
4854
4855__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4856{
4857 struct hlua_txn *htxn;
4858
4859 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4860 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4861
Willy Tarreaueee5b512015-04-03 23:46:31 +02004862 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004863}
4864
4865__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4866{
4867 struct hlua_txn *htxn;
4868
4869 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4870 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4871
Willy Tarreaueee5b512015-04-03 23:46:31 +02004872 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004873}
4874
4875static int hlua_http_req_set_hdr(lua_State *L)
4876{
4877 struct hlua_txn *htxn;
4878
4879 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4880 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4881
Willy Tarreaueee5b512015-04-03 23:46:31 +02004882 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4883 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004884}
4885
4886static int hlua_http_res_set_hdr(lua_State *L)
4887{
4888 struct hlua_txn *htxn;
4889
4890 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4891 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4892
Willy Tarreaueee5b512015-04-03 23:46:31 +02004893 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4894 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004895}
4896
4897/* This function set the method. */
4898static int hlua_http_req_set_meth(lua_State *L)
4899{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004900 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004901 size_t name_len;
4902 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004903
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004904 /* Check if a valid request is parsed */
4905 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4906 lua_pushboolean(L, 0);
4907 return 1;
4908 }
4909
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004910 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004911 return 1;
4912}
4913
4914/* This function set the method. */
4915static int hlua_http_req_set_path(lua_State *L)
4916{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004917 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004918 size_t name_len;
4919 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004920
4921 /* Check if a valid request is parsed */
4922 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4923 lua_pushboolean(L, 0);
4924 return 1;
4925 }
4926
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004927 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004928 return 1;
4929}
4930
4931/* This function set the query-string. */
4932static int hlua_http_req_set_query(lua_State *L)
4933{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004934 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004935 size_t name_len;
4936 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004937
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004938 /* Check if a valid request is parsed */
4939 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4940 lua_pushboolean(L, 0);
4941 return 1;
4942 }
4943
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004944 /* Check length. */
4945 if (name_len > trash.size - 1) {
4946 lua_pushboolean(L, 0);
4947 return 1;
4948 }
4949
4950 /* Add the mark question as prefix. */
4951 chunk_reset(&trash);
4952 trash.str[trash.len++] = '?';
4953 memcpy(trash.str + trash.len, name, name_len);
4954 trash.len += name_len;
4955
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004956 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004957 return 1;
4958}
4959
4960/* This function set the uri. */
4961static int hlua_http_req_set_uri(lua_State *L)
4962{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004963 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004964 size_t name_len;
4965 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004966
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004967 /* Check if a valid request is parsed */
4968 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4969 lua_pushboolean(L, 0);
4970 return 1;
4971 }
4972
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004973 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004974 return 1;
4975}
4976
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004977/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004978static int hlua_http_res_set_status(lua_State *L)
4979{
4980 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4981 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004982 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004983
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004984 /* Check if a valid response is parsed */
4985 if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY))
4986 return 0;
4987
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004988 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004989 return 0;
4990}
4991
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004992/*
4993 *
4994 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004995 * Class TXN
4996 *
4997 *
4998 */
4999
5000/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005001 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005002 */
5003__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5004{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005005 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005006}
5007
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005008__LJMP static int hlua_set_var(lua_State *L)
5009{
5010 struct hlua_txn *htxn;
5011 const char *name;
5012 size_t len;
5013 struct sample smp;
5014
5015 MAY_LJMP(check_args(L, 3, "set_var"));
5016
5017 /* It is useles to retrieve the stream, but this function
5018 * runs only in a stream context.
5019 */
5020 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5021 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5022
5023 /* Converts the third argument in a sample. */
5024 hlua_lua2smp(L, 3, &smp);
5025
5026 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005027 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005028 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005029 return 0;
5030}
5031
Christopher Faulet85d79c92016-11-09 16:54:56 +01005032__LJMP static int hlua_unset_var(lua_State *L)
5033{
5034 struct hlua_txn *htxn;
5035 const char *name;
5036 size_t len;
5037 struct sample smp;
5038
5039 MAY_LJMP(check_args(L, 2, "unset_var"));
5040
5041 /* It is useles to retrieve the stream, but this function
5042 * runs only in a stream context.
5043 */
5044 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5045 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5046
5047 /* Unset the variable. */
5048 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5049 vars_unset_by_name(name, len, &smp);
5050 return 0;
5051}
5052
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005053__LJMP static int hlua_get_var(lua_State *L)
5054{
5055 struct hlua_txn *htxn;
5056 const char *name;
5057 size_t len;
5058 struct sample smp;
5059
5060 MAY_LJMP(check_args(L, 2, "get_var"));
5061
5062 /* It is useles to retrieve the stream, but this function
5063 * runs only in a stream context.
5064 */
5065 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5066 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5067
Willy Tarreau7560dd42016-03-10 16:28:58 +01005068 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005069 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005070 lua_pushnil(L);
5071 return 1;
5072 }
5073
5074 return hlua_smp2lua(L, &smp);
5075}
5076
Willy Tarreau59551662015-03-10 14:23:13 +01005077__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005078{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005079 struct hlua *hlua;
5080
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005081 MAY_LJMP(check_args(L, 2, "set_priv"));
5082
Willy Tarreau87b09662015-04-03 00:22:06 +02005083 /* It is useles to retrieve the stream, but this function
5084 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005085 */
5086 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005087 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005088
5089 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005090 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005091
5092 /* Get and store new value. */
5093 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5094 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5095
5096 return 0;
5097}
5098
Willy Tarreau59551662015-03-10 14:23:13 +01005099__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005100{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005101 struct hlua *hlua;
5102
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005103 MAY_LJMP(check_args(L, 1, "get_priv"));
5104
Willy Tarreau87b09662015-04-03 00:22:06 +02005105 /* It is useles to retrieve the stream, but this function
5106 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005107 */
5108 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005109 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005110
5111 /* Push configuration index in the stack. */
5112 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5113
5114 return 1;
5115}
5116
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005117/* Create stack entry containing a class TXN. This function
5118 * return 0 if the stack does not contains free slots,
5119 * otherwise it returns 1.
5120 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005121static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005122{
Willy Tarreaude491382015-04-06 11:04:28 +02005123 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005124
5125 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005126 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005127 return 0;
5128
5129 /* NOTE: The allocation never fails. The failure
5130 * throw an error, and the function never returns.
5131 * if the throw is not avalaible, the process is aborted.
5132 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005133 /* Create the object: obj[0] = userdata. */
5134 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005135 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005136 lua_rawseti(L, -2, 0);
5137
Willy Tarreaude491382015-04-06 11:04:28 +02005138 htxn->s = s;
5139 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005140 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005141 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005142
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005143 /* Create the "f" field that contains a list of fetches. */
5144 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005145 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005146 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005147 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005148
5149 /* Create the "sf" field that contains a list of stringsafe fetches. */
5150 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005151 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005152 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005153 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005154
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005155 /* Create the "c" field that contains a list of converters. */
5156 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005157 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005158 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005159 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005160
5161 /* Create the "sc" field that contains a list of stringsafe converters. */
5162 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005163 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005164 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005165 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005166
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005167 /* Create the "req" field that contains the request channel object. */
5168 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005169 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005170 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005171 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005172
5173 /* Create the "res" field that contains the response channel object. */
5174 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005175 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005176 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005177 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005178
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005179 /* Creates the HTTP object is the current proxy allows http. */
5180 lua_pushstring(L, "http");
5181 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005182 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005183 return 0;
5184 }
5185 else
5186 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005187 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005188
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005189 /* Pop a class sesison metatable and affect it to the userdata. */
5190 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5191 lua_setmetatable(L, -2);
5192
5193 return 1;
5194}
5195
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005196__LJMP static int hlua_txn_deflog(lua_State *L)
5197{
5198 const char *msg;
5199 struct hlua_txn *htxn;
5200
5201 MAY_LJMP(check_args(L, 2, "deflog"));
5202 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5203 msg = MAY_LJMP(luaL_checkstring(L, 2));
5204
5205 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5206 return 0;
5207}
5208
5209__LJMP static int hlua_txn_log(lua_State *L)
5210{
5211 int level;
5212 const char *msg;
5213 struct hlua_txn *htxn;
5214
5215 MAY_LJMP(check_args(L, 3, "log"));
5216 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5217 level = MAY_LJMP(luaL_checkinteger(L, 2));
5218 msg = MAY_LJMP(luaL_checkstring(L, 3));
5219
5220 if (level < 0 || level >= NB_LOG_LEVELS)
5221 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5222
5223 hlua_sendlog(htxn->s->be, level, msg);
5224 return 0;
5225}
5226
5227__LJMP static int hlua_txn_log_debug(lua_State *L)
5228{
5229 const char *msg;
5230 struct hlua_txn *htxn;
5231
5232 MAY_LJMP(check_args(L, 2, "Debug"));
5233 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5234 msg = MAY_LJMP(luaL_checkstring(L, 2));
5235 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5236 return 0;
5237}
5238
5239__LJMP static int hlua_txn_log_info(lua_State *L)
5240{
5241 const char *msg;
5242 struct hlua_txn *htxn;
5243
5244 MAY_LJMP(check_args(L, 2, "Info"));
5245 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5246 msg = MAY_LJMP(luaL_checkstring(L, 2));
5247 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5248 return 0;
5249}
5250
5251__LJMP static int hlua_txn_log_warning(lua_State *L)
5252{
5253 const char *msg;
5254 struct hlua_txn *htxn;
5255
5256 MAY_LJMP(check_args(L, 2, "Warning"));
5257 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5258 msg = MAY_LJMP(luaL_checkstring(L, 2));
5259 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5260 return 0;
5261}
5262
5263__LJMP static int hlua_txn_log_alert(lua_State *L)
5264{
5265 const char *msg;
5266 struct hlua_txn *htxn;
5267
5268 MAY_LJMP(check_args(L, 2, "Alert"));
5269 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5270 msg = MAY_LJMP(luaL_checkstring(L, 2));
5271 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5272 return 0;
5273}
5274
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005275__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5276{
5277 struct hlua_txn *htxn;
5278 int ll;
5279
5280 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5281 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5282 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5283
5284 if (ll < 0 || ll > 7)
5285 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5286
5287 htxn->s->logs.level = ll;
5288 return 0;
5289}
5290
5291__LJMP static int hlua_txn_set_tos(lua_State *L)
5292{
5293 struct hlua_txn *htxn;
5294 struct connection *cli_conn;
5295 int tos;
5296
5297 MAY_LJMP(check_args(L, 2, "set_tos"));
5298 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5299 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5300
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005301 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005302 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005303
5304 return 0;
5305}
5306
5307__LJMP static int hlua_txn_set_mark(lua_State *L)
5308{
5309#ifdef SO_MARK
5310 struct hlua_txn *htxn;
5311 struct connection *cli_conn;
5312 int mark;
5313
5314 MAY_LJMP(check_args(L, 2, "set_mark"));
5315 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5316 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5317
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005318 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005319 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005320#endif
5321 return 0;
5322}
5323
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005324/* This function is an Lua binding that send pending data
5325 * to the client, and close the stream interface.
5326 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005327__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005328{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005329 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005330 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005331 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005332
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005333 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005334 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005335 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005336
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005337 /* If the flags NOTERM is set, we cannot terminate the http
5338 * session, so we just end the execution of the current
5339 * lua code.
5340 */
5341 if (htxn->flags & HLUA_TXN_NOTERM) {
5342 WILL_LJMP(hlua_done(L));
5343 return 0;
5344 }
5345
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005346 ic = &htxn->s->req;
5347 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005348
Willy Tarreau630ef452015-08-28 10:06:15 +02005349 if (htxn->s->txn) {
5350 /* HTTP mode, let's stay in sync with the stream */
5351 bi_fast_delete(ic->buf, htxn->s->txn->req.sov);
5352 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
5353 htxn->s->txn->req.sov = 0;
5354 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
5355 oc->analysers = AN_RES_HTTP_XFER_BODY;
5356 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
5357 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
5358
Willy Tarreau630ef452015-08-28 10:06:15 +02005359 /* Note that if we want to support keep-alive, we need
5360 * to bypass the close/shutr_now calls below, but that
5361 * may only be done if the HTTP request was already
5362 * processed and the connection header is known (ie
5363 * not during TCP rules).
5364 */
5365 }
5366
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005367 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01005368 channel_abort(ic);
5369 channel_auto_close(ic);
5370 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005371
5372 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01005373 channel_auto_read(oc);
5374 channel_auto_close(oc);
5375 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005376
Willy Tarreau0458b082015-08-28 09:40:04 +02005377 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005378
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005379 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005380 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005381 return 0;
5382}
5383
5384__LJMP static int hlua_log(lua_State *L)
5385{
5386 int level;
5387 const char *msg;
5388
5389 MAY_LJMP(check_args(L, 2, "log"));
5390 level = MAY_LJMP(luaL_checkinteger(L, 1));
5391 msg = MAY_LJMP(luaL_checkstring(L, 2));
5392
5393 if (level < 0 || level >= NB_LOG_LEVELS)
5394 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5395
5396 hlua_sendlog(NULL, level, msg);
5397 return 0;
5398}
5399
5400__LJMP static int hlua_log_debug(lua_State *L)
5401{
5402 const char *msg;
5403
5404 MAY_LJMP(check_args(L, 1, "debug"));
5405 msg = MAY_LJMP(luaL_checkstring(L, 1));
5406 hlua_sendlog(NULL, LOG_DEBUG, msg);
5407 return 0;
5408}
5409
5410__LJMP static int hlua_log_info(lua_State *L)
5411{
5412 const char *msg;
5413
5414 MAY_LJMP(check_args(L, 1, "info"));
5415 msg = MAY_LJMP(luaL_checkstring(L, 1));
5416 hlua_sendlog(NULL, LOG_INFO, msg);
5417 return 0;
5418}
5419
5420__LJMP static int hlua_log_warning(lua_State *L)
5421{
5422 const char *msg;
5423
5424 MAY_LJMP(check_args(L, 1, "warning"));
5425 msg = MAY_LJMP(luaL_checkstring(L, 1));
5426 hlua_sendlog(NULL, LOG_WARNING, msg);
5427 return 0;
5428}
5429
5430__LJMP static int hlua_log_alert(lua_State *L)
5431{
5432 const char *msg;
5433
5434 MAY_LJMP(check_args(L, 1, "alert"));
5435 msg = MAY_LJMP(luaL_checkstring(L, 1));
5436 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005437 return 0;
5438}
5439
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005440__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005441{
5442 int wakeup_ms = lua_tointeger(L, -1);
5443 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005444 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005445 return 0;
5446}
5447
5448__LJMP static int hlua_sleep(lua_State *L)
5449{
5450 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005451 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005452
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005453 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005454
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005455 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005456 wakeup_ms = tick_add(now_ms, delay);
5457 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005458
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005459 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5460 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005461}
5462
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005463__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005464{
5465 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005466 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005467
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005468 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005469
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005470 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005471 wakeup_ms = tick_add(now_ms, delay);
5472 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005473
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005474 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5475 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005476}
5477
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005478/* This functionis an LUA binding. it permits to give back
5479 * the hand at the HAProxy scheduler. It is used when the
5480 * LUA processing consumes a lot of time.
5481 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005482__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005483{
5484 return 0;
5485}
5486
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005487__LJMP static int hlua_yield(lua_State *L)
5488{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005489 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
5490 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005491}
5492
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005493/* This function change the nice of the currently executed
5494 * task. It is used set low or high priority at the current
5495 * task.
5496 */
Willy Tarreau59551662015-03-10 14:23:13 +01005497__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005498{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005499 struct hlua *hlua;
5500 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005501
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005502 MAY_LJMP(check_args(L, 1, "set_nice"));
5503 hlua = hlua_gethlua(L);
5504 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005505
5506 /* If he task is not set, I'm in a start mode. */
5507 if (!hlua || !hlua->task)
5508 return 0;
5509
5510 if (nice < -1024)
5511 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005512 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005513 nice = 1024;
5514
5515 hlua->task->nice = nice;
5516 return 0;
5517}
5518
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005519/* This function is used as a calback of a task. It is called by the
5520 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5521 * return an E_AGAIN signal, the emmiter of this signal must set a
5522 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005523 *
5524 * Task wrapper are longjmp safe because the only one Lua code
5525 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005526 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02005527static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005528{
Olivier Houchard9f6af332018-05-25 14:04:04 +02005529 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005530 enum hlua_exec status;
5531
Christopher Faulet5bc99722018-04-25 10:34:45 +02005532 if (task->thread_mask == MAX_THREADS_MASK)
5533 task_set_affinity(task, tid_bit);
5534
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005535 /* If it is the first call to the task, we must initialize the
5536 * execution timeouts.
5537 */
5538 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005539 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005540
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005541 /* Execute the Lua code. */
5542 status = hlua_ctx_resume(hlua, 1);
5543
5544 switch (status) {
5545 /* finished or yield */
5546 case HLUA_E_OK:
5547 hlua_ctx_destroy(hlua);
5548 task_delete(task);
5549 task_free(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02005550 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005551 break;
5552
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005553 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01005554 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02005555 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005556 break;
5557
5558 /* finished with error. */
5559 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005560 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005561 hlua_ctx_destroy(hlua);
5562 task_delete(task);
5563 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005564 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005565 break;
5566
5567 case HLUA_E_ERR:
5568 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005569 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005570 hlua_ctx_destroy(hlua);
5571 task_delete(task);
5572 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005573 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005574 break;
5575 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005576 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005577}
5578
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005579/* This function is an LUA binding that register LUA function to be
5580 * executed after the HAProxy configuration parsing and before the
5581 * HAProxy scheduler starts. This function expect only one LUA
5582 * argument that is a function. This function returns nothing, but
5583 * throws if an error is encountered.
5584 */
5585__LJMP static int hlua_register_init(lua_State *L)
5586{
5587 struct hlua_init_function *init;
5588 int ref;
5589
5590 MAY_LJMP(check_args(L, 1, "register_init"));
5591
5592 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5593
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005594 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005595 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005596 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005597
5598 init->function_ref = ref;
5599 LIST_ADDQ(&hlua_init_functions, &init->l);
5600 return 0;
5601}
5602
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005603/* This functio is an LUA binding. It permits to register a task
5604 * executed in parallel of the main HAroxy activity. The task is
5605 * created and it is set in the HAProxy scheduler. It can be called
5606 * from the "init" section, "post init" or during the runtime.
5607 *
5608 * Lua prototype:
5609 *
5610 * <none> core.register_task(<function>)
5611 */
5612static int hlua_register_task(lua_State *L)
5613{
5614 struct hlua *hlua;
5615 struct task *task;
5616 int ref;
5617
5618 MAY_LJMP(check_args(L, 1, "register_task"));
5619
5620 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5621
Willy Tarreaubafbe012017-11-24 17:34:44 +01005622 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005623 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005624 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005625
Emeric Brunc60def82017-09-27 14:59:38 +02005626 task = task_new(MAX_THREADS_MASK);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005627 task->context = hlua;
5628 task->process = hlua_process_task;
5629
5630 if (!hlua_ctx_init(hlua, task))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005631 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005632
5633 /* Restore the function in the stack. */
5634 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5635 hlua->nargs = 0;
5636
5637 /* Schedule task. */
5638 task_schedule(task, now_ms);
5639
5640 return 0;
5641}
5642
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005643/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5644 * doesn't allow "yield" functions because the HAProxy engine cannot
5645 * resume converters.
5646 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005647static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005648{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005649 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005650 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005651 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005652
Willy Tarreaube508f12016-03-10 11:47:01 +01005653 if (!stream)
5654 return 0;
5655
Willy Tarreau87b09662015-04-03 00:22:06 +02005656 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005657 * Lua context can be not initialized. This behavior
5658 * permits to save performances because a systematic
5659 * Lua initialization cause 5% performances loss.
5660 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005661 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005662 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005663 if (!stream->hlua) {
5664 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5665 return 0;
5666 }
5667 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5668 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5669 return 0;
5670 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005671 }
5672
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005673 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005674 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005675
5676 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005677 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5678 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5679 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005680 else
5681 error = "critical error";
5682 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005683 return 0;
5684 }
5685
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005686 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005687 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005688 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005689 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005690 return 0;
5691 }
5692
5693 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005694 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005695
5696 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005697 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005698 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005699 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005700 return 0;
5701 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005702 hlua_smp2lua(stream->hlua->T, smp);
5703 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005704
5705 /* push keywords in the stack. */
5706 if (arg_p) {
5707 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005708 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005709 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005710 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005711 return 0;
5712 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005713 hlua_arg2lua(stream->hlua->T, arg_p);
5714 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005715 }
5716 }
5717
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005718 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005719 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005720
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005721 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005722 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005723 }
5724
5725 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005726 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005727 /* finished. */
5728 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005729 /* If the stack is empty, the function fails. */
5730 if (lua_gettop(stream->hlua->T) <= 0)
5731 return 0;
5732
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005733 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005734 hlua_lua2smp(stream->hlua->T, -1, smp);
5735 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005736 return 1;
5737
5738 /* yield. */
5739 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005740 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005741 return 0;
5742
5743 /* finished with error. */
5744 case HLUA_E_ERRMSG:
5745 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005746 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005747 fcn->name, lua_tostring(stream->hlua->T, -1));
5748 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005749 return 0;
5750
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005751 case HLUA_E_ETMOUT:
5752 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
5753 return 0;
5754
5755 case HLUA_E_NOMEM:
5756 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
5757 return 0;
5758
5759 case HLUA_E_YIELD:
5760 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
5761 return 0;
5762
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005763 case HLUA_E_ERR:
5764 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005765 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005766
5767 default:
5768 return 0;
5769 }
5770}
5771
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005772/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5773 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005774 * resume sample-fetches. This function will be called by the sample
5775 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005776 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005777static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5778 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005779{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005780 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005781 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005782 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005783 const struct chunk msg = { .len = 0 };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005784
Willy Tarreaube508f12016-03-10 11:47:01 +01005785 if (!stream)
5786 return 0;
5787
Willy Tarreau87b09662015-04-03 00:22:06 +02005788 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005789 * Lua context can be not initialized. This behavior
5790 * permits to save performances because a systematic
5791 * Lua initialization cause 5% performances loss.
5792 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005793 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005794 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005795 if (!stream->hlua) {
5796 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5797 return 0;
5798 }
5799 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5800 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5801 return 0;
5802 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005803 }
5804
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005805 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005806
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005807 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005808 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005809
5810 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005811 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5812 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5813 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005814 else
5815 error = "critical error";
5816 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005817 return 0;
5818 }
5819
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005820 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005821 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005822 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005823 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005824 return 0;
5825 }
5826
5827 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005828 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005829
5830 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005831 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005832 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005833 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005834 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005835 return 0;
5836 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005837 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005838
5839 /* push keywords in the stack. */
5840 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5841 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005842 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005843 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005844 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005845 return 0;
5846 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005847 hlua_arg2lua(stream->hlua->T, arg_p);
5848 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005849 }
5850
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005851 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005852 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005853
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005854 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005855 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005856 }
5857
5858 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005859 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005860 /* finished. */
5861 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005862 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005863 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005864 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005865 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005866 /* If the stack is empty, the function fails. */
5867 if (lua_gettop(stream->hlua->T) <= 0)
5868 return 0;
5869
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005870 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005871 hlua_lua2smp(stream->hlua->T, -1, smp);
5872 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005873
5874 /* Set the end of execution flag. */
5875 smp->flags &= ~SMP_F_MAY_CHANGE;
5876 return 1;
5877
5878 /* yield. */
5879 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005880 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005881 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005882 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005883 return 0;
5884
5885 /* finished with error. */
5886 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005887 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005888 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005889 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005890 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005891 fcn->name, lua_tostring(stream->hlua->T, -1));
5892 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005893 return 0;
5894
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005895 case HLUA_E_ETMOUT:
5896 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
5897 stream_int_retnclose(&stream->si[0], &msg);
5898 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
5899 return 0;
5900
5901 case HLUA_E_NOMEM:
5902 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
5903 stream_int_retnclose(&stream->si[0], &msg);
5904 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
5905 return 0;
5906
5907 case HLUA_E_YIELD:
5908 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
5909 stream_int_retnclose(&stream->si[0], &msg);
5910 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
5911 return 0;
5912
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005913 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005914 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005915 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005916 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005917 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005918
5919 default:
5920 return 0;
5921 }
5922}
5923
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005924/* This function is an LUA binding used for registering
5925 * "sample-conv" functions. It expects a converter name used
5926 * in the haproxy configuration file, and an LUA function.
5927 */
5928__LJMP static int hlua_register_converters(lua_State *L)
5929{
5930 struct sample_conv_kw_list *sck;
5931 const char *name;
5932 int ref;
5933 int len;
5934 struct hlua_function *fcn;
5935
5936 MAY_LJMP(check_args(L, 2, "register_converters"));
5937
5938 /* First argument : converter name. */
5939 name = MAY_LJMP(luaL_checkstring(L, 1));
5940
5941 /* Second argument : lua function. */
5942 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5943
5944 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005945 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005946 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005947 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005948 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005949 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005950 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005951
5952 /* Fill fcn. */
5953 fcn->name = strdup(name);
5954 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005955 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005956 fcn->function_ref = ref;
5957
5958 /* List head */
5959 sck->list.n = sck->list.p = NULL;
5960
5961 /* converter keyword. */
5962 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005963 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005964 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005965 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005966
5967 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
5968 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01005969 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 +01005970 sck->kw[0].val_args = NULL;
5971 sck->kw[0].in_type = SMP_T_STR;
5972 sck->kw[0].out_type = SMP_T_STR;
5973 sck->kw[0].private = fcn;
5974
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005975 /* Register this new converter */
5976 sample_register_convs(sck);
5977
5978 return 0;
5979}
5980
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005981/* This fucntion is an LUA binding used for registering
5982 * "sample-fetch" functions. It expects a converter name used
5983 * in the haproxy configuration file, and an LUA function.
5984 */
5985__LJMP static int hlua_register_fetches(lua_State *L)
5986{
5987 const char *name;
5988 int ref;
5989 int len;
5990 struct sample_fetch_kw_list *sfk;
5991 struct hlua_function *fcn;
5992
5993 MAY_LJMP(check_args(L, 2, "register_fetches"));
5994
5995 /* First argument : sample-fetch name. */
5996 name = MAY_LJMP(luaL_checkstring(L, 1));
5997
5998 /* Second argument : lua function. */
5999 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6000
6001 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006002 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006003 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006004 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006005 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006006 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006007 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006008
6009 /* Fill fcn. */
6010 fcn->name = strdup(name);
6011 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006012 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006013 fcn->function_ref = ref;
6014
6015 /* List head */
6016 sfk->list.n = sfk->list.p = NULL;
6017
6018 /* sample-fetch keyword. */
6019 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006020 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006021 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006022 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006023
6024 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6025 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006026 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 +01006027 sfk->kw[0].val_args = NULL;
6028 sfk->kw[0].out_type = SMP_T_STR;
6029 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6030 sfk->kw[0].val = 0;
6031 sfk->kw[0].private = fcn;
6032
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006033 /* Register this new fetch. */
6034 sample_register_fetches(sfk);
6035
6036 return 0;
6037}
6038
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006039/* This function is a wrapper to execute each LUA function declared
6040 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006041 * return ACT_RET_CONT if the processing is finished (with or without
6042 * error) and return ACT_RET_YIELD if the function must be called again
6043 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006044 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006045static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006046 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006047{
6048 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006049 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006050 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006051 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006052 const struct chunk msg = { .len = 0 };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006053
6054 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01006055 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
6056 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
6057 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
6058 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006059 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006060 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006061 return ACT_RET_CONT;
6062 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006063
Willy Tarreau87b09662015-04-03 00:22:06 +02006064 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006065 * Lua context can be not initialized. This behavior
6066 * permits to save performances because a systematic
6067 * Lua initialization cause 5% performances loss.
6068 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006069 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006070 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006071 if (!s->hlua) {
6072 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6073 rule->arg.hlua_rule->fcn.name);
6074 return ACT_RET_CONT;
6075 }
6076 if (!hlua_ctx_init(s->hlua, s->task)) {
6077 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6078 rule->arg.hlua_rule->fcn.name);
6079 return ACT_RET_CONT;
6080 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006081 }
6082
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006083 consistency_set(s, dir, &s->hlua->cons);
6084
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006085 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006086 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006087
6088 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006089 if (!SET_SAFE_LJMP(s->hlua->T)) {
6090 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6091 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006092 else
6093 error = "critical error";
6094 SEND_ERR(px, "Lua function '%s': %s.\n",
6095 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006096 return ACT_RET_CONT;
6097 }
6098
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006099 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006100 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006101 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006102 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006103 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006104 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006105 }
6106
6107 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006108 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006109
Willy Tarreau87b09662015-04-03 00:22:06 +02006110 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006111 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006112 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006113 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006114 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006115 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006116 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006117 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006118
6119 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006120 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006121 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006122 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006123 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006124 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006125 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006126 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006127 lua_pushstring(s->hlua->T, *arg);
6128 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006129 }
6130
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006131 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006132 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006133
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006134 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006135 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006136 }
6137
6138 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006139 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006140 /* finished. */
6141 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006142 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006143 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006144 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006145 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006146 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006147 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006148 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006149
6150 /* yield. */
6151 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006152 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006153 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006154 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006155 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006156 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006157 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006158 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006159 /* Some actions can be wake up when a "write" event
6160 * is detected on a response channel. This is useful
6161 * only for actions targetted on the requests.
6162 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006163 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006164 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006165 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006166 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006167 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006168 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006169 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006170 /* We can quit the fcuntion without consistency check
6171 * because HAProxy is not able to manipulate data, it
6172 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006173 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006174
6175 /* finished with error. */
6176 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006177 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006178 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006179 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006180 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006181 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006182 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006183 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6184 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006185 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006186
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006187 case HLUA_E_ETMOUT:
6188 if (!consistency_check(s, dir, &s->hlua->cons)) {
6189 stream_int_retnclose(&s->si[0], &msg);
6190 return ACT_RET_ERR;
6191 }
6192 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6193 return 0;
6194
6195 case HLUA_E_NOMEM:
6196 if (!consistency_check(s, dir, &s->hlua->cons)) {
6197 stream_int_retnclose(&s->si[0], &msg);
6198 return ACT_RET_ERR;
6199 }
6200 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6201 return 0;
6202
6203 case HLUA_E_YIELD:
6204 if (!consistency_check(s, dir, &s->hlua->cons)) {
6205 stream_int_retnclose(&s->si[0], &msg);
6206 return ACT_RET_ERR;
6207 }
6208 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6209 rule->arg.hlua_rule->fcn.name);
6210 return 0;
6211
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006212 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006213 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006214 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006215 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006216 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006217 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006218 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006219 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006220
6221 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006222 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006223 }
6224}
6225
Olivier Houchard9f6af332018-05-25 14:04:04 +02006226struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006227{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006228 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006229 struct stream_interface *si = ctx->owner;
6230
6231 /* If the applet is wake up without any expected work, the sheduler
6232 * remove it from the run queue. This flag indicate that the applet
6233 * is waiting for write. If the buffer is full, the main processing
6234 * will send some data and after call the applet, otherwise it call
6235 * the applet ASAP.
6236 */
6237 si_applet_cant_put(si);
6238 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006239 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006240 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006241}
6242
6243static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6244{
6245 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006246 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006247 struct task *task;
6248 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006249 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006250
Willy Tarreaubafbe012017-11-24 17:34:44 +01006251 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006252 if (!hlua) {
6253 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6254 ctx->rule->arg.hlua_rule->fcn.name);
6255 return 0;
6256 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006257 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006258 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006259 ctx->ctx.hlua_apptcp.flags = 0;
6260
6261 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006262 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006263 if (!task) {
6264 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6265 ctx->rule->arg.hlua_rule->fcn.name);
6266 return 0;
6267 }
6268 task->nice = 0;
6269 task->context = ctx;
6270 task->process = hlua_applet_wakeup;
6271 ctx->ctx.hlua_apptcp.task = task;
6272
6273 /* In the execution wrappers linked with a stream, the
6274 * Lua context can be not initialized. This behavior
6275 * permits to save performances because a systematic
6276 * Lua initialization cause 5% performances loss.
6277 */
6278 if (!hlua_ctx_init(hlua, task)) {
6279 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6280 ctx->rule->arg.hlua_rule->fcn.name);
6281 return 0;
6282 }
6283
6284 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006285 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006286
6287 /* The following Lua calls can fail. */
6288 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006289 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6290 error = lua_tostring(hlua->T, -1);
6291 else
6292 error = "critical error";
6293 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6294 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006295 RESET_SAFE_LJMP(hlua->T);
6296 return 0;
6297 }
6298
6299 /* Check stack available size. */
6300 if (!lua_checkstack(hlua->T, 1)) {
6301 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6302 ctx->rule->arg.hlua_rule->fcn.name);
6303 RESET_SAFE_LJMP(hlua->T);
6304 return 0;
6305 }
6306
6307 /* Restore the function in the stack. */
6308 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6309
6310 /* Create and and push object stream in the stack. */
6311 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6312 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6313 ctx->rule->arg.hlua_rule->fcn.name);
6314 RESET_SAFE_LJMP(hlua->T);
6315 return 0;
6316 }
6317 hlua->nargs = 1;
6318
6319 /* push keywords in the stack. */
6320 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6321 if (!lua_checkstack(hlua->T, 1)) {
6322 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6323 ctx->rule->arg.hlua_rule->fcn.name);
6324 RESET_SAFE_LJMP(hlua->T);
6325 return 0;
6326 }
6327 lua_pushstring(hlua->T, *arg);
6328 hlua->nargs++;
6329 }
6330
6331 RESET_SAFE_LJMP(hlua->T);
6332
6333 /* Wakeup the applet ASAP. */
6334 si_applet_cant_get(si);
6335 si_applet_cant_put(si);
6336
6337 return 1;
6338}
6339
6340static void hlua_applet_tcp_fct(struct appctx *ctx)
6341{
6342 struct stream_interface *si = ctx->owner;
6343 struct stream *strm = si_strm(si);
6344 struct channel *res = si_ic(si);
6345 struct act_rule *rule = ctx->rule;
6346 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006347 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006348
6349 /* The applet execution is already done. */
6350 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE)
6351 return;
6352
6353 /* If the stream is disconnect or closed, ldo nothing. */
6354 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6355 return;
6356
6357 /* Execute the function. */
6358 switch (hlua_ctx_resume(hlua, 1)) {
6359 /* finished. */
6360 case HLUA_E_OK:
6361 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6362
6363 /* log time */
6364 strm->logs.tv_request = now;
6365
6366 /* eat the whole request */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006367 co_skip(si_oc(si), si_ob(si)->o);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006368 res->flags |= CF_READ_NULL;
6369 si_shutr(si);
6370 return;
6371
6372 /* yield. */
6373 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006374 if (hlua->wake_time != TICK_ETERNITY)
6375 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006376 return;
6377
6378 /* finished with error. */
6379 case HLUA_E_ERRMSG:
6380 /* Display log. */
6381 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6382 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6383 lua_pop(hlua->T, 1);
6384 goto error;
6385
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006386 case HLUA_E_ETMOUT:
6387 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6388 rule->arg.hlua_rule->fcn.name);
6389 goto error;
6390
6391 case HLUA_E_NOMEM:
6392 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6393 rule->arg.hlua_rule->fcn.name);
6394 goto error;
6395
6396 case HLUA_E_YIELD: /* unexpected */
6397 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6398 rule->arg.hlua_rule->fcn.name);
6399 goto error;
6400
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006401 case HLUA_E_ERR:
6402 /* Display log. */
6403 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6404 rule->arg.hlua_rule->fcn.name);
6405 goto error;
6406
6407 default:
6408 goto error;
6409 }
6410
6411error:
6412
6413 /* For all other cases, just close the stream. */
6414 si_shutw(si);
6415 si_shutr(si);
6416 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6417}
6418
6419static void hlua_applet_tcp_release(struct appctx *ctx)
6420{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006421 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006422 task_free(ctx->ctx.hlua_apptcp.task);
6423 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006424 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006425 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006426}
6427
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006428/* The function returns 1 if the initialisation is complete, 0 if
6429 * an errors occurs and -1 if more data are required for initializing
6430 * the applet.
6431 */
6432static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6433{
6434 struct stream_interface *si = ctx->owner;
6435 struct channel *req = si_oc(si);
6436 struct http_msg *msg;
6437 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006438 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006439 char **arg;
6440 struct hdr_ctx hdr;
6441 struct task *task;
6442 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006443 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006444
6445 /* Wait for a full HTTP request. */
6446 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6447 if (smp.flags & SMP_F_MAY_CHANGE)
6448 return -1;
6449 return 0;
6450 }
6451 txn = strm->txn;
6452 msg = &txn->req;
6453
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006454 /* We want two things in HTTP mode :
6455 * - enforce server-close mode if we were in keep-alive, so that the
6456 * applet is released after each response ;
6457 * - enable request body transfer to the applet in order to resync
6458 * with the response body.
6459 */
6460 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6461 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006462
Willy Tarreaubafbe012017-11-24 17:34:44 +01006463 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006464 if (!hlua) {
6465 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6466 ctx->rule->arg.hlua_rule->fcn.name);
6467 return 0;
6468 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006469 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006470 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006471 ctx->ctx.hlua_apphttp.left_bytes = -1;
6472 ctx->ctx.hlua_apphttp.flags = 0;
6473
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006474 if (txn->req.flags & HTTP_MSGF_VER_11)
6475 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6476
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006477 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006478 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006479 if (!task) {
6480 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6481 ctx->rule->arg.hlua_rule->fcn.name);
6482 return 0;
6483 }
6484 task->nice = 0;
6485 task->context = ctx;
6486 task->process = hlua_applet_wakeup;
6487 ctx->ctx.hlua_apphttp.task = task;
6488
6489 /* In the execution wrappers linked with a stream, the
6490 * Lua context can be not initialized. This behavior
6491 * permits to save performances because a systematic
6492 * Lua initialization cause 5% performances loss.
6493 */
6494 if (!hlua_ctx_init(hlua, task)) {
6495 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6496 ctx->rule->arg.hlua_rule->fcn.name);
6497 return 0;
6498 }
6499
6500 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006501 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006502
6503 /* The following Lua calls can fail. */
6504 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006505 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6506 error = lua_tostring(hlua->T, -1);
6507 else
6508 error = "critical error";
6509 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6510 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006511 return 0;
6512 }
6513
6514 /* Check stack available size. */
6515 if (!lua_checkstack(hlua->T, 1)) {
6516 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6517 ctx->rule->arg.hlua_rule->fcn.name);
6518 RESET_SAFE_LJMP(hlua->T);
6519 return 0;
6520 }
6521
6522 /* Restore the function in the stack. */
6523 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6524
6525 /* Create and and push object stream in the stack. */
6526 if (!hlua_applet_http_new(hlua->T, ctx)) {
6527 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6528 ctx->rule->arg.hlua_rule->fcn.name);
6529 RESET_SAFE_LJMP(hlua->T);
6530 return 0;
6531 }
6532 hlua->nargs = 1;
6533
6534 /* Look for a 100-continue expected. */
6535 if (msg->flags & HTTP_MSGF_VER_11) {
6536 hdr.idx = 0;
6537 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) &&
6538 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6539 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6540 }
6541
6542 /* push keywords in the stack. */
6543 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6544 if (!lua_checkstack(hlua->T, 1)) {
6545 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6546 ctx->rule->arg.hlua_rule->fcn.name);
6547 RESET_SAFE_LJMP(hlua->T);
6548 return 0;
6549 }
6550 lua_pushstring(hlua->T, *arg);
6551 hlua->nargs++;
6552 }
6553
6554 RESET_SAFE_LJMP(hlua->T);
6555
6556 /* Wakeup the applet when data is ready for read. */
6557 si_applet_cant_get(si);
6558
6559 return 1;
6560}
6561
6562static void hlua_applet_http_fct(struct appctx *ctx)
6563{
6564 struct stream_interface *si = ctx->owner;
6565 struct stream *strm = si_strm(si);
6566 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006567 struct act_rule *rule = ctx->rule;
6568 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006569 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006570 char *blk1;
6571 int len1;
6572 char *blk2;
6573 int len2;
6574 int ret;
6575
6576 /* If the stream is disconnect or closed, ldo nothing. */
6577 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6578 return;
6579
6580 /* Set the currently running flag. */
6581 if (!HLUA_IS_RUNNING(hlua) &&
6582 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6583
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006584 /* Wait for full HTTP analysys. */
6585 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
6586 si_applet_cant_get(si);
6587 return;
6588 }
6589
6590 /* Store the max amount of bytes that we can read. */
6591 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6592
6593 /* We need to flush the request header. This left the body
6594 * for the Lua.
6595 */
6596
6597 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006598 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006599 if (ret == -1)
6600 return;
6601
6602 /* No data available, ask for more data. */
6603 if (ret == 1)
6604 len2 = 0;
6605 if (ret == 0)
6606 len1 = 0;
6607 if (len1 + len2 < strm->txn->req.eoh + 2) {
6608 si_applet_cant_get(si);
6609 return;
6610 }
6611
6612 /* skip the requests bytes. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006613 co_skip(si_oc(si), strm->txn->req.eoh + 2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006614 }
6615
6616 /* Executes The applet if it is not done. */
6617 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6618
6619 /* Execute the function. */
6620 switch (hlua_ctx_resume(hlua, 1)) {
6621 /* finished. */
6622 case HLUA_E_OK:
6623 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6624 break;
6625
6626 /* yield. */
6627 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006628 if (hlua->wake_time != TICK_ETERNITY)
6629 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006630 return;
6631
6632 /* finished with error. */
6633 case HLUA_E_ERRMSG:
6634 /* Display log. */
6635 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6636 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6637 lua_pop(hlua->T, 1);
6638 goto error;
6639
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006640 case HLUA_E_ETMOUT:
6641 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6642 rule->arg.hlua_rule->fcn.name);
6643 goto error;
6644
6645 case HLUA_E_NOMEM:
6646 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6647 rule->arg.hlua_rule->fcn.name);
6648 goto error;
6649
6650 case HLUA_E_YIELD: /* unexpected */
6651 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6652 rule->arg.hlua_rule->fcn.name);
6653 goto error;
6654
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006655 case HLUA_E_ERR:
6656 /* Display log. */
6657 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6658 rule->arg.hlua_rule->fcn.name);
6659 goto error;
6660
6661 default:
6662 goto error;
6663 }
6664 }
6665
6666 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6667
6668 /* We must send the final chunk. */
6669 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6670 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6671
6672 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006673 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006674
6675 /* critical error. */
6676 if (ret == -2 || ret == -3) {
6677 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6678 rule->arg.hlua_rule->fcn.name);
6679 goto error;
6680 }
6681
6682 /* no enough space error. */
6683 if (ret == -1) {
6684 si_applet_cant_put(si);
6685 return;
6686 }
6687
6688 /* set the last chunk sent. */
6689 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6690 }
6691
6692 /* close the connection. */
6693
6694 /* status / log */
6695 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6696 strm->logs.tv_request = now;
6697
6698 /* eat the whole request */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006699 co_skip(si_oc(si), si_ob(si)->o);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006700 res->flags |= CF_READ_NULL;
6701 si_shutr(si);
6702
6703 return;
6704 }
6705
6706error:
6707
6708 /* If we are in HTTP mode, and we are not send any
6709 * data, return a 500 server error in best effort:
6710 * if there are no room avalaible in the buffer,
6711 * just close the connection.
6712 */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006713 ci_putblk(res, error_500, strlen(error_500));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006714 if (!(strm->flags & SF_ERR_MASK))
6715 strm->flags |= SF_ERR_RESOURCE;
6716 si_shutw(si);
6717 si_shutr(si);
6718 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6719}
6720
6721static void hlua_applet_http_release(struct appctx *ctx)
6722{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006723 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006724 task_free(ctx->ctx.hlua_apphttp.task);
6725 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006726 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006727 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006728}
6729
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006730/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6731 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006732 *
6733 * This function can fail with an abort() due to an Lua critical error.
6734 * We are in the configuration parsing process of HAProxy, this abort() is
6735 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006736 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006737static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6738 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006739{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006740 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006741 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006742
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006743 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006744 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006745 if (!rule->arg.hlua_rule) {
6746 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006747 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006748 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006749
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006750 /* Memory for arguments. */
6751 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6752 if (!rule->arg.hlua_rule->args) {
6753 memprintf(err, "out of memory error");
6754 return ACT_RET_PRS_ERR;
6755 }
6756
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006757 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006758 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006759
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006760 /* Expect some arguments */
6761 for (i = 0; i < fcn->nargs; i++) {
6762 if (*args[i+1] == '\0') {
6763 memprintf(err, "expect %d arguments", fcn->nargs);
6764 return ACT_RET_PRS_ERR;
6765 }
6766 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6767 if (!rule->arg.hlua_rule->args[i]) {
6768 memprintf(err, "out of memory error");
6769 return ACT_RET_PRS_ERR;
6770 }
6771 (*cur_arg)++;
6772 }
6773 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006774
Thierry FOURNIER42148732015-09-02 17:17:33 +02006775 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006776 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006777 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006778}
6779
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006780static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6781 struct act_rule *rule, char **err)
6782{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006783 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006784
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006785 /* HTTP applets are forbidden in tcp-request rules.
6786 * HTTP applet request requires everything initilized by
6787 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6788 * The applet will be immediately initilized, but its before
6789 * the call of this analyzer.
6790 */
6791 if (rule->from != ACT_F_HTTP_REQ) {
6792 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6793 return ACT_RET_PRS_ERR;
6794 }
6795
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006796 /* Memory for the rule. */
6797 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6798 if (!rule->arg.hlua_rule) {
6799 memprintf(err, "out of memory error");
6800 return ACT_RET_PRS_ERR;
6801 }
6802
6803 /* Reference the Lua function and store the reference. */
6804 rule->arg.hlua_rule->fcn = *fcn;
6805
6806 /* TODO: later accept arguments. */
6807 rule->arg.hlua_rule->args = NULL;
6808
6809 /* Add applet pointer in the rule. */
6810 rule->applet.obj_type = OBJ_TYPE_APPLET;
6811 rule->applet.name = fcn->name;
6812 rule->applet.init = hlua_applet_http_init;
6813 rule->applet.fct = hlua_applet_http_fct;
6814 rule->applet.release = hlua_applet_http_release;
6815 rule->applet.timeout = hlua_timeout_applet;
6816
6817 return ACT_RET_PRS_OK;
6818}
6819
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006820/* This function is an LUA binding used for registering
6821 * "sample-conv" functions. It expects a converter name used
6822 * in the haproxy configuration file, and an LUA function.
6823 */
6824__LJMP static int hlua_register_action(lua_State *L)
6825{
6826 struct action_kw_list *akl;
6827 const char *name;
6828 int ref;
6829 int len;
6830 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006831 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006832
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006833 /* Initialise the number of expected arguments at 0. */
6834 nargs = 0;
6835
6836 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6837 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006838
6839 /* First argument : converter name. */
6840 name = MAY_LJMP(luaL_checkstring(L, 1));
6841
6842 /* Second argument : environment. */
6843 if (lua_type(L, 2) != LUA_TTABLE)
6844 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6845
6846 /* Third argument : lua function. */
6847 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6848
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006849 /* Fouth argument : number of mandatories arguments expected on the configuration line. */
6850 if (lua_gettop(L) >= 4)
6851 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6852
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006853 /* browse the second argulent as an array. */
6854 lua_pushnil(L);
6855 while (lua_next(L, 2) != 0) {
6856 if (lua_type(L, -1) != LUA_TSTRING)
6857 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6858
6859 /* Check required environment. Only accepted "http" or "tcp". */
6860 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006861 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006862 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006863 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006864 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006865 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006866 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006867
6868 /* Fill fcn. */
6869 fcn->name = strdup(name);
6870 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006871 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006872 fcn->function_ref = ref;
6873
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006874 /* Set the expected number od arguments. */
6875 fcn->nargs = nargs;
6876
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006877 /* List head */
6878 akl->list.n = akl->list.p = NULL;
6879
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006880 /* action keyword. */
6881 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006882 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006883 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006884 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006885
6886 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6887
6888 akl->kw[0].match_pfx = 0;
6889 akl->kw[0].private = fcn;
6890 akl->kw[0].parse = action_register_lua;
6891
6892 /* select the action registering point. */
6893 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6894 tcp_req_cont_keywords_register(akl);
6895 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6896 tcp_res_cont_keywords_register(akl);
6897 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6898 http_req_keywords_register(akl);
6899 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6900 http_res_keywords_register(akl);
6901 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006902 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006903 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6904 "are expected.", lua_tostring(L, -1)));
6905
6906 /* pop the environment string. */
6907 lua_pop(L, 1);
6908 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006909 return ACT_RET_PRS_OK;
6910}
6911
6912static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6913 struct act_rule *rule, char **err)
6914{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006915 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006916
6917 /* Memory for the rule. */
6918 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6919 if (!rule->arg.hlua_rule) {
6920 memprintf(err, "out of memory error");
6921 return ACT_RET_PRS_ERR;
6922 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006923
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006924 /* Reference the Lua function and store the reference. */
6925 rule->arg.hlua_rule->fcn = *fcn;
6926
6927 /* TODO: later accept arguments. */
6928 rule->arg.hlua_rule->args = NULL;
6929
6930 /* Add applet pointer in the rule. */
6931 rule->applet.obj_type = OBJ_TYPE_APPLET;
6932 rule->applet.name = fcn->name;
6933 rule->applet.init = hlua_applet_tcp_init;
6934 rule->applet.fct = hlua_applet_tcp_fct;
6935 rule->applet.release = hlua_applet_tcp_release;
6936 rule->applet.timeout = hlua_timeout_applet;
6937
6938 return 0;
6939}
6940
6941/* This function is an LUA binding used for registering
6942 * "sample-conv" functions. It expects a converter name used
6943 * in the haproxy configuration file, and an LUA function.
6944 */
6945__LJMP static int hlua_register_service(lua_State *L)
6946{
6947 struct action_kw_list *akl;
6948 const char *name;
6949 const char *env;
6950 int ref;
6951 int len;
6952 struct hlua_function *fcn;
6953
6954 MAY_LJMP(check_args(L, 3, "register_service"));
6955
6956 /* First argument : converter name. */
6957 name = MAY_LJMP(luaL_checkstring(L, 1));
6958
6959 /* Second argument : environment. */
6960 env = MAY_LJMP(luaL_checkstring(L, 2));
6961
6962 /* Third argument : lua function. */
6963 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6964
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006965 /* Allocate and fill the sample fetch keyword struct. */
6966 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
6967 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006968 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006969 fcn = calloc(1, sizeof(*fcn));
6970 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006971 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006972
6973 /* Fill fcn. */
6974 len = strlen("<lua.>") + strlen(name) + 1;
6975 fcn->name = calloc(1, len);
6976 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006977 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006978 snprintf((char *)fcn->name, len, "<lua.%s>", name);
6979 fcn->function_ref = ref;
6980
6981 /* List head */
6982 akl->list.n = akl->list.p = NULL;
6983
6984 /* converter keyword. */
6985 len = strlen("lua.") + strlen(name) + 1;
6986 akl->kw[0].kw = calloc(1, len);
6987 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006988 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006989
6990 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6991
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01006992 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006993 if (strcmp(env, "tcp") == 0)
6994 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006995 else if (strcmp(env, "http") == 0)
6996 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006997 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006998 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01006999 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007000
7001 akl->kw[0].match_pfx = 0;
7002 akl->kw[0].private = fcn;
7003
7004 /* End of array. */
7005 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7006
7007 /* Register this new converter */
7008 service_keywords_register(akl);
7009
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007010 return 0;
7011}
7012
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007013/* This function initialises Lua cli handler. It copies the
7014 * arguments in the Lua stack and create channel IO objects.
7015 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007016static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007017{
7018 struct hlua *hlua;
7019 struct hlua_function *fcn;
7020 int i;
7021 const char *error;
7022
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007023 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007024 appctx->ctx.hlua_cli.fcn = private;
7025
Willy Tarreaubafbe012017-11-24 17:34:44 +01007026 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007027 if (!hlua) {
7028 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007029 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007030 }
7031 HLUA_INIT(hlua);
7032 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007033
7034 /* Create task used by signal to wakeup applets.
7035 * We use the same wakeup fonction than the Lua applet_tcp and
7036 * applet_http. It is absolutely compatible.
7037 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007038 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007039 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007040 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007041 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007042 }
7043 appctx->ctx.hlua_cli.task->nice = 0;
7044 appctx->ctx.hlua_cli.task->context = appctx;
7045 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7046
7047 /* Initialises the Lua context */
7048 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
7049 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007050 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007051 }
7052
7053 /* The following Lua calls can fail. */
7054 if (!SET_SAFE_LJMP(hlua->T)) {
7055 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7056 error = lua_tostring(hlua->T, -1);
7057 else
7058 error = "critical error";
7059 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7060 goto error;
7061 }
7062
7063 /* Check stack available size. */
7064 if (!lua_checkstack(hlua->T, 2)) {
7065 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7066 goto error;
7067 }
7068
7069 /* Restore the function in the stack. */
7070 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7071
7072 /* Once the arguments parsed, the CLI is like an AppletTCP,
7073 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007074 */
7075 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7076 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7077 goto error;
7078 }
7079 hlua->nargs = 1;
7080
7081 /* push keywords in the stack. */
7082 for (i = 0; *args[i]; i++) {
7083 /* Check stack available size. */
7084 if (!lua_checkstack(hlua->T, 1)) {
7085 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7086 goto error;
7087 }
7088 lua_pushstring(hlua->T, args[i]);
7089 hlua->nargs++;
7090 }
7091
7092 /* We must initialize the execution timeouts. */
7093 hlua->max_time = hlua_timeout_session;
7094
7095 /* At this point the execution is safe. */
7096 RESET_SAFE_LJMP(hlua->T);
7097
7098 /* It's ok */
7099 return 0;
7100
7101 /* It's not ok. */
7102error:
7103 RESET_SAFE_LJMP(hlua->T);
7104 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007105 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007106 return 1;
7107}
7108
7109static int hlua_cli_io_handler_fct(struct appctx *appctx)
7110{
7111 struct hlua *hlua;
7112 struct stream_interface *si;
7113 struct hlua_function *fcn;
7114
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007115 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007116 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007117 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007118
7119 /* If the stream is disconnect or closed, ldo nothing. */
7120 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7121 return 1;
7122
7123 /* Execute the function. */
7124 switch (hlua_ctx_resume(hlua, 1)) {
7125
7126 /* finished. */
7127 case HLUA_E_OK:
7128 return 1;
7129
7130 /* yield. */
7131 case HLUA_E_AGAIN:
7132 /* We want write. */
7133 if (HLUA_IS_WAKERESWR(hlua))
7134 si_applet_cant_put(si);
7135 /* Set the timeout. */
7136 if (hlua->wake_time != TICK_ETERNITY)
7137 task_schedule(hlua->task, hlua->wake_time);
7138 return 0;
7139
7140 /* finished with error. */
7141 case HLUA_E_ERRMSG:
7142 /* Display log. */
7143 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7144 fcn->name, lua_tostring(hlua->T, -1));
7145 lua_pop(hlua->T, 1);
7146 return 1;
7147
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007148 case HLUA_E_ETMOUT:
7149 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7150 fcn->name);
7151 return 1;
7152
7153 case HLUA_E_NOMEM:
7154 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7155 fcn->name);
7156 return 1;
7157
7158 case HLUA_E_YIELD: /* unexpected */
7159 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7160 fcn->name);
7161 return 1;
7162
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007163 case HLUA_E_ERR:
7164 /* Display log. */
7165 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7166 fcn->name);
7167 return 1;
7168
7169 default:
7170 return 1;
7171 }
7172
7173 return 1;
7174}
7175
7176static void hlua_cli_io_release_fct(struct appctx *appctx)
7177{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007178 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007179 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007180}
7181
7182/* This function is an LUA binding used for registering
7183 * new keywords in the cli. It expects a list of keywords
7184 * which are the "path". It is limited to 5 keywords. A
7185 * description of the command, a function to be executed
7186 * for the parsing and a function for io handlers.
7187 */
7188__LJMP static int hlua_register_cli(lua_State *L)
7189{
7190 struct cli_kw_list *cli_kws;
7191 const char *message;
7192 int ref_io;
7193 int len;
7194 struct hlua_function *fcn;
7195 int index;
7196 int i;
7197
7198 MAY_LJMP(check_args(L, 3, "register_cli"));
7199
7200 /* First argument : an array of maximum 5 keywords. */
7201 if (!lua_istable(L, 1))
7202 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7203
7204 /* Second argument : string with contextual message. */
7205 message = MAY_LJMP(luaL_checkstring(L, 2));
7206
7207 /* Third and fourth argument : lua function. */
7208 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7209
7210 /* Allocate and fill the sample fetch keyword struct. */
7211 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7212 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007213 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007214 fcn = calloc(1, sizeof(*fcn));
7215 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007216 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007217
7218 /* Fill path. */
7219 index = 0;
7220 lua_pushnil(L);
7221 while(lua_next(L, 1) != 0) {
7222 if (index >= 5)
7223 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7224 if (lua_type(L, -1) != LUA_TSTRING)
7225 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7226 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7227 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007228 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007229 index++;
7230 lua_pop(L, 1);
7231 }
7232
7233 /* Copy help message. */
7234 cli_kws->kw[0].usage = strdup(message);
7235 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007236 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007237
7238 /* Fill fcn io handler. */
7239 len = strlen("<lua.cli>") + 1;
7240 for (i = 0; i < index; i++)
7241 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7242 fcn->name = calloc(1, len);
7243 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007244 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007245 strncat((char *)fcn->name, "<lua.cli", len);
7246 for (i = 0; i < index; i++) {
7247 strncat((char *)fcn->name, ".", len);
7248 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7249 }
7250 strncat((char *)fcn->name, ">", len);
7251 fcn->function_ref = ref_io;
7252
7253 /* Fill last entries. */
7254 cli_kws->kw[0].private = fcn;
7255 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7256 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7257 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7258
7259 /* Register this new converter */
7260 cli_register_kw(cli_kws);
7261
7262 return 0;
7263}
7264
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007265static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7266 struct proxy *defpx, const char *file, int line,
7267 char **err, unsigned int *timeout)
7268{
7269 const char *error;
7270
7271 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
7272 if (error && *error != '\0') {
7273 memprintf(err, "%s: invalid timeout", args[0]);
7274 return -1;
7275 }
7276 return 0;
7277}
7278
7279static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7280 struct proxy *defpx, const char *file, int line,
7281 char **err)
7282{
7283 return hlua_read_timeout(args, section_type, curpx, defpx,
7284 file, line, err, &hlua_timeout_session);
7285}
7286
7287static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7288 struct proxy *defpx, const char *file, int line,
7289 char **err)
7290{
7291 return hlua_read_timeout(args, section_type, curpx, defpx,
7292 file, line, err, &hlua_timeout_task);
7293}
7294
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007295static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7296 struct proxy *defpx, const char *file, int line,
7297 char **err)
7298{
7299 return hlua_read_timeout(args, section_type, curpx, defpx,
7300 file, line, err, &hlua_timeout_applet);
7301}
7302
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007303static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7304 struct proxy *defpx, const char *file, int line,
7305 char **err)
7306{
7307 char *error;
7308
7309 hlua_nb_instruction = strtoll(args[1], &error, 10);
7310 if (*error != '\0') {
7311 memprintf(err, "%s: invalid number", args[0]);
7312 return -1;
7313 }
7314 return 0;
7315}
7316
Willy Tarreau32f61e22015-03-18 17:54:59 +01007317static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7318 struct proxy *defpx, const char *file, int line,
7319 char **err)
7320{
7321 char *error;
7322
7323 if (*(args[1]) == 0) {
7324 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7325 return -1;
7326 }
7327 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7328 if (*error != '\0') {
7329 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7330 return -1;
7331 }
7332 return 0;
7333}
7334
7335
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007336/* This function is called by the main configuration key "lua-load". It loads and
7337 * execute an lua file during the parsing of the HAProxy configuration file. It is
7338 * the main lua entry point.
7339 *
7340 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
7341 * occured, otherwise it returns 0.
7342 *
7343 * In some error case, LUA set an error message in top of the stack. This function
7344 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007345 *
7346 * This function can fail with an abort() due to an Lua critical error.
7347 * We are in the configuration parsing process of HAProxy, this abort() is
7348 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007349 */
7350static int hlua_load(char **args, int section_type, struct proxy *curpx,
7351 struct proxy *defpx, const char *file, int line,
7352 char **err)
7353{
7354 int error;
7355
7356 /* Just load and compile the file. */
7357 error = luaL_loadfile(gL.T, args[1]);
7358 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007359 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007360 lua_pop(gL.T, 1);
7361 return -1;
7362 }
7363
7364 /* If no syntax error where detected, execute the code. */
7365 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7366 switch (error) {
7367 case LUA_OK:
7368 break;
7369 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007370 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007371 lua_pop(gL.T, 1);
7372 return -1;
7373 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007374 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007375 return -1;
7376 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007377 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007378 lua_pop(gL.T, 1);
7379 return -1;
7380 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007381 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007382 lua_pop(gL.T, 1);
7383 return -1;
7384 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007385 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007386 lua_pop(gL.T, 1);
7387 return -1;
7388 }
7389
7390 return 0;
7391}
7392
7393/* configuration keywords declaration */
7394static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007395 { CFG_GLOBAL, "lua-load", hlua_load },
7396 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7397 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007398 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007399 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007400 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007401 { 0, NULL, NULL },
7402}};
7403
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007404/* This function can fail with an abort() due to an Lua critical error.
7405 * We are in the initialisation process of HAProxy, this abort() is
7406 * tolerated.
7407 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007408int hlua_post_init()
7409{
7410 struct hlua_init_function *init;
7411 const char *msg;
7412 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007413 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007414
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007415 /* Call post initialisation function in safe environement. */
7416 if (!SET_SAFE_LJMP(gL.T)) {
7417 if (lua_type(gL.T, -1) == LUA_TSTRING)
7418 error = lua_tostring(gL.T, -1);
7419 else
7420 error = "critical error";
7421 fprintf(stderr, "Lua post-init: %s.\n", error);
7422 exit(1);
7423 }
7424 hlua_fcn_post_init(gL.T);
7425 RESET_SAFE_LJMP(gL.T);
7426
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007427 list_for_each_entry(init, &hlua_init_functions, l) {
7428 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7429 ret = hlua_ctx_resume(&gL, 0);
7430 switch (ret) {
7431 case HLUA_E_OK:
7432 lua_pop(gL.T, -1);
7433 return 1;
7434 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007435 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007436 return 0;
7437 case HLUA_E_ERRMSG:
7438 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007439 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007440 return 0;
7441 case HLUA_E_ERR:
7442 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007443 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007444 return 0;
7445 }
7446 }
7447 return 1;
7448}
7449
Willy Tarreau32f61e22015-03-18 17:54:59 +01007450/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7451 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7452 * is the previously allocated size or the kind of object in case of a new
7453 * allocation. <nsize> is the requested new size.
7454 */
7455static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7456{
7457 struct hlua_mem_allocator *zone = ud;
7458
7459 if (nsize == 0) {
7460 /* it's a free */
7461 if (ptr)
7462 zone->allocated -= osize;
7463 free(ptr);
7464 return NULL;
7465 }
7466
7467 if (!ptr) {
7468 /* it's a new allocation */
7469 if (zone->limit && zone->allocated + nsize > zone->limit)
7470 return NULL;
7471
7472 ptr = malloc(nsize);
7473 if (ptr)
7474 zone->allocated += nsize;
7475 return ptr;
7476 }
7477
7478 /* it's a realloc */
7479 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7480 return NULL;
7481
7482 ptr = realloc(ptr, nsize);
7483 if (ptr)
7484 zone->allocated += nsize - osize;
7485 return ptr;
7486}
7487
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007488/* Ithis function can fail with an abort() due to an Lua critical error.
7489 * We are in the initialisation process of HAProxy, this abort() is
7490 * tolerated.
7491 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007492void hlua_init(void)
7493{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007494 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007495 int idx;
7496 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007497 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007498 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007499 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007500#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007501 struct srv_kw *kw;
7502 int tmp_error;
7503 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007504 char *args[] = { /* SSL client configuration. */
7505 "ssl",
7506 "verify",
7507 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007508 NULL
7509 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007510#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007511
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007512 HA_SPIN_INIT(&hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02007513
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007514 /* Initialise struct hlua and com signals pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007515 pool_head_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007516
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007517 /* Register configuration keywords. */
7518 cfg_register_keywords(&cfg_kws);
7519
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007520 /* Init main lua stack. */
7521 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007522 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007523 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007524 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007525 hlua_sethlua(&gL);
7526 gL.Tref = LUA_REFNIL;
7527 gL.task = NULL;
7528
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007529 /* From this point, until the end of the initialisation fucntion,
7530 * the Lua function can fail with an abort. We are in the initialisation
7531 * process of HAProxy, this abort() is tolerated.
7532 */
7533
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007534 /* Initialise lua. */
7535 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007536
Thierry Fournier75933d42016-01-21 09:30:18 +01007537 /* Set safe environment for the initialisation. */
7538 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007539 if (lua_type(gL.T, -1) == LUA_TSTRING)
7540 error_msg = lua_tostring(gL.T, -1);
7541 else
7542 error_msg = "critical error";
7543 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007544 exit(1);
7545 }
7546
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007547 /*
7548 *
7549 * Create "core" object.
7550 *
7551 */
7552
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007553 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007554 lua_newtable(gL.T);
7555
7556 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007557 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007558 hlua_class_const_int(gL.T, log_levels[i], i);
7559
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007560 /* Register special functions. */
7561 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007562 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007563 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007564 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007565 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007566 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007567 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007568 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007569 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007570 hlua_class_function(gL.T, "sleep", hlua_sleep);
7571 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007572 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7573 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7574 hlua_class_function(gL.T, "set_map", hlua_set_map);
7575 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007576 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007577 hlua_class_function(gL.T, "log", hlua_log);
7578 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7579 hlua_class_function(gL.T, "Info", hlua_log_info);
7580 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7581 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007582 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007583 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007584
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007585 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007586
7587 /*
7588 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007589 * Register class Map
7590 *
7591 */
7592
7593 /* This table entry is the object "Map" base. */
7594 lua_newtable(gL.T);
7595
7596 /* register pattern types. */
7597 for (i=0; i<PAT_MATCH_NUM; i++)
7598 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007599 for (i=0; i<PAT_MATCH_NUM; i++) {
7600 snprintf(trash.str, trash.size, "_%s", pat_match_names[i]);
7601 hlua_class_const_int(gL.T, trash.str, i);
7602 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007603
7604 /* register constructor. */
7605 hlua_class_function(gL.T, "new", hlua_map_new);
7606
7607 /* Create and fill the metatable. */
7608 lua_newtable(gL.T);
7609
7610 /* Create and fille the __index entry. */
7611 lua_pushstring(gL.T, "__index");
7612 lua_newtable(gL.T);
7613
7614 /* Register . */
7615 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7616 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7617
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007618 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007619
Thierry Fournier45e78d72016-02-19 18:34:46 +01007620 /* Register previous table in the registry with reference and named entry.
7621 * The function hlua_register_metatable() pops the stack, so we
7622 * previously create a copy of the table.
7623 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007624 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007625 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007626
7627 /* Assign the metatable to the mai Map object. */
7628 lua_setmetatable(gL.T, -2);
7629
7630 /* Set a name to the table. */
7631 lua_setglobal(gL.T, "Map");
7632
7633 /*
7634 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007635 * Register class Channel
7636 *
7637 */
7638
7639 /* Create and fill the metatable. */
7640 lua_newtable(gL.T);
7641
7642 /* Create and fille the __index entry. */
7643 lua_pushstring(gL.T, "__index");
7644 lua_newtable(gL.T);
7645
7646 /* Register . */
7647 hlua_class_function(gL.T, "get", hlua_channel_get);
7648 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7649 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7650 hlua_class_function(gL.T, "set", hlua_channel_set);
7651 hlua_class_function(gL.T, "append", hlua_channel_append);
7652 hlua_class_function(gL.T, "send", hlua_channel_send);
7653 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7654 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7655 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007656 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007657
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007658 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007659
7660 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007661 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007662
7663 /*
7664 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007665 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007666 *
7667 */
7668
7669 /* Create and fill the metatable. */
7670 lua_newtable(gL.T);
7671
7672 /* Create and fille the __index entry. */
7673 lua_pushstring(gL.T, "__index");
7674 lua_newtable(gL.T);
7675
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007676 /* Browse existing fetches and create the associated
7677 * object method.
7678 */
7679 sf = NULL;
7680 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7681
7682 /* Dont register the keywork if the arguments check function are
7683 * not safe during the runtime.
7684 */
7685 if ((sf->val_args != NULL) &&
7686 (sf->val_args != val_payload_lv) &&
7687 (sf->val_args != val_hdr))
7688 continue;
7689
7690 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7691 * by an underscore.
7692 */
7693 strncpy(trash.str, sf->kw, trash.size);
7694 trash.str[trash.size - 1] = '\0';
7695 for (p = trash.str; *p; p++)
7696 if (*p == '.' || *p == '-' || *p == '+')
7697 *p = '_';
7698
7699 /* Register the function. */
7700 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007701 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007702 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007703 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007704 }
7705
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007706 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007707
7708 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007709 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007710
7711 /*
7712 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007713 * Register class Converters
7714 *
7715 */
7716
7717 /* Create and fill the metatable. */
7718 lua_newtable(gL.T);
7719
7720 /* Create and fill the __index entry. */
7721 lua_pushstring(gL.T, "__index");
7722 lua_newtable(gL.T);
7723
7724 /* Browse existing converters and create the associated
7725 * object method.
7726 */
7727 sc = NULL;
7728 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7729 /* Dont register the keywork if the arguments check function are
7730 * not safe during the runtime.
7731 */
7732 if (sc->val_args != NULL)
7733 continue;
7734
7735 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7736 * by an underscore.
7737 */
7738 strncpy(trash.str, sc->kw, trash.size);
7739 trash.str[trash.size - 1] = '\0';
7740 for (p = trash.str; *p; p++)
7741 if (*p == '.' || *p == '-' || *p == '+')
7742 *p = '_';
7743
7744 /* Register the function. */
7745 lua_pushstring(gL.T, trash.str);
7746 lua_pushlightuserdata(gL.T, sc);
7747 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007748 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007749 }
7750
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007751 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007752
7753 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007754 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007755
7756 /*
7757 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007758 * Register class HTTP
7759 *
7760 */
7761
7762 /* Create and fill the metatable. */
7763 lua_newtable(gL.T);
7764
7765 /* Create and fille the __index entry. */
7766 lua_pushstring(gL.T, "__index");
7767 lua_newtable(gL.T);
7768
7769 /* Register Lua functions. */
7770 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7771 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7772 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7773 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7774 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7775 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7776 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7777 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7778 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7779 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7780
7781 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7782 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7783 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7784 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7785 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7786 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007787 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007788
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007789 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007790
7791 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007792 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007793
7794 /*
7795 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007796 * Register class AppletTCP
7797 *
7798 */
7799
7800 /* Create and fill the metatable. */
7801 lua_newtable(gL.T);
7802
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007803 /* Create and fille the __index entry. */
7804 lua_pushstring(gL.T, "__index");
7805 lua_newtable(gL.T);
7806
7807 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007808 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7809 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7810 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7811 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7812 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007813 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7814 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7815 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007816
7817 lua_settable(gL.T, -3);
7818
7819 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007820 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007821
7822 /*
7823 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007824 * Register class AppletHTTP
7825 *
7826 */
7827
7828 /* Create and fill the metatable. */
7829 lua_newtable(gL.T);
7830
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007831 /* Create and fille the __index entry. */
7832 lua_pushstring(gL.T, "__index");
7833 lua_newtable(gL.T);
7834
7835 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007836 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7837 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007838 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7839 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7840 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007841 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7842 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7843 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7844 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7845 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7846 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7847
7848 lua_settable(gL.T, -3);
7849
7850 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007851 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007852
7853 /*
7854 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007855 * Register class TXN
7856 *
7857 */
7858
7859 /* Create and fill the metatable. */
7860 lua_newtable(gL.T);
7861
7862 /* Create and fille the __index entry. */
7863 lua_pushstring(gL.T, "__index");
7864 lua_newtable(gL.T);
7865
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007866 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01007867 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7868 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007869 hlua_class_function(gL.T, "set_var", hlua_set_var);
Christopher Faulet85d79c92016-11-09 16:54:56 +01007870 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007871 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007872 hlua_class_function(gL.T, "done", hlua_txn_done);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007873 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
7874 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7875 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007876 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7877 hlua_class_function(gL.T, "log", hlua_txn_log);
7878 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7879 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7880 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7881 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007882
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007883 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007884
7885 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007886 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007887
7888 /*
7889 *
7890 * Register class Socket
7891 *
7892 */
7893
7894 /* Create and fill the metatable. */
7895 lua_newtable(gL.T);
7896
7897 /* Create and fille the __index entry. */
7898 lua_pushstring(gL.T, "__index");
7899 lua_newtable(gL.T);
7900
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007901#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007902 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007903#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007904 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7905 hlua_class_function(gL.T, "send", hlua_socket_send);
7906 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7907 hlua_class_function(gL.T, "close", hlua_socket_close);
7908 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7909 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7910 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7911 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7912
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007913 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007914
7915 /* Register the garbage collector entry. */
7916 lua_pushstring(gL.T, "__gc");
7917 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007918 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007919
7920 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007921 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007922
7923 /* Proxy and server configuration initialisation. */
7924 memset(&socket_proxy, 0, sizeof(socket_proxy));
7925 init_new_proxy(&socket_proxy);
7926 socket_proxy.parent = NULL;
7927 socket_proxy.last_change = now.tv_sec;
7928 socket_proxy.id = "LUA-SOCKET";
7929 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
7930 socket_proxy.maxconn = 0;
7931 socket_proxy.accept = NULL;
7932 socket_proxy.options2 |= PR_O2_INDEPSTR;
7933 socket_proxy.srv = NULL;
7934 socket_proxy.conn_retries = 0;
7935 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
7936
7937 /* Init TCP server: unchanged parameters */
7938 memset(&socket_tcp, 0, sizeof(socket_tcp));
7939 socket_tcp.next = NULL;
7940 socket_tcp.proxy = &socket_proxy;
7941 socket_tcp.obj_type = OBJ_TYPE_SERVER;
7942 LIST_INIT(&socket_tcp.actconns);
7943 LIST_INIT(&socket_tcp.pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02007944 socket_tcp.priv_conns = NULL;
7945 socket_tcp.idle_conns = NULL;
7946 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02007947 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007948 socket_tcp.last_change = 0;
7949 socket_tcp.id = "LUA-TCP-CONN";
7950 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7951 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7952 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
7953
7954 /* XXX: Copy default parameter from default server,
7955 * but the default server is not initialized.
7956 */
7957 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
7958 socket_tcp.minconn = socket_proxy.defsrv.minconn;
7959 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
7960 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
7961 socket_tcp.onerror = socket_proxy.defsrv.onerror;
7962 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
7963 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
7964 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
7965 socket_tcp.uweight = socket_proxy.defsrv.iweight;
7966 socket_tcp.iweight = socket_proxy.defsrv.iweight;
7967
7968 socket_tcp.check.status = HCHK_STATUS_INI;
7969 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
7970 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
7971 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
7972 socket_tcp.check.server = &socket_tcp;
7973
7974 socket_tcp.agent.status = HCHK_STATUS_INI;
7975 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
7976 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
7977 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
7978 socket_tcp.agent.server = &socket_tcp;
7979
Willy Tarreaua261e9b2016-12-22 20:44:00 +01007980 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007981
7982#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007983 /* Init TCP server: unchanged parameters */
7984 memset(&socket_ssl, 0, sizeof(socket_ssl));
7985 socket_ssl.next = NULL;
7986 socket_ssl.proxy = &socket_proxy;
7987 socket_ssl.obj_type = OBJ_TYPE_SERVER;
7988 LIST_INIT(&socket_ssl.actconns);
7989 LIST_INIT(&socket_ssl.pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02007990 socket_tcp.priv_conns = NULL;
7991 socket_tcp.idle_conns = NULL;
7992 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02007993 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007994 socket_ssl.last_change = 0;
7995 socket_ssl.id = "LUA-SSL-CONN";
7996 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7997 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7998 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
7999
8000 /* XXX: Copy default parameter from default server,
8001 * but the default server is not initialized.
8002 */
8003 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8004 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8005 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8006 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8007 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8008 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8009 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8010 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8011 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8012 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8013
8014 socket_ssl.check.status = HCHK_STATUS_INI;
8015 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8016 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8017 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8018 socket_ssl.check.server = &socket_ssl;
8019
8020 socket_ssl.agent.status = HCHK_STATUS_INI;
8021 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8022 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8023 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8024 socket_ssl.agent.server = &socket_ssl;
8025
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008026 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008027 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008028
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008029 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008030 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8031 /*
8032 *
8033 * If the keyword is not known, we can search in the registered
8034 * server keywords. This is usefull to configure special SSL
8035 * features like client certificates and ssl_verify.
8036 *
8037 */
8038 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8039 if (tmp_error != 0) {
8040 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8041 abort(); /* This must be never arrives because the command line
8042 not editable by the user. */
8043 }
8044 idx += kw->skip;
8045 }
8046 }
8047
8048 /* Initialize SSL server. */
Willy Tarreaucbe6da52018-05-18 17:08:28 +02008049 if (socket_ssl.xprt->prepare_srv) {
8050 int saved_used_backed = global.ssl_used_backend;
8051 // don't affect maxconn automatic computation
Willy Tarreau17d45382016-12-22 21:16:08 +01008052 socket_ssl.xprt->prepare_srv(&socket_ssl);
Willy Tarreaucbe6da52018-05-18 17:08:28 +02008053 global.ssl_used_backend = saved_used_backed;
8054 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008055#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008056
8057 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008058}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008059
8060__attribute__((constructor))
8061static void __hlua_init(void)
8062{
8063 char *ptr = NULL;
8064 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8065 hap_register_build_opts(ptr, 1);
8066}