blob: 4de5db5ac47629d6e0a08b1cc4ab38e56ce4011a [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 FOURNIER7e7ac322015-02-16 19:27:16 +01001563}
1564
Willy Tarreau87b09662015-04-03 00:22:06 +02001565/* This function is called when the "struct stream" is destroyed.
1566 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001567 * Wake all the pending signals.
1568 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001569static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001570{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001571 struct xref *peer;
1572
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001573 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001574 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1575 if (peer)
1576 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001577
1578 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001579 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1580 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001581}
1582
1583/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001584 * uses this object. If the stream does not exists, just quit.
1585 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001586 * pending signal can rest in the read and write lists. destroy
1587 * it.
1588 */
1589__LJMP static int hlua_socket_gc(lua_State *L)
1590{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001591 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001592 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001593 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001594
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001595 MAY_LJMP(check_args(L, 1, "__gc"));
1596
1597 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001598 peer = xref_get_peer_and_lock(&socket->xref);
1599 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001600 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001601 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001602
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001603 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001604 appctx->ctx.hlua_cosocket.die = 1;
1605 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001606
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001607 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001608 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001609 return 0;
1610}
1611
1612/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001613 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001614 */
sada05ed3302018-05-11 11:48:18 -07001615__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001616{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001617 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001618 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001619 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001620
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001621 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001622
1623 /* Check if we run on the same thread than the xreator thread.
1624 * We cannot access to the socket if the thread is different.
1625 */
1626 if (socket->tid != tid)
1627 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1628
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001629 peer = xref_get_peer_and_lock(&socket->xref);
1630 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001631 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001632 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001633
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001634 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001635 appctx->ctx.hlua_cosocket.die = 1;
1636 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001637
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001638 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001639 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001640 return 0;
1641}
1642
sada05ed3302018-05-11 11:48:18 -07001643/* The close function calls close_helper.
1644 */
1645__LJMP static int hlua_socket_close(lua_State *L)
1646{
1647 MAY_LJMP(check_args(L, 1, "close"));
1648 return hlua_socket_close_helper(L);
1649}
1650
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001651/* This Lua function assumes that the stack contain three parameters.
1652 * 1 - USERDATA containing a struct socket
1653 * 2 - INTEGER with values of the macro defined below
1654 * If the integer is -1, we must read at most one line.
1655 * If the integer is -2, we ust read all the data until the
1656 * end of the stream.
1657 * If the integer is positive value, we must read a number of
1658 * bytes corresponding to this value.
1659 */
1660#define HLSR_READ_LINE (-1)
1661#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001662__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001663{
1664 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1665 int wanted = lua_tointeger(L, 2);
1666 struct hlua *hlua = hlua_gethlua(L);
1667 struct appctx *appctx;
1668 int len;
1669 int nblk;
1670 char *blk1;
1671 int len1;
1672 char *blk2;
1673 int len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001674 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001675 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001676 struct stream_interface *si;
1677 struct stream *s;
1678 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001679 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001680
1681 /* Check if this lua stack is schedulable. */
1682 if (!hlua || !hlua->task)
1683 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1684 "'frontend', 'backend' or 'task'"));
1685
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001686 /* Check if we run on the same thread than the xreator thread.
1687 * We cannot access to the socket if the thread is different.
1688 */
1689 if (socket->tid != tid)
1690 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1691
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001692 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001693 peer = xref_get_peer_and_lock(&socket->xref);
1694 if (!peer)
1695 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001696 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1697 si = appctx->owner;
1698 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001700 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001702 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001703 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001704 if (nblk < 0) /* Connection close. */
1705 goto connection_closed;
1706 if (nblk == 0) /* No data avalaible. */
1707 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001708
1709 /* remove final \r\n. */
1710 if (nblk == 1) {
1711 if (blk1[len1-1] == '\n') {
1712 len1--;
1713 skip_at_end++;
1714 if (blk1[len1-1] == '\r') {
1715 len1--;
1716 skip_at_end++;
1717 }
1718 }
1719 }
1720 else {
1721 if (blk2[len2-1] == '\n') {
1722 len2--;
1723 skip_at_end++;
1724 if (blk2[len2-1] == '\r') {
1725 len2--;
1726 skip_at_end++;
1727 }
1728 }
1729 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001730 }
1731
1732 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001733 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001734 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001735 if (nblk < 0) /* Connection close. */
1736 goto connection_closed;
1737 if (nblk == 0) /* No data avalaible. */
1738 goto connection_empty;
1739 }
1740
1741 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001742 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001743 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001744 if (nblk < 0) /* Connection close. */
1745 goto connection_closed;
1746 if (nblk == 0) /* No data avalaible. */
1747 goto connection_empty;
1748
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001749 missing_bytes = wanted - socket->b.n;
1750 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001751 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001752 len1 = missing_bytes;
1753 } if (nblk == 2 && len1 + len2 > missing_bytes)
1754 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001755 }
1756
1757 len = len1;
1758
1759 luaL_addlstring(&socket->b, blk1, len1);
1760 if (nblk == 2) {
1761 len += len2;
1762 luaL_addlstring(&socket->b, blk2, len2);
1763 }
1764
1765 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001766 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001767
1768 /* Don't wait anything. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001769 stream_int_notify(&s->si[0]);
1770 stream_int_update_applet(&s->si[0]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001771
1772 /* If the pattern reclaim to read all the data
1773 * in the connection, got out.
1774 */
1775 if (wanted == HLSR_READ_ALL)
1776 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001777 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001778 goto connection_empty;
1779
1780 /* Return result. */
1781 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001782 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001783 return 1;
1784
1785connection_closed:
1786
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001787 xref_unlock(&socket->xref, peer);
1788
1789no_peer:
1790
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001791 /* If the buffer containds data. */
1792 if (socket->b.n > 0) {
1793 luaL_pushresult(&socket->b);
1794 return 1;
1795 }
1796 lua_pushnil(L);
1797 lua_pushstring(L, "connection closed.");
1798 return 2;
1799
1800connection_empty:
1801
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001802 appctx = objt_appctx(s->si[0].end);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001803 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1804 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001805 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001806 }
1807 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001808 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001809 return 0;
1810}
1811
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001812/* This Lua function gets two parameters. The first one can be string
1813 * or a number. If the string is "*l", the user requires one line. If
1814 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001815 * If the value is a number, the user require a number of bytes equal
1816 * to the value. The default value is "*l" (a line).
1817 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001818 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001819 * integer takes this values:
1820 * -1 : read a line
1821 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001822 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001823 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001824 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001825 * concatenated with the read data.
1826 */
1827__LJMP static int hlua_socket_receive(struct lua_State *L)
1828{
1829 int wanted = HLSR_READ_LINE;
1830 const char *pattern;
1831 int type;
1832 char *error;
1833 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001834 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001835
1836 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1837 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1838
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001839 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001840
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001841 /* Check if we run on the same thread than the xreator thread.
1842 * We cannot access to the socket if the thread is different.
1843 */
1844 if (socket->tid != tid)
1845 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1846
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001847 /* check for pattern. */
1848 if (lua_gettop(L) >= 2) {
1849 type = lua_type(L, 2);
1850 if (type == LUA_TSTRING) {
1851 pattern = lua_tostring(L, 2);
1852 if (strcmp(pattern, "*a") == 0)
1853 wanted = HLSR_READ_ALL;
1854 else if (strcmp(pattern, "*l") == 0)
1855 wanted = HLSR_READ_LINE;
1856 else {
1857 wanted = strtoll(pattern, &error, 10);
1858 if (*error != '\0')
1859 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1860 }
1861 }
1862 else if (type == LUA_TNUMBER) {
1863 wanted = lua_tointeger(L, 2);
1864 if (wanted < 0)
1865 WILL_LJMP(luaL_error(L, "Unsupported size."));
1866 }
1867 }
1868
1869 /* Set pattern. */
1870 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001871
1872 /* Check if we would replace the top by itself. */
1873 if (lua_gettop(L) != 2)
1874 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001875
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001876 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001877 luaL_buffinit(L, &socket->b);
1878
1879 /* Check prefix. */
1880 if (lua_gettop(L) >= 3) {
1881 if (lua_type(L, 3) != LUA_TSTRING)
1882 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1883 pattern = lua_tolstring(L, 3, &len);
1884 luaL_addlstring(&socket->b, pattern, len);
1885 }
1886
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001887 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001888}
1889
1890/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001891 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001892 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001893static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001894{
1895 struct hlua_socket *socket;
1896 struct hlua *hlua = hlua_gethlua(L);
1897 struct appctx *appctx;
1898 size_t buf_len;
1899 const char *buf;
1900 int len;
1901 int send_len;
1902 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001903 struct xref *peer;
1904 struct stream_interface *si;
1905 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906
1907 /* Check if this lua stack is schedulable. */
1908 if (!hlua || !hlua->task)
1909 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1910 "'frontend', 'backend' or 'task'"));
1911
1912 /* Get object */
1913 socket = MAY_LJMP(hlua_checksocket(L, 1));
1914 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001915 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001916
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001917 /* Check if we run on the same thread than the xreator thread.
1918 * We cannot access to the socket if the thread is different.
1919 */
1920 if (socket->tid != tid)
1921 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1922
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001923 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001924 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001925 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001926 lua_pushinteger(L, -1);
1927 return 1;
1928 }
1929 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1930 si = appctx->owner;
1931 s = si_strm(si);
1932
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001933 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001934 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001935 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001936 lua_pushinteger(L, -1);
1937 return 1;
1938 }
1939
1940 /* Update the input buffer data. */
1941 buf += sent;
1942 send_len = buf_len - sent;
1943
1944 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001945 if (sent >= buf_len) {
1946 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001947 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001948 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001949
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001950 /* Check if the buffer is avalaible because HAProxy doesn't allocate
1951 * the request buffer if its not required.
1952 */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001953 if (s->req.buf->size == 0) {
Christopher Faulet33834b12016-12-19 09:29:06 +01001954 appctx = hlua->task->context;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001955 if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01001956 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001957 }
1958
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001959 /* Check for avalaible space. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001960 len = buffer_total_space(s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01001961 if (len <= 0) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001962 appctx = objt_appctx(s->si[0].end);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001963 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
1964 xref_unlock(&socket->xref, peer);
Christopher Faulet33834b12016-12-19 09:29:06 +01001965 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001966 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001967 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01001968 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001969
1970 /* send data */
1971 if (len < send_len)
1972 send_len = len;
Willy Tarreau06d80a92017-10-19 14:32:15 +02001973 len = ci_putblk(&s->req, buf+sent, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001974
1975 /* "Not enough space" (-1), "Buffer too little to contain
1976 * the data" (-2) are not expected because the available length
1977 * is tested.
1978 * Other unknown error are also not expected.
1979 */
1980 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01001981 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001982 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001983
sada05ed3302018-05-11 11:48:18 -07001984 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001985 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001986 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001987 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001988 return 1;
1989 }
1990
1991 /* update buffers. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001992 stream_int_notify(&s->si[0]);
1993 stream_int_update_applet(&s->si[0]);
Willy Tarreaude70fa12015-09-26 11:25:05 +02001994
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001995 s->req.rex = TICK_ETERNITY;
1996 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001997
1998 /* Update length sent. */
1999 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002000 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002001
2002 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002003 if (sent + len >= buf_len) {
2004 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002005 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002006 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002007
2008hlua_socket_write_yield_return:
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002009 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002010 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002011 return 0;
2012}
2013
2014/* This function initiate the send of data. It just check the input
2015 * parameters and push an integer in the Lua stack that contain the
2016 * amount of data writed in the buffer. This is used by the function
2017 * "hlua_socket_write_yield" that can yield.
2018 *
2019 * The Lua function gets between 3 and 4 parameters. The first one is
2020 * the associated object. The second is a string buffer. The third is
2021 * a facultative integer that represents where is the buffer position
2022 * of the start of the data that can send. The first byte is the
2023 * position "1". The default value is "1". The fourth argument is a
2024 * facultative integer that represents where is the buffer position
2025 * of the end of the data that can send. The default is the last byte.
2026 */
2027static int hlua_socket_send(struct lua_State *L)
2028{
2029 int i;
2030 int j;
2031 const char *buf;
2032 size_t buf_len;
2033
2034 /* Check number of arguments. */
2035 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2036 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2037
2038 /* Get the string. */
2039 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2040
2041 /* Get and check j. */
2042 if (lua_gettop(L) == 4) {
2043 j = MAY_LJMP(luaL_checkinteger(L, 4));
2044 if (j < 0)
2045 j = buf_len + j + 1;
2046 if (j > buf_len)
2047 j = buf_len + 1;
2048 lua_pop(L, 1);
2049 }
2050 else
2051 j = buf_len;
2052
2053 /* Get and check i. */
2054 if (lua_gettop(L) == 3) {
2055 i = MAY_LJMP(luaL_checkinteger(L, 3));
2056 if (i < 0)
2057 i = buf_len + i + 1;
2058 if (i > buf_len)
2059 i = buf_len + 1;
2060 lua_pop(L, 1);
2061 } else
2062 i = 1;
2063
2064 /* Check bth i and j. */
2065 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002066 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002067 return 1;
2068 }
2069 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002070 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002071 return 1;
2072 }
2073 if (i == 0)
2074 i = 1;
2075 if (j == 0)
2076 j = 1;
2077
2078 /* Pop the string. */
2079 lua_pop(L, 1);
2080
2081 /* Update the buffer length. */
2082 buf += i - 1;
2083 buf_len = j - i + 1;
2084 lua_pushlstring(L, buf, buf_len);
2085
2086 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002087 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002088
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002089 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002090}
2091
Willy Tarreau22b0a682015-06-17 19:43:49 +02002092#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002093__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2094{
2095 static char buffer[SOCKET_INFO_MAX_LEN];
2096 int ret;
2097 int len;
2098 char *p;
2099
2100 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2101 if (ret <= 0) {
2102 lua_pushnil(L);
2103 return 1;
2104 }
2105
2106 if (ret == AF_UNIX) {
2107 lua_pushstring(L, buffer+1);
2108 return 1;
2109 }
2110 else if (ret == AF_INET6) {
2111 buffer[0] = '[';
2112 len = strlen(buffer);
2113 buffer[len] = ']';
2114 len++;
2115 buffer[len] = ':';
2116 len++;
2117 p = buffer;
2118 }
2119 else if (ret == AF_INET) {
2120 p = buffer + 1;
2121 len = strlen(p);
2122 p[len] = ':';
2123 len++;
2124 }
2125 else {
2126 lua_pushnil(L);
2127 return 1;
2128 }
2129
2130 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2131 lua_pushnil(L);
2132 return 1;
2133 }
2134
2135 lua_pushstring(L, p);
2136 return 1;
2137}
2138
2139/* Returns information about the peer of the connection. */
2140__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2141{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002142 struct hlua_socket *socket;
2143 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002144 struct xref *peer;
2145 struct appctx *appctx;
2146 struct stream_interface *si;
2147 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002148 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002149
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002150 MAY_LJMP(check_args(L, 1, "getpeername"));
2151
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002152 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002153
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002154 /* Check if we run on the same thread than the xreator thread.
2155 * We cannot access to the socket if the thread is different.
2156 */
2157 if (socket->tid != tid)
2158 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2159
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002160 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002161 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002162 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002163 lua_pushnil(L);
2164 return 1;
2165 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002166 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2167 si = appctx->owner;
2168 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002169
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002170 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002171 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002172 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002173 lua_pushnil(L);
2174 return 1;
2175 }
2176
Willy Tarreaua71f6422016-11-16 17:00:14 +01002177 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002178 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002179 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002180 lua_pushnil(L);
2181 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002182 }
2183
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002184 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2185 xref_unlock(&socket->xref, peer);
2186 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002187}
2188
2189/* Returns information about my connection side. */
2190static int hlua_socket_getsockname(struct lua_State *L)
2191{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002192 struct hlua_socket *socket;
2193 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002194 struct appctx *appctx;
2195 struct xref *peer;
2196 struct stream_interface *si;
2197 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002198 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002199
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002200 MAY_LJMP(check_args(L, 1, "getsockname"));
2201
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002202 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002203
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002204 /* Check if we run on the same thread than the xreator thread.
2205 * We cannot access to the socket if the thread is different.
2206 */
2207 if (socket->tid != tid)
2208 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2209
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002210 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002211 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002212 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002213 lua_pushnil(L);
2214 return 1;
2215 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002216 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2217 si = appctx->owner;
2218 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002219
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002220 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002221 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002222 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002223 lua_pushnil(L);
2224 return 1;
2225 }
2226
Willy Tarreaua71f6422016-11-16 17:00:14 +01002227 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002228 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002229 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002230 lua_pushnil(L);
2231 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002232 }
2233
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002234 ret = hlua_socket_info(L, &conn->addr.from);
2235 xref_unlock(&socket->xref, peer);
2236 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002237}
2238
2239/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002240static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002241 .obj_type = OBJ_TYPE_APPLET,
2242 .name = "<LUA_TCP>",
2243 .fct = hlua_socket_handler,
2244 .release = hlua_socket_release,
2245};
2246
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002247__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002248{
2249 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2250 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002251 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002252 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002253 struct stream_interface *si;
2254 struct stream *s;
2255
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002256 /* Check if we run on the same thread than the xreator thread.
2257 * We cannot access to the socket if the thread is different.
2258 */
2259 if (socket->tid != tid)
2260 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2261
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002262 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002263 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002264 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002265 lua_pushnil(L);
2266 lua_pushstring(L, "Can't connect");
2267 return 2;
2268 }
2269 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2270 si = appctx->owner;
2271 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002272
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002273 /* Check if we run on the same thread than the xreator thread.
2274 * We cannot access to the socket if the thread is different.
2275 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002276 if (socket->tid != tid) {
2277 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002278 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002279 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002280
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002281 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002282 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002283 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002284 lua_pushnil(L);
2285 lua_pushstring(L, "Can't connect");
2286 return 2;
2287 }
2288
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002289 appctx = objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002290
2291 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002292 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002293 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294 lua_pushinteger(L, 1);
2295 return 1;
2296 }
2297
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002298 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2299 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002300 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002301 }
2302 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002303 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002304 return 0;
2305}
2306
2307/* This function fail or initite the connection. */
2308__LJMP static int hlua_socket_connect(struct lua_State *L)
2309{
2310 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002311 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002312 const char *ip;
2313 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002314 struct hlua *hlua;
2315 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002316 int low, high;
2317 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002318 struct xref *peer;
2319 struct stream_interface *si;
2320 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002321
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002322 if (lua_gettop(L) < 2)
2323 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002324
2325 /* Get args. */
2326 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002327
2328 /* Check if we run on the same thread than the xreator thread.
2329 * We cannot access to the socket if the thread is different.
2330 */
2331 if (socket->tid != tid)
2332 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2333
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002334 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002335 if (lua_gettop(L) >= 3) {
2336 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002337 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002338
Tim Duesterhus6edab862018-01-06 19:04:45 +01002339 /* Force the ip to end with a colon, to support IPv6 addresses
2340 * that are not enclosed within square brackets.
2341 */
2342 if (port > 0) {
2343 luaL_buffinit(L, &b);
2344 luaL_addstring(&b, ip);
2345 luaL_addchar(&b, ':');
2346 luaL_pushresult(&b);
2347 ip = lua_tolstring(L, lua_gettop(L), NULL);
2348 }
2349 }
2350
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002351 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002352 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002353 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002354 lua_pushnil(L);
2355 return 1;
2356 }
2357 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2358 si = appctx->owner;
2359 s = si_strm(si);
2360
2361 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002362 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002363 if (!conn) {
2364 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002365 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002366 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002367
Willy Tarreau3adac082015-09-26 17:51:09 +02002368 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002369 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002370
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002371 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002372 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002373 if (!addr) {
2374 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002375 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002376 }
2377 if (low != high) {
2378 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002379 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002380 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002381 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002382
2383 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002384 if (low == 0) {
2385 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002386 if (port == -1) {
2387 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002388 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002389 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002390 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2391 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002392 if (port == -1) {
2393 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002394 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002395 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002396 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2397 }
2398 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002399
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002400 hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002401 appctx = objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002402
2403 /* inform the stream that we want to be notified whenever the
2404 * connection completes.
2405 */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002406 si_applet_cant_get(&s->si[0]);
2407 si_applet_cant_put(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002408 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002409
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002410 hlua->flags |= HLUA_MUST_GC;
2411
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002412 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2413 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002414 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002415 }
2416 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002417
2418 task_wakeup(s->task, TASK_WOKEN_INIT);
2419 /* Return yield waiting for connection. */
2420
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002421 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002422
2423 return 0;
2424}
2425
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002426#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002427__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2428{
2429 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002430 struct xref *peer;
2431 struct appctx *appctx;
2432 struct stream_interface *si;
2433 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002434
2435 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2436 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002437
2438 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002439 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002440 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002441 lua_pushnil(L);
2442 return 1;
2443 }
2444 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2445 si = appctx->owner;
2446 s = si_strm(si);
2447
2448 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002449 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002450 return MAY_LJMP(hlua_socket_connect(L));
2451}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002452#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002453
2454__LJMP static int hlua_socket_setoption(struct lua_State *L)
2455{
2456 return 0;
2457}
2458
2459__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2460{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002461 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002462 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002463 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002464 struct xref *peer;
2465 struct appctx *appctx;
2466 struct stream_interface *si;
2467 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002468
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002469 MAY_LJMP(check_args(L, 2, "settimeout"));
2470
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002471 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002472
2473 /* round up for inputs that are fractions and convert to millis */
2474 dtmout = (0.5 + MAY_LJMP(luaL_checknumber(L, 2))) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002475
Thierry Fournier17a921b2018-03-08 09:59:02 +01002476 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002477 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002478 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2479
Mark Lakes56cc1252018-03-27 09:48:06 +02002480 if (dtmout > INT_MAX) /* overflow check */
2481 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d", INT_MAX));
2482
2483 tmout = MS_TO_TICKS((int)dtmout);
2484
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002485 /* Check if we run on the same thread than the xreator thread.
2486 * We cannot access to the socket if the thread is different.
2487 */
2488 if (socket->tid != tid)
2489 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2490
Mark Lakes56cc1252018-03-27 09:48:06 +02002491 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002492 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002493 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002494 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2495 WILL_LJMP(lua_error(L));
2496 return 0;
2497 }
2498 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2499 si = appctx->owner;
2500 s = si_strm(si);
2501
2502 s->req.rto = tmout;
2503 s->req.wto = tmout;
2504 s->res.rto = tmout;
2505 s->res.wto = tmout;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002506 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002507
Thierry Fourniere9636f12018-03-08 09:54:32 +01002508 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002509 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002510}
2511
2512__LJMP static int hlua_socket_new(lua_State *L)
2513{
2514 struct hlua_socket *socket;
2515 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002516 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002517 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002518
2519 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002520 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002521 hlua_pusherror(L, "socket: full stack");
2522 goto out_fail_conf;
2523 }
2524
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002525 /* Create the object: obj[0] = userdata. */
2526 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002527 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002528 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002529 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002530 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002531
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002532 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002533 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002534 hlua_pusherror(L, "socket: uninitialized pools.");
2535 goto out_fail_conf;
2536 }
2537
Willy Tarreau87b09662015-04-03 00:22:06 +02002538 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002539 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2540 lua_setmetatable(L, -2);
2541
Willy Tarreaud420a972015-04-06 00:39:18 +02002542 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002543 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002544 if (!appctx) {
2545 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002546 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002547 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002548
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002549 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002550 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002551 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2552 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002553
Willy Tarreaud420a972015-04-06 00:39:18 +02002554 /* Now create a session, task and stream for this applet */
2555 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2556 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002557 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002558 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002559 }
2560
Willy Tarreau87787ac2017-08-28 16:22:54 +02002561 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002562 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002563 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002564 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002565 }
2566
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002567 /* Initialise cross reference between stream and Lua socket object. */
2568 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002569
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002570 /* Configure "right" stream interface. this "si" is used to connect
2571 * and retrieve data from the server. The connection is initialized
2572 * with the "struct server".
2573 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002574 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002575
2576 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002577 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2578 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002579
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002580 return 1;
2581
Willy Tarreaud420a972015-04-06 00:39:18 +02002582 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002583 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002584 out_fail_sess:
2585 appctx_free(appctx);
2586 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002587 WILL_LJMP(lua_error(L));
2588 return 0;
2589}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002590
2591/*
2592 *
2593 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002594 * Class Channel
2595 *
2596 *
2597 */
2598
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002599/* The state between the channel data and the HTTP parser state can be
2600 * unconsistent, so reset the parser and call it again. Warning, this
2601 * action not revalidate the request and not send a 400 if the modified
2602 * resuest is not valid.
2603 *
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002604 * This function never fails. The direction is set using dir, which equals
2605 * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002606 */
2607static void hlua_resynchonize_proto(struct stream *stream, int dir)
2608{
2609 /* Protocol HTTP. */
2610 if (stream->be->mode == PR_MODE_HTTP) {
2611
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002612 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002613 http_txn_reset_req(stream->txn);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002614 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002615 http_txn_reset_res(stream->txn);
2616
2617 if (stream->txn->hdr_idx.v)
2618 hdr_idx_init(&stream->txn->hdr_idx);
2619
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002620 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002621 http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002622 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002623 http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx);
2624 }
2625}
2626
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002627/* This function is called before the Lua execution. It stores
2628 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002629 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002630static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002631{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002632 c->mode = stream->be->mode;
2633 switch (c->mode) {
2634 case PR_MODE_HTTP:
2635 c->data.http.dir = opt & SMP_OPT_DIR;
2636 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2637 c->data.http.state = stream->txn->req.msg_state;
2638 else
2639 c->data.http.state = stream->txn->rsp.msg_state;
2640 break;
2641 default:
2642 break;
2643 }
2644}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002645
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002646/* This function is called after the Lua execution. it
2647 * returns true if the parser state is consistent, otherwise,
2648 * it return false.
2649 *
2650 * In HTTP mode, the parser state must be in the same state
2651 * or greater when we exit the function. Even if we do a
2652 * control yield. This prevent to break the HTTP message
2653 * from the Lua code.
2654 */
2655static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2656{
2657 if (c->mode != stream->be->mode)
2658 return 0;
2659
2660 switch (c->mode) {
2661 case PR_MODE_HTTP:
2662 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002663 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002664 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2665 return stream->txn->req.msg_state >= c->data.http.state;
2666 else
2667 return stream->txn->rsp.msg_state >= c->data.http.state;
2668 default:
2669 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002670 }
2671 return 1;
2672}
2673
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002674/* Returns the struct hlua_channel join to the class channel in the
2675 * stack entry "ud" or throws an argument error.
2676 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002677__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002678{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002679 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002680}
2681
Willy Tarreau47860ed2015-03-10 14:07:50 +01002682/* Pushes the channel onto the top of the stack. If the stask does not have a
2683 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002684 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002685static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002686{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002687 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002688 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002689 return 0;
2690
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002691 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002692 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002693 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002694
2695 /* Pop a class sesison metatable and affect it to the userdata. */
2696 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2697 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002698 return 1;
2699}
2700
2701/* Duplicate all the data present in the input channel and put it
2702 * in a string LUA variables. Returns -1 and push a nil value in
2703 * the stack if the channel is closed and all the data are consumed,
2704 * returns 0 if no data are available, otherwise it returns the length
2705 * of the builded string.
2706 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002707static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002708{
2709 char *blk1;
2710 char *blk2;
2711 int len1;
2712 int len2;
2713 int ret;
2714 luaL_Buffer b;
2715
Willy Tarreau06d80a92017-10-19 14:32:15 +02002716 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002717 if (unlikely(ret == 0))
2718 return 0;
2719
2720 if (unlikely(ret < 0)) {
2721 lua_pushnil(L);
2722 return -1;
2723 }
2724
2725 luaL_buffinit(L, &b);
2726 luaL_addlstring(&b, blk1, len1);
2727 if (unlikely(ret == 2))
2728 luaL_addlstring(&b, blk2, len2);
2729 luaL_pushresult(&b);
2730
2731 if (unlikely(ret == 2))
2732 return len1 + len2;
2733 return len1;
2734}
2735
2736/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2737 * a yield. This function keep the data in the buffer.
2738 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002739__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002740{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002741 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002742
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002743 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2744
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002745 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002746 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002747 return 1;
2748}
2749
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002750/* Check arguments for the function "hlua_channel_dup_yield". */
2751__LJMP static int hlua_channel_dup(lua_State *L)
2752{
2753 MAY_LJMP(check_args(L, 1, "dup"));
2754 MAY_LJMP(hlua_checkchannel(L, 1));
2755 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2756}
2757
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002758/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2759 * a yield. This function consumes the data in the buffer. It returns
2760 * a string containing the data or a nil pointer if no data are available
2761 * and the channel is closed.
2762 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002763__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002764{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002765 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002766 int ret;
2767
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002768 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002769
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002770 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002771 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002772 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002773
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002774 if (unlikely(ret == -1))
2775 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002776
Willy Tarreau47860ed2015-03-10 14:07:50 +01002777 chn->buf->i -= ret;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002778 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002779 return 1;
2780}
2781
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002782/* Check arguments for the fucntion "hlua_channel_get_yield". */
2783__LJMP static int hlua_channel_get(lua_State *L)
2784{
2785 MAY_LJMP(check_args(L, 1, "get"));
2786 MAY_LJMP(hlua_checkchannel(L, 1));
2787 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2788}
2789
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002790/* This functions consumes and returns one line. If the channel is closed,
2791 * and the last data does not contains a final '\n', the data are returned
2792 * without the final '\n'. When no more data are avalaible, it returns nil
2793 * value.
2794 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002795__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002796{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002797 char *blk1;
2798 char *blk2;
2799 int len1;
2800 int len2;
2801 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002802 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002803 int ret;
2804 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002805
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002806 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2807
Willy Tarreau06d80a92017-10-19 14:32:15 +02002808 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002809 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002810 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002811
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002812 if (ret == -1) {
2813 lua_pushnil(L);
2814 return 1;
2815 }
2816
2817 luaL_buffinit(L, &b);
2818 luaL_addlstring(&b, blk1, len1);
2819 len = len1;
2820 if (unlikely(ret == 2)) {
2821 luaL_addlstring(&b, blk2, len2);
2822 len += len2;
2823 }
2824 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002825 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002826 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002827 return 1;
2828}
2829
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002830/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2831__LJMP static int hlua_channel_getline(lua_State *L)
2832{
2833 MAY_LJMP(check_args(L, 1, "getline"));
2834 MAY_LJMP(hlua_checkchannel(L, 1));
2835 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2836}
2837
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002838/* This function takes a string as input, and append it at the
2839 * input side of channel. If the data is too big, but a space
2840 * is probably available after sending some data, the function
2841 * yield. If the data is bigger than the buffer, or if the
2842 * channel is closed, it returns -1. otherwise, it returns the
2843 * amount of data writed.
2844 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002845__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002846{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002847 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002848 size_t len;
2849 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2850 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2851 int ret;
2852 int max;
2853
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002854 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2855 * the request buffer if its not required.
2856 */
2857 if (chn->buf->size == 0) {
2858 si_applet_cant_put(chn_prod(chn));
2859 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
2860 }
2861
Willy Tarreau47860ed2015-03-10 14:07:50 +01002862 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002863 if (max > len - l)
2864 max = len - l;
2865
Willy Tarreau06d80a92017-10-19 14:32:15 +02002866 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002867 if (ret == -2 || ret == -3) {
2868 lua_pushinteger(L, -1);
2869 return 1;
2870 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002871 if (ret == -1) {
2872 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002873 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002874 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002875 l += ret;
2876 lua_pop(L, 1);
2877 lua_pushinteger(L, l);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002878 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002879
Willy Tarreau47860ed2015-03-10 14:07:50 +01002880 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2881 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002882 /* There are no space avalaible, and the output buffer is empty.
2883 * in this case, we cannot add more data, so we cannot yield,
2884 * we return the amount of copyied data.
2885 */
2886 return 1;
2887 }
2888 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002889 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002890 return 1;
2891}
2892
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002893/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002894 * of the writed string, or -1 if the channel is closed or if the
2895 * buffer size is too little for the data.
2896 */
2897__LJMP static int hlua_channel_append(lua_State *L)
2898{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002899 size_t len;
2900
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002902 MAY_LJMP(hlua_checkchannel(L, 1));
2903 MAY_LJMP(luaL_checklstring(L, 2, &len));
2904 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002905 lua_pushinteger(L, 0);
2906
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002907 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002908}
2909
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002910/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002911 * his process by cleaning the buffer. The result is a replacement
2912 * of the current data. It returns the length of the writed string,
2913 * or -1 if the channel is closed or if the buffer size is too
2914 * little for the data.
2915 */
2916__LJMP static int hlua_channel_set(lua_State *L)
2917{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002918 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002919
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002920 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002921 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002922 lua_pushinteger(L, 0);
2923
Willy Tarreau47860ed2015-03-10 14:07:50 +01002924 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002925
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002926 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002927}
2928
2929/* Append data in the output side of the buffer. This data is immediatly
2930 * sent. The fcuntion returns the ammount of data writed. If the buffer
2931 * cannot contains the data, the function yield. The function returns -1
2932 * if the channel is closed.
2933 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002934__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002935{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002936 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937 size_t len;
2938 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2939 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2940 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002941 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002942
Willy Tarreau47860ed2015-03-10 14:07:50 +01002943 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002944 lua_pushinteger(L, -1);
2945 return 1;
2946 }
2947
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002948 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2949 * the request buffer if its not required.
2950 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002951 if (chn->buf->size == 0) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002952 si_applet_cant_put(chn_prod(chn));
2953 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002954 }
2955
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002956 /* the writed data will be immediatly sent, so we can check
2957 * the avalaible space without taking in account the reserve.
2958 * The reserve is guaranted for the processing of incoming
2959 * data, because the buffer will be flushed.
2960 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002961 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002962
2963 /* If there are no space avalaible, and the output buffer is empty.
2964 * in this case, we cannot add more data, so we cannot yield,
2965 * we return the amount of copyied data.
2966 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002967 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002968 return 1;
2969
2970 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971 if (max > len - l)
2972 max = len - l;
2973
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002974 /* The buffer avalaible size may be not contiguous. This test
2975 * detects a non contiguous buffer and realign it.
2976 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002977 if (bi_space_for_replace(chn->buf) < max)
2978 buffer_slow_realign(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002979
2980 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002981 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002982
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002983 /* buffer replace considers that the input part is filled.
2984 * so, I must forward these new data in the output part.
2985 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002986 b_adv(chn->buf, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002987
2988 l += max;
2989 lua_pop(L, 1);
2990 lua_pushinteger(L, l);
2991
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002992 /* If there are no space avalaible, and the output buffer is empty.
2993 * in this case, we cannot add more data, so we cannot yield,
2994 * we return the amount of copyied data.
2995 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002996 max = chn->buf->size - buffer_len(chn->buf);
2997 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002998 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002999
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003000 if (l < len) {
3001 /* If we are waiting for space in the response buffer, we
3002 * must set the flag WAKERESWR. This flag required the task
3003 * wake up if any activity is detected on the response buffer.
3004 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003005 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003006 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003007 else
3008 HLUA_SET_WAKEREQWR(hlua);
3009 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003010 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003011
3012 return 1;
3013}
3014
3015/* Just a wraper of "_hlua_channel_send". This wrapper permits
3016 * yield the LUA process, and resume it without checking the
3017 * input arguments.
3018 */
3019__LJMP static int hlua_channel_send(lua_State *L)
3020{
3021 MAY_LJMP(check_args(L, 2, "send"));
3022 lua_pushinteger(L, 0);
3023
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003024 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003025}
3026
3027/* This function forward and amount of butes. The data pass from
3028 * the input side of the buffer to the output side, and can be
3029 * forwarded. This function never fails.
3030 *
3031 * The Lua function takes an amount of bytes to be forwarded in
3032 * imput. It returns the number of bytes forwarded.
3033 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003034__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003035{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003036 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003037 int len;
3038 int l;
3039 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003040 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003041
3042 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3043 len = MAY_LJMP(luaL_checkinteger(L, 2));
3044 l = MAY_LJMP(luaL_checkinteger(L, -1));
3045
3046 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003047 if (max > chn->buf->i)
3048 max = chn->buf->i;
3049 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003050 l += max;
3051
3052 lua_pop(L, 1);
3053 lua_pushinteger(L, l);
3054
3055 /* Check if it miss bytes to forward. */
3056 if (l < len) {
3057 /* The the input channel or the output channel are closed, we
3058 * must return the amount of data forwarded.
3059 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003060 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003061 return 1;
3062
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003063 /* If we are waiting for space data in the response buffer, we
3064 * must set the flag WAKERESWR. This flag required the task
3065 * wake up if any activity is detected on the response buffer.
3066 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003067 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003068 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003069 else
3070 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003071
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003072 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01003073 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003074 }
3075
3076 return 1;
3077}
3078
3079/* Just check the input and prepare the stack for the previous
3080 * function "hlua_channel_forward_yield"
3081 */
3082__LJMP static int hlua_channel_forward(lua_State *L)
3083{
3084 MAY_LJMP(check_args(L, 2, "forward"));
3085 MAY_LJMP(hlua_checkchannel(L, 1));
3086 MAY_LJMP(luaL_checkinteger(L, 2));
3087
3088 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003089 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003090}
3091
3092/* Just returns the number of bytes available in the input
3093 * side of the buffer. This function never fails.
3094 */
3095__LJMP static int hlua_channel_get_in_len(lua_State *L)
3096{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003097 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003098
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003099 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003100 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01003101 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003102 return 1;
3103}
3104
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003105/* Returns true if the channel is full. */
3106__LJMP static int hlua_channel_is_full(lua_State *L)
3107{
3108 struct channel *chn;
3109 int rem;
3110
3111 MAY_LJMP(check_args(L, 1, "is_full"));
3112 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3113
3114 rem = chn->buf->size;
3115 rem -= chn->buf->o; /* Output size */
3116 rem -= chn->buf->i; /* Input size */
3117 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3118
3119 lua_pushboolean(L, rem <= 0);
3120 return 1;
3121}
3122
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003123/* Just returns the number of bytes available in the output
3124 * side of the buffer. This function never fails.
3125 */
3126__LJMP static int hlua_channel_get_out_len(lua_State *L)
3127{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003128 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003129
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003130 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003131 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01003132 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003133 return 1;
3134}
3135
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003136/*
3137 *
3138 *
3139 * Class Fetches
3140 *
3141 *
3142 */
3143
3144/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003145 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003146 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003147__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003148{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003149 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003150}
3151
3152/* This function creates and push in the stack a fetch object according
3153 * with a current TXN.
3154 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003155static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003156{
Willy Tarreau7073c472015-04-06 11:15:40 +02003157 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003158
3159 /* Check stack size. */
3160 if (!lua_checkstack(L, 3))
3161 return 0;
3162
3163 /* Create the object: obj[0] = userdata.
3164 * Note that the base of the Fetches object is the
3165 * transaction object.
3166 */
3167 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003168 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003169 lua_rawseti(L, -2, 0);
3170
Willy Tarreau7073c472015-04-06 11:15:40 +02003171 hsmp->s = txn->s;
3172 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003173 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003174 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003175
3176 /* Pop a class sesison metatable and affect it to the userdata. */
3177 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3178 lua_setmetatable(L, -2);
3179
3180 return 1;
3181}
3182
3183/* This function is an LUA binding. It is called with each sample-fetch.
3184 * It uses closure argument to store the associated sample-fetch. It
3185 * returns only one argument or throws an error. An error is thrown
3186 * only if an error is encountered during the argument parsing. If
3187 * the "sample-fetch" function fails, nil is returned.
3188 */
3189__LJMP static int hlua_run_sample_fetch(lua_State *L)
3190{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003191 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003192 struct sample_fetch *f;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003193 struct arg args[ARGM_NBARGS + 1];
3194 int i;
3195 struct sample smp;
3196
3197 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003198 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003199
3200 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003201 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003202
Thierry FOURNIERca988662015-12-20 18:43:03 +01003203 /* Check execution authorization. */
3204 if (f->use & SMP_USE_HTTP_ANY &&
3205 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3206 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3207 "is not available in Lua services", f->kw);
3208 WILL_LJMP(lua_error(L));
3209 }
3210
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003211 /* Get extra arguments. */
3212 for (i = 0; i < lua_gettop(L) - 1; i++) {
3213 if (i >= ARGM_NBARGS)
3214 break;
3215 hlua_lua2arg(L, i + 2, &args[i]);
3216 }
3217 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003218 args[i].data.str.str = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003219
3220 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003221 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003222
3223 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003224 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003225 lua_pushfstring(L, "error in arguments");
3226 WILL_LJMP(lua_error(L));
3227 }
3228
3229 /* Initialise the sample. */
3230 memset(&smp, 0, sizeof(smp));
3231
3232 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003233 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003234 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003235 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003236 lua_pushstring(L, "");
3237 else
3238 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003239 return 1;
3240 }
3241
3242 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003243 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003244 hlua_smp2lua_str(L, &smp);
3245 else
3246 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003247 return 1;
3248}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003249
3250/*
3251 *
3252 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003253 * Class Converters
3254 *
3255 *
3256 */
3257
3258/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003259 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003260 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003261__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003262{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003263 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003264}
3265
3266/* This function creates and push in the stack a Converters object
3267 * according with a current TXN.
3268 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003269static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003270{
Willy Tarreau7073c472015-04-06 11:15:40 +02003271 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003272
3273 /* Check stack size. */
3274 if (!lua_checkstack(L, 3))
3275 return 0;
3276
3277 /* Create the object: obj[0] = userdata.
3278 * Note that the base of the Converters object is the
3279 * same than the TXN object.
3280 */
3281 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003282 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003283 lua_rawseti(L, -2, 0);
3284
Willy Tarreau7073c472015-04-06 11:15:40 +02003285 hsmp->s = txn->s;
3286 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003287 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003288 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003289
Willy Tarreau87b09662015-04-03 00:22:06 +02003290 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003291 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3292 lua_setmetatable(L, -2);
3293
3294 return 1;
3295}
3296
3297/* This function is an LUA binding. It is called with each converter.
3298 * It uses closure argument to store the associated converter. It
3299 * returns only one argument or throws an error. An error is thrown
3300 * only if an error is encountered during the argument parsing. If
3301 * the converter function function fails, nil is returned.
3302 */
3303__LJMP static int hlua_run_sample_conv(lua_State *L)
3304{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003305 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003306 struct sample_conv *conv;
3307 struct arg args[ARGM_NBARGS + 1];
3308 int i;
3309 struct sample smp;
3310
3311 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003312 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003313
3314 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003315 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003316
3317 /* Get extra arguments. */
3318 for (i = 0; i < lua_gettop(L) - 2; i++) {
3319 if (i >= ARGM_NBARGS)
3320 break;
3321 hlua_lua2arg(L, i + 3, &args[i]);
3322 }
3323 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003324 args[i].data.str.str = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003325
3326 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003327 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003328
3329 /* Run the special args checker. */
3330 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3331 hlua_pusherror(L, "error in arguments");
3332 WILL_LJMP(lua_error(L));
3333 }
3334
3335 /* Initialise the sample. */
3336 if (!hlua_lua2smp(L, 2, &smp)) {
3337 hlua_pusherror(L, "error in the input argument");
3338 WILL_LJMP(lua_error(L));
3339 }
3340
Willy Tarreau1777ea62016-03-10 16:15:46 +01003341 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3342
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003343 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003344 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003345 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003346 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003347 WILL_LJMP(lua_error(L));
3348 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003349 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3350 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003351 hlua_pusherror(L, "error during the input argument casting");
3352 WILL_LJMP(lua_error(L));
3353 }
3354
3355 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003356 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003357 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003358 lua_pushstring(L, "");
3359 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003360 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003361 return 1;
3362 }
3363
3364 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003365 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003366 hlua_smp2lua_str(L, &smp);
3367 else
3368 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003369 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003370}
3371
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003372/*
3373 *
3374 *
3375 * Class AppletTCP
3376 *
3377 *
3378 */
3379
3380/* Returns a struct hlua_txn if the stack entry "ud" is
3381 * a class stream, otherwise it throws an error.
3382 */
3383__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3384{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003385 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003386}
3387
3388/* This function creates and push in the stack an Applet object
3389 * according with a current TXN.
3390 */
3391static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3392{
3393 struct hlua_appctx *appctx;
3394 struct stream_interface *si = ctx->owner;
3395 struct stream *s = si_strm(si);
3396 struct proxy *p = s->be;
3397
3398 /* Check stack size. */
3399 if (!lua_checkstack(L, 3))
3400 return 0;
3401
3402 /* Create the object: obj[0] = userdata.
3403 * Note that the base of the Converters object is the
3404 * same than the TXN object.
3405 */
3406 lua_newtable(L);
3407 appctx = lua_newuserdata(L, sizeof(*appctx));
3408 lua_rawseti(L, -2, 0);
3409 appctx->appctx = ctx;
3410 appctx->htxn.s = s;
3411 appctx->htxn.p = p;
3412
3413 /* Create the "f" field that contains a list of fetches. */
3414 lua_pushstring(L, "f");
3415 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3416 return 0;
3417 lua_settable(L, -3);
3418
3419 /* Create the "sf" field that contains a list of stringsafe fetches. */
3420 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003421 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003422 return 0;
3423 lua_settable(L, -3);
3424
3425 /* Create the "c" field that contains a list of converters. */
3426 lua_pushstring(L, "c");
3427 if (!hlua_converters_new(L, &appctx->htxn, 0))
3428 return 0;
3429 lua_settable(L, -3);
3430
3431 /* Create the "sc" field that contains a list of stringsafe converters. */
3432 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003433 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003434 return 0;
3435 lua_settable(L, -3);
3436
3437 /* Pop a class stream metatable and affect it to the table. */
3438 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3439 lua_setmetatable(L, -2);
3440
3441 return 1;
3442}
3443
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003444__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3445{
3446 struct hlua_appctx *appctx;
3447 struct stream *s;
3448 const char *name;
3449 size_t len;
3450 struct sample smp;
3451
3452 MAY_LJMP(check_args(L, 3, "set_var"));
3453
3454 /* It is useles to retrieve the stream, but this function
3455 * runs only in a stream context.
3456 */
3457 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3458 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3459 s = appctx->htxn.s;
3460
3461 /* Converts the third argument in a sample. */
3462 hlua_lua2smp(L, 3, &smp);
3463
3464 /* Store the sample in a variable. */
3465 smp_set_owner(&smp, s->be, s->sess, s, 0);
3466 vars_set_by_name(name, len, &smp);
3467 return 0;
3468}
3469
3470__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3471{
3472 struct hlua_appctx *appctx;
3473 struct stream *s;
3474 const char *name;
3475 size_t len;
3476 struct sample smp;
3477
3478 MAY_LJMP(check_args(L, 2, "unset_var"));
3479
3480 /* It is useles to retrieve the stream, but this function
3481 * runs only in a stream context.
3482 */
3483 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3484 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3485 s = appctx->htxn.s;
3486
3487 /* Unset the variable. */
3488 smp_set_owner(&smp, s->be, s->sess, s, 0);
3489 vars_unset_by_name(name, len, &smp);
3490 return 0;
3491}
3492
3493__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3494{
3495 struct hlua_appctx *appctx;
3496 struct stream *s;
3497 const char *name;
3498 size_t len;
3499 struct sample smp;
3500
3501 MAY_LJMP(check_args(L, 2, "get_var"));
3502
3503 /* It is useles to retrieve the stream, but this function
3504 * runs only in a stream context.
3505 */
3506 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3507 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3508 s = appctx->htxn.s;
3509
3510 smp_set_owner(&smp, s->be, s->sess, s, 0);
3511 if (!vars_get_by_name(name, len, &smp)) {
3512 lua_pushnil(L);
3513 return 1;
3514 }
3515
3516 return hlua_smp2lua(L, &smp);
3517}
3518
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003519__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3520{
3521 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3522 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003523 struct hlua *hlua;
3524
3525 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003526 if (!s->hlua)
3527 return 0;
3528 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003529
3530 MAY_LJMP(check_args(L, 2, "set_priv"));
3531
3532 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003533 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003534
3535 /* Get and store new value. */
3536 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3537 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3538
3539 return 0;
3540}
3541
3542__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3543{
3544 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3545 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003546 struct hlua *hlua;
3547
3548 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003549 if (!s->hlua) {
3550 lua_pushnil(L);
3551 return 1;
3552 }
3553 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003554
3555 /* Push configuration index in the stack. */
3556 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3557
3558 return 1;
3559}
3560
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003561/* If expected data not yet available, it returns a yield. This function
3562 * consumes the data in the buffer. It returns a string containing the
3563 * data. This string can be empty.
3564 */
3565__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3566{
3567 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3568 struct stream_interface *si = appctx->appctx->owner;
3569 int ret;
3570 char *blk1;
3571 int len1;
3572 char *blk2;
3573 int len2;
3574
3575 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003576 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003577
3578 /* Data not yet avalaible. return yield. */
3579 if (ret == 0) {
3580 si_applet_cant_get(si);
3581 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
3582 }
3583
3584 /* End of data: commit the total strings and return. */
3585 if (ret < 0) {
3586 luaL_pushresult(&appctx->b);
3587 return 1;
3588 }
3589
3590 /* Ensure that the block 2 length is usable. */
3591 if (ret == 1)
3592 len2 = 0;
3593
3594 /* dont check the max length read and dont check. */
3595 luaL_addlstring(&appctx->b, blk1, len1);
3596 luaL_addlstring(&appctx->b, blk2, len2);
3597
3598 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003599 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003600 luaL_pushresult(&appctx->b);
3601 return 1;
3602}
3603
3604/* Check arguments for the fucntion "hlua_channel_get_yield". */
3605__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3606{
3607 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3608
3609 /* Initialise the string catenation. */
3610 luaL_buffinit(L, &appctx->b);
3611
3612 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3613}
3614
3615/* If expected data not yet available, it returns a yield. This function
3616 * consumes the data in the buffer. It returns a string containing the
3617 * data. This string can be empty.
3618 */
3619__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3620{
3621 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3622 struct stream_interface *si = appctx->appctx->owner;
3623 int len = MAY_LJMP(luaL_checkinteger(L, 2));
3624 int ret;
3625 char *blk1;
3626 int len1;
3627 char *blk2;
3628 int len2;
3629
3630 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003631 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003632
3633 /* Data not yet avalaible. return yield. */
3634 if (ret == 0) {
3635 si_applet_cant_get(si);
3636 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3637 }
3638
3639 /* End of data: commit the total strings and return. */
3640 if (ret < 0) {
3641 luaL_pushresult(&appctx->b);
3642 return 1;
3643 }
3644
3645 /* Ensure that the block 2 length is usable. */
3646 if (ret == 1)
3647 len2 = 0;
3648
3649 if (len == -1) {
3650
3651 /* If len == -1, catenate all the data avalaile and
3652 * yield because we want to get all the data until
3653 * the end of data stream.
3654 */
3655 luaL_addlstring(&appctx->b, blk1, len1);
3656 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003657 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003658 si_applet_cant_get(si);
3659 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3660
3661 } else {
3662
3663 /* Copy the fisrt block caping to the length required. */
3664 if (len1 > len)
3665 len1 = len;
3666 luaL_addlstring(&appctx->b, blk1, len1);
3667 len -= len1;
3668
3669 /* Copy the second block. */
3670 if (len2 > len)
3671 len2 = len;
3672 luaL_addlstring(&appctx->b, blk2, len2);
3673 len -= len2;
3674
3675 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003676 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003677
3678 /* If we are no other data avalaible, yield waiting for new data. */
3679 if (len > 0) {
3680 lua_pushinteger(L, len);
3681 lua_replace(L, 2);
3682 si_applet_cant_get(si);
3683 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3684 }
3685
3686 /* return the result. */
3687 luaL_pushresult(&appctx->b);
3688 return 1;
3689 }
3690
3691 /* we never executes this */
3692 hlua_pusherror(L, "Lua: internal error");
3693 WILL_LJMP(lua_error(L));
3694 return 0;
3695}
3696
3697/* Check arguments for the fucntion "hlua_channel_get_yield". */
3698__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3699{
3700 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3701 int len = -1;
3702
3703 if (lua_gettop(L) > 2)
3704 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3705 if (lua_gettop(L) >= 2) {
3706 len = MAY_LJMP(luaL_checkinteger(L, 2));
3707 lua_pop(L, 1);
3708 }
3709
3710 /* Confirm or set the required length */
3711 lua_pushinteger(L, len);
3712
3713 /* Initialise the string catenation. */
3714 luaL_buffinit(L, &appctx->b);
3715
3716 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3717}
3718
3719/* Append data in the output side of the buffer. This data is immediatly
3720 * sent. The fcuntion returns the ammount of data writed. If the buffer
3721 * cannot contains the data, the function yield. The function returns -1
3722 * if the channel is closed.
3723 */
3724__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3725{
3726 size_t len;
3727 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3728 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3729 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3730 struct stream_interface *si = appctx->appctx->owner;
3731 struct channel *chn = si_ic(si);
3732 int max;
3733
3734 /* Get the max amount of data which can write as input in the channel. */
3735 max = channel_recv_max(chn);
3736 if (max > (len - l))
3737 max = len - l;
3738
3739 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003740 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003741
3742 /* update counters. */
3743 l += max;
3744 lua_pop(L, 1);
3745 lua_pushinteger(L, l);
3746
3747 /* If some data is not send, declares the situation to the
3748 * applet, and returns a yield.
3749 */
3750 if (l < len) {
3751 si_applet_cant_put(si);
3752 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
3753 }
3754
3755 return 1;
3756}
3757
3758/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3759 * yield the LUA process, and resume it without checking the
3760 * input arguments.
3761 */
3762__LJMP static int hlua_applet_tcp_send(lua_State *L)
3763{
3764 MAY_LJMP(check_args(L, 2, "send"));
3765 lua_pushinteger(L, 0);
3766
3767 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3768}
3769
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003770/*
3771 *
3772 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003773 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003774 *
3775 *
3776 */
3777
3778/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003779 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003780 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003781__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003782{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003783 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003784}
3785
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003786/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003787 * according with a current TXN.
3788 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003789static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003790{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003791 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003792 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003793 struct stream_interface *si = ctx->owner;
3794 struct stream *s = si_strm(si);
3795 struct proxy *px = s->be;
3796 struct http_txn *txn = s->txn;
3797 const char *path;
3798 const char *end;
3799 const char *p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003800
3801 /* Check stack size. */
3802 if (!lua_checkstack(L, 3))
3803 return 0;
3804
3805 /* Create the object: obj[0] = userdata.
3806 * Note that the base of the Converters object is the
3807 * same than the TXN object.
3808 */
3809 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003810 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003811 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003812 appctx->appctx = ctx;
3813 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003814 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003815 appctx->htxn.s = s;
3816 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003817
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003818 /* Create the "f" field that contains a list of fetches. */
3819 lua_pushstring(L, "f");
3820 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3821 return 0;
3822 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003823
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003824 /* Create the "sf" field that contains a list of stringsafe fetches. */
3825 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003826 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003827 return 0;
3828 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003829
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003830 /* Create the "c" field that contains a list of converters. */
3831 lua_pushstring(L, "c");
3832 if (!hlua_converters_new(L, &appctx->htxn, 0))
3833 return 0;
3834 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003835
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003836 /* Create the "sc" field that contains a list of stringsafe converters. */
3837 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003838 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003839 return 0;
3840 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003841
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003842 /* Stores the request method. */
3843 lua_pushstring(L, "method");
3844 lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l);
3845 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003846
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003847 /* Stores the http version. */
3848 lua_pushstring(L, "version");
3849 lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
3850 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003851
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003852 /* creates an array of headers. hlua_http_get_headers() crates and push
3853 * the array on the top of the stack.
3854 */
3855 lua_pushstring(L, "headers");
3856 htxn.s = s;
3857 htxn.p = px;
3858 htxn.dir = SMP_OPT_DIR_REQ;
3859 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3860 return 0;
3861 lua_settable(L, -3);
3862
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003863 /* Get path and qs */
3864 path = http_get_path(txn);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003865 if (path) {
3866 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
3867 p = path;
3868 while (p < end && *p != '?')
3869 p++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003870
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003871 /* Stores the request path. */
3872 lua_pushstring(L, "path");
3873 lua_pushlstring(L, path, p - path);
3874 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003875
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003876 /* Stores the query string. */
3877 lua_pushstring(L, "qs");
3878 if (*p == '?')
3879 p++;
3880 lua_pushlstring(L, p, end - p);
3881 lua_settable(L, -3);
3882 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003883
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003884 /* Stores the request path. */
3885 lua_pushstring(L, "length");
3886 lua_pushinteger(L, txn->req.body_len);
3887 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003888
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003889 /* Create an array of HTTP request headers. */
3890 lua_pushstring(L, "headers");
3891 MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req));
3892 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003893
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003894 /* Create an empty array of HTTP request headers. */
3895 lua_pushstring(L, "response");
3896 lua_newtable(L);
3897 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003898
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003899 /* Pop a class stream metatable and affect it to the table. */
3900 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3901 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003902
3903 return 1;
3904}
3905
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003906__LJMP static int hlua_applet_http_set_var(lua_State *L)
3907{
3908 struct hlua_appctx *appctx;
3909 struct stream *s;
3910 const char *name;
3911 size_t len;
3912 struct sample smp;
3913
3914 MAY_LJMP(check_args(L, 3, "set_var"));
3915
3916 /* It is useles to retrieve the stream, but this function
3917 * runs only in a stream context.
3918 */
3919 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3920 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3921 s = appctx->htxn.s;
3922
3923 /* Converts the third argument in a sample. */
3924 hlua_lua2smp(L, 3, &smp);
3925
3926 /* Store the sample in a variable. */
3927 smp_set_owner(&smp, s->be, s->sess, s, 0);
3928 vars_set_by_name(name, len, &smp);
3929 return 0;
3930}
3931
3932__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3933{
3934 struct hlua_appctx *appctx;
3935 struct stream *s;
3936 const char *name;
3937 size_t len;
3938 struct sample smp;
3939
3940 MAY_LJMP(check_args(L, 2, "unset_var"));
3941
3942 /* It is useles to retrieve the stream, but this function
3943 * runs only in a stream context.
3944 */
3945 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3946 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3947 s = appctx->htxn.s;
3948
3949 /* Unset the variable. */
3950 smp_set_owner(&smp, s->be, s->sess, s, 0);
3951 vars_unset_by_name(name, len, &smp);
3952 return 0;
3953}
3954
3955__LJMP static int hlua_applet_http_get_var(lua_State *L)
3956{
3957 struct hlua_appctx *appctx;
3958 struct stream *s;
3959 const char *name;
3960 size_t len;
3961 struct sample smp;
3962
3963 MAY_LJMP(check_args(L, 2, "get_var"));
3964
3965 /* It is useles to retrieve the stream, but this function
3966 * runs only in a stream context.
3967 */
3968 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3969 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3970 s = appctx->htxn.s;
3971
3972 smp_set_owner(&smp, s->be, s->sess, s, 0);
3973 if (!vars_get_by_name(name, len, &smp)) {
3974 lua_pushnil(L);
3975 return 1;
3976 }
3977
3978 return hlua_smp2lua(L, &smp);
3979}
3980
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003981__LJMP static int hlua_applet_http_set_priv(lua_State *L)
3982{
3983 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3984 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003985 struct hlua *hlua;
3986
3987 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003988 if (!s->hlua)
3989 return 0;
3990 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003991
3992 MAY_LJMP(check_args(L, 2, "set_priv"));
3993
3994 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003995 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003996
3997 /* Get and store new value. */
3998 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3999 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4000
4001 return 0;
4002}
4003
4004__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4005{
4006 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4007 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004008 struct hlua *hlua;
4009
4010 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004011 if (!s->hlua) {
4012 lua_pushnil(L);
4013 return 1;
4014 }
4015 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004016
4017 /* Push configuration index in the stack. */
4018 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4019
4020 return 1;
4021}
4022
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004023/* If expected data not yet available, it returns a yield. This function
4024 * consumes the data in the buffer. It returns a string containing the
4025 * data. This string can be empty.
4026 */
4027__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004028{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004029 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4030 struct stream_interface *si = appctx->appctx->owner;
4031 struct channel *chn = si_ic(si);
4032 int ret;
4033 char *blk1;
4034 int len1;
4035 char *blk2;
4036 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004037
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004038 /* Maybe we cant send a 100-continue ? */
4039 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004040 ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004041 /* if ret == -2 or -3 the channel closed or the message si too
4042 * big for the buffers. We cant send anything. So, we ignoring
4043 * the error, considers that the 100-continue is sent, and try
4044 * to receive.
4045 * If ret is -1, we dont have room in the buffer, so we yield.
4046 */
4047 if (ret == -1) {
4048 si_applet_cant_put(si);
4049 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
4050 }
4051 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4052 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004053
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004054 /* Check for the end of the data. */
4055 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4056 luaL_pushresult(&appctx->b);
4057 return 1;
4058 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004059
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004060 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004061 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004062
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004063 /* Data not yet avalaible. return yield. */
4064 if (ret == 0) {
4065 si_applet_cant_get(si);
4066 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
4067 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004068
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004069 /* End of data: commit the total strings and return. */
4070 if (ret < 0) {
4071 luaL_pushresult(&appctx->b);
4072 return 1;
4073 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004074
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004075 /* Ensure that the block 2 length is usable. */
4076 if (ret == 1)
4077 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004078
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004079 /* Copy the fisrt block caping to the length required. */
4080 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4081 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4082 luaL_addlstring(&appctx->b, blk1, len1);
4083 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004084
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004085 /* Copy the second block. */
4086 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4087 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4088 luaL_addlstring(&appctx->b, blk2, len2);
4089 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004090
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004091 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004092 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004093 luaL_pushresult(&appctx->b);
4094 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004095}
4096
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004097/* Check arguments for the fucntion "hlua_channel_get_yield". */
4098__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004099{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004100 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004101
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004102 /* Initialise the string catenation. */
4103 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004104
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004105 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004106}
4107
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004108/* If expected data not yet available, it returns a yield. This function
4109 * consumes the data in the buffer. It returns a string containing the
4110 * data. This string can be empty.
4111 */
4112__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004113{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004114 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4115 struct stream_interface *si = appctx->appctx->owner;
4116 int len = MAY_LJMP(luaL_checkinteger(L, 2));
4117 struct channel *chn = si_ic(si);
4118 int ret;
4119 char *blk1;
4120 int len1;
4121 char *blk2;
4122 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004123
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004124 /* Maybe we cant send a 100-continue ? */
4125 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004126 ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004127 /* if ret == -2 or -3 the channel closed or the message si too
4128 * big for the buffers. We cant send anything. So, we ignoring
4129 * the error, considers that the 100-continue is sent, and try
4130 * to receive.
4131 * If ret is -1, we dont have room in the buffer, so we yield.
4132 */
4133 if (ret == -1) {
4134 si_applet_cant_put(si);
4135 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4136 }
4137 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4138 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004139
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004140 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004141 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004142
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004143 /* Data not yet avalaible. return yield. */
4144 if (ret == 0) {
4145 si_applet_cant_get(si);
4146 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4147 }
4148
4149 /* End of data: commit the total strings and return. */
4150 if (ret < 0) {
4151 luaL_pushresult(&appctx->b);
4152 return 1;
4153 }
4154
4155 /* Ensure that the block 2 length is usable. */
4156 if (ret == 1)
4157 len2 = 0;
4158
4159 /* Copy the fisrt block caping to the length required. */
4160 if (len1 > len)
4161 len1 = len;
4162 luaL_addlstring(&appctx->b, blk1, len1);
4163 len -= len1;
4164
4165 /* Copy the second block. */
4166 if (len2 > len)
4167 len2 = len;
4168 luaL_addlstring(&appctx->b, blk2, len2);
4169 len -= len2;
4170
4171 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004172 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004173 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4174 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4175
4176 /* If we are no other data avalaible, yield waiting for new data. */
4177 if (len > 0) {
4178 lua_pushinteger(L, len);
4179 lua_replace(L, 2);
4180 si_applet_cant_get(si);
4181 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4182 }
4183
4184 /* return the result. */
4185 luaL_pushresult(&appctx->b);
4186 return 1;
4187}
4188
4189/* Check arguments for the fucntion "hlua_channel_get_yield". */
4190__LJMP static int hlua_applet_http_recv(lua_State *L)
4191{
4192 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4193 int len = -1;
4194
4195 /* Check arguments. */
4196 if (lua_gettop(L) > 2)
4197 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4198 if (lua_gettop(L) >= 2) {
4199 len = MAY_LJMP(luaL_checkinteger(L, 2));
4200 lua_pop(L, 1);
4201 }
4202
4203 /* Check the required length */
4204 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4205 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4206 lua_pushinteger(L, len);
4207
4208 /* Initialise the string catenation. */
4209 luaL_buffinit(L, &appctx->b);
4210
4211 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4212}
4213
4214/* Append data in the output side of the buffer. This data is immediatly
4215 * sent. The fcuntion returns the ammount of data writed. If the buffer
4216 * cannot contains the data, the function yield. The function returns -1
4217 * if the channel is closed.
4218 */
4219__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4220{
4221 size_t len;
4222 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4223 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4224 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4225 struct stream_interface *si = appctx->appctx->owner;
4226 struct channel *chn = si_ic(si);
4227 int max;
4228
4229 /* Get the max amount of data which can write as input in the channel. */
4230 max = channel_recv_max(chn);
4231 if (max > (len - l))
4232 max = len - l;
4233
4234 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004235 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004236
4237 /* update counters. */
4238 l += max;
4239 lua_pop(L, 1);
4240 lua_pushinteger(L, l);
4241
4242 /* If some data is not send, declares the situation to the
4243 * applet, and returns a yield.
4244 */
4245 if (l < len) {
4246 si_applet_cant_put(si);
4247 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
4248 }
4249
4250 return 1;
4251}
4252
4253/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4254 * yield the LUA process, and resume it without checking the
4255 * input arguments.
4256 */
4257__LJMP static int hlua_applet_http_send(lua_State *L)
4258{
4259 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4260 size_t len;
4261 char hex[10];
4262
4263 MAY_LJMP(luaL_checklstring(L, 2, &len));
4264
4265 /* If transfer encoding chunked is selected, we surround the data
4266 * by chunk data.
4267 */
4268 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4269 snprintf(hex, 9, "%x", (unsigned int)len);
4270 lua_pushfstring(L, "%s\r\n", hex);
4271 lua_insert(L, 2); /* swap the last 2 entries. */
4272 lua_pushstring(L, "\r\n");
4273 lua_concat(L, 3);
4274 }
4275
4276 /* This interger is used for followinf the amount of data sent. */
4277 lua_pushinteger(L, 0);
4278
4279 /* We want to send some data. Headers must be sent. */
4280 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4281 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4282 WILL_LJMP(lua_error(L));
4283 }
4284
4285 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4286}
4287
4288__LJMP static int hlua_applet_http_addheader(lua_State *L)
4289{
4290 const char *name;
4291 int ret;
4292
4293 MAY_LJMP(hlua_checkapplet_http(L, 1));
4294 name = MAY_LJMP(luaL_checkstring(L, 2));
4295 MAY_LJMP(luaL_checkstring(L, 3));
4296
4297 /* Push in the stack the "response" entry. */
4298 ret = lua_getfield(L, 1, "response");
4299 if (ret != LUA_TTABLE) {
4300 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4301 "is expected as an array. %s found", lua_typename(L, ret));
4302 WILL_LJMP(lua_error(L));
4303 }
4304
4305 /* check if the header is already registered if it is not
4306 * the case, register it.
4307 */
4308 ret = lua_getfield(L, -1, name);
4309 if (ret == LUA_TNIL) {
4310
4311 /* Entry not found. */
4312 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4313
4314 /* Insert the new header name in the array in the top of the stack.
4315 * It left the new array in the top of the stack.
4316 */
4317 lua_newtable(L);
4318 lua_pushvalue(L, 2);
4319 lua_pushvalue(L, -2);
4320 lua_settable(L, -4);
4321
4322 } else if (ret != LUA_TTABLE) {
4323
4324 /* corruption error. */
4325 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4326 "is expected as an array. %s found", name, lua_typename(L, ret));
4327 WILL_LJMP(lua_error(L));
4328 }
4329
4330 /* Now the top od thestack is an array of values. We push
4331 * the header value as new entry.
4332 */
4333 lua_pushvalue(L, 3);
4334 ret = lua_rawlen(L, -2);
4335 lua_rawseti(L, -2, ret + 1);
4336 lua_pushboolean(L, 1);
4337 return 1;
4338}
4339
4340__LJMP static int hlua_applet_http_status(lua_State *L)
4341{
4342 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4343 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004344 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004345
4346 if (status < 100 || status > 599) {
4347 lua_pushboolean(L, 0);
4348 return 1;
4349 }
4350
4351 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004352 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004353 lua_pushboolean(L, 1);
4354 return 1;
4355}
4356
4357/* We will build the status line and the headers of the HTTP response.
4358 * We will try send at once if its not possible, we give back the hand
4359 * waiting for more room.
4360 */
4361__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4362{
4363 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4364 struct stream_interface *si = appctx->appctx->owner;
4365 struct channel *chn = si_ic(si);
4366 int ret;
4367 size_t len;
4368 const char *msg;
4369
4370 /* Get the message as the first argument on the stack. */
4371 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4372
4373 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004374 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004375
4376 /* if ret == -2 or -3 the channel closed or the message si too
4377 * big for the buffers.
4378 */
4379 if (ret == -2 || ret == -3) {
4380 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4381 WILL_LJMP(lua_error(L));
4382 }
4383
4384 /* If ret is -1, we dont have room in the buffer, so we yield. */
4385 if (ret == -1) {
4386 si_applet_cant_put(si);
4387 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
4388 }
4389
4390 /* Headers sent, set the flag. */
4391 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4392 return 0;
4393}
4394
4395__LJMP static int hlua_applet_http_start_response(lua_State *L)
4396{
4397 struct chunk *tmp = get_trash_chunk();
4398 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004399 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02004400 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004401 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02004402 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004403 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004404 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004405 int hdr_chunked = 0;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004406 const char *reason = appctx->appctx->ctx.hlua_apphttp.reason;
4407
4408 if (reason == NULL)
4409 reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004410
4411 /* Use the same http version than the request. */
4412 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004413 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004414 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004415 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004416
4417 /* Get the array associated to the field "response" in the object AppletHTTP. */
4418 lua_pushvalue(L, 0);
4419 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4420 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4421 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4422 WILL_LJMP(lua_error(L));
4423 }
4424
4425 /* Browse the list of headers. */
4426 lua_pushnil(L);
4427 while(lua_next(L, -2) != 0) {
4428
4429 /* We expect a string as -2. */
4430 if (lua_type(L, -2) != LUA_TSTRING) {
4431 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4432 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4433 lua_typename(L, lua_type(L, -2)));
4434 WILL_LJMP(lua_error(L));
4435 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004436 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004437
4438 /* We expect an array as -1. */
4439 if (lua_type(L, -1) != LUA_TTABLE) {
4440 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4441 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4442 name,
4443 lua_typename(L, lua_type(L, -1)));
4444 WILL_LJMP(lua_error(L));
4445 }
4446
4447 /* Browse the table who is on the top of the stack. */
4448 lua_pushnil(L);
4449 while(lua_next(L, -2) != 0) {
4450
4451 /* We expect a number as -2. */
4452 if (lua_type(L, -2) != LUA_TNUMBER) {
4453 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4454 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4455 name,
4456 lua_typename(L, lua_type(L, -2)));
4457 WILL_LJMP(lua_error(L));
4458 }
4459 id = lua_tointeger(L, -2);
4460
4461 /* We expect a string as -2. */
4462 if (lua_type(L, -1) != LUA_TSTRING) {
4463 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4464 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4465 name, id,
4466 lua_typename(L, lua_type(L, -1)));
4467 WILL_LJMP(lua_error(L));
4468 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004469 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004470
4471 /* Catenate a new header. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004472 if (tmp->len + name_len + 2 + value_len + 2 < tmp->size) {
4473 memcpy(tmp->str + tmp->len, name, name_len);
4474 tmp->len += name_len;
4475 tmp->str[tmp->len++] = ':';
4476 tmp->str[tmp->len++] = ' ';
4477
4478 memcpy(tmp->str + tmp->len, value, value_len);
4479 tmp->len += value_len;
4480 tmp->str[tmp->len++] = '\r';
4481 tmp->str[tmp->len++] = '\n';
4482 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004483
4484 /* Protocol checks. */
4485
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004486 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004487 * is done without control. If it contains a bad value,
4488 * the content-length remains negative so that we can
4489 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004490 */
Willy Tarreaua3294632017-08-23 11:24:47 +02004491 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004492 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004493
4494 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004495 if (name_len == 17 && value_len == 7 &&
4496 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004497 strcasecmp("chunked", value) == 0)
4498 hdr_chunked = 1;
4499
4500 /* Remove the array from the stack, and get next element with a remaining string. */
4501 lua_pop(L, 1);
4502 }
4503
4504 /* Remove the array from the stack, and get next element with a remaining string. */
4505 lua_pop(L, 1);
4506 }
4507
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004508 /* If we dont have a content-length set, and the HTTP version is 1.1
4509 * and the status code implies the presence of a message body, we must
4510 * announce a transfer encoding chunked. This is required by haproxy
4511 * for the keepalive compliance. If the applet annouces a transfer-encoding
4512 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004513 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004514 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004515 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
4516 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4517 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4518 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004519 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
4520 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
4521 }
4522
4523 /* Finalize headers. */
4524 chunk_appendf(tmp, "\r\n");
4525
4526 /* Remove the last entry and the array of headers */
4527 lua_pop(L, 2);
4528
4529 /* Push the headers block. */
4530 lua_pushlstring(L, tmp->str, tmp->len);
4531
4532 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
4533}
4534
4535/*
4536 *
4537 *
4538 * Class HTTP
4539 *
4540 *
4541 */
4542
4543/* Returns a struct hlua_txn if the stack entry "ud" is
4544 * a class stream, otherwise it throws an error.
4545 */
4546__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
4547{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004548 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004549}
4550
4551/* This function creates and push in the stack a HTTP object
4552 * according with a current TXN.
4553 */
4554static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4555{
4556 struct hlua_txn *htxn;
4557
4558 /* Check stack size. */
4559 if (!lua_checkstack(L, 3))
4560 return 0;
4561
4562 /* Create the object: obj[0] = userdata.
4563 * Note that the base of the Converters object is the
4564 * same than the TXN object.
4565 */
4566 lua_newtable(L);
4567 htxn = lua_newuserdata(L, sizeof(*htxn));
4568 lua_rawseti(L, -2, 0);
4569
4570 htxn->s = txn->s;
4571 htxn->p = txn->p;
4572
4573 /* Pop a class stream metatable and affect it to the table. */
4574 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4575 lua_setmetatable(L, -2);
4576
4577 return 1;
4578}
4579
4580/* This function creates ans returns an array of HTTP headers.
4581 * This function does not fails. It is used as wrapper with the
4582 * 2 following functions.
4583 */
4584__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4585{
4586 const char *cur_ptr, *cur_next, *p;
4587 int old_idx, cur_idx;
4588 struct hdr_idx_elem *cur_hdr;
4589 const char *hn, *hv;
4590 int hnl, hvl;
4591 int type;
4592 const char *in;
4593 char *out;
4594 int len;
4595
4596 /* Create the table. */
4597 lua_newtable(L);
4598
4599 if (!htxn->s->txn)
4600 return 1;
4601
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004602 /* Check if a valid response is parsed */
4603 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4604 return 1;
4605
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004606 /* Build array of headers. */
4607 old_idx = 0;
4608 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
4609
4610 while (1) {
4611 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
4612 if (!cur_idx)
4613 break;
4614 old_idx = cur_idx;
4615
4616 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
4617 cur_ptr = cur_next;
4618 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
4619
4620 /* Now we have one full header at cur_ptr of len cur_hdr->len,
4621 * and the next header starts at cur_next. We'll check
4622 * this header in the list as well as against the default
4623 * rule.
4624 */
4625
4626 /* look for ': *'. */
4627 hn = cur_ptr;
4628 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
4629 if (p >= cur_ptr+cur_hdr->len)
4630 continue;
4631 hnl = p - hn;
4632 p++;
4633 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
4634 p++;
4635 if (p >= cur_ptr+cur_hdr->len)
4636 continue;
4637 hv = p;
4638 hvl = cur_ptr+cur_hdr->len-p;
4639
4640 /* Lowercase the key. Don't check the size of trash, it have
4641 * the size of one buffer and the input data contains in one
4642 * buffer.
4643 */
4644 out = trash.str;
4645 for (in=hn; in<hn+hnl; in++, out++)
4646 *out = tolower(*in);
4647 *out = '\0';
4648
4649 /* Check for existing entry:
4650 * assume that the table is on the top of the stack, and
4651 * push the key in the stack, the function lua_gettable()
4652 * perform the lookup.
4653 */
4654 lua_pushlstring(L, trash.str, hnl);
4655 lua_gettable(L, -2);
4656 type = lua_type(L, -1);
4657
4658 switch (type) {
4659 case LUA_TNIL:
4660 /* Table not found, create it. */
4661 lua_pop(L, 1); /* remove the nil value. */
4662 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
4663 lua_newtable(L); /* create and push empty table. */
4664 lua_pushlstring(L, hv, hvl); /* push header value. */
4665 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4666 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4667 break;
4668
4669 case LUA_TTABLE:
4670 /* Entry found: push the value in the table. */
4671 len = lua_rawlen(L, -1);
4672 lua_pushlstring(L, hv, hvl); /* push header value. */
4673 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4674 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4675 break;
4676
4677 default:
4678 /* Other cases are errors. */
4679 hlua_pusherror(L, "internal error during the parsing of headers.");
4680 WILL_LJMP(lua_error(L));
4681 }
4682 }
4683
4684 return 1;
4685}
4686
4687__LJMP static int hlua_http_req_get_headers(lua_State *L)
4688{
4689 struct hlua_txn *htxn;
4690
4691 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4692 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4693
4694 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4695}
4696
4697__LJMP static int hlua_http_res_get_headers(lua_State *L)
4698{
4699 struct hlua_txn *htxn;
4700
4701 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4702 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4703
4704 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4705}
4706
4707/* This function replace full header, or just a value in
4708 * the request or in the response. It is a wrapper fir the
4709 * 4 following functions.
4710 */
4711__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4712 struct http_msg *msg, int action)
4713{
4714 size_t name_len;
4715 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4716 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4717 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
4718 struct my_regex re;
4719
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004720 /* Check if a valid response is parsed */
4721 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4722 return 0;
4723
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004724 if (!regex_comp(reg, &re, 1, 1, NULL))
4725 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4726
4727 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
4728 regex_free(&re);
4729 return 0;
4730}
4731
4732__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4733{
4734 struct hlua_txn *htxn;
4735
4736 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4737 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4738
4739 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4740}
4741
4742__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4743{
4744 struct hlua_txn *htxn;
4745
4746 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4747 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4748
4749 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4750}
4751
4752__LJMP static int hlua_http_req_rep_val(lua_State *L)
4753{
4754 struct hlua_txn *htxn;
4755
4756 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4757 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4758
4759 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4760}
4761
4762__LJMP static int hlua_http_res_rep_val(lua_State *L)
4763{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004764 struct hlua_txn *htxn;
4765
4766 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4767 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4768
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004769 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004770}
4771
4772/* This function deletes all the occurences of an header.
4773 * It is a wrapper for the 2 following functions.
4774 */
4775__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4776{
4777 size_t len;
4778 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4779 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004780 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004781
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004782 /* Check if a valid response is parsed */
4783 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4784 return 0;
4785
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004786 ctx.idx = 0;
4787 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
4788 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4789 return 0;
4790}
4791
4792__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4793{
4794 struct hlua_txn *htxn;
4795
4796 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4797 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4798
Willy Tarreaueee5b512015-04-03 23:46:31 +02004799 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004800}
4801
4802__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4803{
4804 struct hlua_txn *htxn;
4805
4806 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4807 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4808
Willy Tarreaueee5b512015-04-03 23:46:31 +02004809 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004810}
4811
4812/* This function adds an header. It is a wrapper used by
4813 * the 2 following functions.
4814 */
4815__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4816{
4817 size_t name_len;
4818 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4819 size_t value_len;
4820 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
4821 char *p;
4822
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004823 /* Check if a valid message is parsed */
4824 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4825 return 0;
4826
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004827 /* Check length. */
4828 trash.len = value_len + name_len + 2;
4829 if (trash.len > trash.size)
4830 return 0;
4831
4832 /* Creates the header string. */
4833 p = trash.str;
4834 memcpy(p, name, name_len);
4835 p += name_len;
4836 *p = ':';
4837 p++;
4838 *p = ' ';
4839 p++;
4840 memcpy(p, value, value_len);
4841
Willy Tarreaueee5b512015-04-03 23:46:31 +02004842 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004843 trash.str, trash.len) != 0);
4844
4845 return 0;
4846}
4847
4848__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4849{
4850 struct hlua_txn *htxn;
4851
4852 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4853 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4854
Willy Tarreaueee5b512015-04-03 23:46:31 +02004855 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004856}
4857
4858__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4859{
4860 struct hlua_txn *htxn;
4861
4862 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4863 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4864
Willy Tarreaueee5b512015-04-03 23:46:31 +02004865 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004866}
4867
4868static int hlua_http_req_set_hdr(lua_State *L)
4869{
4870 struct hlua_txn *htxn;
4871
4872 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4873 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4874
Willy Tarreaueee5b512015-04-03 23:46:31 +02004875 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4876 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004877}
4878
4879static int hlua_http_res_set_hdr(lua_State *L)
4880{
4881 struct hlua_txn *htxn;
4882
4883 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4884 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4885
Willy Tarreaueee5b512015-04-03 23:46:31 +02004886 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4887 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004888}
4889
4890/* This function set the method. */
4891static int hlua_http_req_set_meth(lua_State *L)
4892{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004893 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004894 size_t name_len;
4895 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004896
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004897 /* Check if a valid request is parsed */
4898 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4899 lua_pushboolean(L, 0);
4900 return 1;
4901 }
4902
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004903 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004904 return 1;
4905}
4906
4907/* This function set the method. */
4908static int hlua_http_req_set_path(lua_State *L)
4909{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004910 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004911 size_t name_len;
4912 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004913
4914 /* Check if a valid request is parsed */
4915 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4916 lua_pushboolean(L, 0);
4917 return 1;
4918 }
4919
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004920 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004921 return 1;
4922}
4923
4924/* This function set the query-string. */
4925static int hlua_http_req_set_query(lua_State *L)
4926{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004927 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004928 size_t name_len;
4929 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004930
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004931 /* Check if a valid request is parsed */
4932 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4933 lua_pushboolean(L, 0);
4934 return 1;
4935 }
4936
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004937 /* Check length. */
4938 if (name_len > trash.size - 1) {
4939 lua_pushboolean(L, 0);
4940 return 1;
4941 }
4942
4943 /* Add the mark question as prefix. */
4944 chunk_reset(&trash);
4945 trash.str[trash.len++] = '?';
4946 memcpy(trash.str + trash.len, name, name_len);
4947 trash.len += name_len;
4948
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004949 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004950 return 1;
4951}
4952
4953/* This function set the uri. */
4954static int hlua_http_req_set_uri(lua_State *L)
4955{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004956 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004957 size_t name_len;
4958 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004959
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004960 /* Check if a valid request is parsed */
4961 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4962 lua_pushboolean(L, 0);
4963 return 1;
4964 }
4965
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004966 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004967 return 1;
4968}
4969
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004970/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004971static int hlua_http_res_set_status(lua_State *L)
4972{
4973 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4974 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004975 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004976
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004977 /* Check if a valid response is parsed */
4978 if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY))
4979 return 0;
4980
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004981 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004982 return 0;
4983}
4984
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004985/*
4986 *
4987 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004988 * Class TXN
4989 *
4990 *
4991 */
4992
4993/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004994 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004995 */
4996__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
4997{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004998 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004999}
5000
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005001__LJMP static int hlua_set_var(lua_State *L)
5002{
5003 struct hlua_txn *htxn;
5004 const char *name;
5005 size_t len;
5006 struct sample smp;
5007
5008 MAY_LJMP(check_args(L, 3, "set_var"));
5009
5010 /* It is useles to retrieve the stream, but this function
5011 * runs only in a stream context.
5012 */
5013 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5014 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5015
5016 /* Converts the third argument in a sample. */
5017 hlua_lua2smp(L, 3, &smp);
5018
5019 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005020 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005021 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005022 return 0;
5023}
5024
Christopher Faulet85d79c92016-11-09 16:54:56 +01005025__LJMP static int hlua_unset_var(lua_State *L)
5026{
5027 struct hlua_txn *htxn;
5028 const char *name;
5029 size_t len;
5030 struct sample smp;
5031
5032 MAY_LJMP(check_args(L, 2, "unset_var"));
5033
5034 /* It is useles to retrieve the stream, but this function
5035 * runs only in a stream context.
5036 */
5037 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5038 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5039
5040 /* Unset the variable. */
5041 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5042 vars_unset_by_name(name, len, &smp);
5043 return 0;
5044}
5045
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005046__LJMP static int hlua_get_var(lua_State *L)
5047{
5048 struct hlua_txn *htxn;
5049 const char *name;
5050 size_t len;
5051 struct sample smp;
5052
5053 MAY_LJMP(check_args(L, 2, "get_var"));
5054
5055 /* It is useles to retrieve the stream, but this function
5056 * runs only in a stream context.
5057 */
5058 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5059 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5060
Willy Tarreau7560dd42016-03-10 16:28:58 +01005061 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005062 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005063 lua_pushnil(L);
5064 return 1;
5065 }
5066
5067 return hlua_smp2lua(L, &smp);
5068}
5069
Willy Tarreau59551662015-03-10 14:23:13 +01005070__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005071{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005072 struct hlua *hlua;
5073
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005074 MAY_LJMP(check_args(L, 2, "set_priv"));
5075
Willy Tarreau87b09662015-04-03 00:22:06 +02005076 /* It is useles to retrieve the stream, but this function
5077 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005078 */
5079 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005080 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005081
5082 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005083 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005084
5085 /* Get and store new value. */
5086 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5087 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5088
5089 return 0;
5090}
5091
Willy Tarreau59551662015-03-10 14:23:13 +01005092__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005093{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005094 struct hlua *hlua;
5095
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005096 MAY_LJMP(check_args(L, 1, "get_priv"));
5097
Willy Tarreau87b09662015-04-03 00:22:06 +02005098 /* It is useles to retrieve the stream, but this function
5099 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005100 */
5101 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005102 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005103
5104 /* Push configuration index in the stack. */
5105 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5106
5107 return 1;
5108}
5109
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005110/* Create stack entry containing a class TXN. This function
5111 * return 0 if the stack does not contains free slots,
5112 * otherwise it returns 1.
5113 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005114static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005115{
Willy Tarreaude491382015-04-06 11:04:28 +02005116 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005117
5118 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005119 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005120 return 0;
5121
5122 /* NOTE: The allocation never fails. The failure
5123 * throw an error, and the function never returns.
5124 * if the throw is not avalaible, the process is aborted.
5125 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005126 /* Create the object: obj[0] = userdata. */
5127 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005128 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005129 lua_rawseti(L, -2, 0);
5130
Willy Tarreaude491382015-04-06 11:04:28 +02005131 htxn->s = s;
5132 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005133 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005134 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005135
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005136 /* Create the "f" field that contains a list of fetches. */
5137 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005138 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005139 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005140 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005141
5142 /* Create the "sf" field that contains a list of stringsafe fetches. */
5143 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005144 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005145 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005146 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005147
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005148 /* Create the "c" field that contains a list of converters. */
5149 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005150 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005151 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005152 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005153
5154 /* Create the "sc" field that contains a list of stringsafe converters. */
5155 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005156 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005157 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005158 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005159
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005160 /* Create the "req" field that contains the request channel object. */
5161 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005162 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005163 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005164 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005165
5166 /* Create the "res" field that contains the response channel object. */
5167 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005168 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005169 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005170 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005171
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005172 /* Creates the HTTP object is the current proxy allows http. */
5173 lua_pushstring(L, "http");
5174 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005175 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005176 return 0;
5177 }
5178 else
5179 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005180 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005181
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005182 /* Pop a class sesison metatable and affect it to the userdata. */
5183 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5184 lua_setmetatable(L, -2);
5185
5186 return 1;
5187}
5188
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005189__LJMP static int hlua_txn_deflog(lua_State *L)
5190{
5191 const char *msg;
5192 struct hlua_txn *htxn;
5193
5194 MAY_LJMP(check_args(L, 2, "deflog"));
5195 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5196 msg = MAY_LJMP(luaL_checkstring(L, 2));
5197
5198 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5199 return 0;
5200}
5201
5202__LJMP static int hlua_txn_log(lua_State *L)
5203{
5204 int level;
5205 const char *msg;
5206 struct hlua_txn *htxn;
5207
5208 MAY_LJMP(check_args(L, 3, "log"));
5209 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5210 level = MAY_LJMP(luaL_checkinteger(L, 2));
5211 msg = MAY_LJMP(luaL_checkstring(L, 3));
5212
5213 if (level < 0 || level >= NB_LOG_LEVELS)
5214 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5215
5216 hlua_sendlog(htxn->s->be, level, msg);
5217 return 0;
5218}
5219
5220__LJMP static int hlua_txn_log_debug(lua_State *L)
5221{
5222 const char *msg;
5223 struct hlua_txn *htxn;
5224
5225 MAY_LJMP(check_args(L, 2, "Debug"));
5226 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5227 msg = MAY_LJMP(luaL_checkstring(L, 2));
5228 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5229 return 0;
5230}
5231
5232__LJMP static int hlua_txn_log_info(lua_State *L)
5233{
5234 const char *msg;
5235 struct hlua_txn *htxn;
5236
5237 MAY_LJMP(check_args(L, 2, "Info"));
5238 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5239 msg = MAY_LJMP(luaL_checkstring(L, 2));
5240 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5241 return 0;
5242}
5243
5244__LJMP static int hlua_txn_log_warning(lua_State *L)
5245{
5246 const char *msg;
5247 struct hlua_txn *htxn;
5248
5249 MAY_LJMP(check_args(L, 2, "Warning"));
5250 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5251 msg = MAY_LJMP(luaL_checkstring(L, 2));
5252 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5253 return 0;
5254}
5255
5256__LJMP static int hlua_txn_log_alert(lua_State *L)
5257{
5258 const char *msg;
5259 struct hlua_txn *htxn;
5260
5261 MAY_LJMP(check_args(L, 2, "Alert"));
5262 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5263 msg = MAY_LJMP(luaL_checkstring(L, 2));
5264 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5265 return 0;
5266}
5267
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005268__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5269{
5270 struct hlua_txn *htxn;
5271 int ll;
5272
5273 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5274 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5275 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5276
5277 if (ll < 0 || ll > 7)
5278 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5279
5280 htxn->s->logs.level = ll;
5281 return 0;
5282}
5283
5284__LJMP static int hlua_txn_set_tos(lua_State *L)
5285{
5286 struct hlua_txn *htxn;
5287 struct connection *cli_conn;
5288 int tos;
5289
5290 MAY_LJMP(check_args(L, 2, "set_tos"));
5291 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5292 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5293
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005294 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005295 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005296
5297 return 0;
5298}
5299
5300__LJMP static int hlua_txn_set_mark(lua_State *L)
5301{
5302#ifdef SO_MARK
5303 struct hlua_txn *htxn;
5304 struct connection *cli_conn;
5305 int mark;
5306
5307 MAY_LJMP(check_args(L, 2, "set_mark"));
5308 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5309 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5310
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005311 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005312 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005313#endif
5314 return 0;
5315}
5316
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005317/* This function is an Lua binding that send pending data
5318 * to the client, and close the stream interface.
5319 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005320__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005321{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005322 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005323 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005324 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005325
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005326 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005327 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005328 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005329
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005330 /* If the flags NOTERM is set, we cannot terminate the http
5331 * session, so we just end the execution of the current
5332 * lua code.
5333 */
5334 if (htxn->flags & HLUA_TXN_NOTERM) {
5335 WILL_LJMP(hlua_done(L));
5336 return 0;
5337 }
5338
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005339 ic = &htxn->s->req;
5340 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005341
Willy Tarreau630ef452015-08-28 10:06:15 +02005342 if (htxn->s->txn) {
5343 /* HTTP mode, let's stay in sync with the stream */
5344 bi_fast_delete(ic->buf, htxn->s->txn->req.sov);
5345 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
5346 htxn->s->txn->req.sov = 0;
5347 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
5348 oc->analysers = AN_RES_HTTP_XFER_BODY;
5349 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
5350 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
5351
Willy Tarreau630ef452015-08-28 10:06:15 +02005352 /* Note that if we want to support keep-alive, we need
5353 * to bypass the close/shutr_now calls below, but that
5354 * may only be done if the HTTP request was already
5355 * processed and the connection header is known (ie
5356 * not during TCP rules).
5357 */
5358 }
5359
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005360 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01005361 channel_abort(ic);
5362 channel_auto_close(ic);
5363 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005364
5365 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01005366 channel_auto_read(oc);
5367 channel_auto_close(oc);
5368 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005369
Willy Tarreau0458b082015-08-28 09:40:04 +02005370 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005371
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005372 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005373 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005374 return 0;
5375}
5376
5377__LJMP static int hlua_log(lua_State *L)
5378{
5379 int level;
5380 const char *msg;
5381
5382 MAY_LJMP(check_args(L, 2, "log"));
5383 level = MAY_LJMP(luaL_checkinteger(L, 1));
5384 msg = MAY_LJMP(luaL_checkstring(L, 2));
5385
5386 if (level < 0 || level >= NB_LOG_LEVELS)
5387 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5388
5389 hlua_sendlog(NULL, level, msg);
5390 return 0;
5391}
5392
5393__LJMP static int hlua_log_debug(lua_State *L)
5394{
5395 const char *msg;
5396
5397 MAY_LJMP(check_args(L, 1, "debug"));
5398 msg = MAY_LJMP(luaL_checkstring(L, 1));
5399 hlua_sendlog(NULL, LOG_DEBUG, msg);
5400 return 0;
5401}
5402
5403__LJMP static int hlua_log_info(lua_State *L)
5404{
5405 const char *msg;
5406
5407 MAY_LJMP(check_args(L, 1, "info"));
5408 msg = MAY_LJMP(luaL_checkstring(L, 1));
5409 hlua_sendlog(NULL, LOG_INFO, msg);
5410 return 0;
5411}
5412
5413__LJMP static int hlua_log_warning(lua_State *L)
5414{
5415 const char *msg;
5416
5417 MAY_LJMP(check_args(L, 1, "warning"));
5418 msg = MAY_LJMP(luaL_checkstring(L, 1));
5419 hlua_sendlog(NULL, LOG_WARNING, msg);
5420 return 0;
5421}
5422
5423__LJMP static int hlua_log_alert(lua_State *L)
5424{
5425 const char *msg;
5426
5427 MAY_LJMP(check_args(L, 1, "alert"));
5428 msg = MAY_LJMP(luaL_checkstring(L, 1));
5429 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005430 return 0;
5431}
5432
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005433__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005434{
5435 int wakeup_ms = lua_tointeger(L, -1);
5436 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005437 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005438 return 0;
5439}
5440
5441__LJMP static int hlua_sleep(lua_State *L)
5442{
5443 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005444 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005445
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005446 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005447
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005448 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005449 wakeup_ms = tick_add(now_ms, delay);
5450 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005451
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005452 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5453 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005454}
5455
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005456__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005457{
5458 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005459 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005460
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005461 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005462
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005463 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005464 wakeup_ms = tick_add(now_ms, delay);
5465 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005466
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005467 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5468 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005469}
5470
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005471/* This functionis an LUA binding. it permits to give back
5472 * the hand at the HAProxy scheduler. It is used when the
5473 * LUA processing consumes a lot of time.
5474 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005475__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005476{
5477 return 0;
5478}
5479
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005480__LJMP static int hlua_yield(lua_State *L)
5481{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005482 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
5483 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005484}
5485
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005486/* This function change the nice of the currently executed
5487 * task. It is used set low or high priority at the current
5488 * task.
5489 */
Willy Tarreau59551662015-03-10 14:23:13 +01005490__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005491{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005492 struct hlua *hlua;
5493 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005494
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005495 MAY_LJMP(check_args(L, 1, "set_nice"));
5496 hlua = hlua_gethlua(L);
5497 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005498
5499 /* If he task is not set, I'm in a start mode. */
5500 if (!hlua || !hlua->task)
5501 return 0;
5502
5503 if (nice < -1024)
5504 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005505 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005506 nice = 1024;
5507
5508 hlua->task->nice = nice;
5509 return 0;
5510}
5511
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005512/* This function is used as a calback of a task. It is called by the
5513 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5514 * return an E_AGAIN signal, the emmiter of this signal must set a
5515 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005516 *
5517 * Task wrapper are longjmp safe because the only one Lua code
5518 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005519 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02005520static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005521{
Olivier Houchard9f6af332018-05-25 14:04:04 +02005522 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005523 enum hlua_exec status;
5524
Christopher Faulet5bc99722018-04-25 10:34:45 +02005525 if (task->thread_mask == MAX_THREADS_MASK)
5526 task_set_affinity(task, tid_bit);
5527
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005528 /* If it is the first call to the task, we must initialize the
5529 * execution timeouts.
5530 */
5531 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005532 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005533
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005534 /* Execute the Lua code. */
5535 status = hlua_ctx_resume(hlua, 1);
5536
5537 switch (status) {
5538 /* finished or yield */
5539 case HLUA_E_OK:
5540 hlua_ctx_destroy(hlua);
5541 task_delete(task);
5542 task_free(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02005543 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005544 break;
5545
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005546 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01005547 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02005548 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005549 break;
5550
5551 /* finished with error. */
5552 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005553 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005554 hlua_ctx_destroy(hlua);
5555 task_delete(task);
5556 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005557 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005558 break;
5559
5560 case HLUA_E_ERR:
5561 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005562 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005563 hlua_ctx_destroy(hlua);
5564 task_delete(task);
5565 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005566 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005567 break;
5568 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005569 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005570}
5571
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005572/* This function is an LUA binding that register LUA function to be
5573 * executed after the HAProxy configuration parsing and before the
5574 * HAProxy scheduler starts. This function expect only one LUA
5575 * argument that is a function. This function returns nothing, but
5576 * throws if an error is encountered.
5577 */
5578__LJMP static int hlua_register_init(lua_State *L)
5579{
5580 struct hlua_init_function *init;
5581 int ref;
5582
5583 MAY_LJMP(check_args(L, 1, "register_init"));
5584
5585 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5586
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005587 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005588 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005589 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005590
5591 init->function_ref = ref;
5592 LIST_ADDQ(&hlua_init_functions, &init->l);
5593 return 0;
5594}
5595
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005596/* This functio is an LUA binding. It permits to register a task
5597 * executed in parallel of the main HAroxy activity. The task is
5598 * created and it is set in the HAProxy scheduler. It can be called
5599 * from the "init" section, "post init" or during the runtime.
5600 *
5601 * Lua prototype:
5602 *
5603 * <none> core.register_task(<function>)
5604 */
5605static int hlua_register_task(lua_State *L)
5606{
5607 struct hlua *hlua;
5608 struct task *task;
5609 int ref;
5610
5611 MAY_LJMP(check_args(L, 1, "register_task"));
5612
5613 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5614
Willy Tarreaubafbe012017-11-24 17:34:44 +01005615 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005616 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005617 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005618
Emeric Brunc60def82017-09-27 14:59:38 +02005619 task = task_new(MAX_THREADS_MASK);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005620 task->context = hlua;
5621 task->process = hlua_process_task;
5622
5623 if (!hlua_ctx_init(hlua, task))
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
5626 /* Restore the function in the stack. */
5627 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5628 hlua->nargs = 0;
5629
5630 /* Schedule task. */
5631 task_schedule(task, now_ms);
5632
5633 return 0;
5634}
5635
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005636/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5637 * doesn't allow "yield" functions because the HAProxy engine cannot
5638 * resume converters.
5639 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005640static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005641{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005642 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005643 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005644 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005645
Willy Tarreaube508f12016-03-10 11:47:01 +01005646 if (!stream)
5647 return 0;
5648
Willy Tarreau87b09662015-04-03 00:22:06 +02005649 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005650 * Lua context can be not initialized. This behavior
5651 * permits to save performances because a systematic
5652 * Lua initialization cause 5% performances loss.
5653 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005654 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005655 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005656 if (!stream->hlua) {
5657 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5658 return 0;
5659 }
5660 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5661 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5662 return 0;
5663 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005664 }
5665
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005666 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005667 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005668
5669 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005670 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5671 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5672 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005673 else
5674 error = "critical error";
5675 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005676 return 0;
5677 }
5678
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005679 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005680 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005681 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005682 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005683 return 0;
5684 }
5685
5686 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005687 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005688
5689 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005690 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005691 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005692 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005693 return 0;
5694 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005695 hlua_smp2lua(stream->hlua->T, smp);
5696 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005697
5698 /* push keywords in the stack. */
5699 if (arg_p) {
5700 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005701 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005702 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005703 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005704 return 0;
5705 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005706 hlua_arg2lua(stream->hlua->T, arg_p);
5707 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005708 }
5709 }
5710
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005711 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005712 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005713
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005714 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005715 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005716 }
5717
5718 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005719 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005720 /* finished. */
5721 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005722 /* If the stack is empty, the function fails. */
5723 if (lua_gettop(stream->hlua->T) <= 0)
5724 return 0;
5725
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005726 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005727 hlua_lua2smp(stream->hlua->T, -1, smp);
5728 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005729 return 1;
5730
5731 /* yield. */
5732 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005733 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005734 return 0;
5735
5736 /* finished with error. */
5737 case HLUA_E_ERRMSG:
5738 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005739 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005740 fcn->name, lua_tostring(stream->hlua->T, -1));
5741 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005742 return 0;
5743
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005744 case HLUA_E_ETMOUT:
5745 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
5746 return 0;
5747
5748 case HLUA_E_NOMEM:
5749 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
5750 return 0;
5751
5752 case HLUA_E_YIELD:
5753 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
5754 return 0;
5755
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005756 case HLUA_E_ERR:
5757 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005758 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005759
5760 default:
5761 return 0;
5762 }
5763}
5764
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005765/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5766 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005767 * resume sample-fetches. This function will be called by the sample
5768 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005769 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005770static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5771 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005772{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005773 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005774 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005775 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005776 const struct chunk msg = { .len = 0 };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005777
Willy Tarreaube508f12016-03-10 11:47:01 +01005778 if (!stream)
5779 return 0;
5780
Willy Tarreau87b09662015-04-03 00:22:06 +02005781 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005782 * Lua context can be not initialized. This behavior
5783 * permits to save performances because a systematic
5784 * Lua initialization cause 5% performances loss.
5785 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005786 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005787 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005788 if (!stream->hlua) {
5789 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5790 return 0;
5791 }
5792 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5793 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5794 return 0;
5795 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005796 }
5797
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005798 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005799
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005800 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005801 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005802
5803 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005804 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5805 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5806 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005807 else
5808 error = "critical error";
5809 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005810 return 0;
5811 }
5812
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005813 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005814 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005815 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005816 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005817 return 0;
5818 }
5819
5820 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005821 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005822
5823 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005824 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005825 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005826 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005827 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005828 return 0;
5829 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005830 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005831
5832 /* push keywords in the stack. */
5833 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5834 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005835 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005836 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005837 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005838 return 0;
5839 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005840 hlua_arg2lua(stream->hlua->T, arg_p);
5841 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005842 }
5843
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005844 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005845 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005846
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005847 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005848 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005849 }
5850
5851 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005852 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005853 /* finished. */
5854 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005855 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005856 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005857 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005858 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005859 /* If the stack is empty, the function fails. */
5860 if (lua_gettop(stream->hlua->T) <= 0)
5861 return 0;
5862
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005863 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005864 hlua_lua2smp(stream->hlua->T, -1, smp);
5865 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005866
5867 /* Set the end of execution flag. */
5868 smp->flags &= ~SMP_F_MAY_CHANGE;
5869 return 1;
5870
5871 /* yield. */
5872 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005873 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005874 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005875 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005876 return 0;
5877
5878 /* finished with error. */
5879 case HLUA_E_ERRMSG:
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 FOURNIERfa0e5dd2015-02-16 20:19:18 +01005882 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005883 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005884 fcn->name, lua_tostring(stream->hlua->T, -1));
5885 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005886 return 0;
5887
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005888 case HLUA_E_ETMOUT:
5889 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
5890 stream_int_retnclose(&stream->si[0], &msg);
5891 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
5892 return 0;
5893
5894 case HLUA_E_NOMEM:
5895 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
5896 stream_int_retnclose(&stream->si[0], &msg);
5897 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
5898 return 0;
5899
5900 case HLUA_E_YIELD:
5901 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
5902 stream_int_retnclose(&stream->si[0], &msg);
5903 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
5904 return 0;
5905
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005906 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005907 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005908 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005909 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005910 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005911
5912 default:
5913 return 0;
5914 }
5915}
5916
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005917/* This function is an LUA binding used for registering
5918 * "sample-conv" functions. It expects a converter name used
5919 * in the haproxy configuration file, and an LUA function.
5920 */
5921__LJMP static int hlua_register_converters(lua_State *L)
5922{
5923 struct sample_conv_kw_list *sck;
5924 const char *name;
5925 int ref;
5926 int len;
5927 struct hlua_function *fcn;
5928
5929 MAY_LJMP(check_args(L, 2, "register_converters"));
5930
5931 /* First argument : converter name. */
5932 name = MAY_LJMP(luaL_checkstring(L, 1));
5933
5934 /* Second argument : lua function. */
5935 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5936
5937 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005938 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005939 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005940 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005941 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005942 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005943 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005944
5945 /* Fill fcn. */
5946 fcn->name = strdup(name);
5947 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005948 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005949 fcn->function_ref = ref;
5950
5951 /* List head */
5952 sck->list.n = sck->list.p = NULL;
5953
5954 /* converter keyword. */
5955 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005956 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005957 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005958 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005959
5960 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
5961 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01005962 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 +01005963 sck->kw[0].val_args = NULL;
5964 sck->kw[0].in_type = SMP_T_STR;
5965 sck->kw[0].out_type = SMP_T_STR;
5966 sck->kw[0].private = fcn;
5967
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005968 /* Register this new converter */
5969 sample_register_convs(sck);
5970
5971 return 0;
5972}
5973
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005974/* This fucntion is an LUA binding used for registering
5975 * "sample-fetch" functions. It expects a converter name used
5976 * in the haproxy configuration file, and an LUA function.
5977 */
5978__LJMP static int hlua_register_fetches(lua_State *L)
5979{
5980 const char *name;
5981 int ref;
5982 int len;
5983 struct sample_fetch_kw_list *sfk;
5984 struct hlua_function *fcn;
5985
5986 MAY_LJMP(check_args(L, 2, "register_fetches"));
5987
5988 /* First argument : sample-fetch name. */
5989 name = MAY_LJMP(luaL_checkstring(L, 1));
5990
5991 /* Second argument : lua function. */
5992 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5993
5994 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005995 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005996 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005997 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005998 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005999 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006000 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006001
6002 /* Fill fcn. */
6003 fcn->name = strdup(name);
6004 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006005 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006006 fcn->function_ref = ref;
6007
6008 /* List head */
6009 sfk->list.n = sfk->list.p = NULL;
6010
6011 /* sample-fetch keyword. */
6012 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006013 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006014 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006015 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006016
6017 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6018 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006019 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 +01006020 sfk->kw[0].val_args = NULL;
6021 sfk->kw[0].out_type = SMP_T_STR;
6022 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6023 sfk->kw[0].val = 0;
6024 sfk->kw[0].private = fcn;
6025
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006026 /* Register this new fetch. */
6027 sample_register_fetches(sfk);
6028
6029 return 0;
6030}
6031
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006032/* This function is a wrapper to execute each LUA function declared
6033 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006034 * return ACT_RET_CONT if the processing is finished (with or without
6035 * error) and return ACT_RET_YIELD if the function must be called again
6036 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006037 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006038static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006039 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006040{
6041 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006042 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006043 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006044 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006045 const struct chunk msg = { .len = 0 };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006046
6047 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01006048 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
6049 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
6050 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
6051 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006052 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006053 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006054 return ACT_RET_CONT;
6055 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006056
Willy Tarreau87b09662015-04-03 00:22:06 +02006057 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006058 * Lua context can be not initialized. This behavior
6059 * permits to save performances because a systematic
6060 * Lua initialization cause 5% performances loss.
6061 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006062 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006063 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006064 if (!s->hlua) {
6065 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6066 rule->arg.hlua_rule->fcn.name);
6067 return ACT_RET_CONT;
6068 }
6069 if (!hlua_ctx_init(s->hlua, s->task)) {
6070 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6071 rule->arg.hlua_rule->fcn.name);
6072 return ACT_RET_CONT;
6073 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006074 }
6075
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006076 consistency_set(s, dir, &s->hlua->cons);
6077
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006078 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006079 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006080
6081 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006082 if (!SET_SAFE_LJMP(s->hlua->T)) {
6083 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6084 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006085 else
6086 error = "critical error";
6087 SEND_ERR(px, "Lua function '%s': %s.\n",
6088 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006089 return ACT_RET_CONT;
6090 }
6091
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006092 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006093 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006094 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006095 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006096 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006097 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006098 }
6099
6100 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006101 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006102
Willy Tarreau87b09662015-04-03 00:22:06 +02006103 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006104 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006105 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006106 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006107 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006108 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006109 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006110 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006111
6112 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006113 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006114 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006115 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006116 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006117 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006118 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006119 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006120 lua_pushstring(s->hlua->T, *arg);
6121 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006122 }
6123
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006124 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006125 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006126
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006127 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006128 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006129 }
6130
6131 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006132 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006133 /* finished. */
6134 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006135 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006136 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006137 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006138 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006139 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006140 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006141 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006142
6143 /* yield. */
6144 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006145 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006146 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006147 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006148 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006149 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006150 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006151 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006152 /* Some actions can be wake up when a "write" event
6153 * is detected on a response channel. This is useful
6154 * only for actions targetted on the requests.
6155 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006156 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006157 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006158 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006159 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006160 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006161 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006162 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006163 /* We can quit the fcuntion without consistency check
6164 * because HAProxy is not able to manipulate data, it
6165 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006166 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006167
6168 /* finished with error. */
6169 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006170 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006171 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006172 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006173 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006174 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006175 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006176 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6177 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006178 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006179
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006180 case HLUA_E_ETMOUT:
6181 if (!consistency_check(s, dir, &s->hlua->cons)) {
6182 stream_int_retnclose(&s->si[0], &msg);
6183 return ACT_RET_ERR;
6184 }
6185 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6186 return 0;
6187
6188 case HLUA_E_NOMEM:
6189 if (!consistency_check(s, dir, &s->hlua->cons)) {
6190 stream_int_retnclose(&s->si[0], &msg);
6191 return ACT_RET_ERR;
6192 }
6193 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6194 return 0;
6195
6196 case HLUA_E_YIELD:
6197 if (!consistency_check(s, dir, &s->hlua->cons)) {
6198 stream_int_retnclose(&s->si[0], &msg);
6199 return ACT_RET_ERR;
6200 }
6201 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6202 rule->arg.hlua_rule->fcn.name);
6203 return 0;
6204
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006205 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006206 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006207 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006208 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006209 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006210 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006211 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006212 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006213
6214 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006215 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006216 }
6217}
6218
Olivier Houchard9f6af332018-05-25 14:04:04 +02006219struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006220{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006221 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006222 struct stream_interface *si = ctx->owner;
6223
6224 /* If the applet is wake up without any expected work, the sheduler
6225 * remove it from the run queue. This flag indicate that the applet
6226 * is waiting for write. If the buffer is full, the main processing
6227 * will send some data and after call the applet, otherwise it call
6228 * the applet ASAP.
6229 */
6230 si_applet_cant_put(si);
6231 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006232 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006233 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006234}
6235
6236static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6237{
6238 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006239 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006240 struct task *task;
6241 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006242 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006243
Willy Tarreaubafbe012017-11-24 17:34:44 +01006244 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006245 if (!hlua) {
6246 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6247 ctx->rule->arg.hlua_rule->fcn.name);
6248 return 0;
6249 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006250 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006251 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006252 ctx->ctx.hlua_apptcp.flags = 0;
6253
6254 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006255 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006256 if (!task) {
6257 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6258 ctx->rule->arg.hlua_rule->fcn.name);
6259 return 0;
6260 }
6261 task->nice = 0;
6262 task->context = ctx;
6263 task->process = hlua_applet_wakeup;
6264 ctx->ctx.hlua_apptcp.task = task;
6265
6266 /* In the execution wrappers linked with a stream, the
6267 * Lua context can be not initialized. This behavior
6268 * permits to save performances because a systematic
6269 * Lua initialization cause 5% performances loss.
6270 */
6271 if (!hlua_ctx_init(hlua, task)) {
6272 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6273 ctx->rule->arg.hlua_rule->fcn.name);
6274 return 0;
6275 }
6276
6277 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006278 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006279
6280 /* The following Lua calls can fail. */
6281 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006282 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6283 error = lua_tostring(hlua->T, -1);
6284 else
6285 error = "critical error";
6286 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6287 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006288 RESET_SAFE_LJMP(hlua->T);
6289 return 0;
6290 }
6291
6292 /* Check stack available size. */
6293 if (!lua_checkstack(hlua->T, 1)) {
6294 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6295 ctx->rule->arg.hlua_rule->fcn.name);
6296 RESET_SAFE_LJMP(hlua->T);
6297 return 0;
6298 }
6299
6300 /* Restore the function in the stack. */
6301 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6302
6303 /* Create and and push object stream in the stack. */
6304 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6305 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6306 ctx->rule->arg.hlua_rule->fcn.name);
6307 RESET_SAFE_LJMP(hlua->T);
6308 return 0;
6309 }
6310 hlua->nargs = 1;
6311
6312 /* push keywords in the stack. */
6313 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6314 if (!lua_checkstack(hlua->T, 1)) {
6315 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6316 ctx->rule->arg.hlua_rule->fcn.name);
6317 RESET_SAFE_LJMP(hlua->T);
6318 return 0;
6319 }
6320 lua_pushstring(hlua->T, *arg);
6321 hlua->nargs++;
6322 }
6323
6324 RESET_SAFE_LJMP(hlua->T);
6325
6326 /* Wakeup the applet ASAP. */
6327 si_applet_cant_get(si);
6328 si_applet_cant_put(si);
6329
6330 return 1;
6331}
6332
6333static void hlua_applet_tcp_fct(struct appctx *ctx)
6334{
6335 struct stream_interface *si = ctx->owner;
6336 struct stream *strm = si_strm(si);
6337 struct channel *res = si_ic(si);
6338 struct act_rule *rule = ctx->rule;
6339 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006340 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006341
6342 /* The applet execution is already done. */
6343 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE)
6344 return;
6345
6346 /* If the stream is disconnect or closed, ldo nothing. */
6347 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6348 return;
6349
6350 /* Execute the function. */
6351 switch (hlua_ctx_resume(hlua, 1)) {
6352 /* finished. */
6353 case HLUA_E_OK:
6354 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6355
6356 /* log time */
6357 strm->logs.tv_request = now;
6358
6359 /* eat the whole request */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006360 co_skip(si_oc(si), si_ob(si)->o);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006361 res->flags |= CF_READ_NULL;
6362 si_shutr(si);
6363 return;
6364
6365 /* yield. */
6366 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006367 if (hlua->wake_time != TICK_ETERNITY)
6368 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006369 return;
6370
6371 /* finished with error. */
6372 case HLUA_E_ERRMSG:
6373 /* Display log. */
6374 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6375 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6376 lua_pop(hlua->T, 1);
6377 goto error;
6378
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006379 case HLUA_E_ETMOUT:
6380 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6381 rule->arg.hlua_rule->fcn.name);
6382 goto error;
6383
6384 case HLUA_E_NOMEM:
6385 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6386 rule->arg.hlua_rule->fcn.name);
6387 goto error;
6388
6389 case HLUA_E_YIELD: /* unexpected */
6390 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6391 rule->arg.hlua_rule->fcn.name);
6392 goto error;
6393
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006394 case HLUA_E_ERR:
6395 /* Display log. */
6396 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6397 rule->arg.hlua_rule->fcn.name);
6398 goto error;
6399
6400 default:
6401 goto error;
6402 }
6403
6404error:
6405
6406 /* For all other cases, just close the stream. */
6407 si_shutw(si);
6408 si_shutr(si);
6409 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6410}
6411
6412static void hlua_applet_tcp_release(struct appctx *ctx)
6413{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006414 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006415 task_free(ctx->ctx.hlua_apptcp.task);
6416 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006417 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006418 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006419}
6420
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006421/* The function returns 1 if the initialisation is complete, 0 if
6422 * an errors occurs and -1 if more data are required for initializing
6423 * the applet.
6424 */
6425static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6426{
6427 struct stream_interface *si = ctx->owner;
6428 struct channel *req = si_oc(si);
6429 struct http_msg *msg;
6430 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006431 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006432 char **arg;
6433 struct hdr_ctx hdr;
6434 struct task *task;
6435 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006436 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006437
6438 /* Wait for a full HTTP request. */
6439 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6440 if (smp.flags & SMP_F_MAY_CHANGE)
6441 return -1;
6442 return 0;
6443 }
6444 txn = strm->txn;
6445 msg = &txn->req;
6446
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006447 /* We want two things in HTTP mode :
6448 * - enforce server-close mode if we were in keep-alive, so that the
6449 * applet is released after each response ;
6450 * - enable request body transfer to the applet in order to resync
6451 * with the response body.
6452 */
6453 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6454 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006455
Willy Tarreaubafbe012017-11-24 17:34:44 +01006456 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006457 if (!hlua) {
6458 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6459 ctx->rule->arg.hlua_rule->fcn.name);
6460 return 0;
6461 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006462 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006463 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006464 ctx->ctx.hlua_apphttp.left_bytes = -1;
6465 ctx->ctx.hlua_apphttp.flags = 0;
6466
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006467 if (txn->req.flags & HTTP_MSGF_VER_11)
6468 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6469
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006470 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006471 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006472 if (!task) {
6473 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6474 ctx->rule->arg.hlua_rule->fcn.name);
6475 return 0;
6476 }
6477 task->nice = 0;
6478 task->context = ctx;
6479 task->process = hlua_applet_wakeup;
6480 ctx->ctx.hlua_apphttp.task = task;
6481
6482 /* In the execution wrappers linked with a stream, the
6483 * Lua context can be not initialized. This behavior
6484 * permits to save performances because a systematic
6485 * Lua initialization cause 5% performances loss.
6486 */
6487 if (!hlua_ctx_init(hlua, task)) {
6488 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6489 ctx->rule->arg.hlua_rule->fcn.name);
6490 return 0;
6491 }
6492
6493 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006494 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006495
6496 /* The following Lua calls can fail. */
6497 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006498 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6499 error = lua_tostring(hlua->T, -1);
6500 else
6501 error = "critical error";
6502 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6503 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006504 return 0;
6505 }
6506
6507 /* Check stack available size. */
6508 if (!lua_checkstack(hlua->T, 1)) {
6509 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6510 ctx->rule->arg.hlua_rule->fcn.name);
6511 RESET_SAFE_LJMP(hlua->T);
6512 return 0;
6513 }
6514
6515 /* Restore the function in the stack. */
6516 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6517
6518 /* Create and and push object stream in the stack. */
6519 if (!hlua_applet_http_new(hlua->T, ctx)) {
6520 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6521 ctx->rule->arg.hlua_rule->fcn.name);
6522 RESET_SAFE_LJMP(hlua->T);
6523 return 0;
6524 }
6525 hlua->nargs = 1;
6526
6527 /* Look for a 100-continue expected. */
6528 if (msg->flags & HTTP_MSGF_VER_11) {
6529 hdr.idx = 0;
6530 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) &&
6531 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6532 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6533 }
6534
6535 /* push keywords in the stack. */
6536 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6537 if (!lua_checkstack(hlua->T, 1)) {
6538 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6539 ctx->rule->arg.hlua_rule->fcn.name);
6540 RESET_SAFE_LJMP(hlua->T);
6541 return 0;
6542 }
6543 lua_pushstring(hlua->T, *arg);
6544 hlua->nargs++;
6545 }
6546
6547 RESET_SAFE_LJMP(hlua->T);
6548
6549 /* Wakeup the applet when data is ready for read. */
6550 si_applet_cant_get(si);
6551
6552 return 1;
6553}
6554
6555static void hlua_applet_http_fct(struct appctx *ctx)
6556{
6557 struct stream_interface *si = ctx->owner;
6558 struct stream *strm = si_strm(si);
6559 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006560 struct act_rule *rule = ctx->rule;
6561 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006562 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006563 char *blk1;
6564 int len1;
6565 char *blk2;
6566 int len2;
6567 int ret;
6568
6569 /* If the stream is disconnect or closed, ldo nothing. */
6570 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6571 return;
6572
6573 /* Set the currently running flag. */
6574 if (!HLUA_IS_RUNNING(hlua) &&
6575 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6576
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006577 /* Wait for full HTTP analysys. */
6578 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
6579 si_applet_cant_get(si);
6580 return;
6581 }
6582
6583 /* Store the max amount of bytes that we can read. */
6584 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6585
6586 /* We need to flush the request header. This left the body
6587 * for the Lua.
6588 */
6589
6590 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006591 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006592 if (ret == -1)
6593 return;
6594
6595 /* No data available, ask for more data. */
6596 if (ret == 1)
6597 len2 = 0;
6598 if (ret == 0)
6599 len1 = 0;
6600 if (len1 + len2 < strm->txn->req.eoh + 2) {
6601 si_applet_cant_get(si);
6602 return;
6603 }
6604
6605 /* skip the requests bytes. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006606 co_skip(si_oc(si), strm->txn->req.eoh + 2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006607 }
6608
6609 /* Executes The applet if it is not done. */
6610 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6611
6612 /* Execute the function. */
6613 switch (hlua_ctx_resume(hlua, 1)) {
6614 /* finished. */
6615 case HLUA_E_OK:
6616 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6617 break;
6618
6619 /* yield. */
6620 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006621 if (hlua->wake_time != TICK_ETERNITY)
6622 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006623 return;
6624
6625 /* finished with error. */
6626 case HLUA_E_ERRMSG:
6627 /* Display log. */
6628 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6629 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6630 lua_pop(hlua->T, 1);
6631 goto error;
6632
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006633 case HLUA_E_ETMOUT:
6634 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6635 rule->arg.hlua_rule->fcn.name);
6636 goto error;
6637
6638 case HLUA_E_NOMEM:
6639 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6640 rule->arg.hlua_rule->fcn.name);
6641 goto error;
6642
6643 case HLUA_E_YIELD: /* unexpected */
6644 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6645 rule->arg.hlua_rule->fcn.name);
6646 goto error;
6647
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006648 case HLUA_E_ERR:
6649 /* Display log. */
6650 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6651 rule->arg.hlua_rule->fcn.name);
6652 goto error;
6653
6654 default:
6655 goto error;
6656 }
6657 }
6658
6659 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6660
6661 /* We must send the final chunk. */
6662 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6663 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6664
6665 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006666 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006667
6668 /* critical error. */
6669 if (ret == -2 || ret == -3) {
6670 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6671 rule->arg.hlua_rule->fcn.name);
6672 goto error;
6673 }
6674
6675 /* no enough space error. */
6676 if (ret == -1) {
6677 si_applet_cant_put(si);
6678 return;
6679 }
6680
6681 /* set the last chunk sent. */
6682 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6683 }
6684
6685 /* close the connection. */
6686
6687 /* status / log */
6688 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6689 strm->logs.tv_request = now;
6690
6691 /* eat the whole request */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006692 co_skip(si_oc(si), si_ob(si)->o);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006693 res->flags |= CF_READ_NULL;
6694 si_shutr(si);
6695
6696 return;
6697 }
6698
6699error:
6700
6701 /* If we are in HTTP mode, and we are not send any
6702 * data, return a 500 server error in best effort:
6703 * if there are no room avalaible in the buffer,
6704 * just close the connection.
6705 */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006706 ci_putblk(res, error_500, strlen(error_500));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006707 if (!(strm->flags & SF_ERR_MASK))
6708 strm->flags |= SF_ERR_RESOURCE;
6709 si_shutw(si);
6710 si_shutr(si);
6711 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6712}
6713
6714static void hlua_applet_http_release(struct appctx *ctx)
6715{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006716 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006717 task_free(ctx->ctx.hlua_apphttp.task);
6718 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006719 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006720 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006721}
6722
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006723/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6724 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006725 *
6726 * This function can fail with an abort() due to an Lua critical error.
6727 * We are in the configuration parsing process of HAProxy, this abort() is
6728 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006729 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006730static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6731 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006732{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006733 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006734 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006735
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006736 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006737 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006738 if (!rule->arg.hlua_rule) {
6739 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006740 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006741 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006742
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006743 /* Memory for arguments. */
6744 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6745 if (!rule->arg.hlua_rule->args) {
6746 memprintf(err, "out of memory error");
6747 return ACT_RET_PRS_ERR;
6748 }
6749
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006750 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006751 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006752
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006753 /* Expect some arguments */
6754 for (i = 0; i < fcn->nargs; i++) {
6755 if (*args[i+1] == '\0') {
6756 memprintf(err, "expect %d arguments", fcn->nargs);
6757 return ACT_RET_PRS_ERR;
6758 }
6759 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6760 if (!rule->arg.hlua_rule->args[i]) {
6761 memprintf(err, "out of memory error");
6762 return ACT_RET_PRS_ERR;
6763 }
6764 (*cur_arg)++;
6765 }
6766 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006767
Thierry FOURNIER42148732015-09-02 17:17:33 +02006768 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006769 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006770 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006771}
6772
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006773static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6774 struct act_rule *rule, char **err)
6775{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006776 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006777
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006778 /* HTTP applets are forbidden in tcp-request rules.
6779 * HTTP applet request requires everything initilized by
6780 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6781 * The applet will be immediately initilized, but its before
6782 * the call of this analyzer.
6783 */
6784 if (rule->from != ACT_F_HTTP_REQ) {
6785 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6786 return ACT_RET_PRS_ERR;
6787 }
6788
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006789 /* Memory for the rule. */
6790 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6791 if (!rule->arg.hlua_rule) {
6792 memprintf(err, "out of memory error");
6793 return ACT_RET_PRS_ERR;
6794 }
6795
6796 /* Reference the Lua function and store the reference. */
6797 rule->arg.hlua_rule->fcn = *fcn;
6798
6799 /* TODO: later accept arguments. */
6800 rule->arg.hlua_rule->args = NULL;
6801
6802 /* Add applet pointer in the rule. */
6803 rule->applet.obj_type = OBJ_TYPE_APPLET;
6804 rule->applet.name = fcn->name;
6805 rule->applet.init = hlua_applet_http_init;
6806 rule->applet.fct = hlua_applet_http_fct;
6807 rule->applet.release = hlua_applet_http_release;
6808 rule->applet.timeout = hlua_timeout_applet;
6809
6810 return ACT_RET_PRS_OK;
6811}
6812
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006813/* This function is an LUA binding used for registering
6814 * "sample-conv" functions. It expects a converter name used
6815 * in the haproxy configuration file, and an LUA function.
6816 */
6817__LJMP static int hlua_register_action(lua_State *L)
6818{
6819 struct action_kw_list *akl;
6820 const char *name;
6821 int ref;
6822 int len;
6823 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006824 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006825
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006826 /* Initialise the number of expected arguments at 0. */
6827 nargs = 0;
6828
6829 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6830 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006831
6832 /* First argument : converter name. */
6833 name = MAY_LJMP(luaL_checkstring(L, 1));
6834
6835 /* Second argument : environment. */
6836 if (lua_type(L, 2) != LUA_TTABLE)
6837 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6838
6839 /* Third argument : lua function. */
6840 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6841
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006842 /* Fouth argument : number of mandatories arguments expected on the configuration line. */
6843 if (lua_gettop(L) >= 4)
6844 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6845
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006846 /* browse the second argulent as an array. */
6847 lua_pushnil(L);
6848 while (lua_next(L, 2) != 0) {
6849 if (lua_type(L, -1) != LUA_TSTRING)
6850 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6851
6852 /* Check required environment. Only accepted "http" or "tcp". */
6853 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006854 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006855 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006856 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006857 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006858 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006859 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006860
6861 /* Fill fcn. */
6862 fcn->name = strdup(name);
6863 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006864 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006865 fcn->function_ref = ref;
6866
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006867 /* Set the expected number od arguments. */
6868 fcn->nargs = nargs;
6869
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006870 /* List head */
6871 akl->list.n = akl->list.p = NULL;
6872
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006873 /* action keyword. */
6874 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006875 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006876 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006877 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006878
6879 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6880
6881 akl->kw[0].match_pfx = 0;
6882 akl->kw[0].private = fcn;
6883 akl->kw[0].parse = action_register_lua;
6884
6885 /* select the action registering point. */
6886 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6887 tcp_req_cont_keywords_register(akl);
6888 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6889 tcp_res_cont_keywords_register(akl);
6890 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6891 http_req_keywords_register(akl);
6892 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6893 http_res_keywords_register(akl);
6894 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006895 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006896 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6897 "are expected.", lua_tostring(L, -1)));
6898
6899 /* pop the environment string. */
6900 lua_pop(L, 1);
6901 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006902 return ACT_RET_PRS_OK;
6903}
6904
6905static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6906 struct act_rule *rule, char **err)
6907{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006908 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006909
6910 /* Memory for the rule. */
6911 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6912 if (!rule->arg.hlua_rule) {
6913 memprintf(err, "out of memory error");
6914 return ACT_RET_PRS_ERR;
6915 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006916
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006917 /* Reference the Lua function and store the reference. */
6918 rule->arg.hlua_rule->fcn = *fcn;
6919
6920 /* TODO: later accept arguments. */
6921 rule->arg.hlua_rule->args = NULL;
6922
6923 /* Add applet pointer in the rule. */
6924 rule->applet.obj_type = OBJ_TYPE_APPLET;
6925 rule->applet.name = fcn->name;
6926 rule->applet.init = hlua_applet_tcp_init;
6927 rule->applet.fct = hlua_applet_tcp_fct;
6928 rule->applet.release = hlua_applet_tcp_release;
6929 rule->applet.timeout = hlua_timeout_applet;
6930
6931 return 0;
6932}
6933
6934/* This function is an LUA binding used for registering
6935 * "sample-conv" functions. It expects a converter name used
6936 * in the haproxy configuration file, and an LUA function.
6937 */
6938__LJMP static int hlua_register_service(lua_State *L)
6939{
6940 struct action_kw_list *akl;
6941 const char *name;
6942 const char *env;
6943 int ref;
6944 int len;
6945 struct hlua_function *fcn;
6946
6947 MAY_LJMP(check_args(L, 3, "register_service"));
6948
6949 /* First argument : converter name. */
6950 name = MAY_LJMP(luaL_checkstring(L, 1));
6951
6952 /* Second argument : environment. */
6953 env = MAY_LJMP(luaL_checkstring(L, 2));
6954
6955 /* Third argument : lua function. */
6956 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6957
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006958 /* Allocate and fill the sample fetch keyword struct. */
6959 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
6960 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006961 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006962 fcn = calloc(1, sizeof(*fcn));
6963 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006964 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006965
6966 /* Fill fcn. */
6967 len = strlen("<lua.>") + strlen(name) + 1;
6968 fcn->name = calloc(1, len);
6969 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006970 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006971 snprintf((char *)fcn->name, len, "<lua.%s>", name);
6972 fcn->function_ref = ref;
6973
6974 /* List head */
6975 akl->list.n = akl->list.p = NULL;
6976
6977 /* converter keyword. */
6978 len = strlen("lua.") + strlen(name) + 1;
6979 akl->kw[0].kw = calloc(1, len);
6980 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006981 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006982
6983 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6984
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01006985 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006986 if (strcmp(env, "tcp") == 0)
6987 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006988 else if (strcmp(env, "http") == 0)
6989 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006990 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006991 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01006992 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006993
6994 akl->kw[0].match_pfx = 0;
6995 akl->kw[0].private = fcn;
6996
6997 /* End of array. */
6998 memset(&akl->kw[1], 0, sizeof(*akl->kw));
6999
7000 /* Register this new converter */
7001 service_keywords_register(akl);
7002
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007003 return 0;
7004}
7005
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007006/* This function initialises Lua cli handler. It copies the
7007 * arguments in the Lua stack and create channel IO objects.
7008 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007009static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007010{
7011 struct hlua *hlua;
7012 struct hlua_function *fcn;
7013 int i;
7014 const char *error;
7015
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007016 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007017 appctx->ctx.hlua_cli.fcn = private;
7018
Willy Tarreaubafbe012017-11-24 17:34:44 +01007019 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007020 if (!hlua) {
7021 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007022 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007023 }
7024 HLUA_INIT(hlua);
7025 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007026
7027 /* Create task used by signal to wakeup applets.
7028 * We use the same wakeup fonction than the Lua applet_tcp and
7029 * applet_http. It is absolutely compatible.
7030 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007031 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007032 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007033 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007034 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007035 }
7036 appctx->ctx.hlua_cli.task->nice = 0;
7037 appctx->ctx.hlua_cli.task->context = appctx;
7038 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7039
7040 /* Initialises the Lua context */
7041 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
7042 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007043 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007044 }
7045
7046 /* The following Lua calls can fail. */
7047 if (!SET_SAFE_LJMP(hlua->T)) {
7048 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7049 error = lua_tostring(hlua->T, -1);
7050 else
7051 error = "critical error";
7052 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7053 goto error;
7054 }
7055
7056 /* Check stack available size. */
7057 if (!lua_checkstack(hlua->T, 2)) {
7058 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7059 goto error;
7060 }
7061
7062 /* Restore the function in the stack. */
7063 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7064
7065 /* Once the arguments parsed, the CLI is like an AppletTCP,
7066 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007067 */
7068 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7069 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7070 goto error;
7071 }
7072 hlua->nargs = 1;
7073
7074 /* push keywords in the stack. */
7075 for (i = 0; *args[i]; i++) {
7076 /* Check stack available size. */
7077 if (!lua_checkstack(hlua->T, 1)) {
7078 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7079 goto error;
7080 }
7081 lua_pushstring(hlua->T, args[i]);
7082 hlua->nargs++;
7083 }
7084
7085 /* We must initialize the execution timeouts. */
7086 hlua->max_time = hlua_timeout_session;
7087
7088 /* At this point the execution is safe. */
7089 RESET_SAFE_LJMP(hlua->T);
7090
7091 /* It's ok */
7092 return 0;
7093
7094 /* It's not ok. */
7095error:
7096 RESET_SAFE_LJMP(hlua->T);
7097 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007098 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007099 return 1;
7100}
7101
7102static int hlua_cli_io_handler_fct(struct appctx *appctx)
7103{
7104 struct hlua *hlua;
7105 struct stream_interface *si;
7106 struct hlua_function *fcn;
7107
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007108 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007109 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007110 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007111
7112 /* If the stream is disconnect or closed, ldo nothing. */
7113 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7114 return 1;
7115
7116 /* Execute the function. */
7117 switch (hlua_ctx_resume(hlua, 1)) {
7118
7119 /* finished. */
7120 case HLUA_E_OK:
7121 return 1;
7122
7123 /* yield. */
7124 case HLUA_E_AGAIN:
7125 /* We want write. */
7126 if (HLUA_IS_WAKERESWR(hlua))
7127 si_applet_cant_put(si);
7128 /* Set the timeout. */
7129 if (hlua->wake_time != TICK_ETERNITY)
7130 task_schedule(hlua->task, hlua->wake_time);
7131 return 0;
7132
7133 /* finished with error. */
7134 case HLUA_E_ERRMSG:
7135 /* Display log. */
7136 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7137 fcn->name, lua_tostring(hlua->T, -1));
7138 lua_pop(hlua->T, 1);
7139 return 1;
7140
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007141 case HLUA_E_ETMOUT:
7142 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7143 fcn->name);
7144 return 1;
7145
7146 case HLUA_E_NOMEM:
7147 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7148 fcn->name);
7149 return 1;
7150
7151 case HLUA_E_YIELD: /* unexpected */
7152 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7153 fcn->name);
7154 return 1;
7155
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007156 case HLUA_E_ERR:
7157 /* Display log. */
7158 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7159 fcn->name);
7160 return 1;
7161
7162 default:
7163 return 1;
7164 }
7165
7166 return 1;
7167}
7168
7169static void hlua_cli_io_release_fct(struct appctx *appctx)
7170{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007171 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007172 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007173}
7174
7175/* This function is an LUA binding used for registering
7176 * new keywords in the cli. It expects a list of keywords
7177 * which are the "path". It is limited to 5 keywords. A
7178 * description of the command, a function to be executed
7179 * for the parsing and a function for io handlers.
7180 */
7181__LJMP static int hlua_register_cli(lua_State *L)
7182{
7183 struct cli_kw_list *cli_kws;
7184 const char *message;
7185 int ref_io;
7186 int len;
7187 struct hlua_function *fcn;
7188 int index;
7189 int i;
7190
7191 MAY_LJMP(check_args(L, 3, "register_cli"));
7192
7193 /* First argument : an array of maximum 5 keywords. */
7194 if (!lua_istable(L, 1))
7195 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7196
7197 /* Second argument : string with contextual message. */
7198 message = MAY_LJMP(luaL_checkstring(L, 2));
7199
7200 /* Third and fourth argument : lua function. */
7201 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7202
7203 /* Allocate and fill the sample fetch keyword struct. */
7204 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7205 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007206 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007207 fcn = calloc(1, sizeof(*fcn));
7208 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007209 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007210
7211 /* Fill path. */
7212 index = 0;
7213 lua_pushnil(L);
7214 while(lua_next(L, 1) != 0) {
7215 if (index >= 5)
7216 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7217 if (lua_type(L, -1) != LUA_TSTRING)
7218 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7219 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7220 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007221 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007222 index++;
7223 lua_pop(L, 1);
7224 }
7225
7226 /* Copy help message. */
7227 cli_kws->kw[0].usage = strdup(message);
7228 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007229 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007230
7231 /* Fill fcn io handler. */
7232 len = strlen("<lua.cli>") + 1;
7233 for (i = 0; i < index; i++)
7234 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7235 fcn->name = calloc(1, len);
7236 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007237 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007238 strncat((char *)fcn->name, "<lua.cli", len);
7239 for (i = 0; i < index; i++) {
7240 strncat((char *)fcn->name, ".", len);
7241 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7242 }
7243 strncat((char *)fcn->name, ">", len);
7244 fcn->function_ref = ref_io;
7245
7246 /* Fill last entries. */
7247 cli_kws->kw[0].private = fcn;
7248 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7249 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7250 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7251
7252 /* Register this new converter */
7253 cli_register_kw(cli_kws);
7254
7255 return 0;
7256}
7257
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007258static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7259 struct proxy *defpx, const char *file, int line,
7260 char **err, unsigned int *timeout)
7261{
7262 const char *error;
7263
7264 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
7265 if (error && *error != '\0') {
7266 memprintf(err, "%s: invalid timeout", args[0]);
7267 return -1;
7268 }
7269 return 0;
7270}
7271
7272static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7273 struct proxy *defpx, const char *file, int line,
7274 char **err)
7275{
7276 return hlua_read_timeout(args, section_type, curpx, defpx,
7277 file, line, err, &hlua_timeout_session);
7278}
7279
7280static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7281 struct proxy *defpx, const char *file, int line,
7282 char **err)
7283{
7284 return hlua_read_timeout(args, section_type, curpx, defpx,
7285 file, line, err, &hlua_timeout_task);
7286}
7287
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007288static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7289 struct proxy *defpx, const char *file, int line,
7290 char **err)
7291{
7292 return hlua_read_timeout(args, section_type, curpx, defpx,
7293 file, line, err, &hlua_timeout_applet);
7294}
7295
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007296static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7297 struct proxy *defpx, const char *file, int line,
7298 char **err)
7299{
7300 char *error;
7301
7302 hlua_nb_instruction = strtoll(args[1], &error, 10);
7303 if (*error != '\0') {
7304 memprintf(err, "%s: invalid number", args[0]);
7305 return -1;
7306 }
7307 return 0;
7308}
7309
Willy Tarreau32f61e22015-03-18 17:54:59 +01007310static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7311 struct proxy *defpx, const char *file, int line,
7312 char **err)
7313{
7314 char *error;
7315
7316 if (*(args[1]) == 0) {
7317 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7318 return -1;
7319 }
7320 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7321 if (*error != '\0') {
7322 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7323 return -1;
7324 }
7325 return 0;
7326}
7327
7328
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007329/* This function is called by the main configuration key "lua-load". It loads and
7330 * execute an lua file during the parsing of the HAProxy configuration file. It is
7331 * the main lua entry point.
7332 *
7333 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
7334 * occured, otherwise it returns 0.
7335 *
7336 * In some error case, LUA set an error message in top of the stack. This function
7337 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007338 *
7339 * This function can fail with an abort() due to an Lua critical error.
7340 * We are in the configuration parsing process of HAProxy, this abort() is
7341 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007342 */
7343static int hlua_load(char **args, int section_type, struct proxy *curpx,
7344 struct proxy *defpx, const char *file, int line,
7345 char **err)
7346{
7347 int error;
7348
7349 /* Just load and compile the file. */
7350 error = luaL_loadfile(gL.T, args[1]);
7351 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007352 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007353 lua_pop(gL.T, 1);
7354 return -1;
7355 }
7356
7357 /* If no syntax error where detected, execute the code. */
7358 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7359 switch (error) {
7360 case LUA_OK:
7361 break;
7362 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007363 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007364 lua_pop(gL.T, 1);
7365 return -1;
7366 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007367 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007368 return -1;
7369 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007370 memprintf(err, "Lua message handler 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_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007374 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007375 lua_pop(gL.T, 1);
7376 return -1;
7377 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007378 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007379 lua_pop(gL.T, 1);
7380 return -1;
7381 }
7382
7383 return 0;
7384}
7385
7386/* configuration keywords declaration */
7387static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007388 { CFG_GLOBAL, "lua-load", hlua_load },
7389 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7390 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007391 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007392 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007393 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007394 { 0, NULL, NULL },
7395}};
7396
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007397/* This function can fail with an abort() due to an Lua critical error.
7398 * We are in the initialisation process of HAProxy, this abort() is
7399 * tolerated.
7400 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007401int hlua_post_init()
7402{
7403 struct hlua_init_function *init;
7404 const char *msg;
7405 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007406 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007407
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007408 /* Call post initialisation function in safe environement. */
7409 if (!SET_SAFE_LJMP(gL.T)) {
7410 if (lua_type(gL.T, -1) == LUA_TSTRING)
7411 error = lua_tostring(gL.T, -1);
7412 else
7413 error = "critical error";
7414 fprintf(stderr, "Lua post-init: %s.\n", error);
7415 exit(1);
7416 }
7417 hlua_fcn_post_init(gL.T);
7418 RESET_SAFE_LJMP(gL.T);
7419
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007420 list_for_each_entry(init, &hlua_init_functions, l) {
7421 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7422 ret = hlua_ctx_resume(&gL, 0);
7423 switch (ret) {
7424 case HLUA_E_OK:
7425 lua_pop(gL.T, -1);
7426 return 1;
7427 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007428 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007429 return 0;
7430 case HLUA_E_ERRMSG:
7431 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007432 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007433 return 0;
7434 case HLUA_E_ERR:
7435 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007436 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007437 return 0;
7438 }
7439 }
7440 return 1;
7441}
7442
Willy Tarreau32f61e22015-03-18 17:54:59 +01007443/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7444 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7445 * is the previously allocated size or the kind of object in case of a new
7446 * allocation. <nsize> is the requested new size.
7447 */
7448static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7449{
7450 struct hlua_mem_allocator *zone = ud;
7451
7452 if (nsize == 0) {
7453 /* it's a free */
7454 if (ptr)
7455 zone->allocated -= osize;
7456 free(ptr);
7457 return NULL;
7458 }
7459
7460 if (!ptr) {
7461 /* it's a new allocation */
7462 if (zone->limit && zone->allocated + nsize > zone->limit)
7463 return NULL;
7464
7465 ptr = malloc(nsize);
7466 if (ptr)
7467 zone->allocated += nsize;
7468 return ptr;
7469 }
7470
7471 /* it's a realloc */
7472 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7473 return NULL;
7474
7475 ptr = realloc(ptr, nsize);
7476 if (ptr)
7477 zone->allocated += nsize - osize;
7478 return ptr;
7479}
7480
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007481/* Ithis function can fail with an abort() due to an Lua critical error.
7482 * We are in the initialisation process of HAProxy, this abort() is
7483 * tolerated.
7484 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007485void hlua_init(void)
7486{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007487 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007488 int idx;
7489 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007490 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007491 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007492 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007493#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007494 struct srv_kw *kw;
7495 int tmp_error;
7496 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007497 char *args[] = { /* SSL client configuration. */
7498 "ssl",
7499 "verify",
7500 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007501 NULL
7502 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007503#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007504
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007505 HA_SPIN_INIT(&hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02007506
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007507 /* Initialise struct hlua and com signals pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007508 pool_head_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007509
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007510 /* Register configuration keywords. */
7511 cfg_register_keywords(&cfg_kws);
7512
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007513 /* Init main lua stack. */
7514 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007515 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007516 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007517 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007518 hlua_sethlua(&gL);
7519 gL.Tref = LUA_REFNIL;
7520 gL.task = NULL;
7521
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007522 /* From this point, until the end of the initialisation fucntion,
7523 * the Lua function can fail with an abort. We are in the initialisation
7524 * process of HAProxy, this abort() is tolerated.
7525 */
7526
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007527 /* Initialise lua. */
7528 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007529
Thierry Fournier75933d42016-01-21 09:30:18 +01007530 /* Set safe environment for the initialisation. */
7531 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007532 if (lua_type(gL.T, -1) == LUA_TSTRING)
7533 error_msg = lua_tostring(gL.T, -1);
7534 else
7535 error_msg = "critical error";
7536 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007537 exit(1);
7538 }
7539
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007540 /*
7541 *
7542 * Create "core" object.
7543 *
7544 */
7545
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007546 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007547 lua_newtable(gL.T);
7548
7549 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007550 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007551 hlua_class_const_int(gL.T, log_levels[i], i);
7552
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007553 /* Register special functions. */
7554 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007555 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007556 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007557 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007558 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007559 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007560 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007561 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007562 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007563 hlua_class_function(gL.T, "sleep", hlua_sleep);
7564 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007565 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7566 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7567 hlua_class_function(gL.T, "set_map", hlua_set_map);
7568 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007569 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007570 hlua_class_function(gL.T, "log", hlua_log);
7571 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7572 hlua_class_function(gL.T, "Info", hlua_log_info);
7573 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7574 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007575 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007576 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007577
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007578 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007579
7580 /*
7581 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007582 * Register class Map
7583 *
7584 */
7585
7586 /* This table entry is the object "Map" base. */
7587 lua_newtable(gL.T);
7588
7589 /* register pattern types. */
7590 for (i=0; i<PAT_MATCH_NUM; i++)
7591 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007592 for (i=0; i<PAT_MATCH_NUM; i++) {
7593 snprintf(trash.str, trash.size, "_%s", pat_match_names[i]);
7594 hlua_class_const_int(gL.T, trash.str, i);
7595 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007596
7597 /* register constructor. */
7598 hlua_class_function(gL.T, "new", hlua_map_new);
7599
7600 /* Create and fill the metatable. */
7601 lua_newtable(gL.T);
7602
7603 /* Create and fille the __index entry. */
7604 lua_pushstring(gL.T, "__index");
7605 lua_newtable(gL.T);
7606
7607 /* Register . */
7608 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7609 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7610
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007611 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007612
Thierry Fournier45e78d72016-02-19 18:34:46 +01007613 /* Register previous table in the registry with reference and named entry.
7614 * The function hlua_register_metatable() pops the stack, so we
7615 * previously create a copy of the table.
7616 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007617 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007618 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007619
7620 /* Assign the metatable to the mai Map object. */
7621 lua_setmetatable(gL.T, -2);
7622
7623 /* Set a name to the table. */
7624 lua_setglobal(gL.T, "Map");
7625
7626 /*
7627 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007628 * Register class Channel
7629 *
7630 */
7631
7632 /* Create and fill the metatable. */
7633 lua_newtable(gL.T);
7634
7635 /* Create and fille the __index entry. */
7636 lua_pushstring(gL.T, "__index");
7637 lua_newtable(gL.T);
7638
7639 /* Register . */
7640 hlua_class_function(gL.T, "get", hlua_channel_get);
7641 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7642 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7643 hlua_class_function(gL.T, "set", hlua_channel_set);
7644 hlua_class_function(gL.T, "append", hlua_channel_append);
7645 hlua_class_function(gL.T, "send", hlua_channel_send);
7646 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7647 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7648 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007649 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007650
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007651 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007652
7653 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007654 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007655
7656 /*
7657 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007658 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007659 *
7660 */
7661
7662 /* Create and fill the metatable. */
7663 lua_newtable(gL.T);
7664
7665 /* Create and fille the __index entry. */
7666 lua_pushstring(gL.T, "__index");
7667 lua_newtable(gL.T);
7668
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007669 /* Browse existing fetches and create the associated
7670 * object method.
7671 */
7672 sf = NULL;
7673 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7674
7675 /* Dont register the keywork if the arguments check function are
7676 * not safe during the runtime.
7677 */
7678 if ((sf->val_args != NULL) &&
7679 (sf->val_args != val_payload_lv) &&
7680 (sf->val_args != val_hdr))
7681 continue;
7682
7683 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7684 * by an underscore.
7685 */
7686 strncpy(trash.str, sf->kw, trash.size);
7687 trash.str[trash.size - 1] = '\0';
7688 for (p = trash.str; *p; p++)
7689 if (*p == '.' || *p == '-' || *p == '+')
7690 *p = '_';
7691
7692 /* Register the function. */
7693 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007694 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007695 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007696 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007697 }
7698
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007699 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007700
7701 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007702 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007703
7704 /*
7705 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007706 * Register class Converters
7707 *
7708 */
7709
7710 /* Create and fill the metatable. */
7711 lua_newtable(gL.T);
7712
7713 /* Create and fill the __index entry. */
7714 lua_pushstring(gL.T, "__index");
7715 lua_newtable(gL.T);
7716
7717 /* Browse existing converters and create the associated
7718 * object method.
7719 */
7720 sc = NULL;
7721 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7722 /* Dont register the keywork if the arguments check function are
7723 * not safe during the runtime.
7724 */
7725 if (sc->val_args != NULL)
7726 continue;
7727
7728 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7729 * by an underscore.
7730 */
7731 strncpy(trash.str, sc->kw, trash.size);
7732 trash.str[trash.size - 1] = '\0';
7733 for (p = trash.str; *p; p++)
7734 if (*p == '.' || *p == '-' || *p == '+')
7735 *p = '_';
7736
7737 /* Register the function. */
7738 lua_pushstring(gL.T, trash.str);
7739 lua_pushlightuserdata(gL.T, sc);
7740 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007741 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007742 }
7743
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007744 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007745
7746 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007747 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007748
7749 /*
7750 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007751 * Register class HTTP
7752 *
7753 */
7754
7755 /* Create and fill the metatable. */
7756 lua_newtable(gL.T);
7757
7758 /* Create and fille the __index entry. */
7759 lua_pushstring(gL.T, "__index");
7760 lua_newtable(gL.T);
7761
7762 /* Register Lua functions. */
7763 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7764 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7765 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7766 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7767 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7768 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7769 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7770 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7771 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7772 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7773
7774 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7775 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7776 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7777 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7778 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7779 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007780 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007781
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007782 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007783
7784 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007785 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007786
7787 /*
7788 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007789 * Register class AppletTCP
7790 *
7791 */
7792
7793 /* Create and fill the metatable. */
7794 lua_newtable(gL.T);
7795
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007796 /* Create and fille the __index entry. */
7797 lua_pushstring(gL.T, "__index");
7798 lua_newtable(gL.T);
7799
7800 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007801 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7802 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7803 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7804 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7805 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007806 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7807 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7808 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007809
7810 lua_settable(gL.T, -3);
7811
7812 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007813 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007814
7815 /*
7816 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007817 * Register class AppletHTTP
7818 *
7819 */
7820
7821 /* Create and fill the metatable. */
7822 lua_newtable(gL.T);
7823
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007824 /* Create and fille the __index entry. */
7825 lua_pushstring(gL.T, "__index");
7826 lua_newtable(gL.T);
7827
7828 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007829 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7830 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007831 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7832 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7833 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007834 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7835 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7836 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7837 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7838 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7839 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7840
7841 lua_settable(gL.T, -3);
7842
7843 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007844 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007845
7846 /*
7847 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007848 * Register class TXN
7849 *
7850 */
7851
7852 /* Create and fill the metatable. */
7853 lua_newtable(gL.T);
7854
7855 /* Create and fille the __index entry. */
7856 lua_pushstring(gL.T, "__index");
7857 lua_newtable(gL.T);
7858
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007859 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01007860 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7861 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007862 hlua_class_function(gL.T, "set_var", hlua_set_var);
Christopher Faulet85d79c92016-11-09 16:54:56 +01007863 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007864 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007865 hlua_class_function(gL.T, "done", hlua_txn_done);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007866 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
7867 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7868 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007869 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7870 hlua_class_function(gL.T, "log", hlua_txn_log);
7871 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7872 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7873 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7874 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007875
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007876 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007877
7878 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007879 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007880
7881 /*
7882 *
7883 * Register class Socket
7884 *
7885 */
7886
7887 /* Create and fill the metatable. */
7888 lua_newtable(gL.T);
7889
7890 /* Create and fille the __index entry. */
7891 lua_pushstring(gL.T, "__index");
7892 lua_newtable(gL.T);
7893
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007894#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007895 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007896#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007897 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7898 hlua_class_function(gL.T, "send", hlua_socket_send);
7899 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7900 hlua_class_function(gL.T, "close", hlua_socket_close);
7901 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7902 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7903 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7904 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7905
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007906 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007907
7908 /* Register the garbage collector entry. */
7909 lua_pushstring(gL.T, "__gc");
7910 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007911 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007912
7913 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007914 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007915
7916 /* Proxy and server configuration initialisation. */
7917 memset(&socket_proxy, 0, sizeof(socket_proxy));
7918 init_new_proxy(&socket_proxy);
7919 socket_proxy.parent = NULL;
7920 socket_proxy.last_change = now.tv_sec;
7921 socket_proxy.id = "LUA-SOCKET";
7922 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
7923 socket_proxy.maxconn = 0;
7924 socket_proxy.accept = NULL;
7925 socket_proxy.options2 |= PR_O2_INDEPSTR;
7926 socket_proxy.srv = NULL;
7927 socket_proxy.conn_retries = 0;
7928 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
7929
7930 /* Init TCP server: unchanged parameters */
7931 memset(&socket_tcp, 0, sizeof(socket_tcp));
7932 socket_tcp.next = NULL;
7933 socket_tcp.proxy = &socket_proxy;
7934 socket_tcp.obj_type = OBJ_TYPE_SERVER;
7935 LIST_INIT(&socket_tcp.actconns);
7936 LIST_INIT(&socket_tcp.pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02007937 socket_tcp.priv_conns = NULL;
7938 socket_tcp.idle_conns = NULL;
7939 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02007940 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007941 socket_tcp.last_change = 0;
7942 socket_tcp.id = "LUA-TCP-CONN";
7943 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7944 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7945 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
7946
7947 /* XXX: Copy default parameter from default server,
7948 * but the default server is not initialized.
7949 */
7950 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
7951 socket_tcp.minconn = socket_proxy.defsrv.minconn;
7952 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
7953 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
7954 socket_tcp.onerror = socket_proxy.defsrv.onerror;
7955 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
7956 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
7957 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
7958 socket_tcp.uweight = socket_proxy.defsrv.iweight;
7959 socket_tcp.iweight = socket_proxy.defsrv.iweight;
7960
7961 socket_tcp.check.status = HCHK_STATUS_INI;
7962 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
7963 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
7964 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
7965 socket_tcp.check.server = &socket_tcp;
7966
7967 socket_tcp.agent.status = HCHK_STATUS_INI;
7968 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
7969 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
7970 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
7971 socket_tcp.agent.server = &socket_tcp;
7972
Willy Tarreaua261e9b2016-12-22 20:44:00 +01007973 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007974
7975#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007976 /* Init TCP server: unchanged parameters */
7977 memset(&socket_ssl, 0, sizeof(socket_ssl));
7978 socket_ssl.next = NULL;
7979 socket_ssl.proxy = &socket_proxy;
7980 socket_ssl.obj_type = OBJ_TYPE_SERVER;
7981 LIST_INIT(&socket_ssl.actconns);
7982 LIST_INIT(&socket_ssl.pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02007983 socket_tcp.priv_conns = NULL;
7984 socket_tcp.idle_conns = NULL;
7985 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02007986 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007987 socket_ssl.last_change = 0;
7988 socket_ssl.id = "LUA-SSL-CONN";
7989 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7990 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7991 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
7992
7993 /* XXX: Copy default parameter from default server,
7994 * but the default server is not initialized.
7995 */
7996 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
7997 socket_ssl.minconn = socket_proxy.defsrv.minconn;
7998 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
7999 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8000 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8001 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8002 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8003 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8004 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8005 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8006
8007 socket_ssl.check.status = HCHK_STATUS_INI;
8008 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8009 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8010 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8011 socket_ssl.check.server = &socket_ssl;
8012
8013 socket_ssl.agent.status = HCHK_STATUS_INI;
8014 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8015 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8016 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8017 socket_ssl.agent.server = &socket_ssl;
8018
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008019 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008020 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008021
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008022 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008023 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8024 /*
8025 *
8026 * If the keyword is not known, we can search in the registered
8027 * server keywords. This is usefull to configure special SSL
8028 * features like client certificates and ssl_verify.
8029 *
8030 */
8031 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8032 if (tmp_error != 0) {
8033 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8034 abort(); /* This must be never arrives because the command line
8035 not editable by the user. */
8036 }
8037 idx += kw->skip;
8038 }
8039 }
8040
8041 /* Initialize SSL server. */
Willy Tarreaucbe6da52018-05-18 17:08:28 +02008042 if (socket_ssl.xprt->prepare_srv) {
8043 int saved_used_backed = global.ssl_used_backend;
8044 // don't affect maxconn automatic computation
Willy Tarreau17d45382016-12-22 21:16:08 +01008045 socket_ssl.xprt->prepare_srv(&socket_ssl);
Willy Tarreaucbe6da52018-05-18 17:08:28 +02008046 global.ssl_used_backend = saved_used_backed;
8047 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008048#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008049
8050 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008051}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008052
8053__attribute__((constructor))
8054static void __hlua_init(void)
8055{
8056 char *ptr = NULL;
8057 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8058 hap_register_build_opts(ptr, 1);
8059}