blob: dbdc975c5c518b02a920125b4cd2f8de6ec4caa6 [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 FOURNIER61ba0e22017-07-12 11:41:21 +020028#include <common/hathreads.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010029
William Lallemand9ed62032016-11-21 17:49:11 +010030#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010031#include <types/hlua.h>
32#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010033#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010034
Thierry FOURNIER55da1652015-01-23 11:36:30 +010035#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020036#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010037#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010038#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010039#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010040#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010041#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010042#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010043#include <proto/hlua_fcn.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020044#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010045#include <proto/obj_type.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010046#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010047#include <proto/payload.h>
48#include <proto/proto_http.h>
49#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010050#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020051#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020052#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010053#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010054#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010055#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020056#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010057
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010058/* Lua uses longjmp to perform yield or throwing errors. This
59 * macro is used only for identifying the function that can
60 * not return because a longjmp is executed.
61 * __LJMP marks a prototype of hlua file that can use longjmp.
62 * WILL_LJMP() marks an lua function that will use longjmp.
63 * MAY_LJMP() marks an lua function that may use longjmp.
64 */
65#define __LJMP
66#define WILL_LJMP(func) func
67#define MAY_LJMP(func) func
68
Thierry FOURNIERbabae282015-09-17 11:36:37 +020069/* This couple of function executes securely some Lua calls outside of
70 * the lua runtime environment. Each Lua call can return a longjmp
71 * if it encounter a memory error.
72 *
73 * Lua documentation extract:
74 *
75 * If an error happens outside any protected environment, Lua calls
76 * a panic function (see lua_atpanic) and then calls abort, thus
77 * exiting the host application. Your panic function can avoid this
78 * exit by never returning (e.g., doing a long jump to your own
79 * recovery point outside Lua).
80 *
81 * The panic function runs as if it were a message handler (see
82 * §2.3); in particular, the error message is at the top of the
83 * stack. However, there is no guarantee about stack space. To push
84 * anything on the stack, the panic function must first check the
85 * available space (see §4.2).
86 *
87 * We must check all the Lua entry point. This includes:
88 * - The include/proto/hlua.h exported functions
89 * - the task wrapper function
90 * - The action wrapper function
91 * - The converters wrapper function
92 * - The sample-fetch wrapper functions
93 *
94 * It is tolerated that the initilisation function returns an abort.
95 * Before each Lua abort, an error message is writed on stderr.
96 *
97 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
98 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
99 * because they must be exists in the program stack when the longjmp
100 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200101 *
102 * Note that the Lua processing is not really thread safe. It provides
103 * heavy system which consists to add our own lock function in the Lua
104 * code and recompile the library. This system will probably not accepted
105 * by maintainers of various distribs.
106 *
107 * Our main excution point of the Lua is the function lua_resume(). A
108 * quick looking on the Lua sources displays a lua_lock() a the start
109 * of function and a lua_unlock() at the end of the function. So I
110 * conclude that the Lua thread safe mode just perform a mutex around
111 * all execution. So I prefer to do this in the HAProxy code, it will be
112 * easier for distro maintainers.
113 *
114 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
115 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
116 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200117 */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100118__decl_hathreads(HA_SPINLOCK_T hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200119THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200120static int hlua_panic_safe(lua_State *L) { return 0; }
121static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
122
123#define SET_SAFE_LJMP(__L) \
124 ({ \
125 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100126 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200127 if (setjmp(safe_ljmp_env) != 0) { \
128 lua_atpanic(__L, hlua_panic_safe); \
129 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100130 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200131 } else { \
132 lua_atpanic(__L, hlua_panic_ljmp); \
133 ret = 1; \
134 } \
135 ret; \
136 })
137
138/* If we are the last function catching Lua errors, we
139 * must reset the panic function.
140 */
141#define RESET_SAFE_LJMP(__L) \
142 do { \
143 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100144 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200145 } while(0)
146
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200147/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200148#define APPLET_DONE 0x01 /* applet processing is done. */
149#define APPLET_100C 0x02 /* 100 continue expected. */
150#define APPLET_HDR_SENT 0x04 /* Response header sent. */
151#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
152#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100153#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200154
155#define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n"
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200156
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100157/* The main Lua execution context. */
158struct hlua gL;
159
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100160/* This is the memory pool containing struct lua for applets
161 * (including cli).
162 */
163struct pool_head *pool2_hlua;
164
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100165/* Used for Socket connection. */
166static struct proxy socket_proxy;
167static struct server socket_tcp;
168#ifdef USE_OPENSSL
169static struct server socket_ssl;
170#endif
171
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100172/* List head of the function called at the initialisation time. */
173struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
174
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100175/* The following variables contains the reference of the different
176 * Lua classes. These references are useful for identify metadata
177 * associated with an object.
178 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100179static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100180static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100181static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100182static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100183static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100184static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200185static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200186static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200187static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100188
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100189/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200190 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100191 * short timeout. Lua linked with tasks doesn't have a timeout
192 * because a task may remain alive during all the haproxy execution.
193 */
194static unsigned int hlua_timeout_session = 4000; /* session timeout. */
195static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200196static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100197
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100198/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
199 * it is used for preventing infinite loops.
200 *
201 * I test the scheer with an infinite loop containing one incrementation
202 * and one test. I run this loop between 10 seconds, I raise a ceil of
203 * 710M loops from one interrupt each 9000 instructions, so I fix the value
204 * to one interrupt each 10 000 instructions.
205 *
206 * configured | Number of
207 * instructions | loops executed
208 * between two | in milions
209 * forced yields |
210 * ---------------+---------------
211 * 10 | 160
212 * 500 | 670
213 * 1000 | 680
214 * 5000 | 700
215 * 7000 | 700
216 * 8000 | 700
217 * 9000 | 710 <- ceil
218 * 10000 | 710
219 * 100000 | 710
220 * 1000000 | 710
221 *
222 */
223static unsigned int hlua_nb_instruction = 10000;
224
Willy Tarreau32f61e22015-03-18 17:54:59 +0100225/* Descriptor for the memory allocation state. If limit is not null, it will
226 * be enforced on any memory allocation.
227 */
228struct hlua_mem_allocator {
229 size_t allocated;
230 size_t limit;
231};
232
233static struct hlua_mem_allocator hlua_global_allocator;
234
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200235static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200236 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200237 "Cache-Control: no-cache\r\n"
238 "Connection: close\r\n"
239 "Content-Type: text/html\r\n"
240 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200241 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200242
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100243/* These functions converts types between HAProxy internal args or
244 * sample and LUA types. Another function permits to check if the
245 * LUA stack contains arguments according with an required ARG_T
246 * format.
247 */
248static int hlua_arg2lua(lua_State *L, const struct arg *arg);
249static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100250__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100251 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100252static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100253static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100254static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
255
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200256__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
257
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200258#define SEND_ERR(__be, __fmt, __args...) \
259 do { \
260 send_log(__be, LOG_ERR, __fmt, ## __args); \
261 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
262 Alert(__fmt, ## __args); \
263 } while (0)
264
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100265/* Used to check an Lua function type in the stack. It creates and
266 * returns a reference of the function. This function throws an
267 * error if the rgument is not a "function".
268 */
269__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
270{
271 if (!lua_isfunction(L, argno)) {
272 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1));
273 WILL_LJMP(luaL_argerror(L, argno, msg));
274 }
275 lua_pushvalue(L, argno);
276 return luaL_ref(L, LUA_REGISTRYINDEX);
277}
278
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200279/* Return the string that is of the top of the stack. */
280const char *hlua_get_top_error_string(lua_State *L)
281{
282 if (lua_gettop(L) < 1)
283 return "unknown error";
284 if (lua_type(L, -1) != LUA_TSTRING)
285 return "unknown error";
286 return lua_tostring(L, -1);
287}
288
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100289/* This function check the number of arguments available in the
290 * stack. If the number of arguments available is not the same
291 * then <nb> an error is throwed.
292 */
293__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
294{
295 if (lua_gettop(L) == nb)
296 return;
297 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
298}
299
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100300/* This fucntion push an error string prefixed by the file name
301 * and the line number where the error is encountered.
302 */
303static int hlua_pusherror(lua_State *L, const char *fmt, ...)
304{
305 va_list argp;
306 va_start(argp, fmt);
307 luaL_where(L, 1);
308 lua_pushvfstring(L, fmt, argp);
309 va_end(argp);
310 lua_concat(L, 2);
311 return 1;
312}
313
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100314/* This functions is used with sample fetch and converters. It
315 * converts the HAProxy configuration argument in a lua stack
316 * values.
317 *
318 * It takes an array of "arg", and each entry of the array is
319 * converted and pushed in the LUA stack.
320 */
321static int hlua_arg2lua(lua_State *L, const struct arg *arg)
322{
323 switch (arg->type) {
324 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100325 case ARGT_TIME:
326 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100327 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100328 break;
329
330 case ARGT_STR:
331 lua_pushlstring(L, arg->data.str.str, arg->data.str.len);
332 break;
333
334 case ARGT_IPV4:
335 case ARGT_IPV6:
336 case ARGT_MSK4:
337 case ARGT_MSK6:
338 case ARGT_FE:
339 case ARGT_BE:
340 case ARGT_TAB:
341 case ARGT_SRV:
342 case ARGT_USR:
343 case ARGT_MAP:
344 default:
345 lua_pushnil(L);
346 break;
347 }
348 return 1;
349}
350
351/* This function take one entrie in an LUA stack at the index "ud",
352 * and try to convert it in an HAProxy argument entry. This is useful
353 * with sample fetch wrappers. The input arguments are gived to the
354 * lua wrapper and converted as arg list by thi function.
355 */
356static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
357{
358 switch (lua_type(L, ud)) {
359
360 case LUA_TNUMBER:
361 case LUA_TBOOLEAN:
362 arg->type = ARGT_SINT;
363 arg->data.sint = lua_tointeger(L, ud);
364 break;
365
366 case LUA_TSTRING:
367 arg->type = ARGT_STR;
368 arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len);
369 break;
370
371 case LUA_TUSERDATA:
372 case LUA_TNIL:
373 case LUA_TTABLE:
374 case LUA_TFUNCTION:
375 case LUA_TTHREAD:
376 case LUA_TLIGHTUSERDATA:
377 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200378 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100379 break;
380 }
381 return 1;
382}
383
384/* the following functions are used to convert a struct sample
385 * in Lua type. This useful to convert the return of the
386 * fetchs or converters.
387 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100388static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100389{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200390 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100391 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100392 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200393 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100394 break;
395
396 case SMP_T_BIN:
397 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200398 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100399 break;
400
401 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200402 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100403 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
404 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
405 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
406 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
407 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
408 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
409 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
410 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
411 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200412 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100413 break;
414 default:
415 lua_pushnil(L);
416 break;
417 }
418 break;
419
420 case SMP_T_IPV4:
421 case SMP_T_IPV6:
422 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200423 if (sample_casts[smp->data.type][SMP_T_STR] &&
424 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200425 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100426 else
427 lua_pushnil(L);
428 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100429 default:
430 lua_pushnil(L);
431 break;
432 }
433 return 1;
434}
435
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100436/* the following functions are used to convert a struct sample
437 * in Lua strings. This is useful to convert the return of the
438 * fetchs or converters.
439 */
440static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
441{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200442 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100443
444 case SMP_T_BIN:
445 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200446 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100447 break;
448
449 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200450 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100451 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
452 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
453 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
454 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
455 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
456 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
457 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
458 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
459 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200460 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100461 break;
462 default:
463 lua_pushstring(L, "");
464 break;
465 }
466 break;
467
468 case SMP_T_SINT:
469 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100470 case SMP_T_IPV4:
471 case SMP_T_IPV6:
472 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200473 if (sample_casts[smp->data.type][SMP_T_STR] &&
474 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200475 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100476 else
477 lua_pushstring(L, "");
478 break;
479 default:
480 lua_pushstring(L, "");
481 break;
482 }
483 return 1;
484}
485
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100486/* the following functions are used to convert an Lua type in a
487 * struct sample. This is useful to provide data from a converter
488 * to the LUA code.
489 */
490static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
491{
492 switch (lua_type(L, ud)) {
493
494 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200495 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200496 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100497 break;
498
499
500 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200501 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200502 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100503 break;
504
505 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200506 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100507 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200508 smp->data.u.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100509 break;
510
511 case LUA_TUSERDATA:
512 case LUA_TNIL:
513 case LUA_TTABLE:
514 case LUA_TFUNCTION:
515 case LUA_TTHREAD:
516 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200517 case LUA_TNONE:
518 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200519 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200520 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100521 break;
522 }
523 return 1;
524}
525
526/* This function check the "argp" builded by another conversion function
527 * is in accord with the expected argp defined by the "mask". The fucntion
528 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100529 *
530 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
531 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100532 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100533__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100534 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100535{
536 int min_arg;
537 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100538 struct proxy *px;
539 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100540
541 idx = 0;
542 min_arg = ARGM(mask);
543 mask >>= ARGM_BITS;
544
545 while (1) {
546
547 /* Check oversize. */
548 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100549 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100550 }
551
552 /* Check for mandatory arguments. */
553 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100554 if (idx < min_arg) {
555
556 /* If miss other argument than the first one, we return an error. */
557 if (idx > 0)
558 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
559
560 /* If first argument have a certain type, some default values
561 * may be used. See the function smp_resolve_args().
562 */
563 switch (mask & ARGT_MASK) {
564
565 case ARGT_FE:
566 if (!(p->cap & PR_CAP_FE))
567 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
568 argp[idx].data.prx = p;
569 argp[idx].type = ARGT_FE;
570 argp[idx+1].type = ARGT_STOP;
571 break;
572
573 case ARGT_BE:
574 if (!(p->cap & PR_CAP_BE))
575 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
576 argp[idx].data.prx = p;
577 argp[idx].type = ARGT_BE;
578 argp[idx+1].type = ARGT_STOP;
579 break;
580
581 case ARGT_TAB:
582 argp[idx].data.prx = p;
583 argp[idx].type = ARGT_TAB;
584 argp[idx+1].type = ARGT_STOP;
585 break;
586
587 default:
588 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
589 break;
590 }
591 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100592 return 0;
593 }
594
595 /* Check for exceed the number of requiered argument. */
596 if ((mask & ARGT_MASK) == ARGT_STOP &&
597 argp[idx].type != ARGT_STOP) {
598 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
599 }
600
601 if ((mask & ARGT_MASK) == ARGT_STOP &&
602 argp[idx].type == ARGT_STOP) {
603 return 0;
604 }
605
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100606 /* Convert some argument types. */
607 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100608 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100609 if (argp[idx].type != ARGT_SINT)
610 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
611 argp[idx].type = ARGT_SINT;
612 break;
613
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100614 case ARGT_TIME:
615 if (argp[idx].type != ARGT_SINT)
616 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200617 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100618 break;
619
620 case ARGT_SIZE:
621 if (argp[idx].type != ARGT_SINT)
622 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200623 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100624 break;
625
626 case ARGT_FE:
627 if (argp[idx].type != ARGT_STR)
628 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
629 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
630 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200631 argp[idx].data.prx = proxy_fe_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100632 if (!argp[idx].data.prx)
633 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
634 argp[idx].type = ARGT_FE;
635 break;
636
637 case ARGT_BE:
638 if (argp[idx].type != ARGT_STR)
639 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
640 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
641 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200642 argp[idx].data.prx = proxy_be_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100643 if (!argp[idx].data.prx)
644 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
645 argp[idx].type = ARGT_BE;
646 break;
647
648 case ARGT_TAB:
649 if (argp[idx].type != ARGT_STR)
650 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
651 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
652 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200653 argp[idx].data.prx = proxy_tbl_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100654 if (!argp[idx].data.prx)
655 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
656 argp[idx].type = ARGT_TAB;
657 break;
658
659 case ARGT_SRV:
660 if (argp[idx].type != ARGT_STR)
661 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
662 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
663 trash.str[argp[idx].data.str.len] = 0;
664 sname = strrchr(trash.str, '/');
665 if (sname) {
666 *sname++ = '\0';
667 pname = trash.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200668 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100669 if (!px)
670 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
671 }
672 else {
673 sname = trash.str;
674 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100675 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100676 argp[idx].data.srv = findserver(px, sname);
677 if (!argp[idx].data.srv)
678 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
679 argp[idx].type = ARGT_SRV;
680 break;
681
682 case ARGT_IPV4:
683 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
684 trash.str[argp[idx].data.str.len] = 0;
685 if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4))
686 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
687 argp[idx].type = ARGT_IPV4;
688 break;
689
690 case ARGT_MSK4:
691 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
692 trash.str[argp[idx].data.str.len] = 0;
693 if (!str2mask(trash.str, &argp[idx].data.ipv4))
694 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
695 argp[idx].type = ARGT_MSK4;
696 break;
697
698 case ARGT_IPV6:
699 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
700 trash.str[argp[idx].data.str.len] = 0;
701 if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6))
702 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
703 argp[idx].type = ARGT_IPV6;
704 break;
705
706 case ARGT_MSK6:
707 case ARGT_MAP:
708 case ARGT_REG:
709 case ARGT_USR:
710 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100711 break;
712 }
713
714 /* Check for type of argument. */
715 if ((mask & ARGT_MASK) != argp[idx].type) {
716 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
717 arg_type_names[(mask & ARGT_MASK)],
718 arg_type_names[argp[idx].type & ARGT_MASK]);
719 WILL_LJMP(luaL_argerror(L, first + idx, msg));
720 }
721
722 /* Next argument. */
723 mask >>= ARGT_BITS;
724 idx++;
725 }
726}
727
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100728/*
729 * The following functions are used to make correspondance between the the
730 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100731 *
732 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100733 * - hlua_sethlua : create the association between hlua context and lua_state.
734 */
735static inline struct hlua *hlua_gethlua(lua_State *L)
736{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100737 struct hlua **hlua = lua_getextraspace(L);
738 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100739}
740static inline void hlua_sethlua(struct hlua *hlua)
741{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100742 struct hlua **hlua_store = lua_getextraspace(hlua->T);
743 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100744}
745
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100746/* This function is used to send logs. It try to send on screen (stderr)
747 * and on the default syslog server.
748 */
749static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
750{
751 struct tm tm;
752 char *p;
753
754 /* Cleanup the log message. */
755 p = trash.str;
756 for (; *msg != '\0'; msg++, p++) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200757 if (p >= trash.str + trash.size - 1) {
758 /* Break the message if exceed the buffer size. */
759 *(p-4) = ' ';
760 *(p-3) = '.';
761 *(p-2) = '.';
762 *(p-1) = '.';
763 break;
764 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100765 if (isprint(*msg))
766 *p = *msg;
767 else
768 *p = '.';
769 }
770 *p = '\0';
771
Thierry FOURNIER5554e292015-09-09 11:21:37 +0200772 send_log(px, level, "%s\n", trash.str);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100773 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200774 get_localtime(date.tv_sec, &tm);
775 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100776 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
777 (int)getpid(), trash.str);
778 fflush(stderr);
779 }
780}
781
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100782/* This function just ensure that the yield will be always
783 * returned with a timeout and permit to set some flags
784 */
785__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100786 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100787{
788 struct hlua *hlua = hlua_gethlua(L);
789
790 /* Set the wake timeout. If timeout is required, we set
791 * the expiration time.
792 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200793 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100794
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100795 hlua->flags |= flags;
796
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100797 /* Process the yield. */
798 WILL_LJMP(lua_yieldk(L, nresults, ctx, k));
799}
800
Willy Tarreau87b09662015-04-03 00:22:06 +0200801/* This function initialises the Lua environment stored in the stream.
802 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100803 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200804 *
805 * This function is particular. it initialises a new Lua thread. If the
806 * initialisation fails (example: out of memory error), the lua function
807 * throws an error (longjmp).
808 *
809 * This function manipulates two Lua stack: the main and the thread. Only
810 * the main stack can fail. The thread is not manipulated. This function
811 * MUST NOT manipulate the created thread stack state, because is not
812 * proctected agains error throwed by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100813 */
814int hlua_ctx_init(struct hlua *lua, struct task *task)
815{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200816 if (!SET_SAFE_LJMP(gL.T)) {
817 lua->Tref = LUA_REFNIL;
818 return 0;
819 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100820 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100821 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100822 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100823 lua->T = lua_newthread(gL.T);
824 if (!lua->T) {
825 lua->Tref = LUA_REFNIL;
Thierry FOURNIER0a976202017-07-12 11:18:00 +0200826 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100827 return 0;
828 }
829 hlua_sethlua(lua);
830 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
831 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200832 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100833 return 1;
834}
835
Willy Tarreau87b09662015-04-03 00:22:06 +0200836/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100837 * is destroyed. The destroy also the memory context. The struct "lua"
838 * is not freed.
839 */
840void hlua_ctx_destroy(struct hlua *lua)
841{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100842 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100843 return;
844
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100845 if (!lua->T)
846 goto end;
847
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100848 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200849 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100850
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200851 if (!SET_SAFE_LJMP(lua->T))
852 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100853 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200854 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200855
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200856 if (!SET_SAFE_LJMP(gL.T))
857 return;
858 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
859 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200860 /* Forces a garbage collecting process. If the Lua program is finished
861 * without error, we run the GC on the thread pointer. Its freed all
862 * the unused memory.
863 * If the thread is finnish with an error or is currently yielded,
864 * it seems that the GC applied on the thread doesn't clean anything,
865 * so e run the GC on the main thread.
866 * NOTE: maybe this action locks all the Lua threads untiml the en of
867 * the garbage collection.
868 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200869 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200870 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200871 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200872 lua_gc(gL.T, LUA_GCCOLLECT, 0);
873 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200874 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200875
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200876 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100877
878end:
879 pool_free2(pool2_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100880}
881
882/* This function is used to restore the Lua context when a coroutine
883 * fails. This function copy the common memory between old coroutine
884 * and the new coroutine. The old coroutine is destroyed, and its
885 * replaced by the new coroutine.
886 * If the flag "keep_msg" is set, the last entry of the old is assumed
887 * as string error message and it is copied in the new stack.
888 */
889static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
890{
891 lua_State *T;
892 int new_ref;
893
894 /* Renew the main LUA stack doesn't have sense. */
895 if (lua == &gL)
896 return 0;
897
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100898 /* New Lua coroutine. */
899 T = lua_newthread(gL.T);
900 if (!T)
901 return 0;
902
903 /* Copy last error message. */
904 if (keep_msg)
905 lua_xmove(lua->T, T, 1);
906
907 /* Copy data between the coroutines. */
908 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
909 lua_xmove(lua->T, T, 1);
910 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
911
912 /* Destroy old data. */
913 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
914
915 /* The thread is garbage collected by Lua. */
916 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
917
918 /* Fill the struct with the new coroutine values. */
919 lua->Mref = new_ref;
920 lua->T = T;
921 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
922
923 /* Set context. */
924 hlua_sethlua(lua);
925
926 return 1;
927}
928
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100929void hlua_hook(lua_State *L, lua_Debug *ar)
930{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100931 struct hlua *hlua = hlua_gethlua(L);
932
933 /* Lua cannot yield when its returning from a function,
934 * so, we can fix the interrupt hook to 1 instruction,
935 * expecting that the function is finnished.
936 */
937 if (lua_gethookmask(L) & LUA_MASKRET) {
938 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
939 return;
940 }
941
942 /* restore the interrupt condition. */
943 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
944
945 /* If we interrupt the Lua processing in yieldable state, we yield.
946 * If the state is not yieldable, trying yield causes an error.
947 */
948 if (lua_isyieldable(L))
949 WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
950
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +0100951 /* If we cannot yield, update the clock and check the timeout. */
952 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200953 hlua->run_time += now_ms - hlua->start_time;
954 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100955 lua_pushfstring(L, "execution timeout");
956 WILL_LJMP(lua_error(L));
957 }
958
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200959 /* Update the start time. */
960 hlua->start_time = now_ms;
961
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100962 /* Try to interrupt the process at the end of the current
963 * unyieldable function.
964 */
965 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100966}
967
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100968/* This function start or resumes the Lua stack execution. If the flag
969 * "yield_allowed" if no set and the LUA stack execution returns a yield
970 * The function return an error.
971 *
972 * The function can returns 4 values:
973 * - HLUA_E_OK : The execution is terminated without any errors.
974 * - HLUA_E_AGAIN : The execution must continue at the next associated
975 * task wakeup.
976 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
977 * the top of the stack.
978 * - HLUA_E_ERR : An error has occured without error message.
979 *
980 * If an error occured, the stack is renewed and it is ready to run new
981 * LUA code.
982 */
983static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
984{
985 int ret;
986 const char *msg;
987
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200988 /* Initialise run time counter. */
989 if (!HLUA_IS_RUNNING(lua))
990 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100991
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200992 /* Lock the whole Lua execution. This lock must be before the
993 * label "resume_execution".
994 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100995 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200996
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100997resume_execution:
998
999 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1000 * instructions. it is used for preventing infinite loops.
1001 */
1002 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1003
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001004 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001005 HLUA_SET_RUN(lua);
1006 HLUA_CLR_CTRLYIELD(lua);
1007 HLUA_CLR_WAKERESWR(lua);
1008 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001009
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001010 /* Update the start time. */
1011 lua->start_time = now_ms;
1012
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001013 /* Call the function. */
1014 ret = lua_resume(lua->T, gL.T, lua->nargs);
1015 switch (ret) {
1016
1017 case LUA_OK:
1018 ret = HLUA_E_OK;
1019 break;
1020
1021 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001022 /* Check if the execution timeout is expired. It it is the case, we
1023 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001024 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001025 tv_update_date(0, 1);
1026 lua->run_time += now_ms - lua->start_time;
1027 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001028 lua_settop(lua->T, 0); /* Empty the stack. */
1029 if (!lua_checkstack(lua->T, 1)) {
1030 ret = HLUA_E_ERR;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001031 break;
1032 }
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001033 lua_pushfstring(lua->T, "execution timeout");
1034 ret = HLUA_E_ERRMSG;
1035 break;
1036 }
1037 /* Process the forced yield. if the general yield is not allowed or
1038 * if no task were associated this the current Lua execution
1039 * coroutine, we resume the execution. Else we want to return in the
1040 * scheduler and we want to be waked up again, to continue the
1041 * current Lua execution. So we schedule our own task.
1042 */
1043 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001044 if (!yield_allowed || !lua->task)
1045 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001046 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001047 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001048 if (!yield_allowed) {
1049 lua_settop(lua->T, 0); /* Empty the stack. */
1050 if (!lua_checkstack(lua->T, 1)) {
1051 ret = HLUA_E_ERR;
1052 break;
1053 }
1054 lua_pushfstring(lua->T, "yield not allowed");
1055 ret = HLUA_E_ERRMSG;
1056 break;
1057 }
1058 ret = HLUA_E_AGAIN;
1059 break;
1060
1061 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001062
1063 /* Special exit case. The traditionnal exit is returned as an error
1064 * because the errors ares the only one mean to return immediately
1065 * from and lua execution.
1066 */
1067 if (lua->flags & HLUA_EXIT) {
1068 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001069 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001070 break;
1071 }
1072
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001073 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001074 if (!lua_checkstack(lua->T, 1)) {
1075 ret = HLUA_E_ERR;
1076 break;
1077 }
1078 msg = lua_tostring(lua->T, -1);
1079 lua_settop(lua->T, 0); /* Empty the stack. */
1080 lua_pop(lua->T, 1);
1081 if (msg)
1082 lua_pushfstring(lua->T, "runtime error: %s", msg);
1083 else
1084 lua_pushfstring(lua->T, "unknown runtime error");
1085 ret = HLUA_E_ERRMSG;
1086 break;
1087
1088 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001089 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001090 lua_settop(lua->T, 0); /* Empty the stack. */
1091 if (!lua_checkstack(lua->T, 1)) {
1092 ret = HLUA_E_ERR;
1093 break;
1094 }
1095 lua_pushfstring(lua->T, "out of memory error");
1096 ret = HLUA_E_ERRMSG;
1097 break;
1098
1099 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001100 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001101 if (!lua_checkstack(lua->T, 1)) {
1102 ret = HLUA_E_ERR;
1103 break;
1104 }
1105 msg = lua_tostring(lua->T, -1);
1106 lua_settop(lua->T, 0); /* Empty the stack. */
1107 lua_pop(lua->T, 1);
1108 if (msg)
1109 lua_pushfstring(lua->T, "message handler error: %s", msg);
1110 else
1111 lua_pushfstring(lua->T, "message handler error");
1112 ret = HLUA_E_ERRMSG;
1113 break;
1114
1115 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001116 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001117 lua_settop(lua->T, 0); /* Empty the stack. */
1118 if (!lua_checkstack(lua->T, 1)) {
1119 ret = HLUA_E_ERR;
1120 break;
1121 }
1122 lua_pushfstring(lua->T, "unknonwn error");
1123 ret = HLUA_E_ERRMSG;
1124 break;
1125 }
1126
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001127 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001128 if (lua->flags & HLUA_MUST_GC &&
1129 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001130 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1131
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001132 switch (ret) {
1133 case HLUA_E_AGAIN:
1134 break;
1135
1136 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001137 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001138 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001139 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001140 break;
1141
1142 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001143 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001144 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001145 hlua_ctx_renew(lua, 0);
1146 break;
1147
1148 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001149 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001150 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001151 break;
1152 }
1153
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001154 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001155 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001156
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001157 return ret;
1158}
1159
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001160/* This function exit the current code. */
1161__LJMP static int hlua_done(lua_State *L)
1162{
1163 struct hlua *hlua = hlua_gethlua(L);
1164
1165 hlua->flags |= HLUA_EXIT;
1166 WILL_LJMP(lua_error(L));
1167
1168 return 0;
1169}
1170
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001171/* This function is an LUA binding. It provides a function
1172 * for deleting ACL from a referenced ACL file.
1173 */
1174__LJMP static int hlua_del_acl(lua_State *L)
1175{
1176 const char *name;
1177 const char *key;
1178 struct pat_ref *ref;
1179
1180 MAY_LJMP(check_args(L, 2, "del_acl"));
1181
1182 name = MAY_LJMP(luaL_checkstring(L, 1));
1183 key = MAY_LJMP(luaL_checkstring(L, 2));
1184
1185 ref = pat_ref_lookup(name);
1186 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001187 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001188
1189 pat_ref_delete(ref, key);
1190 return 0;
1191}
1192
1193/* This function is an LUA binding. It provides a function
1194 * for deleting map entry from a referenced map file.
1195 */
1196static int hlua_del_map(lua_State *L)
1197{
1198 const char *name;
1199 const char *key;
1200 struct pat_ref *ref;
1201
1202 MAY_LJMP(check_args(L, 2, "del_map"));
1203
1204 name = MAY_LJMP(luaL_checkstring(L, 1));
1205 key = MAY_LJMP(luaL_checkstring(L, 2));
1206
1207 ref = pat_ref_lookup(name);
1208 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001209 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001210
1211 pat_ref_delete(ref, key);
1212 return 0;
1213}
1214
1215/* This function is an LUA binding. It provides a function
1216 * for adding ACL pattern from a referenced ACL file.
1217 */
1218static int hlua_add_acl(lua_State *L)
1219{
1220 const char *name;
1221 const char *key;
1222 struct pat_ref *ref;
1223
1224 MAY_LJMP(check_args(L, 2, "add_acl"));
1225
1226 name = MAY_LJMP(luaL_checkstring(L, 1));
1227 key = MAY_LJMP(luaL_checkstring(L, 2));
1228
1229 ref = pat_ref_lookup(name);
1230 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001231 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001232
1233 if (pat_ref_find_elt(ref, key) == NULL)
1234 pat_ref_add(ref, key, NULL, NULL);
1235 return 0;
1236}
1237
1238/* This function is an LUA binding. It provides a function
1239 * for setting map pattern and sample from a referenced map
1240 * file.
1241 */
1242static int hlua_set_map(lua_State *L)
1243{
1244 const char *name;
1245 const char *key;
1246 const char *value;
1247 struct pat_ref *ref;
1248
1249 MAY_LJMP(check_args(L, 3, "set_map"));
1250
1251 name = MAY_LJMP(luaL_checkstring(L, 1));
1252 key = MAY_LJMP(luaL_checkstring(L, 2));
1253 value = MAY_LJMP(luaL_checkstring(L, 3));
1254
1255 ref = pat_ref_lookup(name);
1256 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001257 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001258
1259 if (pat_ref_find_elt(ref, key) != NULL)
1260 pat_ref_set(ref, key, value, NULL);
1261 else
1262 pat_ref_add(ref, key, value, NULL);
1263 return 0;
1264}
1265
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001266/* A class is a lot of memory that contain data. This data can be a table,
1267 * an integer or user data. This data is associated with a metatable. This
1268 * metatable have an original version registred in the global context with
1269 * the name of the object (_G[<name>] = <metable> ).
1270 *
1271 * A metable is a table that modify the standard behavior of a standard
1272 * access to the associated data. The entries of this new metatable are
1273 * defined as is:
1274 *
1275 * http://lua-users.org/wiki/MetatableEvents
1276 *
1277 * __index
1278 *
1279 * we access an absent field in a table, the result is nil. This is
1280 * true, but it is not the whole truth. Actually, such access triggers
1281 * the interpreter to look for an __index metamethod: If there is no
1282 * such method, as usually happens, then the access results in nil;
1283 * otherwise, the metamethod will provide the result.
1284 *
1285 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1286 * the key does not appear in the table, but the metatable has an __index
1287 * property:
1288 *
1289 * - if the value is a function, the function is called, passing in the
1290 * table and the key; the return value of that function is returned as
1291 * the result.
1292 *
1293 * - if the value is another table, the value of the key in that table is
1294 * asked for and returned (and if it doesn't exist in that table, but that
1295 * table's metatable has an __index property, then it continues on up)
1296 *
1297 * - Use "rawget(myTable,key)" to skip this metamethod.
1298 *
1299 * http://www.lua.org/pil/13.4.1.html
1300 *
1301 * __newindex
1302 *
1303 * Like __index, but control property assignment.
1304 *
1305 * __mode - Control weak references. A string value with one or both
1306 * of the characters 'k' and 'v' which specifies that the the
1307 * keys and/or values in the table are weak references.
1308 *
1309 * __call - Treat a table like a function. When a table is followed by
1310 * parenthesis such as "myTable( 'foo' )" and the metatable has
1311 * a __call key pointing to a function, that function is invoked
1312 * (passing any specified arguments) and the return value is
1313 * returned.
1314 *
1315 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1316 * called, if the metatable for myTable has a __metatable
1317 * key, the value of that key is returned instead of the
1318 * actual metatable.
1319 *
1320 * __tostring - Control string representation. When the builtin
1321 * "tostring( myTable )" function is called, if the metatable
1322 * for myTable has a __tostring property set to a function,
1323 * that function is invoked (passing myTable to it) and the
1324 * return value is used as the string representation.
1325 *
1326 * __len - Control table length. When the table length is requested using
1327 * the length operator ( '#' ), if the metatable for myTable has
1328 * a __len key pointing to a function, that function is invoked
1329 * (passing myTable to it) and the return value used as the value
1330 * of "#myTable".
1331 *
1332 * __gc - Userdata finalizer code. When userdata is set to be garbage
1333 * collected, if the metatable has a __gc field pointing to a
1334 * function, that function is first invoked, passing the userdata
1335 * to it. The __gc metamethod is not called for tables.
1336 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1337 *
1338 * Special metamethods for redefining standard operators:
1339 * http://www.lua.org/pil/13.1.html
1340 *
1341 * __add "+"
1342 * __sub "-"
1343 * __mul "*"
1344 * __div "/"
1345 * __unm "!"
1346 * __pow "^"
1347 * __concat ".."
1348 *
1349 * Special methods for redfining standar relations
1350 * http://www.lua.org/pil/13.2.html
1351 *
1352 * __eq "=="
1353 * __lt "<"
1354 * __le "<="
1355 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001356
1357/*
1358 *
1359 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001360 * Class Map
1361 *
1362 *
1363 */
1364
1365/* Returns a struct hlua_map if the stack entry "ud" is
1366 * a class session, otherwise it throws an error.
1367 */
1368__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1369{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001370 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001371}
1372
1373/* This function is the map constructor. It don't need
1374 * the class Map object. It creates and return a new Map
1375 * object. It must be called only during "body" or "init"
1376 * context because it process some filesystem accesses.
1377 */
1378__LJMP static int hlua_map_new(struct lua_State *L)
1379{
1380 const char *fn;
1381 int match = PAT_MATCH_STR;
1382 struct sample_conv conv;
1383 const char *file = "";
1384 int line = 0;
1385 lua_Debug ar;
1386 char *err = NULL;
1387 struct arg args[2];
1388
1389 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1390 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1391
1392 fn = MAY_LJMP(luaL_checkstring(L, 1));
1393
1394 if (lua_gettop(L) >= 2) {
1395 match = MAY_LJMP(luaL_checkinteger(L, 2));
1396 if (match < 0 || match >= PAT_MATCH_NUM)
1397 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1398 }
1399
1400 /* Get Lua filename and line number. */
1401 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1402 lua_getinfo(L, "Sl", &ar); /* get info about it */
1403 if (ar.currentline > 0) { /* is there info? */
1404 file = ar.short_src;
1405 line = ar.currentline;
1406 }
1407 }
1408
1409 /* fill fake sample_conv struct. */
1410 conv.kw = ""; /* unused. */
1411 conv.process = NULL; /* unused. */
1412 conv.arg_mask = 0; /* unused. */
1413 conv.val_args = NULL; /* unused. */
1414 conv.out_type = SMP_T_STR;
1415 conv.private = (void *)(long)match;
1416 switch (match) {
1417 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1418 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1419 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1420 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1421 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1422 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1423 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001424 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001425 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1426 default:
1427 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1428 }
1429
1430 /* fill fake args. */
1431 args[0].type = ARGT_STR;
1432 args[0].data.str.str = (char *)fn;
1433 args[1].type = ARGT_STOP;
1434
1435 /* load the map. */
1436 if (!sample_load_map(args, &conv, file, line, &err)) {
1437 /* error case: we cant use luaL_error because we must
1438 * free the err variable.
1439 */
1440 luaL_where(L, 1);
1441 lua_pushfstring(L, "'new': %s.", err);
1442 lua_concat(L, 2);
1443 free(err);
1444 WILL_LJMP(lua_error(L));
1445 }
1446
1447 /* create the lua object. */
1448 lua_newtable(L);
1449 lua_pushlightuserdata(L, args[0].data.map);
1450 lua_rawseti(L, -2, 0);
1451
1452 /* Pop a class Map metatable and affect it to the userdata. */
1453 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1454 lua_setmetatable(L, -2);
1455
1456
1457 return 1;
1458}
1459
1460__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1461{
1462 struct map_descriptor *desc;
1463 struct pattern *pat;
1464 struct sample smp;
1465
1466 MAY_LJMP(check_args(L, 2, "lookup"));
1467 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001468 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001469 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001470 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001471 }
1472 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001473 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001474 smp.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001475 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 +02001476 }
1477
1478 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001479 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001480 if (str)
1481 lua_pushstring(L, "");
1482 else
1483 lua_pushnil(L);
1484 return 1;
1485 }
1486
1487 /* The Lua pattern must return a string, so we can't check the returned type */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001488 lua_pushlstring(L, pat->data->u.str.str, pat->data->u.str.len);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001489 return 1;
1490}
1491
1492__LJMP static int hlua_map_lookup(struct lua_State *L)
1493{
1494 return _hlua_map_lookup(L, 0);
1495}
1496
1497__LJMP static int hlua_map_slookup(struct lua_State *L)
1498{
1499 return _hlua_map_lookup(L, 1);
1500}
1501
1502/*
1503 *
1504 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001505 * Class Socket
1506 *
1507 *
1508 */
1509
1510__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1511{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001512 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001513}
1514
1515/* This function is the handler called for each I/O on the established
1516 * connection. It is used for notify space avalaible to send or data
1517 * received.
1518 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001519static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001520{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001521 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001522 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001523
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001524 if (appctx->ctx.hlua_cosocket.die) {
1525 si_shutw(si);
1526 si_shutr(si);
1527 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001528 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1529 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001530 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1531 }
1532
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001533 /* If the connection object is not avalaible, close all the
1534 * streams and wakeup everithing waiting for.
1535 */
1536 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001537 si_shutw(si);
1538 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001539 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001540 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1541 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001542 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001543 }
1544
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001545 /* If we cant write, wakeup the pending write signals. */
1546 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001547 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001548
1549 /* If we cant read, wakeup the pending read signals. */
1550 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001551 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001552
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001553 /* if the connection is not estabkished, inform the stream that we want
1554 * to be notified whenever the connection completes.
1555 */
1556 if (!(c->flags & CO_FL_CONNECTED)) {
1557 si_applet_cant_get(si);
1558 si_applet_cant_put(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001559 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001560 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001561
1562 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001563 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001564
1565 /* Wake the tasks which wants to write if the buffer have avalaible space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001566 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001567 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001568
1569 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001570 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001571 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001572}
1573
Willy Tarreau87b09662015-04-03 00:22:06 +02001574/* This function is called when the "struct stream" is destroyed.
1575 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001576 * Wake all the pending signals.
1577 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001578static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001579{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001580 struct xref *peer;
1581
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001582 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001583 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1584 if (peer)
1585 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001586
1587 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001588 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1589 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001590}
1591
1592/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001593 * uses this object. If the stream does not exists, just quit.
1594 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001595 * pending signal can rest in the read and write lists. destroy
1596 * it.
1597 */
1598__LJMP static int hlua_socket_gc(lua_State *L)
1599{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001600 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001601 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001602 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001603
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001604 MAY_LJMP(check_args(L, 1, "__gc"));
1605
1606 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001607 peer = xref_get_peer_and_lock(&socket->xref);
1608 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001609 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001610 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001611
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001612 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001613 appctx->ctx.hlua_cosocket.die = 1;
1614 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001615
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001616 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001617 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001618 return 0;
1619}
1620
1621/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001622 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001623 */
1624__LJMP static int hlua_socket_close(lua_State *L)
1625{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001626 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001627 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001628 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001629
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001630 MAY_LJMP(check_args(L, 1, "close"));
1631
1632 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001633
1634 /* Check if we run on the same thread than the xreator thread.
1635 * We cannot access to the socket if the thread is different.
1636 */
1637 if (socket->tid != tid)
1638 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1639
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001640 peer = xref_get_peer_and_lock(&socket->xref);
1641 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001642 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001643 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001644
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001645 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001646 appctx->ctx.hlua_cosocket.die = 1;
1647 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001648
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001649 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001650 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001651 return 0;
1652}
1653
1654/* This Lua function assumes that the stack contain three parameters.
1655 * 1 - USERDATA containing a struct socket
1656 * 2 - INTEGER with values of the macro defined below
1657 * If the integer is -1, we must read at most one line.
1658 * If the integer is -2, we ust read all the data until the
1659 * end of the stream.
1660 * If the integer is positive value, we must read a number of
1661 * bytes corresponding to this value.
1662 */
1663#define HLSR_READ_LINE (-1)
1664#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001665__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001666{
1667 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1668 int wanted = lua_tointeger(L, 2);
1669 struct hlua *hlua = hlua_gethlua(L);
1670 struct appctx *appctx;
1671 int len;
1672 int nblk;
1673 char *blk1;
1674 int len1;
1675 char *blk2;
1676 int len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001677 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001678 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001679 struct stream_interface *si;
1680 struct stream *s;
1681 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001682
1683 /* Check if this lua stack is schedulable. */
1684 if (!hlua || !hlua->task)
1685 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1686 "'frontend', 'backend' or 'task'"));
1687
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001688 /* Check if we run on the same thread than the xreator thread.
1689 * We cannot access to the socket if the thread is different.
1690 */
1691 if (socket->tid != tid)
1692 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1693
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001694 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001695 peer = xref_get_peer_and_lock(&socket->xref);
1696 if (!peer)
1697 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001698 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1699 si = appctx->owner;
1700 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001701
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001702 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001704 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001705 nblk = co_getline_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;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001710
1711 /* remove final \r\n. */
1712 if (nblk == 1) {
1713 if (blk1[len1-1] == '\n') {
1714 len1--;
1715 skip_at_end++;
1716 if (blk1[len1-1] == '\r') {
1717 len1--;
1718 skip_at_end++;
1719 }
1720 }
1721 }
1722 else {
1723 if (blk2[len2-1] == '\n') {
1724 len2--;
1725 skip_at_end++;
1726 if (blk2[len2-1] == '\r') {
1727 len2--;
1728 skip_at_end++;
1729 }
1730 }
1731 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001732 }
1733
1734 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001735 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001736 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001737 if (nblk < 0) /* Connection close. */
1738 goto connection_closed;
1739 if (nblk == 0) /* No data avalaible. */
1740 goto connection_empty;
1741 }
1742
1743 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001744 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001745 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001746 if (nblk < 0) /* Connection close. */
1747 goto connection_closed;
1748 if (nblk == 0) /* No data avalaible. */
1749 goto connection_empty;
1750
1751 if (len1 > wanted) {
1752 nblk = 1;
1753 len1 = wanted;
1754 } if (nblk == 2 && len1 + len2 > wanted)
1755 len2 = wanted - len1;
1756 }
1757
1758 len = len1;
1759
1760 luaL_addlstring(&socket->b, blk1, len1);
1761 if (nblk == 2) {
1762 len += len2;
1763 luaL_addlstring(&socket->b, blk2, len2);
1764 }
1765
1766 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001767 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001768
1769 /* Don't wait anything. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001770 stream_int_notify(&s->si[0]);
1771 stream_int_update_applet(&s->si[0]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001772
1773 /* If the pattern reclaim to read all the data
1774 * in the connection, got out.
1775 */
1776 if (wanted == HLSR_READ_ALL)
1777 goto connection_empty;
1778 else if (wanted >= 0 && len < wanted)
1779 goto connection_empty;
1780
1781 /* Return result. */
1782 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001783 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001784 return 1;
1785
1786connection_closed:
1787
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001788 xref_unlock(&socket->xref, peer);
1789
1790no_peer:
1791
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001792 /* If the buffer containds data. */
1793 if (socket->b.n > 0) {
1794 luaL_pushresult(&socket->b);
1795 return 1;
1796 }
1797 lua_pushnil(L);
1798 lua_pushstring(L, "connection closed.");
1799 return 2;
1800
1801connection_empty:
1802
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001803 appctx = objt_appctx(s->si[0].end);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001804 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1805 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001806 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001807 }
1808 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001809 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001810 return 0;
1811}
1812
1813/* This Lus function gets two parameters. The first one can be string
1814 * or a number. If the string is "*l", the user require one line. If
1815 * the string is "*a", the user require all the content of the stream.
1816 * If the value is a number, the user require a number of bytes equal
1817 * to the value. The default value is "*l" (a line).
1818 *
1819 * This paraeter with a variable type is converted in integer. This
1820 * integer takes this values:
1821 * -1 : read a line
1822 * -2 : read all the stream
1823 * >0 : amount if bytes.
1824 *
1825 * The second parameter is optinal. It contains a string that must be
1826 * concatenated with the read data.
1827 */
1828__LJMP static int hlua_socket_receive(struct lua_State *L)
1829{
1830 int wanted = HLSR_READ_LINE;
1831 const char *pattern;
1832 int type;
1833 char *error;
1834 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001835 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001836
1837 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1838 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1839
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001840 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001841
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001842 /* Check if we run on the same thread than the xreator thread.
1843 * We cannot access to the socket if the thread is different.
1844 */
1845 if (socket->tid != tid)
1846 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1847
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001848 /* check for pattern. */
1849 if (lua_gettop(L) >= 2) {
1850 type = lua_type(L, 2);
1851 if (type == LUA_TSTRING) {
1852 pattern = lua_tostring(L, 2);
1853 if (strcmp(pattern, "*a") == 0)
1854 wanted = HLSR_READ_ALL;
1855 else if (strcmp(pattern, "*l") == 0)
1856 wanted = HLSR_READ_LINE;
1857 else {
1858 wanted = strtoll(pattern, &error, 10);
1859 if (*error != '\0')
1860 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1861 }
1862 }
1863 else if (type == LUA_TNUMBER) {
1864 wanted = lua_tointeger(L, 2);
1865 if (wanted < 0)
1866 WILL_LJMP(luaL_error(L, "Unsupported size."));
1867 }
1868 }
1869
1870 /* Set pattern. */
1871 lua_pushinteger(L, wanted);
1872 lua_replace(L, 2);
1873
1874 /* init bufffer, and fiil it wih prefix. */
1875 luaL_buffinit(L, &socket->b);
1876
1877 /* Check prefix. */
1878 if (lua_gettop(L) >= 3) {
1879 if (lua_type(L, 3) != LUA_TSTRING)
1880 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1881 pattern = lua_tolstring(L, 3, &len);
1882 luaL_addlstring(&socket->b, pattern, len);
1883 }
1884
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001885 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001886}
1887
1888/* Write the Lua input string in the output buffer.
1889 * This fucntion returns a yield if no space are available.
1890 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001891static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001892{
1893 struct hlua_socket *socket;
1894 struct hlua *hlua = hlua_gethlua(L);
1895 struct appctx *appctx;
1896 size_t buf_len;
1897 const char *buf;
1898 int len;
1899 int send_len;
1900 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001901 struct xref *peer;
1902 struct stream_interface *si;
1903 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904
1905 /* Check if this lua stack is schedulable. */
1906 if (!hlua || !hlua->task)
1907 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1908 "'frontend', 'backend' or 'task'"));
1909
1910 /* Get object */
1911 socket = MAY_LJMP(hlua_checksocket(L, 1));
1912 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001913 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001914
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001915 /* Check if we run on the same thread than the xreator thread.
1916 * We cannot access to the socket if the thread is different.
1917 */
1918 if (socket->tid != tid)
1919 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1920
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001921 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001922 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001923 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001924 lua_pushinteger(L, -1);
1925 return 1;
1926 }
1927 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1928 si = appctx->owner;
1929 s = si_strm(si);
1930
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001931 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001932 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001933 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001934 lua_pushinteger(L, -1);
1935 return 1;
1936 }
1937
1938 /* Update the input buffer data. */
1939 buf += sent;
1940 send_len = buf_len - sent;
1941
1942 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001943 if (sent >= buf_len) {
1944 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001945 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001946 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001947
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001948 /* Check if the buffer is avalaible because HAProxy doesn't allocate
1949 * the request buffer if its not required.
1950 */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001951 if (s->req.buf->size == 0) {
Christopher Faulet33834b12016-12-19 09:29:06 +01001952 appctx = hlua->task->context;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001953 if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01001954 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001955 }
1956
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001957 /* Check for avalaible space. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001958 len = buffer_total_space(s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01001959 if (len <= 0) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001960 appctx = objt_appctx(s->si[0].end);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001961 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
1962 xref_unlock(&socket->xref, peer);
Christopher Faulet33834b12016-12-19 09:29:06 +01001963 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001964 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001965 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01001966 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001967
1968 /* send data */
1969 if (len < send_len)
1970 send_len = len;
Willy Tarreau06d80a92017-10-19 14:32:15 +02001971 len = ci_putblk(&s->req, buf+sent, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001972
1973 /* "Not enough space" (-1), "Buffer too little to contain
1974 * the data" (-2) are not expected because the available length
1975 * is tested.
1976 * Other unknown error are also not expected.
1977 */
1978 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01001979 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001980 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001981
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001982 MAY_LJMP(hlua_socket_close(L));
1983 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001984 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001985 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001986 return 1;
1987 }
1988
1989 /* update buffers. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001990 stream_int_notify(&s->si[0]);
1991 stream_int_update_applet(&s->si[0]);
Willy Tarreaude70fa12015-09-26 11:25:05 +02001992
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001993 s->req.rex = TICK_ETERNITY;
1994 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001995
1996 /* Update length sent. */
1997 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001998 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001999
2000 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002001 if (sent + len >= buf_len) {
2002 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002003 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002004 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002005
2006hlua_socket_write_yield_return:
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002007 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002008 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002009 return 0;
2010}
2011
2012/* This function initiate the send of data. It just check the input
2013 * parameters and push an integer in the Lua stack that contain the
2014 * amount of data writed in the buffer. This is used by the function
2015 * "hlua_socket_write_yield" that can yield.
2016 *
2017 * The Lua function gets between 3 and 4 parameters. The first one is
2018 * the associated object. The second is a string buffer. The third is
2019 * a facultative integer that represents where is the buffer position
2020 * of the start of the data that can send. The first byte is the
2021 * position "1". The default value is "1". The fourth argument is a
2022 * facultative integer that represents where is the buffer position
2023 * of the end of the data that can send. The default is the last byte.
2024 */
2025static int hlua_socket_send(struct lua_State *L)
2026{
2027 int i;
2028 int j;
2029 const char *buf;
2030 size_t buf_len;
2031
2032 /* Check number of arguments. */
2033 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2034 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2035
2036 /* Get the string. */
2037 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2038
2039 /* Get and check j. */
2040 if (lua_gettop(L) == 4) {
2041 j = MAY_LJMP(luaL_checkinteger(L, 4));
2042 if (j < 0)
2043 j = buf_len + j + 1;
2044 if (j > buf_len)
2045 j = buf_len + 1;
2046 lua_pop(L, 1);
2047 }
2048 else
2049 j = buf_len;
2050
2051 /* Get and check i. */
2052 if (lua_gettop(L) == 3) {
2053 i = MAY_LJMP(luaL_checkinteger(L, 3));
2054 if (i < 0)
2055 i = buf_len + i + 1;
2056 if (i > buf_len)
2057 i = buf_len + 1;
2058 lua_pop(L, 1);
2059 } else
2060 i = 1;
2061
2062 /* Check bth i and j. */
2063 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002064 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002065 return 1;
2066 }
2067 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002068 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002069 return 1;
2070 }
2071 if (i == 0)
2072 i = 1;
2073 if (j == 0)
2074 j = 1;
2075
2076 /* Pop the string. */
2077 lua_pop(L, 1);
2078
2079 /* Update the buffer length. */
2080 buf += i - 1;
2081 buf_len = j - i + 1;
2082 lua_pushlstring(L, buf, buf_len);
2083
2084 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002085 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002086
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002087 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002088}
2089
Willy Tarreau22b0a682015-06-17 19:43:49 +02002090#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002091__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2092{
2093 static char buffer[SOCKET_INFO_MAX_LEN];
2094 int ret;
2095 int len;
2096 char *p;
2097
2098 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2099 if (ret <= 0) {
2100 lua_pushnil(L);
2101 return 1;
2102 }
2103
2104 if (ret == AF_UNIX) {
2105 lua_pushstring(L, buffer+1);
2106 return 1;
2107 }
2108 else if (ret == AF_INET6) {
2109 buffer[0] = '[';
2110 len = strlen(buffer);
2111 buffer[len] = ']';
2112 len++;
2113 buffer[len] = ':';
2114 len++;
2115 p = buffer;
2116 }
2117 else if (ret == AF_INET) {
2118 p = buffer + 1;
2119 len = strlen(p);
2120 p[len] = ':';
2121 len++;
2122 }
2123 else {
2124 lua_pushnil(L);
2125 return 1;
2126 }
2127
2128 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2129 lua_pushnil(L);
2130 return 1;
2131 }
2132
2133 lua_pushstring(L, p);
2134 return 1;
2135}
2136
2137/* Returns information about the peer of the connection. */
2138__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2139{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002140 struct hlua_socket *socket;
2141 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002142 struct xref *peer;
2143 struct appctx *appctx;
2144 struct stream_interface *si;
2145 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002146 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002147
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002148 MAY_LJMP(check_args(L, 1, "getpeername"));
2149
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002150 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002151
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002152 /* Check if we run on the same thread than the xreator thread.
2153 * We cannot access to the socket if the thread is different.
2154 */
2155 if (socket->tid != tid)
2156 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2157
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002158 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002159 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002160 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002161 lua_pushnil(L);
2162 return 1;
2163 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002164 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2165 si = appctx->owner;
2166 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002167
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002168 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002169 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002170 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002171 lua_pushnil(L);
2172 return 1;
2173 }
2174
Willy Tarreaua71f6422016-11-16 17:00:14 +01002175 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002176 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002177 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002178 lua_pushnil(L);
2179 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002180 }
2181
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002182 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2183 xref_unlock(&socket->xref, peer);
2184 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002185}
2186
2187/* Returns information about my connection side. */
2188static int hlua_socket_getsockname(struct lua_State *L)
2189{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002190 struct hlua_socket *socket;
2191 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002192 struct appctx *appctx;
2193 struct xref *peer;
2194 struct stream_interface *si;
2195 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002196 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002197
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002198 MAY_LJMP(check_args(L, 1, "getsockname"));
2199
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002200 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002201
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002202 /* Check if we run on the same thread than the xreator thread.
2203 * We cannot access to the socket if the thread is different.
2204 */
2205 if (socket->tid != tid)
2206 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2207
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002208 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002209 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002210 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002211 lua_pushnil(L);
2212 return 1;
2213 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002214 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2215 si = appctx->owner;
2216 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002217
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002218 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002219 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002220 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002221 lua_pushnil(L);
2222 return 1;
2223 }
2224
Willy Tarreaua71f6422016-11-16 17:00:14 +01002225 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002226 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002227 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002228 lua_pushnil(L);
2229 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002230 }
2231
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002232 ret = hlua_socket_info(L, &conn->addr.from);
2233 xref_unlock(&socket->xref, peer);
2234 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002235}
2236
2237/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002238static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002239 .obj_type = OBJ_TYPE_APPLET,
2240 .name = "<LUA_TCP>",
2241 .fct = hlua_socket_handler,
2242 .release = hlua_socket_release,
2243};
2244
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002245__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002246{
2247 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2248 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002249 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002250 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002251 struct stream_interface *si;
2252 struct stream *s;
2253
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002254 /* Check if we run on the same thread than the xreator thread.
2255 * We cannot access to the socket if the thread is different.
2256 */
2257 if (socket->tid != tid)
2258 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2259
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002260 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002261 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002262 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002263 lua_pushnil(L);
2264 lua_pushstring(L, "Can't connect");
2265 return 2;
2266 }
2267 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2268 si = appctx->owner;
2269 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002270
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002271 /* Check if we run on the same thread than the xreator thread.
2272 * We cannot access to the socket if the thread is different.
2273 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002274 if (socket->tid != tid) {
2275 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002276 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002277 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002278
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002279 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002280 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002281 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002282 lua_pushnil(L);
2283 lua_pushstring(L, "Can't connect");
2284 return 2;
2285 }
2286
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002287 appctx = objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002288
2289 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002290 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002291 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002292 lua_pushinteger(L, 1);
2293 return 1;
2294 }
2295
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002296 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2297 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002298 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002299 }
2300 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002301 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002302 return 0;
2303}
2304
2305/* This function fail or initite the connection. */
2306__LJMP static int hlua_socket_connect(struct lua_State *L)
2307{
2308 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002309 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002310 const char *ip;
2311 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002312 struct hlua *hlua;
2313 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002314 int low, high;
2315 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002316 struct xref *peer;
2317 struct stream_interface *si;
2318 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002319
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002320 if (lua_gettop(L) < 2)
2321 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002322
2323 /* Get args. */
2324 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002325
2326 /* Check if we run on the same thread than the xreator thread.
2327 * We cannot access to the socket if the thread is different.
2328 */
2329 if (socket->tid != tid)
2330 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2331
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002332 ip = MAY_LJMP(luaL_checkstring(L, 2));
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002333 if (lua_gettop(L) >= 3)
2334 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002335
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002336 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002337 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002338 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002339 lua_pushnil(L);
2340 return 1;
2341 }
2342 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2343 si = appctx->owner;
2344 s = si_strm(si);
2345
2346 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002347 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002348 if (!conn) {
2349 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002351 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002352
Willy Tarreau3adac082015-09-26 17:51:09 +02002353 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002354 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002355
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002356 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002357 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002358 if (!addr) {
2359 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002360 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002361 }
2362 if (low != high) {
2363 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002364 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002365 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002366 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002367
2368 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002369 if (low == 0) {
2370 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002371 if (port == -1) {
2372 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002373 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002374 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002375 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2376 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002377 if (port == -1) {
2378 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002379 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002380 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002381 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2382 }
2383 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002384
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002385 hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002386 appctx = objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002387
2388 /* inform the stream that we want to be notified whenever the
2389 * connection completes.
2390 */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002391 si_applet_cant_get(&s->si[0]);
2392 si_applet_cant_put(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002393 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002394
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002395 hlua->flags |= HLUA_MUST_GC;
2396
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002397 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2398 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002399 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002400 }
2401 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002402 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002403
2404 return 0;
2405}
2406
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002407#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002408__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2409{
2410 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002411 struct xref *peer;
2412 struct appctx *appctx;
2413 struct stream_interface *si;
2414 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002415
2416 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2417 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002418
2419 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002420 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002421 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002422 lua_pushnil(L);
2423 return 1;
2424 }
2425 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2426 si = appctx->owner;
2427 s = si_strm(si);
2428
2429 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002430 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002431 return MAY_LJMP(hlua_socket_connect(L));
2432}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002433#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002434
2435__LJMP static int hlua_socket_setoption(struct lua_State *L)
2436{
2437 return 0;
2438}
2439
2440__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2441{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002442 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002443 int tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002444 struct xref *peer;
2445 struct appctx *appctx;
2446 struct stream_interface *si;
2447 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002448
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002449 MAY_LJMP(check_args(L, 2, "settimeout"));
2450
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002451 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002452 tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002453
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002454 /* Check if we run on the same thread than the xreator thread.
2455 * We cannot access to the socket if the thread is different.
2456 */
2457 if (socket->tid != tid)
2458 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2459
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002460 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002461 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002462 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002463 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2464 WILL_LJMP(lua_error(L));
2465 return 0;
2466 }
2467 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2468 si = appctx->owner;
2469 s = si_strm(si);
2470
2471 s->req.rto = tmout;
2472 s->req.wto = tmout;
2473 s->res.rto = tmout;
2474 s->res.wto = tmout;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002475 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002476
2477 return 0;
2478}
2479
2480__LJMP static int hlua_socket_new(lua_State *L)
2481{
2482 struct hlua_socket *socket;
2483 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002484 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002485 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002486
2487 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002488 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002489 hlua_pusherror(L, "socket: full stack");
2490 goto out_fail_conf;
2491 }
2492
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002493 /* Create the object: obj[0] = userdata. */
2494 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002495 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002496 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002497 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002498 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002499
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002500 /* Check if the various memory pools are intialized. */
Willy Tarreau87b09662015-04-03 00:22:06 +02002501 if (!pool2_stream || !pool2_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002502 hlua_pusherror(L, "socket: uninitialized pools.");
2503 goto out_fail_conf;
2504 }
2505
Willy Tarreau87b09662015-04-03 00:22:06 +02002506 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002507 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2508 lua_setmetatable(L, -2);
2509
Willy Tarreaud420a972015-04-06 00:39:18 +02002510 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002511 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002512 if (!appctx) {
2513 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002514 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002515 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002516
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002517 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002518 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002519 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2520 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002521
Willy Tarreaud420a972015-04-06 00:39:18 +02002522 /* Now create a session, task and stream for this applet */
2523 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2524 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002525 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002526 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002527 }
2528
Willy Tarreau87787ac2017-08-28 16:22:54 +02002529 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002530 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002531 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002532 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002533 }
2534
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002535 /* Initialise cross reference between stream and Lua socket object. */
2536 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002537
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002538 /* Configure "right" stream interface. this "si" is used to connect
2539 * and retrieve data from the server. The connection is initialized
2540 * with the "struct server".
2541 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002542 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002543
2544 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002545 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2546 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002547
Willy Tarreau87787ac2017-08-28 16:22:54 +02002548 task_wakeup(strm->task, TASK_WOKEN_INIT);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002549 /* Return yield waiting for connection. */
2550 return 1;
2551
Willy Tarreaud420a972015-04-06 00:39:18 +02002552 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002553 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002554 out_fail_sess:
2555 appctx_free(appctx);
2556 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002557 WILL_LJMP(lua_error(L));
2558 return 0;
2559}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002560
2561/*
2562 *
2563 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002564 * Class Channel
2565 *
2566 *
2567 */
2568
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002569/* The state between the channel data and the HTTP parser state can be
2570 * unconsistent, so reset the parser and call it again. Warning, this
2571 * action not revalidate the request and not send a 400 if the modified
2572 * resuest is not valid.
2573 *
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002574 * This function never fails. The direction is set using dir, which equals
2575 * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002576 */
2577static void hlua_resynchonize_proto(struct stream *stream, int dir)
2578{
2579 /* Protocol HTTP. */
2580 if (stream->be->mode == PR_MODE_HTTP) {
2581
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002582 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002583 http_txn_reset_req(stream->txn);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002584 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002585 http_txn_reset_res(stream->txn);
2586
2587 if (stream->txn->hdr_idx.v)
2588 hdr_idx_init(&stream->txn->hdr_idx);
2589
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002590 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002591 http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002592 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002593 http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx);
2594 }
2595}
2596
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002597/* This function is called before the Lua execution. It stores
2598 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002599 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002600static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002601{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002602 c->mode = stream->be->mode;
2603 switch (c->mode) {
2604 case PR_MODE_HTTP:
2605 c->data.http.dir = opt & SMP_OPT_DIR;
2606 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2607 c->data.http.state = stream->txn->req.msg_state;
2608 else
2609 c->data.http.state = stream->txn->rsp.msg_state;
2610 break;
2611 default:
2612 break;
2613 }
2614}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002615
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002616/* This function is called after the Lua execution. it
2617 * returns true if the parser state is consistent, otherwise,
2618 * it return false.
2619 *
2620 * In HTTP mode, the parser state must be in the same state
2621 * or greater when we exit the function. Even if we do a
2622 * control yield. This prevent to break the HTTP message
2623 * from the Lua code.
2624 */
2625static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2626{
2627 if (c->mode != stream->be->mode)
2628 return 0;
2629
2630 switch (c->mode) {
2631 case PR_MODE_HTTP:
2632 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002633 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002634 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2635 return stream->txn->req.msg_state >= c->data.http.state;
2636 else
2637 return stream->txn->rsp.msg_state >= c->data.http.state;
2638 default:
2639 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002640 }
2641 return 1;
2642}
2643
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002644/* Returns the struct hlua_channel join to the class channel in the
2645 * stack entry "ud" or throws an argument error.
2646 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002647__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002648{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002649 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002650}
2651
Willy Tarreau47860ed2015-03-10 14:07:50 +01002652/* Pushes the channel onto the top of the stack. If the stask does not have a
2653 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002654 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002655static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002656{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002657 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002658 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002659 return 0;
2660
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002661 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002662 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002663 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002664
2665 /* Pop a class sesison metatable and affect it to the userdata. */
2666 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2667 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002668 return 1;
2669}
2670
2671/* Duplicate all the data present in the input channel and put it
2672 * in a string LUA variables. Returns -1 and push a nil value in
2673 * the stack if the channel is closed and all the data are consumed,
2674 * returns 0 if no data are available, otherwise it returns the length
2675 * of the builded string.
2676 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002677static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002678{
2679 char *blk1;
2680 char *blk2;
2681 int len1;
2682 int len2;
2683 int ret;
2684 luaL_Buffer b;
2685
Willy Tarreau06d80a92017-10-19 14:32:15 +02002686 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002687 if (unlikely(ret == 0))
2688 return 0;
2689
2690 if (unlikely(ret < 0)) {
2691 lua_pushnil(L);
2692 return -1;
2693 }
2694
2695 luaL_buffinit(L, &b);
2696 luaL_addlstring(&b, blk1, len1);
2697 if (unlikely(ret == 2))
2698 luaL_addlstring(&b, blk2, len2);
2699 luaL_pushresult(&b);
2700
2701 if (unlikely(ret == 2))
2702 return len1 + len2;
2703 return len1;
2704}
2705
2706/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2707 * a yield. This function keep the data in the buffer.
2708 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002709__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002710{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002711 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002712
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002713 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2714
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002715 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002716 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002717 return 1;
2718}
2719
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002720/* Check arguments for the function "hlua_channel_dup_yield". */
2721__LJMP static int hlua_channel_dup(lua_State *L)
2722{
2723 MAY_LJMP(check_args(L, 1, "dup"));
2724 MAY_LJMP(hlua_checkchannel(L, 1));
2725 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2726}
2727
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002728/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2729 * a yield. This function consumes the data in the buffer. It returns
2730 * a string containing the data or a nil pointer if no data are available
2731 * and the channel is closed.
2732 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002733__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002734{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002735 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002736 int ret;
2737
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002738 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002739
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002740 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002741 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002742 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002743
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002744 if (unlikely(ret == -1))
2745 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002746
Willy Tarreau47860ed2015-03-10 14:07:50 +01002747 chn->buf->i -= ret;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002748 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002749 return 1;
2750}
2751
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002752/* Check arguments for the fucntion "hlua_channel_get_yield". */
2753__LJMP static int hlua_channel_get(lua_State *L)
2754{
2755 MAY_LJMP(check_args(L, 1, "get"));
2756 MAY_LJMP(hlua_checkchannel(L, 1));
2757 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2758}
2759
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002760/* This functions consumes and returns one line. If the channel is closed,
2761 * and the last data does not contains a final '\n', the data are returned
2762 * without the final '\n'. When no more data are avalaible, it returns nil
2763 * value.
2764 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002765__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002766{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002767 char *blk1;
2768 char *blk2;
2769 int len1;
2770 int len2;
2771 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002772 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002773 int ret;
2774 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002775
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002776 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2777
Willy Tarreau06d80a92017-10-19 14:32:15 +02002778 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002779 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002780 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002781
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002782 if (ret == -1) {
2783 lua_pushnil(L);
2784 return 1;
2785 }
2786
2787 luaL_buffinit(L, &b);
2788 luaL_addlstring(&b, blk1, len1);
2789 len = len1;
2790 if (unlikely(ret == 2)) {
2791 luaL_addlstring(&b, blk2, len2);
2792 len += len2;
2793 }
2794 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002795 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002796 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002797 return 1;
2798}
2799
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002800/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2801__LJMP static int hlua_channel_getline(lua_State *L)
2802{
2803 MAY_LJMP(check_args(L, 1, "getline"));
2804 MAY_LJMP(hlua_checkchannel(L, 1));
2805 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2806}
2807
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002808/* This function takes a string as input, and append it at the
2809 * input side of channel. If the data is too big, but a space
2810 * is probably available after sending some data, the function
2811 * yield. If the data is bigger than the buffer, or if the
2812 * channel is closed, it returns -1. otherwise, it returns the
2813 * amount of data writed.
2814 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002815__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002816{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002817 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002818 size_t len;
2819 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2820 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2821 int ret;
2822 int max;
2823
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002824 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2825 * the request buffer if its not required.
2826 */
2827 if (chn->buf->size == 0) {
2828 si_applet_cant_put(chn_prod(chn));
2829 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
2830 }
2831
Willy Tarreau47860ed2015-03-10 14:07:50 +01002832 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002833 if (max > len - l)
2834 max = len - l;
2835
Willy Tarreau06d80a92017-10-19 14:32:15 +02002836 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002837 if (ret == -2 || ret == -3) {
2838 lua_pushinteger(L, -1);
2839 return 1;
2840 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002841 if (ret == -1) {
2842 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002843 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002844 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002845 l += ret;
2846 lua_pop(L, 1);
2847 lua_pushinteger(L, l);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002848 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002849
Willy Tarreau47860ed2015-03-10 14:07:50 +01002850 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2851 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002852 /* There are no space avalaible, and the output buffer is empty.
2853 * in this case, we cannot add more data, so we cannot yield,
2854 * we return the amount of copyied data.
2855 */
2856 return 1;
2857 }
2858 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002859 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002860 return 1;
2861}
2862
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002863/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002864 * of the writed string, or -1 if the channel is closed or if the
2865 * buffer size is too little for the data.
2866 */
2867__LJMP static int hlua_channel_append(lua_State *L)
2868{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002869 size_t len;
2870
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002871 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002872 MAY_LJMP(hlua_checkchannel(L, 1));
2873 MAY_LJMP(luaL_checklstring(L, 2, &len));
2874 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002875 lua_pushinteger(L, 0);
2876
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002877 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002878}
2879
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002880/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002881 * his process by cleaning the buffer. The result is a replacement
2882 * of the current data. It returns the length of the writed string,
2883 * or -1 if the channel is closed or if the buffer size is too
2884 * little for the data.
2885 */
2886__LJMP static int hlua_channel_set(lua_State *L)
2887{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002888 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002889
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002890 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002891 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002892 lua_pushinteger(L, 0);
2893
Willy Tarreau47860ed2015-03-10 14:07:50 +01002894 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002895
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002896 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002897}
2898
2899/* Append data in the output side of the buffer. This data is immediatly
2900 * sent. The fcuntion returns the ammount of data writed. If the buffer
2901 * cannot contains the data, the function yield. The function returns -1
2902 * if the channel is closed.
2903 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002904__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002905{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002906 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002907 size_t len;
2908 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2909 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2910 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002911 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912
Willy Tarreau47860ed2015-03-10 14:07:50 +01002913 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002914 lua_pushinteger(L, -1);
2915 return 1;
2916 }
2917
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002918 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2919 * the request buffer if its not required.
2920 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002921 if (chn->buf->size == 0) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002922 si_applet_cant_put(chn_prod(chn));
2923 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002924 }
2925
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002926 /* the writed data will be immediatly sent, so we can check
2927 * the avalaible space without taking in account the reserve.
2928 * The reserve is guaranted for the processing of incoming
2929 * data, because the buffer will be flushed.
2930 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002931 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002932
2933 /* If there are no space avalaible, and the output buffer is empty.
2934 * in this case, we cannot add more data, so we cannot yield,
2935 * we return the amount of copyied data.
2936 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002937 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002938 return 1;
2939
2940 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002941 if (max > len - l)
2942 max = len - l;
2943
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002944 /* The buffer avalaible size may be not contiguous. This test
2945 * detects a non contiguous buffer and realign it.
2946 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002947 if (bi_space_for_replace(chn->buf) < max)
2948 buffer_slow_realign(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002949
2950 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002951 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002952
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002953 /* buffer replace considers that the input part is filled.
2954 * so, I must forward these new data in the output part.
2955 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002956 b_adv(chn->buf, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002957
2958 l += max;
2959 lua_pop(L, 1);
2960 lua_pushinteger(L, l);
2961
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002962 /* If there are no space avalaible, and the output buffer is empty.
2963 * in this case, we cannot add more data, so we cannot yield,
2964 * we return the amount of copyied data.
2965 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002966 max = chn->buf->size - buffer_len(chn->buf);
2967 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002968 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002969
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002970 if (l < len) {
2971 /* If we are waiting for space in the response buffer, we
2972 * must set the flag WAKERESWR. This flag required the task
2973 * wake up if any activity is detected on the response buffer.
2974 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002975 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002976 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002977 else
2978 HLUA_SET_WAKEREQWR(hlua);
2979 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002980 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002981
2982 return 1;
2983}
2984
2985/* Just a wraper of "_hlua_channel_send". This wrapper permits
2986 * yield the LUA process, and resume it without checking the
2987 * input arguments.
2988 */
2989__LJMP static int hlua_channel_send(lua_State *L)
2990{
2991 MAY_LJMP(check_args(L, 2, "send"));
2992 lua_pushinteger(L, 0);
2993
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002994 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002995}
2996
2997/* This function forward and amount of butes. The data pass from
2998 * the input side of the buffer to the output side, and can be
2999 * forwarded. This function never fails.
3000 *
3001 * The Lua function takes an amount of bytes to be forwarded in
3002 * imput. It returns the number of bytes forwarded.
3003 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003004__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003005{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003006 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003007 int len;
3008 int l;
3009 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003010 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003011
3012 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3013 len = MAY_LJMP(luaL_checkinteger(L, 2));
3014 l = MAY_LJMP(luaL_checkinteger(L, -1));
3015
3016 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003017 if (max > chn->buf->i)
3018 max = chn->buf->i;
3019 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003020 l += max;
3021
3022 lua_pop(L, 1);
3023 lua_pushinteger(L, l);
3024
3025 /* Check if it miss bytes to forward. */
3026 if (l < len) {
3027 /* The the input channel or the output channel are closed, we
3028 * must return the amount of data forwarded.
3029 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003030 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003031 return 1;
3032
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003033 /* If we are waiting for space data in the response buffer, we
3034 * must set the flag WAKERESWR. This flag required the task
3035 * wake up if any activity is detected on the response buffer.
3036 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003037 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003038 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003039 else
3040 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003041
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003042 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01003043 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003044 }
3045
3046 return 1;
3047}
3048
3049/* Just check the input and prepare the stack for the previous
3050 * function "hlua_channel_forward_yield"
3051 */
3052__LJMP static int hlua_channel_forward(lua_State *L)
3053{
3054 MAY_LJMP(check_args(L, 2, "forward"));
3055 MAY_LJMP(hlua_checkchannel(L, 1));
3056 MAY_LJMP(luaL_checkinteger(L, 2));
3057
3058 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003059 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003060}
3061
3062/* Just returns the number of bytes available in the input
3063 * side of the buffer. This function never fails.
3064 */
3065__LJMP static int hlua_channel_get_in_len(lua_State *L)
3066{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003067 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003068
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003069 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003070 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01003071 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003072 return 1;
3073}
3074
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003075/* Returns true if the channel is full. */
3076__LJMP static int hlua_channel_is_full(lua_State *L)
3077{
3078 struct channel *chn;
3079 int rem;
3080
3081 MAY_LJMP(check_args(L, 1, "is_full"));
3082 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3083
3084 rem = chn->buf->size;
3085 rem -= chn->buf->o; /* Output size */
3086 rem -= chn->buf->i; /* Input size */
3087 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3088
3089 lua_pushboolean(L, rem <= 0);
3090 return 1;
3091}
3092
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003093/* Just returns the number of bytes available in the output
3094 * side of the buffer. This function never fails.
3095 */
3096__LJMP static int hlua_channel_get_out_len(lua_State *L)
3097{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003098 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003099
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003100 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003101 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01003102 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003103 return 1;
3104}
3105
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003106/*
3107 *
3108 *
3109 * Class Fetches
3110 *
3111 *
3112 */
3113
3114/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003115 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003116 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003117__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003118{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003119 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003120}
3121
3122/* This function creates and push in the stack a fetch object according
3123 * with a current TXN.
3124 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003125static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003126{
Willy Tarreau7073c472015-04-06 11:15:40 +02003127 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003128
3129 /* Check stack size. */
3130 if (!lua_checkstack(L, 3))
3131 return 0;
3132
3133 /* Create the object: obj[0] = userdata.
3134 * Note that the base of the Fetches object is the
3135 * transaction object.
3136 */
3137 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003138 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003139 lua_rawseti(L, -2, 0);
3140
Willy Tarreau7073c472015-04-06 11:15:40 +02003141 hsmp->s = txn->s;
3142 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003143 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003144 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003145
3146 /* Pop a class sesison metatable and affect it to the userdata. */
3147 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3148 lua_setmetatable(L, -2);
3149
3150 return 1;
3151}
3152
3153/* This function is an LUA binding. It is called with each sample-fetch.
3154 * It uses closure argument to store the associated sample-fetch. It
3155 * returns only one argument or throws an error. An error is thrown
3156 * only if an error is encountered during the argument parsing. If
3157 * the "sample-fetch" function fails, nil is returned.
3158 */
3159__LJMP static int hlua_run_sample_fetch(lua_State *L)
3160{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003161 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003162 struct sample_fetch *f;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003163 struct arg args[ARGM_NBARGS + 1];
3164 int i;
3165 struct sample smp;
3166
3167 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003168 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003169
3170 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003171 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003172
Thierry FOURNIERca988662015-12-20 18:43:03 +01003173 /* Check execution authorization. */
3174 if (f->use & SMP_USE_HTTP_ANY &&
3175 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3176 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3177 "is not available in Lua services", f->kw);
3178 WILL_LJMP(lua_error(L));
3179 }
3180
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003181 /* Get extra arguments. */
3182 for (i = 0; i < lua_gettop(L) - 1; i++) {
3183 if (i >= ARGM_NBARGS)
3184 break;
3185 hlua_lua2arg(L, i + 2, &args[i]);
3186 }
3187 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003188 args[i].data.str.str = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003189
3190 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003191 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003192
3193 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003194 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003195 lua_pushfstring(L, "error in arguments");
3196 WILL_LJMP(lua_error(L));
3197 }
3198
3199 /* Initialise the sample. */
3200 memset(&smp, 0, sizeof(smp));
3201
3202 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003203 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003204 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003205 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003206 lua_pushstring(L, "");
3207 else
3208 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003209 return 1;
3210 }
3211
3212 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003213 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003214 hlua_smp2lua_str(L, &smp);
3215 else
3216 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003217 return 1;
3218}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003219
3220/*
3221 *
3222 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003223 * Class Converters
3224 *
3225 *
3226 */
3227
3228/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003229 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003230 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003231__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003232{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003233 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003234}
3235
3236/* This function creates and push in the stack a Converters object
3237 * according with a current TXN.
3238 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003239static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003240{
Willy Tarreau7073c472015-04-06 11:15:40 +02003241 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003242
3243 /* Check stack size. */
3244 if (!lua_checkstack(L, 3))
3245 return 0;
3246
3247 /* Create the object: obj[0] = userdata.
3248 * Note that the base of the Converters object is the
3249 * same than the TXN object.
3250 */
3251 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003252 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003253 lua_rawseti(L, -2, 0);
3254
Willy Tarreau7073c472015-04-06 11:15:40 +02003255 hsmp->s = txn->s;
3256 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003257 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003258 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003259
Willy Tarreau87b09662015-04-03 00:22:06 +02003260 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003261 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3262 lua_setmetatable(L, -2);
3263
3264 return 1;
3265}
3266
3267/* This function is an LUA binding. It is called with each converter.
3268 * It uses closure argument to store the associated converter. It
3269 * returns only one argument or throws an error. An error is thrown
3270 * only if an error is encountered during the argument parsing. If
3271 * the converter function function fails, nil is returned.
3272 */
3273__LJMP static int hlua_run_sample_conv(lua_State *L)
3274{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003275 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003276 struct sample_conv *conv;
3277 struct arg args[ARGM_NBARGS + 1];
3278 int i;
3279 struct sample smp;
3280
3281 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003282 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003283
3284 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003285 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003286
3287 /* Get extra arguments. */
3288 for (i = 0; i < lua_gettop(L) - 2; i++) {
3289 if (i >= ARGM_NBARGS)
3290 break;
3291 hlua_lua2arg(L, i + 3, &args[i]);
3292 }
3293 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003294 args[i].data.str.str = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003295
3296 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003297 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003298
3299 /* Run the special args checker. */
3300 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3301 hlua_pusherror(L, "error in arguments");
3302 WILL_LJMP(lua_error(L));
3303 }
3304
3305 /* Initialise the sample. */
3306 if (!hlua_lua2smp(L, 2, &smp)) {
3307 hlua_pusherror(L, "error in the input argument");
3308 WILL_LJMP(lua_error(L));
3309 }
3310
Willy Tarreau1777ea62016-03-10 16:15:46 +01003311 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3312
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003313 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003314 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003315 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003316 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003317 WILL_LJMP(lua_error(L));
3318 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003319 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3320 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003321 hlua_pusherror(L, "error during the input argument casting");
3322 WILL_LJMP(lua_error(L));
3323 }
3324
3325 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003326 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003327 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003328 lua_pushstring(L, "");
3329 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003330 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003331 return 1;
3332 }
3333
3334 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003335 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003336 hlua_smp2lua_str(L, &smp);
3337 else
3338 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003339 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003340}
3341
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003342/*
3343 *
3344 *
3345 * Class AppletTCP
3346 *
3347 *
3348 */
3349
3350/* Returns a struct hlua_txn if the stack entry "ud" is
3351 * a class stream, otherwise it throws an error.
3352 */
3353__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3354{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003355 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003356}
3357
3358/* This function creates and push in the stack an Applet object
3359 * according with a current TXN.
3360 */
3361static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3362{
3363 struct hlua_appctx *appctx;
3364 struct stream_interface *si = ctx->owner;
3365 struct stream *s = si_strm(si);
3366 struct proxy *p = s->be;
3367
3368 /* Check stack size. */
3369 if (!lua_checkstack(L, 3))
3370 return 0;
3371
3372 /* Create the object: obj[0] = userdata.
3373 * Note that the base of the Converters object is the
3374 * same than the TXN object.
3375 */
3376 lua_newtable(L);
3377 appctx = lua_newuserdata(L, sizeof(*appctx));
3378 lua_rawseti(L, -2, 0);
3379 appctx->appctx = ctx;
3380 appctx->htxn.s = s;
3381 appctx->htxn.p = p;
3382
3383 /* Create the "f" field that contains a list of fetches. */
3384 lua_pushstring(L, "f");
3385 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3386 return 0;
3387 lua_settable(L, -3);
3388
3389 /* Create the "sf" field that contains a list of stringsafe fetches. */
3390 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003391 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003392 return 0;
3393 lua_settable(L, -3);
3394
3395 /* Create the "c" field that contains a list of converters. */
3396 lua_pushstring(L, "c");
3397 if (!hlua_converters_new(L, &appctx->htxn, 0))
3398 return 0;
3399 lua_settable(L, -3);
3400
3401 /* Create the "sc" field that contains a list of stringsafe converters. */
3402 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003403 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003404 return 0;
3405 lua_settable(L, -3);
3406
3407 /* Pop a class stream metatable and affect it to the table. */
3408 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3409 lua_setmetatable(L, -2);
3410
3411 return 1;
3412}
3413
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003414__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3415{
3416 struct hlua_appctx *appctx;
3417 struct stream *s;
3418 const char *name;
3419 size_t len;
3420 struct sample smp;
3421
3422 MAY_LJMP(check_args(L, 3, "set_var"));
3423
3424 /* It is useles to retrieve the stream, but this function
3425 * runs only in a stream context.
3426 */
3427 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3428 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3429 s = appctx->htxn.s;
3430
3431 /* Converts the third argument in a sample. */
3432 hlua_lua2smp(L, 3, &smp);
3433
3434 /* Store the sample in a variable. */
3435 smp_set_owner(&smp, s->be, s->sess, s, 0);
3436 vars_set_by_name(name, len, &smp);
3437 return 0;
3438}
3439
3440__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3441{
3442 struct hlua_appctx *appctx;
3443 struct stream *s;
3444 const char *name;
3445 size_t len;
3446 struct sample smp;
3447
3448 MAY_LJMP(check_args(L, 2, "unset_var"));
3449
3450 /* It is useles to retrieve the stream, but this function
3451 * runs only in a stream context.
3452 */
3453 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3454 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3455 s = appctx->htxn.s;
3456
3457 /* Unset the variable. */
3458 smp_set_owner(&smp, s->be, s->sess, s, 0);
3459 vars_unset_by_name(name, len, &smp);
3460 return 0;
3461}
3462
3463__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3464{
3465 struct hlua_appctx *appctx;
3466 struct stream *s;
3467 const char *name;
3468 size_t len;
3469 struct sample smp;
3470
3471 MAY_LJMP(check_args(L, 2, "get_var"));
3472
3473 /* It is useles to retrieve the stream, but this function
3474 * runs only in a stream context.
3475 */
3476 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3477 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3478 s = appctx->htxn.s;
3479
3480 smp_set_owner(&smp, s->be, s->sess, s, 0);
3481 if (!vars_get_by_name(name, len, &smp)) {
3482 lua_pushnil(L);
3483 return 1;
3484 }
3485
3486 return hlua_smp2lua(L, &smp);
3487}
3488
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003489__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3490{
3491 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3492 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003493 struct hlua *hlua;
3494
3495 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003496 if (!s->hlua)
3497 return 0;
3498 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003499
3500 MAY_LJMP(check_args(L, 2, "set_priv"));
3501
3502 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003503 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003504
3505 /* Get and store new value. */
3506 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3507 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3508
3509 return 0;
3510}
3511
3512__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3513{
3514 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3515 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003516 struct hlua *hlua;
3517
3518 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003519 if (!s->hlua) {
3520 lua_pushnil(L);
3521 return 1;
3522 }
3523 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003524
3525 /* Push configuration index in the stack. */
3526 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3527
3528 return 1;
3529}
3530
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003531/* If expected data not yet available, it returns a yield. This function
3532 * consumes the data in the buffer. It returns a string containing the
3533 * data. This string can be empty.
3534 */
3535__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3536{
3537 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3538 struct stream_interface *si = appctx->appctx->owner;
3539 int ret;
3540 char *blk1;
3541 int len1;
3542 char *blk2;
3543 int len2;
3544
3545 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003546 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003547
3548 /* Data not yet avalaible. return yield. */
3549 if (ret == 0) {
3550 si_applet_cant_get(si);
3551 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
3552 }
3553
3554 /* End of data: commit the total strings and return. */
3555 if (ret < 0) {
3556 luaL_pushresult(&appctx->b);
3557 return 1;
3558 }
3559
3560 /* Ensure that the block 2 length is usable. */
3561 if (ret == 1)
3562 len2 = 0;
3563
3564 /* dont check the max length read and dont check. */
3565 luaL_addlstring(&appctx->b, blk1, len1);
3566 luaL_addlstring(&appctx->b, blk2, len2);
3567
3568 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003569 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003570 luaL_pushresult(&appctx->b);
3571 return 1;
3572}
3573
3574/* Check arguments for the fucntion "hlua_channel_get_yield". */
3575__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3576{
3577 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3578
3579 /* Initialise the string catenation. */
3580 luaL_buffinit(L, &appctx->b);
3581
3582 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3583}
3584
3585/* If expected data not yet available, it returns a yield. This function
3586 * consumes the data in the buffer. It returns a string containing the
3587 * data. This string can be empty.
3588 */
3589__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3590{
3591 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3592 struct stream_interface *si = appctx->appctx->owner;
3593 int len = MAY_LJMP(luaL_checkinteger(L, 2));
3594 int ret;
3595 char *blk1;
3596 int len1;
3597 char *blk2;
3598 int len2;
3599
3600 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003601 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003602
3603 /* Data not yet avalaible. return yield. */
3604 if (ret == 0) {
3605 si_applet_cant_get(si);
3606 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3607 }
3608
3609 /* End of data: commit the total strings and return. */
3610 if (ret < 0) {
3611 luaL_pushresult(&appctx->b);
3612 return 1;
3613 }
3614
3615 /* Ensure that the block 2 length is usable. */
3616 if (ret == 1)
3617 len2 = 0;
3618
3619 if (len == -1) {
3620
3621 /* If len == -1, catenate all the data avalaile and
3622 * yield because we want to get all the data until
3623 * the end of data stream.
3624 */
3625 luaL_addlstring(&appctx->b, blk1, len1);
3626 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003627 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003628 si_applet_cant_get(si);
3629 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3630
3631 } else {
3632
3633 /* Copy the fisrt block caping to the length required. */
3634 if (len1 > len)
3635 len1 = len;
3636 luaL_addlstring(&appctx->b, blk1, len1);
3637 len -= len1;
3638
3639 /* Copy the second block. */
3640 if (len2 > len)
3641 len2 = len;
3642 luaL_addlstring(&appctx->b, blk2, len2);
3643 len -= len2;
3644
3645 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003646 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003647
3648 /* If we are no other data avalaible, yield waiting for new data. */
3649 if (len > 0) {
3650 lua_pushinteger(L, len);
3651 lua_replace(L, 2);
3652 si_applet_cant_get(si);
3653 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3654 }
3655
3656 /* return the result. */
3657 luaL_pushresult(&appctx->b);
3658 return 1;
3659 }
3660
3661 /* we never executes this */
3662 hlua_pusherror(L, "Lua: internal error");
3663 WILL_LJMP(lua_error(L));
3664 return 0;
3665}
3666
3667/* Check arguments for the fucntion "hlua_channel_get_yield". */
3668__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3669{
3670 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3671 int len = -1;
3672
3673 if (lua_gettop(L) > 2)
3674 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3675 if (lua_gettop(L) >= 2) {
3676 len = MAY_LJMP(luaL_checkinteger(L, 2));
3677 lua_pop(L, 1);
3678 }
3679
3680 /* Confirm or set the required length */
3681 lua_pushinteger(L, len);
3682
3683 /* Initialise the string catenation. */
3684 luaL_buffinit(L, &appctx->b);
3685
3686 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3687}
3688
3689/* Append data in the output side of the buffer. This data is immediatly
3690 * sent. The fcuntion returns the ammount of data writed. If the buffer
3691 * cannot contains the data, the function yield. The function returns -1
3692 * if the channel is closed.
3693 */
3694__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3695{
3696 size_t len;
3697 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3698 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3699 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3700 struct stream_interface *si = appctx->appctx->owner;
3701 struct channel *chn = si_ic(si);
3702 int max;
3703
3704 /* Get the max amount of data which can write as input in the channel. */
3705 max = channel_recv_max(chn);
3706 if (max > (len - l))
3707 max = len - l;
3708
3709 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003710 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003711
3712 /* update counters. */
3713 l += max;
3714 lua_pop(L, 1);
3715 lua_pushinteger(L, l);
3716
3717 /* If some data is not send, declares the situation to the
3718 * applet, and returns a yield.
3719 */
3720 if (l < len) {
3721 si_applet_cant_put(si);
3722 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
3723 }
3724
3725 return 1;
3726}
3727
3728/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3729 * yield the LUA process, and resume it without checking the
3730 * input arguments.
3731 */
3732__LJMP static int hlua_applet_tcp_send(lua_State *L)
3733{
3734 MAY_LJMP(check_args(L, 2, "send"));
3735 lua_pushinteger(L, 0);
3736
3737 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3738}
3739
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003740/*
3741 *
3742 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003743 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003744 *
3745 *
3746 */
3747
3748/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003749 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003750 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003751__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003752{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003753 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003754}
3755
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003756/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003757 * according with a current TXN.
3758 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003759static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003760{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003761 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003762 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003763 struct stream_interface *si = ctx->owner;
3764 struct stream *s = si_strm(si);
3765 struct proxy *px = s->be;
3766 struct http_txn *txn = s->txn;
3767 const char *path;
3768 const char *end;
3769 const char *p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003770
3771 /* Check stack size. */
3772 if (!lua_checkstack(L, 3))
3773 return 0;
3774
3775 /* Create the object: obj[0] = userdata.
3776 * Note that the base of the Converters object is the
3777 * same than the TXN object.
3778 */
3779 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003780 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003781 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003782 appctx->appctx = ctx;
3783 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003784 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003785 appctx->htxn.s = s;
3786 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003787
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003788 /* Create the "f" field that contains a list of fetches. */
3789 lua_pushstring(L, "f");
3790 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3791 return 0;
3792 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003793
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003794 /* Create the "sf" field that contains a list of stringsafe fetches. */
3795 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003796 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003797 return 0;
3798 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003799
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003800 /* Create the "c" field that contains a list of converters. */
3801 lua_pushstring(L, "c");
3802 if (!hlua_converters_new(L, &appctx->htxn, 0))
3803 return 0;
3804 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003805
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003806 /* Create the "sc" field that contains a list of stringsafe converters. */
3807 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003808 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003809 return 0;
3810 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003811
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003812 /* Stores the request method. */
3813 lua_pushstring(L, "method");
3814 lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l);
3815 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003816
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003817 /* Stores the http version. */
3818 lua_pushstring(L, "version");
3819 lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
3820 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003821
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003822 /* creates an array of headers. hlua_http_get_headers() crates and push
3823 * the array on the top of the stack.
3824 */
3825 lua_pushstring(L, "headers");
3826 htxn.s = s;
3827 htxn.p = px;
3828 htxn.dir = SMP_OPT_DIR_REQ;
3829 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3830 return 0;
3831 lua_settable(L, -3);
3832
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003833 /* Get path and qs */
3834 path = http_get_path(txn);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003835 if (path) {
3836 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
3837 p = path;
3838 while (p < end && *p != '?')
3839 p++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003840
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003841 /* Stores the request path. */
3842 lua_pushstring(L, "path");
3843 lua_pushlstring(L, path, p - path);
3844 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003845
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003846 /* Stores the query string. */
3847 lua_pushstring(L, "qs");
3848 if (*p == '?')
3849 p++;
3850 lua_pushlstring(L, p, end - p);
3851 lua_settable(L, -3);
3852 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003853
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003854 /* Stores the request path. */
3855 lua_pushstring(L, "length");
3856 lua_pushinteger(L, txn->req.body_len);
3857 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003858
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003859 /* Create an array of HTTP request headers. */
3860 lua_pushstring(L, "headers");
3861 MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req));
3862 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003863
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003864 /* Create an empty array of HTTP request headers. */
3865 lua_pushstring(L, "response");
3866 lua_newtable(L);
3867 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003868
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003869 /* Pop a class stream metatable and affect it to the table. */
3870 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3871 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003872
3873 return 1;
3874}
3875
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003876__LJMP static int hlua_applet_http_set_var(lua_State *L)
3877{
3878 struct hlua_appctx *appctx;
3879 struct stream *s;
3880 const char *name;
3881 size_t len;
3882 struct sample smp;
3883
3884 MAY_LJMP(check_args(L, 3, "set_var"));
3885
3886 /* It is useles to retrieve the stream, but this function
3887 * runs only in a stream context.
3888 */
3889 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3890 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3891 s = appctx->htxn.s;
3892
3893 /* Converts the third argument in a sample. */
3894 hlua_lua2smp(L, 3, &smp);
3895
3896 /* Store the sample in a variable. */
3897 smp_set_owner(&smp, s->be, s->sess, s, 0);
3898 vars_set_by_name(name, len, &smp);
3899 return 0;
3900}
3901
3902__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3903{
3904 struct hlua_appctx *appctx;
3905 struct stream *s;
3906 const char *name;
3907 size_t len;
3908 struct sample smp;
3909
3910 MAY_LJMP(check_args(L, 2, "unset_var"));
3911
3912 /* It is useles to retrieve the stream, but this function
3913 * runs only in a stream context.
3914 */
3915 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3916 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3917 s = appctx->htxn.s;
3918
3919 /* Unset the variable. */
3920 smp_set_owner(&smp, s->be, s->sess, s, 0);
3921 vars_unset_by_name(name, len, &smp);
3922 return 0;
3923}
3924
3925__LJMP static int hlua_applet_http_get_var(lua_State *L)
3926{
3927 struct hlua_appctx *appctx;
3928 struct stream *s;
3929 const char *name;
3930 size_t len;
3931 struct sample smp;
3932
3933 MAY_LJMP(check_args(L, 2, "get_var"));
3934
3935 /* It is useles to retrieve the stream, but this function
3936 * runs only in a stream context.
3937 */
3938 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3939 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3940 s = appctx->htxn.s;
3941
3942 smp_set_owner(&smp, s->be, s->sess, s, 0);
3943 if (!vars_get_by_name(name, len, &smp)) {
3944 lua_pushnil(L);
3945 return 1;
3946 }
3947
3948 return hlua_smp2lua(L, &smp);
3949}
3950
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003951__LJMP static int hlua_applet_http_set_priv(lua_State *L)
3952{
3953 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3954 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003955 struct hlua *hlua;
3956
3957 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003958 if (!s->hlua)
3959 return 0;
3960 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003961
3962 MAY_LJMP(check_args(L, 2, "set_priv"));
3963
3964 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003965 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003966
3967 /* Get and store new value. */
3968 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3969 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3970
3971 return 0;
3972}
3973
3974__LJMP static int hlua_applet_http_get_priv(lua_State *L)
3975{
3976 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3977 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003978 struct hlua *hlua;
3979
3980 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003981 if (!s->hlua) {
3982 lua_pushnil(L);
3983 return 1;
3984 }
3985 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003986
3987 /* Push configuration index in the stack. */
3988 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3989
3990 return 1;
3991}
3992
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003993/* If expected data not yet available, it returns a yield. This function
3994 * consumes the data in the buffer. It returns a string containing the
3995 * data. This string can be empty.
3996 */
3997__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003998{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003999 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4000 struct stream_interface *si = appctx->appctx->owner;
4001 struct channel *chn = si_ic(si);
4002 int ret;
4003 char *blk1;
4004 int len1;
4005 char *blk2;
4006 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004007
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004008 /* Maybe we cant send a 100-continue ? */
4009 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004010 ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004011 /* if ret == -2 or -3 the channel closed or the message si too
4012 * big for the buffers. We cant send anything. So, we ignoring
4013 * the error, considers that the 100-continue is sent, and try
4014 * to receive.
4015 * If ret is -1, we dont have room in the buffer, so we yield.
4016 */
4017 if (ret == -1) {
4018 si_applet_cant_put(si);
4019 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
4020 }
4021 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4022 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004023
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004024 /* Check for the end of the data. */
4025 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4026 luaL_pushresult(&appctx->b);
4027 return 1;
4028 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004029
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004030 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004031 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004032
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004033 /* Data not yet avalaible. return yield. */
4034 if (ret == 0) {
4035 si_applet_cant_get(si);
4036 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
4037 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004038
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004039 /* End of data: commit the total strings and return. */
4040 if (ret < 0) {
4041 luaL_pushresult(&appctx->b);
4042 return 1;
4043 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004044
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004045 /* Ensure that the block 2 length is usable. */
4046 if (ret == 1)
4047 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004048
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004049 /* Copy the fisrt block caping to the length required. */
4050 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4051 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4052 luaL_addlstring(&appctx->b, blk1, len1);
4053 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004054
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004055 /* Copy the second block. */
4056 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4057 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4058 luaL_addlstring(&appctx->b, blk2, len2);
4059 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004060
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004061 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004062 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004063 luaL_pushresult(&appctx->b);
4064 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004065}
4066
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004067/* Check arguments for the fucntion "hlua_channel_get_yield". */
4068__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004069{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004070 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004071
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004072 /* Initialise the string catenation. */
4073 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004074
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004075 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004076}
4077
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004078/* If expected data not yet available, it returns a yield. This function
4079 * consumes the data in the buffer. It returns a string containing the
4080 * data. This string can be empty.
4081 */
4082__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004083{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004084 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4085 struct stream_interface *si = appctx->appctx->owner;
4086 int len = MAY_LJMP(luaL_checkinteger(L, 2));
4087 struct channel *chn = si_ic(si);
4088 int ret;
4089 char *blk1;
4090 int len1;
4091 char *blk2;
4092 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004093
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004094 /* Maybe we cant send a 100-continue ? */
4095 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004096 ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004097 /* if ret == -2 or -3 the channel closed or the message si too
4098 * big for the buffers. We cant send anything. So, we ignoring
4099 * the error, considers that the 100-continue is sent, and try
4100 * to receive.
4101 * If ret is -1, we dont have room in the buffer, so we yield.
4102 */
4103 if (ret == -1) {
4104 si_applet_cant_put(si);
4105 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4106 }
4107 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4108 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004109
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004110 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004111 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004112
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004113 /* Data not yet avalaible. return yield. */
4114 if (ret == 0) {
4115 si_applet_cant_get(si);
4116 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4117 }
4118
4119 /* End of data: commit the total strings and return. */
4120 if (ret < 0) {
4121 luaL_pushresult(&appctx->b);
4122 return 1;
4123 }
4124
4125 /* Ensure that the block 2 length is usable. */
4126 if (ret == 1)
4127 len2 = 0;
4128
4129 /* Copy the fisrt block caping to the length required. */
4130 if (len1 > len)
4131 len1 = len;
4132 luaL_addlstring(&appctx->b, blk1, len1);
4133 len -= len1;
4134
4135 /* Copy the second block. */
4136 if (len2 > len)
4137 len2 = len;
4138 luaL_addlstring(&appctx->b, blk2, len2);
4139 len -= len2;
4140
4141 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004142 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004143 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4144 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4145
4146 /* If we are no other data avalaible, yield waiting for new data. */
4147 if (len > 0) {
4148 lua_pushinteger(L, len);
4149 lua_replace(L, 2);
4150 si_applet_cant_get(si);
4151 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4152 }
4153
4154 /* return the result. */
4155 luaL_pushresult(&appctx->b);
4156 return 1;
4157}
4158
4159/* Check arguments for the fucntion "hlua_channel_get_yield". */
4160__LJMP static int hlua_applet_http_recv(lua_State *L)
4161{
4162 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4163 int len = -1;
4164
4165 /* Check arguments. */
4166 if (lua_gettop(L) > 2)
4167 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4168 if (lua_gettop(L) >= 2) {
4169 len = MAY_LJMP(luaL_checkinteger(L, 2));
4170 lua_pop(L, 1);
4171 }
4172
4173 /* Check the required length */
4174 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4175 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4176 lua_pushinteger(L, len);
4177
4178 /* Initialise the string catenation. */
4179 luaL_buffinit(L, &appctx->b);
4180
4181 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4182}
4183
4184/* Append data in the output side of the buffer. This data is immediatly
4185 * sent. The fcuntion returns the ammount of data writed. If the buffer
4186 * cannot contains the data, the function yield. The function returns -1
4187 * if the channel is closed.
4188 */
4189__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4190{
4191 size_t len;
4192 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4193 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4194 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4195 struct stream_interface *si = appctx->appctx->owner;
4196 struct channel *chn = si_ic(si);
4197 int max;
4198
4199 /* Get the max amount of data which can write as input in the channel. */
4200 max = channel_recv_max(chn);
4201 if (max > (len - l))
4202 max = len - l;
4203
4204 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004205 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004206
4207 /* update counters. */
4208 l += max;
4209 lua_pop(L, 1);
4210 lua_pushinteger(L, l);
4211
4212 /* If some data is not send, declares the situation to the
4213 * applet, and returns a yield.
4214 */
4215 if (l < len) {
4216 si_applet_cant_put(si);
4217 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
4218 }
4219
4220 return 1;
4221}
4222
4223/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4224 * yield the LUA process, and resume it without checking the
4225 * input arguments.
4226 */
4227__LJMP static int hlua_applet_http_send(lua_State *L)
4228{
4229 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4230 size_t len;
4231 char hex[10];
4232
4233 MAY_LJMP(luaL_checklstring(L, 2, &len));
4234
4235 /* If transfer encoding chunked is selected, we surround the data
4236 * by chunk data.
4237 */
4238 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4239 snprintf(hex, 9, "%x", (unsigned int)len);
4240 lua_pushfstring(L, "%s\r\n", hex);
4241 lua_insert(L, 2); /* swap the last 2 entries. */
4242 lua_pushstring(L, "\r\n");
4243 lua_concat(L, 3);
4244 }
4245
4246 /* This interger is used for followinf the amount of data sent. */
4247 lua_pushinteger(L, 0);
4248
4249 /* We want to send some data. Headers must be sent. */
4250 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4251 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4252 WILL_LJMP(lua_error(L));
4253 }
4254
4255 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4256}
4257
4258__LJMP static int hlua_applet_http_addheader(lua_State *L)
4259{
4260 const char *name;
4261 int ret;
4262
4263 MAY_LJMP(hlua_checkapplet_http(L, 1));
4264 name = MAY_LJMP(luaL_checkstring(L, 2));
4265 MAY_LJMP(luaL_checkstring(L, 3));
4266
4267 /* Push in the stack the "response" entry. */
4268 ret = lua_getfield(L, 1, "response");
4269 if (ret != LUA_TTABLE) {
4270 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4271 "is expected as an array. %s found", lua_typename(L, ret));
4272 WILL_LJMP(lua_error(L));
4273 }
4274
4275 /* check if the header is already registered if it is not
4276 * the case, register it.
4277 */
4278 ret = lua_getfield(L, -1, name);
4279 if (ret == LUA_TNIL) {
4280
4281 /* Entry not found. */
4282 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4283
4284 /* Insert the new header name in the array in the top of the stack.
4285 * It left the new array in the top of the stack.
4286 */
4287 lua_newtable(L);
4288 lua_pushvalue(L, 2);
4289 lua_pushvalue(L, -2);
4290 lua_settable(L, -4);
4291
4292 } else if (ret != LUA_TTABLE) {
4293
4294 /* corruption error. */
4295 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4296 "is expected as an array. %s found", name, lua_typename(L, ret));
4297 WILL_LJMP(lua_error(L));
4298 }
4299
4300 /* Now the top od thestack is an array of values. We push
4301 * the header value as new entry.
4302 */
4303 lua_pushvalue(L, 3);
4304 ret = lua_rawlen(L, -2);
4305 lua_rawseti(L, -2, ret + 1);
4306 lua_pushboolean(L, 1);
4307 return 1;
4308}
4309
4310__LJMP static int hlua_applet_http_status(lua_State *L)
4311{
4312 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4313 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004314 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004315
4316 if (status < 100 || status > 599) {
4317 lua_pushboolean(L, 0);
4318 return 1;
4319 }
4320
4321 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004322 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004323 lua_pushboolean(L, 1);
4324 return 1;
4325}
4326
4327/* We will build the status line and the headers of the HTTP response.
4328 * We will try send at once if its not possible, we give back the hand
4329 * waiting for more room.
4330 */
4331__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4332{
4333 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4334 struct stream_interface *si = appctx->appctx->owner;
4335 struct channel *chn = si_ic(si);
4336 int ret;
4337 size_t len;
4338 const char *msg;
4339
4340 /* Get the message as the first argument on the stack. */
4341 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4342
4343 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004344 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004345
4346 /* if ret == -2 or -3 the channel closed or the message si too
4347 * big for the buffers.
4348 */
4349 if (ret == -2 || ret == -3) {
4350 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4351 WILL_LJMP(lua_error(L));
4352 }
4353
4354 /* If ret is -1, we dont have room in the buffer, so we yield. */
4355 if (ret == -1) {
4356 si_applet_cant_put(si);
4357 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
4358 }
4359
4360 /* Headers sent, set the flag. */
4361 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4362 return 0;
4363}
4364
4365__LJMP static int hlua_applet_http_start_response(lua_State *L)
4366{
4367 struct chunk *tmp = get_trash_chunk();
4368 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004369 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02004370 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004371 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02004372 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004373 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004374 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004375 int hdr_chunked = 0;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004376 const char *reason = appctx->appctx->ctx.hlua_apphttp.reason;
4377
4378 if (reason == NULL)
4379 reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004380
4381 /* Use the same http version than the request. */
4382 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004383 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004384 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004385 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004386
4387 /* Get the array associated to the field "response" in the object AppletHTTP. */
4388 lua_pushvalue(L, 0);
4389 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4390 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4391 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4392 WILL_LJMP(lua_error(L));
4393 }
4394
4395 /* Browse the list of headers. */
4396 lua_pushnil(L);
4397 while(lua_next(L, -2) != 0) {
4398
4399 /* We expect a string as -2. */
4400 if (lua_type(L, -2) != LUA_TSTRING) {
4401 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4402 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4403 lua_typename(L, lua_type(L, -2)));
4404 WILL_LJMP(lua_error(L));
4405 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004406 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004407
4408 /* We expect an array as -1. */
4409 if (lua_type(L, -1) != LUA_TTABLE) {
4410 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4411 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4412 name,
4413 lua_typename(L, lua_type(L, -1)));
4414 WILL_LJMP(lua_error(L));
4415 }
4416
4417 /* Browse the table who is on the top of the stack. */
4418 lua_pushnil(L);
4419 while(lua_next(L, -2) != 0) {
4420
4421 /* We expect a number as -2. */
4422 if (lua_type(L, -2) != LUA_TNUMBER) {
4423 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4424 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4425 name,
4426 lua_typename(L, lua_type(L, -2)));
4427 WILL_LJMP(lua_error(L));
4428 }
4429 id = lua_tointeger(L, -2);
4430
4431 /* We expect a string as -2. */
4432 if (lua_type(L, -1) != LUA_TSTRING) {
4433 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4434 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4435 name, id,
4436 lua_typename(L, lua_type(L, -1)));
4437 WILL_LJMP(lua_error(L));
4438 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004439 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004440
4441 /* Catenate a new header. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004442 if (tmp->len + name_len + 2 + value_len + 2 < tmp->size) {
4443 memcpy(tmp->str + tmp->len, name, name_len);
4444 tmp->len += name_len;
4445 tmp->str[tmp->len++] = ':';
4446 tmp->str[tmp->len++] = ' ';
4447
4448 memcpy(tmp->str + tmp->len, value, value_len);
4449 tmp->len += value_len;
4450 tmp->str[tmp->len++] = '\r';
4451 tmp->str[tmp->len++] = '\n';
4452 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004453
4454 /* Protocol checks. */
4455
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004456 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004457 * is done without control. If it contains a bad value,
4458 * the content-length remains negative so that we can
4459 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004460 */
Willy Tarreaua3294632017-08-23 11:24:47 +02004461 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004462 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004463
4464 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004465 if (name_len == 17 && value_len == 7 &&
4466 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004467 strcasecmp("chunked", value) == 0)
4468 hdr_chunked = 1;
4469
4470 /* Remove the array from the stack, and get next element with a remaining string. */
4471 lua_pop(L, 1);
4472 }
4473
4474 /* Remove the array from the stack, and get next element with a remaining string. */
4475 lua_pop(L, 1);
4476 }
4477
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004478 /* If we dont have a content-length set, and the HTTP version is 1.1
4479 * and the status code implies the presence of a message body, we must
4480 * announce a transfer encoding chunked. This is required by haproxy
4481 * for the keepalive compliance. If the applet annouces a transfer-encoding
4482 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004483 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004484 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004485 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
4486 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4487 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4488 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004489 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
4490 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
4491 }
4492
4493 /* Finalize headers. */
4494 chunk_appendf(tmp, "\r\n");
4495
4496 /* Remove the last entry and the array of headers */
4497 lua_pop(L, 2);
4498
4499 /* Push the headers block. */
4500 lua_pushlstring(L, tmp->str, tmp->len);
4501
4502 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
4503}
4504
4505/*
4506 *
4507 *
4508 * Class HTTP
4509 *
4510 *
4511 */
4512
4513/* Returns a struct hlua_txn if the stack entry "ud" is
4514 * a class stream, otherwise it throws an error.
4515 */
4516__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
4517{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004518 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004519}
4520
4521/* This function creates and push in the stack a HTTP object
4522 * according with a current TXN.
4523 */
4524static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4525{
4526 struct hlua_txn *htxn;
4527
4528 /* Check stack size. */
4529 if (!lua_checkstack(L, 3))
4530 return 0;
4531
4532 /* Create the object: obj[0] = userdata.
4533 * Note that the base of the Converters object is the
4534 * same than the TXN object.
4535 */
4536 lua_newtable(L);
4537 htxn = lua_newuserdata(L, sizeof(*htxn));
4538 lua_rawseti(L, -2, 0);
4539
4540 htxn->s = txn->s;
4541 htxn->p = txn->p;
4542
4543 /* Pop a class stream metatable and affect it to the table. */
4544 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4545 lua_setmetatable(L, -2);
4546
4547 return 1;
4548}
4549
4550/* This function creates ans returns an array of HTTP headers.
4551 * This function does not fails. It is used as wrapper with the
4552 * 2 following functions.
4553 */
4554__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4555{
4556 const char *cur_ptr, *cur_next, *p;
4557 int old_idx, cur_idx;
4558 struct hdr_idx_elem *cur_hdr;
4559 const char *hn, *hv;
4560 int hnl, hvl;
4561 int type;
4562 const char *in;
4563 char *out;
4564 int len;
4565
4566 /* Create the table. */
4567 lua_newtable(L);
4568
4569 if (!htxn->s->txn)
4570 return 1;
4571
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004572 /* Check if a valid response is parsed */
4573 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4574 return 1;
4575
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004576 /* Build array of headers. */
4577 old_idx = 0;
4578 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
4579
4580 while (1) {
4581 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
4582 if (!cur_idx)
4583 break;
4584 old_idx = cur_idx;
4585
4586 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
4587 cur_ptr = cur_next;
4588 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
4589
4590 /* Now we have one full header at cur_ptr of len cur_hdr->len,
4591 * and the next header starts at cur_next. We'll check
4592 * this header in the list as well as against the default
4593 * rule.
4594 */
4595
4596 /* look for ': *'. */
4597 hn = cur_ptr;
4598 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
4599 if (p >= cur_ptr+cur_hdr->len)
4600 continue;
4601 hnl = p - hn;
4602 p++;
4603 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
4604 p++;
4605 if (p >= cur_ptr+cur_hdr->len)
4606 continue;
4607 hv = p;
4608 hvl = cur_ptr+cur_hdr->len-p;
4609
4610 /* Lowercase the key. Don't check the size of trash, it have
4611 * the size of one buffer and the input data contains in one
4612 * buffer.
4613 */
4614 out = trash.str;
4615 for (in=hn; in<hn+hnl; in++, out++)
4616 *out = tolower(*in);
4617 *out = '\0';
4618
4619 /* Check for existing entry:
4620 * assume that the table is on the top of the stack, and
4621 * push the key in the stack, the function lua_gettable()
4622 * perform the lookup.
4623 */
4624 lua_pushlstring(L, trash.str, hnl);
4625 lua_gettable(L, -2);
4626 type = lua_type(L, -1);
4627
4628 switch (type) {
4629 case LUA_TNIL:
4630 /* Table not found, create it. */
4631 lua_pop(L, 1); /* remove the nil value. */
4632 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
4633 lua_newtable(L); /* create and push empty table. */
4634 lua_pushlstring(L, hv, hvl); /* push header value. */
4635 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4636 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4637 break;
4638
4639 case LUA_TTABLE:
4640 /* Entry found: push the value in the table. */
4641 len = lua_rawlen(L, -1);
4642 lua_pushlstring(L, hv, hvl); /* push header value. */
4643 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4644 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4645 break;
4646
4647 default:
4648 /* Other cases are errors. */
4649 hlua_pusherror(L, "internal error during the parsing of headers.");
4650 WILL_LJMP(lua_error(L));
4651 }
4652 }
4653
4654 return 1;
4655}
4656
4657__LJMP static int hlua_http_req_get_headers(lua_State *L)
4658{
4659 struct hlua_txn *htxn;
4660
4661 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4662 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4663
4664 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4665}
4666
4667__LJMP static int hlua_http_res_get_headers(lua_State *L)
4668{
4669 struct hlua_txn *htxn;
4670
4671 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4672 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4673
4674 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4675}
4676
4677/* This function replace full header, or just a value in
4678 * the request or in the response. It is a wrapper fir the
4679 * 4 following functions.
4680 */
4681__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4682 struct http_msg *msg, int action)
4683{
4684 size_t name_len;
4685 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4686 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4687 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
4688 struct my_regex re;
4689
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004690 /* Check if a valid response is parsed */
4691 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4692 return 0;
4693
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004694 if (!regex_comp(reg, &re, 1, 1, NULL))
4695 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4696
4697 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
4698 regex_free(&re);
4699 return 0;
4700}
4701
4702__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4703{
4704 struct hlua_txn *htxn;
4705
4706 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4707 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4708
4709 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4710}
4711
4712__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4713{
4714 struct hlua_txn *htxn;
4715
4716 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4717 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4718
4719 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4720}
4721
4722__LJMP static int hlua_http_req_rep_val(lua_State *L)
4723{
4724 struct hlua_txn *htxn;
4725
4726 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4727 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4728
4729 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4730}
4731
4732__LJMP static int hlua_http_res_rep_val(lua_State *L)
4733{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004734 struct hlua_txn *htxn;
4735
4736 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4737 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4738
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004739 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004740}
4741
4742/* This function deletes all the occurences of an header.
4743 * It is a wrapper for the 2 following functions.
4744 */
4745__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4746{
4747 size_t len;
4748 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4749 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004750 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004751
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004752 /* Check if a valid response is parsed */
4753 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4754 return 0;
4755
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004756 ctx.idx = 0;
4757 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
4758 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4759 return 0;
4760}
4761
4762__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4763{
4764 struct hlua_txn *htxn;
4765
4766 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4767 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4768
Willy Tarreaueee5b512015-04-03 23:46:31 +02004769 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004770}
4771
4772__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4773{
4774 struct hlua_txn *htxn;
4775
4776 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4777 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4778
Willy Tarreaueee5b512015-04-03 23:46:31 +02004779 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004780}
4781
4782/* This function adds an header. It is a wrapper used by
4783 * the 2 following functions.
4784 */
4785__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4786{
4787 size_t name_len;
4788 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4789 size_t value_len;
4790 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
4791 char *p;
4792
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004793 /* Check if a valid message is parsed */
4794 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4795 return 0;
4796
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004797 /* Check length. */
4798 trash.len = value_len + name_len + 2;
4799 if (trash.len > trash.size)
4800 return 0;
4801
4802 /* Creates the header string. */
4803 p = trash.str;
4804 memcpy(p, name, name_len);
4805 p += name_len;
4806 *p = ':';
4807 p++;
4808 *p = ' ';
4809 p++;
4810 memcpy(p, value, value_len);
4811
Willy Tarreaueee5b512015-04-03 23:46:31 +02004812 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004813 trash.str, trash.len) != 0);
4814
4815 return 0;
4816}
4817
4818__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4819{
4820 struct hlua_txn *htxn;
4821
4822 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4823 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4824
Willy Tarreaueee5b512015-04-03 23:46:31 +02004825 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004826}
4827
4828__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4829{
4830 struct hlua_txn *htxn;
4831
4832 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4833 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4834
Willy Tarreaueee5b512015-04-03 23:46:31 +02004835 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004836}
4837
4838static int hlua_http_req_set_hdr(lua_State *L)
4839{
4840 struct hlua_txn *htxn;
4841
4842 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4843 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4844
Willy Tarreaueee5b512015-04-03 23:46:31 +02004845 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4846 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004847}
4848
4849static int hlua_http_res_set_hdr(lua_State *L)
4850{
4851 struct hlua_txn *htxn;
4852
4853 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4854 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4855
Willy Tarreaueee5b512015-04-03 23:46:31 +02004856 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4857 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004858}
4859
4860/* This function set the method. */
4861static int hlua_http_req_set_meth(lua_State *L)
4862{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004863 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004864 size_t name_len;
4865 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004866
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004867 /* Check if a valid request is parsed */
4868 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4869 lua_pushboolean(L, 0);
4870 return 1;
4871 }
4872
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004873 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004874 return 1;
4875}
4876
4877/* This function set the method. */
4878static int hlua_http_req_set_path(lua_State *L)
4879{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004880 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004881 size_t name_len;
4882 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004883
4884 /* Check if a valid request is parsed */
4885 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4886 lua_pushboolean(L, 0);
4887 return 1;
4888 }
4889
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004890 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004891 return 1;
4892}
4893
4894/* This function set the query-string. */
4895static int hlua_http_req_set_query(lua_State *L)
4896{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004897 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004898 size_t name_len;
4899 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004900
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004901 /* Check if a valid request is parsed */
4902 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4903 lua_pushboolean(L, 0);
4904 return 1;
4905 }
4906
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004907 /* Check length. */
4908 if (name_len > trash.size - 1) {
4909 lua_pushboolean(L, 0);
4910 return 1;
4911 }
4912
4913 /* Add the mark question as prefix. */
4914 chunk_reset(&trash);
4915 trash.str[trash.len++] = '?';
4916 memcpy(trash.str + trash.len, name, name_len);
4917 trash.len += name_len;
4918
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004919 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004920 return 1;
4921}
4922
4923/* This function set the uri. */
4924static int hlua_http_req_set_uri(lua_State *L)
4925{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004926 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004927 size_t name_len;
4928 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004929
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004930 /* Check if a valid request is parsed */
4931 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4932 lua_pushboolean(L, 0);
4933 return 1;
4934 }
4935
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004936 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004937 return 1;
4938}
4939
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004940/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004941static int hlua_http_res_set_status(lua_State *L)
4942{
4943 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4944 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004945 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004946
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004947 /* Check if a valid response is parsed */
4948 if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY))
4949 return 0;
4950
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004951 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004952 return 0;
4953}
4954
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004955/*
4956 *
4957 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004958 * Class TXN
4959 *
4960 *
4961 */
4962
4963/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004964 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004965 */
4966__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
4967{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004968 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004969}
4970
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004971__LJMP static int hlua_set_var(lua_State *L)
4972{
4973 struct hlua_txn *htxn;
4974 const char *name;
4975 size_t len;
4976 struct sample smp;
4977
4978 MAY_LJMP(check_args(L, 3, "set_var"));
4979
4980 /* It is useles to retrieve the stream, but this function
4981 * runs only in a stream context.
4982 */
4983 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4984 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4985
4986 /* Converts the third argument in a sample. */
4987 hlua_lua2smp(L, 3, &smp);
4988
4989 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01004990 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01004991 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004992 return 0;
4993}
4994
Christopher Faulet85d79c92016-11-09 16:54:56 +01004995__LJMP static int hlua_unset_var(lua_State *L)
4996{
4997 struct hlua_txn *htxn;
4998 const char *name;
4999 size_t len;
5000 struct sample smp;
5001
5002 MAY_LJMP(check_args(L, 2, "unset_var"));
5003
5004 /* It is useles to retrieve the stream, but this function
5005 * runs only in a stream context.
5006 */
5007 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5008 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5009
5010 /* Unset the variable. */
5011 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5012 vars_unset_by_name(name, len, &smp);
5013 return 0;
5014}
5015
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005016__LJMP static int hlua_get_var(lua_State *L)
5017{
5018 struct hlua_txn *htxn;
5019 const char *name;
5020 size_t len;
5021 struct sample smp;
5022
5023 MAY_LJMP(check_args(L, 2, "get_var"));
5024
5025 /* It is useles to retrieve the stream, but this function
5026 * runs only in a stream context.
5027 */
5028 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5029 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5030
Willy Tarreau7560dd42016-03-10 16:28:58 +01005031 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005032 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005033 lua_pushnil(L);
5034 return 1;
5035 }
5036
5037 return hlua_smp2lua(L, &smp);
5038}
5039
Willy Tarreau59551662015-03-10 14:23:13 +01005040__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005041{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005042 struct hlua *hlua;
5043
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005044 MAY_LJMP(check_args(L, 2, "set_priv"));
5045
Willy Tarreau87b09662015-04-03 00:22:06 +02005046 /* It is useles to retrieve the stream, but this function
5047 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005048 */
5049 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005050 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005051
5052 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005053 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005054
5055 /* Get and store new value. */
5056 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5057 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5058
5059 return 0;
5060}
5061
Willy Tarreau59551662015-03-10 14:23:13 +01005062__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005063{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005064 struct hlua *hlua;
5065
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005066 MAY_LJMP(check_args(L, 1, "get_priv"));
5067
Willy Tarreau87b09662015-04-03 00:22:06 +02005068 /* It is useles to retrieve the stream, but this function
5069 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005070 */
5071 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005072 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005073
5074 /* Push configuration index in the stack. */
5075 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5076
5077 return 1;
5078}
5079
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005080/* Create stack entry containing a class TXN. This function
5081 * return 0 if the stack does not contains free slots,
5082 * otherwise it returns 1.
5083 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005084static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005085{
Willy Tarreaude491382015-04-06 11:04:28 +02005086 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005087
5088 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005089 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005090 return 0;
5091
5092 /* NOTE: The allocation never fails. The failure
5093 * throw an error, and the function never returns.
5094 * if the throw is not avalaible, the process is aborted.
5095 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005096 /* Create the object: obj[0] = userdata. */
5097 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005098 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005099 lua_rawseti(L, -2, 0);
5100
Willy Tarreaude491382015-04-06 11:04:28 +02005101 htxn->s = s;
5102 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005103 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005104 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005105
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005106 /* Create the "f" field that contains a list of fetches. */
5107 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005108 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005109 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005110 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005111
5112 /* Create the "sf" field that contains a list of stringsafe fetches. */
5113 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005114 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005115 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005116 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005117
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005118 /* Create the "c" field that contains a list of converters. */
5119 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005120 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005121 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005122 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005123
5124 /* Create the "sc" field that contains a list of stringsafe converters. */
5125 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005126 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005127 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005128 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005129
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005130 /* Create the "req" field that contains the request channel object. */
5131 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005132 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005133 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005134 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005135
5136 /* Create the "res" field that contains the response channel object. */
5137 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005138 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005139 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005140 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005141
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005142 /* Creates the HTTP object is the current proxy allows http. */
5143 lua_pushstring(L, "http");
5144 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005145 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005146 return 0;
5147 }
5148 else
5149 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005150 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005151
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005152 /* Pop a class sesison metatable and affect it to the userdata. */
5153 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5154 lua_setmetatable(L, -2);
5155
5156 return 1;
5157}
5158
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005159__LJMP static int hlua_txn_deflog(lua_State *L)
5160{
5161 const char *msg;
5162 struct hlua_txn *htxn;
5163
5164 MAY_LJMP(check_args(L, 2, "deflog"));
5165 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5166 msg = MAY_LJMP(luaL_checkstring(L, 2));
5167
5168 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5169 return 0;
5170}
5171
5172__LJMP static int hlua_txn_log(lua_State *L)
5173{
5174 int level;
5175 const char *msg;
5176 struct hlua_txn *htxn;
5177
5178 MAY_LJMP(check_args(L, 3, "log"));
5179 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5180 level = MAY_LJMP(luaL_checkinteger(L, 2));
5181 msg = MAY_LJMP(luaL_checkstring(L, 3));
5182
5183 if (level < 0 || level >= NB_LOG_LEVELS)
5184 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5185
5186 hlua_sendlog(htxn->s->be, level, msg);
5187 return 0;
5188}
5189
5190__LJMP static int hlua_txn_log_debug(lua_State *L)
5191{
5192 const char *msg;
5193 struct hlua_txn *htxn;
5194
5195 MAY_LJMP(check_args(L, 2, "Debug"));
5196 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5197 msg = MAY_LJMP(luaL_checkstring(L, 2));
5198 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5199 return 0;
5200}
5201
5202__LJMP static int hlua_txn_log_info(lua_State *L)
5203{
5204 const char *msg;
5205 struct hlua_txn *htxn;
5206
5207 MAY_LJMP(check_args(L, 2, "Info"));
5208 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5209 msg = MAY_LJMP(luaL_checkstring(L, 2));
5210 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5211 return 0;
5212}
5213
5214__LJMP static int hlua_txn_log_warning(lua_State *L)
5215{
5216 const char *msg;
5217 struct hlua_txn *htxn;
5218
5219 MAY_LJMP(check_args(L, 2, "Warning"));
5220 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5221 msg = MAY_LJMP(luaL_checkstring(L, 2));
5222 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5223 return 0;
5224}
5225
5226__LJMP static int hlua_txn_log_alert(lua_State *L)
5227{
5228 const char *msg;
5229 struct hlua_txn *htxn;
5230
5231 MAY_LJMP(check_args(L, 2, "Alert"));
5232 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5233 msg = MAY_LJMP(luaL_checkstring(L, 2));
5234 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5235 return 0;
5236}
5237
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005238__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5239{
5240 struct hlua_txn *htxn;
5241 int ll;
5242
5243 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5244 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5245 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5246
5247 if (ll < 0 || ll > 7)
5248 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5249
5250 htxn->s->logs.level = ll;
5251 return 0;
5252}
5253
5254__LJMP static int hlua_txn_set_tos(lua_State *L)
5255{
5256 struct hlua_txn *htxn;
5257 struct connection *cli_conn;
5258 int tos;
5259
5260 MAY_LJMP(check_args(L, 2, "set_tos"));
5261 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5262 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5263
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005264 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005265 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005266
5267 return 0;
5268}
5269
5270__LJMP static int hlua_txn_set_mark(lua_State *L)
5271{
5272#ifdef SO_MARK
5273 struct hlua_txn *htxn;
5274 struct connection *cli_conn;
5275 int mark;
5276
5277 MAY_LJMP(check_args(L, 2, "set_mark"));
5278 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5279 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5280
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005281 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005282 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005283#endif
5284 return 0;
5285}
5286
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005287/* This function is an Lua binding that send pending data
5288 * to the client, and close the stream interface.
5289 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005290__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005291{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005292 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005293 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005294 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005295
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005296 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005297 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005298 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005299
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005300 /* If the flags NOTERM is set, we cannot terminate the http
5301 * session, so we just end the execution of the current
5302 * lua code.
5303 */
5304 if (htxn->flags & HLUA_TXN_NOTERM) {
5305 WILL_LJMP(hlua_done(L));
5306 return 0;
5307 }
5308
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005309 ic = &htxn->s->req;
5310 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005311
Willy Tarreau630ef452015-08-28 10:06:15 +02005312 if (htxn->s->txn) {
5313 /* HTTP mode, let's stay in sync with the stream */
5314 bi_fast_delete(ic->buf, htxn->s->txn->req.sov);
5315 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
5316 htxn->s->txn->req.sov = 0;
5317 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
5318 oc->analysers = AN_RES_HTTP_XFER_BODY;
5319 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
5320 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
5321
Willy Tarreau630ef452015-08-28 10:06:15 +02005322 /* Note that if we want to support keep-alive, we need
5323 * to bypass the close/shutr_now calls below, but that
5324 * may only be done if the HTTP request was already
5325 * processed and the connection header is known (ie
5326 * not during TCP rules).
5327 */
5328 }
5329
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005330 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01005331 channel_abort(ic);
5332 channel_auto_close(ic);
5333 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005334
5335 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01005336 channel_auto_read(oc);
5337 channel_auto_close(oc);
5338 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005339
Willy Tarreau0458b082015-08-28 09:40:04 +02005340 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005341
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005342 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005343 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005344 return 0;
5345}
5346
5347__LJMP static int hlua_log(lua_State *L)
5348{
5349 int level;
5350 const char *msg;
5351
5352 MAY_LJMP(check_args(L, 2, "log"));
5353 level = MAY_LJMP(luaL_checkinteger(L, 1));
5354 msg = MAY_LJMP(luaL_checkstring(L, 2));
5355
5356 if (level < 0 || level >= NB_LOG_LEVELS)
5357 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5358
5359 hlua_sendlog(NULL, level, msg);
5360 return 0;
5361}
5362
5363__LJMP static int hlua_log_debug(lua_State *L)
5364{
5365 const char *msg;
5366
5367 MAY_LJMP(check_args(L, 1, "debug"));
5368 msg = MAY_LJMP(luaL_checkstring(L, 1));
5369 hlua_sendlog(NULL, LOG_DEBUG, msg);
5370 return 0;
5371}
5372
5373__LJMP static int hlua_log_info(lua_State *L)
5374{
5375 const char *msg;
5376
5377 MAY_LJMP(check_args(L, 1, "info"));
5378 msg = MAY_LJMP(luaL_checkstring(L, 1));
5379 hlua_sendlog(NULL, LOG_INFO, msg);
5380 return 0;
5381}
5382
5383__LJMP static int hlua_log_warning(lua_State *L)
5384{
5385 const char *msg;
5386
5387 MAY_LJMP(check_args(L, 1, "warning"));
5388 msg = MAY_LJMP(luaL_checkstring(L, 1));
5389 hlua_sendlog(NULL, LOG_WARNING, msg);
5390 return 0;
5391}
5392
5393__LJMP static int hlua_log_alert(lua_State *L)
5394{
5395 const char *msg;
5396
5397 MAY_LJMP(check_args(L, 1, "alert"));
5398 msg = MAY_LJMP(luaL_checkstring(L, 1));
5399 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005400 return 0;
5401}
5402
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005403__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005404{
5405 int wakeup_ms = lua_tointeger(L, -1);
5406 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005407 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005408 return 0;
5409}
5410
5411__LJMP static int hlua_sleep(lua_State *L)
5412{
5413 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005414 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005415
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005416 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005417
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005418 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005419 wakeup_ms = tick_add(now_ms, delay);
5420 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005421
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005422 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5423 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005424}
5425
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005426__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005427{
5428 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005429 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005430
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005431 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005432
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005433 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005434 wakeup_ms = tick_add(now_ms, delay);
5435 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005436
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005437 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5438 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005439}
5440
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005441/* This functionis an LUA binding. it permits to give back
5442 * the hand at the HAProxy scheduler. It is used when the
5443 * LUA processing consumes a lot of time.
5444 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005445__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005446{
5447 return 0;
5448}
5449
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005450__LJMP static int hlua_yield(lua_State *L)
5451{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005452 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
5453 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005454}
5455
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005456/* This function change the nice of the currently executed
5457 * task. It is used set low or high priority at the current
5458 * task.
5459 */
Willy Tarreau59551662015-03-10 14:23:13 +01005460__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005461{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005462 struct hlua *hlua;
5463 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005464
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005465 MAY_LJMP(check_args(L, 1, "set_nice"));
5466 hlua = hlua_gethlua(L);
5467 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005468
5469 /* If he task is not set, I'm in a start mode. */
5470 if (!hlua || !hlua->task)
5471 return 0;
5472
5473 if (nice < -1024)
5474 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005475 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005476 nice = 1024;
5477
5478 hlua->task->nice = nice;
5479 return 0;
5480}
5481
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005482/* This function is used as a calback of a task. It is called by the
5483 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5484 * return an E_AGAIN signal, the emmiter of this signal must set a
5485 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005486 *
5487 * Task wrapper are longjmp safe because the only one Lua code
5488 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005489 */
5490static struct task *hlua_process_task(struct task *task)
5491{
5492 struct hlua *hlua = task->context;
5493 enum hlua_exec status;
5494
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005495 /* If it is the first call to the task, we must initialize the
5496 * execution timeouts.
5497 */
5498 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005499 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005500
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005501 /* Execute the Lua code. */
5502 status = hlua_ctx_resume(hlua, 1);
5503
5504 switch (status) {
5505 /* finished or yield */
5506 case HLUA_E_OK:
5507 hlua_ctx_destroy(hlua);
5508 task_delete(task);
5509 task_free(task);
5510 break;
5511
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005512 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
5513 if (hlua->wake_time != TICK_ETERNITY)
Emeric Brun253e53e2017-10-17 18:58:40 +02005514 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005515 break;
5516
5517 /* finished with error. */
5518 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005519 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005520 hlua_ctx_destroy(hlua);
5521 task_delete(task);
5522 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005523 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005524 break;
5525
5526 case HLUA_E_ERR:
5527 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005528 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005529 hlua_ctx_destroy(hlua);
5530 task_delete(task);
5531 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005532 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005533 break;
5534 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005535 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005536}
5537
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005538/* This function is an LUA binding that register LUA function to be
5539 * executed after the HAProxy configuration parsing and before the
5540 * HAProxy scheduler starts. This function expect only one LUA
5541 * argument that is a function. This function returns nothing, but
5542 * throws if an error is encountered.
5543 */
5544__LJMP static int hlua_register_init(lua_State *L)
5545{
5546 struct hlua_init_function *init;
5547 int ref;
5548
5549 MAY_LJMP(check_args(L, 1, "register_init"));
5550
5551 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5552
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005553 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005554 if (!init)
5555 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5556
5557 init->function_ref = ref;
5558 LIST_ADDQ(&hlua_init_functions, &init->l);
5559 return 0;
5560}
5561
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005562/* This functio is an LUA binding. It permits to register a task
5563 * executed in parallel of the main HAroxy activity. The task is
5564 * created and it is set in the HAProxy scheduler. It can be called
5565 * from the "init" section, "post init" or during the runtime.
5566 *
5567 * Lua prototype:
5568 *
5569 * <none> core.register_task(<function>)
5570 */
5571static int hlua_register_task(lua_State *L)
5572{
5573 struct hlua *hlua;
5574 struct task *task;
5575 int ref;
5576
5577 MAY_LJMP(check_args(L, 1, "register_task"));
5578
5579 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5580
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005581 hlua = pool_alloc2(pool2_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005582 if (!hlua)
5583 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5584
Emeric Brunc60def82017-09-27 14:59:38 +02005585 task = task_new(MAX_THREADS_MASK);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005586 task->context = hlua;
5587 task->process = hlua_process_task;
5588
5589 if (!hlua_ctx_init(hlua, task))
5590 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5591
5592 /* Restore the function in the stack. */
5593 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5594 hlua->nargs = 0;
5595
5596 /* Schedule task. */
5597 task_schedule(task, now_ms);
5598
5599 return 0;
5600}
5601
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005602/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5603 * doesn't allow "yield" functions because the HAProxy engine cannot
5604 * resume converters.
5605 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005606static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005607{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005608 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005609 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005610 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005611
Willy Tarreaube508f12016-03-10 11:47:01 +01005612 if (!stream)
5613 return 0;
5614
Willy Tarreau87b09662015-04-03 00:22:06 +02005615 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005616 * Lua context can be not initialized. This behavior
5617 * permits to save performances because a systematic
5618 * Lua initialization cause 5% performances loss.
5619 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005620 if (!stream->hlua) {
5621 stream->hlua = pool_alloc2(pool2_hlua);
5622 if (!stream->hlua) {
5623 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5624 return 0;
5625 }
5626 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5627 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5628 return 0;
5629 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005630 }
5631
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005632 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005633 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005634
5635 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005636 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5637 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5638 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005639 else
5640 error = "critical error";
5641 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005642 return 0;
5643 }
5644
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005645 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005646 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005647 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005648 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005649 return 0;
5650 }
5651
5652 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005653 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005654
5655 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005656 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005657 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005658 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005659 return 0;
5660 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005661 hlua_smp2lua(stream->hlua->T, smp);
5662 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005663
5664 /* push keywords in the stack. */
5665 if (arg_p) {
5666 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005667 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005668 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005669 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005670 return 0;
5671 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005672 hlua_arg2lua(stream->hlua->T, arg_p);
5673 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005674 }
5675 }
5676
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005677 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005678 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005679
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005680 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005681 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005682 }
5683
5684 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005685 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005686 /* finished. */
5687 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005688 /* If the stack is empty, the function fails. */
5689 if (lua_gettop(stream->hlua->T) <= 0)
5690 return 0;
5691
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005692 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005693 hlua_lua2smp(stream->hlua->T, -1, smp);
5694 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005695 return 1;
5696
5697 /* yield. */
5698 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005699 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005700 return 0;
5701
5702 /* finished with error. */
5703 case HLUA_E_ERRMSG:
5704 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005705 SEND_ERR(stream->be, "Lua converter '%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 FOURNIER9be813f2015-02-16 20:21:12 +01005708 return 0;
5709
5710 case HLUA_E_ERR:
5711 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005712 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005713
5714 default:
5715 return 0;
5716 }
5717}
5718
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005719/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5720 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005721 * resume sample-fetches. This function will be called by the sample
5722 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005723 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005724static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5725 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005726{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005727 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005728 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005729 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005730 const struct chunk msg = { .len = 0 };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005731
Willy Tarreaube508f12016-03-10 11:47:01 +01005732 if (!stream)
5733 return 0;
5734
Willy Tarreau87b09662015-04-03 00:22:06 +02005735 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005736 * Lua context can be not initialized. This behavior
5737 * permits to save performances because a systematic
5738 * Lua initialization cause 5% performances loss.
5739 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005740 if (!stream->hlua) {
5741 stream->hlua = pool_alloc2(pool2_hlua);
5742 if (!stream->hlua) {
5743 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5744 return 0;
5745 }
5746 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5747 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5748 return 0;
5749 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005750 }
5751
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005752 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005753
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005754 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005755 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005756
5757 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005758 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5759 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5760 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005761 else
5762 error = "critical error";
5763 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005764 return 0;
5765 }
5766
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005767 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005768 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005769 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005770 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005771 return 0;
5772 }
5773
5774 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005775 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005776
5777 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005778 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005779 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005780 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005781 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005782 return 0;
5783 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005784 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005785
5786 /* push keywords in the stack. */
5787 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5788 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005789 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005790 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005791 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005792 return 0;
5793 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005794 hlua_arg2lua(stream->hlua->T, arg_p);
5795 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005796 }
5797
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005798 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005799 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005800
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005801 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005802 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005803 }
5804
5805 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005806 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005807 /* finished. */
5808 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005809 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005810 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005811 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005812 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005813 /* If the stack is empty, the function fails. */
5814 if (lua_gettop(stream->hlua->T) <= 0)
5815 return 0;
5816
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005817 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005818 hlua_lua2smp(stream->hlua->T, -1, smp);
5819 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005820
5821 /* Set the end of execution flag. */
5822 smp->flags &= ~SMP_F_MAY_CHANGE;
5823 return 1;
5824
5825 /* yield. */
5826 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005827 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005828 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005829 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005830 return 0;
5831
5832 /* finished with error. */
5833 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005834 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005835 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005836 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005837 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005838 fcn->name, lua_tostring(stream->hlua->T, -1));
5839 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005840 return 0;
5841
5842 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005843 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005844 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005845 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005846 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005847
5848 default:
5849 return 0;
5850 }
5851}
5852
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005853/* This function is an LUA binding used for registering
5854 * "sample-conv" functions. It expects a converter name used
5855 * in the haproxy configuration file, and an LUA function.
5856 */
5857__LJMP static int hlua_register_converters(lua_State *L)
5858{
5859 struct sample_conv_kw_list *sck;
5860 const char *name;
5861 int ref;
5862 int len;
5863 struct hlua_function *fcn;
5864
5865 MAY_LJMP(check_args(L, 2, "register_converters"));
5866
5867 /* First argument : converter name. */
5868 name = MAY_LJMP(luaL_checkstring(L, 1));
5869
5870 /* Second argument : lua function. */
5871 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5872
5873 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005874 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005875 if (!sck)
5876 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005877 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005878 if (!fcn)
5879 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5880
5881 /* Fill fcn. */
5882 fcn->name = strdup(name);
5883 if (!fcn->name)
5884 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5885 fcn->function_ref = ref;
5886
5887 /* List head */
5888 sck->list.n = sck->list.p = NULL;
5889
5890 /* converter keyword. */
5891 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005892 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005893 if (!sck->kw[0].kw)
5894 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5895
5896 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
5897 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01005898 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 +01005899 sck->kw[0].val_args = NULL;
5900 sck->kw[0].in_type = SMP_T_STR;
5901 sck->kw[0].out_type = SMP_T_STR;
5902 sck->kw[0].private = fcn;
5903
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005904 /* Register this new converter */
5905 sample_register_convs(sck);
5906
5907 return 0;
5908}
5909
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005910/* This fucntion is an LUA binding used for registering
5911 * "sample-fetch" functions. It expects a converter name used
5912 * in the haproxy configuration file, and an LUA function.
5913 */
5914__LJMP static int hlua_register_fetches(lua_State *L)
5915{
5916 const char *name;
5917 int ref;
5918 int len;
5919 struct sample_fetch_kw_list *sfk;
5920 struct hlua_function *fcn;
5921
5922 MAY_LJMP(check_args(L, 2, "register_fetches"));
5923
5924 /* First argument : sample-fetch name. */
5925 name = MAY_LJMP(luaL_checkstring(L, 1));
5926
5927 /* Second argument : lua function. */
5928 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5929
5930 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005931 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005932 if (!sfk)
5933 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005934 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005935 if (!fcn)
5936 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5937
5938 /* Fill fcn. */
5939 fcn->name = strdup(name);
5940 if (!fcn->name)
5941 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5942 fcn->function_ref = ref;
5943
5944 /* List head */
5945 sfk->list.n = sfk->list.p = NULL;
5946
5947 /* sample-fetch keyword. */
5948 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005949 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005950 if (!sfk->kw[0].kw)
5951 return luaL_error(L, "lua out of memory error.");
5952
5953 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
5954 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01005955 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 +01005956 sfk->kw[0].val_args = NULL;
5957 sfk->kw[0].out_type = SMP_T_STR;
5958 sfk->kw[0].use = SMP_USE_HTTP_ANY;
5959 sfk->kw[0].val = 0;
5960 sfk->kw[0].private = fcn;
5961
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005962 /* Register this new fetch. */
5963 sample_register_fetches(sfk);
5964
5965 return 0;
5966}
5967
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005968/* This function is a wrapper to execute each LUA function declared
5969 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005970 * return ACT_RET_CONT if the processing is finished (with or without
5971 * error) and return ACT_RET_YIELD if the function must be called again
5972 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005973 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005974static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02005975 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005976{
5977 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005978 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005979 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005980 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005981 const struct chunk msg = { .len = 0 };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005982
5983 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01005984 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
5985 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
5986 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
5987 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005988 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005989 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005990 return ACT_RET_CONT;
5991 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005992
Willy Tarreau87b09662015-04-03 00:22:06 +02005993 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005994 * Lua context can be not initialized. This behavior
5995 * permits to save performances because a systematic
5996 * Lua initialization cause 5% performances loss.
5997 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005998 if (!s->hlua) {
5999 s->hlua = pool_alloc2(pool2_hlua);
6000 if (!s->hlua) {
6001 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6002 rule->arg.hlua_rule->fcn.name);
6003 return ACT_RET_CONT;
6004 }
6005 if (!hlua_ctx_init(s->hlua, s->task)) {
6006 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6007 rule->arg.hlua_rule->fcn.name);
6008 return ACT_RET_CONT;
6009 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006010 }
6011
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006012 consistency_set(s, dir, &s->hlua->cons);
6013
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006014 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006015 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006016
6017 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006018 if (!SET_SAFE_LJMP(s->hlua->T)) {
6019 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6020 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006021 else
6022 error = "critical error";
6023 SEND_ERR(px, "Lua function '%s': %s.\n",
6024 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006025 return ACT_RET_CONT;
6026 }
6027
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006028 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006029 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006030 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006031 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006032 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006033 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006034 }
6035
6036 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006037 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006038
Willy Tarreau87b09662015-04-03 00:22:06 +02006039 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006040 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006041 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006042 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006043 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006044 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006045 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006046 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006047
6048 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006049 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006050 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006051 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006052 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006053 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006054 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006055 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006056 lua_pushstring(s->hlua->T, *arg);
6057 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006058 }
6059
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006060 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006061 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006062
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006063 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006064 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006065 }
6066
6067 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006068 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006069 /* finished. */
6070 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006071 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006072 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006073 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006074 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006075 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006076 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006077 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006078
6079 /* yield. */
6080 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006081 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006082 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006083 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006084 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006085 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006086 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006087 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006088 /* Some actions can be wake up when a "write" event
6089 * is detected on a response channel. This is useful
6090 * only for actions targetted on the requests.
6091 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006092 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006093 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006094 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006095 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006096 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006097 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006098 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006099 /* We can quit the fcuntion without consistency check
6100 * because HAProxy is not able to manipulate data, it
6101 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006102 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006103
6104 /* finished with error. */
6105 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006106 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006107 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006108 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006109 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006110 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006111 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006112 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6113 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006114 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006115
6116 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006117 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006118 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006119 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006120 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006121 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006122 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006123 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006124
6125 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006126 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006127 }
6128}
6129
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006130struct task *hlua_applet_wakeup(struct task *t)
6131{
6132 struct appctx *ctx = t->context;
6133 struct stream_interface *si = ctx->owner;
6134
6135 /* If the applet is wake up without any expected work, the sheduler
6136 * remove it from the run queue. This flag indicate that the applet
6137 * is waiting for write. If the buffer is full, the main processing
6138 * will send some data and after call the applet, otherwise it call
6139 * the applet ASAP.
6140 */
6141 si_applet_cant_put(si);
6142 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006143 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006144 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006145}
6146
6147static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6148{
6149 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006150 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006151 struct task *task;
6152 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006153 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006154
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006155 hlua = pool_alloc2(pool2_hlua);
6156 if (!hlua) {
6157 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6158 ctx->rule->arg.hlua_rule->fcn.name);
6159 return 0;
6160 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006161 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006162 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006163 ctx->ctx.hlua_apptcp.flags = 0;
6164
6165 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006166 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006167 if (!task) {
6168 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6169 ctx->rule->arg.hlua_rule->fcn.name);
6170 return 0;
6171 }
6172 task->nice = 0;
6173 task->context = ctx;
6174 task->process = hlua_applet_wakeup;
6175 ctx->ctx.hlua_apptcp.task = task;
6176
6177 /* In the execution wrappers linked with a stream, the
6178 * Lua context can be not initialized. This behavior
6179 * permits to save performances because a systematic
6180 * Lua initialization cause 5% performances loss.
6181 */
6182 if (!hlua_ctx_init(hlua, task)) {
6183 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6184 ctx->rule->arg.hlua_rule->fcn.name);
6185 return 0;
6186 }
6187
6188 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006189 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006190
6191 /* The following Lua calls can fail. */
6192 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006193 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6194 error = lua_tostring(hlua->T, -1);
6195 else
6196 error = "critical error";
6197 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6198 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006199 RESET_SAFE_LJMP(hlua->T);
6200 return 0;
6201 }
6202
6203 /* Check stack available size. */
6204 if (!lua_checkstack(hlua->T, 1)) {
6205 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6206 ctx->rule->arg.hlua_rule->fcn.name);
6207 RESET_SAFE_LJMP(hlua->T);
6208 return 0;
6209 }
6210
6211 /* Restore the function in the stack. */
6212 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6213
6214 /* Create and and push object stream in the stack. */
6215 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6216 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6217 ctx->rule->arg.hlua_rule->fcn.name);
6218 RESET_SAFE_LJMP(hlua->T);
6219 return 0;
6220 }
6221 hlua->nargs = 1;
6222
6223 /* push keywords in the stack. */
6224 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6225 if (!lua_checkstack(hlua->T, 1)) {
6226 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6227 ctx->rule->arg.hlua_rule->fcn.name);
6228 RESET_SAFE_LJMP(hlua->T);
6229 return 0;
6230 }
6231 lua_pushstring(hlua->T, *arg);
6232 hlua->nargs++;
6233 }
6234
6235 RESET_SAFE_LJMP(hlua->T);
6236
6237 /* Wakeup the applet ASAP. */
6238 si_applet_cant_get(si);
6239 si_applet_cant_put(si);
6240
6241 return 1;
6242}
6243
6244static void hlua_applet_tcp_fct(struct appctx *ctx)
6245{
6246 struct stream_interface *si = ctx->owner;
6247 struct stream *strm = si_strm(si);
6248 struct channel *res = si_ic(si);
6249 struct act_rule *rule = ctx->rule;
6250 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006251 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006252
6253 /* The applet execution is already done. */
6254 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE)
6255 return;
6256
6257 /* If the stream is disconnect or closed, ldo nothing. */
6258 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6259 return;
6260
6261 /* Execute the function. */
6262 switch (hlua_ctx_resume(hlua, 1)) {
6263 /* finished. */
6264 case HLUA_E_OK:
6265 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6266
6267 /* log time */
6268 strm->logs.tv_request = now;
6269
6270 /* eat the whole request */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006271 co_skip(si_oc(si), si_ob(si)->o);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006272 res->flags |= CF_READ_NULL;
6273 si_shutr(si);
6274 return;
6275
6276 /* yield. */
6277 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006278 if (hlua->wake_time != TICK_ETERNITY)
6279 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006280 return;
6281
6282 /* finished with error. */
6283 case HLUA_E_ERRMSG:
6284 /* Display log. */
6285 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6286 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6287 lua_pop(hlua->T, 1);
6288 goto error;
6289
6290 case HLUA_E_ERR:
6291 /* Display log. */
6292 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6293 rule->arg.hlua_rule->fcn.name);
6294 goto error;
6295
6296 default:
6297 goto error;
6298 }
6299
6300error:
6301
6302 /* For all other cases, just close the stream. */
6303 si_shutw(si);
6304 si_shutr(si);
6305 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6306}
6307
6308static void hlua_applet_tcp_release(struct appctx *ctx)
6309{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006310 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006311 task_free(ctx->ctx.hlua_apptcp.task);
6312 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006313 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006314 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006315}
6316
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006317/* The function returns 1 if the initialisation is complete, 0 if
6318 * an errors occurs and -1 if more data are required for initializing
6319 * the applet.
6320 */
6321static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6322{
6323 struct stream_interface *si = ctx->owner;
6324 struct channel *req = si_oc(si);
6325 struct http_msg *msg;
6326 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006327 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006328 char **arg;
6329 struct hdr_ctx hdr;
6330 struct task *task;
6331 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006332 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006333
6334 /* Wait for a full HTTP request. */
6335 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6336 if (smp.flags & SMP_F_MAY_CHANGE)
6337 return -1;
6338 return 0;
6339 }
6340 txn = strm->txn;
6341 msg = &txn->req;
6342
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006343 /* We want two things in HTTP mode :
6344 * - enforce server-close mode if we were in keep-alive, so that the
6345 * applet is released after each response ;
6346 * - enable request body transfer to the applet in order to resync
6347 * with the response body.
6348 */
6349 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6350 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006351
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006352 hlua = pool_alloc2(pool2_hlua);
6353 if (!hlua) {
6354 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6355 ctx->rule->arg.hlua_rule->fcn.name);
6356 return 0;
6357 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006358 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006359 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006360 ctx->ctx.hlua_apphttp.left_bytes = -1;
6361 ctx->ctx.hlua_apphttp.flags = 0;
6362
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006363 if (txn->req.flags & HTTP_MSGF_VER_11)
6364 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6365
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006366 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006367 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006368 if (!task) {
6369 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6370 ctx->rule->arg.hlua_rule->fcn.name);
6371 return 0;
6372 }
6373 task->nice = 0;
6374 task->context = ctx;
6375 task->process = hlua_applet_wakeup;
6376 ctx->ctx.hlua_apphttp.task = task;
6377
6378 /* In the execution wrappers linked with a stream, the
6379 * Lua context can be not initialized. This behavior
6380 * permits to save performances because a systematic
6381 * Lua initialization cause 5% performances loss.
6382 */
6383 if (!hlua_ctx_init(hlua, task)) {
6384 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6385 ctx->rule->arg.hlua_rule->fcn.name);
6386 return 0;
6387 }
6388
6389 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006390 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006391
6392 /* The following Lua calls can fail. */
6393 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006394 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6395 error = lua_tostring(hlua->T, -1);
6396 else
6397 error = "critical error";
6398 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6399 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006400 return 0;
6401 }
6402
6403 /* Check stack available size. */
6404 if (!lua_checkstack(hlua->T, 1)) {
6405 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6406 ctx->rule->arg.hlua_rule->fcn.name);
6407 RESET_SAFE_LJMP(hlua->T);
6408 return 0;
6409 }
6410
6411 /* Restore the function in the stack. */
6412 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6413
6414 /* Create and and push object stream in the stack. */
6415 if (!hlua_applet_http_new(hlua->T, ctx)) {
6416 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6417 ctx->rule->arg.hlua_rule->fcn.name);
6418 RESET_SAFE_LJMP(hlua->T);
6419 return 0;
6420 }
6421 hlua->nargs = 1;
6422
6423 /* Look for a 100-continue expected. */
6424 if (msg->flags & HTTP_MSGF_VER_11) {
6425 hdr.idx = 0;
6426 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) &&
6427 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6428 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6429 }
6430
6431 /* push keywords in the stack. */
6432 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6433 if (!lua_checkstack(hlua->T, 1)) {
6434 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6435 ctx->rule->arg.hlua_rule->fcn.name);
6436 RESET_SAFE_LJMP(hlua->T);
6437 return 0;
6438 }
6439 lua_pushstring(hlua->T, *arg);
6440 hlua->nargs++;
6441 }
6442
6443 RESET_SAFE_LJMP(hlua->T);
6444
6445 /* Wakeup the applet when data is ready for read. */
6446 si_applet_cant_get(si);
6447
6448 return 1;
6449}
6450
6451static void hlua_applet_http_fct(struct appctx *ctx)
6452{
6453 struct stream_interface *si = ctx->owner;
6454 struct stream *strm = si_strm(si);
6455 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006456 struct act_rule *rule = ctx->rule;
6457 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006458 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006459 char *blk1;
6460 int len1;
6461 char *blk2;
6462 int len2;
6463 int ret;
6464
6465 /* If the stream is disconnect or closed, ldo nothing. */
6466 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6467 return;
6468
6469 /* Set the currently running flag. */
6470 if (!HLUA_IS_RUNNING(hlua) &&
6471 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6472
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006473 /* Wait for full HTTP analysys. */
6474 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
6475 si_applet_cant_get(si);
6476 return;
6477 }
6478
6479 /* Store the max amount of bytes that we can read. */
6480 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6481
6482 /* We need to flush the request header. This left the body
6483 * for the Lua.
6484 */
6485
6486 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006487 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006488 if (ret == -1)
6489 return;
6490
6491 /* No data available, ask for more data. */
6492 if (ret == 1)
6493 len2 = 0;
6494 if (ret == 0)
6495 len1 = 0;
6496 if (len1 + len2 < strm->txn->req.eoh + 2) {
6497 si_applet_cant_get(si);
6498 return;
6499 }
6500
6501 /* skip the requests bytes. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006502 co_skip(si_oc(si), strm->txn->req.eoh + 2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006503 }
6504
6505 /* Executes The applet if it is not done. */
6506 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6507
6508 /* Execute the function. */
6509 switch (hlua_ctx_resume(hlua, 1)) {
6510 /* finished. */
6511 case HLUA_E_OK:
6512 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6513 break;
6514
6515 /* yield. */
6516 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006517 if (hlua->wake_time != TICK_ETERNITY)
6518 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006519 return;
6520
6521 /* finished with error. */
6522 case HLUA_E_ERRMSG:
6523 /* Display log. */
6524 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6525 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6526 lua_pop(hlua->T, 1);
6527 goto error;
6528
6529 case HLUA_E_ERR:
6530 /* Display log. */
6531 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6532 rule->arg.hlua_rule->fcn.name);
6533 goto error;
6534
6535 default:
6536 goto error;
6537 }
6538 }
6539
6540 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6541
6542 /* We must send the final chunk. */
6543 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6544 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6545
6546 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006547 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006548
6549 /* critical error. */
6550 if (ret == -2 || ret == -3) {
6551 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6552 rule->arg.hlua_rule->fcn.name);
6553 goto error;
6554 }
6555
6556 /* no enough space error. */
6557 if (ret == -1) {
6558 si_applet_cant_put(si);
6559 return;
6560 }
6561
6562 /* set the last chunk sent. */
6563 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6564 }
6565
6566 /* close the connection. */
6567
6568 /* status / log */
6569 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6570 strm->logs.tv_request = now;
6571
6572 /* eat the whole request */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006573 co_skip(si_oc(si), si_ob(si)->o);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006574 res->flags |= CF_READ_NULL;
6575 si_shutr(si);
6576
6577 return;
6578 }
6579
6580error:
6581
6582 /* If we are in HTTP mode, and we are not send any
6583 * data, return a 500 server error in best effort:
6584 * if there are no room avalaible in the buffer,
6585 * just close the connection.
6586 */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006587 ci_putblk(res, error_500, strlen(error_500));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006588 if (!(strm->flags & SF_ERR_MASK))
6589 strm->flags |= SF_ERR_RESOURCE;
6590 si_shutw(si);
6591 si_shutr(si);
6592 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6593}
6594
6595static void hlua_applet_http_release(struct appctx *ctx)
6596{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006597 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006598 task_free(ctx->ctx.hlua_apphttp.task);
6599 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006600 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006601 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006602}
6603
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006604/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6605 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006606 *
6607 * This function can fail with an abort() due to an Lua critical error.
6608 * We are in the configuration parsing process of HAProxy, this abort() is
6609 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006610 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006611static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6612 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006613{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006614 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006615 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006616
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006617 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006618 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006619 if (!rule->arg.hlua_rule) {
6620 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006621 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006622 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006623
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006624 /* Memory for arguments. */
6625 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6626 if (!rule->arg.hlua_rule->args) {
6627 memprintf(err, "out of memory error");
6628 return ACT_RET_PRS_ERR;
6629 }
6630
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006631 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006632 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006633
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006634 /* Expect some arguments */
6635 for (i = 0; i < fcn->nargs; i++) {
6636 if (*args[i+1] == '\0') {
6637 memprintf(err, "expect %d arguments", fcn->nargs);
6638 return ACT_RET_PRS_ERR;
6639 }
6640 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6641 if (!rule->arg.hlua_rule->args[i]) {
6642 memprintf(err, "out of memory error");
6643 return ACT_RET_PRS_ERR;
6644 }
6645 (*cur_arg)++;
6646 }
6647 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006648
Thierry FOURNIER42148732015-09-02 17:17:33 +02006649 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006650 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006651 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006652}
6653
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006654static enum act_parse_ret action_register_service_http(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 FOURNIERa30b5db2015-09-18 09:04:27 +02006658
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006659 /* HTTP applets are forbidden in tcp-request rules.
6660 * HTTP applet request requires everything initilized by
6661 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6662 * The applet will be immediately initilized, but its before
6663 * the call of this analyzer.
6664 */
6665 if (rule->from != ACT_F_HTTP_REQ) {
6666 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6667 return ACT_RET_PRS_ERR;
6668 }
6669
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006670 /* Memory for the rule. */
6671 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6672 if (!rule->arg.hlua_rule) {
6673 memprintf(err, "out of memory error");
6674 return ACT_RET_PRS_ERR;
6675 }
6676
6677 /* Reference the Lua function and store the reference. */
6678 rule->arg.hlua_rule->fcn = *fcn;
6679
6680 /* TODO: later accept arguments. */
6681 rule->arg.hlua_rule->args = NULL;
6682
6683 /* Add applet pointer in the rule. */
6684 rule->applet.obj_type = OBJ_TYPE_APPLET;
6685 rule->applet.name = fcn->name;
6686 rule->applet.init = hlua_applet_http_init;
6687 rule->applet.fct = hlua_applet_http_fct;
6688 rule->applet.release = hlua_applet_http_release;
6689 rule->applet.timeout = hlua_timeout_applet;
6690
6691 return ACT_RET_PRS_OK;
6692}
6693
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006694/* This function is an LUA binding used for registering
6695 * "sample-conv" functions. It expects a converter name used
6696 * in the haproxy configuration file, and an LUA function.
6697 */
6698__LJMP static int hlua_register_action(lua_State *L)
6699{
6700 struct action_kw_list *akl;
6701 const char *name;
6702 int ref;
6703 int len;
6704 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006705 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006706
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006707 /* Initialise the number of expected arguments at 0. */
6708 nargs = 0;
6709
6710 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6711 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006712
6713 /* First argument : converter name. */
6714 name = MAY_LJMP(luaL_checkstring(L, 1));
6715
6716 /* Second argument : environment. */
6717 if (lua_type(L, 2) != LUA_TTABLE)
6718 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6719
6720 /* Third argument : lua function. */
6721 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6722
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006723 /* Fouth argument : number of mandatories arguments expected on the configuration line. */
6724 if (lua_gettop(L) >= 4)
6725 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6726
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006727 /* browse the second argulent as an array. */
6728 lua_pushnil(L);
6729 while (lua_next(L, 2) != 0) {
6730 if (lua_type(L, -1) != LUA_TSTRING)
6731 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6732
6733 /* Check required environment. Only accepted "http" or "tcp". */
6734 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006735 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006736 if (!akl)
6737 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006738 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006739 if (!fcn)
6740 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6741
6742 /* Fill fcn. */
6743 fcn->name = strdup(name);
6744 if (!fcn->name)
6745 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6746 fcn->function_ref = ref;
6747
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006748 /* Set the expected number od arguments. */
6749 fcn->nargs = nargs;
6750
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006751 /* List head */
6752 akl->list.n = akl->list.p = NULL;
6753
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006754 /* action keyword. */
6755 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006756 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006757 if (!akl->kw[0].kw)
6758 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6759
6760 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6761
6762 akl->kw[0].match_pfx = 0;
6763 akl->kw[0].private = fcn;
6764 akl->kw[0].parse = action_register_lua;
6765
6766 /* select the action registering point. */
6767 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6768 tcp_req_cont_keywords_register(akl);
6769 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6770 tcp_res_cont_keywords_register(akl);
6771 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6772 http_req_keywords_register(akl);
6773 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6774 http_res_keywords_register(akl);
6775 else
6776 WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. "
6777 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6778 "are expected.", lua_tostring(L, -1)));
6779
6780 /* pop the environment string. */
6781 lua_pop(L, 1);
6782 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006783 return ACT_RET_PRS_OK;
6784}
6785
6786static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6787 struct act_rule *rule, char **err)
6788{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006789 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006790
6791 /* Memory for the rule. */
6792 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6793 if (!rule->arg.hlua_rule) {
6794 memprintf(err, "out of memory error");
6795 return ACT_RET_PRS_ERR;
6796 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006797
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006798 /* Reference the Lua function and store the reference. */
6799 rule->arg.hlua_rule->fcn = *fcn;
6800
6801 /* TODO: later accept arguments. */
6802 rule->arg.hlua_rule->args = NULL;
6803
6804 /* Add applet pointer in the rule. */
6805 rule->applet.obj_type = OBJ_TYPE_APPLET;
6806 rule->applet.name = fcn->name;
6807 rule->applet.init = hlua_applet_tcp_init;
6808 rule->applet.fct = hlua_applet_tcp_fct;
6809 rule->applet.release = hlua_applet_tcp_release;
6810 rule->applet.timeout = hlua_timeout_applet;
6811
6812 return 0;
6813}
6814
6815/* This function is an LUA binding used for registering
6816 * "sample-conv" functions. It expects a converter name used
6817 * in the haproxy configuration file, and an LUA function.
6818 */
6819__LJMP static int hlua_register_service(lua_State *L)
6820{
6821 struct action_kw_list *akl;
6822 const char *name;
6823 const char *env;
6824 int ref;
6825 int len;
6826 struct hlua_function *fcn;
6827
6828 MAY_LJMP(check_args(L, 3, "register_service"));
6829
6830 /* First argument : converter name. */
6831 name = MAY_LJMP(luaL_checkstring(L, 1));
6832
6833 /* Second argument : environment. */
6834 env = MAY_LJMP(luaL_checkstring(L, 2));
6835
6836 /* Third argument : lua function. */
6837 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6838
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006839 /* Allocate and fill the sample fetch keyword struct. */
6840 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
6841 if (!akl)
6842 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6843 fcn = calloc(1, sizeof(*fcn));
6844 if (!fcn)
6845 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6846
6847 /* Fill fcn. */
6848 len = strlen("<lua.>") + strlen(name) + 1;
6849 fcn->name = calloc(1, len);
6850 if (!fcn->name)
6851 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6852 snprintf((char *)fcn->name, len, "<lua.%s>", name);
6853 fcn->function_ref = ref;
6854
6855 /* List head */
6856 akl->list.n = akl->list.p = NULL;
6857
6858 /* converter keyword. */
6859 len = strlen("lua.") + strlen(name) + 1;
6860 akl->kw[0].kw = calloc(1, len);
6861 if (!akl->kw[0].kw)
6862 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6863
6864 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6865
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01006866 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006867 if (strcmp(env, "tcp") == 0)
6868 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006869 else if (strcmp(env, "http") == 0)
6870 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006871 else
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006872 WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. "
6873 "'tcp' or 'http' are expected."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006874
6875 akl->kw[0].match_pfx = 0;
6876 akl->kw[0].private = fcn;
6877
6878 /* End of array. */
6879 memset(&akl->kw[1], 0, sizeof(*akl->kw));
6880
6881 /* Register this new converter */
6882 service_keywords_register(akl);
6883
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006884 return 0;
6885}
6886
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006887/* This function initialises Lua cli handler. It copies the
6888 * arguments in the Lua stack and create channel IO objects.
6889 */
6890static int hlua_cli_parse_fct(char **args, struct appctx *appctx, void *private)
6891{
6892 struct hlua *hlua;
6893 struct hlua_function *fcn;
6894 int i;
6895 const char *error;
6896
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006897 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006898 appctx->ctx.hlua_cli.fcn = private;
6899
6900 hlua = pool_alloc2(pool2_hlua);
6901 if (!hlua) {
6902 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006903 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006904 }
6905 HLUA_INIT(hlua);
6906 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006907
6908 /* Create task used by signal to wakeup applets.
6909 * We use the same wakeup fonction than the Lua applet_tcp and
6910 * applet_http. It is absolutely compatible.
6911 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006912 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006913 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01006914 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006915 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006916 }
6917 appctx->ctx.hlua_cli.task->nice = 0;
6918 appctx->ctx.hlua_cli.task->context = appctx;
6919 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
6920
6921 /* Initialises the Lua context */
6922 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
6923 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006924 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006925 }
6926
6927 /* The following Lua calls can fail. */
6928 if (!SET_SAFE_LJMP(hlua->T)) {
6929 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6930 error = lua_tostring(hlua->T, -1);
6931 else
6932 error = "critical error";
6933 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
6934 goto error;
6935 }
6936
6937 /* Check stack available size. */
6938 if (!lua_checkstack(hlua->T, 2)) {
6939 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6940 goto error;
6941 }
6942
6943 /* Restore the function in the stack. */
6944 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
6945
6946 /* Once the arguments parsed, the CLI is like an AppletTCP,
6947 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006948 */
6949 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
6950 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6951 goto error;
6952 }
6953 hlua->nargs = 1;
6954
6955 /* push keywords in the stack. */
6956 for (i = 0; *args[i]; i++) {
6957 /* Check stack available size. */
6958 if (!lua_checkstack(hlua->T, 1)) {
6959 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6960 goto error;
6961 }
6962 lua_pushstring(hlua->T, args[i]);
6963 hlua->nargs++;
6964 }
6965
6966 /* We must initialize the execution timeouts. */
6967 hlua->max_time = hlua_timeout_session;
6968
6969 /* At this point the execution is safe. */
6970 RESET_SAFE_LJMP(hlua->T);
6971
6972 /* It's ok */
6973 return 0;
6974
6975 /* It's not ok. */
6976error:
6977 RESET_SAFE_LJMP(hlua->T);
6978 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006979 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006980 return 1;
6981}
6982
6983static int hlua_cli_io_handler_fct(struct appctx *appctx)
6984{
6985 struct hlua *hlua;
6986 struct stream_interface *si;
6987 struct hlua_function *fcn;
6988
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006989 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006990 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01006991 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006992
6993 /* If the stream is disconnect or closed, ldo nothing. */
6994 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6995 return 1;
6996
6997 /* Execute the function. */
6998 switch (hlua_ctx_resume(hlua, 1)) {
6999
7000 /* finished. */
7001 case HLUA_E_OK:
7002 return 1;
7003
7004 /* yield. */
7005 case HLUA_E_AGAIN:
7006 /* We want write. */
7007 if (HLUA_IS_WAKERESWR(hlua))
7008 si_applet_cant_put(si);
7009 /* Set the timeout. */
7010 if (hlua->wake_time != TICK_ETERNITY)
7011 task_schedule(hlua->task, hlua->wake_time);
7012 return 0;
7013
7014 /* finished with error. */
7015 case HLUA_E_ERRMSG:
7016 /* Display log. */
7017 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7018 fcn->name, lua_tostring(hlua->T, -1));
7019 lua_pop(hlua->T, 1);
7020 return 1;
7021
7022 case HLUA_E_ERR:
7023 /* Display log. */
7024 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7025 fcn->name);
7026 return 1;
7027
7028 default:
7029 return 1;
7030 }
7031
7032 return 1;
7033}
7034
7035static void hlua_cli_io_release_fct(struct appctx *appctx)
7036{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007037 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007038 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007039}
7040
7041/* This function is an LUA binding used for registering
7042 * new keywords in the cli. It expects a list of keywords
7043 * which are the "path". It is limited to 5 keywords. A
7044 * description of the command, a function to be executed
7045 * for the parsing and a function for io handlers.
7046 */
7047__LJMP static int hlua_register_cli(lua_State *L)
7048{
7049 struct cli_kw_list *cli_kws;
7050 const char *message;
7051 int ref_io;
7052 int len;
7053 struct hlua_function *fcn;
7054 int index;
7055 int i;
7056
7057 MAY_LJMP(check_args(L, 3, "register_cli"));
7058
7059 /* First argument : an array of maximum 5 keywords. */
7060 if (!lua_istable(L, 1))
7061 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7062
7063 /* Second argument : string with contextual message. */
7064 message = MAY_LJMP(luaL_checkstring(L, 2));
7065
7066 /* Third and fourth argument : lua function. */
7067 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7068
7069 /* Allocate and fill the sample fetch keyword struct. */
7070 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7071 if (!cli_kws)
7072 WILL_LJMP(luaL_error(L, "lua out of memory error."));
7073 fcn = calloc(1, sizeof(*fcn));
7074 if (!fcn)
7075 WILL_LJMP(luaL_error(L, "lua out of memory error."));
7076
7077 /* Fill path. */
7078 index = 0;
7079 lua_pushnil(L);
7080 while(lua_next(L, 1) != 0) {
7081 if (index >= 5)
7082 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7083 if (lua_type(L, -1) != LUA_TSTRING)
7084 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7085 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7086 if (!cli_kws->kw[0].str_kw[index])
7087 WILL_LJMP(luaL_error(L, "lua out of memory error."));
7088 index++;
7089 lua_pop(L, 1);
7090 }
7091
7092 /* Copy help message. */
7093 cli_kws->kw[0].usage = strdup(message);
7094 if (!cli_kws->kw[0].usage)
7095 WILL_LJMP(luaL_error(L, "lua out of memory error."));
7096
7097 /* Fill fcn io handler. */
7098 len = strlen("<lua.cli>") + 1;
7099 for (i = 0; i < index; i++)
7100 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7101 fcn->name = calloc(1, len);
7102 if (!fcn->name)
7103 WILL_LJMP(luaL_error(L, "lua out of memory error."));
7104 strncat((char *)fcn->name, "<lua.cli", len);
7105 for (i = 0; i < index; i++) {
7106 strncat((char *)fcn->name, ".", len);
7107 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7108 }
7109 strncat((char *)fcn->name, ">", len);
7110 fcn->function_ref = ref_io;
7111
7112 /* Fill last entries. */
7113 cli_kws->kw[0].private = fcn;
7114 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7115 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7116 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7117
7118 /* Register this new converter */
7119 cli_register_kw(cli_kws);
7120
7121 return 0;
7122}
7123
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007124static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7125 struct proxy *defpx, const char *file, int line,
7126 char **err, unsigned int *timeout)
7127{
7128 const char *error;
7129
7130 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
7131 if (error && *error != '\0') {
7132 memprintf(err, "%s: invalid timeout", args[0]);
7133 return -1;
7134 }
7135 return 0;
7136}
7137
7138static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7139 struct proxy *defpx, const char *file, int line,
7140 char **err)
7141{
7142 return hlua_read_timeout(args, section_type, curpx, defpx,
7143 file, line, err, &hlua_timeout_session);
7144}
7145
7146static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7147 struct proxy *defpx, const char *file, int line,
7148 char **err)
7149{
7150 return hlua_read_timeout(args, section_type, curpx, defpx,
7151 file, line, err, &hlua_timeout_task);
7152}
7153
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007154static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7155 struct proxy *defpx, const char *file, int line,
7156 char **err)
7157{
7158 return hlua_read_timeout(args, section_type, curpx, defpx,
7159 file, line, err, &hlua_timeout_applet);
7160}
7161
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007162static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7163 struct proxy *defpx, const char *file, int line,
7164 char **err)
7165{
7166 char *error;
7167
7168 hlua_nb_instruction = strtoll(args[1], &error, 10);
7169 if (*error != '\0') {
7170 memprintf(err, "%s: invalid number", args[0]);
7171 return -1;
7172 }
7173 return 0;
7174}
7175
Willy Tarreau32f61e22015-03-18 17:54:59 +01007176static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7177 struct proxy *defpx, const char *file, int line,
7178 char **err)
7179{
7180 char *error;
7181
7182 if (*(args[1]) == 0) {
7183 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7184 return -1;
7185 }
7186 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7187 if (*error != '\0') {
7188 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7189 return -1;
7190 }
7191 return 0;
7192}
7193
7194
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007195/* This function is called by the main configuration key "lua-load". It loads and
7196 * execute an lua file during the parsing of the HAProxy configuration file. It is
7197 * the main lua entry point.
7198 *
7199 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
7200 * occured, otherwise it returns 0.
7201 *
7202 * In some error case, LUA set an error message in top of the stack. This function
7203 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007204 *
7205 * This function can fail with an abort() due to an Lua critical error.
7206 * We are in the configuration parsing process of HAProxy, this abort() is
7207 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007208 */
7209static int hlua_load(char **args, int section_type, struct proxy *curpx,
7210 struct proxy *defpx, const char *file, int line,
7211 char **err)
7212{
7213 int error;
7214
7215 /* Just load and compile the file. */
7216 error = luaL_loadfile(gL.T, args[1]);
7217 if (error) {
7218 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
7219 lua_pop(gL.T, 1);
7220 return -1;
7221 }
7222
7223 /* If no syntax error where detected, execute the code. */
7224 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7225 switch (error) {
7226 case LUA_OK:
7227 break;
7228 case LUA_ERRRUN:
7229 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
7230 lua_pop(gL.T, 1);
7231 return -1;
7232 case LUA_ERRMEM:
7233 memprintf(err, "lua out of memory error\n");
7234 return -1;
7235 case LUA_ERRERR:
7236 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
7237 lua_pop(gL.T, 1);
7238 return -1;
7239 case LUA_ERRGCMM:
7240 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
7241 lua_pop(gL.T, 1);
7242 return -1;
7243 default:
7244 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
7245 lua_pop(gL.T, 1);
7246 return -1;
7247 }
7248
7249 return 0;
7250}
7251
7252/* configuration keywords declaration */
7253static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007254 { CFG_GLOBAL, "lua-load", hlua_load },
7255 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7256 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007257 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007258 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007259 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007260 { 0, NULL, NULL },
7261}};
7262
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007263/* This function can fail with an abort() due to an Lua critical error.
7264 * We are in the initialisation process of HAProxy, this abort() is
7265 * tolerated.
7266 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007267int hlua_post_init()
7268{
7269 struct hlua_init_function *init;
7270 const char *msg;
7271 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007272 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007273
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007274 /* Call post initialisation function in safe environement. */
7275 if (!SET_SAFE_LJMP(gL.T)) {
7276 if (lua_type(gL.T, -1) == LUA_TSTRING)
7277 error = lua_tostring(gL.T, -1);
7278 else
7279 error = "critical error";
7280 fprintf(stderr, "Lua post-init: %s.\n", error);
7281 exit(1);
7282 }
7283 hlua_fcn_post_init(gL.T);
7284 RESET_SAFE_LJMP(gL.T);
7285
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007286 list_for_each_entry(init, &hlua_init_functions, l) {
7287 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7288 ret = hlua_ctx_resume(&gL, 0);
7289 switch (ret) {
7290 case HLUA_E_OK:
7291 lua_pop(gL.T, -1);
7292 return 1;
7293 case HLUA_E_AGAIN:
7294 Alert("lua init: yield not allowed.\n");
7295 return 0;
7296 case HLUA_E_ERRMSG:
7297 msg = lua_tostring(gL.T, -1);
7298 Alert("lua init: %s.\n", msg);
7299 return 0;
7300 case HLUA_E_ERR:
7301 default:
7302 Alert("lua init: unknown runtime error.\n");
7303 return 0;
7304 }
7305 }
7306 return 1;
7307}
7308
Willy Tarreau32f61e22015-03-18 17:54:59 +01007309/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7310 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7311 * is the previously allocated size or the kind of object in case of a new
7312 * allocation. <nsize> is the requested new size.
7313 */
7314static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7315{
7316 struct hlua_mem_allocator *zone = ud;
7317
7318 if (nsize == 0) {
7319 /* it's a free */
7320 if (ptr)
7321 zone->allocated -= osize;
7322 free(ptr);
7323 return NULL;
7324 }
7325
7326 if (!ptr) {
7327 /* it's a new allocation */
7328 if (zone->limit && zone->allocated + nsize > zone->limit)
7329 return NULL;
7330
7331 ptr = malloc(nsize);
7332 if (ptr)
7333 zone->allocated += nsize;
7334 return ptr;
7335 }
7336
7337 /* it's a realloc */
7338 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7339 return NULL;
7340
7341 ptr = realloc(ptr, nsize);
7342 if (ptr)
7343 zone->allocated += nsize - osize;
7344 return ptr;
7345}
7346
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007347/* Ithis function can fail with an abort() due to an Lua critical error.
7348 * We are in the initialisation process of HAProxy, this abort() is
7349 * tolerated.
7350 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007351void hlua_init(void)
7352{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007353 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007354 int idx;
7355 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007356 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007357 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007358 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007359#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007360 struct srv_kw *kw;
7361 int tmp_error;
7362 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007363 char *args[] = { /* SSL client configuration. */
7364 "ssl",
7365 "verify",
7366 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007367 NULL
7368 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007369#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007370
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007371 HA_SPIN_INIT(&hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02007372
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007373 /* Initialise struct hlua and com signals pool */
7374 pool2_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007375
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007376 /* Register configuration keywords. */
7377 cfg_register_keywords(&cfg_kws);
7378
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007379 /* Init main lua stack. */
7380 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007381 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007382 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007383 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007384 hlua_sethlua(&gL);
7385 gL.Tref = LUA_REFNIL;
7386 gL.task = NULL;
7387
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007388 /* From this point, until the end of the initialisation fucntion,
7389 * the Lua function can fail with an abort. We are in the initialisation
7390 * process of HAProxy, this abort() is tolerated.
7391 */
7392
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007393 /* Initialise lua. */
7394 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007395
Thierry Fournier75933d42016-01-21 09:30:18 +01007396 /* Set safe environment for the initialisation. */
7397 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007398 if (lua_type(gL.T, -1) == LUA_TSTRING)
7399 error_msg = lua_tostring(gL.T, -1);
7400 else
7401 error_msg = "critical error";
7402 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007403 exit(1);
7404 }
7405
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007406 /*
7407 *
7408 * Create "core" object.
7409 *
7410 */
7411
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007412 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007413 lua_newtable(gL.T);
7414
7415 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007416 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007417 hlua_class_const_int(gL.T, log_levels[i], i);
7418
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007419 /* Register special functions. */
7420 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007421 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007422 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007423 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007424 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007425 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007426 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007427 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007428 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007429 hlua_class_function(gL.T, "sleep", hlua_sleep);
7430 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007431 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7432 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7433 hlua_class_function(gL.T, "set_map", hlua_set_map);
7434 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007435 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007436 hlua_class_function(gL.T, "log", hlua_log);
7437 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7438 hlua_class_function(gL.T, "Info", hlua_log_info);
7439 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7440 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007441 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007442 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007443
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007444 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007445
7446 /*
7447 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007448 * Register class Map
7449 *
7450 */
7451
7452 /* This table entry is the object "Map" base. */
7453 lua_newtable(gL.T);
7454
7455 /* register pattern types. */
7456 for (i=0; i<PAT_MATCH_NUM; i++)
7457 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007458 for (i=0; i<PAT_MATCH_NUM; i++) {
7459 snprintf(trash.str, trash.size, "_%s", pat_match_names[i]);
7460 hlua_class_const_int(gL.T, trash.str, i);
7461 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007462
7463 /* register constructor. */
7464 hlua_class_function(gL.T, "new", hlua_map_new);
7465
7466 /* Create and fill the metatable. */
7467 lua_newtable(gL.T);
7468
7469 /* Create and fille the __index entry. */
7470 lua_pushstring(gL.T, "__index");
7471 lua_newtable(gL.T);
7472
7473 /* Register . */
7474 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7475 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7476
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007477 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007478
Thierry Fournier45e78d72016-02-19 18:34:46 +01007479 /* Register previous table in the registry with reference and named entry.
7480 * The function hlua_register_metatable() pops the stack, so we
7481 * previously create a copy of the table.
7482 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007483 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007484 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007485
7486 /* Assign the metatable to the mai Map object. */
7487 lua_setmetatable(gL.T, -2);
7488
7489 /* Set a name to the table. */
7490 lua_setglobal(gL.T, "Map");
7491
7492 /*
7493 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007494 * Register class Channel
7495 *
7496 */
7497
7498 /* Create and fill the metatable. */
7499 lua_newtable(gL.T);
7500
7501 /* Create and fille the __index entry. */
7502 lua_pushstring(gL.T, "__index");
7503 lua_newtable(gL.T);
7504
7505 /* Register . */
7506 hlua_class_function(gL.T, "get", hlua_channel_get);
7507 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7508 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7509 hlua_class_function(gL.T, "set", hlua_channel_set);
7510 hlua_class_function(gL.T, "append", hlua_channel_append);
7511 hlua_class_function(gL.T, "send", hlua_channel_send);
7512 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7513 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7514 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007515 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007516
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007517 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007518
7519 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007520 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007521
7522 /*
7523 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007524 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007525 *
7526 */
7527
7528 /* Create and fill the metatable. */
7529 lua_newtable(gL.T);
7530
7531 /* Create and fille the __index entry. */
7532 lua_pushstring(gL.T, "__index");
7533 lua_newtable(gL.T);
7534
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007535 /* Browse existing fetches and create the associated
7536 * object method.
7537 */
7538 sf = NULL;
7539 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7540
7541 /* Dont register the keywork if the arguments check function are
7542 * not safe during the runtime.
7543 */
7544 if ((sf->val_args != NULL) &&
7545 (sf->val_args != val_payload_lv) &&
7546 (sf->val_args != val_hdr))
7547 continue;
7548
7549 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7550 * by an underscore.
7551 */
7552 strncpy(trash.str, sf->kw, trash.size);
7553 trash.str[trash.size - 1] = '\0';
7554 for (p = trash.str; *p; p++)
7555 if (*p == '.' || *p == '-' || *p == '+')
7556 *p = '_';
7557
7558 /* Register the function. */
7559 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007560 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007561 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007562 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007563 }
7564
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007565 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007566
7567 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007568 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007569
7570 /*
7571 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007572 * Register class Converters
7573 *
7574 */
7575
7576 /* Create and fill the metatable. */
7577 lua_newtable(gL.T);
7578
7579 /* Create and fill the __index entry. */
7580 lua_pushstring(gL.T, "__index");
7581 lua_newtable(gL.T);
7582
7583 /* Browse existing converters and create the associated
7584 * object method.
7585 */
7586 sc = NULL;
7587 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7588 /* Dont register the keywork if the arguments check function are
7589 * not safe during the runtime.
7590 */
7591 if (sc->val_args != NULL)
7592 continue;
7593
7594 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7595 * by an underscore.
7596 */
7597 strncpy(trash.str, sc->kw, trash.size);
7598 trash.str[trash.size - 1] = '\0';
7599 for (p = trash.str; *p; p++)
7600 if (*p == '.' || *p == '-' || *p == '+')
7601 *p = '_';
7602
7603 /* Register the function. */
7604 lua_pushstring(gL.T, trash.str);
7605 lua_pushlightuserdata(gL.T, sc);
7606 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007607 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007608 }
7609
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007610 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007611
7612 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007613 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007614
7615 /*
7616 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007617 * Register class HTTP
7618 *
7619 */
7620
7621 /* Create and fill the metatable. */
7622 lua_newtable(gL.T);
7623
7624 /* Create and fille the __index entry. */
7625 lua_pushstring(gL.T, "__index");
7626 lua_newtable(gL.T);
7627
7628 /* Register Lua functions. */
7629 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7630 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7631 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7632 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7633 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7634 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7635 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7636 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7637 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7638 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7639
7640 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7641 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7642 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7643 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7644 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7645 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007646 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007647
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007648 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007649
7650 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007651 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007652
7653 /*
7654 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007655 * Register class AppletTCP
7656 *
7657 */
7658
7659 /* Create and fill the metatable. */
7660 lua_newtable(gL.T);
7661
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007662 /* Create and fille the __index entry. */
7663 lua_pushstring(gL.T, "__index");
7664 lua_newtable(gL.T);
7665
7666 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007667 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7668 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7669 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7670 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7671 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007672 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7673 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7674 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007675
7676 lua_settable(gL.T, -3);
7677
7678 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007679 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007680
7681 /*
7682 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007683 * Register class AppletHTTP
7684 *
7685 */
7686
7687 /* Create and fill the metatable. */
7688 lua_newtable(gL.T);
7689
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007690 /* Create and fille the __index entry. */
7691 lua_pushstring(gL.T, "__index");
7692 lua_newtable(gL.T);
7693
7694 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007695 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7696 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007697 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7698 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7699 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007700 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7701 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7702 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7703 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7704 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7705 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7706
7707 lua_settable(gL.T, -3);
7708
7709 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007710 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007711
7712 /*
7713 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007714 * Register class TXN
7715 *
7716 */
7717
7718 /* Create and fill the metatable. */
7719 lua_newtable(gL.T);
7720
7721 /* Create and fille the __index entry. */
7722 lua_pushstring(gL.T, "__index");
7723 lua_newtable(gL.T);
7724
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007725 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01007726 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7727 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007728 hlua_class_function(gL.T, "set_var", hlua_set_var);
Christopher Faulet85d79c92016-11-09 16:54:56 +01007729 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007730 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007731 hlua_class_function(gL.T, "done", hlua_txn_done);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007732 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
7733 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7734 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007735 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7736 hlua_class_function(gL.T, "log", hlua_txn_log);
7737 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7738 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7739 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7740 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007741
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007742 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007743
7744 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007745 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007746
7747 /*
7748 *
7749 * Register class Socket
7750 *
7751 */
7752
7753 /* Create and fill the metatable. */
7754 lua_newtable(gL.T);
7755
7756 /* Create and fille the __index entry. */
7757 lua_pushstring(gL.T, "__index");
7758 lua_newtable(gL.T);
7759
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007760#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007761 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007762#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007763 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7764 hlua_class_function(gL.T, "send", hlua_socket_send);
7765 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7766 hlua_class_function(gL.T, "close", hlua_socket_close);
7767 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7768 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7769 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7770 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7771
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007772 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007773
7774 /* Register the garbage collector entry. */
7775 lua_pushstring(gL.T, "__gc");
7776 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007777 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007778
7779 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007780 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007781
7782 /* Proxy and server configuration initialisation. */
7783 memset(&socket_proxy, 0, sizeof(socket_proxy));
7784 init_new_proxy(&socket_proxy);
7785 socket_proxy.parent = NULL;
7786 socket_proxy.last_change = now.tv_sec;
7787 socket_proxy.id = "LUA-SOCKET";
7788 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
7789 socket_proxy.maxconn = 0;
7790 socket_proxy.accept = NULL;
7791 socket_proxy.options2 |= PR_O2_INDEPSTR;
7792 socket_proxy.srv = NULL;
7793 socket_proxy.conn_retries = 0;
7794 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
7795
7796 /* Init TCP server: unchanged parameters */
7797 memset(&socket_tcp, 0, sizeof(socket_tcp));
7798 socket_tcp.next = NULL;
7799 socket_tcp.proxy = &socket_proxy;
7800 socket_tcp.obj_type = OBJ_TYPE_SERVER;
7801 LIST_INIT(&socket_tcp.actconns);
7802 LIST_INIT(&socket_tcp.pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02007803 socket_tcp.priv_conns = NULL;
7804 socket_tcp.idle_conns = NULL;
7805 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02007806 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007807 socket_tcp.last_change = 0;
7808 socket_tcp.id = "LUA-TCP-CONN";
7809 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7810 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7811 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
7812
7813 /* XXX: Copy default parameter from default server,
7814 * but the default server is not initialized.
7815 */
7816 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
7817 socket_tcp.minconn = socket_proxy.defsrv.minconn;
7818 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
7819 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
7820 socket_tcp.onerror = socket_proxy.defsrv.onerror;
7821 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
7822 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
7823 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
7824 socket_tcp.uweight = socket_proxy.defsrv.iweight;
7825 socket_tcp.iweight = socket_proxy.defsrv.iweight;
7826
7827 socket_tcp.check.status = HCHK_STATUS_INI;
7828 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
7829 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
7830 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
7831 socket_tcp.check.server = &socket_tcp;
7832
7833 socket_tcp.agent.status = HCHK_STATUS_INI;
7834 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
7835 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
7836 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
7837 socket_tcp.agent.server = &socket_tcp;
7838
Willy Tarreaua261e9b2016-12-22 20:44:00 +01007839 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007840
7841#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007842 /* Init TCP server: unchanged parameters */
7843 memset(&socket_ssl, 0, sizeof(socket_ssl));
7844 socket_ssl.next = NULL;
7845 socket_ssl.proxy = &socket_proxy;
7846 socket_ssl.obj_type = OBJ_TYPE_SERVER;
7847 LIST_INIT(&socket_ssl.actconns);
7848 LIST_INIT(&socket_ssl.pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02007849 socket_tcp.priv_conns = NULL;
7850 socket_tcp.idle_conns = NULL;
7851 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02007852 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007853 socket_ssl.last_change = 0;
7854 socket_ssl.id = "LUA-SSL-CONN";
7855 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7856 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7857 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
7858
7859 /* XXX: Copy default parameter from default server,
7860 * but the default server is not initialized.
7861 */
7862 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
7863 socket_ssl.minconn = socket_proxy.defsrv.minconn;
7864 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
7865 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
7866 socket_ssl.onerror = socket_proxy.defsrv.onerror;
7867 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
7868 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
7869 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
7870 socket_ssl.uweight = socket_proxy.defsrv.iweight;
7871 socket_ssl.iweight = socket_proxy.defsrv.iweight;
7872
7873 socket_ssl.check.status = HCHK_STATUS_INI;
7874 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
7875 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
7876 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
7877 socket_ssl.check.server = &socket_ssl;
7878
7879 socket_ssl.agent.status = HCHK_STATUS_INI;
7880 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
7881 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
7882 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
7883 socket_ssl.agent.server = &socket_ssl;
7884
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007885 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01007886 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007887
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007888 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007889 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
7890 /*
7891 *
7892 * If the keyword is not known, we can search in the registered
7893 * server keywords. This is usefull to configure special SSL
7894 * features like client certificates and ssl_verify.
7895 *
7896 */
7897 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
7898 if (tmp_error != 0) {
7899 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
7900 abort(); /* This must be never arrives because the command line
7901 not editable by the user. */
7902 }
7903 idx += kw->skip;
7904 }
7905 }
7906
7907 /* Initialize SSL server. */
Willy Tarreau17d45382016-12-22 21:16:08 +01007908 if (socket_ssl.xprt->prepare_srv)
7909 socket_ssl.xprt->prepare_srv(&socket_ssl);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007910#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01007911
7912 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007913}
Willy Tarreaubb57d942016-12-21 19:04:56 +01007914
7915__attribute__((constructor))
7916static void __hlua_init(void)
7917{
7918 char *ptr = NULL;
7919 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
7920 hap_register_build_opts(ptr, 1);
7921}