blob: 36b1b3ff4df4044b0473186d212b3f0c3d8f5899 [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 Tarreau06d80a92017-10-19 14:32:15 +02001665 nblk = co_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 Tarreau06d80a92017-10-19 14:32:15 +02001696 nblk = co_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 Tarreau06d80a92017-10-19 14:32:15 +02001705 nblk = co_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 Tarreau06d80a92017-10-19 14:32:15 +02001727 co_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;
Willy Tarreau06d80a92017-10-19 14:32:15 +02001907 len = ci_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 Tarreau06d80a92017-10-19 14:32:15 +02002554 ret = ci_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 Tarreau06d80a92017-10-19 14:32:15 +02002646 ret = ci_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 Tarreau06d80a92017-10-19 14:32:15 +02002704 ret = ci_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. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003414 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003415
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. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003437 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003438 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. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003469 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003470
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);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003495 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003496 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. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003514 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003515
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. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003578 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003579
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) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02003878 ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003879 /* 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. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003899 ret = co_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. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003930 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003931 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) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02003964 ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003965 /* 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. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003979 ret = co_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. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004010 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004011 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. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004073 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004074
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. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004212 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004213
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
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005363 /* If it is the first call to the task, we must initialize the
5364 * execution timeouts.
5365 */
5366 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005367 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005368
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005369 /* Execute the Lua code. */
5370 status = hlua_ctx_resume(hlua, 1);
5371
5372 switch (status) {
5373 /* finished or yield */
5374 case HLUA_E_OK:
5375 hlua_ctx_destroy(hlua);
5376 task_delete(task);
5377 task_free(task);
5378 break;
5379
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005380 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
5381 if (hlua->wake_time != TICK_ETERNITY)
Emeric Brun253e53e2017-10-17 18:58:40 +02005382 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005383 break;
5384
5385 /* finished with error. */
5386 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005387 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005388 hlua_ctx_destroy(hlua);
5389 task_delete(task);
5390 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005391 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005392 break;
5393
5394 case HLUA_E_ERR:
5395 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005396 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005397 hlua_ctx_destroy(hlua);
5398 task_delete(task);
5399 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005400 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005401 break;
5402 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005403 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005404}
5405
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005406/* This function is an LUA binding that register LUA function to be
5407 * executed after the HAProxy configuration parsing and before the
5408 * HAProxy scheduler starts. This function expect only one LUA
5409 * argument that is a function. This function returns nothing, but
5410 * throws if an error is encountered.
5411 */
5412__LJMP static int hlua_register_init(lua_State *L)
5413{
5414 struct hlua_init_function *init;
5415 int ref;
5416
5417 MAY_LJMP(check_args(L, 1, "register_init"));
5418
5419 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5420
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005421 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005422 if (!init)
5423 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5424
5425 init->function_ref = ref;
5426 LIST_ADDQ(&hlua_init_functions, &init->l);
5427 return 0;
5428}
5429
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005430/* This functio is an LUA binding. It permits to register a task
5431 * executed in parallel of the main HAroxy activity. The task is
5432 * created and it is set in the HAProxy scheduler. It can be called
5433 * from the "init" section, "post init" or during the runtime.
5434 *
5435 * Lua prototype:
5436 *
5437 * <none> core.register_task(<function>)
5438 */
5439static int hlua_register_task(lua_State *L)
5440{
5441 struct hlua *hlua;
5442 struct task *task;
5443 int ref;
5444
5445 MAY_LJMP(check_args(L, 1, "register_task"));
5446
5447 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5448
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005449 hlua = pool_alloc2(pool2_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005450 if (!hlua)
5451 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5452
Emeric Brunc60def82017-09-27 14:59:38 +02005453 task = task_new(MAX_THREADS_MASK);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005454 task->context = hlua;
5455 task->process = hlua_process_task;
5456
5457 if (!hlua_ctx_init(hlua, task))
5458 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5459
5460 /* Restore the function in the stack. */
5461 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5462 hlua->nargs = 0;
5463
5464 /* Schedule task. */
5465 task_schedule(task, now_ms);
5466
5467 return 0;
5468}
5469
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005470/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5471 * doesn't allow "yield" functions because the HAProxy engine cannot
5472 * resume converters.
5473 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005474static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005475{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005476 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005477 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005478 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005479
Willy Tarreaube508f12016-03-10 11:47:01 +01005480 if (!stream)
5481 return 0;
5482
Willy Tarreau87b09662015-04-03 00:22:06 +02005483 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005484 * Lua context can be not initialized. This behavior
5485 * permits to save performances because a systematic
5486 * Lua initialization cause 5% performances loss.
5487 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005488 if (!stream->hlua) {
5489 stream->hlua = pool_alloc2(pool2_hlua);
5490 if (!stream->hlua) {
5491 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5492 return 0;
5493 }
5494 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5495 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5496 return 0;
5497 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005498 }
5499
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005500 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005501 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005502
5503 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005504 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5505 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5506 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005507 else
5508 error = "critical error";
5509 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005510 return 0;
5511 }
5512
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005513 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005514 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005515 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005516 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005517 return 0;
5518 }
5519
5520 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005521 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005522
5523 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005524 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005525 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005526 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005527 return 0;
5528 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005529 hlua_smp2lua(stream->hlua->T, smp);
5530 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005531
5532 /* push keywords in the stack. */
5533 if (arg_p) {
5534 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005535 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005536 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005537 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005538 return 0;
5539 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005540 hlua_arg2lua(stream->hlua->T, arg_p);
5541 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005542 }
5543 }
5544
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005545 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005546 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005547
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005548 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005549 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005550 }
5551
5552 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005553 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005554 /* finished. */
5555 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005556 /* If the stack is empty, the function fails. */
5557 if (lua_gettop(stream->hlua->T) <= 0)
5558 return 0;
5559
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005560 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005561 hlua_lua2smp(stream->hlua->T, -1, smp);
5562 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005563 return 1;
5564
5565 /* yield. */
5566 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005567 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005568 return 0;
5569
5570 /* finished with error. */
5571 case HLUA_E_ERRMSG:
5572 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005573 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005574 fcn->name, lua_tostring(stream->hlua->T, -1));
5575 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005576 return 0;
5577
5578 case HLUA_E_ERR:
5579 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005580 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005581
5582 default:
5583 return 0;
5584 }
5585}
5586
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005587/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5588 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005589 * resume sample-fetches. This function will be called by the sample
5590 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005591 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005592static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5593 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005594{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005595 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005596 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005597 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005598 const struct chunk msg = { .len = 0 };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005599
Willy Tarreaube508f12016-03-10 11:47:01 +01005600 if (!stream)
5601 return 0;
5602
Willy Tarreau87b09662015-04-03 00:22:06 +02005603 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005604 * Lua context can be not initialized. This behavior
5605 * permits to save performances because a systematic
5606 * Lua initialization cause 5% performances loss.
5607 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005608 if (!stream->hlua) {
5609 stream->hlua = pool_alloc2(pool2_hlua);
5610 if (!stream->hlua) {
5611 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5612 return 0;
5613 }
5614 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5615 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5616 return 0;
5617 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005618 }
5619
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005620 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005621
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005622 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005623 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005624
5625 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005626 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5627 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5628 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005629 else
5630 error = "critical error";
5631 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005632 return 0;
5633 }
5634
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005635 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005636 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005637 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005638 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005639 return 0;
5640 }
5641
5642 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005643 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005644
5645 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005646 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005647 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005648 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005649 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005650 return 0;
5651 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005652 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005653
5654 /* push keywords in the stack. */
5655 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5656 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005657 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005658 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005659 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005660 return 0;
5661 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005662 hlua_arg2lua(stream->hlua->T, arg_p);
5663 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005664 }
5665
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005666 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005667 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005668
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005669 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005670 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005671 }
5672
5673 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005674 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005675 /* finished. */
5676 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005677 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005678 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005679 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005680 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005681 /* If the stack is empty, the function fails. */
5682 if (lua_gettop(stream->hlua->T) <= 0)
5683 return 0;
5684
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005685 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005686 hlua_lua2smp(stream->hlua->T, -1, smp);
5687 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005688
5689 /* Set the end of execution flag. */
5690 smp->flags &= ~SMP_F_MAY_CHANGE;
5691 return 1;
5692
5693 /* yield. */
5694 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005695 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005696 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005697 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005698 return 0;
5699
5700 /* finished with error. */
5701 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005702 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005703 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005704 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005705 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005706 fcn->name, lua_tostring(stream->hlua->T, -1));
5707 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005708 return 0;
5709
5710 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005711 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005712 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005713 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005714 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005715
5716 default:
5717 return 0;
5718 }
5719}
5720
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005721/* This function is an LUA binding used for registering
5722 * "sample-conv" functions. It expects a converter name used
5723 * in the haproxy configuration file, and an LUA function.
5724 */
5725__LJMP static int hlua_register_converters(lua_State *L)
5726{
5727 struct sample_conv_kw_list *sck;
5728 const char *name;
5729 int ref;
5730 int len;
5731 struct hlua_function *fcn;
5732
5733 MAY_LJMP(check_args(L, 2, "register_converters"));
5734
5735 /* First argument : converter name. */
5736 name = MAY_LJMP(luaL_checkstring(L, 1));
5737
5738 /* Second argument : lua function. */
5739 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5740
5741 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005742 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005743 if (!sck)
5744 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005745 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005746 if (!fcn)
5747 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5748
5749 /* Fill fcn. */
5750 fcn->name = strdup(name);
5751 if (!fcn->name)
5752 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5753 fcn->function_ref = ref;
5754
5755 /* List head */
5756 sck->list.n = sck->list.p = NULL;
5757
5758 /* converter keyword. */
5759 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005760 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005761 if (!sck->kw[0].kw)
5762 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5763
5764 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
5765 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01005766 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 +01005767 sck->kw[0].val_args = NULL;
5768 sck->kw[0].in_type = SMP_T_STR;
5769 sck->kw[0].out_type = SMP_T_STR;
5770 sck->kw[0].private = fcn;
5771
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005772 /* Register this new converter */
5773 sample_register_convs(sck);
5774
5775 return 0;
5776}
5777
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005778/* This fucntion is an LUA binding used for registering
5779 * "sample-fetch" functions. It expects a converter name used
5780 * in the haproxy configuration file, and an LUA function.
5781 */
5782__LJMP static int hlua_register_fetches(lua_State *L)
5783{
5784 const char *name;
5785 int ref;
5786 int len;
5787 struct sample_fetch_kw_list *sfk;
5788 struct hlua_function *fcn;
5789
5790 MAY_LJMP(check_args(L, 2, "register_fetches"));
5791
5792 /* First argument : sample-fetch name. */
5793 name = MAY_LJMP(luaL_checkstring(L, 1));
5794
5795 /* Second argument : lua function. */
5796 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5797
5798 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005799 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005800 if (!sfk)
5801 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005802 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005803 if (!fcn)
5804 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5805
5806 /* Fill fcn. */
5807 fcn->name = strdup(name);
5808 if (!fcn->name)
5809 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5810 fcn->function_ref = ref;
5811
5812 /* List head */
5813 sfk->list.n = sfk->list.p = NULL;
5814
5815 /* sample-fetch keyword. */
5816 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005817 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005818 if (!sfk->kw[0].kw)
5819 return luaL_error(L, "lua out of memory error.");
5820
5821 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
5822 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01005823 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 +01005824 sfk->kw[0].val_args = NULL;
5825 sfk->kw[0].out_type = SMP_T_STR;
5826 sfk->kw[0].use = SMP_USE_HTTP_ANY;
5827 sfk->kw[0].val = 0;
5828 sfk->kw[0].private = fcn;
5829
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005830 /* Register this new fetch. */
5831 sample_register_fetches(sfk);
5832
5833 return 0;
5834}
5835
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005836/* This function is a wrapper to execute each LUA function declared
5837 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005838 * return ACT_RET_CONT if the processing is finished (with or without
5839 * error) and return ACT_RET_YIELD if the function must be called again
5840 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005841 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005842static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02005843 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005844{
5845 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005846 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005847 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005848 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005849 const struct chunk msg = { .len = 0 };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005850
5851 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01005852 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
5853 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
5854 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
5855 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005856 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005857 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005858 return ACT_RET_CONT;
5859 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005860
Willy Tarreau87b09662015-04-03 00:22:06 +02005861 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005862 * Lua context can be not initialized. This behavior
5863 * permits to save performances because a systematic
5864 * Lua initialization cause 5% performances loss.
5865 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005866 if (!s->hlua) {
5867 s->hlua = pool_alloc2(pool2_hlua);
5868 if (!s->hlua) {
5869 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
5870 rule->arg.hlua_rule->fcn.name);
5871 return ACT_RET_CONT;
5872 }
5873 if (!hlua_ctx_init(s->hlua, s->task)) {
5874 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
5875 rule->arg.hlua_rule->fcn.name);
5876 return ACT_RET_CONT;
5877 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005878 }
5879
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005880 consistency_set(s, dir, &s->hlua->cons);
5881
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005882 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005883 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005884
5885 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005886 if (!SET_SAFE_LJMP(s->hlua->T)) {
5887 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
5888 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005889 else
5890 error = "critical error";
5891 SEND_ERR(px, "Lua function '%s': %s.\n",
5892 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005893 return ACT_RET_CONT;
5894 }
5895
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005896 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005897 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005898 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005899 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005900 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005901 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005902 }
5903
5904 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005905 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005906
Willy Tarreau87b09662015-04-03 00:22:06 +02005907 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005908 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005909 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005910 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005911 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005912 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005913 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005914 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005915
5916 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005917 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005918 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005919 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005920 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005921 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005922 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005923 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005924 lua_pushstring(s->hlua->T, *arg);
5925 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005926 }
5927
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005928 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005929 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005930
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005931 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005932 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005933 }
5934
5935 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005936 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005937 /* finished. */
5938 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005939 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005940 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005941 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005942 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005943 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005944 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005945 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005946
5947 /* yield. */
5948 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005949 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005950 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005951 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005952 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005953 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005954 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005955 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005956 /* Some actions can be wake up when a "write" event
5957 * is detected on a response channel. This is useful
5958 * only for actions targetted on the requests.
5959 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005960 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005961 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01005962 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005963 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005964 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005965 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005966 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005967 /* We can quit the fcuntion without consistency check
5968 * because HAProxy is not able to manipulate data, it
5969 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005970 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005971
5972 /* finished with error. */
5973 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005974 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005975 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005976 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005977 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005978 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005979 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005980 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
5981 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005982 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005983
5984 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005985 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005986 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005987 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005988 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005989 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005990 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005991 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005992
5993 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005994 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005995 }
5996}
5997
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005998struct task *hlua_applet_wakeup(struct task *t)
5999{
6000 struct appctx *ctx = t->context;
6001 struct stream_interface *si = ctx->owner;
6002
6003 /* If the applet is wake up without any expected work, the sheduler
6004 * remove it from the run queue. This flag indicate that the applet
6005 * is waiting for write. If the buffer is full, the main processing
6006 * will send some data and after call the applet, otherwise it call
6007 * the applet ASAP.
6008 */
6009 si_applet_cant_put(si);
6010 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006011 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006012 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006013}
6014
6015static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6016{
6017 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006018 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006019 struct task *task;
6020 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006021 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006022
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006023 hlua = pool_alloc2(pool2_hlua);
6024 if (!hlua) {
6025 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6026 ctx->rule->arg.hlua_rule->fcn.name);
6027 return 0;
6028 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006029 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006030 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006031 ctx->ctx.hlua_apptcp.flags = 0;
6032
6033 /* Create task used by signal to wakeup applets. */
Emeric Brunc60def82017-09-27 14:59:38 +02006034 task = task_new(MAX_THREADS_MASK);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006035 if (!task) {
6036 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6037 ctx->rule->arg.hlua_rule->fcn.name);
6038 return 0;
6039 }
6040 task->nice = 0;
6041 task->context = ctx;
6042 task->process = hlua_applet_wakeup;
6043 ctx->ctx.hlua_apptcp.task = task;
6044
6045 /* In the execution wrappers linked with a stream, the
6046 * Lua context can be not initialized. This behavior
6047 * permits to save performances because a systematic
6048 * Lua initialization cause 5% performances loss.
6049 */
6050 if (!hlua_ctx_init(hlua, task)) {
6051 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6052 ctx->rule->arg.hlua_rule->fcn.name);
6053 return 0;
6054 }
6055
6056 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006057 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006058
6059 /* The following Lua calls can fail. */
6060 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006061 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6062 error = lua_tostring(hlua->T, -1);
6063 else
6064 error = "critical error";
6065 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6066 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006067 RESET_SAFE_LJMP(hlua->T);
6068 return 0;
6069 }
6070
6071 /* Check stack available size. */
6072 if (!lua_checkstack(hlua->T, 1)) {
6073 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6074 ctx->rule->arg.hlua_rule->fcn.name);
6075 RESET_SAFE_LJMP(hlua->T);
6076 return 0;
6077 }
6078
6079 /* Restore the function in the stack. */
6080 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6081
6082 /* Create and and push object stream in the stack. */
6083 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6084 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6085 ctx->rule->arg.hlua_rule->fcn.name);
6086 RESET_SAFE_LJMP(hlua->T);
6087 return 0;
6088 }
6089 hlua->nargs = 1;
6090
6091 /* push keywords in the stack. */
6092 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6093 if (!lua_checkstack(hlua->T, 1)) {
6094 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6095 ctx->rule->arg.hlua_rule->fcn.name);
6096 RESET_SAFE_LJMP(hlua->T);
6097 return 0;
6098 }
6099 lua_pushstring(hlua->T, *arg);
6100 hlua->nargs++;
6101 }
6102
6103 RESET_SAFE_LJMP(hlua->T);
6104
6105 /* Wakeup the applet ASAP. */
6106 si_applet_cant_get(si);
6107 si_applet_cant_put(si);
6108
6109 return 1;
6110}
6111
6112static void hlua_applet_tcp_fct(struct appctx *ctx)
6113{
6114 struct stream_interface *si = ctx->owner;
6115 struct stream *strm = si_strm(si);
6116 struct channel *res = si_ic(si);
6117 struct act_rule *rule = ctx->rule;
6118 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006119 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006120
6121 /* The applet execution is already done. */
6122 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE)
6123 return;
6124
6125 /* If the stream is disconnect or closed, ldo nothing. */
6126 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6127 return;
6128
6129 /* Execute the function. */
6130 switch (hlua_ctx_resume(hlua, 1)) {
6131 /* finished. */
6132 case HLUA_E_OK:
6133 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6134
6135 /* log time */
6136 strm->logs.tv_request = now;
6137
6138 /* eat the whole request */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006139 co_skip(si_oc(si), si_ob(si)->o);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006140 res->flags |= CF_READ_NULL;
6141 si_shutr(si);
6142 return;
6143
6144 /* yield. */
6145 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006146 if (hlua->wake_time != TICK_ETERNITY)
6147 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006148 return;
6149
6150 /* finished with error. */
6151 case HLUA_E_ERRMSG:
6152 /* Display log. */
6153 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6154 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6155 lua_pop(hlua->T, 1);
6156 goto error;
6157
6158 case HLUA_E_ERR:
6159 /* Display log. */
6160 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6161 rule->arg.hlua_rule->fcn.name);
6162 goto error;
6163
6164 default:
6165 goto error;
6166 }
6167
6168error:
6169
6170 /* For all other cases, just close the stream. */
6171 si_shutw(si);
6172 si_shutr(si);
6173 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6174}
6175
6176static void hlua_applet_tcp_release(struct appctx *ctx)
6177{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006178 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006179 task_free(ctx->ctx.hlua_apptcp.task);
6180 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006181 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006182 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006183}
6184
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006185/* The function returns 1 if the initialisation is complete, 0 if
6186 * an errors occurs and -1 if more data are required for initializing
6187 * the applet.
6188 */
6189static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6190{
6191 struct stream_interface *si = ctx->owner;
6192 struct channel *req = si_oc(si);
6193 struct http_msg *msg;
6194 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006195 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006196 char **arg;
6197 struct hdr_ctx hdr;
6198 struct task *task;
6199 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006200 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006201
6202 /* Wait for a full HTTP request. */
6203 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6204 if (smp.flags & SMP_F_MAY_CHANGE)
6205 return -1;
6206 return 0;
6207 }
6208 txn = strm->txn;
6209 msg = &txn->req;
6210
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006211 /* We want two things in HTTP mode :
6212 * - enforce server-close mode if we were in keep-alive, so that the
6213 * applet is released after each response ;
6214 * - enable request body transfer to the applet in order to resync
6215 * with the response body.
6216 */
6217 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6218 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006219
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006220 hlua = pool_alloc2(pool2_hlua);
6221 if (!hlua) {
6222 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6223 ctx->rule->arg.hlua_rule->fcn.name);
6224 return 0;
6225 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006226 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006227 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006228 ctx->ctx.hlua_apphttp.left_bytes = -1;
6229 ctx->ctx.hlua_apphttp.flags = 0;
6230
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006231 if (txn->req.flags & HTTP_MSGF_VER_11)
6232 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6233
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006234 /* Create task used by signal to wakeup applets. */
Emeric Brunc60def82017-09-27 14:59:38 +02006235 task = task_new(MAX_THREADS_MASK);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006236 if (!task) {
6237 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6238 ctx->rule->arg.hlua_rule->fcn.name);
6239 return 0;
6240 }
6241 task->nice = 0;
6242 task->context = ctx;
6243 task->process = hlua_applet_wakeup;
6244 ctx->ctx.hlua_apphttp.task = task;
6245
6246 /* In the execution wrappers linked with a stream, the
6247 * Lua context can be not initialized. This behavior
6248 * permits to save performances because a systematic
6249 * Lua initialization cause 5% performances loss.
6250 */
6251 if (!hlua_ctx_init(hlua, task)) {
6252 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6253 ctx->rule->arg.hlua_rule->fcn.name);
6254 return 0;
6255 }
6256
6257 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006258 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006259
6260 /* The following Lua calls can fail. */
6261 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006262 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6263 error = lua_tostring(hlua->T, -1);
6264 else
6265 error = "critical error";
6266 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6267 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006268 return 0;
6269 }
6270
6271 /* Check stack available size. */
6272 if (!lua_checkstack(hlua->T, 1)) {
6273 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6274 ctx->rule->arg.hlua_rule->fcn.name);
6275 RESET_SAFE_LJMP(hlua->T);
6276 return 0;
6277 }
6278
6279 /* Restore the function in the stack. */
6280 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6281
6282 /* Create and and push object stream in the stack. */
6283 if (!hlua_applet_http_new(hlua->T, ctx)) {
6284 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6285 ctx->rule->arg.hlua_rule->fcn.name);
6286 RESET_SAFE_LJMP(hlua->T);
6287 return 0;
6288 }
6289 hlua->nargs = 1;
6290
6291 /* Look for a 100-continue expected. */
6292 if (msg->flags & HTTP_MSGF_VER_11) {
6293 hdr.idx = 0;
6294 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) &&
6295 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6296 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6297 }
6298
6299 /* push keywords in the stack. */
6300 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6301 if (!lua_checkstack(hlua->T, 1)) {
6302 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6303 ctx->rule->arg.hlua_rule->fcn.name);
6304 RESET_SAFE_LJMP(hlua->T);
6305 return 0;
6306 }
6307 lua_pushstring(hlua->T, *arg);
6308 hlua->nargs++;
6309 }
6310
6311 RESET_SAFE_LJMP(hlua->T);
6312
6313 /* Wakeup the applet when data is ready for read. */
6314 si_applet_cant_get(si);
6315
6316 return 1;
6317}
6318
6319static void hlua_applet_http_fct(struct appctx *ctx)
6320{
6321 struct stream_interface *si = ctx->owner;
6322 struct stream *strm = si_strm(si);
6323 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006324 struct act_rule *rule = ctx->rule;
6325 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006326 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006327 char *blk1;
6328 int len1;
6329 char *blk2;
6330 int len2;
6331 int ret;
6332
6333 /* If the stream is disconnect or closed, ldo nothing. */
6334 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6335 return;
6336
6337 /* Set the currently running flag. */
6338 if (!HLUA_IS_RUNNING(hlua) &&
6339 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6340
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006341 /* Wait for full HTTP analysys. */
6342 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
6343 si_applet_cant_get(si);
6344 return;
6345 }
6346
6347 /* Store the max amount of bytes that we can read. */
6348 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6349
6350 /* We need to flush the request header. This left the body
6351 * for the Lua.
6352 */
6353
6354 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006355 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006356 if (ret == -1)
6357 return;
6358
6359 /* No data available, ask for more data. */
6360 if (ret == 1)
6361 len2 = 0;
6362 if (ret == 0)
6363 len1 = 0;
6364 if (len1 + len2 < strm->txn->req.eoh + 2) {
6365 si_applet_cant_get(si);
6366 return;
6367 }
6368
6369 /* skip the requests bytes. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006370 co_skip(si_oc(si), strm->txn->req.eoh + 2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006371 }
6372
6373 /* Executes The applet if it is not done. */
6374 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6375
6376 /* Execute the function. */
6377 switch (hlua_ctx_resume(hlua, 1)) {
6378 /* finished. */
6379 case HLUA_E_OK:
6380 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6381 break;
6382
6383 /* yield. */
6384 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006385 if (hlua->wake_time != TICK_ETERNITY)
6386 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006387 return;
6388
6389 /* finished with error. */
6390 case HLUA_E_ERRMSG:
6391 /* Display log. */
6392 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6393 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6394 lua_pop(hlua->T, 1);
6395 goto error;
6396
6397 case HLUA_E_ERR:
6398 /* Display log. */
6399 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6400 rule->arg.hlua_rule->fcn.name);
6401 goto error;
6402
6403 default:
6404 goto error;
6405 }
6406 }
6407
6408 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6409
6410 /* We must send the final chunk. */
6411 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6412 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6413
6414 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006415 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006416
6417 /* critical error. */
6418 if (ret == -2 || ret == -3) {
6419 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6420 rule->arg.hlua_rule->fcn.name);
6421 goto error;
6422 }
6423
6424 /* no enough space error. */
6425 if (ret == -1) {
6426 si_applet_cant_put(si);
6427 return;
6428 }
6429
6430 /* set the last chunk sent. */
6431 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6432 }
6433
6434 /* close the connection. */
6435
6436 /* status / log */
6437 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6438 strm->logs.tv_request = now;
6439
6440 /* eat the whole request */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006441 co_skip(si_oc(si), si_ob(si)->o);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006442 res->flags |= CF_READ_NULL;
6443 si_shutr(si);
6444
6445 return;
6446 }
6447
6448error:
6449
6450 /* If we are in HTTP mode, and we are not send any
6451 * data, return a 500 server error in best effort:
6452 * if there are no room avalaible in the buffer,
6453 * just close the connection.
6454 */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006455 ci_putblk(res, error_500, strlen(error_500));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006456 if (!(strm->flags & SF_ERR_MASK))
6457 strm->flags |= SF_ERR_RESOURCE;
6458 si_shutw(si);
6459 si_shutr(si);
6460 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6461}
6462
6463static void hlua_applet_http_release(struct appctx *ctx)
6464{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006465 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006466 task_free(ctx->ctx.hlua_apphttp.task);
6467 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006468 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006469 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006470}
6471
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006472/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6473 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006474 *
6475 * This function can fail with an abort() due to an Lua critical error.
6476 * We are in the configuration parsing process of HAProxy, this abort() is
6477 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006478 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006479static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6480 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006481{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006482 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006483 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006484
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006485 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006486 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006487 if (!rule->arg.hlua_rule) {
6488 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006489 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006490 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006491
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006492 /* Memory for arguments. */
6493 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6494 if (!rule->arg.hlua_rule->args) {
6495 memprintf(err, "out of memory error");
6496 return ACT_RET_PRS_ERR;
6497 }
6498
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006499 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006500 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006501
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006502 /* Expect some arguments */
6503 for (i = 0; i < fcn->nargs; i++) {
6504 if (*args[i+1] == '\0') {
6505 memprintf(err, "expect %d arguments", fcn->nargs);
6506 return ACT_RET_PRS_ERR;
6507 }
6508 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6509 if (!rule->arg.hlua_rule->args[i]) {
6510 memprintf(err, "out of memory error");
6511 return ACT_RET_PRS_ERR;
6512 }
6513 (*cur_arg)++;
6514 }
6515 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006516
Thierry FOURNIER42148732015-09-02 17:17:33 +02006517 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006518 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006519 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006520}
6521
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006522static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6523 struct act_rule *rule, char **err)
6524{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006525 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006526
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006527 /* HTTP applets are forbidden in tcp-request rules.
6528 * HTTP applet request requires everything initilized by
6529 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6530 * The applet will be immediately initilized, but its before
6531 * the call of this analyzer.
6532 */
6533 if (rule->from != ACT_F_HTTP_REQ) {
6534 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6535 return ACT_RET_PRS_ERR;
6536 }
6537
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006538 /* Memory for the rule. */
6539 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6540 if (!rule->arg.hlua_rule) {
6541 memprintf(err, "out of memory error");
6542 return ACT_RET_PRS_ERR;
6543 }
6544
6545 /* Reference the Lua function and store the reference. */
6546 rule->arg.hlua_rule->fcn = *fcn;
6547
6548 /* TODO: later accept arguments. */
6549 rule->arg.hlua_rule->args = NULL;
6550
6551 /* Add applet pointer in the rule. */
6552 rule->applet.obj_type = OBJ_TYPE_APPLET;
6553 rule->applet.name = fcn->name;
6554 rule->applet.init = hlua_applet_http_init;
6555 rule->applet.fct = hlua_applet_http_fct;
6556 rule->applet.release = hlua_applet_http_release;
6557 rule->applet.timeout = hlua_timeout_applet;
6558
6559 return ACT_RET_PRS_OK;
6560}
6561
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006562/* This function is an LUA binding used for registering
6563 * "sample-conv" functions. It expects a converter name used
6564 * in the haproxy configuration file, and an LUA function.
6565 */
6566__LJMP static int hlua_register_action(lua_State *L)
6567{
6568 struct action_kw_list *akl;
6569 const char *name;
6570 int ref;
6571 int len;
6572 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006573 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006574
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006575 /* Initialise the number of expected arguments at 0. */
6576 nargs = 0;
6577
6578 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6579 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006580
6581 /* First argument : converter name. */
6582 name = MAY_LJMP(luaL_checkstring(L, 1));
6583
6584 /* Second argument : environment. */
6585 if (lua_type(L, 2) != LUA_TTABLE)
6586 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6587
6588 /* Third argument : lua function. */
6589 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6590
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006591 /* Fouth argument : number of mandatories arguments expected on the configuration line. */
6592 if (lua_gettop(L) >= 4)
6593 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6594
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006595 /* browse the second argulent as an array. */
6596 lua_pushnil(L);
6597 while (lua_next(L, 2) != 0) {
6598 if (lua_type(L, -1) != LUA_TSTRING)
6599 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6600
6601 /* Check required environment. Only accepted "http" or "tcp". */
6602 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006603 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006604 if (!akl)
6605 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006606 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006607 if (!fcn)
6608 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6609
6610 /* Fill fcn. */
6611 fcn->name = strdup(name);
6612 if (!fcn->name)
6613 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6614 fcn->function_ref = ref;
6615
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006616 /* Set the expected number od arguments. */
6617 fcn->nargs = nargs;
6618
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006619 /* List head */
6620 akl->list.n = akl->list.p = NULL;
6621
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006622 /* action keyword. */
6623 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006624 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006625 if (!akl->kw[0].kw)
6626 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6627
6628 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6629
6630 akl->kw[0].match_pfx = 0;
6631 akl->kw[0].private = fcn;
6632 akl->kw[0].parse = action_register_lua;
6633
6634 /* select the action registering point. */
6635 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6636 tcp_req_cont_keywords_register(akl);
6637 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6638 tcp_res_cont_keywords_register(akl);
6639 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6640 http_req_keywords_register(akl);
6641 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6642 http_res_keywords_register(akl);
6643 else
6644 WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. "
6645 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6646 "are expected.", lua_tostring(L, -1)));
6647
6648 /* pop the environment string. */
6649 lua_pop(L, 1);
6650 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006651 return ACT_RET_PRS_OK;
6652}
6653
6654static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6655 struct act_rule *rule, char **err)
6656{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006657 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006658
6659 /* Memory for the rule. */
6660 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6661 if (!rule->arg.hlua_rule) {
6662 memprintf(err, "out of memory error");
6663 return ACT_RET_PRS_ERR;
6664 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006665
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006666 /* Reference the Lua function and store the reference. */
6667 rule->arg.hlua_rule->fcn = *fcn;
6668
6669 /* TODO: later accept arguments. */
6670 rule->arg.hlua_rule->args = NULL;
6671
6672 /* Add applet pointer in the rule. */
6673 rule->applet.obj_type = OBJ_TYPE_APPLET;
6674 rule->applet.name = fcn->name;
6675 rule->applet.init = hlua_applet_tcp_init;
6676 rule->applet.fct = hlua_applet_tcp_fct;
6677 rule->applet.release = hlua_applet_tcp_release;
6678 rule->applet.timeout = hlua_timeout_applet;
6679
6680 return 0;
6681}
6682
6683/* This function is an LUA binding used for registering
6684 * "sample-conv" functions. It expects a converter name used
6685 * in the haproxy configuration file, and an LUA function.
6686 */
6687__LJMP static int hlua_register_service(lua_State *L)
6688{
6689 struct action_kw_list *akl;
6690 const char *name;
6691 const char *env;
6692 int ref;
6693 int len;
6694 struct hlua_function *fcn;
6695
6696 MAY_LJMP(check_args(L, 3, "register_service"));
6697
6698 /* First argument : converter name. */
6699 name = MAY_LJMP(luaL_checkstring(L, 1));
6700
6701 /* Second argument : environment. */
6702 env = MAY_LJMP(luaL_checkstring(L, 2));
6703
6704 /* Third argument : lua function. */
6705 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6706
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006707 /* Allocate and fill the sample fetch keyword struct. */
6708 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
6709 if (!akl)
6710 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6711 fcn = calloc(1, sizeof(*fcn));
6712 if (!fcn)
6713 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6714
6715 /* Fill fcn. */
6716 len = strlen("<lua.>") + strlen(name) + 1;
6717 fcn->name = calloc(1, len);
6718 if (!fcn->name)
6719 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6720 snprintf((char *)fcn->name, len, "<lua.%s>", name);
6721 fcn->function_ref = ref;
6722
6723 /* List head */
6724 akl->list.n = akl->list.p = NULL;
6725
6726 /* converter keyword. */
6727 len = strlen("lua.") + strlen(name) + 1;
6728 akl->kw[0].kw = calloc(1, len);
6729 if (!akl->kw[0].kw)
6730 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6731
6732 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6733
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01006734 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006735 if (strcmp(env, "tcp") == 0)
6736 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006737 else if (strcmp(env, "http") == 0)
6738 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006739 else
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006740 WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. "
6741 "'tcp' or 'http' are expected."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006742
6743 akl->kw[0].match_pfx = 0;
6744 akl->kw[0].private = fcn;
6745
6746 /* End of array. */
6747 memset(&akl->kw[1], 0, sizeof(*akl->kw));
6748
6749 /* Register this new converter */
6750 service_keywords_register(akl);
6751
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006752 return 0;
6753}
6754
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006755/* This function initialises Lua cli handler. It copies the
6756 * arguments in the Lua stack and create channel IO objects.
6757 */
6758static int hlua_cli_parse_fct(char **args, struct appctx *appctx, void *private)
6759{
6760 struct hlua *hlua;
6761 struct hlua_function *fcn;
6762 int i;
6763 const char *error;
6764
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006765 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006766 appctx->ctx.hlua_cli.fcn = private;
6767
6768 hlua = pool_alloc2(pool2_hlua);
6769 if (!hlua) {
6770 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006771 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006772 }
6773 HLUA_INIT(hlua);
6774 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006775
6776 /* Create task used by signal to wakeup applets.
6777 * We use the same wakeup fonction than the Lua applet_tcp and
6778 * applet_http. It is absolutely compatible.
6779 */
Emeric Brunc60def82017-09-27 14:59:38 +02006780 appctx->ctx.hlua_cli.task = task_new(MAX_THREADS_MASK);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006781 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01006782 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006783 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006784 }
6785 appctx->ctx.hlua_cli.task->nice = 0;
6786 appctx->ctx.hlua_cli.task->context = appctx;
6787 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
6788
6789 /* Initialises the Lua context */
6790 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
6791 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006792 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006793 }
6794
6795 /* The following Lua calls can fail. */
6796 if (!SET_SAFE_LJMP(hlua->T)) {
6797 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6798 error = lua_tostring(hlua->T, -1);
6799 else
6800 error = "critical error";
6801 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
6802 goto error;
6803 }
6804
6805 /* Check stack available size. */
6806 if (!lua_checkstack(hlua->T, 2)) {
6807 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6808 goto error;
6809 }
6810
6811 /* Restore the function in the stack. */
6812 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
6813
6814 /* Once the arguments parsed, the CLI is like an AppletTCP,
6815 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006816 */
6817 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
6818 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6819 goto error;
6820 }
6821 hlua->nargs = 1;
6822
6823 /* push keywords in the stack. */
6824 for (i = 0; *args[i]; i++) {
6825 /* Check stack available size. */
6826 if (!lua_checkstack(hlua->T, 1)) {
6827 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6828 goto error;
6829 }
6830 lua_pushstring(hlua->T, args[i]);
6831 hlua->nargs++;
6832 }
6833
6834 /* We must initialize the execution timeouts. */
6835 hlua->max_time = hlua_timeout_session;
6836
6837 /* At this point the execution is safe. */
6838 RESET_SAFE_LJMP(hlua->T);
6839
6840 /* It's ok */
6841 return 0;
6842
6843 /* It's not ok. */
6844error:
6845 RESET_SAFE_LJMP(hlua->T);
6846 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006847 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006848 return 1;
6849}
6850
6851static int hlua_cli_io_handler_fct(struct appctx *appctx)
6852{
6853 struct hlua *hlua;
6854 struct stream_interface *si;
6855 struct hlua_function *fcn;
6856
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006857 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006858 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01006859 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006860
6861 /* If the stream is disconnect or closed, ldo nothing. */
6862 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6863 return 1;
6864
6865 /* Execute the function. */
6866 switch (hlua_ctx_resume(hlua, 1)) {
6867
6868 /* finished. */
6869 case HLUA_E_OK:
6870 return 1;
6871
6872 /* yield. */
6873 case HLUA_E_AGAIN:
6874 /* We want write. */
6875 if (HLUA_IS_WAKERESWR(hlua))
6876 si_applet_cant_put(si);
6877 /* Set the timeout. */
6878 if (hlua->wake_time != TICK_ETERNITY)
6879 task_schedule(hlua->task, hlua->wake_time);
6880 return 0;
6881
6882 /* finished with error. */
6883 case HLUA_E_ERRMSG:
6884 /* Display log. */
6885 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
6886 fcn->name, lua_tostring(hlua->T, -1));
6887 lua_pop(hlua->T, 1);
6888 return 1;
6889
6890 case HLUA_E_ERR:
6891 /* Display log. */
6892 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
6893 fcn->name);
6894 return 1;
6895
6896 default:
6897 return 1;
6898 }
6899
6900 return 1;
6901}
6902
6903static void hlua_cli_io_release_fct(struct appctx *appctx)
6904{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006905 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006906 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006907}
6908
6909/* This function is an LUA binding used for registering
6910 * new keywords in the cli. It expects a list of keywords
6911 * which are the "path". It is limited to 5 keywords. A
6912 * description of the command, a function to be executed
6913 * for the parsing and a function for io handlers.
6914 */
6915__LJMP static int hlua_register_cli(lua_State *L)
6916{
6917 struct cli_kw_list *cli_kws;
6918 const char *message;
6919 int ref_io;
6920 int len;
6921 struct hlua_function *fcn;
6922 int index;
6923 int i;
6924
6925 MAY_LJMP(check_args(L, 3, "register_cli"));
6926
6927 /* First argument : an array of maximum 5 keywords. */
6928 if (!lua_istable(L, 1))
6929 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
6930
6931 /* Second argument : string with contextual message. */
6932 message = MAY_LJMP(luaL_checkstring(L, 2));
6933
6934 /* Third and fourth argument : lua function. */
6935 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
6936
6937 /* Allocate and fill the sample fetch keyword struct. */
6938 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
6939 if (!cli_kws)
6940 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6941 fcn = calloc(1, sizeof(*fcn));
6942 if (!fcn)
6943 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6944
6945 /* Fill path. */
6946 index = 0;
6947 lua_pushnil(L);
6948 while(lua_next(L, 1) != 0) {
6949 if (index >= 5)
6950 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
6951 if (lua_type(L, -1) != LUA_TSTRING)
6952 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
6953 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
6954 if (!cli_kws->kw[0].str_kw[index])
6955 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6956 index++;
6957 lua_pop(L, 1);
6958 }
6959
6960 /* Copy help message. */
6961 cli_kws->kw[0].usage = strdup(message);
6962 if (!cli_kws->kw[0].usage)
6963 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6964
6965 /* Fill fcn io handler. */
6966 len = strlen("<lua.cli>") + 1;
6967 for (i = 0; i < index; i++)
6968 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
6969 fcn->name = calloc(1, len);
6970 if (!fcn->name)
6971 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6972 strncat((char *)fcn->name, "<lua.cli", len);
6973 for (i = 0; i < index; i++) {
6974 strncat((char *)fcn->name, ".", len);
6975 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
6976 }
6977 strncat((char *)fcn->name, ">", len);
6978 fcn->function_ref = ref_io;
6979
6980 /* Fill last entries. */
6981 cli_kws->kw[0].private = fcn;
6982 cli_kws->kw[0].parse = hlua_cli_parse_fct;
6983 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
6984 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
6985
6986 /* Register this new converter */
6987 cli_register_kw(cli_kws);
6988
6989 return 0;
6990}
6991
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006992static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
6993 struct proxy *defpx, const char *file, int line,
6994 char **err, unsigned int *timeout)
6995{
6996 const char *error;
6997
6998 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
6999 if (error && *error != '\0') {
7000 memprintf(err, "%s: invalid timeout", args[0]);
7001 return -1;
7002 }
7003 return 0;
7004}
7005
7006static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7007 struct proxy *defpx, const char *file, int line,
7008 char **err)
7009{
7010 return hlua_read_timeout(args, section_type, curpx, defpx,
7011 file, line, err, &hlua_timeout_session);
7012}
7013
7014static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7015 struct proxy *defpx, const char *file, int line,
7016 char **err)
7017{
7018 return hlua_read_timeout(args, section_type, curpx, defpx,
7019 file, line, err, &hlua_timeout_task);
7020}
7021
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007022static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7023 struct proxy *defpx, const char *file, int line,
7024 char **err)
7025{
7026 return hlua_read_timeout(args, section_type, curpx, defpx,
7027 file, line, err, &hlua_timeout_applet);
7028}
7029
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007030static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7031 struct proxy *defpx, const char *file, int line,
7032 char **err)
7033{
7034 char *error;
7035
7036 hlua_nb_instruction = strtoll(args[1], &error, 10);
7037 if (*error != '\0') {
7038 memprintf(err, "%s: invalid number", args[0]);
7039 return -1;
7040 }
7041 return 0;
7042}
7043
Willy Tarreau32f61e22015-03-18 17:54:59 +01007044static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7045 struct proxy *defpx, const char *file, int line,
7046 char **err)
7047{
7048 char *error;
7049
7050 if (*(args[1]) == 0) {
7051 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7052 return -1;
7053 }
7054 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7055 if (*error != '\0') {
7056 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7057 return -1;
7058 }
7059 return 0;
7060}
7061
7062
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007063/* This function is called by the main configuration key "lua-load". It loads and
7064 * execute an lua file during the parsing of the HAProxy configuration file. It is
7065 * the main lua entry point.
7066 *
7067 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
7068 * occured, otherwise it returns 0.
7069 *
7070 * In some error case, LUA set an error message in top of the stack. This function
7071 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007072 *
7073 * This function can fail with an abort() due to an Lua critical error.
7074 * We are in the configuration parsing process of HAProxy, this abort() is
7075 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007076 */
7077static int hlua_load(char **args, int section_type, struct proxy *curpx,
7078 struct proxy *defpx, const char *file, int line,
7079 char **err)
7080{
7081 int error;
7082
7083 /* Just load and compile the file. */
7084 error = luaL_loadfile(gL.T, args[1]);
7085 if (error) {
7086 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
7087 lua_pop(gL.T, 1);
7088 return -1;
7089 }
7090
7091 /* If no syntax error where detected, execute the code. */
7092 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7093 switch (error) {
7094 case LUA_OK:
7095 break;
7096 case LUA_ERRRUN:
7097 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
7098 lua_pop(gL.T, 1);
7099 return -1;
7100 case LUA_ERRMEM:
7101 memprintf(err, "lua out of memory error\n");
7102 return -1;
7103 case LUA_ERRERR:
7104 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
7105 lua_pop(gL.T, 1);
7106 return -1;
7107 case LUA_ERRGCMM:
7108 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
7109 lua_pop(gL.T, 1);
7110 return -1;
7111 default:
7112 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
7113 lua_pop(gL.T, 1);
7114 return -1;
7115 }
7116
7117 return 0;
7118}
7119
7120/* configuration keywords declaration */
7121static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007122 { CFG_GLOBAL, "lua-load", hlua_load },
7123 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7124 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007125 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007126 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007127 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007128 { 0, NULL, NULL },
7129}};
7130
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007131/* This function can fail with an abort() due to an Lua critical error.
7132 * We are in the initialisation process of HAProxy, this abort() is
7133 * tolerated.
7134 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007135int hlua_post_init()
7136{
7137 struct hlua_init_function *init;
7138 const char *msg;
7139 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007140 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007141
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007142 /* Call post initialisation function in safe environement. */
7143 if (!SET_SAFE_LJMP(gL.T)) {
7144 if (lua_type(gL.T, -1) == LUA_TSTRING)
7145 error = lua_tostring(gL.T, -1);
7146 else
7147 error = "critical error";
7148 fprintf(stderr, "Lua post-init: %s.\n", error);
7149 exit(1);
7150 }
7151 hlua_fcn_post_init(gL.T);
7152 RESET_SAFE_LJMP(gL.T);
7153
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007154 list_for_each_entry(init, &hlua_init_functions, l) {
7155 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7156 ret = hlua_ctx_resume(&gL, 0);
7157 switch (ret) {
7158 case HLUA_E_OK:
7159 lua_pop(gL.T, -1);
7160 return 1;
7161 case HLUA_E_AGAIN:
7162 Alert("lua init: yield not allowed.\n");
7163 return 0;
7164 case HLUA_E_ERRMSG:
7165 msg = lua_tostring(gL.T, -1);
7166 Alert("lua init: %s.\n", msg);
7167 return 0;
7168 case HLUA_E_ERR:
7169 default:
7170 Alert("lua init: unknown runtime error.\n");
7171 return 0;
7172 }
7173 }
7174 return 1;
7175}
7176
Willy Tarreau32f61e22015-03-18 17:54:59 +01007177/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7178 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7179 * is the previously allocated size or the kind of object in case of a new
7180 * allocation. <nsize> is the requested new size.
7181 */
7182static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7183{
7184 struct hlua_mem_allocator *zone = ud;
7185
7186 if (nsize == 0) {
7187 /* it's a free */
7188 if (ptr)
7189 zone->allocated -= osize;
7190 free(ptr);
7191 return NULL;
7192 }
7193
7194 if (!ptr) {
7195 /* it's a new allocation */
7196 if (zone->limit && zone->allocated + nsize > zone->limit)
7197 return NULL;
7198
7199 ptr = malloc(nsize);
7200 if (ptr)
7201 zone->allocated += nsize;
7202 return ptr;
7203 }
7204
7205 /* it's a realloc */
7206 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7207 return NULL;
7208
7209 ptr = realloc(ptr, nsize);
7210 if (ptr)
7211 zone->allocated += nsize - osize;
7212 return ptr;
7213}
7214
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007215/* Ithis function can fail with an abort() due to an Lua critical error.
7216 * We are in the initialisation process of HAProxy, this abort() is
7217 * tolerated.
7218 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007219void hlua_init(void)
7220{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007221 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007222 int idx;
7223 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007224 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007225 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007226 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007227#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007228 struct srv_kw *kw;
7229 int tmp_error;
7230 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007231 char *args[] = { /* SSL client configuration. */
7232 "ssl",
7233 "verify",
7234 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007235 NULL
7236 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007237#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007238
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007239 /* Initialise struct hlua and com signals pool */
7240 pool2_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007241
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007242 /* Register configuration keywords. */
7243 cfg_register_keywords(&cfg_kws);
7244
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007245 /* Init main lua stack. */
7246 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007247 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007248 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007249 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007250 hlua_sethlua(&gL);
7251 gL.Tref = LUA_REFNIL;
7252 gL.task = NULL;
7253
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007254 /* From this point, until the end of the initialisation fucntion,
7255 * the Lua function can fail with an abort. We are in the initialisation
7256 * process of HAProxy, this abort() is tolerated.
7257 */
7258
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007259 /* Initialise lua. */
7260 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007261
Thierry Fournier75933d42016-01-21 09:30:18 +01007262 /* Set safe environment for the initialisation. */
7263 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007264 if (lua_type(gL.T, -1) == LUA_TSTRING)
7265 error_msg = lua_tostring(gL.T, -1);
7266 else
7267 error_msg = "critical error";
7268 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007269 exit(1);
7270 }
7271
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007272 /*
7273 *
7274 * Create "core" object.
7275 *
7276 */
7277
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007278 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007279 lua_newtable(gL.T);
7280
7281 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007282 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007283 hlua_class_const_int(gL.T, log_levels[i], i);
7284
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007285 /* Register special functions. */
7286 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007287 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007288 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007289 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007290 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007291 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007292 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007293 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007294 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007295 hlua_class_function(gL.T, "sleep", hlua_sleep);
7296 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007297 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7298 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7299 hlua_class_function(gL.T, "set_map", hlua_set_map);
7300 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007301 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007302 hlua_class_function(gL.T, "log", hlua_log);
7303 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7304 hlua_class_function(gL.T, "Info", hlua_log_info);
7305 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7306 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007307 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007308 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007309
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007310 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007311
7312 /*
7313 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007314 * Register class Map
7315 *
7316 */
7317
7318 /* This table entry is the object "Map" base. */
7319 lua_newtable(gL.T);
7320
7321 /* register pattern types. */
7322 for (i=0; i<PAT_MATCH_NUM; i++)
7323 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007324 for (i=0; i<PAT_MATCH_NUM; i++) {
7325 snprintf(trash.str, trash.size, "_%s", pat_match_names[i]);
7326 hlua_class_const_int(gL.T, trash.str, i);
7327 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007328
7329 /* register constructor. */
7330 hlua_class_function(gL.T, "new", hlua_map_new);
7331
7332 /* Create and fill the metatable. */
7333 lua_newtable(gL.T);
7334
7335 /* Create and fille the __index entry. */
7336 lua_pushstring(gL.T, "__index");
7337 lua_newtable(gL.T);
7338
7339 /* Register . */
7340 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7341 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7342
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007343 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007344
Thierry Fournier45e78d72016-02-19 18:34:46 +01007345 /* Register previous table in the registry with reference and named entry.
7346 * The function hlua_register_metatable() pops the stack, so we
7347 * previously create a copy of the table.
7348 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007349 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007350 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007351
7352 /* Assign the metatable to the mai Map object. */
7353 lua_setmetatable(gL.T, -2);
7354
7355 /* Set a name to the table. */
7356 lua_setglobal(gL.T, "Map");
7357
7358 /*
7359 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007360 * Register class Channel
7361 *
7362 */
7363
7364 /* Create and fill the metatable. */
7365 lua_newtable(gL.T);
7366
7367 /* Create and fille the __index entry. */
7368 lua_pushstring(gL.T, "__index");
7369 lua_newtable(gL.T);
7370
7371 /* Register . */
7372 hlua_class_function(gL.T, "get", hlua_channel_get);
7373 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7374 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7375 hlua_class_function(gL.T, "set", hlua_channel_set);
7376 hlua_class_function(gL.T, "append", hlua_channel_append);
7377 hlua_class_function(gL.T, "send", hlua_channel_send);
7378 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7379 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7380 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007381 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007382
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007383 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007384
7385 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007386 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007387
7388 /*
7389 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007390 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007391 *
7392 */
7393
7394 /* Create and fill the metatable. */
7395 lua_newtable(gL.T);
7396
7397 /* Create and fille the __index entry. */
7398 lua_pushstring(gL.T, "__index");
7399 lua_newtable(gL.T);
7400
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007401 /* Browse existing fetches and create the associated
7402 * object method.
7403 */
7404 sf = NULL;
7405 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7406
7407 /* Dont register the keywork if the arguments check function are
7408 * not safe during the runtime.
7409 */
7410 if ((sf->val_args != NULL) &&
7411 (sf->val_args != val_payload_lv) &&
7412 (sf->val_args != val_hdr))
7413 continue;
7414
7415 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7416 * by an underscore.
7417 */
7418 strncpy(trash.str, sf->kw, trash.size);
7419 trash.str[trash.size - 1] = '\0';
7420 for (p = trash.str; *p; p++)
7421 if (*p == '.' || *p == '-' || *p == '+')
7422 *p = '_';
7423
7424 /* Register the function. */
7425 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007426 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007427 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007428 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007429 }
7430
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007431 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007432
7433 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007434 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007435
7436 /*
7437 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007438 * Register class Converters
7439 *
7440 */
7441
7442 /* Create and fill the metatable. */
7443 lua_newtable(gL.T);
7444
7445 /* Create and fill the __index entry. */
7446 lua_pushstring(gL.T, "__index");
7447 lua_newtable(gL.T);
7448
7449 /* Browse existing converters and create the associated
7450 * object method.
7451 */
7452 sc = NULL;
7453 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7454 /* Dont register the keywork if the arguments check function are
7455 * not safe during the runtime.
7456 */
7457 if (sc->val_args != NULL)
7458 continue;
7459
7460 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7461 * by an underscore.
7462 */
7463 strncpy(trash.str, sc->kw, trash.size);
7464 trash.str[trash.size - 1] = '\0';
7465 for (p = trash.str; *p; p++)
7466 if (*p == '.' || *p == '-' || *p == '+')
7467 *p = '_';
7468
7469 /* Register the function. */
7470 lua_pushstring(gL.T, trash.str);
7471 lua_pushlightuserdata(gL.T, sc);
7472 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007473 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007474 }
7475
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007476 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007477
7478 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007479 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007480
7481 /*
7482 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007483 * Register class HTTP
7484 *
7485 */
7486
7487 /* Create and fill the metatable. */
7488 lua_newtable(gL.T);
7489
7490 /* Create and fille the __index entry. */
7491 lua_pushstring(gL.T, "__index");
7492 lua_newtable(gL.T);
7493
7494 /* Register Lua functions. */
7495 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7496 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7497 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7498 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7499 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7500 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7501 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7502 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7503 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7504 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7505
7506 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7507 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7508 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7509 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7510 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7511 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007512 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007513
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007514 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007515
7516 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007517 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007518
7519 /*
7520 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007521 * Register class AppletTCP
7522 *
7523 */
7524
7525 /* Create and fill the metatable. */
7526 lua_newtable(gL.T);
7527
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007528 /* Create and fille the __index entry. */
7529 lua_pushstring(gL.T, "__index");
7530 lua_newtable(gL.T);
7531
7532 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007533 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7534 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7535 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7536 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7537 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007538 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7539 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7540 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007541
7542 lua_settable(gL.T, -3);
7543
7544 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007545 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007546
7547 /*
7548 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007549 * Register class AppletHTTP
7550 *
7551 */
7552
7553 /* Create and fill the metatable. */
7554 lua_newtable(gL.T);
7555
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007556 /* Create and fille the __index entry. */
7557 lua_pushstring(gL.T, "__index");
7558 lua_newtable(gL.T);
7559
7560 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007561 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7562 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007563 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7564 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7565 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007566 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7567 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7568 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7569 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7570 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7571 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7572
7573 lua_settable(gL.T, -3);
7574
7575 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007576 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007577
7578 /*
7579 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007580 * Register class TXN
7581 *
7582 */
7583
7584 /* Create and fill the metatable. */
7585 lua_newtable(gL.T);
7586
7587 /* Create and fille the __index entry. */
7588 lua_pushstring(gL.T, "__index");
7589 lua_newtable(gL.T);
7590
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007591 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01007592 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7593 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007594 hlua_class_function(gL.T, "set_var", hlua_set_var);
Christopher Faulet85d79c92016-11-09 16:54:56 +01007595 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007596 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007597 hlua_class_function(gL.T, "done", hlua_txn_done);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007598 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
7599 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7600 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007601 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7602 hlua_class_function(gL.T, "log", hlua_txn_log);
7603 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7604 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7605 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7606 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007607
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007608 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007609
7610 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007611 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007612
7613 /*
7614 *
7615 * Register class Socket
7616 *
7617 */
7618
7619 /* Create and fill the metatable. */
7620 lua_newtable(gL.T);
7621
7622 /* Create and fille the __index entry. */
7623 lua_pushstring(gL.T, "__index");
7624 lua_newtable(gL.T);
7625
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007626#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007627 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007628#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007629 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7630 hlua_class_function(gL.T, "send", hlua_socket_send);
7631 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7632 hlua_class_function(gL.T, "close", hlua_socket_close);
7633 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7634 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7635 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7636 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7637
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007638 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007639
7640 /* Register the garbage collector entry. */
7641 lua_pushstring(gL.T, "__gc");
7642 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007643 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007644
7645 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007646 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007647
7648 /* Proxy and server configuration initialisation. */
7649 memset(&socket_proxy, 0, sizeof(socket_proxy));
7650 init_new_proxy(&socket_proxy);
7651 socket_proxy.parent = NULL;
7652 socket_proxy.last_change = now.tv_sec;
7653 socket_proxy.id = "LUA-SOCKET";
7654 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
7655 socket_proxy.maxconn = 0;
7656 socket_proxy.accept = NULL;
7657 socket_proxy.options2 |= PR_O2_INDEPSTR;
7658 socket_proxy.srv = NULL;
7659 socket_proxy.conn_retries = 0;
7660 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
7661
7662 /* Init TCP server: unchanged parameters */
7663 memset(&socket_tcp, 0, sizeof(socket_tcp));
7664 socket_tcp.next = NULL;
7665 socket_tcp.proxy = &socket_proxy;
7666 socket_tcp.obj_type = OBJ_TYPE_SERVER;
7667 LIST_INIT(&socket_tcp.actconns);
7668 LIST_INIT(&socket_tcp.pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02007669 socket_tcp.priv_conns = NULL;
7670 socket_tcp.idle_conns = NULL;
7671 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02007672 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007673 socket_tcp.last_change = 0;
7674 socket_tcp.id = "LUA-TCP-CONN";
7675 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7676 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7677 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
7678
7679 /* XXX: Copy default parameter from default server,
7680 * but the default server is not initialized.
7681 */
7682 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
7683 socket_tcp.minconn = socket_proxy.defsrv.minconn;
7684 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
7685 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
7686 socket_tcp.onerror = socket_proxy.defsrv.onerror;
7687 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
7688 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
7689 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
7690 socket_tcp.uweight = socket_proxy.defsrv.iweight;
7691 socket_tcp.iweight = socket_proxy.defsrv.iweight;
7692
7693 socket_tcp.check.status = HCHK_STATUS_INI;
7694 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
7695 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
7696 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
7697 socket_tcp.check.server = &socket_tcp;
7698
7699 socket_tcp.agent.status = HCHK_STATUS_INI;
7700 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
7701 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
7702 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
7703 socket_tcp.agent.server = &socket_tcp;
7704
Willy Tarreaua261e9b2016-12-22 20:44:00 +01007705 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007706
7707#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007708 /* Init TCP server: unchanged parameters */
7709 memset(&socket_ssl, 0, sizeof(socket_ssl));
7710 socket_ssl.next = NULL;
7711 socket_ssl.proxy = &socket_proxy;
7712 socket_ssl.obj_type = OBJ_TYPE_SERVER;
7713 LIST_INIT(&socket_ssl.actconns);
7714 LIST_INIT(&socket_ssl.pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02007715 socket_tcp.priv_conns = NULL;
7716 socket_tcp.idle_conns = NULL;
7717 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02007718 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007719 socket_ssl.last_change = 0;
7720 socket_ssl.id = "LUA-SSL-CONN";
7721 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7722 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7723 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
7724
7725 /* XXX: Copy default parameter from default server,
7726 * but the default server is not initialized.
7727 */
7728 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
7729 socket_ssl.minconn = socket_proxy.defsrv.minconn;
7730 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
7731 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
7732 socket_ssl.onerror = socket_proxy.defsrv.onerror;
7733 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
7734 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
7735 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
7736 socket_ssl.uweight = socket_proxy.defsrv.iweight;
7737 socket_ssl.iweight = socket_proxy.defsrv.iweight;
7738
7739 socket_ssl.check.status = HCHK_STATUS_INI;
7740 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
7741 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
7742 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
7743 socket_ssl.check.server = &socket_ssl;
7744
7745 socket_ssl.agent.status = HCHK_STATUS_INI;
7746 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
7747 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
7748 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
7749 socket_ssl.agent.server = &socket_ssl;
7750
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007751 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01007752 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007753
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007754 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007755 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
7756 /*
7757 *
7758 * If the keyword is not known, we can search in the registered
7759 * server keywords. This is usefull to configure special SSL
7760 * features like client certificates and ssl_verify.
7761 *
7762 */
7763 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
7764 if (tmp_error != 0) {
7765 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
7766 abort(); /* This must be never arrives because the command line
7767 not editable by the user. */
7768 }
7769 idx += kw->skip;
7770 }
7771 }
7772
7773 /* Initialize SSL server. */
Willy Tarreau17d45382016-12-22 21:16:08 +01007774 if (socket_ssl.xprt->prepare_srv)
7775 socket_ssl.xprt->prepare_srv(&socket_ssl);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007776#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01007777
7778 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007779}
Willy Tarreaubb57d942016-12-21 19:04:56 +01007780
7781__attribute__((constructor))
7782static void __hlua_init(void)
7783{
7784 char *ptr = NULL;
7785 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
7786 hap_register_build_opts(ptr, 1);
7787}