blob: c68495b34c872748ab7f3db4eedeca6ae96cb252 [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>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020014#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010015
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010016#include <lauxlib.h>
17#include <lua.h>
18#include <lualib.h>
19
Thierry FOURNIER463119c2015-03-10 00:35:36 +010020#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
21#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010022#endif
23
Thierry FOURNIER380d0932015-01-23 14:27:52 +010024#include <ebpttree.h>
25
26#include <common/cfgparse.h>
Thierry FOURNIER2da788e2017-09-11 18:37:23 +020027#include <common/xref.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010028
William Lallemand9ed62032016-11-21 17:49:11 +010029#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010030#include <types/hlua.h>
31#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010032#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010033
Thierry FOURNIER55da1652015-01-23 11:36:30 +010034#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020035#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010036#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010037#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010038#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010039#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010040#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010041#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010042#include <proto/hlua_fcn.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020043#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010044#include <proto/obj_type.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010045#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010046#include <proto/payload.h>
47#include <proto/proto_http.h>
48#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010049#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020050#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020051#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010052#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010053#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010054#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020055#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010056
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010057/* Lua uses longjmp to perform yield or throwing errors. This
58 * macro is used only for identifying the function that can
59 * not return because a longjmp is executed.
60 * __LJMP marks a prototype of hlua file that can use longjmp.
61 * WILL_LJMP() marks an lua function that will use longjmp.
62 * MAY_LJMP() marks an lua function that may use longjmp.
63 */
64#define __LJMP
65#define WILL_LJMP(func) func
66#define MAY_LJMP(func) func
67
Thierry FOURNIERbabae282015-09-17 11:36:37 +020068/* This couple of function executes securely some Lua calls outside of
69 * the lua runtime environment. Each Lua call can return a longjmp
70 * if it encounter a memory error.
71 *
72 * Lua documentation extract:
73 *
74 * If an error happens outside any protected environment, Lua calls
75 * a panic function (see lua_atpanic) and then calls abort, thus
76 * exiting the host application. Your panic function can avoid this
77 * exit by never returning (e.g., doing a long jump to your own
78 * recovery point outside Lua).
79 *
80 * The panic function runs as if it were a message handler (see
81 * §2.3); in particular, the error message is at the top of the
82 * stack. However, there is no guarantee about stack space. To push
83 * anything on the stack, the panic function must first check the
84 * available space (see §4.2).
85 *
86 * We must check all the Lua entry point. This includes:
87 * - The include/proto/hlua.h exported functions
88 * - the task wrapper function
89 * - The action wrapper function
90 * - The converters wrapper function
91 * - The sample-fetch wrapper functions
92 *
93 * It is tolerated that the initilisation function returns an abort.
94 * Before each Lua abort, an error message is writed on stderr.
95 *
96 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
97 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
98 * because they must be exists in the program stack when the longjmp
99 * is called.
100 */
101jmp_buf safe_ljmp_env;
102static int hlua_panic_safe(lua_State *L) { return 0; }
103static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
104
105#define SET_SAFE_LJMP(__L) \
106 ({ \
107 int ret; \
108 if (setjmp(safe_ljmp_env) != 0) { \
109 lua_atpanic(__L, hlua_panic_safe); \
110 ret = 0; \
111 } else { \
112 lua_atpanic(__L, hlua_panic_ljmp); \
113 ret = 1; \
114 } \
115 ret; \
116 })
117
118/* If we are the last function catching Lua errors, we
119 * must reset the panic function.
120 */
121#define RESET_SAFE_LJMP(__L) \
122 do { \
123 lua_atpanic(__L, hlua_panic_safe); \
124 } while(0)
125
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200126/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200127#define APPLET_DONE 0x01 /* applet processing is done. */
128#define APPLET_100C 0x02 /* 100 continue expected. */
129#define APPLET_HDR_SENT 0x04 /* Response header sent. */
130#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
131#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100132#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200133
134#define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n"
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200135
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100136/* The main Lua execution context. */
137struct hlua gL;
138
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100139/* This is the memory pool containing struct lua for applets
140 * (including cli).
141 */
142struct pool_head *pool2_hlua;
143
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100144/* Used for Socket connection. */
145static struct proxy socket_proxy;
146static struct server socket_tcp;
147#ifdef USE_OPENSSL
148static struct server socket_ssl;
149#endif
150
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100151/* List head of the function called at the initialisation time. */
152struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
153
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100154/* The following variables contains the reference of the different
155 * Lua classes. These references are useful for identify metadata
156 * associated with an object.
157 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100158static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100159static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100160static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100161static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100162static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100163static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200164static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200165static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200166static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100167
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100168/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200169 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100170 * short timeout. Lua linked with tasks doesn't have a timeout
171 * because a task may remain alive during all the haproxy execution.
172 */
173static unsigned int hlua_timeout_session = 4000; /* session timeout. */
174static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200175static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100176
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100177/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
178 * it is used for preventing infinite loops.
179 *
180 * I test the scheer with an infinite loop containing one incrementation
181 * and one test. I run this loop between 10 seconds, I raise a ceil of
182 * 710M loops from one interrupt each 9000 instructions, so I fix the value
183 * to one interrupt each 10 000 instructions.
184 *
185 * configured | Number of
186 * instructions | loops executed
187 * between two | in milions
188 * forced yields |
189 * ---------------+---------------
190 * 10 | 160
191 * 500 | 670
192 * 1000 | 680
193 * 5000 | 700
194 * 7000 | 700
195 * 8000 | 700
196 * 9000 | 710 <- ceil
197 * 10000 | 710
198 * 100000 | 710
199 * 1000000 | 710
200 *
201 */
202static unsigned int hlua_nb_instruction = 10000;
203
Willy Tarreau32f61e22015-03-18 17:54:59 +0100204/* Descriptor for the memory allocation state. If limit is not null, it will
205 * be enforced on any memory allocation.
206 */
207struct hlua_mem_allocator {
208 size_t allocated;
209 size_t limit;
210};
211
212static struct hlua_mem_allocator hlua_global_allocator;
213
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200214static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200215 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200216 "Cache-Control: no-cache\r\n"
217 "Connection: close\r\n"
218 "Content-Type: text/html\r\n"
219 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200220 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200221
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100222/* These functions converts types between HAProxy internal args or
223 * sample and LUA types. Another function permits to check if the
224 * LUA stack contains arguments according with an required ARG_T
225 * format.
226 */
227static int hlua_arg2lua(lua_State *L, const struct arg *arg);
228static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100229__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100230 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100231static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100232static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100233static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
234
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200235__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
236
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200237#define SEND_ERR(__be, __fmt, __args...) \
238 do { \
239 send_log(__be, LOG_ERR, __fmt, ## __args); \
240 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
241 Alert(__fmt, ## __args); \
242 } while (0)
243
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100244/* Used to check an Lua function type in the stack. It creates and
245 * returns a reference of the function. This function throws an
246 * error if the rgument is not a "function".
247 */
248__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
249{
250 if (!lua_isfunction(L, argno)) {
251 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1));
252 WILL_LJMP(luaL_argerror(L, argno, msg));
253 }
254 lua_pushvalue(L, argno);
255 return luaL_ref(L, LUA_REGISTRYINDEX);
256}
257
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200258/* Return the string that is of the top of the stack. */
259const char *hlua_get_top_error_string(lua_State *L)
260{
261 if (lua_gettop(L) < 1)
262 return "unknown error";
263 if (lua_type(L, -1) != LUA_TSTRING)
264 return "unknown error";
265 return lua_tostring(L, -1);
266}
267
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100268/* This function check the number of arguments available in the
269 * stack. If the number of arguments available is not the same
270 * then <nb> an error is throwed.
271 */
272__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
273{
274 if (lua_gettop(L) == nb)
275 return;
276 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
277}
278
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100279/* This fucntion push an error string prefixed by the file name
280 * and the line number where the error is encountered.
281 */
282static int hlua_pusherror(lua_State *L, const char *fmt, ...)
283{
284 va_list argp;
285 va_start(argp, fmt);
286 luaL_where(L, 1);
287 lua_pushvfstring(L, fmt, argp);
288 va_end(argp);
289 lua_concat(L, 2);
290 return 1;
291}
292
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100293/* This functions is used with sample fetch and converters. It
294 * converts the HAProxy configuration argument in a lua stack
295 * values.
296 *
297 * It takes an array of "arg", and each entry of the array is
298 * converted and pushed in the LUA stack.
299 */
300static int hlua_arg2lua(lua_State *L, const struct arg *arg)
301{
302 switch (arg->type) {
303 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100304 case ARGT_TIME:
305 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100306 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100307 break;
308
309 case ARGT_STR:
310 lua_pushlstring(L, arg->data.str.str, arg->data.str.len);
311 break;
312
313 case ARGT_IPV4:
314 case ARGT_IPV6:
315 case ARGT_MSK4:
316 case ARGT_MSK6:
317 case ARGT_FE:
318 case ARGT_BE:
319 case ARGT_TAB:
320 case ARGT_SRV:
321 case ARGT_USR:
322 case ARGT_MAP:
323 default:
324 lua_pushnil(L);
325 break;
326 }
327 return 1;
328}
329
330/* This function take one entrie in an LUA stack at the index "ud",
331 * and try to convert it in an HAProxy argument entry. This is useful
332 * with sample fetch wrappers. The input arguments are gived to the
333 * lua wrapper and converted as arg list by thi function.
334 */
335static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
336{
337 switch (lua_type(L, ud)) {
338
339 case LUA_TNUMBER:
340 case LUA_TBOOLEAN:
341 arg->type = ARGT_SINT;
342 arg->data.sint = lua_tointeger(L, ud);
343 break;
344
345 case LUA_TSTRING:
346 arg->type = ARGT_STR;
347 arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len);
348 break;
349
350 case LUA_TUSERDATA:
351 case LUA_TNIL:
352 case LUA_TTABLE:
353 case LUA_TFUNCTION:
354 case LUA_TTHREAD:
355 case LUA_TLIGHTUSERDATA:
356 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200357 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100358 break;
359 }
360 return 1;
361}
362
363/* the following functions are used to convert a struct sample
364 * in Lua type. This useful to convert the return of the
365 * fetchs or converters.
366 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100367static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100368{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200369 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100370 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100371 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200372 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100373 break;
374
375 case SMP_T_BIN:
376 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200377 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100378 break;
379
380 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200381 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100382 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
383 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
384 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
385 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
386 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
387 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
388 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
389 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
390 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200391 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100392 break;
393 default:
394 lua_pushnil(L);
395 break;
396 }
397 break;
398
399 case SMP_T_IPV4:
400 case SMP_T_IPV6:
401 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200402 if (sample_casts[smp->data.type][SMP_T_STR] &&
403 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200404 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100405 else
406 lua_pushnil(L);
407 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100408 default:
409 lua_pushnil(L);
410 break;
411 }
412 return 1;
413}
414
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100415/* the following functions are used to convert a struct sample
416 * in Lua strings. This is useful to convert the return of the
417 * fetchs or converters.
418 */
419static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
420{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200421 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100422
423 case SMP_T_BIN:
424 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200425 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100426 break;
427
428 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200429 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100430 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
431 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
432 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
433 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
434 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
435 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
436 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
437 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
438 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200439 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100440 break;
441 default:
442 lua_pushstring(L, "");
443 break;
444 }
445 break;
446
447 case SMP_T_SINT:
448 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100449 case SMP_T_IPV4:
450 case SMP_T_IPV6:
451 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200452 if (sample_casts[smp->data.type][SMP_T_STR] &&
453 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200454 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100455 else
456 lua_pushstring(L, "");
457 break;
458 default:
459 lua_pushstring(L, "");
460 break;
461 }
462 return 1;
463}
464
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100465/* the following functions are used to convert an Lua type in a
466 * struct sample. This is useful to provide data from a converter
467 * to the LUA code.
468 */
469static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
470{
471 switch (lua_type(L, ud)) {
472
473 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200474 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200475 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100476 break;
477
478
479 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200480 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200481 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100482 break;
483
484 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200485 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100486 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200487 smp->data.u.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100488 break;
489
490 case LUA_TUSERDATA:
491 case LUA_TNIL:
492 case LUA_TTABLE:
493 case LUA_TFUNCTION:
494 case LUA_TTHREAD:
495 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200496 case LUA_TNONE:
497 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200498 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200499 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100500 break;
501 }
502 return 1;
503}
504
505/* This function check the "argp" builded by another conversion function
506 * is in accord with the expected argp defined by the "mask". The fucntion
507 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100508 *
509 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
510 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100511 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100512__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100513 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100514{
515 int min_arg;
516 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100517 struct proxy *px;
518 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100519
520 idx = 0;
521 min_arg = ARGM(mask);
522 mask >>= ARGM_BITS;
523
524 while (1) {
525
526 /* Check oversize. */
527 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100528 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100529 }
530
531 /* Check for mandatory arguments. */
532 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100533 if (idx < min_arg) {
534
535 /* If miss other argument than the first one, we return an error. */
536 if (idx > 0)
537 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
538
539 /* If first argument have a certain type, some default values
540 * may be used. See the function smp_resolve_args().
541 */
542 switch (mask & ARGT_MASK) {
543
544 case ARGT_FE:
545 if (!(p->cap & PR_CAP_FE))
546 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
547 argp[idx].data.prx = p;
548 argp[idx].type = ARGT_FE;
549 argp[idx+1].type = ARGT_STOP;
550 break;
551
552 case ARGT_BE:
553 if (!(p->cap & PR_CAP_BE))
554 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
555 argp[idx].data.prx = p;
556 argp[idx].type = ARGT_BE;
557 argp[idx+1].type = ARGT_STOP;
558 break;
559
560 case ARGT_TAB:
561 argp[idx].data.prx = p;
562 argp[idx].type = ARGT_TAB;
563 argp[idx+1].type = ARGT_STOP;
564 break;
565
566 default:
567 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
568 break;
569 }
570 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100571 return 0;
572 }
573
574 /* Check for exceed the number of requiered argument. */
575 if ((mask & ARGT_MASK) == ARGT_STOP &&
576 argp[idx].type != ARGT_STOP) {
577 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
578 }
579
580 if ((mask & ARGT_MASK) == ARGT_STOP &&
581 argp[idx].type == ARGT_STOP) {
582 return 0;
583 }
584
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100585 /* Convert some argument types. */
586 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100587 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100588 if (argp[idx].type != ARGT_SINT)
589 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
590 argp[idx].type = ARGT_SINT;
591 break;
592
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100593 case ARGT_TIME:
594 if (argp[idx].type != ARGT_SINT)
595 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200596 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100597 break;
598
599 case ARGT_SIZE:
600 if (argp[idx].type != ARGT_SINT)
601 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200602 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100603 break;
604
605 case ARGT_FE:
606 if (argp[idx].type != ARGT_STR)
607 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
608 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
609 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200610 argp[idx].data.prx = proxy_fe_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100611 if (!argp[idx].data.prx)
612 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
613 argp[idx].type = ARGT_FE;
614 break;
615
616 case ARGT_BE:
617 if (argp[idx].type != ARGT_STR)
618 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
619 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
620 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200621 argp[idx].data.prx = proxy_be_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100622 if (!argp[idx].data.prx)
623 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
624 argp[idx].type = ARGT_BE;
625 break;
626
627 case ARGT_TAB:
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 Tarreaue2dc1fa2015-05-26 12:08:07 +0200632 argp[idx].data.prx = proxy_tbl_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, "table doesn't exist"));
635 argp[idx].type = ARGT_TAB;
636 break;
637
638 case ARGT_SRV:
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;
643 sname = strrchr(trash.str, '/');
644 if (sname) {
645 *sname++ = '\0';
646 pname = trash.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200647 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100648 if (!px)
649 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
650 }
651 else {
652 sname = trash.str;
653 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100654 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100655 argp[idx].data.srv = findserver(px, sname);
656 if (!argp[idx].data.srv)
657 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
658 argp[idx].type = ARGT_SRV;
659 break;
660
661 case ARGT_IPV4:
662 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
663 trash.str[argp[idx].data.str.len] = 0;
664 if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4))
665 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
666 argp[idx].type = ARGT_IPV4;
667 break;
668
669 case ARGT_MSK4:
670 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
671 trash.str[argp[idx].data.str.len] = 0;
672 if (!str2mask(trash.str, &argp[idx].data.ipv4))
673 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
674 argp[idx].type = ARGT_MSK4;
675 break;
676
677 case ARGT_IPV6:
678 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
679 trash.str[argp[idx].data.str.len] = 0;
680 if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6))
681 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
682 argp[idx].type = ARGT_IPV6;
683 break;
684
685 case ARGT_MSK6:
686 case ARGT_MAP:
687 case ARGT_REG:
688 case ARGT_USR:
689 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100690 break;
691 }
692
693 /* Check for type of argument. */
694 if ((mask & ARGT_MASK) != argp[idx].type) {
695 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
696 arg_type_names[(mask & ARGT_MASK)],
697 arg_type_names[argp[idx].type & ARGT_MASK]);
698 WILL_LJMP(luaL_argerror(L, first + idx, msg));
699 }
700
701 /* Next argument. */
702 mask >>= ARGT_BITS;
703 idx++;
704 }
705}
706
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100707/*
708 * The following functions are used to make correspondance between the the
709 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100710 *
711 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100712 * - hlua_sethlua : create the association between hlua context and lua_state.
713 */
714static inline struct hlua *hlua_gethlua(lua_State *L)
715{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100716 struct hlua **hlua = lua_getextraspace(L);
717 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100718}
719static inline void hlua_sethlua(struct hlua *hlua)
720{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100721 struct hlua **hlua_store = lua_getextraspace(hlua->T);
722 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100723}
724
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100725/* This function is used to send logs. It try to send on screen (stderr)
726 * and on the default syslog server.
727 */
728static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
729{
730 struct tm tm;
731 char *p;
732
733 /* Cleanup the log message. */
734 p = trash.str;
735 for (; *msg != '\0'; msg++, p++) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200736 if (p >= trash.str + trash.size - 1) {
737 /* Break the message if exceed the buffer size. */
738 *(p-4) = ' ';
739 *(p-3) = '.';
740 *(p-2) = '.';
741 *(p-1) = '.';
742 break;
743 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100744 if (isprint(*msg))
745 *p = *msg;
746 else
747 *p = '.';
748 }
749 *p = '\0';
750
Thierry FOURNIER5554e292015-09-09 11:21:37 +0200751 send_log(px, level, "%s\n", trash.str);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100752 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200753 get_localtime(date.tv_sec, &tm);
754 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100755 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
756 (int)getpid(), trash.str);
757 fflush(stderr);
758 }
759}
760
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100761/* This function just ensure that the yield will be always
762 * returned with a timeout and permit to set some flags
763 */
764__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100765 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100766{
767 struct hlua *hlua = hlua_gethlua(L);
768
769 /* Set the wake timeout. If timeout is required, we set
770 * the expiration time.
771 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200772 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100773
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100774 hlua->flags |= flags;
775
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100776 /* Process the yield. */
777 WILL_LJMP(lua_yieldk(L, nresults, ctx, k));
778}
779
Willy Tarreau87b09662015-04-03 00:22:06 +0200780/* This function initialises the Lua environment stored in the stream.
781 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100782 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200783 *
784 * This function is particular. it initialises a new Lua thread. If the
785 * initialisation fails (example: out of memory error), the lua function
786 * throws an error (longjmp).
787 *
788 * This function manipulates two Lua stack: the main and the thread. Only
789 * the main stack can fail. The thread is not manipulated. This function
790 * MUST NOT manipulate the created thread stack state, because is not
791 * proctected agains error throwed by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100792 */
793int hlua_ctx_init(struct hlua *lua, struct task *task)
794{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200795 if (!SET_SAFE_LJMP(gL.T)) {
796 lua->Tref = LUA_REFNIL;
797 return 0;
798 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100799 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100800 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100801 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100802 lua->T = lua_newthread(gL.T);
803 if (!lua->T) {
804 lua->Tref = LUA_REFNIL;
Thierry FOURNIER0a976202017-07-12 11:18:00 +0200805 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100806 return 0;
807 }
808 hlua_sethlua(lua);
809 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
810 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200811 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100812 return 1;
813}
814
Willy Tarreau87b09662015-04-03 00:22:06 +0200815/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100816 * is destroyed. The destroy also the memory context. The struct "lua"
817 * is not freed.
818 */
819void hlua_ctx_destroy(struct hlua *lua)
820{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100821 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100822 return;
823
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100824 if (!lua->T)
825 goto end;
826
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100827 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200828 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100829
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200830 if (!SET_SAFE_LJMP(lua->T))
831 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100832 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200833 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200834
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200835 if (!SET_SAFE_LJMP(gL.T))
836 return;
837 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
838 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200839 /* Forces a garbage collecting process. If the Lua program is finished
840 * without error, we run the GC on the thread pointer. Its freed all
841 * the unused memory.
842 * If the thread is finnish with an error or is currently yielded,
843 * it seems that the GC applied on the thread doesn't clean anything,
844 * so e run the GC on the main thread.
845 * NOTE: maybe this action locks all the Lua threads untiml the en of
846 * the garbage collection.
847 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200848 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200849 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200850 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200851 lua_gc(gL.T, LUA_GCCOLLECT, 0);
852 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200853 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200854
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200855 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100856
857end:
858 pool_free2(pool2_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100859}
860
861/* This function is used to restore the Lua context when a coroutine
862 * fails. This function copy the common memory between old coroutine
863 * and the new coroutine. The old coroutine is destroyed, and its
864 * replaced by the new coroutine.
865 * If the flag "keep_msg" is set, the last entry of the old is assumed
866 * as string error message and it is copied in the new stack.
867 */
868static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
869{
870 lua_State *T;
871 int new_ref;
872
873 /* Renew the main LUA stack doesn't have sense. */
874 if (lua == &gL)
875 return 0;
876
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100877 /* New Lua coroutine. */
878 T = lua_newthread(gL.T);
879 if (!T)
880 return 0;
881
882 /* Copy last error message. */
883 if (keep_msg)
884 lua_xmove(lua->T, T, 1);
885
886 /* Copy data between the coroutines. */
887 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
888 lua_xmove(lua->T, T, 1);
889 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
890
891 /* Destroy old data. */
892 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
893
894 /* The thread is garbage collected by Lua. */
895 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
896
897 /* Fill the struct with the new coroutine values. */
898 lua->Mref = new_ref;
899 lua->T = T;
900 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
901
902 /* Set context. */
903 hlua_sethlua(lua);
904
905 return 1;
906}
907
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100908void hlua_hook(lua_State *L, lua_Debug *ar)
909{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100910 struct hlua *hlua = hlua_gethlua(L);
911
912 /* Lua cannot yield when its returning from a function,
913 * so, we can fix the interrupt hook to 1 instruction,
914 * expecting that the function is finnished.
915 */
916 if (lua_gethookmask(L) & LUA_MASKRET) {
917 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
918 return;
919 }
920
921 /* restore the interrupt condition. */
922 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
923
924 /* If we interrupt the Lua processing in yieldable state, we yield.
925 * If the state is not yieldable, trying yield causes an error.
926 */
927 if (lua_isyieldable(L))
928 WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
929
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +0100930 /* If we cannot yield, update the clock and check the timeout. */
931 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200932 hlua->run_time += now_ms - hlua->start_time;
933 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100934 lua_pushfstring(L, "execution timeout");
935 WILL_LJMP(lua_error(L));
936 }
937
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200938 /* Update the start time. */
939 hlua->start_time = now_ms;
940
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100941 /* Try to interrupt the process at the end of the current
942 * unyieldable function.
943 */
944 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100945}
946
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100947/* This function start or resumes the Lua stack execution. If the flag
948 * "yield_allowed" if no set and the LUA stack execution returns a yield
949 * The function return an error.
950 *
951 * The function can returns 4 values:
952 * - HLUA_E_OK : The execution is terminated without any errors.
953 * - HLUA_E_AGAIN : The execution must continue at the next associated
954 * task wakeup.
955 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
956 * the top of the stack.
957 * - HLUA_E_ERR : An error has occured without error message.
958 *
959 * If an error occured, the stack is renewed and it is ready to run new
960 * LUA code.
961 */
962static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
963{
964 int ret;
965 const char *msg;
966
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200967 /* Initialise run time counter. */
968 if (!HLUA_IS_RUNNING(lua))
969 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100970
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100971resume_execution:
972
973 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
974 * instructions. it is used for preventing infinite loops.
975 */
976 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
977
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +0100978 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +0200979 HLUA_SET_RUN(lua);
980 HLUA_CLR_CTRLYIELD(lua);
981 HLUA_CLR_WAKERESWR(lua);
982 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +0100983
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200984 /* Update the start time. */
985 lua->start_time = now_ms;
986
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100987 /* Call the function. */
988 ret = lua_resume(lua->T, gL.T, lua->nargs);
989 switch (ret) {
990
991 case LUA_OK:
992 ret = HLUA_E_OK;
993 break;
994
995 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100996 /* Check if the execution timeout is expired. It it is the case, we
997 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100998 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200999 tv_update_date(0, 1);
1000 lua->run_time += now_ms - lua->start_time;
1001 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001002 lua_settop(lua->T, 0); /* Empty the stack. */
1003 if (!lua_checkstack(lua->T, 1)) {
1004 ret = HLUA_E_ERR;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001005 break;
1006 }
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001007 lua_pushfstring(lua->T, "execution timeout");
1008 ret = HLUA_E_ERRMSG;
1009 break;
1010 }
1011 /* Process the forced yield. if the general yield is not allowed or
1012 * if no task were associated this the current Lua execution
1013 * coroutine, we resume the execution. Else we want to return in the
1014 * scheduler and we want to be waked up again, to continue the
1015 * current Lua execution. So we schedule our own task.
1016 */
1017 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001018 if (!yield_allowed || !lua->task)
1019 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001020 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001021 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001022 if (!yield_allowed) {
1023 lua_settop(lua->T, 0); /* Empty the stack. */
1024 if (!lua_checkstack(lua->T, 1)) {
1025 ret = HLUA_E_ERR;
1026 break;
1027 }
1028 lua_pushfstring(lua->T, "yield not allowed");
1029 ret = HLUA_E_ERRMSG;
1030 break;
1031 }
1032 ret = HLUA_E_AGAIN;
1033 break;
1034
1035 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001036
1037 /* Special exit case. The traditionnal exit is returned as an error
1038 * because the errors ares the only one mean to return immediately
1039 * from and lua execution.
1040 */
1041 if (lua->flags & HLUA_EXIT) {
1042 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001043 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001044 break;
1045 }
1046
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001047 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001048 if (!lua_checkstack(lua->T, 1)) {
1049 ret = HLUA_E_ERR;
1050 break;
1051 }
1052 msg = lua_tostring(lua->T, -1);
1053 lua_settop(lua->T, 0); /* Empty the stack. */
1054 lua_pop(lua->T, 1);
1055 if (msg)
1056 lua_pushfstring(lua->T, "runtime error: %s", msg);
1057 else
1058 lua_pushfstring(lua->T, "unknown runtime error");
1059 ret = HLUA_E_ERRMSG;
1060 break;
1061
1062 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001063 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001064 lua_settop(lua->T, 0); /* Empty the stack. */
1065 if (!lua_checkstack(lua->T, 1)) {
1066 ret = HLUA_E_ERR;
1067 break;
1068 }
1069 lua_pushfstring(lua->T, "out of memory error");
1070 ret = HLUA_E_ERRMSG;
1071 break;
1072
1073 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001074 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001075 if (!lua_checkstack(lua->T, 1)) {
1076 ret = HLUA_E_ERR;
1077 break;
1078 }
1079 msg = lua_tostring(lua->T, -1);
1080 lua_settop(lua->T, 0); /* Empty the stack. */
1081 lua_pop(lua->T, 1);
1082 if (msg)
1083 lua_pushfstring(lua->T, "message handler error: %s", msg);
1084 else
1085 lua_pushfstring(lua->T, "message handler error");
1086 ret = HLUA_E_ERRMSG;
1087 break;
1088
1089 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001090 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001091 lua_settop(lua->T, 0); /* Empty the stack. */
1092 if (!lua_checkstack(lua->T, 1)) {
1093 ret = HLUA_E_ERR;
1094 break;
1095 }
1096 lua_pushfstring(lua->T, "unknonwn error");
1097 ret = HLUA_E_ERRMSG;
1098 break;
1099 }
1100
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001101 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001102 if (lua->flags & HLUA_MUST_GC &&
1103 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001104 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1105
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001106 switch (ret) {
1107 case HLUA_E_AGAIN:
1108 break;
1109
1110 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001111 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001112 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001113 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001114 break;
1115
1116 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001117 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001118 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001119 hlua_ctx_renew(lua, 0);
1120 break;
1121
1122 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001123 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001124 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001125 break;
1126 }
1127
1128 return ret;
1129}
1130
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001131/* This function exit the current code. */
1132__LJMP static int hlua_done(lua_State *L)
1133{
1134 struct hlua *hlua = hlua_gethlua(L);
1135
1136 hlua->flags |= HLUA_EXIT;
1137 WILL_LJMP(lua_error(L));
1138
1139 return 0;
1140}
1141
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001142/* This function is an LUA binding. It provides a function
1143 * for deleting ACL from a referenced ACL file.
1144 */
1145__LJMP static int hlua_del_acl(lua_State *L)
1146{
1147 const char *name;
1148 const char *key;
1149 struct pat_ref *ref;
1150
1151 MAY_LJMP(check_args(L, 2, "del_acl"));
1152
1153 name = MAY_LJMP(luaL_checkstring(L, 1));
1154 key = MAY_LJMP(luaL_checkstring(L, 2));
1155
1156 ref = pat_ref_lookup(name);
1157 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001158 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001159
1160 pat_ref_delete(ref, key);
1161 return 0;
1162}
1163
1164/* This function is an LUA binding. It provides a function
1165 * for deleting map entry from a referenced map file.
1166 */
1167static int hlua_del_map(lua_State *L)
1168{
1169 const char *name;
1170 const char *key;
1171 struct pat_ref *ref;
1172
1173 MAY_LJMP(check_args(L, 2, "del_map"));
1174
1175 name = MAY_LJMP(luaL_checkstring(L, 1));
1176 key = MAY_LJMP(luaL_checkstring(L, 2));
1177
1178 ref = pat_ref_lookup(name);
1179 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001180 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001181
1182 pat_ref_delete(ref, key);
1183 return 0;
1184}
1185
1186/* This function is an LUA binding. It provides a function
1187 * for adding ACL pattern from a referenced ACL file.
1188 */
1189static int hlua_add_acl(lua_State *L)
1190{
1191 const char *name;
1192 const char *key;
1193 struct pat_ref *ref;
1194
1195 MAY_LJMP(check_args(L, 2, "add_acl"));
1196
1197 name = MAY_LJMP(luaL_checkstring(L, 1));
1198 key = MAY_LJMP(luaL_checkstring(L, 2));
1199
1200 ref = pat_ref_lookup(name);
1201 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001202 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001203
1204 if (pat_ref_find_elt(ref, key) == NULL)
1205 pat_ref_add(ref, key, NULL, NULL);
1206 return 0;
1207}
1208
1209/* This function is an LUA binding. It provides a function
1210 * for setting map pattern and sample from a referenced map
1211 * file.
1212 */
1213static int hlua_set_map(lua_State *L)
1214{
1215 const char *name;
1216 const char *key;
1217 const char *value;
1218 struct pat_ref *ref;
1219
1220 MAY_LJMP(check_args(L, 3, "set_map"));
1221
1222 name = MAY_LJMP(luaL_checkstring(L, 1));
1223 key = MAY_LJMP(luaL_checkstring(L, 2));
1224 value = MAY_LJMP(luaL_checkstring(L, 3));
1225
1226 ref = pat_ref_lookup(name);
1227 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001228 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001229
1230 if (pat_ref_find_elt(ref, key) != NULL)
1231 pat_ref_set(ref, key, value, NULL);
1232 else
1233 pat_ref_add(ref, key, value, NULL);
1234 return 0;
1235}
1236
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001237/* A class is a lot of memory that contain data. This data can be a table,
1238 * an integer or user data. This data is associated with a metatable. This
1239 * metatable have an original version registred in the global context with
1240 * the name of the object (_G[<name>] = <metable> ).
1241 *
1242 * A metable is a table that modify the standard behavior of a standard
1243 * access to the associated data. The entries of this new metatable are
1244 * defined as is:
1245 *
1246 * http://lua-users.org/wiki/MetatableEvents
1247 *
1248 * __index
1249 *
1250 * we access an absent field in a table, the result is nil. This is
1251 * true, but it is not the whole truth. Actually, such access triggers
1252 * the interpreter to look for an __index metamethod: If there is no
1253 * such method, as usually happens, then the access results in nil;
1254 * otherwise, the metamethod will provide the result.
1255 *
1256 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1257 * the key does not appear in the table, but the metatable has an __index
1258 * property:
1259 *
1260 * - if the value is a function, the function is called, passing in the
1261 * table and the key; the return value of that function is returned as
1262 * the result.
1263 *
1264 * - if the value is another table, the value of the key in that table is
1265 * asked for and returned (and if it doesn't exist in that table, but that
1266 * table's metatable has an __index property, then it continues on up)
1267 *
1268 * - Use "rawget(myTable,key)" to skip this metamethod.
1269 *
1270 * http://www.lua.org/pil/13.4.1.html
1271 *
1272 * __newindex
1273 *
1274 * Like __index, but control property assignment.
1275 *
1276 * __mode - Control weak references. A string value with one or both
1277 * of the characters 'k' and 'v' which specifies that the the
1278 * keys and/or values in the table are weak references.
1279 *
1280 * __call - Treat a table like a function. When a table is followed by
1281 * parenthesis such as "myTable( 'foo' )" and the metatable has
1282 * a __call key pointing to a function, that function is invoked
1283 * (passing any specified arguments) and the return value is
1284 * returned.
1285 *
1286 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1287 * called, if the metatable for myTable has a __metatable
1288 * key, the value of that key is returned instead of the
1289 * actual metatable.
1290 *
1291 * __tostring - Control string representation. When the builtin
1292 * "tostring( myTable )" function is called, if the metatable
1293 * for myTable has a __tostring property set to a function,
1294 * that function is invoked (passing myTable to it) and the
1295 * return value is used as the string representation.
1296 *
1297 * __len - Control table length. When the table length is requested using
1298 * the length operator ( '#' ), if the metatable for myTable has
1299 * a __len key pointing to a function, that function is invoked
1300 * (passing myTable to it) and the return value used as the value
1301 * of "#myTable".
1302 *
1303 * __gc - Userdata finalizer code. When userdata is set to be garbage
1304 * collected, if the metatable has a __gc field pointing to a
1305 * function, that function is first invoked, passing the userdata
1306 * to it. The __gc metamethod is not called for tables.
1307 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1308 *
1309 * Special metamethods for redefining standard operators:
1310 * http://www.lua.org/pil/13.1.html
1311 *
1312 * __add "+"
1313 * __sub "-"
1314 * __mul "*"
1315 * __div "/"
1316 * __unm "!"
1317 * __pow "^"
1318 * __concat ".."
1319 *
1320 * Special methods for redfining standar relations
1321 * http://www.lua.org/pil/13.2.html
1322 *
1323 * __eq "=="
1324 * __lt "<"
1325 * __le "<="
1326 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001327
1328/*
1329 *
1330 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001331 * Class Map
1332 *
1333 *
1334 */
1335
1336/* Returns a struct hlua_map if the stack entry "ud" is
1337 * a class session, otherwise it throws an error.
1338 */
1339__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1340{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001341 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001342}
1343
1344/* This function is the map constructor. It don't need
1345 * the class Map object. It creates and return a new Map
1346 * object. It must be called only during "body" or "init"
1347 * context because it process some filesystem accesses.
1348 */
1349__LJMP static int hlua_map_new(struct lua_State *L)
1350{
1351 const char *fn;
1352 int match = PAT_MATCH_STR;
1353 struct sample_conv conv;
1354 const char *file = "";
1355 int line = 0;
1356 lua_Debug ar;
1357 char *err = NULL;
1358 struct arg args[2];
1359
1360 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1361 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1362
1363 fn = MAY_LJMP(luaL_checkstring(L, 1));
1364
1365 if (lua_gettop(L) >= 2) {
1366 match = MAY_LJMP(luaL_checkinteger(L, 2));
1367 if (match < 0 || match >= PAT_MATCH_NUM)
1368 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1369 }
1370
1371 /* Get Lua filename and line number. */
1372 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1373 lua_getinfo(L, "Sl", &ar); /* get info about it */
1374 if (ar.currentline > 0) { /* is there info? */
1375 file = ar.short_src;
1376 line = ar.currentline;
1377 }
1378 }
1379
1380 /* fill fake sample_conv struct. */
1381 conv.kw = ""; /* unused. */
1382 conv.process = NULL; /* unused. */
1383 conv.arg_mask = 0; /* unused. */
1384 conv.val_args = NULL; /* unused. */
1385 conv.out_type = SMP_T_STR;
1386 conv.private = (void *)(long)match;
1387 switch (match) {
1388 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1389 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1390 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1391 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1392 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1393 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1394 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001395 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001396 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1397 default:
1398 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1399 }
1400
1401 /* fill fake args. */
1402 args[0].type = ARGT_STR;
1403 args[0].data.str.str = (char *)fn;
1404 args[1].type = ARGT_STOP;
1405
1406 /* load the map. */
1407 if (!sample_load_map(args, &conv, file, line, &err)) {
1408 /* error case: we cant use luaL_error because we must
1409 * free the err variable.
1410 */
1411 luaL_where(L, 1);
1412 lua_pushfstring(L, "'new': %s.", err);
1413 lua_concat(L, 2);
1414 free(err);
1415 WILL_LJMP(lua_error(L));
1416 }
1417
1418 /* create the lua object. */
1419 lua_newtable(L);
1420 lua_pushlightuserdata(L, args[0].data.map);
1421 lua_rawseti(L, -2, 0);
1422
1423 /* Pop a class Map metatable and affect it to the userdata. */
1424 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1425 lua_setmetatable(L, -2);
1426
1427
1428 return 1;
1429}
1430
1431__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1432{
1433 struct map_descriptor *desc;
1434 struct pattern *pat;
1435 struct sample smp;
1436
1437 MAY_LJMP(check_args(L, 2, "lookup"));
1438 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001439 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001440 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001441 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001442 }
1443 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001444 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001445 smp.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001446 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 +02001447 }
1448
1449 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001450 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001451 if (str)
1452 lua_pushstring(L, "");
1453 else
1454 lua_pushnil(L);
1455 return 1;
1456 }
1457
1458 /* The Lua pattern must return a string, so we can't check the returned type */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001459 lua_pushlstring(L, pat->data->u.str.str, pat->data->u.str.len);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001460 return 1;
1461}
1462
1463__LJMP static int hlua_map_lookup(struct lua_State *L)
1464{
1465 return _hlua_map_lookup(L, 0);
1466}
1467
1468__LJMP static int hlua_map_slookup(struct lua_State *L)
1469{
1470 return _hlua_map_lookup(L, 1);
1471}
1472
1473/*
1474 *
1475 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001476 * Class Socket
1477 *
1478 *
1479 */
1480
1481__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1482{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001483 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001484}
1485
1486/* This function is the handler called for each I/O on the established
1487 * connection. It is used for notify space avalaible to send or data
1488 * received.
1489 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001490static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001491{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001492 struct stream_interface *si = appctx->owner;
Willy Tarreau50fe03b2014-11-28 13:59:31 +01001493 struct connection *c = objt_conn(si_opposite(si)->end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001494
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001495 if (appctx->ctx.hlua_cosocket.die) {
1496 si_shutw(si);
1497 si_shutr(si);
1498 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001499 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1500 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001501 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1502 }
1503
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001504 /* If the connection object is not avalaible, close all the
1505 * streams and wakeup everithing waiting for.
1506 */
1507 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001508 si_shutw(si);
1509 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001510 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001511 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1512 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001513 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001514 }
1515
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001516 /* If we cant write, wakeup the pending write signals. */
1517 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001518 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001519
1520 /* If we cant read, wakeup the pending read signals. */
1521 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001522 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001523
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001524 /* if the connection is not estabkished, inform the stream that we want
1525 * to be notified whenever the connection completes.
1526 */
1527 if (!(c->flags & CO_FL_CONNECTED)) {
1528 si_applet_cant_get(si);
1529 si_applet_cant_put(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001530 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001531 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001532
1533 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001534 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001535
1536 /* Wake the tasks which wants to write if the buffer have avalaible space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001537 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001538 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001539
1540 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001541 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001542 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001543}
1544
Willy Tarreau87b09662015-04-03 00:22:06 +02001545/* This function is called when the "struct stream" is destroyed.
1546 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001547 * Wake all the pending signals.
1548 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001549static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001550{
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001551 /* Remove my link in the original object. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001552 xref_disconnect(&appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001553
1554 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001555 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1556 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001557}
1558
1559/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001560 * uses this object. If the stream does not exists, just quit.
1561 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001562 * pending signal can rest in the read and write lists. destroy
1563 * it.
1564 */
1565__LJMP static int hlua_socket_gc(lua_State *L)
1566{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001567 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001568 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001569 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001570
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001571 MAY_LJMP(check_args(L, 1, "__gc"));
1572
1573 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001574 peer = xref_get_peer(&socket->xref);
1575 if (!peer) {
1576 xref_disconnect(&socket->xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001577 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001578 }
1579 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001580
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001581 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001582 appctx->ctx.hlua_cosocket.die = 1;
1583 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001584
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001585 /* Remove all reference between the Lua stack and the coroutine stream. */
1586 xref_disconnect(&socket->xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001587 return 0;
1588}
1589
1590/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001591 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001592 */
1593__LJMP static int hlua_socket_close(lua_State *L)
1594{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001595 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001596 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001597 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001598
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001599 MAY_LJMP(check_args(L, 1, "close"));
1600
1601 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001602 peer = xref_get_peer(&socket->xref);
1603 if (!peer) {
1604 xref_disconnect(&socket->xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001605 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001606 }
1607 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001608
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001609 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001610 appctx->ctx.hlua_cosocket.die = 1;
1611 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001612
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001613 /* Remove all reference between the Lua stack and the coroutine stream. */
1614 xref_disconnect(&socket->xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001615 return 0;
1616}
1617
1618/* This Lua function assumes that the stack contain three parameters.
1619 * 1 - USERDATA containing a struct socket
1620 * 2 - INTEGER with values of the macro defined below
1621 * If the integer is -1, we must read at most one line.
1622 * If the integer is -2, we ust read all the data until the
1623 * end of the stream.
1624 * If the integer is positive value, we must read a number of
1625 * bytes corresponding to this value.
1626 */
1627#define HLSR_READ_LINE (-1)
1628#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001629__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001630{
1631 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1632 int wanted = lua_tointeger(L, 2);
1633 struct hlua *hlua = hlua_gethlua(L);
1634 struct appctx *appctx;
1635 int len;
1636 int nblk;
1637 char *blk1;
1638 int len1;
1639 char *blk2;
1640 int len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001641 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001642 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001643 struct stream_interface *si;
1644 struct stream *s;
1645 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001646
1647 /* Check if this lua stack is schedulable. */
1648 if (!hlua || !hlua->task)
1649 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1650 "'frontend', 'backend' or 'task'"));
1651
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001652 /* check for connection break. If some data where read, return it. */
1653 peer = xref_get_peer(&socket->xref);
1654 if (!peer) {
1655 xref_disconnect(&socket->xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001656 goto connection_closed;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001657 }
1658 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1659 si = appctx->owner;
1660 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001661
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001662 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001663 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001664 /* Read line. */
Willy Tarreau81389672015-03-10 12:03:52 +01001665 nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001666 if (nblk < 0) /* Connection close. */
1667 goto connection_closed;
1668 if (nblk == 0) /* No data avalaible. */
1669 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001670
1671 /* remove final \r\n. */
1672 if (nblk == 1) {
1673 if (blk1[len1-1] == '\n') {
1674 len1--;
1675 skip_at_end++;
1676 if (blk1[len1-1] == '\r') {
1677 len1--;
1678 skip_at_end++;
1679 }
1680 }
1681 }
1682 else {
1683 if (blk2[len2-1] == '\n') {
1684 len2--;
1685 skip_at_end++;
1686 if (blk2[len2-1] == '\r') {
1687 len2--;
1688 skip_at_end++;
1689 }
1690 }
1691 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001692 }
1693
1694 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001695 /* Read all the available data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001696 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001697 if (nblk < 0) /* Connection close. */
1698 goto connection_closed;
1699 if (nblk == 0) /* No data avalaible. */
1700 goto connection_empty;
1701 }
1702
1703 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001704 /* Read a block of data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001705 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001706 if (nblk < 0) /* Connection close. */
1707 goto connection_closed;
1708 if (nblk == 0) /* No data avalaible. */
1709 goto connection_empty;
1710
1711 if (len1 > wanted) {
1712 nblk = 1;
1713 len1 = wanted;
1714 } if (nblk == 2 && len1 + len2 > wanted)
1715 len2 = wanted - len1;
1716 }
1717
1718 len = len1;
1719
1720 luaL_addlstring(&socket->b, blk1, len1);
1721 if (nblk == 2) {
1722 len += len2;
1723 luaL_addlstring(&socket->b, blk2, len2);
1724 }
1725
1726 /* Consume data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001727 bo_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001728
1729 /* Don't wait anything. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001730 stream_int_notify(&s->si[0]);
1731 stream_int_update_applet(&s->si[0]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001732
1733 /* If the pattern reclaim to read all the data
1734 * in the connection, got out.
1735 */
1736 if (wanted == HLSR_READ_ALL)
1737 goto connection_empty;
1738 else if (wanted >= 0 && len < wanted)
1739 goto connection_empty;
1740
1741 /* Return result. */
1742 luaL_pushresult(&socket->b);
1743 return 1;
1744
1745connection_closed:
1746
1747 /* If the buffer containds data. */
1748 if (socket->b.n > 0) {
1749 luaL_pushresult(&socket->b);
1750 return 1;
1751 }
1752 lua_pushnil(L);
1753 lua_pushstring(L, "connection closed.");
1754 return 2;
1755
1756connection_empty:
1757
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001758 appctx = objt_appctx(s->si[0].end);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001759 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001760 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001761 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001762 return 0;
1763}
1764
1765/* This Lus function gets two parameters. The first one can be string
1766 * or a number. If the string is "*l", the user require one line. If
1767 * the string is "*a", the user require all the content of the stream.
1768 * If the value is a number, the user require a number of bytes equal
1769 * to the value. The default value is "*l" (a line).
1770 *
1771 * This paraeter with a variable type is converted in integer. This
1772 * integer takes this values:
1773 * -1 : read a line
1774 * -2 : read all the stream
1775 * >0 : amount if bytes.
1776 *
1777 * The second parameter is optinal. It contains a string that must be
1778 * concatenated with the read data.
1779 */
1780__LJMP static int hlua_socket_receive(struct lua_State *L)
1781{
1782 int wanted = HLSR_READ_LINE;
1783 const char *pattern;
1784 int type;
1785 char *error;
1786 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001787 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001788
1789 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1790 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1791
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001792 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793
1794 /* check for pattern. */
1795 if (lua_gettop(L) >= 2) {
1796 type = lua_type(L, 2);
1797 if (type == LUA_TSTRING) {
1798 pattern = lua_tostring(L, 2);
1799 if (strcmp(pattern, "*a") == 0)
1800 wanted = HLSR_READ_ALL;
1801 else if (strcmp(pattern, "*l") == 0)
1802 wanted = HLSR_READ_LINE;
1803 else {
1804 wanted = strtoll(pattern, &error, 10);
1805 if (*error != '\0')
1806 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1807 }
1808 }
1809 else if (type == LUA_TNUMBER) {
1810 wanted = lua_tointeger(L, 2);
1811 if (wanted < 0)
1812 WILL_LJMP(luaL_error(L, "Unsupported size."));
1813 }
1814 }
1815
1816 /* Set pattern. */
1817 lua_pushinteger(L, wanted);
1818 lua_replace(L, 2);
1819
1820 /* init bufffer, and fiil it wih prefix. */
1821 luaL_buffinit(L, &socket->b);
1822
1823 /* Check prefix. */
1824 if (lua_gettop(L) >= 3) {
1825 if (lua_type(L, 3) != LUA_TSTRING)
1826 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1827 pattern = lua_tolstring(L, 3, &len);
1828 luaL_addlstring(&socket->b, pattern, len);
1829 }
1830
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001831 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832}
1833
1834/* Write the Lua input string in the output buffer.
1835 * This fucntion returns a yield if no space are available.
1836 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001837static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001838{
1839 struct hlua_socket *socket;
1840 struct hlua *hlua = hlua_gethlua(L);
1841 struct appctx *appctx;
1842 size_t buf_len;
1843 const char *buf;
1844 int len;
1845 int send_len;
1846 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001847 struct xref *peer;
1848 struct stream_interface *si;
1849 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001850
1851 /* Check if this lua stack is schedulable. */
1852 if (!hlua || !hlua->task)
1853 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1854 "'frontend', 'backend' or 'task'"));
1855
1856 /* Get object */
1857 socket = MAY_LJMP(hlua_checksocket(L, 1));
1858 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001859 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001860
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001861 /* check for connection break. If some data where read, return it. */
1862 peer = xref_get_peer(&socket->xref);
1863 if (!peer) {
1864 xref_disconnect(&socket->xref);
1865 lua_pushinteger(L, -1);
1866 return 1;
1867 }
1868 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1869 si = appctx->owner;
1870 s = si_strm(si);
1871
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001872 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001873 if (channel_output_closed(&s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001874 lua_pushinteger(L, -1);
1875 return 1;
1876 }
1877
1878 /* Update the input buffer data. */
1879 buf += sent;
1880 send_len = buf_len - sent;
1881
1882 /* All the data are sent. */
1883 if (sent >= buf_len)
1884 return 1; /* Implicitly return the length sent. */
1885
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001886 /* Check if the buffer is avalaible because HAProxy doesn't allocate
1887 * the request buffer if its not required.
1888 */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001889 if (s->req.buf->size == 0) {
Christopher Faulet33834b12016-12-19 09:29:06 +01001890 appctx = hlua->task->context;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001891 if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01001892 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001893 }
1894
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001895 /* Check for avalaible space. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001896 len = buffer_total_space(s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01001897 if (len <= 0) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001898 appctx = objt_appctx(s->si[0].end);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001899 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task))
Christopher Faulet33834b12016-12-19 09:29:06 +01001900 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001901 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01001902 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001903
1904 /* send data */
1905 if (len < send_len)
1906 send_len = len;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001907 len = bi_putblk(&s->req, buf+sent, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908
1909 /* "Not enough space" (-1), "Buffer too little to contain
1910 * the data" (-2) are not expected because the available length
1911 * is tested.
1912 * Other unknown error are also not expected.
1913 */
1914 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01001915 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001916 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001917
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001918 MAY_LJMP(hlua_socket_close(L));
1919 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001920 lua_pushinteger(L, -1);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001921 return 1;
1922 }
1923
1924 /* update buffers. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001925 stream_int_notify(&s->si[0]);
1926 stream_int_update_applet(&s->si[0]);
Willy Tarreaude70fa12015-09-26 11:25:05 +02001927
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001928 s->req.rex = TICK_ETERNITY;
1929 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001930
1931 /* Update length sent. */
1932 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001933 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001934
1935 /* All the data buffer is sent ? */
1936 if (sent + len >= buf_len)
1937 return 1;
1938
1939hlua_socket_write_yield_return:
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001940 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001941 return 0;
1942}
1943
1944/* This function initiate the send of data. It just check the input
1945 * parameters and push an integer in the Lua stack that contain the
1946 * amount of data writed in the buffer. This is used by the function
1947 * "hlua_socket_write_yield" that can yield.
1948 *
1949 * The Lua function gets between 3 and 4 parameters. The first one is
1950 * the associated object. The second is a string buffer. The third is
1951 * a facultative integer that represents where is the buffer position
1952 * of the start of the data that can send. The first byte is the
1953 * position "1". The default value is "1". The fourth argument is a
1954 * facultative integer that represents where is the buffer position
1955 * of the end of the data that can send. The default is the last byte.
1956 */
1957static int hlua_socket_send(struct lua_State *L)
1958{
1959 int i;
1960 int j;
1961 const char *buf;
1962 size_t buf_len;
1963
1964 /* Check number of arguments. */
1965 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
1966 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
1967
1968 /* Get the string. */
1969 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
1970
1971 /* Get and check j. */
1972 if (lua_gettop(L) == 4) {
1973 j = MAY_LJMP(luaL_checkinteger(L, 4));
1974 if (j < 0)
1975 j = buf_len + j + 1;
1976 if (j > buf_len)
1977 j = buf_len + 1;
1978 lua_pop(L, 1);
1979 }
1980 else
1981 j = buf_len;
1982
1983 /* Get and check i. */
1984 if (lua_gettop(L) == 3) {
1985 i = MAY_LJMP(luaL_checkinteger(L, 3));
1986 if (i < 0)
1987 i = buf_len + i + 1;
1988 if (i > buf_len)
1989 i = buf_len + 1;
1990 lua_pop(L, 1);
1991 } else
1992 i = 1;
1993
1994 /* Check bth i and j. */
1995 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001996 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001997 return 1;
1998 }
1999 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002000 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002001 return 1;
2002 }
2003 if (i == 0)
2004 i = 1;
2005 if (j == 0)
2006 j = 1;
2007
2008 /* Pop the string. */
2009 lua_pop(L, 1);
2010
2011 /* Update the buffer length. */
2012 buf += i - 1;
2013 buf_len = j - i + 1;
2014 lua_pushlstring(L, buf, buf_len);
2015
2016 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002017 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002018
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002019 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002020}
2021
Willy Tarreau22b0a682015-06-17 19:43:49 +02002022#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002023__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2024{
2025 static char buffer[SOCKET_INFO_MAX_LEN];
2026 int ret;
2027 int len;
2028 char *p;
2029
2030 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2031 if (ret <= 0) {
2032 lua_pushnil(L);
2033 return 1;
2034 }
2035
2036 if (ret == AF_UNIX) {
2037 lua_pushstring(L, buffer+1);
2038 return 1;
2039 }
2040 else if (ret == AF_INET6) {
2041 buffer[0] = '[';
2042 len = strlen(buffer);
2043 buffer[len] = ']';
2044 len++;
2045 buffer[len] = ':';
2046 len++;
2047 p = buffer;
2048 }
2049 else if (ret == AF_INET) {
2050 p = buffer + 1;
2051 len = strlen(p);
2052 p[len] = ':';
2053 len++;
2054 }
2055 else {
2056 lua_pushnil(L);
2057 return 1;
2058 }
2059
2060 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2061 lua_pushnil(L);
2062 return 1;
2063 }
2064
2065 lua_pushstring(L, p);
2066 return 1;
2067}
2068
2069/* Returns information about the peer of the connection. */
2070__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2071{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002072 struct hlua_socket *socket;
2073 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002074 struct xref *peer;
2075 struct appctx *appctx;
2076 struct stream_interface *si;
2077 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002078
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002079 MAY_LJMP(check_args(L, 1, "getpeername"));
2080
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002081 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002082
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002083 /* check for connection break. If some data where read, return it. */
2084 peer = xref_get_peer(&socket->xref);
2085 if (!peer) {
2086 xref_disconnect(&socket->xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002087 lua_pushnil(L);
2088 return 1;
2089 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002090 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2091 si = appctx->owner;
2092 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002093
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002094 conn = objt_conn(s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002095 if (!conn) {
2096 lua_pushnil(L);
2097 return 1;
2098 }
2099
Willy Tarreaua71f6422016-11-16 17:00:14 +01002100 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002101 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Willy Tarreaua71f6422016-11-16 17:00:14 +01002102 lua_pushnil(L);
2103 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002104 }
2105
2106 return MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2107}
2108
2109/* Returns information about my connection side. */
2110static int hlua_socket_getsockname(struct lua_State *L)
2111{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002112 struct hlua_socket *socket;
2113 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002114 struct appctx *appctx;
2115 struct xref *peer;
2116 struct stream_interface *si;
2117 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002118
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002119 MAY_LJMP(check_args(L, 1, "getsockname"));
2120
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002121 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002122
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002123 /* check for connection break. If some data where read, return it. */
2124 peer = xref_get_peer(&socket->xref);
2125 if (!peer) {
2126 xref_disconnect(&socket->xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002127 lua_pushnil(L);
2128 return 1;
2129 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002130 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2131 si = appctx->owner;
2132 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002133
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002134 conn = objt_conn(s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002135 if (!conn) {
2136 lua_pushnil(L);
2137 return 1;
2138 }
2139
Willy Tarreaua71f6422016-11-16 17:00:14 +01002140 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002141 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Willy Tarreaua71f6422016-11-16 17:00:14 +01002142 lua_pushnil(L);
2143 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002144 }
2145
2146 return hlua_socket_info(L, &conn->addr.from);
2147}
2148
2149/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002150static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002151 .obj_type = OBJ_TYPE_APPLET,
2152 .name = "<LUA_TCP>",
2153 .fct = hlua_socket_handler,
2154 .release = hlua_socket_release,
2155};
2156
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002157__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002158{
2159 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2160 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002161 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002162 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002163 struct stream_interface *si;
2164 struct stream *s;
2165
2166 /* check for connection break. If some data where read, return it. */
2167 peer = xref_get_peer(&socket->xref);
2168 if (!peer) {
2169 xref_disconnect(&socket->xref);
2170 lua_pushnil(L);
2171 lua_pushstring(L, "Can't connect");
2172 return 2;
2173 }
2174 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2175 si = appctx->owner;
2176 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002177
2178 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002179 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002180 lua_pushnil(L);
2181 lua_pushstring(L, "Can't connect");
2182 return 2;
2183 }
2184
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002185 appctx = objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002186
2187 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002188 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002189 lua_pushinteger(L, 1);
2190 return 1;
2191 }
2192
Thierry FOURNIERd6975962017-07-12 14:31:10 +02002193 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002194 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002195 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002196 return 0;
2197}
2198
2199/* This function fail or initite the connection. */
2200__LJMP static int hlua_socket_connect(struct lua_State *L)
2201{
2202 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002203 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002204 const char *ip;
2205 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002206 struct hlua *hlua;
2207 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002208 int low, high;
2209 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002210 struct xref *peer;
2211 struct stream_interface *si;
2212 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002213
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002214 if (lua_gettop(L) < 2)
2215 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002216
2217 /* Get args. */
2218 socket = MAY_LJMP(hlua_checksocket(L, 1));
2219 ip = MAY_LJMP(luaL_checkstring(L, 2));
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002220 if (lua_gettop(L) >= 3)
2221 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002222
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002223 /* check for connection break. If some data where read, return it. */
2224 peer = xref_get_peer(&socket->xref);
2225 if (!peer) {
2226 xref_disconnect(&socket->xref);
2227 lua_pushnil(L);
2228 return 1;
2229 }
2230 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2231 si = appctx->owner;
2232 s = si_strm(si);
2233
2234 /* Initialise connection. */
2235 conn = si_alloc_conn(&s->si[1]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002236 if (!conn)
2237 WILL_LJMP(luaL_error(L, "connect: internal error"));
2238
Willy Tarreau3adac082015-09-26 17:51:09 +02002239 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002240 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002241
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002242 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002243 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002244 if (!addr)
2245 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
2246 if (low != high)
2247 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
2248 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002249
2250 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002251 if (low == 0) {
2252 if (conn->addr.to.ss_family == AF_INET) {
2253 if (port == -1)
2254 WILL_LJMP(luaL_error(L, "connect: port missing"));
2255 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2256 } else if (conn->addr.to.ss_family == AF_INET6) {
2257 if (port == -1)
2258 WILL_LJMP(luaL_error(L, "connect: port missing"));
2259 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2260 }
2261 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002262
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002263 hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002264 appctx = objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002265
2266 /* inform the stream that we want to be notified whenever the
2267 * connection completes.
2268 */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002269 si_applet_cant_get(&s->si[0]);
2270 si_applet_cant_put(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002271 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002272
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002273 hlua->flags |= HLUA_MUST_GC;
2274
Thierry FOURNIERd6975962017-07-12 14:31:10 +02002275 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task))
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002276 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002277 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002278
2279 return 0;
2280}
2281
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002282#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002283__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2284{
2285 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002286 struct xref *peer;
2287 struct appctx *appctx;
2288 struct stream_interface *si;
2289 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002290
2291 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2292 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002293
2294 /* check for connection break. If some data where read, return it. */
2295 peer = xref_get_peer(&socket->xref);
2296 if (!peer) {
2297 xref_disconnect(&socket->xref);
2298 lua_pushnil(L);
2299 return 1;
2300 }
2301 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2302 si = appctx->owner;
2303 s = si_strm(si);
2304
2305 s->target = &socket_ssl.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002306 return MAY_LJMP(hlua_socket_connect(L));
2307}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002308#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002309
2310__LJMP static int hlua_socket_setoption(struct lua_State *L)
2311{
2312 return 0;
2313}
2314
2315__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2316{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002317 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002318 int tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002319 struct xref *peer;
2320 struct appctx *appctx;
2321 struct stream_interface *si;
2322 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002323
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002324 MAY_LJMP(check_args(L, 2, "settimeout"));
2325
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002326 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002327 tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002328
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002329 /* check for connection break. If some data where read, return it. */
2330 peer = xref_get_peer(&socket->xref);
2331 if (!peer) {
2332 xref_disconnect(&socket->xref);
2333 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2334 WILL_LJMP(lua_error(L));
2335 return 0;
2336 }
2337 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2338 si = appctx->owner;
2339 s = si_strm(si);
2340
2341 s->req.rto = tmout;
2342 s->req.wto = tmout;
2343 s->res.rto = tmout;
2344 s->res.wto = tmout;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002345
2346 return 0;
2347}
2348
2349__LJMP static int hlua_socket_new(lua_State *L)
2350{
2351 struct hlua_socket *socket;
2352 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002353 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002354 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002355
2356 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002357 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002358 hlua_pusherror(L, "socket: full stack");
2359 goto out_fail_conf;
2360 }
2361
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002362 /* Create the object: obj[0] = userdata. */
2363 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002364 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002365 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002366 memset(socket, 0, sizeof(*socket));
2367
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002368 /* Check if the various memory pools are intialized. */
Willy Tarreau87b09662015-04-03 00:22:06 +02002369 if (!pool2_stream || !pool2_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002370 hlua_pusherror(L, "socket: uninitialized pools.");
2371 goto out_fail_conf;
2372 }
2373
Willy Tarreau87b09662015-04-03 00:22:06 +02002374 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002375 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2376 lua_setmetatable(L, -2);
2377
Willy Tarreaud420a972015-04-06 00:39:18 +02002378 /* Create the applet context */
2379 appctx = appctx_new(&update_applet);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002380 if (!appctx) {
2381 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002382 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002383 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002384
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002385 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002386 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002387 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2388 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002389
Willy Tarreaud420a972015-04-06 00:39:18 +02002390 /* Now create a session, task and stream for this applet */
2391 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2392 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002393 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002394 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002395 }
2396
Willy Tarreau87787ac2017-08-28 16:22:54 +02002397 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002398 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002399 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002400 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002401 }
2402
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002403 /* Initialise cross reference between stream and Lua socket object. */
2404 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002405
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002406 /* Configure "right" stream interface. this "si" is used to connect
2407 * and retrieve data from the server. The connection is initialized
2408 * with the "struct server".
2409 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002410 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002411
2412 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002413 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2414 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002415
Willy Tarreau87787ac2017-08-28 16:22:54 +02002416 task_wakeup(strm->task, TASK_WOKEN_INIT);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002417 /* Return yield waiting for connection. */
2418 return 1;
2419
Willy Tarreaud420a972015-04-06 00:39:18 +02002420 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002421 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002422 out_fail_sess:
2423 appctx_free(appctx);
2424 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002425 WILL_LJMP(lua_error(L));
2426 return 0;
2427}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002428
2429/*
2430 *
2431 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002432 * Class Channel
2433 *
2434 *
2435 */
2436
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002437/* The state between the channel data and the HTTP parser state can be
2438 * unconsistent, so reset the parser and call it again. Warning, this
2439 * action not revalidate the request and not send a 400 if the modified
2440 * resuest is not valid.
2441 *
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002442 * This function never fails. The direction is set using dir, which equals
2443 * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002444 */
2445static void hlua_resynchonize_proto(struct stream *stream, int dir)
2446{
2447 /* Protocol HTTP. */
2448 if (stream->be->mode == PR_MODE_HTTP) {
2449
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002450 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002451 http_txn_reset_req(stream->txn);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002452 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002453 http_txn_reset_res(stream->txn);
2454
2455 if (stream->txn->hdr_idx.v)
2456 hdr_idx_init(&stream->txn->hdr_idx);
2457
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002458 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002459 http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002460 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002461 http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx);
2462 }
2463}
2464
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002465/* This function is called before the Lua execution. It stores
2466 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002467 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002468static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002469{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002470 c->mode = stream->be->mode;
2471 switch (c->mode) {
2472 case PR_MODE_HTTP:
2473 c->data.http.dir = opt & SMP_OPT_DIR;
2474 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2475 c->data.http.state = stream->txn->req.msg_state;
2476 else
2477 c->data.http.state = stream->txn->rsp.msg_state;
2478 break;
2479 default:
2480 break;
2481 }
2482}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002483
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002484/* This function is called after the Lua execution. it
2485 * returns true if the parser state is consistent, otherwise,
2486 * it return false.
2487 *
2488 * In HTTP mode, the parser state must be in the same state
2489 * or greater when we exit the function. Even if we do a
2490 * control yield. This prevent to break the HTTP message
2491 * from the Lua code.
2492 */
2493static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2494{
2495 if (c->mode != stream->be->mode)
2496 return 0;
2497
2498 switch (c->mode) {
2499 case PR_MODE_HTTP:
2500 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002501 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002502 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2503 return stream->txn->req.msg_state >= c->data.http.state;
2504 else
2505 return stream->txn->rsp.msg_state >= c->data.http.state;
2506 default:
2507 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002508 }
2509 return 1;
2510}
2511
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002512/* Returns the struct hlua_channel join to the class channel in the
2513 * stack entry "ud" or throws an argument error.
2514 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002515__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002516{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002517 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002518}
2519
Willy Tarreau47860ed2015-03-10 14:07:50 +01002520/* Pushes the channel onto the top of the stack. If the stask does not have a
2521 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002522 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002523static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002524{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002525 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002526 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002527 return 0;
2528
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002529 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002530 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002531 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002532
2533 /* Pop a class sesison metatable and affect it to the userdata. */
2534 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2535 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002536 return 1;
2537}
2538
2539/* Duplicate all the data present in the input channel and put it
2540 * in a string LUA variables. Returns -1 and push a nil value in
2541 * the stack if the channel is closed and all the data are consumed,
2542 * returns 0 if no data are available, otherwise it returns the length
2543 * of the builded string.
2544 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002545static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002546{
2547 char *blk1;
2548 char *blk2;
2549 int len1;
2550 int len2;
2551 int ret;
2552 luaL_Buffer b;
2553
Willy Tarreau47860ed2015-03-10 14:07:50 +01002554 ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002555 if (unlikely(ret == 0))
2556 return 0;
2557
2558 if (unlikely(ret < 0)) {
2559 lua_pushnil(L);
2560 return -1;
2561 }
2562
2563 luaL_buffinit(L, &b);
2564 luaL_addlstring(&b, blk1, len1);
2565 if (unlikely(ret == 2))
2566 luaL_addlstring(&b, blk2, len2);
2567 luaL_pushresult(&b);
2568
2569 if (unlikely(ret == 2))
2570 return len1 + len2;
2571 return len1;
2572}
2573
2574/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2575 * a yield. This function keep the data in the buffer.
2576 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002577__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002578{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002579 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002580
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002581 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2582
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002583 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002584 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002585 return 1;
2586}
2587
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002588/* Check arguments for the function "hlua_channel_dup_yield". */
2589__LJMP static int hlua_channel_dup(lua_State *L)
2590{
2591 MAY_LJMP(check_args(L, 1, "dup"));
2592 MAY_LJMP(hlua_checkchannel(L, 1));
2593 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2594}
2595
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002596/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2597 * a yield. This function consumes the data in the buffer. It returns
2598 * a string containing the data or a nil pointer if no data are available
2599 * and the channel is closed.
2600 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002601__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002602{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002603 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002604 int ret;
2605
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002606 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002607
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002608 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002609 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002610 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002611
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002612 if (unlikely(ret == -1))
2613 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002614
Willy Tarreau47860ed2015-03-10 14:07:50 +01002615 chn->buf->i -= ret;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002616 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002617 return 1;
2618}
2619
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002620/* Check arguments for the fucntion "hlua_channel_get_yield". */
2621__LJMP static int hlua_channel_get(lua_State *L)
2622{
2623 MAY_LJMP(check_args(L, 1, "get"));
2624 MAY_LJMP(hlua_checkchannel(L, 1));
2625 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2626}
2627
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002628/* This functions consumes and returns one line. If the channel is closed,
2629 * and the last data does not contains a final '\n', the data are returned
2630 * without the final '\n'. When no more data are avalaible, it returns nil
2631 * value.
2632 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002633__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002634{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002635 char *blk1;
2636 char *blk2;
2637 int len1;
2638 int len2;
2639 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002640 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002641 int ret;
2642 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002643
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002644 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2645
Willy Tarreau47860ed2015-03-10 14:07:50 +01002646 ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002647 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002648 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002649
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002650 if (ret == -1) {
2651 lua_pushnil(L);
2652 return 1;
2653 }
2654
2655 luaL_buffinit(L, &b);
2656 luaL_addlstring(&b, blk1, len1);
2657 len = len1;
2658 if (unlikely(ret == 2)) {
2659 luaL_addlstring(&b, blk2, len2);
2660 len += len2;
2661 }
2662 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002663 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002664 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002665 return 1;
2666}
2667
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002668/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2669__LJMP static int hlua_channel_getline(lua_State *L)
2670{
2671 MAY_LJMP(check_args(L, 1, "getline"));
2672 MAY_LJMP(hlua_checkchannel(L, 1));
2673 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2674}
2675
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002676/* This function takes a string as input, and append it at the
2677 * input side of channel. If the data is too big, but a space
2678 * is probably available after sending some data, the function
2679 * yield. If the data is bigger than the buffer, or if the
2680 * channel is closed, it returns -1. otherwise, it returns the
2681 * amount of data writed.
2682 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002683__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002684{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002685 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002686 size_t len;
2687 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2688 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2689 int ret;
2690 int max;
2691
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002692 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2693 * the request buffer if its not required.
2694 */
2695 if (chn->buf->size == 0) {
2696 si_applet_cant_put(chn_prod(chn));
2697 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
2698 }
2699
Willy Tarreau47860ed2015-03-10 14:07:50 +01002700 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002701 if (max > len - l)
2702 max = len - l;
2703
Willy Tarreau47860ed2015-03-10 14:07:50 +01002704 ret = bi_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002705 if (ret == -2 || ret == -3) {
2706 lua_pushinteger(L, -1);
2707 return 1;
2708 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002709 if (ret == -1) {
2710 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002711 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002712 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002713 l += ret;
2714 lua_pop(L, 1);
2715 lua_pushinteger(L, l);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002716 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002717
Willy Tarreau47860ed2015-03-10 14:07:50 +01002718 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2719 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002720 /* There are no space avalaible, and the output buffer is empty.
2721 * in this case, we cannot add more data, so we cannot yield,
2722 * we return the amount of copyied data.
2723 */
2724 return 1;
2725 }
2726 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002727 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002728 return 1;
2729}
2730
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002731/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002732 * of the writed string, or -1 if the channel is closed or if the
2733 * buffer size is too little for the data.
2734 */
2735__LJMP static int hlua_channel_append(lua_State *L)
2736{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002737 size_t len;
2738
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002739 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002740 MAY_LJMP(hlua_checkchannel(L, 1));
2741 MAY_LJMP(luaL_checklstring(L, 2, &len));
2742 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002743 lua_pushinteger(L, 0);
2744
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002745 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002746}
2747
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002748/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002749 * his process by cleaning the buffer. The result is a replacement
2750 * of the current data. It returns the length of the writed string,
2751 * or -1 if the channel is closed or if the buffer size is too
2752 * little for the data.
2753 */
2754__LJMP static int hlua_channel_set(lua_State *L)
2755{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002756 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002757
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002758 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002759 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002760 lua_pushinteger(L, 0);
2761
Willy Tarreau47860ed2015-03-10 14:07:50 +01002762 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002763
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002764 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002765}
2766
2767/* Append data in the output side of the buffer. This data is immediatly
2768 * sent. The fcuntion returns the ammount of data writed. If the buffer
2769 * cannot contains the data, the function yield. The function returns -1
2770 * if the channel is closed.
2771 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002772__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002773{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002774 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002775 size_t len;
2776 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2777 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2778 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002779 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002780
Willy Tarreau47860ed2015-03-10 14:07:50 +01002781 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002782 lua_pushinteger(L, -1);
2783 return 1;
2784 }
2785
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002786 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2787 * the request buffer if its not required.
2788 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002789 if (chn->buf->size == 0) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002790 si_applet_cant_put(chn_prod(chn));
2791 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002792 }
2793
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002794 /* the writed data will be immediatly sent, so we can check
2795 * the avalaible space without taking in account the reserve.
2796 * The reserve is guaranted for the processing of incoming
2797 * data, because the buffer will be flushed.
2798 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002799 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002800
2801 /* If there are no space avalaible, and the output buffer is empty.
2802 * in this case, we cannot add more data, so we cannot yield,
2803 * we return the amount of copyied data.
2804 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002805 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002806 return 1;
2807
2808 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002809 if (max > len - l)
2810 max = len - l;
2811
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002812 /* The buffer avalaible size may be not contiguous. This test
2813 * detects a non contiguous buffer and realign it.
2814 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002815 if (bi_space_for_replace(chn->buf) < max)
2816 buffer_slow_realign(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002817
2818 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002819 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002820
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002821 /* buffer replace considers that the input part is filled.
2822 * so, I must forward these new data in the output part.
2823 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002824 b_adv(chn->buf, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002825
2826 l += max;
2827 lua_pop(L, 1);
2828 lua_pushinteger(L, l);
2829
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002830 /* If there are no space avalaible, and the output buffer is empty.
2831 * in this case, we cannot add more data, so we cannot yield,
2832 * we return the amount of copyied data.
2833 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002834 max = chn->buf->size - buffer_len(chn->buf);
2835 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002836 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002837
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002838 if (l < len) {
2839 /* If we are waiting for space in the response buffer, we
2840 * must set the flag WAKERESWR. This flag required the task
2841 * wake up if any activity is detected on the response buffer.
2842 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002843 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002844 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002845 else
2846 HLUA_SET_WAKEREQWR(hlua);
2847 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002848 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002849
2850 return 1;
2851}
2852
2853/* Just a wraper of "_hlua_channel_send". This wrapper permits
2854 * yield the LUA process, and resume it without checking the
2855 * input arguments.
2856 */
2857__LJMP static int hlua_channel_send(lua_State *L)
2858{
2859 MAY_LJMP(check_args(L, 2, "send"));
2860 lua_pushinteger(L, 0);
2861
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002862 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002863}
2864
2865/* This function forward and amount of butes. The data pass from
2866 * the input side of the buffer to the output side, and can be
2867 * forwarded. This function never fails.
2868 *
2869 * The Lua function takes an amount of bytes to be forwarded in
2870 * imput. It returns the number of bytes forwarded.
2871 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002872__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002873{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002874 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002875 int len;
2876 int l;
2877 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002878 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002879
2880 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2881 len = MAY_LJMP(luaL_checkinteger(L, 2));
2882 l = MAY_LJMP(luaL_checkinteger(L, -1));
2883
2884 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002885 if (max > chn->buf->i)
2886 max = chn->buf->i;
2887 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002888 l += max;
2889
2890 lua_pop(L, 1);
2891 lua_pushinteger(L, l);
2892
2893 /* Check if it miss bytes to forward. */
2894 if (l < len) {
2895 /* The the input channel or the output channel are closed, we
2896 * must return the amount of data forwarded.
2897 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002898 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002899 return 1;
2900
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002901 /* If we are waiting for space data in the response buffer, we
2902 * must set the flag WAKERESWR. This flag required the task
2903 * wake up if any activity is detected on the response buffer.
2904 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002905 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002906 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002907 else
2908 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002909
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002910 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002911 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912 }
2913
2914 return 1;
2915}
2916
2917/* Just check the input and prepare the stack for the previous
2918 * function "hlua_channel_forward_yield"
2919 */
2920__LJMP static int hlua_channel_forward(lua_State *L)
2921{
2922 MAY_LJMP(check_args(L, 2, "forward"));
2923 MAY_LJMP(hlua_checkchannel(L, 1));
2924 MAY_LJMP(luaL_checkinteger(L, 2));
2925
2926 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002927 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002928}
2929
2930/* Just returns the number of bytes available in the input
2931 * side of the buffer. This function never fails.
2932 */
2933__LJMP static int hlua_channel_get_in_len(lua_State *L)
2934{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002935 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002936
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002937 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002938 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002939 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002940 return 1;
2941}
2942
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002943/* Returns true if the channel is full. */
2944__LJMP static int hlua_channel_is_full(lua_State *L)
2945{
2946 struct channel *chn;
2947 int rem;
2948
2949 MAY_LJMP(check_args(L, 1, "is_full"));
2950 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2951
2952 rem = chn->buf->size;
2953 rem -= chn->buf->o; /* Output size */
2954 rem -= chn->buf->i; /* Input size */
2955 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
2956
2957 lua_pushboolean(L, rem <= 0);
2958 return 1;
2959}
2960
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002961/* Just returns the number of bytes available in the output
2962 * side of the buffer. This function never fails.
2963 */
2964__LJMP static int hlua_channel_get_out_len(lua_State *L)
2965{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002966 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002967
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002968 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002969 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002970 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971 return 1;
2972}
2973
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002974/*
2975 *
2976 *
2977 * Class Fetches
2978 *
2979 *
2980 */
2981
2982/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002983 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002984 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002985__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002986{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002987 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002988}
2989
2990/* This function creates and push in the stack a fetch object according
2991 * with a current TXN.
2992 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01002993static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002994{
Willy Tarreau7073c472015-04-06 11:15:40 +02002995 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002996
2997 /* Check stack size. */
2998 if (!lua_checkstack(L, 3))
2999 return 0;
3000
3001 /* Create the object: obj[0] = userdata.
3002 * Note that the base of the Fetches object is the
3003 * transaction object.
3004 */
3005 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003006 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003007 lua_rawseti(L, -2, 0);
3008
Willy Tarreau7073c472015-04-06 11:15:40 +02003009 hsmp->s = txn->s;
3010 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003011 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003012 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003013
3014 /* Pop a class sesison metatable and affect it to the userdata. */
3015 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3016 lua_setmetatable(L, -2);
3017
3018 return 1;
3019}
3020
3021/* This function is an LUA binding. It is called with each sample-fetch.
3022 * It uses closure argument to store the associated sample-fetch. It
3023 * returns only one argument or throws an error. An error is thrown
3024 * only if an error is encountered during the argument parsing. If
3025 * the "sample-fetch" function fails, nil is returned.
3026 */
3027__LJMP static int hlua_run_sample_fetch(lua_State *L)
3028{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003029 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003030 struct sample_fetch *f;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003031 struct arg args[ARGM_NBARGS + 1];
3032 int i;
3033 struct sample smp;
3034
3035 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003036 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003037
3038 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003039 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003040
Thierry FOURNIERca988662015-12-20 18:43:03 +01003041 /* Check execution authorization. */
3042 if (f->use & SMP_USE_HTTP_ANY &&
3043 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3044 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3045 "is not available in Lua services", f->kw);
3046 WILL_LJMP(lua_error(L));
3047 }
3048
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003049 /* Get extra arguments. */
3050 for (i = 0; i < lua_gettop(L) - 1; i++) {
3051 if (i >= ARGM_NBARGS)
3052 break;
3053 hlua_lua2arg(L, i + 2, &args[i]);
3054 }
3055 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003056 args[i].data.str.str = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003057
3058 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003059 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003060
3061 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003062 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003063 lua_pushfstring(L, "error in arguments");
3064 WILL_LJMP(lua_error(L));
3065 }
3066
3067 /* Initialise the sample. */
3068 memset(&smp, 0, sizeof(smp));
3069
3070 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003071 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003072 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003073 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003074 lua_pushstring(L, "");
3075 else
3076 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003077 return 1;
3078 }
3079
3080 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003081 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003082 hlua_smp2lua_str(L, &smp);
3083 else
3084 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003085 return 1;
3086}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003087
3088/*
3089 *
3090 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003091 * Class Converters
3092 *
3093 *
3094 */
3095
3096/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003097 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003098 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003099__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003100{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003101 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003102}
3103
3104/* This function creates and push in the stack a Converters object
3105 * according with a current TXN.
3106 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003107static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003108{
Willy Tarreau7073c472015-04-06 11:15:40 +02003109 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003110
3111 /* Check stack size. */
3112 if (!lua_checkstack(L, 3))
3113 return 0;
3114
3115 /* Create the object: obj[0] = userdata.
3116 * Note that the base of the Converters object is the
3117 * same than the TXN object.
3118 */
3119 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003120 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003121 lua_rawseti(L, -2, 0);
3122
Willy Tarreau7073c472015-04-06 11:15:40 +02003123 hsmp->s = txn->s;
3124 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003125 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003126 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003127
Willy Tarreau87b09662015-04-03 00:22:06 +02003128 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003129 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3130 lua_setmetatable(L, -2);
3131
3132 return 1;
3133}
3134
3135/* This function is an LUA binding. It is called with each converter.
3136 * It uses closure argument to store the associated converter. It
3137 * returns only one argument or throws an error. An error is thrown
3138 * only if an error is encountered during the argument parsing. If
3139 * the converter function function fails, nil is returned.
3140 */
3141__LJMP static int hlua_run_sample_conv(lua_State *L)
3142{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003143 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003144 struct sample_conv *conv;
3145 struct arg args[ARGM_NBARGS + 1];
3146 int i;
3147 struct sample smp;
3148
3149 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003150 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003151
3152 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003153 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003154
3155 /* Get extra arguments. */
3156 for (i = 0; i < lua_gettop(L) - 2; i++) {
3157 if (i >= ARGM_NBARGS)
3158 break;
3159 hlua_lua2arg(L, i + 3, &args[i]);
3160 }
3161 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003162 args[i].data.str.str = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003163
3164 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003165 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003166
3167 /* Run the special args checker. */
3168 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3169 hlua_pusherror(L, "error in arguments");
3170 WILL_LJMP(lua_error(L));
3171 }
3172
3173 /* Initialise the sample. */
3174 if (!hlua_lua2smp(L, 2, &smp)) {
3175 hlua_pusherror(L, "error in the input argument");
3176 WILL_LJMP(lua_error(L));
3177 }
3178
Willy Tarreau1777ea62016-03-10 16:15:46 +01003179 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3180
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003181 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003182 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003183 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003184 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003185 WILL_LJMP(lua_error(L));
3186 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003187 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3188 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003189 hlua_pusherror(L, "error during the input argument casting");
3190 WILL_LJMP(lua_error(L));
3191 }
3192
3193 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003194 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003195 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003196 lua_pushstring(L, "");
3197 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003198 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003199 return 1;
3200 }
3201
3202 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003203 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003204 hlua_smp2lua_str(L, &smp);
3205 else
3206 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003207 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003208}
3209
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003210/*
3211 *
3212 *
3213 * Class AppletTCP
3214 *
3215 *
3216 */
3217
3218/* Returns a struct hlua_txn if the stack entry "ud" is
3219 * a class stream, otherwise it throws an error.
3220 */
3221__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3222{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003223 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003224}
3225
3226/* This function creates and push in the stack an Applet object
3227 * according with a current TXN.
3228 */
3229static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3230{
3231 struct hlua_appctx *appctx;
3232 struct stream_interface *si = ctx->owner;
3233 struct stream *s = si_strm(si);
3234 struct proxy *p = s->be;
3235
3236 /* Check stack size. */
3237 if (!lua_checkstack(L, 3))
3238 return 0;
3239
3240 /* Create the object: obj[0] = userdata.
3241 * Note that the base of the Converters object is the
3242 * same than the TXN object.
3243 */
3244 lua_newtable(L);
3245 appctx = lua_newuserdata(L, sizeof(*appctx));
3246 lua_rawseti(L, -2, 0);
3247 appctx->appctx = ctx;
3248 appctx->htxn.s = s;
3249 appctx->htxn.p = p;
3250
3251 /* Create the "f" field that contains a list of fetches. */
3252 lua_pushstring(L, "f");
3253 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3254 return 0;
3255 lua_settable(L, -3);
3256
3257 /* Create the "sf" field that contains a list of stringsafe fetches. */
3258 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003259 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003260 return 0;
3261 lua_settable(L, -3);
3262
3263 /* Create the "c" field that contains a list of converters. */
3264 lua_pushstring(L, "c");
3265 if (!hlua_converters_new(L, &appctx->htxn, 0))
3266 return 0;
3267 lua_settable(L, -3);
3268
3269 /* Create the "sc" field that contains a list of stringsafe converters. */
3270 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003271 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003272 return 0;
3273 lua_settable(L, -3);
3274
3275 /* Pop a class stream metatable and affect it to the table. */
3276 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3277 lua_setmetatable(L, -2);
3278
3279 return 1;
3280}
3281
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003282__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3283{
3284 struct hlua_appctx *appctx;
3285 struct stream *s;
3286 const char *name;
3287 size_t len;
3288 struct sample smp;
3289
3290 MAY_LJMP(check_args(L, 3, "set_var"));
3291
3292 /* It is useles to retrieve the stream, but this function
3293 * runs only in a stream context.
3294 */
3295 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3296 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3297 s = appctx->htxn.s;
3298
3299 /* Converts the third argument in a sample. */
3300 hlua_lua2smp(L, 3, &smp);
3301
3302 /* Store the sample in a variable. */
3303 smp_set_owner(&smp, s->be, s->sess, s, 0);
3304 vars_set_by_name(name, len, &smp);
3305 return 0;
3306}
3307
3308__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3309{
3310 struct hlua_appctx *appctx;
3311 struct stream *s;
3312 const char *name;
3313 size_t len;
3314 struct sample smp;
3315
3316 MAY_LJMP(check_args(L, 2, "unset_var"));
3317
3318 /* It is useles to retrieve the stream, but this function
3319 * runs only in a stream context.
3320 */
3321 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3322 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3323 s = appctx->htxn.s;
3324
3325 /* Unset the variable. */
3326 smp_set_owner(&smp, s->be, s->sess, s, 0);
3327 vars_unset_by_name(name, len, &smp);
3328 return 0;
3329}
3330
3331__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3332{
3333 struct hlua_appctx *appctx;
3334 struct stream *s;
3335 const char *name;
3336 size_t len;
3337 struct sample smp;
3338
3339 MAY_LJMP(check_args(L, 2, "get_var"));
3340
3341 /* It is useles to retrieve the stream, but this function
3342 * runs only in a stream context.
3343 */
3344 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3345 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3346 s = appctx->htxn.s;
3347
3348 smp_set_owner(&smp, s->be, s->sess, s, 0);
3349 if (!vars_get_by_name(name, len, &smp)) {
3350 lua_pushnil(L);
3351 return 1;
3352 }
3353
3354 return hlua_smp2lua(L, &smp);
3355}
3356
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003357__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3358{
3359 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3360 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003361 struct hlua *hlua;
3362
3363 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003364 if (!s->hlua)
3365 return 0;
3366 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003367
3368 MAY_LJMP(check_args(L, 2, "set_priv"));
3369
3370 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003371 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003372
3373 /* Get and store new value. */
3374 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3375 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3376
3377 return 0;
3378}
3379
3380__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3381{
3382 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3383 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003384 struct hlua *hlua;
3385
3386 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003387 if (!s->hlua) {
3388 lua_pushnil(L);
3389 return 1;
3390 }
3391 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003392
3393 /* Push configuration index in the stack. */
3394 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3395
3396 return 1;
3397}
3398
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003399/* If expected data not yet available, it returns a yield. This function
3400 * consumes the data in the buffer. It returns a string containing the
3401 * data. This string can be empty.
3402 */
3403__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3404{
3405 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3406 struct stream_interface *si = appctx->appctx->owner;
3407 int ret;
3408 char *blk1;
3409 int len1;
3410 char *blk2;
3411 int len2;
3412
3413 /* Read the maximum amount of data avalaible. */
3414 ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
3415
3416 /* Data not yet avalaible. return yield. */
3417 if (ret == 0) {
3418 si_applet_cant_get(si);
3419 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
3420 }
3421
3422 /* End of data: commit the total strings and return. */
3423 if (ret < 0) {
3424 luaL_pushresult(&appctx->b);
3425 return 1;
3426 }
3427
3428 /* Ensure that the block 2 length is usable. */
3429 if (ret == 1)
3430 len2 = 0;
3431
3432 /* dont check the max length read and dont check. */
3433 luaL_addlstring(&appctx->b, blk1, len1);
3434 luaL_addlstring(&appctx->b, blk2, len2);
3435
3436 /* Consume input channel output buffer data. */
3437 bo_skip(si_oc(si), len1 + len2);
3438 luaL_pushresult(&appctx->b);
3439 return 1;
3440}
3441
3442/* Check arguments for the fucntion "hlua_channel_get_yield". */
3443__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3444{
3445 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3446
3447 /* Initialise the string catenation. */
3448 luaL_buffinit(L, &appctx->b);
3449
3450 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3451}
3452
3453/* If expected data not yet available, it returns a yield. This function
3454 * consumes the data in the buffer. It returns a string containing the
3455 * data. This string can be empty.
3456 */
3457__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3458{
3459 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3460 struct stream_interface *si = appctx->appctx->owner;
3461 int len = MAY_LJMP(luaL_checkinteger(L, 2));
3462 int ret;
3463 char *blk1;
3464 int len1;
3465 char *blk2;
3466 int len2;
3467
3468 /* Read the maximum amount of data avalaible. */
3469 ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
3470
3471 /* Data not yet avalaible. return yield. */
3472 if (ret == 0) {
3473 si_applet_cant_get(si);
3474 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3475 }
3476
3477 /* End of data: commit the total strings and return. */
3478 if (ret < 0) {
3479 luaL_pushresult(&appctx->b);
3480 return 1;
3481 }
3482
3483 /* Ensure that the block 2 length is usable. */
3484 if (ret == 1)
3485 len2 = 0;
3486
3487 if (len == -1) {
3488
3489 /* If len == -1, catenate all the data avalaile and
3490 * yield because we want to get all the data until
3491 * the end of data stream.
3492 */
3493 luaL_addlstring(&appctx->b, blk1, len1);
3494 luaL_addlstring(&appctx->b, blk2, len2);
3495 bo_skip(si_oc(si), len1 + len2);
3496 si_applet_cant_get(si);
3497 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3498
3499 } else {
3500
3501 /* Copy the fisrt block caping to the length required. */
3502 if (len1 > len)
3503 len1 = len;
3504 luaL_addlstring(&appctx->b, blk1, len1);
3505 len -= len1;
3506
3507 /* Copy the second block. */
3508 if (len2 > len)
3509 len2 = len;
3510 luaL_addlstring(&appctx->b, blk2, len2);
3511 len -= len2;
3512
3513 /* Consume input channel output buffer data. */
3514 bo_skip(si_oc(si), len1 + len2);
3515
3516 /* If we are no other data avalaible, yield waiting for new data. */
3517 if (len > 0) {
3518 lua_pushinteger(L, len);
3519 lua_replace(L, 2);
3520 si_applet_cant_get(si);
3521 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3522 }
3523
3524 /* return the result. */
3525 luaL_pushresult(&appctx->b);
3526 return 1;
3527 }
3528
3529 /* we never executes this */
3530 hlua_pusherror(L, "Lua: internal error");
3531 WILL_LJMP(lua_error(L));
3532 return 0;
3533}
3534
3535/* Check arguments for the fucntion "hlua_channel_get_yield". */
3536__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3537{
3538 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3539 int len = -1;
3540
3541 if (lua_gettop(L) > 2)
3542 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3543 if (lua_gettop(L) >= 2) {
3544 len = MAY_LJMP(luaL_checkinteger(L, 2));
3545 lua_pop(L, 1);
3546 }
3547
3548 /* Confirm or set the required length */
3549 lua_pushinteger(L, len);
3550
3551 /* Initialise the string catenation. */
3552 luaL_buffinit(L, &appctx->b);
3553
3554 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3555}
3556
3557/* Append data in the output side of the buffer. This data is immediatly
3558 * sent. The fcuntion returns the ammount of data writed. If the buffer
3559 * cannot contains the data, the function yield. The function returns -1
3560 * if the channel is closed.
3561 */
3562__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3563{
3564 size_t len;
3565 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3566 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3567 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3568 struct stream_interface *si = appctx->appctx->owner;
3569 struct channel *chn = si_ic(si);
3570 int max;
3571
3572 /* Get the max amount of data which can write as input in the channel. */
3573 max = channel_recv_max(chn);
3574 if (max > (len - l))
3575 max = len - l;
3576
3577 /* Copy data. */
3578 bi_putblk(chn, str + l, max);
3579
3580 /* update counters. */
3581 l += max;
3582 lua_pop(L, 1);
3583 lua_pushinteger(L, l);
3584
3585 /* If some data is not send, declares the situation to the
3586 * applet, and returns a yield.
3587 */
3588 if (l < len) {
3589 si_applet_cant_put(si);
3590 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
3591 }
3592
3593 return 1;
3594}
3595
3596/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3597 * yield the LUA process, and resume it without checking the
3598 * input arguments.
3599 */
3600__LJMP static int hlua_applet_tcp_send(lua_State *L)
3601{
3602 MAY_LJMP(check_args(L, 2, "send"));
3603 lua_pushinteger(L, 0);
3604
3605 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3606}
3607
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003608/*
3609 *
3610 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003611 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003612 *
3613 *
3614 */
3615
3616/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003617 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003618 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003619__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003620{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003621 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003622}
3623
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003624/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003625 * according with a current TXN.
3626 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003627static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003628{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003629 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003630 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003631 struct stream_interface *si = ctx->owner;
3632 struct stream *s = si_strm(si);
3633 struct proxy *px = s->be;
3634 struct http_txn *txn = s->txn;
3635 const char *path;
3636 const char *end;
3637 const char *p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003638
3639 /* Check stack size. */
3640 if (!lua_checkstack(L, 3))
3641 return 0;
3642
3643 /* Create the object: obj[0] = userdata.
3644 * Note that the base of the Converters object is the
3645 * same than the TXN object.
3646 */
3647 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003648 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003649 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003650 appctx->appctx = ctx;
3651 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003652 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003653 appctx->htxn.s = s;
3654 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003655
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003656 /* Create the "f" field that contains a list of fetches. */
3657 lua_pushstring(L, "f");
3658 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3659 return 0;
3660 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003661
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003662 /* Create the "sf" field that contains a list of stringsafe fetches. */
3663 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003664 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003665 return 0;
3666 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003667
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003668 /* Create the "c" field that contains a list of converters. */
3669 lua_pushstring(L, "c");
3670 if (!hlua_converters_new(L, &appctx->htxn, 0))
3671 return 0;
3672 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003673
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003674 /* Create the "sc" field that contains a list of stringsafe converters. */
3675 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003676 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003677 return 0;
3678 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003679
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003680 /* Stores the request method. */
3681 lua_pushstring(L, "method");
3682 lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l);
3683 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003684
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003685 /* Stores the http version. */
3686 lua_pushstring(L, "version");
3687 lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
3688 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003689
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003690 /* creates an array of headers. hlua_http_get_headers() crates and push
3691 * the array on the top of the stack.
3692 */
3693 lua_pushstring(L, "headers");
3694 htxn.s = s;
3695 htxn.p = px;
3696 htxn.dir = SMP_OPT_DIR_REQ;
3697 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3698 return 0;
3699 lua_settable(L, -3);
3700
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003701 /* Get path and qs */
3702 path = http_get_path(txn);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003703 if (path) {
3704 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
3705 p = path;
3706 while (p < end && *p != '?')
3707 p++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003708
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003709 /* Stores the request path. */
3710 lua_pushstring(L, "path");
3711 lua_pushlstring(L, path, p - path);
3712 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003713
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003714 /* Stores the query string. */
3715 lua_pushstring(L, "qs");
3716 if (*p == '?')
3717 p++;
3718 lua_pushlstring(L, p, end - p);
3719 lua_settable(L, -3);
3720 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003721
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003722 /* Stores the request path. */
3723 lua_pushstring(L, "length");
3724 lua_pushinteger(L, txn->req.body_len);
3725 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003726
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003727 /* Create an array of HTTP request headers. */
3728 lua_pushstring(L, "headers");
3729 MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req));
3730 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003731
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003732 /* Create an empty array of HTTP request headers. */
3733 lua_pushstring(L, "response");
3734 lua_newtable(L);
3735 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003736
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003737 /* Pop a class stream metatable and affect it to the table. */
3738 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3739 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003740
3741 return 1;
3742}
3743
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003744__LJMP static int hlua_applet_http_set_var(lua_State *L)
3745{
3746 struct hlua_appctx *appctx;
3747 struct stream *s;
3748 const char *name;
3749 size_t len;
3750 struct sample smp;
3751
3752 MAY_LJMP(check_args(L, 3, "set_var"));
3753
3754 /* It is useles to retrieve the stream, but this function
3755 * runs only in a stream context.
3756 */
3757 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3758 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3759 s = appctx->htxn.s;
3760
3761 /* Converts the third argument in a sample. */
3762 hlua_lua2smp(L, 3, &smp);
3763
3764 /* Store the sample in a variable. */
3765 smp_set_owner(&smp, s->be, s->sess, s, 0);
3766 vars_set_by_name(name, len, &smp);
3767 return 0;
3768}
3769
3770__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3771{
3772 struct hlua_appctx *appctx;
3773 struct stream *s;
3774 const char *name;
3775 size_t len;
3776 struct sample smp;
3777
3778 MAY_LJMP(check_args(L, 2, "unset_var"));
3779
3780 /* It is useles to retrieve the stream, but this function
3781 * runs only in a stream context.
3782 */
3783 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3784 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3785 s = appctx->htxn.s;
3786
3787 /* Unset the variable. */
3788 smp_set_owner(&smp, s->be, s->sess, s, 0);
3789 vars_unset_by_name(name, len, &smp);
3790 return 0;
3791}
3792
3793__LJMP static int hlua_applet_http_get_var(lua_State *L)
3794{
3795 struct hlua_appctx *appctx;
3796 struct stream *s;
3797 const char *name;
3798 size_t len;
3799 struct sample smp;
3800
3801 MAY_LJMP(check_args(L, 2, "get_var"));
3802
3803 /* It is useles to retrieve the stream, but this function
3804 * runs only in a stream context.
3805 */
3806 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3807 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3808 s = appctx->htxn.s;
3809
3810 smp_set_owner(&smp, s->be, s->sess, s, 0);
3811 if (!vars_get_by_name(name, len, &smp)) {
3812 lua_pushnil(L);
3813 return 1;
3814 }
3815
3816 return hlua_smp2lua(L, &smp);
3817}
3818
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003819__LJMP static int hlua_applet_http_set_priv(lua_State *L)
3820{
3821 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3822 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003823 struct hlua *hlua;
3824
3825 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003826 if (!s->hlua)
3827 return 0;
3828 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003829
3830 MAY_LJMP(check_args(L, 2, "set_priv"));
3831
3832 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003833 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003834
3835 /* Get and store new value. */
3836 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3837 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3838
3839 return 0;
3840}
3841
3842__LJMP static int hlua_applet_http_get_priv(lua_State *L)
3843{
3844 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3845 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003846 struct hlua *hlua;
3847
3848 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003849 if (!s->hlua) {
3850 lua_pushnil(L);
3851 return 1;
3852 }
3853 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003854
3855 /* Push configuration index in the stack. */
3856 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3857
3858 return 1;
3859}
3860
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003861/* If expected data not yet available, it returns a yield. This function
3862 * consumes the data in the buffer. It returns a string containing the
3863 * data. This string can be empty.
3864 */
3865__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003866{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003867 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3868 struct stream_interface *si = appctx->appctx->owner;
3869 struct channel *chn = si_ic(si);
3870 int ret;
3871 char *blk1;
3872 int len1;
3873 char *blk2;
3874 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003875
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003876 /* Maybe we cant send a 100-continue ? */
3877 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
3878 ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C));
3879 /* if ret == -2 or -3 the channel closed or the message si too
3880 * big for the buffers. We cant send anything. So, we ignoring
3881 * the error, considers that the 100-continue is sent, and try
3882 * to receive.
3883 * If ret is -1, we dont have room in the buffer, so we yield.
3884 */
3885 if (ret == -1) {
3886 si_applet_cant_put(si);
3887 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
3888 }
3889 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
3890 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003891
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003892 /* Check for the end of the data. */
3893 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
3894 luaL_pushresult(&appctx->b);
3895 return 1;
3896 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003897
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003898 /* Read the maximum amount of data avalaible. */
3899 ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003900
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003901 /* Data not yet avalaible. return yield. */
3902 if (ret == 0) {
3903 si_applet_cant_get(si);
3904 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
3905 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003906
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003907 /* End of data: commit the total strings and return. */
3908 if (ret < 0) {
3909 luaL_pushresult(&appctx->b);
3910 return 1;
3911 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003912
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003913 /* Ensure that the block 2 length is usable. */
3914 if (ret == 1)
3915 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003916
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003917 /* Copy the fisrt block caping to the length required. */
3918 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
3919 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
3920 luaL_addlstring(&appctx->b, blk1, len1);
3921 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003922
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003923 /* Copy the second block. */
3924 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
3925 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
3926 luaL_addlstring(&appctx->b, blk2, len2);
3927 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003928
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003929 /* Consume input channel output buffer data. */
3930 bo_skip(si_oc(si), len1 + len2);
3931 luaL_pushresult(&appctx->b);
3932 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003933}
3934
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003935/* Check arguments for the fucntion "hlua_channel_get_yield". */
3936__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003937{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003938 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003939
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003940 /* Initialise the string catenation. */
3941 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003942
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003943 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003944}
3945
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003946/* If expected data not yet available, it returns a yield. This function
3947 * consumes the data in the buffer. It returns a string containing the
3948 * data. This string can be empty.
3949 */
3950__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003951{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003952 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3953 struct stream_interface *si = appctx->appctx->owner;
3954 int len = MAY_LJMP(luaL_checkinteger(L, 2));
3955 struct channel *chn = si_ic(si);
3956 int ret;
3957 char *blk1;
3958 int len1;
3959 char *blk2;
3960 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003961
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003962 /* Maybe we cant send a 100-continue ? */
3963 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
3964 ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C));
3965 /* if ret == -2 or -3 the channel closed or the message si too
3966 * big for the buffers. We cant send anything. So, we ignoring
3967 * the error, considers that the 100-continue is sent, and try
3968 * to receive.
3969 * If ret is -1, we dont have room in the buffer, so we yield.
3970 */
3971 if (ret == -1) {
3972 si_applet_cant_put(si);
3973 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
3974 }
3975 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
3976 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003977
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003978 /* Read the maximum amount of data avalaible. */
3979 ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003980
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003981 /* Data not yet avalaible. return yield. */
3982 if (ret == 0) {
3983 si_applet_cant_get(si);
3984 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
3985 }
3986
3987 /* End of data: commit the total strings and return. */
3988 if (ret < 0) {
3989 luaL_pushresult(&appctx->b);
3990 return 1;
3991 }
3992
3993 /* Ensure that the block 2 length is usable. */
3994 if (ret == 1)
3995 len2 = 0;
3996
3997 /* Copy the fisrt block caping to the length required. */
3998 if (len1 > len)
3999 len1 = len;
4000 luaL_addlstring(&appctx->b, blk1, len1);
4001 len -= len1;
4002
4003 /* Copy the second block. */
4004 if (len2 > len)
4005 len2 = len;
4006 luaL_addlstring(&appctx->b, blk2, len2);
4007 len -= len2;
4008
4009 /* Consume input channel output buffer data. */
4010 bo_skip(si_oc(si), len1 + len2);
4011 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4012 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4013
4014 /* If we are no other data avalaible, yield waiting for new data. */
4015 if (len > 0) {
4016 lua_pushinteger(L, len);
4017 lua_replace(L, 2);
4018 si_applet_cant_get(si);
4019 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4020 }
4021
4022 /* return the result. */
4023 luaL_pushresult(&appctx->b);
4024 return 1;
4025}
4026
4027/* Check arguments for the fucntion "hlua_channel_get_yield". */
4028__LJMP static int hlua_applet_http_recv(lua_State *L)
4029{
4030 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4031 int len = -1;
4032
4033 /* Check arguments. */
4034 if (lua_gettop(L) > 2)
4035 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4036 if (lua_gettop(L) >= 2) {
4037 len = MAY_LJMP(luaL_checkinteger(L, 2));
4038 lua_pop(L, 1);
4039 }
4040
4041 /* Check the required length */
4042 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4043 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4044 lua_pushinteger(L, len);
4045
4046 /* Initialise the string catenation. */
4047 luaL_buffinit(L, &appctx->b);
4048
4049 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4050}
4051
4052/* Append data in the output side of the buffer. This data is immediatly
4053 * sent. The fcuntion returns the ammount of data writed. If the buffer
4054 * cannot contains the data, the function yield. The function returns -1
4055 * if the channel is closed.
4056 */
4057__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4058{
4059 size_t len;
4060 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4061 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4062 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4063 struct stream_interface *si = appctx->appctx->owner;
4064 struct channel *chn = si_ic(si);
4065 int max;
4066
4067 /* Get the max amount of data which can write as input in the channel. */
4068 max = channel_recv_max(chn);
4069 if (max > (len - l))
4070 max = len - l;
4071
4072 /* Copy data. */
4073 bi_putblk(chn, str + l, max);
4074
4075 /* update counters. */
4076 l += max;
4077 lua_pop(L, 1);
4078 lua_pushinteger(L, l);
4079
4080 /* If some data is not send, declares the situation to the
4081 * applet, and returns a yield.
4082 */
4083 if (l < len) {
4084 si_applet_cant_put(si);
4085 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
4086 }
4087
4088 return 1;
4089}
4090
4091/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4092 * yield the LUA process, and resume it without checking the
4093 * input arguments.
4094 */
4095__LJMP static int hlua_applet_http_send(lua_State *L)
4096{
4097 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4098 size_t len;
4099 char hex[10];
4100
4101 MAY_LJMP(luaL_checklstring(L, 2, &len));
4102
4103 /* If transfer encoding chunked is selected, we surround the data
4104 * by chunk data.
4105 */
4106 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4107 snprintf(hex, 9, "%x", (unsigned int)len);
4108 lua_pushfstring(L, "%s\r\n", hex);
4109 lua_insert(L, 2); /* swap the last 2 entries. */
4110 lua_pushstring(L, "\r\n");
4111 lua_concat(L, 3);
4112 }
4113
4114 /* This interger is used for followinf the amount of data sent. */
4115 lua_pushinteger(L, 0);
4116
4117 /* We want to send some data. Headers must be sent. */
4118 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4119 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4120 WILL_LJMP(lua_error(L));
4121 }
4122
4123 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4124}
4125
4126__LJMP static int hlua_applet_http_addheader(lua_State *L)
4127{
4128 const char *name;
4129 int ret;
4130
4131 MAY_LJMP(hlua_checkapplet_http(L, 1));
4132 name = MAY_LJMP(luaL_checkstring(L, 2));
4133 MAY_LJMP(luaL_checkstring(L, 3));
4134
4135 /* Push in the stack the "response" entry. */
4136 ret = lua_getfield(L, 1, "response");
4137 if (ret != LUA_TTABLE) {
4138 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4139 "is expected as an array. %s found", lua_typename(L, ret));
4140 WILL_LJMP(lua_error(L));
4141 }
4142
4143 /* check if the header is already registered if it is not
4144 * the case, register it.
4145 */
4146 ret = lua_getfield(L, -1, name);
4147 if (ret == LUA_TNIL) {
4148
4149 /* Entry not found. */
4150 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4151
4152 /* Insert the new header name in the array in the top of the stack.
4153 * It left the new array in the top of the stack.
4154 */
4155 lua_newtable(L);
4156 lua_pushvalue(L, 2);
4157 lua_pushvalue(L, -2);
4158 lua_settable(L, -4);
4159
4160 } else if (ret != LUA_TTABLE) {
4161
4162 /* corruption error. */
4163 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4164 "is expected as an array. %s found", name, lua_typename(L, ret));
4165 WILL_LJMP(lua_error(L));
4166 }
4167
4168 /* Now the top od thestack is an array of values. We push
4169 * the header value as new entry.
4170 */
4171 lua_pushvalue(L, 3);
4172 ret = lua_rawlen(L, -2);
4173 lua_rawseti(L, -2, ret + 1);
4174 lua_pushboolean(L, 1);
4175 return 1;
4176}
4177
4178__LJMP static int hlua_applet_http_status(lua_State *L)
4179{
4180 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4181 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004182 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004183
4184 if (status < 100 || status > 599) {
4185 lua_pushboolean(L, 0);
4186 return 1;
4187 }
4188
4189 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004190 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004191 lua_pushboolean(L, 1);
4192 return 1;
4193}
4194
4195/* We will build the status line and the headers of the HTTP response.
4196 * We will try send at once if its not possible, we give back the hand
4197 * waiting for more room.
4198 */
4199__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4200{
4201 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4202 struct stream_interface *si = appctx->appctx->owner;
4203 struct channel *chn = si_ic(si);
4204 int ret;
4205 size_t len;
4206 const char *msg;
4207
4208 /* Get the message as the first argument on the stack. */
4209 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4210
4211 /* Send the message at once. */
4212 ret = bi_putblk(chn, msg, len);
4213
4214 /* if ret == -2 or -3 the channel closed or the message si too
4215 * big for the buffers.
4216 */
4217 if (ret == -2 || ret == -3) {
4218 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4219 WILL_LJMP(lua_error(L));
4220 }
4221
4222 /* If ret is -1, we dont have room in the buffer, so we yield. */
4223 if (ret == -1) {
4224 si_applet_cant_put(si);
4225 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
4226 }
4227
4228 /* Headers sent, set the flag. */
4229 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4230 return 0;
4231}
4232
4233__LJMP static int hlua_applet_http_start_response(lua_State *L)
4234{
4235 struct chunk *tmp = get_trash_chunk();
4236 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004237 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02004238 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004239 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02004240 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004241 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004242 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004243 int hdr_chunked = 0;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004244 const char *reason = appctx->appctx->ctx.hlua_apphttp.reason;
4245
4246 if (reason == NULL)
4247 reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004248
4249 /* Use the same http version than the request. */
4250 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004251 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004252 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004253 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004254
4255 /* Get the array associated to the field "response" in the object AppletHTTP. */
4256 lua_pushvalue(L, 0);
4257 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4258 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4259 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4260 WILL_LJMP(lua_error(L));
4261 }
4262
4263 /* Browse the list of headers. */
4264 lua_pushnil(L);
4265 while(lua_next(L, -2) != 0) {
4266
4267 /* We expect a string as -2. */
4268 if (lua_type(L, -2) != LUA_TSTRING) {
4269 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4270 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4271 lua_typename(L, lua_type(L, -2)));
4272 WILL_LJMP(lua_error(L));
4273 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004274 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004275
4276 /* We expect an array as -1. */
4277 if (lua_type(L, -1) != LUA_TTABLE) {
4278 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4279 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4280 name,
4281 lua_typename(L, lua_type(L, -1)));
4282 WILL_LJMP(lua_error(L));
4283 }
4284
4285 /* Browse the table who is on the top of the stack. */
4286 lua_pushnil(L);
4287 while(lua_next(L, -2) != 0) {
4288
4289 /* We expect a number as -2. */
4290 if (lua_type(L, -2) != LUA_TNUMBER) {
4291 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4292 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4293 name,
4294 lua_typename(L, lua_type(L, -2)));
4295 WILL_LJMP(lua_error(L));
4296 }
4297 id = lua_tointeger(L, -2);
4298
4299 /* We expect a string as -2. */
4300 if (lua_type(L, -1) != LUA_TSTRING) {
4301 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4302 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4303 name, id,
4304 lua_typename(L, lua_type(L, -1)));
4305 WILL_LJMP(lua_error(L));
4306 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004307 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004308
4309 /* Catenate a new header. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004310 if (tmp->len + name_len + 2 + value_len + 2 < tmp->size) {
4311 memcpy(tmp->str + tmp->len, name, name_len);
4312 tmp->len += name_len;
4313 tmp->str[tmp->len++] = ':';
4314 tmp->str[tmp->len++] = ' ';
4315
4316 memcpy(tmp->str + tmp->len, value, value_len);
4317 tmp->len += value_len;
4318 tmp->str[tmp->len++] = '\r';
4319 tmp->str[tmp->len++] = '\n';
4320 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004321
4322 /* Protocol checks. */
4323
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004324 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004325 * is done without control. If it contains a bad value,
4326 * the content-length remains negative so that we can
4327 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004328 */
Willy Tarreaua3294632017-08-23 11:24:47 +02004329 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004330 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004331
4332 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004333 if (name_len == 17 && value_len == 7 &&
4334 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004335 strcasecmp("chunked", value) == 0)
4336 hdr_chunked = 1;
4337
4338 /* Remove the array from the stack, and get next element with a remaining string. */
4339 lua_pop(L, 1);
4340 }
4341
4342 /* Remove the array from the stack, and get next element with a remaining string. */
4343 lua_pop(L, 1);
4344 }
4345
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004346 /* If we dont have a content-length set, and the HTTP version is 1.1
4347 * and the status code implies the presence of a message body, we must
4348 * announce a transfer encoding chunked. This is required by haproxy
4349 * for the keepalive compliance. If the applet annouces a transfer-encoding
4350 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004351 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004352 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004353 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
4354 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4355 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4356 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004357 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
4358 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
4359 }
4360
4361 /* Finalize headers. */
4362 chunk_appendf(tmp, "\r\n");
4363
4364 /* Remove the last entry and the array of headers */
4365 lua_pop(L, 2);
4366
4367 /* Push the headers block. */
4368 lua_pushlstring(L, tmp->str, tmp->len);
4369
4370 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
4371}
4372
4373/*
4374 *
4375 *
4376 * Class HTTP
4377 *
4378 *
4379 */
4380
4381/* Returns a struct hlua_txn if the stack entry "ud" is
4382 * a class stream, otherwise it throws an error.
4383 */
4384__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
4385{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004386 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004387}
4388
4389/* This function creates and push in the stack a HTTP object
4390 * according with a current TXN.
4391 */
4392static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4393{
4394 struct hlua_txn *htxn;
4395
4396 /* Check stack size. */
4397 if (!lua_checkstack(L, 3))
4398 return 0;
4399
4400 /* Create the object: obj[0] = userdata.
4401 * Note that the base of the Converters object is the
4402 * same than the TXN object.
4403 */
4404 lua_newtable(L);
4405 htxn = lua_newuserdata(L, sizeof(*htxn));
4406 lua_rawseti(L, -2, 0);
4407
4408 htxn->s = txn->s;
4409 htxn->p = txn->p;
4410
4411 /* Pop a class stream metatable and affect it to the table. */
4412 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4413 lua_setmetatable(L, -2);
4414
4415 return 1;
4416}
4417
4418/* This function creates ans returns an array of HTTP headers.
4419 * This function does not fails. It is used as wrapper with the
4420 * 2 following functions.
4421 */
4422__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4423{
4424 const char *cur_ptr, *cur_next, *p;
4425 int old_idx, cur_idx;
4426 struct hdr_idx_elem *cur_hdr;
4427 const char *hn, *hv;
4428 int hnl, hvl;
4429 int type;
4430 const char *in;
4431 char *out;
4432 int len;
4433
4434 /* Create the table. */
4435 lua_newtable(L);
4436
4437 if (!htxn->s->txn)
4438 return 1;
4439
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004440 /* Check if a valid response is parsed */
4441 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4442 return 1;
4443
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004444 /* Build array of headers. */
4445 old_idx = 0;
4446 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
4447
4448 while (1) {
4449 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
4450 if (!cur_idx)
4451 break;
4452 old_idx = cur_idx;
4453
4454 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
4455 cur_ptr = cur_next;
4456 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
4457
4458 /* Now we have one full header at cur_ptr of len cur_hdr->len,
4459 * and the next header starts at cur_next. We'll check
4460 * this header in the list as well as against the default
4461 * rule.
4462 */
4463
4464 /* look for ': *'. */
4465 hn = cur_ptr;
4466 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
4467 if (p >= cur_ptr+cur_hdr->len)
4468 continue;
4469 hnl = p - hn;
4470 p++;
4471 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
4472 p++;
4473 if (p >= cur_ptr+cur_hdr->len)
4474 continue;
4475 hv = p;
4476 hvl = cur_ptr+cur_hdr->len-p;
4477
4478 /* Lowercase the key. Don't check the size of trash, it have
4479 * the size of one buffer and the input data contains in one
4480 * buffer.
4481 */
4482 out = trash.str;
4483 for (in=hn; in<hn+hnl; in++, out++)
4484 *out = tolower(*in);
4485 *out = '\0';
4486
4487 /* Check for existing entry:
4488 * assume that the table is on the top of the stack, and
4489 * push the key in the stack, the function lua_gettable()
4490 * perform the lookup.
4491 */
4492 lua_pushlstring(L, trash.str, hnl);
4493 lua_gettable(L, -2);
4494 type = lua_type(L, -1);
4495
4496 switch (type) {
4497 case LUA_TNIL:
4498 /* Table not found, create it. */
4499 lua_pop(L, 1); /* remove the nil value. */
4500 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
4501 lua_newtable(L); /* create and push empty table. */
4502 lua_pushlstring(L, hv, hvl); /* push header value. */
4503 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4504 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4505 break;
4506
4507 case LUA_TTABLE:
4508 /* Entry found: push the value in the table. */
4509 len = lua_rawlen(L, -1);
4510 lua_pushlstring(L, hv, hvl); /* push header value. */
4511 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4512 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4513 break;
4514
4515 default:
4516 /* Other cases are errors. */
4517 hlua_pusherror(L, "internal error during the parsing of headers.");
4518 WILL_LJMP(lua_error(L));
4519 }
4520 }
4521
4522 return 1;
4523}
4524
4525__LJMP static int hlua_http_req_get_headers(lua_State *L)
4526{
4527 struct hlua_txn *htxn;
4528
4529 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4530 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4531
4532 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4533}
4534
4535__LJMP static int hlua_http_res_get_headers(lua_State *L)
4536{
4537 struct hlua_txn *htxn;
4538
4539 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4540 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4541
4542 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4543}
4544
4545/* This function replace full header, or just a value in
4546 * the request or in the response. It is a wrapper fir the
4547 * 4 following functions.
4548 */
4549__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4550 struct http_msg *msg, int action)
4551{
4552 size_t name_len;
4553 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4554 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4555 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
4556 struct my_regex re;
4557
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004558 /* Check if a valid response is parsed */
4559 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4560 return 0;
4561
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004562 if (!regex_comp(reg, &re, 1, 1, NULL))
4563 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4564
4565 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
4566 regex_free(&re);
4567 return 0;
4568}
4569
4570__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4571{
4572 struct hlua_txn *htxn;
4573
4574 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4575 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4576
4577 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4578}
4579
4580__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4581{
4582 struct hlua_txn *htxn;
4583
4584 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4585 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4586
4587 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4588}
4589
4590__LJMP static int hlua_http_req_rep_val(lua_State *L)
4591{
4592 struct hlua_txn *htxn;
4593
4594 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4595 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4596
4597 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4598}
4599
4600__LJMP static int hlua_http_res_rep_val(lua_State *L)
4601{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004602 struct hlua_txn *htxn;
4603
4604 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4605 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4606
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004607 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004608}
4609
4610/* This function deletes all the occurences of an header.
4611 * It is a wrapper for the 2 following functions.
4612 */
4613__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4614{
4615 size_t len;
4616 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4617 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004618 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004619
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004620 /* Check if a valid response is parsed */
4621 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4622 return 0;
4623
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004624 ctx.idx = 0;
4625 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
4626 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4627 return 0;
4628}
4629
4630__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4631{
4632 struct hlua_txn *htxn;
4633
4634 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4635 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4636
Willy Tarreaueee5b512015-04-03 23:46:31 +02004637 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004638}
4639
4640__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4641{
4642 struct hlua_txn *htxn;
4643
4644 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4645 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4646
Willy Tarreaueee5b512015-04-03 23:46:31 +02004647 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004648}
4649
4650/* This function adds an header. It is a wrapper used by
4651 * the 2 following functions.
4652 */
4653__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4654{
4655 size_t name_len;
4656 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4657 size_t value_len;
4658 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
4659 char *p;
4660
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004661 /* Check if a valid message is parsed */
4662 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4663 return 0;
4664
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004665 /* Check length. */
4666 trash.len = value_len + name_len + 2;
4667 if (trash.len > trash.size)
4668 return 0;
4669
4670 /* Creates the header string. */
4671 p = trash.str;
4672 memcpy(p, name, name_len);
4673 p += name_len;
4674 *p = ':';
4675 p++;
4676 *p = ' ';
4677 p++;
4678 memcpy(p, value, value_len);
4679
Willy Tarreaueee5b512015-04-03 23:46:31 +02004680 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004681 trash.str, trash.len) != 0);
4682
4683 return 0;
4684}
4685
4686__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4687{
4688 struct hlua_txn *htxn;
4689
4690 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4691 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4692
Willy Tarreaueee5b512015-04-03 23:46:31 +02004693 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004694}
4695
4696__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4697{
4698 struct hlua_txn *htxn;
4699
4700 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4701 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4702
Willy Tarreaueee5b512015-04-03 23:46:31 +02004703 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004704}
4705
4706static int hlua_http_req_set_hdr(lua_State *L)
4707{
4708 struct hlua_txn *htxn;
4709
4710 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4711 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4712
Willy Tarreaueee5b512015-04-03 23:46:31 +02004713 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4714 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004715}
4716
4717static int hlua_http_res_set_hdr(lua_State *L)
4718{
4719 struct hlua_txn *htxn;
4720
4721 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4722 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4723
Willy Tarreaueee5b512015-04-03 23:46:31 +02004724 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4725 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004726}
4727
4728/* This function set the method. */
4729static int hlua_http_req_set_meth(lua_State *L)
4730{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004731 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004732 size_t name_len;
4733 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004734
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004735 /* Check if a valid request is parsed */
4736 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4737 lua_pushboolean(L, 0);
4738 return 1;
4739 }
4740
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004741 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004742 return 1;
4743}
4744
4745/* This function set the method. */
4746static int hlua_http_req_set_path(lua_State *L)
4747{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004748 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004749 size_t name_len;
4750 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004751
4752 /* Check if a valid request is parsed */
4753 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4754 lua_pushboolean(L, 0);
4755 return 1;
4756 }
4757
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004758 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004759 return 1;
4760}
4761
4762/* This function set the query-string. */
4763static int hlua_http_req_set_query(lua_State *L)
4764{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004765 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004766 size_t name_len;
4767 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004768
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004769 /* Check if a valid request is parsed */
4770 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4771 lua_pushboolean(L, 0);
4772 return 1;
4773 }
4774
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004775 /* Check length. */
4776 if (name_len > trash.size - 1) {
4777 lua_pushboolean(L, 0);
4778 return 1;
4779 }
4780
4781 /* Add the mark question as prefix. */
4782 chunk_reset(&trash);
4783 trash.str[trash.len++] = '?';
4784 memcpy(trash.str + trash.len, name, name_len);
4785 trash.len += name_len;
4786
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004787 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004788 return 1;
4789}
4790
4791/* This function set the uri. */
4792static int hlua_http_req_set_uri(lua_State *L)
4793{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004794 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004795 size_t name_len;
4796 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004797
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004798 /* Check if a valid request is parsed */
4799 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4800 lua_pushboolean(L, 0);
4801 return 1;
4802 }
4803
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004804 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004805 return 1;
4806}
4807
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004808/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004809static int hlua_http_res_set_status(lua_State *L)
4810{
4811 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4812 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004813 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004814
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004815 /* Check if a valid response is parsed */
4816 if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY))
4817 return 0;
4818
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004819 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004820 return 0;
4821}
4822
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004823/*
4824 *
4825 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004826 * Class TXN
4827 *
4828 *
4829 */
4830
4831/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004832 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004833 */
4834__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
4835{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004836 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004837}
4838
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004839__LJMP static int hlua_set_var(lua_State *L)
4840{
4841 struct hlua_txn *htxn;
4842 const char *name;
4843 size_t len;
4844 struct sample smp;
4845
4846 MAY_LJMP(check_args(L, 3, "set_var"));
4847
4848 /* It is useles to retrieve the stream, but this function
4849 * runs only in a stream context.
4850 */
4851 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4852 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4853
4854 /* Converts the third argument in a sample. */
4855 hlua_lua2smp(L, 3, &smp);
4856
4857 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01004858 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01004859 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004860 return 0;
4861}
4862
Christopher Faulet85d79c92016-11-09 16:54:56 +01004863__LJMP static int hlua_unset_var(lua_State *L)
4864{
4865 struct hlua_txn *htxn;
4866 const char *name;
4867 size_t len;
4868 struct sample smp;
4869
4870 MAY_LJMP(check_args(L, 2, "unset_var"));
4871
4872 /* It is useles to retrieve the stream, but this function
4873 * runs only in a stream context.
4874 */
4875 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4876 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4877
4878 /* Unset the variable. */
4879 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
4880 vars_unset_by_name(name, len, &smp);
4881 return 0;
4882}
4883
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004884__LJMP static int hlua_get_var(lua_State *L)
4885{
4886 struct hlua_txn *htxn;
4887 const char *name;
4888 size_t len;
4889 struct sample smp;
4890
4891 MAY_LJMP(check_args(L, 2, "get_var"));
4892
4893 /* It is useles to retrieve the stream, but this function
4894 * runs only in a stream context.
4895 */
4896 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4897 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4898
Willy Tarreau7560dd42016-03-10 16:28:58 +01004899 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01004900 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004901 lua_pushnil(L);
4902 return 1;
4903 }
4904
4905 return hlua_smp2lua(L, &smp);
4906}
4907
Willy Tarreau59551662015-03-10 14:23:13 +01004908__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004909{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004910 struct hlua *hlua;
4911
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004912 MAY_LJMP(check_args(L, 2, "set_priv"));
4913
Willy Tarreau87b09662015-04-03 00:22:06 +02004914 /* It is useles to retrieve the stream, but this function
4915 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004916 */
4917 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004918 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004919
4920 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004921 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004922
4923 /* Get and store new value. */
4924 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4925 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4926
4927 return 0;
4928}
4929
Willy Tarreau59551662015-03-10 14:23:13 +01004930__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004931{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004932 struct hlua *hlua;
4933
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004934 MAY_LJMP(check_args(L, 1, "get_priv"));
4935
Willy Tarreau87b09662015-04-03 00:22:06 +02004936 /* It is useles to retrieve the stream, but this function
4937 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004938 */
4939 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004940 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004941
4942 /* Push configuration index in the stack. */
4943 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4944
4945 return 1;
4946}
4947
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004948/* Create stack entry containing a class TXN. This function
4949 * return 0 if the stack does not contains free slots,
4950 * otherwise it returns 1.
4951 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02004952static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004953{
Willy Tarreaude491382015-04-06 11:04:28 +02004954 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004955
4956 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01004957 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004958 return 0;
4959
4960 /* NOTE: The allocation never fails. The failure
4961 * throw an error, and the function never returns.
4962 * if the throw is not avalaible, the process is aborted.
4963 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01004964 /* Create the object: obj[0] = userdata. */
4965 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02004966 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01004967 lua_rawseti(L, -2, 0);
4968
Willy Tarreaude491382015-04-06 11:04:28 +02004969 htxn->s = s;
4970 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01004971 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02004972 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004973
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004974 /* Create the "f" field that contains a list of fetches. */
4975 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01004976 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004977 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004978 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004979
4980 /* Create the "sf" field that contains a list of stringsafe fetches. */
4981 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01004982 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004983 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004984 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004985
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004986 /* Create the "c" field that contains a list of converters. */
4987 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02004988 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004989 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004990 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004991
4992 /* Create the "sc" field that contains a list of stringsafe converters. */
4993 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004994 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004995 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004996 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004997
Thierry FOURNIER397826a2015-03-11 19:39:09 +01004998 /* Create the "req" field that contains the request channel object. */
4999 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005000 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005001 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005002 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005003
5004 /* Create the "res" field that contains the response channel object. */
5005 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005006 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005007 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005008 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005009
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005010 /* Creates the HTTP object is the current proxy allows http. */
5011 lua_pushstring(L, "http");
5012 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005013 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005014 return 0;
5015 }
5016 else
5017 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005018 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005019
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005020 /* Pop a class sesison metatable and affect it to the userdata. */
5021 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5022 lua_setmetatable(L, -2);
5023
5024 return 1;
5025}
5026
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005027__LJMP static int hlua_txn_deflog(lua_State *L)
5028{
5029 const char *msg;
5030 struct hlua_txn *htxn;
5031
5032 MAY_LJMP(check_args(L, 2, "deflog"));
5033 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5034 msg = MAY_LJMP(luaL_checkstring(L, 2));
5035
5036 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5037 return 0;
5038}
5039
5040__LJMP static int hlua_txn_log(lua_State *L)
5041{
5042 int level;
5043 const char *msg;
5044 struct hlua_txn *htxn;
5045
5046 MAY_LJMP(check_args(L, 3, "log"));
5047 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5048 level = MAY_LJMP(luaL_checkinteger(L, 2));
5049 msg = MAY_LJMP(luaL_checkstring(L, 3));
5050
5051 if (level < 0 || level >= NB_LOG_LEVELS)
5052 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5053
5054 hlua_sendlog(htxn->s->be, level, msg);
5055 return 0;
5056}
5057
5058__LJMP static int hlua_txn_log_debug(lua_State *L)
5059{
5060 const char *msg;
5061 struct hlua_txn *htxn;
5062
5063 MAY_LJMP(check_args(L, 2, "Debug"));
5064 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5065 msg = MAY_LJMP(luaL_checkstring(L, 2));
5066 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5067 return 0;
5068}
5069
5070__LJMP static int hlua_txn_log_info(lua_State *L)
5071{
5072 const char *msg;
5073 struct hlua_txn *htxn;
5074
5075 MAY_LJMP(check_args(L, 2, "Info"));
5076 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5077 msg = MAY_LJMP(luaL_checkstring(L, 2));
5078 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5079 return 0;
5080}
5081
5082__LJMP static int hlua_txn_log_warning(lua_State *L)
5083{
5084 const char *msg;
5085 struct hlua_txn *htxn;
5086
5087 MAY_LJMP(check_args(L, 2, "Warning"));
5088 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5089 msg = MAY_LJMP(luaL_checkstring(L, 2));
5090 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5091 return 0;
5092}
5093
5094__LJMP static int hlua_txn_log_alert(lua_State *L)
5095{
5096 const char *msg;
5097 struct hlua_txn *htxn;
5098
5099 MAY_LJMP(check_args(L, 2, "Alert"));
5100 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5101 msg = MAY_LJMP(luaL_checkstring(L, 2));
5102 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5103 return 0;
5104}
5105
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005106__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5107{
5108 struct hlua_txn *htxn;
5109 int ll;
5110
5111 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5112 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5113 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5114
5115 if (ll < 0 || ll > 7)
5116 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5117
5118 htxn->s->logs.level = ll;
5119 return 0;
5120}
5121
5122__LJMP static int hlua_txn_set_tos(lua_State *L)
5123{
5124 struct hlua_txn *htxn;
5125 struct connection *cli_conn;
5126 int tos;
5127
5128 MAY_LJMP(check_args(L, 2, "set_tos"));
5129 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5130 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5131
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005132 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005133 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005134
5135 return 0;
5136}
5137
5138__LJMP static int hlua_txn_set_mark(lua_State *L)
5139{
5140#ifdef SO_MARK
5141 struct hlua_txn *htxn;
5142 struct connection *cli_conn;
5143 int mark;
5144
5145 MAY_LJMP(check_args(L, 2, "set_mark"));
5146 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5147 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5148
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005149 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005150 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005151#endif
5152 return 0;
5153}
5154
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005155/* This function is an Lua binding that send pending data
5156 * to the client, and close the stream interface.
5157 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005158__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005159{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005160 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005161 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005162 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005163
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005164 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005165 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005166 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005167
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005168 /* If the flags NOTERM is set, we cannot terminate the http
5169 * session, so we just end the execution of the current
5170 * lua code.
5171 */
5172 if (htxn->flags & HLUA_TXN_NOTERM) {
5173 WILL_LJMP(hlua_done(L));
5174 return 0;
5175 }
5176
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005177 ic = &htxn->s->req;
5178 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005179
Willy Tarreau630ef452015-08-28 10:06:15 +02005180 if (htxn->s->txn) {
5181 /* HTTP mode, let's stay in sync with the stream */
5182 bi_fast_delete(ic->buf, htxn->s->txn->req.sov);
5183 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
5184 htxn->s->txn->req.sov = 0;
5185 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
5186 oc->analysers = AN_RES_HTTP_XFER_BODY;
5187 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
5188 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
5189
Willy Tarreau630ef452015-08-28 10:06:15 +02005190 /* Note that if we want to support keep-alive, we need
5191 * to bypass the close/shutr_now calls below, but that
5192 * may only be done if the HTTP request was already
5193 * processed and the connection header is known (ie
5194 * not during TCP rules).
5195 */
5196 }
5197
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005198 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01005199 channel_abort(ic);
5200 channel_auto_close(ic);
5201 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005202
5203 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01005204 channel_auto_read(oc);
5205 channel_auto_close(oc);
5206 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005207
Willy Tarreau0458b082015-08-28 09:40:04 +02005208 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005209
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005210 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005211 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005212 return 0;
5213}
5214
5215__LJMP static int hlua_log(lua_State *L)
5216{
5217 int level;
5218 const char *msg;
5219
5220 MAY_LJMP(check_args(L, 2, "log"));
5221 level = MAY_LJMP(luaL_checkinteger(L, 1));
5222 msg = MAY_LJMP(luaL_checkstring(L, 2));
5223
5224 if (level < 0 || level >= NB_LOG_LEVELS)
5225 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5226
5227 hlua_sendlog(NULL, level, msg);
5228 return 0;
5229}
5230
5231__LJMP static int hlua_log_debug(lua_State *L)
5232{
5233 const char *msg;
5234
5235 MAY_LJMP(check_args(L, 1, "debug"));
5236 msg = MAY_LJMP(luaL_checkstring(L, 1));
5237 hlua_sendlog(NULL, LOG_DEBUG, msg);
5238 return 0;
5239}
5240
5241__LJMP static int hlua_log_info(lua_State *L)
5242{
5243 const char *msg;
5244
5245 MAY_LJMP(check_args(L, 1, "info"));
5246 msg = MAY_LJMP(luaL_checkstring(L, 1));
5247 hlua_sendlog(NULL, LOG_INFO, msg);
5248 return 0;
5249}
5250
5251__LJMP static int hlua_log_warning(lua_State *L)
5252{
5253 const char *msg;
5254
5255 MAY_LJMP(check_args(L, 1, "warning"));
5256 msg = MAY_LJMP(luaL_checkstring(L, 1));
5257 hlua_sendlog(NULL, LOG_WARNING, msg);
5258 return 0;
5259}
5260
5261__LJMP static int hlua_log_alert(lua_State *L)
5262{
5263 const char *msg;
5264
5265 MAY_LJMP(check_args(L, 1, "alert"));
5266 msg = MAY_LJMP(luaL_checkstring(L, 1));
5267 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005268 return 0;
5269}
5270
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005271__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005272{
5273 int wakeup_ms = lua_tointeger(L, -1);
5274 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005275 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005276 return 0;
5277}
5278
5279__LJMP static int hlua_sleep(lua_State *L)
5280{
5281 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005282 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005283
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005284 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005285
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005286 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005287 wakeup_ms = tick_add(now_ms, delay);
5288 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005289
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005290 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5291 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005292}
5293
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005294__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005295{
5296 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005297 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005298
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005299 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005300
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005301 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005302 wakeup_ms = tick_add(now_ms, delay);
5303 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005304
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005305 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5306 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005307}
5308
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005309/* This functionis an LUA binding. it permits to give back
5310 * the hand at the HAProxy scheduler. It is used when the
5311 * LUA processing consumes a lot of time.
5312 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005313__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005314{
5315 return 0;
5316}
5317
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005318__LJMP static int hlua_yield(lua_State *L)
5319{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005320 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
5321 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005322}
5323
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005324/* This function change the nice of the currently executed
5325 * task. It is used set low or high priority at the current
5326 * task.
5327 */
Willy Tarreau59551662015-03-10 14:23:13 +01005328__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005329{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005330 struct hlua *hlua;
5331 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005332
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005333 MAY_LJMP(check_args(L, 1, "set_nice"));
5334 hlua = hlua_gethlua(L);
5335 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005336
5337 /* If he task is not set, I'm in a start mode. */
5338 if (!hlua || !hlua->task)
5339 return 0;
5340
5341 if (nice < -1024)
5342 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005343 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005344 nice = 1024;
5345
5346 hlua->task->nice = nice;
5347 return 0;
5348}
5349
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005350/* This function is used as a calback of a task. It is called by the
5351 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5352 * return an E_AGAIN signal, the emmiter of this signal must set a
5353 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005354 *
5355 * Task wrapper are longjmp safe because the only one Lua code
5356 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005357 */
5358static struct task *hlua_process_task(struct task *task)
5359{
5360 struct hlua *hlua = task->context;
5361 enum hlua_exec status;
5362
5363 /* We need to remove the task from the wait queue before executing
5364 * the Lua code because we don't know if it needs to wait for
5365 * another timer or not in the case of E_AGAIN.
5366 */
5367 task_delete(task);
5368
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005369 /* If it is the first call to the task, we must initialize the
5370 * execution timeouts.
5371 */
5372 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005373 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005374
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005375 /* Execute the Lua code. */
5376 status = hlua_ctx_resume(hlua, 1);
5377
5378 switch (status) {
5379 /* finished or yield */
5380 case HLUA_E_OK:
5381 hlua_ctx_destroy(hlua);
5382 task_delete(task);
5383 task_free(task);
5384 break;
5385
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005386 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
5387 if (hlua->wake_time != TICK_ETERNITY)
5388 task_schedule(task, hlua->wake_time);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005389 break;
5390
5391 /* finished with error. */
5392 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005393 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005394 hlua_ctx_destroy(hlua);
5395 task_delete(task);
5396 task_free(task);
5397 break;
5398
5399 case HLUA_E_ERR:
5400 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005401 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005402 hlua_ctx_destroy(hlua);
5403 task_delete(task);
5404 task_free(task);
5405 break;
5406 }
5407 return NULL;
5408}
5409
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005410/* This function is an LUA binding that register LUA function to be
5411 * executed after the HAProxy configuration parsing and before the
5412 * HAProxy scheduler starts. This function expect only one LUA
5413 * argument that is a function. This function returns nothing, but
5414 * throws if an error is encountered.
5415 */
5416__LJMP static int hlua_register_init(lua_State *L)
5417{
5418 struct hlua_init_function *init;
5419 int ref;
5420
5421 MAY_LJMP(check_args(L, 1, "register_init"));
5422
5423 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5424
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005425 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005426 if (!init)
5427 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5428
5429 init->function_ref = ref;
5430 LIST_ADDQ(&hlua_init_functions, &init->l);
5431 return 0;
5432}
5433
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005434/* This functio is an LUA binding. It permits to register a task
5435 * executed in parallel of the main HAroxy activity. The task is
5436 * created and it is set in the HAProxy scheduler. It can be called
5437 * from the "init" section, "post init" or during the runtime.
5438 *
5439 * Lua prototype:
5440 *
5441 * <none> core.register_task(<function>)
5442 */
5443static int hlua_register_task(lua_State *L)
5444{
5445 struct hlua *hlua;
5446 struct task *task;
5447 int ref;
5448
5449 MAY_LJMP(check_args(L, 1, "register_task"));
5450
5451 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5452
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005453 hlua = pool_alloc2(pool2_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005454 if (!hlua)
5455 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5456
5457 task = task_new();
5458 task->context = hlua;
5459 task->process = hlua_process_task;
5460
5461 if (!hlua_ctx_init(hlua, task))
5462 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5463
5464 /* Restore the function in the stack. */
5465 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5466 hlua->nargs = 0;
5467
5468 /* Schedule task. */
5469 task_schedule(task, now_ms);
5470
5471 return 0;
5472}
5473
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005474/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5475 * doesn't allow "yield" functions because the HAProxy engine cannot
5476 * resume converters.
5477 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005478static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005479{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005480 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005481 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005482 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005483
Willy Tarreaube508f12016-03-10 11:47:01 +01005484 if (!stream)
5485 return 0;
5486
Willy Tarreau87b09662015-04-03 00:22:06 +02005487 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005488 * Lua context can be not initialized. This behavior
5489 * permits to save performances because a systematic
5490 * Lua initialization cause 5% performances loss.
5491 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005492 if (!stream->hlua) {
5493 stream->hlua = pool_alloc2(pool2_hlua);
5494 if (!stream->hlua) {
5495 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5496 return 0;
5497 }
5498 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5499 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5500 return 0;
5501 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005502 }
5503
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005504 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005505 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005506
5507 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005508 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5509 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5510 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005511 else
5512 error = "critical error";
5513 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005514 return 0;
5515 }
5516
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005517 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005518 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005519 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005520 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005521 return 0;
5522 }
5523
5524 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005525 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005526
5527 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005528 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005529 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005530 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005531 return 0;
5532 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005533 hlua_smp2lua(stream->hlua->T, smp);
5534 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005535
5536 /* push keywords in the stack. */
5537 if (arg_p) {
5538 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005539 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005540 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005541 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005542 return 0;
5543 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005544 hlua_arg2lua(stream->hlua->T, arg_p);
5545 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005546 }
5547 }
5548
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005549 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005550 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005551
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005552 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005553 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005554 }
5555
5556 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005557 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005558 /* finished. */
5559 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005560 /* If the stack is empty, the function fails. */
5561 if (lua_gettop(stream->hlua->T) <= 0)
5562 return 0;
5563
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005564 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005565 hlua_lua2smp(stream->hlua->T, -1, smp);
5566 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005567 return 1;
5568
5569 /* yield. */
5570 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005571 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005572 return 0;
5573
5574 /* finished with error. */
5575 case HLUA_E_ERRMSG:
5576 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005577 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005578 fcn->name, lua_tostring(stream->hlua->T, -1));
5579 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005580 return 0;
5581
5582 case HLUA_E_ERR:
5583 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005584 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005585
5586 default:
5587 return 0;
5588 }
5589}
5590
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005591/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5592 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005593 * resume sample-fetches. This function will be called by the sample
5594 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005595 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005596static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5597 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005598{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005599 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005600 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005601 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005602 const struct chunk msg = { .len = 0 };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005603
Willy Tarreaube508f12016-03-10 11:47:01 +01005604 if (!stream)
5605 return 0;
5606
Willy Tarreau87b09662015-04-03 00:22:06 +02005607 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005608 * Lua context can be not initialized. This behavior
5609 * permits to save performances because a systematic
5610 * Lua initialization cause 5% performances loss.
5611 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005612 if (!stream->hlua) {
5613 stream->hlua = pool_alloc2(pool2_hlua);
5614 if (!stream->hlua) {
5615 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5616 return 0;
5617 }
5618 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5619 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5620 return 0;
5621 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005622 }
5623
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005624 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005625
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005626 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005627 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005628
5629 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005630 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5631 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5632 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005633 else
5634 error = "critical error";
5635 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005636 return 0;
5637 }
5638
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005639 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005640 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005641 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005642 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005643 return 0;
5644 }
5645
5646 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005647 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005648
5649 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005650 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005651 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005652 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005653 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005654 return 0;
5655 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005656 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005657
5658 /* push keywords in the stack. */
5659 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5660 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005661 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005662 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005663 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005664 return 0;
5665 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005666 hlua_arg2lua(stream->hlua->T, arg_p);
5667 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005668 }
5669
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005670 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005671 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005672
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005673 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005674 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005675 }
5676
5677 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005678 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005679 /* finished. */
5680 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005681 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005682 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005683 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005684 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005685 /* If the stack is empty, the function fails. */
5686 if (lua_gettop(stream->hlua->T) <= 0)
5687 return 0;
5688
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005689 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005690 hlua_lua2smp(stream->hlua->T, -1, smp);
5691 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005692
5693 /* Set the end of execution flag. */
5694 smp->flags &= ~SMP_F_MAY_CHANGE;
5695 return 1;
5696
5697 /* yield. */
5698 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005699 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005700 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005701 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005702 return 0;
5703
5704 /* finished with error. */
5705 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005706 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005707 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005708 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005709 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005710 fcn->name, lua_tostring(stream->hlua->T, -1));
5711 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005712 return 0;
5713
5714 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005715 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005716 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005717 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005718 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005719
5720 default:
5721 return 0;
5722 }
5723}
5724
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005725/* This function is an LUA binding used for registering
5726 * "sample-conv" functions. It expects a converter name used
5727 * in the haproxy configuration file, and an LUA function.
5728 */
5729__LJMP static int hlua_register_converters(lua_State *L)
5730{
5731 struct sample_conv_kw_list *sck;
5732 const char *name;
5733 int ref;
5734 int len;
5735 struct hlua_function *fcn;
5736
5737 MAY_LJMP(check_args(L, 2, "register_converters"));
5738
5739 /* First argument : converter name. */
5740 name = MAY_LJMP(luaL_checkstring(L, 1));
5741
5742 /* Second argument : lua function. */
5743 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5744
5745 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005746 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005747 if (!sck)
5748 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005749 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005750 if (!fcn)
5751 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5752
5753 /* Fill fcn. */
5754 fcn->name = strdup(name);
5755 if (!fcn->name)
5756 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5757 fcn->function_ref = ref;
5758
5759 /* List head */
5760 sck->list.n = sck->list.p = NULL;
5761
5762 /* converter keyword. */
5763 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005764 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005765 if (!sck->kw[0].kw)
5766 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5767
5768 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
5769 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01005770 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 +01005771 sck->kw[0].val_args = NULL;
5772 sck->kw[0].in_type = SMP_T_STR;
5773 sck->kw[0].out_type = SMP_T_STR;
5774 sck->kw[0].private = fcn;
5775
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005776 /* Register this new converter */
5777 sample_register_convs(sck);
5778
5779 return 0;
5780}
5781
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005782/* This fucntion is an LUA binding used for registering
5783 * "sample-fetch" functions. It expects a converter name used
5784 * in the haproxy configuration file, and an LUA function.
5785 */
5786__LJMP static int hlua_register_fetches(lua_State *L)
5787{
5788 const char *name;
5789 int ref;
5790 int len;
5791 struct sample_fetch_kw_list *sfk;
5792 struct hlua_function *fcn;
5793
5794 MAY_LJMP(check_args(L, 2, "register_fetches"));
5795
5796 /* First argument : sample-fetch name. */
5797 name = MAY_LJMP(luaL_checkstring(L, 1));
5798
5799 /* Second argument : lua function. */
5800 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5801
5802 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005803 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005804 if (!sfk)
5805 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005806 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005807 if (!fcn)
5808 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5809
5810 /* Fill fcn. */
5811 fcn->name = strdup(name);
5812 if (!fcn->name)
5813 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5814 fcn->function_ref = ref;
5815
5816 /* List head */
5817 sfk->list.n = sfk->list.p = NULL;
5818
5819 /* sample-fetch keyword. */
5820 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005821 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005822 if (!sfk->kw[0].kw)
5823 return luaL_error(L, "lua out of memory error.");
5824
5825 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
5826 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01005827 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 +01005828 sfk->kw[0].val_args = NULL;
5829 sfk->kw[0].out_type = SMP_T_STR;
5830 sfk->kw[0].use = SMP_USE_HTTP_ANY;
5831 sfk->kw[0].val = 0;
5832 sfk->kw[0].private = fcn;
5833
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005834 /* Register this new fetch. */
5835 sample_register_fetches(sfk);
5836
5837 return 0;
5838}
5839
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005840/* This function is a wrapper to execute each LUA function declared
5841 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005842 * return ACT_RET_CONT if the processing is finished (with or without
5843 * error) and return ACT_RET_YIELD if the function must be called again
5844 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005845 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005846static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02005847 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005848{
5849 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005850 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005851 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005852 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005853 const struct chunk msg = { .len = 0 };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005854
5855 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01005856 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
5857 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
5858 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
5859 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005860 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005861 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005862 return ACT_RET_CONT;
5863 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005864
Willy Tarreau87b09662015-04-03 00:22:06 +02005865 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005866 * Lua context can be not initialized. This behavior
5867 * permits to save performances because a systematic
5868 * Lua initialization cause 5% performances loss.
5869 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005870 if (!s->hlua) {
5871 s->hlua = pool_alloc2(pool2_hlua);
5872 if (!s->hlua) {
5873 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
5874 rule->arg.hlua_rule->fcn.name);
5875 return ACT_RET_CONT;
5876 }
5877 if (!hlua_ctx_init(s->hlua, s->task)) {
5878 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
5879 rule->arg.hlua_rule->fcn.name);
5880 return ACT_RET_CONT;
5881 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005882 }
5883
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005884 consistency_set(s, dir, &s->hlua->cons);
5885
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005886 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005887 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005888
5889 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005890 if (!SET_SAFE_LJMP(s->hlua->T)) {
5891 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
5892 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005893 else
5894 error = "critical error";
5895 SEND_ERR(px, "Lua function '%s': %s.\n",
5896 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005897 return ACT_RET_CONT;
5898 }
5899
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005900 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005901 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005902 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005903 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005904 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005905 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005906 }
5907
5908 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005909 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005910
Willy Tarreau87b09662015-04-03 00:22:06 +02005911 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005912 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005913 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005914 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005915 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005916 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005917 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005918 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005919
5920 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005921 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005922 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005923 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005924 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005925 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005926 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005927 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005928 lua_pushstring(s->hlua->T, *arg);
5929 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005930 }
5931
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005932 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005933 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005934
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005935 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005936 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005937 }
5938
5939 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005940 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005941 /* finished. */
5942 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005943 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005944 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005945 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005946 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005947 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005948 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005949 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005950
5951 /* yield. */
5952 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005953 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005954 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005955 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005956 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005957 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005958 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005959 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005960 /* Some actions can be wake up when a "write" event
5961 * is detected on a response channel. This is useful
5962 * only for actions targetted on the requests.
5963 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005964 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005965 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01005966 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005967 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005968 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005969 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005970 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005971 /* We can quit the fcuntion without consistency check
5972 * because HAProxy is not able to manipulate data, it
5973 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005974 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005975
5976 /* finished with error. */
5977 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005978 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005979 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005980 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005981 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005982 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005983 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005984 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
5985 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005986 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005987
5988 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005989 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005990 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005991 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005992 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005993 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005994 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005995 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005996
5997 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005998 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005999 }
6000}
6001
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006002struct task *hlua_applet_wakeup(struct task *t)
6003{
6004 struct appctx *ctx = t->context;
6005 struct stream_interface *si = ctx->owner;
6006
6007 /* If the applet is wake up without any expected work, the sheduler
6008 * remove it from the run queue. This flag indicate that the applet
6009 * is waiting for write. If the buffer is full, the main processing
6010 * will send some data and after call the applet, otherwise it call
6011 * the applet ASAP.
6012 */
6013 si_applet_cant_put(si);
6014 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006015 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006016 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006017}
6018
6019static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6020{
6021 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006022 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006023 struct task *task;
6024 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006025 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006026
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006027 hlua = pool_alloc2(pool2_hlua);
6028 if (!hlua) {
6029 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6030 ctx->rule->arg.hlua_rule->fcn.name);
6031 return 0;
6032 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006033 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006034 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006035 ctx->ctx.hlua_apptcp.flags = 0;
6036
6037 /* Create task used by signal to wakeup applets. */
6038 task = task_new();
6039 if (!task) {
6040 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6041 ctx->rule->arg.hlua_rule->fcn.name);
6042 return 0;
6043 }
6044 task->nice = 0;
6045 task->context = ctx;
6046 task->process = hlua_applet_wakeup;
6047 ctx->ctx.hlua_apptcp.task = task;
6048
6049 /* In the execution wrappers linked with a stream, the
6050 * Lua context can be not initialized. This behavior
6051 * permits to save performances because a systematic
6052 * Lua initialization cause 5% performances loss.
6053 */
6054 if (!hlua_ctx_init(hlua, task)) {
6055 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6056 ctx->rule->arg.hlua_rule->fcn.name);
6057 return 0;
6058 }
6059
6060 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006061 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006062
6063 /* The following Lua calls can fail. */
6064 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006065 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6066 error = lua_tostring(hlua->T, -1);
6067 else
6068 error = "critical error";
6069 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6070 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006071 RESET_SAFE_LJMP(hlua->T);
6072 return 0;
6073 }
6074
6075 /* Check stack available size. */
6076 if (!lua_checkstack(hlua->T, 1)) {
6077 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6078 ctx->rule->arg.hlua_rule->fcn.name);
6079 RESET_SAFE_LJMP(hlua->T);
6080 return 0;
6081 }
6082
6083 /* Restore the function in the stack. */
6084 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6085
6086 /* Create and and push object stream in the stack. */
6087 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6088 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6089 ctx->rule->arg.hlua_rule->fcn.name);
6090 RESET_SAFE_LJMP(hlua->T);
6091 return 0;
6092 }
6093 hlua->nargs = 1;
6094
6095 /* push keywords in the stack. */
6096 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6097 if (!lua_checkstack(hlua->T, 1)) {
6098 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6099 ctx->rule->arg.hlua_rule->fcn.name);
6100 RESET_SAFE_LJMP(hlua->T);
6101 return 0;
6102 }
6103 lua_pushstring(hlua->T, *arg);
6104 hlua->nargs++;
6105 }
6106
6107 RESET_SAFE_LJMP(hlua->T);
6108
6109 /* Wakeup the applet ASAP. */
6110 si_applet_cant_get(si);
6111 si_applet_cant_put(si);
6112
6113 return 1;
6114}
6115
6116static void hlua_applet_tcp_fct(struct appctx *ctx)
6117{
6118 struct stream_interface *si = ctx->owner;
6119 struct stream *strm = si_strm(si);
6120 struct channel *res = si_ic(si);
6121 struct act_rule *rule = ctx->rule;
6122 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006123 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006124
6125 /* The applet execution is already done. */
6126 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE)
6127 return;
6128
6129 /* If the stream is disconnect or closed, ldo nothing. */
6130 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6131 return;
6132
6133 /* Execute the function. */
6134 switch (hlua_ctx_resume(hlua, 1)) {
6135 /* finished. */
6136 case HLUA_E_OK:
6137 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6138
6139 /* log time */
6140 strm->logs.tv_request = now;
6141
6142 /* eat the whole request */
6143 bo_skip(si_oc(si), si_ob(si)->o);
6144 res->flags |= CF_READ_NULL;
6145 si_shutr(si);
6146 return;
6147
6148 /* yield. */
6149 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006150 if (hlua->wake_time != TICK_ETERNITY)
6151 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006152 return;
6153
6154 /* finished with error. */
6155 case HLUA_E_ERRMSG:
6156 /* Display log. */
6157 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6158 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6159 lua_pop(hlua->T, 1);
6160 goto error;
6161
6162 case HLUA_E_ERR:
6163 /* Display log. */
6164 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6165 rule->arg.hlua_rule->fcn.name);
6166 goto error;
6167
6168 default:
6169 goto error;
6170 }
6171
6172error:
6173
6174 /* For all other cases, just close the stream. */
6175 si_shutw(si);
6176 si_shutr(si);
6177 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6178}
6179
6180static void hlua_applet_tcp_release(struct appctx *ctx)
6181{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006182 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006183 task_free(ctx->ctx.hlua_apptcp.task);
6184 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006185 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006186 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006187}
6188
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006189/* The function returns 1 if the initialisation is complete, 0 if
6190 * an errors occurs and -1 if more data are required for initializing
6191 * the applet.
6192 */
6193static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6194{
6195 struct stream_interface *si = ctx->owner;
6196 struct channel *req = si_oc(si);
6197 struct http_msg *msg;
6198 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006199 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006200 char **arg;
6201 struct hdr_ctx hdr;
6202 struct task *task;
6203 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006204 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006205
6206 /* Wait for a full HTTP request. */
6207 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6208 if (smp.flags & SMP_F_MAY_CHANGE)
6209 return -1;
6210 return 0;
6211 }
6212 txn = strm->txn;
6213 msg = &txn->req;
6214
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006215 /* We want two things in HTTP mode :
6216 * - enforce server-close mode if we were in keep-alive, so that the
6217 * applet is released after each response ;
6218 * - enable request body transfer to the applet in order to resync
6219 * with the response body.
6220 */
6221 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6222 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006223
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006224 hlua = pool_alloc2(pool2_hlua);
6225 if (!hlua) {
6226 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6227 ctx->rule->arg.hlua_rule->fcn.name);
6228 return 0;
6229 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006230 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006231 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006232 ctx->ctx.hlua_apphttp.left_bytes = -1;
6233 ctx->ctx.hlua_apphttp.flags = 0;
6234
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006235 if (txn->req.flags & HTTP_MSGF_VER_11)
6236 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6237
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006238 /* Create task used by signal to wakeup applets. */
6239 task = task_new();
6240 if (!task) {
6241 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6242 ctx->rule->arg.hlua_rule->fcn.name);
6243 return 0;
6244 }
6245 task->nice = 0;
6246 task->context = ctx;
6247 task->process = hlua_applet_wakeup;
6248 ctx->ctx.hlua_apphttp.task = task;
6249
6250 /* In the execution wrappers linked with a stream, the
6251 * Lua context can be not initialized. This behavior
6252 * permits to save performances because a systematic
6253 * Lua initialization cause 5% performances loss.
6254 */
6255 if (!hlua_ctx_init(hlua, task)) {
6256 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6257 ctx->rule->arg.hlua_rule->fcn.name);
6258 return 0;
6259 }
6260
6261 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006262 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006263
6264 /* The following Lua calls can fail. */
6265 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006266 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6267 error = lua_tostring(hlua->T, -1);
6268 else
6269 error = "critical error";
6270 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6271 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006272 return 0;
6273 }
6274
6275 /* Check stack available size. */
6276 if (!lua_checkstack(hlua->T, 1)) {
6277 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6278 ctx->rule->arg.hlua_rule->fcn.name);
6279 RESET_SAFE_LJMP(hlua->T);
6280 return 0;
6281 }
6282
6283 /* Restore the function in the stack. */
6284 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6285
6286 /* Create and and push object stream in the stack. */
6287 if (!hlua_applet_http_new(hlua->T, ctx)) {
6288 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6289 ctx->rule->arg.hlua_rule->fcn.name);
6290 RESET_SAFE_LJMP(hlua->T);
6291 return 0;
6292 }
6293 hlua->nargs = 1;
6294
6295 /* Look for a 100-continue expected. */
6296 if (msg->flags & HTTP_MSGF_VER_11) {
6297 hdr.idx = 0;
6298 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) &&
6299 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6300 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6301 }
6302
6303 /* push keywords in the stack. */
6304 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6305 if (!lua_checkstack(hlua->T, 1)) {
6306 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6307 ctx->rule->arg.hlua_rule->fcn.name);
6308 RESET_SAFE_LJMP(hlua->T);
6309 return 0;
6310 }
6311 lua_pushstring(hlua->T, *arg);
6312 hlua->nargs++;
6313 }
6314
6315 RESET_SAFE_LJMP(hlua->T);
6316
6317 /* Wakeup the applet when data is ready for read. */
6318 si_applet_cant_get(si);
6319
6320 return 1;
6321}
6322
6323static void hlua_applet_http_fct(struct appctx *ctx)
6324{
6325 struct stream_interface *si = ctx->owner;
6326 struct stream *strm = si_strm(si);
6327 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006328 struct act_rule *rule = ctx->rule;
6329 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006330 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006331 char *blk1;
6332 int len1;
6333 char *blk2;
6334 int len2;
6335 int ret;
6336
6337 /* If the stream is disconnect or closed, ldo nothing. */
6338 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6339 return;
6340
6341 /* Set the currently running flag. */
6342 if (!HLUA_IS_RUNNING(hlua) &&
6343 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6344
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006345 /* Wait for full HTTP analysys. */
6346 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
6347 si_applet_cant_get(si);
6348 return;
6349 }
6350
6351 /* Store the max amount of bytes that we can read. */
6352 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6353
6354 /* We need to flush the request header. This left the body
6355 * for the Lua.
6356 */
6357
6358 /* Read the maximum amount of data avalaible. */
6359 ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
6360 if (ret == -1)
6361 return;
6362
6363 /* No data available, ask for more data. */
6364 if (ret == 1)
6365 len2 = 0;
6366 if (ret == 0)
6367 len1 = 0;
6368 if (len1 + len2 < strm->txn->req.eoh + 2) {
6369 si_applet_cant_get(si);
6370 return;
6371 }
6372
6373 /* skip the requests bytes. */
6374 bo_skip(si_oc(si), strm->txn->req.eoh + 2);
6375 }
6376
6377 /* Executes The applet if it is not done. */
6378 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6379
6380 /* Execute the function. */
6381 switch (hlua_ctx_resume(hlua, 1)) {
6382 /* finished. */
6383 case HLUA_E_OK:
6384 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6385 break;
6386
6387 /* yield. */
6388 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006389 if (hlua->wake_time != TICK_ETERNITY)
6390 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006391 return;
6392
6393 /* finished with error. */
6394 case HLUA_E_ERRMSG:
6395 /* Display log. */
6396 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6397 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6398 lua_pop(hlua->T, 1);
6399 goto error;
6400
6401 case HLUA_E_ERR:
6402 /* Display log. */
6403 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6404 rule->arg.hlua_rule->fcn.name);
6405 goto error;
6406
6407 default:
6408 goto error;
6409 }
6410 }
6411
6412 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6413
6414 /* We must send the final chunk. */
6415 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6416 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6417
6418 /* sent last chunk at once. */
6419 ret = bi_putblk(res, "0\r\n\r\n", 5);
6420
6421 /* critical error. */
6422 if (ret == -2 || ret == -3) {
6423 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6424 rule->arg.hlua_rule->fcn.name);
6425 goto error;
6426 }
6427
6428 /* no enough space error. */
6429 if (ret == -1) {
6430 si_applet_cant_put(si);
6431 return;
6432 }
6433
6434 /* set the last chunk sent. */
6435 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6436 }
6437
6438 /* close the connection. */
6439
6440 /* status / log */
6441 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6442 strm->logs.tv_request = now;
6443
6444 /* eat the whole request */
6445 bo_skip(si_oc(si), si_ob(si)->o);
6446 res->flags |= CF_READ_NULL;
6447 si_shutr(si);
6448
6449 return;
6450 }
6451
6452error:
6453
6454 /* If we are in HTTP mode, and we are not send any
6455 * data, return a 500 server error in best effort:
6456 * if there are no room avalaible in the buffer,
6457 * just close the connection.
6458 */
6459 bi_putblk(res, error_500, strlen(error_500));
6460 if (!(strm->flags & SF_ERR_MASK))
6461 strm->flags |= SF_ERR_RESOURCE;
6462 si_shutw(si);
6463 si_shutr(si);
6464 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6465}
6466
6467static void hlua_applet_http_release(struct appctx *ctx)
6468{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006469 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006470 task_free(ctx->ctx.hlua_apphttp.task);
6471 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006472 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006473 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006474}
6475
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006476/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6477 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006478 *
6479 * This function can fail with an abort() due to an Lua critical error.
6480 * We are in the configuration parsing process of HAProxy, this abort() is
6481 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006482 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006483static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6484 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006485{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006486 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006487 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006488
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006489 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006490 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006491 if (!rule->arg.hlua_rule) {
6492 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006493 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006494 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006495
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006496 /* Memory for arguments. */
6497 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6498 if (!rule->arg.hlua_rule->args) {
6499 memprintf(err, "out of memory error");
6500 return ACT_RET_PRS_ERR;
6501 }
6502
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006503 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006504 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006505
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006506 /* Expect some arguments */
6507 for (i = 0; i < fcn->nargs; i++) {
6508 if (*args[i+1] == '\0') {
6509 memprintf(err, "expect %d arguments", fcn->nargs);
6510 return ACT_RET_PRS_ERR;
6511 }
6512 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6513 if (!rule->arg.hlua_rule->args[i]) {
6514 memprintf(err, "out of memory error");
6515 return ACT_RET_PRS_ERR;
6516 }
6517 (*cur_arg)++;
6518 }
6519 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006520
Thierry FOURNIER42148732015-09-02 17:17:33 +02006521 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006522 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006523 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006524}
6525
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006526static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6527 struct act_rule *rule, char **err)
6528{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006529 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006530
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006531 /* HTTP applets are forbidden in tcp-request rules.
6532 * HTTP applet request requires everything initilized by
6533 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6534 * The applet will be immediately initilized, but its before
6535 * the call of this analyzer.
6536 */
6537 if (rule->from != ACT_F_HTTP_REQ) {
6538 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6539 return ACT_RET_PRS_ERR;
6540 }
6541
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006542 /* Memory for the rule. */
6543 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6544 if (!rule->arg.hlua_rule) {
6545 memprintf(err, "out of memory error");
6546 return ACT_RET_PRS_ERR;
6547 }
6548
6549 /* Reference the Lua function and store the reference. */
6550 rule->arg.hlua_rule->fcn = *fcn;
6551
6552 /* TODO: later accept arguments. */
6553 rule->arg.hlua_rule->args = NULL;
6554
6555 /* Add applet pointer in the rule. */
6556 rule->applet.obj_type = OBJ_TYPE_APPLET;
6557 rule->applet.name = fcn->name;
6558 rule->applet.init = hlua_applet_http_init;
6559 rule->applet.fct = hlua_applet_http_fct;
6560 rule->applet.release = hlua_applet_http_release;
6561 rule->applet.timeout = hlua_timeout_applet;
6562
6563 return ACT_RET_PRS_OK;
6564}
6565
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006566/* This function is an LUA binding used for registering
6567 * "sample-conv" functions. It expects a converter name used
6568 * in the haproxy configuration file, and an LUA function.
6569 */
6570__LJMP static int hlua_register_action(lua_State *L)
6571{
6572 struct action_kw_list *akl;
6573 const char *name;
6574 int ref;
6575 int len;
6576 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006577 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006578
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006579 /* Initialise the number of expected arguments at 0. */
6580 nargs = 0;
6581
6582 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6583 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006584
6585 /* First argument : converter name. */
6586 name = MAY_LJMP(luaL_checkstring(L, 1));
6587
6588 /* Second argument : environment. */
6589 if (lua_type(L, 2) != LUA_TTABLE)
6590 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6591
6592 /* Third argument : lua function. */
6593 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6594
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006595 /* Fouth argument : number of mandatories arguments expected on the configuration line. */
6596 if (lua_gettop(L) >= 4)
6597 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6598
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006599 /* browse the second argulent as an array. */
6600 lua_pushnil(L);
6601 while (lua_next(L, 2) != 0) {
6602 if (lua_type(L, -1) != LUA_TSTRING)
6603 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6604
6605 /* Check required environment. Only accepted "http" or "tcp". */
6606 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006607 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006608 if (!akl)
6609 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006610 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006611 if (!fcn)
6612 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6613
6614 /* Fill fcn. */
6615 fcn->name = strdup(name);
6616 if (!fcn->name)
6617 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6618 fcn->function_ref = ref;
6619
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006620 /* Set the expected number od arguments. */
6621 fcn->nargs = nargs;
6622
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006623 /* List head */
6624 akl->list.n = akl->list.p = NULL;
6625
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006626 /* action keyword. */
6627 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006628 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006629 if (!akl->kw[0].kw)
6630 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6631
6632 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6633
6634 akl->kw[0].match_pfx = 0;
6635 akl->kw[0].private = fcn;
6636 akl->kw[0].parse = action_register_lua;
6637
6638 /* select the action registering point. */
6639 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6640 tcp_req_cont_keywords_register(akl);
6641 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6642 tcp_res_cont_keywords_register(akl);
6643 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6644 http_req_keywords_register(akl);
6645 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6646 http_res_keywords_register(akl);
6647 else
6648 WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. "
6649 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6650 "are expected.", lua_tostring(L, -1)));
6651
6652 /* pop the environment string. */
6653 lua_pop(L, 1);
6654 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006655 return ACT_RET_PRS_OK;
6656}
6657
6658static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6659 struct act_rule *rule, char **err)
6660{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006661 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006662
6663 /* Memory for the rule. */
6664 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6665 if (!rule->arg.hlua_rule) {
6666 memprintf(err, "out of memory error");
6667 return ACT_RET_PRS_ERR;
6668 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006669
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006670 /* Reference the Lua function and store the reference. */
6671 rule->arg.hlua_rule->fcn = *fcn;
6672
6673 /* TODO: later accept arguments. */
6674 rule->arg.hlua_rule->args = NULL;
6675
6676 /* Add applet pointer in the rule. */
6677 rule->applet.obj_type = OBJ_TYPE_APPLET;
6678 rule->applet.name = fcn->name;
6679 rule->applet.init = hlua_applet_tcp_init;
6680 rule->applet.fct = hlua_applet_tcp_fct;
6681 rule->applet.release = hlua_applet_tcp_release;
6682 rule->applet.timeout = hlua_timeout_applet;
6683
6684 return 0;
6685}
6686
6687/* This function is an LUA binding used for registering
6688 * "sample-conv" functions. It expects a converter name used
6689 * in the haproxy configuration file, and an LUA function.
6690 */
6691__LJMP static int hlua_register_service(lua_State *L)
6692{
6693 struct action_kw_list *akl;
6694 const char *name;
6695 const char *env;
6696 int ref;
6697 int len;
6698 struct hlua_function *fcn;
6699
6700 MAY_LJMP(check_args(L, 3, "register_service"));
6701
6702 /* First argument : converter name. */
6703 name = MAY_LJMP(luaL_checkstring(L, 1));
6704
6705 /* Second argument : environment. */
6706 env = MAY_LJMP(luaL_checkstring(L, 2));
6707
6708 /* Third argument : lua function. */
6709 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6710
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006711 /* Allocate and fill the sample fetch keyword struct. */
6712 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
6713 if (!akl)
6714 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6715 fcn = calloc(1, sizeof(*fcn));
6716 if (!fcn)
6717 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6718
6719 /* Fill fcn. */
6720 len = strlen("<lua.>") + strlen(name) + 1;
6721 fcn->name = calloc(1, len);
6722 if (!fcn->name)
6723 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6724 snprintf((char *)fcn->name, len, "<lua.%s>", name);
6725 fcn->function_ref = ref;
6726
6727 /* List head */
6728 akl->list.n = akl->list.p = NULL;
6729
6730 /* converter keyword. */
6731 len = strlen("lua.") + strlen(name) + 1;
6732 akl->kw[0].kw = calloc(1, len);
6733 if (!akl->kw[0].kw)
6734 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6735
6736 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6737
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01006738 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006739 if (strcmp(env, "tcp") == 0)
6740 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006741 else if (strcmp(env, "http") == 0)
6742 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006743 else
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006744 WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. "
6745 "'tcp' or 'http' are expected."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006746
6747 akl->kw[0].match_pfx = 0;
6748 akl->kw[0].private = fcn;
6749
6750 /* End of array. */
6751 memset(&akl->kw[1], 0, sizeof(*akl->kw));
6752
6753 /* Register this new converter */
6754 service_keywords_register(akl);
6755
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006756 return 0;
6757}
6758
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006759/* This function initialises Lua cli handler. It copies the
6760 * arguments in the Lua stack and create channel IO objects.
6761 */
6762static int hlua_cli_parse_fct(char **args, struct appctx *appctx, void *private)
6763{
6764 struct hlua *hlua;
6765 struct hlua_function *fcn;
6766 int i;
6767 const char *error;
6768
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006769 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006770 appctx->ctx.hlua_cli.fcn = private;
6771
6772 hlua = pool_alloc2(pool2_hlua);
6773 if (!hlua) {
6774 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006775 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006776 }
6777 HLUA_INIT(hlua);
6778 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006779
6780 /* Create task used by signal to wakeup applets.
6781 * We use the same wakeup fonction than the Lua applet_tcp and
6782 * applet_http. It is absolutely compatible.
6783 */
6784 appctx->ctx.hlua_cli.task = task_new();
6785 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01006786 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006787 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006788 }
6789 appctx->ctx.hlua_cli.task->nice = 0;
6790 appctx->ctx.hlua_cli.task->context = appctx;
6791 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
6792
6793 /* Initialises the Lua context */
6794 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
6795 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006796 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006797 }
6798
6799 /* The following Lua calls can fail. */
6800 if (!SET_SAFE_LJMP(hlua->T)) {
6801 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6802 error = lua_tostring(hlua->T, -1);
6803 else
6804 error = "critical error";
6805 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
6806 goto error;
6807 }
6808
6809 /* Check stack available size. */
6810 if (!lua_checkstack(hlua->T, 2)) {
6811 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6812 goto error;
6813 }
6814
6815 /* Restore the function in the stack. */
6816 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
6817
6818 /* Once the arguments parsed, the CLI is like an AppletTCP,
6819 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006820 */
6821 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
6822 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6823 goto error;
6824 }
6825 hlua->nargs = 1;
6826
6827 /* push keywords in the stack. */
6828 for (i = 0; *args[i]; i++) {
6829 /* Check stack available size. */
6830 if (!lua_checkstack(hlua->T, 1)) {
6831 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6832 goto error;
6833 }
6834 lua_pushstring(hlua->T, args[i]);
6835 hlua->nargs++;
6836 }
6837
6838 /* We must initialize the execution timeouts. */
6839 hlua->max_time = hlua_timeout_session;
6840
6841 /* At this point the execution is safe. */
6842 RESET_SAFE_LJMP(hlua->T);
6843
6844 /* It's ok */
6845 return 0;
6846
6847 /* It's not ok. */
6848error:
6849 RESET_SAFE_LJMP(hlua->T);
6850 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006851 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006852 return 1;
6853}
6854
6855static int hlua_cli_io_handler_fct(struct appctx *appctx)
6856{
6857 struct hlua *hlua;
6858 struct stream_interface *si;
6859 struct hlua_function *fcn;
6860
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006861 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006862 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01006863 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006864
6865 /* If the stream is disconnect or closed, ldo nothing. */
6866 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6867 return 1;
6868
6869 /* Execute the function. */
6870 switch (hlua_ctx_resume(hlua, 1)) {
6871
6872 /* finished. */
6873 case HLUA_E_OK:
6874 return 1;
6875
6876 /* yield. */
6877 case HLUA_E_AGAIN:
6878 /* We want write. */
6879 if (HLUA_IS_WAKERESWR(hlua))
6880 si_applet_cant_put(si);
6881 /* Set the timeout. */
6882 if (hlua->wake_time != TICK_ETERNITY)
6883 task_schedule(hlua->task, hlua->wake_time);
6884 return 0;
6885
6886 /* finished with error. */
6887 case HLUA_E_ERRMSG:
6888 /* Display log. */
6889 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
6890 fcn->name, lua_tostring(hlua->T, -1));
6891 lua_pop(hlua->T, 1);
6892 return 1;
6893
6894 case HLUA_E_ERR:
6895 /* Display log. */
6896 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
6897 fcn->name);
6898 return 1;
6899
6900 default:
6901 return 1;
6902 }
6903
6904 return 1;
6905}
6906
6907static void hlua_cli_io_release_fct(struct appctx *appctx)
6908{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006909 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006910 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006911}
6912
6913/* This function is an LUA binding used for registering
6914 * new keywords in the cli. It expects a list of keywords
6915 * which are the "path". It is limited to 5 keywords. A
6916 * description of the command, a function to be executed
6917 * for the parsing and a function for io handlers.
6918 */
6919__LJMP static int hlua_register_cli(lua_State *L)
6920{
6921 struct cli_kw_list *cli_kws;
6922 const char *message;
6923 int ref_io;
6924 int len;
6925 struct hlua_function *fcn;
6926 int index;
6927 int i;
6928
6929 MAY_LJMP(check_args(L, 3, "register_cli"));
6930
6931 /* First argument : an array of maximum 5 keywords. */
6932 if (!lua_istable(L, 1))
6933 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
6934
6935 /* Second argument : string with contextual message. */
6936 message = MAY_LJMP(luaL_checkstring(L, 2));
6937
6938 /* Third and fourth argument : lua function. */
6939 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
6940
6941 /* Allocate and fill the sample fetch keyword struct. */
6942 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
6943 if (!cli_kws)
6944 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6945 fcn = calloc(1, sizeof(*fcn));
6946 if (!fcn)
6947 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6948
6949 /* Fill path. */
6950 index = 0;
6951 lua_pushnil(L);
6952 while(lua_next(L, 1) != 0) {
6953 if (index >= 5)
6954 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
6955 if (lua_type(L, -1) != LUA_TSTRING)
6956 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
6957 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
6958 if (!cli_kws->kw[0].str_kw[index])
6959 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6960 index++;
6961 lua_pop(L, 1);
6962 }
6963
6964 /* Copy help message. */
6965 cli_kws->kw[0].usage = strdup(message);
6966 if (!cli_kws->kw[0].usage)
6967 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6968
6969 /* Fill fcn io handler. */
6970 len = strlen("<lua.cli>") + 1;
6971 for (i = 0; i < index; i++)
6972 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
6973 fcn->name = calloc(1, len);
6974 if (!fcn->name)
6975 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6976 strncat((char *)fcn->name, "<lua.cli", len);
6977 for (i = 0; i < index; i++) {
6978 strncat((char *)fcn->name, ".", len);
6979 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
6980 }
6981 strncat((char *)fcn->name, ">", len);
6982 fcn->function_ref = ref_io;
6983
6984 /* Fill last entries. */
6985 cli_kws->kw[0].private = fcn;
6986 cli_kws->kw[0].parse = hlua_cli_parse_fct;
6987 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
6988 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
6989
6990 /* Register this new converter */
6991 cli_register_kw(cli_kws);
6992
6993 return 0;
6994}
6995
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006996static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
6997 struct proxy *defpx, const char *file, int line,
6998 char **err, unsigned int *timeout)
6999{
7000 const char *error;
7001
7002 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
7003 if (error && *error != '\0') {
7004 memprintf(err, "%s: invalid timeout", args[0]);
7005 return -1;
7006 }
7007 return 0;
7008}
7009
7010static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7011 struct proxy *defpx, const char *file, int line,
7012 char **err)
7013{
7014 return hlua_read_timeout(args, section_type, curpx, defpx,
7015 file, line, err, &hlua_timeout_session);
7016}
7017
7018static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7019 struct proxy *defpx, const char *file, int line,
7020 char **err)
7021{
7022 return hlua_read_timeout(args, section_type, curpx, defpx,
7023 file, line, err, &hlua_timeout_task);
7024}
7025
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007026static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7027 struct proxy *defpx, const char *file, int line,
7028 char **err)
7029{
7030 return hlua_read_timeout(args, section_type, curpx, defpx,
7031 file, line, err, &hlua_timeout_applet);
7032}
7033
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007034static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7035 struct proxy *defpx, const char *file, int line,
7036 char **err)
7037{
7038 char *error;
7039
7040 hlua_nb_instruction = strtoll(args[1], &error, 10);
7041 if (*error != '\0') {
7042 memprintf(err, "%s: invalid number", args[0]);
7043 return -1;
7044 }
7045 return 0;
7046}
7047
Willy Tarreau32f61e22015-03-18 17:54:59 +01007048static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7049 struct proxy *defpx, const char *file, int line,
7050 char **err)
7051{
7052 char *error;
7053
7054 if (*(args[1]) == 0) {
7055 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7056 return -1;
7057 }
7058 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7059 if (*error != '\0') {
7060 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7061 return -1;
7062 }
7063 return 0;
7064}
7065
7066
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007067/* This function is called by the main configuration key "lua-load". It loads and
7068 * execute an lua file during the parsing of the HAProxy configuration file. It is
7069 * the main lua entry point.
7070 *
7071 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
7072 * occured, otherwise it returns 0.
7073 *
7074 * In some error case, LUA set an error message in top of the stack. This function
7075 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007076 *
7077 * This function can fail with an abort() due to an Lua critical error.
7078 * We are in the configuration parsing process of HAProxy, this abort() is
7079 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007080 */
7081static int hlua_load(char **args, int section_type, struct proxy *curpx,
7082 struct proxy *defpx, const char *file, int line,
7083 char **err)
7084{
7085 int error;
7086
7087 /* Just load and compile the file. */
7088 error = luaL_loadfile(gL.T, args[1]);
7089 if (error) {
7090 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
7091 lua_pop(gL.T, 1);
7092 return -1;
7093 }
7094
7095 /* If no syntax error where detected, execute the code. */
7096 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7097 switch (error) {
7098 case LUA_OK:
7099 break;
7100 case LUA_ERRRUN:
7101 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
7102 lua_pop(gL.T, 1);
7103 return -1;
7104 case LUA_ERRMEM:
7105 memprintf(err, "lua out of memory error\n");
7106 return -1;
7107 case LUA_ERRERR:
7108 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
7109 lua_pop(gL.T, 1);
7110 return -1;
7111 case LUA_ERRGCMM:
7112 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
7113 lua_pop(gL.T, 1);
7114 return -1;
7115 default:
7116 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
7117 lua_pop(gL.T, 1);
7118 return -1;
7119 }
7120
7121 return 0;
7122}
7123
7124/* configuration keywords declaration */
7125static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007126 { CFG_GLOBAL, "lua-load", hlua_load },
7127 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7128 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007129 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007130 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007131 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007132 { 0, NULL, NULL },
7133}};
7134
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007135/* This function can fail with an abort() due to an Lua critical error.
7136 * We are in the initialisation process of HAProxy, this abort() is
7137 * tolerated.
7138 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007139int hlua_post_init()
7140{
7141 struct hlua_init_function *init;
7142 const char *msg;
7143 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007144 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007145
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007146 /* Call post initialisation function in safe environement. */
7147 if (!SET_SAFE_LJMP(gL.T)) {
7148 if (lua_type(gL.T, -1) == LUA_TSTRING)
7149 error = lua_tostring(gL.T, -1);
7150 else
7151 error = "critical error";
7152 fprintf(stderr, "Lua post-init: %s.\n", error);
7153 exit(1);
7154 }
7155 hlua_fcn_post_init(gL.T);
7156 RESET_SAFE_LJMP(gL.T);
7157
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007158 list_for_each_entry(init, &hlua_init_functions, l) {
7159 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7160 ret = hlua_ctx_resume(&gL, 0);
7161 switch (ret) {
7162 case HLUA_E_OK:
7163 lua_pop(gL.T, -1);
7164 return 1;
7165 case HLUA_E_AGAIN:
7166 Alert("lua init: yield not allowed.\n");
7167 return 0;
7168 case HLUA_E_ERRMSG:
7169 msg = lua_tostring(gL.T, -1);
7170 Alert("lua init: %s.\n", msg);
7171 return 0;
7172 case HLUA_E_ERR:
7173 default:
7174 Alert("lua init: unknown runtime error.\n");
7175 return 0;
7176 }
7177 }
7178 return 1;
7179}
7180
Willy Tarreau32f61e22015-03-18 17:54:59 +01007181/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7182 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7183 * is the previously allocated size or the kind of object in case of a new
7184 * allocation. <nsize> is the requested new size.
7185 */
7186static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7187{
7188 struct hlua_mem_allocator *zone = ud;
7189
7190 if (nsize == 0) {
7191 /* it's a free */
7192 if (ptr)
7193 zone->allocated -= osize;
7194 free(ptr);
7195 return NULL;
7196 }
7197
7198 if (!ptr) {
7199 /* it's a new allocation */
7200 if (zone->limit && zone->allocated + nsize > zone->limit)
7201 return NULL;
7202
7203 ptr = malloc(nsize);
7204 if (ptr)
7205 zone->allocated += nsize;
7206 return ptr;
7207 }
7208
7209 /* it's a realloc */
7210 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7211 return NULL;
7212
7213 ptr = realloc(ptr, nsize);
7214 if (ptr)
7215 zone->allocated += nsize - osize;
7216 return ptr;
7217}
7218
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007219/* Ithis function can fail with an abort() due to an Lua critical error.
7220 * We are in the initialisation process of HAProxy, this abort() is
7221 * tolerated.
7222 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007223void hlua_init(void)
7224{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007225 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007226 int idx;
7227 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007228 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007229 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007230 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007231#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007232 struct srv_kw *kw;
7233 int tmp_error;
7234 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007235 char *args[] = { /* SSL client configuration. */
7236 "ssl",
7237 "verify",
7238 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007239 NULL
7240 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007241#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007242
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007243 /* Initialise struct hlua and com signals pool */
7244 pool2_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007245
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007246 /* Register configuration keywords. */
7247 cfg_register_keywords(&cfg_kws);
7248
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007249 /* Init main lua stack. */
7250 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007251 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007252 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007253 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007254 hlua_sethlua(&gL);
7255 gL.Tref = LUA_REFNIL;
7256 gL.task = NULL;
7257
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007258 /* From this point, until the end of the initialisation fucntion,
7259 * the Lua function can fail with an abort. We are in the initialisation
7260 * process of HAProxy, this abort() is tolerated.
7261 */
7262
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007263 /* Initialise lua. */
7264 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007265
Thierry Fournier75933d42016-01-21 09:30:18 +01007266 /* Set safe environment for the initialisation. */
7267 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007268 if (lua_type(gL.T, -1) == LUA_TSTRING)
7269 error_msg = lua_tostring(gL.T, -1);
7270 else
7271 error_msg = "critical error";
7272 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007273 exit(1);
7274 }
7275
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007276 /*
7277 *
7278 * Create "core" object.
7279 *
7280 */
7281
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007282 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007283 lua_newtable(gL.T);
7284
7285 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007286 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007287 hlua_class_const_int(gL.T, log_levels[i], i);
7288
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007289 /* Register special functions. */
7290 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007291 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007292 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007293 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007294 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007295 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007296 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007297 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007298 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007299 hlua_class_function(gL.T, "sleep", hlua_sleep);
7300 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007301 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7302 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7303 hlua_class_function(gL.T, "set_map", hlua_set_map);
7304 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007305 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007306 hlua_class_function(gL.T, "log", hlua_log);
7307 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7308 hlua_class_function(gL.T, "Info", hlua_log_info);
7309 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7310 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007311 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007312 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007313
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007314 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007315
7316 /*
7317 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007318 * Register class Map
7319 *
7320 */
7321
7322 /* This table entry is the object "Map" base. */
7323 lua_newtable(gL.T);
7324
7325 /* register pattern types. */
7326 for (i=0; i<PAT_MATCH_NUM; i++)
7327 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007328 for (i=0; i<PAT_MATCH_NUM; i++) {
7329 snprintf(trash.str, trash.size, "_%s", pat_match_names[i]);
7330 hlua_class_const_int(gL.T, trash.str, i);
7331 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007332
7333 /* register constructor. */
7334 hlua_class_function(gL.T, "new", hlua_map_new);
7335
7336 /* Create and fill the metatable. */
7337 lua_newtable(gL.T);
7338
7339 /* Create and fille the __index entry. */
7340 lua_pushstring(gL.T, "__index");
7341 lua_newtable(gL.T);
7342
7343 /* Register . */
7344 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7345 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7346
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007347 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007348
Thierry Fournier45e78d72016-02-19 18:34:46 +01007349 /* Register previous table in the registry with reference and named entry.
7350 * The function hlua_register_metatable() pops the stack, so we
7351 * previously create a copy of the table.
7352 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007353 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007354 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007355
7356 /* Assign the metatable to the mai Map object. */
7357 lua_setmetatable(gL.T, -2);
7358
7359 /* Set a name to the table. */
7360 lua_setglobal(gL.T, "Map");
7361
7362 /*
7363 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007364 * Register class Channel
7365 *
7366 */
7367
7368 /* Create and fill the metatable. */
7369 lua_newtable(gL.T);
7370
7371 /* Create and fille the __index entry. */
7372 lua_pushstring(gL.T, "__index");
7373 lua_newtable(gL.T);
7374
7375 /* Register . */
7376 hlua_class_function(gL.T, "get", hlua_channel_get);
7377 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7378 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7379 hlua_class_function(gL.T, "set", hlua_channel_set);
7380 hlua_class_function(gL.T, "append", hlua_channel_append);
7381 hlua_class_function(gL.T, "send", hlua_channel_send);
7382 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7383 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7384 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007385 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007386
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007387 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007388
7389 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007390 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007391
7392 /*
7393 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007394 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007395 *
7396 */
7397
7398 /* Create and fill the metatable. */
7399 lua_newtable(gL.T);
7400
7401 /* Create and fille the __index entry. */
7402 lua_pushstring(gL.T, "__index");
7403 lua_newtable(gL.T);
7404
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007405 /* Browse existing fetches and create the associated
7406 * object method.
7407 */
7408 sf = NULL;
7409 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7410
7411 /* Dont register the keywork if the arguments check function are
7412 * not safe during the runtime.
7413 */
7414 if ((sf->val_args != NULL) &&
7415 (sf->val_args != val_payload_lv) &&
7416 (sf->val_args != val_hdr))
7417 continue;
7418
7419 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7420 * by an underscore.
7421 */
7422 strncpy(trash.str, sf->kw, trash.size);
7423 trash.str[trash.size - 1] = '\0';
7424 for (p = trash.str; *p; p++)
7425 if (*p == '.' || *p == '-' || *p == '+')
7426 *p = '_';
7427
7428 /* Register the function. */
7429 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007430 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007431 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007432 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007433 }
7434
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007435 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007436
7437 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007438 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007439
7440 /*
7441 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007442 * Register class Converters
7443 *
7444 */
7445
7446 /* Create and fill the metatable. */
7447 lua_newtable(gL.T);
7448
7449 /* Create and fill the __index entry. */
7450 lua_pushstring(gL.T, "__index");
7451 lua_newtable(gL.T);
7452
7453 /* Browse existing converters and create the associated
7454 * object method.
7455 */
7456 sc = NULL;
7457 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7458 /* Dont register the keywork if the arguments check function are
7459 * not safe during the runtime.
7460 */
7461 if (sc->val_args != NULL)
7462 continue;
7463
7464 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7465 * by an underscore.
7466 */
7467 strncpy(trash.str, sc->kw, trash.size);
7468 trash.str[trash.size - 1] = '\0';
7469 for (p = trash.str; *p; p++)
7470 if (*p == '.' || *p == '-' || *p == '+')
7471 *p = '_';
7472
7473 /* Register the function. */
7474 lua_pushstring(gL.T, trash.str);
7475 lua_pushlightuserdata(gL.T, sc);
7476 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007477 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007478 }
7479
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007480 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007481
7482 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007483 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007484
7485 /*
7486 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007487 * Register class HTTP
7488 *
7489 */
7490
7491 /* Create and fill the metatable. */
7492 lua_newtable(gL.T);
7493
7494 /* Create and fille the __index entry. */
7495 lua_pushstring(gL.T, "__index");
7496 lua_newtable(gL.T);
7497
7498 /* Register Lua functions. */
7499 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7500 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7501 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7502 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7503 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7504 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7505 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7506 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7507 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7508 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7509
7510 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7511 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7512 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7513 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7514 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7515 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007516 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007517
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007518 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007519
7520 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007521 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007522
7523 /*
7524 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007525 * Register class AppletTCP
7526 *
7527 */
7528
7529 /* Create and fill the metatable. */
7530 lua_newtable(gL.T);
7531
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007532 /* Create and fille the __index entry. */
7533 lua_pushstring(gL.T, "__index");
7534 lua_newtable(gL.T);
7535
7536 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007537 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7538 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7539 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7540 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7541 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007542 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7543 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7544 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007545
7546 lua_settable(gL.T, -3);
7547
7548 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007549 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007550
7551 /*
7552 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007553 * Register class AppletHTTP
7554 *
7555 */
7556
7557 /* Create and fill the metatable. */
7558 lua_newtable(gL.T);
7559
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007560 /* Create and fille the __index entry. */
7561 lua_pushstring(gL.T, "__index");
7562 lua_newtable(gL.T);
7563
7564 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007565 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7566 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007567 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7568 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7569 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007570 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7571 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7572 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7573 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7574 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7575 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7576
7577 lua_settable(gL.T, -3);
7578
7579 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007580 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007581
7582 /*
7583 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007584 * Register class TXN
7585 *
7586 */
7587
7588 /* Create and fill the metatable. */
7589 lua_newtable(gL.T);
7590
7591 /* Create and fille the __index entry. */
7592 lua_pushstring(gL.T, "__index");
7593 lua_newtable(gL.T);
7594
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007595 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01007596 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7597 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007598 hlua_class_function(gL.T, "set_var", hlua_set_var);
Christopher Faulet85d79c92016-11-09 16:54:56 +01007599 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007600 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007601 hlua_class_function(gL.T, "done", hlua_txn_done);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007602 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
7603 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7604 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007605 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7606 hlua_class_function(gL.T, "log", hlua_txn_log);
7607 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7608 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7609 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7610 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007611
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007612 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007613
7614 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007615 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007616
7617 /*
7618 *
7619 * Register class Socket
7620 *
7621 */
7622
7623 /* Create and fill the metatable. */
7624 lua_newtable(gL.T);
7625
7626 /* Create and fille the __index entry. */
7627 lua_pushstring(gL.T, "__index");
7628 lua_newtable(gL.T);
7629
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007630#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007631 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007632#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007633 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7634 hlua_class_function(gL.T, "send", hlua_socket_send);
7635 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7636 hlua_class_function(gL.T, "close", hlua_socket_close);
7637 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7638 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7639 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7640 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7641
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007642 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007643
7644 /* Register the garbage collector entry. */
7645 lua_pushstring(gL.T, "__gc");
7646 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007647 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007648
7649 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007650 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007651
7652 /* Proxy and server configuration initialisation. */
7653 memset(&socket_proxy, 0, sizeof(socket_proxy));
7654 init_new_proxy(&socket_proxy);
7655 socket_proxy.parent = NULL;
7656 socket_proxy.last_change = now.tv_sec;
7657 socket_proxy.id = "LUA-SOCKET";
7658 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
7659 socket_proxy.maxconn = 0;
7660 socket_proxy.accept = NULL;
7661 socket_proxy.options2 |= PR_O2_INDEPSTR;
7662 socket_proxy.srv = NULL;
7663 socket_proxy.conn_retries = 0;
7664 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
7665
7666 /* Init TCP server: unchanged parameters */
7667 memset(&socket_tcp, 0, sizeof(socket_tcp));
7668 socket_tcp.next = NULL;
7669 socket_tcp.proxy = &socket_proxy;
7670 socket_tcp.obj_type = OBJ_TYPE_SERVER;
7671 LIST_INIT(&socket_tcp.actconns);
7672 LIST_INIT(&socket_tcp.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02007673 LIST_INIT(&socket_tcp.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02007674 LIST_INIT(&socket_tcp.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02007675 LIST_INIT(&socket_tcp.safe_conns);
Emeric Brun52a91d32017-08-31 14:41:55 +02007676 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007677 socket_tcp.last_change = 0;
7678 socket_tcp.id = "LUA-TCP-CONN";
7679 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7680 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7681 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
7682
7683 /* XXX: Copy default parameter from default server,
7684 * but the default server is not initialized.
7685 */
7686 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
7687 socket_tcp.minconn = socket_proxy.defsrv.minconn;
7688 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
7689 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
7690 socket_tcp.onerror = socket_proxy.defsrv.onerror;
7691 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
7692 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
7693 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
7694 socket_tcp.uweight = socket_proxy.defsrv.iweight;
7695 socket_tcp.iweight = socket_proxy.defsrv.iweight;
7696
7697 socket_tcp.check.status = HCHK_STATUS_INI;
7698 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
7699 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
7700 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
7701 socket_tcp.check.server = &socket_tcp;
7702
7703 socket_tcp.agent.status = HCHK_STATUS_INI;
7704 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
7705 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
7706 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
7707 socket_tcp.agent.server = &socket_tcp;
7708
Willy Tarreaua261e9b2016-12-22 20:44:00 +01007709 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007710
7711#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007712 /* Init TCP server: unchanged parameters */
7713 memset(&socket_ssl, 0, sizeof(socket_ssl));
7714 socket_ssl.next = NULL;
7715 socket_ssl.proxy = &socket_proxy;
7716 socket_ssl.obj_type = OBJ_TYPE_SERVER;
7717 LIST_INIT(&socket_ssl.actconns);
7718 LIST_INIT(&socket_ssl.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02007719 LIST_INIT(&socket_ssl.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02007720 LIST_INIT(&socket_ssl.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02007721 LIST_INIT(&socket_ssl.safe_conns);
Emeric Brun52a91d32017-08-31 14:41:55 +02007722 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007723 socket_ssl.last_change = 0;
7724 socket_ssl.id = "LUA-SSL-CONN";
7725 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7726 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7727 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
7728
7729 /* XXX: Copy default parameter from default server,
7730 * but the default server is not initialized.
7731 */
7732 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
7733 socket_ssl.minconn = socket_proxy.defsrv.minconn;
7734 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
7735 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
7736 socket_ssl.onerror = socket_proxy.defsrv.onerror;
7737 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
7738 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
7739 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
7740 socket_ssl.uweight = socket_proxy.defsrv.iweight;
7741 socket_ssl.iweight = socket_proxy.defsrv.iweight;
7742
7743 socket_ssl.check.status = HCHK_STATUS_INI;
7744 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
7745 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
7746 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
7747 socket_ssl.check.server = &socket_ssl;
7748
7749 socket_ssl.agent.status = HCHK_STATUS_INI;
7750 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
7751 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
7752 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
7753 socket_ssl.agent.server = &socket_ssl;
7754
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007755 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01007756 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007757
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007758 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007759 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
7760 /*
7761 *
7762 * If the keyword is not known, we can search in the registered
7763 * server keywords. This is usefull to configure special SSL
7764 * features like client certificates and ssl_verify.
7765 *
7766 */
7767 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
7768 if (tmp_error != 0) {
7769 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
7770 abort(); /* This must be never arrives because the command line
7771 not editable by the user. */
7772 }
7773 idx += kw->skip;
7774 }
7775 }
7776
7777 /* Initialize SSL server. */
Willy Tarreau17d45382016-12-22 21:16:08 +01007778 if (socket_ssl.xprt->prepare_srv)
7779 socket_ssl.xprt->prepare_srv(&socket_ssl);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007780#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01007781
7782 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007783}
Willy Tarreaubb57d942016-12-21 19:04:56 +01007784
7785__attribute__((constructor))
7786static void __hlua_init(void)
7787{
7788 char *ptr = NULL;
7789 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
7790 hap_register_build_opts(ptr, 1);
7791}