blob: 07e92631cdd28a0ddc62779a08afe1bd9798f5d8 [file] [log] [blame]
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001#include <sys/socket.h>
2
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003#include <ctype.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01006#include <lauxlib.h>
7#include <lua.h>
8#include <lualib.h>
9
Thierry FOURNIER463119c2015-03-10 00:35:36 +010010#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
11#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010012#endif
13
Thierry FOURNIER380d0932015-01-23 14:27:52 +010014#include <ebpttree.h>
15
16#include <common/cfgparse.h>
17
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010018#include <types/connection.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010019#include <types/hlua.h>
20#include <types/proxy.h>
21
Thierry FOURNIER55da1652015-01-23 11:36:30 +010022#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020023#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010024#include <proto/channel.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010025#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010026#include <proto/hlua.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020027#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010028#include <proto/obj_type.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010029#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010030#include <proto/payload.h>
31#include <proto/proto_http.h>
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +010032#include <proto/proto_tcp.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010033#include <proto/raw_sock.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010034#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010035#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020036#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020037#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010038#include <proto/ssl_sock.h>
39#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010040#include <proto/task.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020041#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010042
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010043/* Lua uses longjmp to perform yield or throwing errors. This
44 * macro is used only for identifying the function that can
45 * not return because a longjmp is executed.
46 * __LJMP marks a prototype of hlua file that can use longjmp.
47 * WILL_LJMP() marks an lua function that will use longjmp.
48 * MAY_LJMP() marks an lua function that may use longjmp.
49 */
50#define __LJMP
51#define WILL_LJMP(func) func
52#define MAY_LJMP(func) func
53
Thierry FOURNIERbabae282015-09-17 11:36:37 +020054/* This couple of function executes securely some Lua calls outside of
55 * the lua runtime environment. Each Lua call can return a longjmp
56 * if it encounter a memory error.
57 *
58 * Lua documentation extract:
59 *
60 * If an error happens outside any protected environment, Lua calls
61 * a panic function (see lua_atpanic) and then calls abort, thus
62 * exiting the host application. Your panic function can avoid this
63 * exit by never returning (e.g., doing a long jump to your own
64 * recovery point outside Lua).
65 *
66 * The panic function runs as if it were a message handler (see
67 * §2.3); in particular, the error message is at the top of the
68 * stack. However, there is no guarantee about stack space. To push
69 * anything on the stack, the panic function must first check the
70 * available space (see §4.2).
71 *
72 * We must check all the Lua entry point. This includes:
73 * - The include/proto/hlua.h exported functions
74 * - the task wrapper function
75 * - The action wrapper function
76 * - The converters wrapper function
77 * - The sample-fetch wrapper functions
78 *
79 * It is tolerated that the initilisation function returns an abort.
80 * Before each Lua abort, an error message is writed on stderr.
81 *
82 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
83 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
84 * because they must be exists in the program stack when the longjmp
85 * is called.
86 */
87jmp_buf safe_ljmp_env;
88static int hlua_panic_safe(lua_State *L) { return 0; }
89static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
90
91#define SET_SAFE_LJMP(__L) \
92 ({ \
93 int ret; \
94 if (setjmp(safe_ljmp_env) != 0) { \
95 lua_atpanic(__L, hlua_panic_safe); \
96 ret = 0; \
97 } else { \
98 lua_atpanic(__L, hlua_panic_ljmp); \
99 ret = 1; \
100 } \
101 ret; \
102 })
103
104/* If we are the last function catching Lua errors, we
105 * must reset the panic function.
106 */
107#define RESET_SAFE_LJMP(__L) \
108 do { \
109 lua_atpanic(__L, hlua_panic_safe); \
110 } while(0)
111
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100112/* The main Lua execution context. */
113struct hlua gL;
114
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100115/* This is the memory pool containing all the signal structs. These
116 * struct are used to store each requiered signal between two tasks.
117 */
118struct pool_head *pool2_hlua_com;
119
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100120/* Used for Socket connection. */
121static struct proxy socket_proxy;
122static struct server socket_tcp;
123#ifdef USE_OPENSSL
124static struct server socket_ssl;
125#endif
126
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100127/* List head of the function called at the initialisation time. */
128struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
129
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100130/* The following variables contains the reference of the different
131 * Lua classes. These references are useful for identify metadata
132 * associated with an object.
133 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100134static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100135static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100136static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100137static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100138static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100139static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200140static int class_map_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100141
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100142/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200143 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100144 * short timeout. Lua linked with tasks doesn't have a timeout
145 * because a task may remain alive during all the haproxy execution.
146 */
147static unsigned int hlua_timeout_session = 4000; /* session timeout. */
148static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
149
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100150/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
151 * it is used for preventing infinite loops.
152 *
153 * I test the scheer with an infinite loop containing one incrementation
154 * and one test. I run this loop between 10 seconds, I raise a ceil of
155 * 710M loops from one interrupt each 9000 instructions, so I fix the value
156 * to one interrupt each 10 000 instructions.
157 *
158 * configured | Number of
159 * instructions | loops executed
160 * between two | in milions
161 * forced yields |
162 * ---------------+---------------
163 * 10 | 160
164 * 500 | 670
165 * 1000 | 680
166 * 5000 | 700
167 * 7000 | 700
168 * 8000 | 700
169 * 9000 | 710 <- ceil
170 * 10000 | 710
171 * 100000 | 710
172 * 1000000 | 710
173 *
174 */
175static unsigned int hlua_nb_instruction = 10000;
176
Willy Tarreau32f61e22015-03-18 17:54:59 +0100177/* Descriptor for the memory allocation state. If limit is not null, it will
178 * be enforced on any memory allocation.
179 */
180struct hlua_mem_allocator {
181 size_t allocated;
182 size_t limit;
183};
184
185static struct hlua_mem_allocator hlua_global_allocator;
186
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100187/* These functions converts types between HAProxy internal args or
188 * sample and LUA types. Another function permits to check if the
189 * LUA stack contains arguments according with an required ARG_T
190 * format.
191 */
192static int hlua_arg2lua(lua_State *L, const struct arg *arg);
193static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100194__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
195 unsigned int mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100196static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100197static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100198static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
199
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200200#define SEND_ERR(__be, __fmt, __args...) \
201 do { \
202 send_log(__be, LOG_ERR, __fmt, ## __args); \
203 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
204 Alert(__fmt, ## __args); \
205 } while (0)
206
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100207/* Used to check an Lua function type in the stack. It creates and
208 * returns a reference of the function. This function throws an
209 * error if the rgument is not a "function".
210 */
211__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
212{
213 if (!lua_isfunction(L, argno)) {
214 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1));
215 WILL_LJMP(luaL_argerror(L, argno, msg));
216 }
217 lua_pushvalue(L, argno);
218 return luaL_ref(L, LUA_REGISTRYINDEX);
219}
220
221/* The three following functions are useful for adding entries
222 * in a table. These functions takes a string and respectively an
223 * integer, a string or a function and add it to the table in the
224 * top of the stack.
225 *
226 * These functions throws an error if no more stack size is
227 * available.
228 */
229__LJMP static inline void hlua_class_const_int(lua_State *L, const char *name,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100230 int value)
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100231{
232 if (!lua_checkstack(L, 2))
233 WILL_LJMP(luaL_error(L, "full stack"));
234 lua_pushstring(L, name);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100235 lua_pushinteger(L, value);
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100236 lua_settable(L, -3);
237}
238__LJMP static inline void hlua_class_const_str(lua_State *L, const char *name,
239 const char *value)
240{
241 if (!lua_checkstack(L, 2))
242 WILL_LJMP(luaL_error(L, "full stack"));
243 lua_pushstring(L, name);
244 lua_pushstring(L, value);
245 lua_settable(L, -3);
246}
247__LJMP static inline void hlua_class_function(lua_State *L, const char *name,
248 int (*function)(lua_State *L))
249{
250 if (!lua_checkstack(L, 2))
251 WILL_LJMP(luaL_error(L, "full stack"));
252 lua_pushstring(L, name);
253 lua_pushcclosure(L, function, 0);
254 lua_settable(L, -3);
255}
256
257/* This function check the number of arguments available in the
258 * stack. If the number of arguments available is not the same
259 * then <nb> an error is throwed.
260 */
261__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
262{
263 if (lua_gettop(L) == nb)
264 return;
265 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
266}
267
268/* Return true if the data in stack[<ud>] is an object of
269 * type <class_ref>.
270 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +0100271static int hlua_metaistype(lua_State *L, int ud, int class_ref)
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100272{
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100273 if (!lua_getmetatable(L, ud))
274 return 0;
275
276 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
277 if (!lua_rawequal(L, -1, -2)) {
278 lua_pop(L, 2);
279 return 0;
280 }
281
282 lua_pop(L, 2);
283 return 1;
284}
285
286/* Return an object of the expected type, or throws an error. */
287__LJMP static void *hlua_checkudata(lua_State *L, int ud, int class_ref)
288{
Thierry FOURNIER2297bc22015-03-11 17:43:33 +0100289 void *p;
290
291 /* Check if the stack entry is an array. */
292 if (!lua_istable(L, ud))
293 WILL_LJMP(luaL_argerror(L, ud, NULL));
294 /* Check if the metadata have the expected type. */
295 if (!hlua_metaistype(L, ud, class_ref))
296 WILL_LJMP(luaL_argerror(L, ud, NULL));
297 /* Push on the stack at the entry [0] of the table. */
298 lua_rawgeti(L, ud, 0);
299 /* Check if this entry is userdata. */
300 p = lua_touserdata(L, -1);
301 if (!p)
302 WILL_LJMP(luaL_argerror(L, ud, NULL));
303 /* Remove the entry returned by lua_rawgeti(). */
304 lua_pop(L, 1);
305 /* Return the associated struct. */
306 return p;
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100307}
308
309/* This fucntion push an error string prefixed by the file name
310 * and the line number where the error is encountered.
311 */
312static int hlua_pusherror(lua_State *L, const char *fmt, ...)
313{
314 va_list argp;
315 va_start(argp, fmt);
316 luaL_where(L, 1);
317 lua_pushvfstring(L, fmt, argp);
318 va_end(argp);
319 lua_concat(L, 2);
320 return 1;
321}
322
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100323/* This function register a new signal. "lua" is the current lua
324 * execution context. It contains a pointer to the associated task.
325 * "link" is a list head attached to an other task that must be wake
326 * the lua task if an event occurs. This is useful with external
327 * events like TCP I/O or sleep functions. This funcion allocate
328 * memory for the signal.
329 */
330static int hlua_com_new(struct hlua *lua, struct list *link)
331{
332 struct hlua_com *com = pool_alloc2(pool2_hlua_com);
333 if (!com)
334 return 0;
335 LIST_ADDQ(&lua->com, &com->purge_me);
336 LIST_ADDQ(link, &com->wake_me);
337 com->task = lua->task;
338 return 1;
339}
340
341/* This function purge all the pending signals when the LUA execution
342 * is finished. This prevent than a coprocess try to wake a deleted
343 * task. This function remove the memory associated to the signal.
344 */
345static void hlua_com_purge(struct hlua *lua)
346{
347 struct hlua_com *com, *back;
348
349 /* Delete all pending communication signals. */
350 list_for_each_entry_safe(com, back, &lua->com, purge_me) {
351 LIST_DEL(&com->purge_me);
352 LIST_DEL(&com->wake_me);
353 pool_free2(pool2_hlua_com, com);
354 }
355}
356
357/* This function sends signals. It wakes all the tasks attached
358 * to a list head, and remove the signal, and free the used
359 * memory.
360 */
361static void hlua_com_wake(struct list *wake)
362{
363 struct hlua_com *com, *back;
364
365 /* Wake task and delete all pending communication signals. */
366 list_for_each_entry_safe(com, back, wake, wake_me) {
367 LIST_DEL(&com->purge_me);
368 LIST_DEL(&com->wake_me);
369 task_wakeup(com->task, TASK_WOKEN_MSG);
370 pool_free2(pool2_hlua_com, com);
371 }
372}
373
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100374/* This functions is used with sample fetch and converters. It
375 * converts the HAProxy configuration argument in a lua stack
376 * values.
377 *
378 * It takes an array of "arg", and each entry of the array is
379 * converted and pushed in the LUA stack.
380 */
381static int hlua_arg2lua(lua_State *L, const struct arg *arg)
382{
383 switch (arg->type) {
384 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100385 case ARGT_TIME:
386 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100387 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100388 break;
389
390 case ARGT_STR:
391 lua_pushlstring(L, arg->data.str.str, arg->data.str.len);
392 break;
393
394 case ARGT_IPV4:
395 case ARGT_IPV6:
396 case ARGT_MSK4:
397 case ARGT_MSK6:
398 case ARGT_FE:
399 case ARGT_BE:
400 case ARGT_TAB:
401 case ARGT_SRV:
402 case ARGT_USR:
403 case ARGT_MAP:
404 default:
405 lua_pushnil(L);
406 break;
407 }
408 return 1;
409}
410
411/* This function take one entrie in an LUA stack at the index "ud",
412 * and try to convert it in an HAProxy argument entry. This is useful
413 * with sample fetch wrappers. The input arguments are gived to the
414 * lua wrapper and converted as arg list by thi function.
415 */
416static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
417{
418 switch (lua_type(L, ud)) {
419
420 case LUA_TNUMBER:
421 case LUA_TBOOLEAN:
422 arg->type = ARGT_SINT;
423 arg->data.sint = lua_tointeger(L, ud);
424 break;
425
426 case LUA_TSTRING:
427 arg->type = ARGT_STR;
428 arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len);
429 break;
430
431 case LUA_TUSERDATA:
432 case LUA_TNIL:
433 case LUA_TTABLE:
434 case LUA_TFUNCTION:
435 case LUA_TTHREAD:
436 case LUA_TLIGHTUSERDATA:
437 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200438 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100439 break;
440 }
441 return 1;
442}
443
444/* the following functions are used to convert a struct sample
445 * in Lua type. This useful to convert the return of the
446 * fetchs or converters.
447 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100448static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200450 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100451 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200453 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100454 break;
455
456 case SMP_T_BIN:
457 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200458 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100459 break;
460
461 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200462 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100463 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
464 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
465 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
466 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
467 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
468 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
469 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
470 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
471 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200472 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100473 break;
474 default:
475 lua_pushnil(L);
476 break;
477 }
478 break;
479
480 case SMP_T_IPV4:
481 case SMP_T_IPV6:
482 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200483 if (sample_casts[smp->data.type][SMP_T_STR] &&
484 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200485 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100486 else
487 lua_pushnil(L);
488 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100489 default:
490 lua_pushnil(L);
491 break;
492 }
493 return 1;
494}
495
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100496/* the following functions are used to convert a struct sample
497 * in Lua strings. This is useful to convert the return of the
498 * fetchs or converters.
499 */
500static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
501{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200502 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100503
504 case SMP_T_BIN:
505 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200506 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100507 break;
508
509 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200510 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100511 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
512 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
513 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
514 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
515 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
516 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
517 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
518 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
519 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200520 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100521 break;
522 default:
523 lua_pushstring(L, "");
524 break;
525 }
526 break;
527
528 case SMP_T_SINT:
529 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100530 case SMP_T_IPV4:
531 case SMP_T_IPV6:
532 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200533 if (sample_casts[smp->data.type][SMP_T_STR] &&
534 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200535 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100536 else
537 lua_pushstring(L, "");
538 break;
539 default:
540 lua_pushstring(L, "");
541 break;
542 }
543 return 1;
544}
545
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100546/* the following functions are used to convert an Lua type in a
547 * struct sample. This is useful to provide data from a converter
548 * to the LUA code.
549 */
550static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
551{
552 switch (lua_type(L, ud)) {
553
554 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200555 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100557 break;
558
559
560 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200561 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200562 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100563 break;
564
565 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200566 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100567 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200568 smp->data.u.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100569 break;
570
571 case LUA_TUSERDATA:
572 case LUA_TNIL:
573 case LUA_TTABLE:
574 case LUA_TFUNCTION:
575 case LUA_TTHREAD:
576 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200577 case LUA_TNONE:
578 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200579 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200580 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100581 break;
582 }
583 return 1;
584}
585
586/* This function check the "argp" builded by another conversion function
587 * is in accord with the expected argp defined by the "mask". The fucntion
588 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100589 *
590 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
591 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100592 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100593__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
594 unsigned int mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100595{
596 int min_arg;
597 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100598 struct proxy *px;
599 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100600
601 idx = 0;
602 min_arg = ARGM(mask);
603 mask >>= ARGM_BITS;
604
605 while (1) {
606
607 /* Check oversize. */
608 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100609 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100610 }
611
612 /* Check for mandatory arguments. */
613 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100614 if (idx < min_arg) {
615
616 /* If miss other argument than the first one, we return an error. */
617 if (idx > 0)
618 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
619
620 /* If first argument have a certain type, some default values
621 * may be used. See the function smp_resolve_args().
622 */
623 switch (mask & ARGT_MASK) {
624
625 case ARGT_FE:
626 if (!(p->cap & PR_CAP_FE))
627 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
628 argp[idx].data.prx = p;
629 argp[idx].type = ARGT_FE;
630 argp[idx+1].type = ARGT_STOP;
631 break;
632
633 case ARGT_BE:
634 if (!(p->cap & PR_CAP_BE))
635 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
636 argp[idx].data.prx = p;
637 argp[idx].type = ARGT_BE;
638 argp[idx+1].type = ARGT_STOP;
639 break;
640
641 case ARGT_TAB:
642 argp[idx].data.prx = p;
643 argp[idx].type = ARGT_TAB;
644 argp[idx+1].type = ARGT_STOP;
645 break;
646
647 default:
648 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
649 break;
650 }
651 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100652 return 0;
653 }
654
655 /* Check for exceed the number of requiered argument. */
656 if ((mask & ARGT_MASK) == ARGT_STOP &&
657 argp[idx].type != ARGT_STOP) {
658 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
659 }
660
661 if ((mask & ARGT_MASK) == ARGT_STOP &&
662 argp[idx].type == ARGT_STOP) {
663 return 0;
664 }
665
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100666 /* Convert some argument types. */
667 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100668 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100669 if (argp[idx].type != ARGT_SINT)
670 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
671 argp[idx].type = ARGT_SINT;
672 break;
673
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100674 case ARGT_TIME:
675 if (argp[idx].type != ARGT_SINT)
676 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200677 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100678 break;
679
680 case ARGT_SIZE:
681 if (argp[idx].type != ARGT_SINT)
682 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200683 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100684 break;
685
686 case ARGT_FE:
687 if (argp[idx].type != ARGT_STR)
688 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
689 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
690 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200691 argp[idx].data.prx = proxy_fe_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100692 if (!argp[idx].data.prx)
693 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
694 argp[idx].type = ARGT_FE;
695 break;
696
697 case ARGT_BE:
698 if (argp[idx].type != ARGT_STR)
699 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
700 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
701 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200702 argp[idx].data.prx = proxy_be_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100703 if (!argp[idx].data.prx)
704 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
705 argp[idx].type = ARGT_BE;
706 break;
707
708 case ARGT_TAB:
709 if (argp[idx].type != ARGT_STR)
710 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
711 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
712 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200713 argp[idx].data.prx = proxy_tbl_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100714 if (!argp[idx].data.prx)
715 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
716 argp[idx].type = ARGT_TAB;
717 break;
718
719 case ARGT_SRV:
720 if (argp[idx].type != ARGT_STR)
721 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
722 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
723 trash.str[argp[idx].data.str.len] = 0;
724 sname = strrchr(trash.str, '/');
725 if (sname) {
726 *sname++ = '\0';
727 pname = trash.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200728 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100729 if (!px)
730 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
731 }
732 else {
733 sname = trash.str;
734 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100735 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100736 argp[idx].data.srv = findserver(px, sname);
737 if (!argp[idx].data.srv)
738 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
739 argp[idx].type = ARGT_SRV;
740 break;
741
742 case ARGT_IPV4:
743 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
744 trash.str[argp[idx].data.str.len] = 0;
745 if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4))
746 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
747 argp[idx].type = ARGT_IPV4;
748 break;
749
750 case ARGT_MSK4:
751 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
752 trash.str[argp[idx].data.str.len] = 0;
753 if (!str2mask(trash.str, &argp[idx].data.ipv4))
754 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
755 argp[idx].type = ARGT_MSK4;
756 break;
757
758 case ARGT_IPV6:
759 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
760 trash.str[argp[idx].data.str.len] = 0;
761 if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6))
762 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
763 argp[idx].type = ARGT_IPV6;
764 break;
765
766 case ARGT_MSK6:
767 case ARGT_MAP:
768 case ARGT_REG:
769 case ARGT_USR:
770 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100771 break;
772 }
773
774 /* Check for type of argument. */
775 if ((mask & ARGT_MASK) != argp[idx].type) {
776 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
777 arg_type_names[(mask & ARGT_MASK)],
778 arg_type_names[argp[idx].type & ARGT_MASK]);
779 WILL_LJMP(luaL_argerror(L, first + idx, msg));
780 }
781
782 /* Next argument. */
783 mask >>= ARGT_BITS;
784 idx++;
785 }
786}
787
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100788/*
789 * The following functions are used to make correspondance between the the
790 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100791 *
792 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100793 * - hlua_sethlua : create the association between hlua context and lua_state.
794 */
795static inline struct hlua *hlua_gethlua(lua_State *L)
796{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100797 struct hlua **hlua = lua_getextraspace(L);
798 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100799}
800static inline void hlua_sethlua(struct hlua *hlua)
801{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100802 struct hlua **hlua_store = lua_getextraspace(hlua->T);
803 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100804}
805
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100806/* This function is used to send logs. It try to send on screen (stderr)
807 * and on the default syslog server.
808 */
809static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
810{
811 struct tm tm;
812 char *p;
813
814 /* Cleanup the log message. */
815 p = trash.str;
816 for (; *msg != '\0'; msg++, p++) {
817 if (p >= trash.str + trash.size - 1)
818 return;
819 if (isprint(*msg))
820 *p = *msg;
821 else
822 *p = '.';
823 }
824 *p = '\0';
825
Thierry FOURNIER5554e292015-09-09 11:21:37 +0200826 send_log(px, level, "%s\n", trash.str);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100827 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200828 get_localtime(date.tv_sec, &tm);
829 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100830 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
831 (int)getpid(), trash.str);
832 fflush(stderr);
833 }
834}
835
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100836/* This function just ensure that the yield will be always
837 * returned with a timeout and permit to set some flags
838 */
839__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100840 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100841{
842 struct hlua *hlua = hlua_gethlua(L);
843
844 /* Set the wake timeout. If timeout is required, we set
845 * the expiration time.
846 */
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100847 hlua->wake_time = tick_first(timeout, hlua->expire);
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100848
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100849 hlua->flags |= flags;
850
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100851 /* Process the yield. */
852 WILL_LJMP(lua_yieldk(L, nresults, ctx, k));
853}
854
Willy Tarreau87b09662015-04-03 00:22:06 +0200855/* This function initialises the Lua environment stored in the stream.
856 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100857 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200858 *
859 * This function is particular. it initialises a new Lua thread. If the
860 * initialisation fails (example: out of memory error), the lua function
861 * throws an error (longjmp).
862 *
863 * This function manipulates two Lua stack: the main and the thread. Only
864 * the main stack can fail. The thread is not manipulated. This function
865 * MUST NOT manipulate the created thread stack state, because is not
866 * proctected agains error throwed by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100867 */
868int hlua_ctx_init(struct hlua *lua, struct task *task)
869{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200870 if (!SET_SAFE_LJMP(gL.T)) {
871 lua->Tref = LUA_REFNIL;
872 return 0;
873 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100874 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100875 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100876 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100877 lua->T = lua_newthread(gL.T);
878 if (!lua->T) {
879 lua->Tref = LUA_REFNIL;
880 return 0;
881 }
882 hlua_sethlua(lua);
883 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
884 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200885 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100886 return 1;
887}
888
Willy Tarreau87b09662015-04-03 00:22:06 +0200889/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100890 * is destroyed. The destroy also the memory context. The struct "lua"
891 * is not freed.
892 */
893void hlua_ctx_destroy(struct hlua *lua)
894{
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100895 if (!lua->T)
896 return;
897
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100898 /* Purge all the pending signals. */
899 hlua_com_purge(lua);
900
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100901 /* The thread is garbage collected by Lua. */
902 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
903 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
904}
905
906/* This function is used to restore the Lua context when a coroutine
907 * fails. This function copy the common memory between old coroutine
908 * and the new coroutine. The old coroutine is destroyed, and its
909 * replaced by the new coroutine.
910 * If the flag "keep_msg" is set, the last entry of the old is assumed
911 * as string error message and it is copied in the new stack.
912 */
913static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
914{
915 lua_State *T;
916 int new_ref;
917
918 /* Renew the main LUA stack doesn't have sense. */
919 if (lua == &gL)
920 return 0;
921
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100922 /* New Lua coroutine. */
923 T = lua_newthread(gL.T);
924 if (!T)
925 return 0;
926
927 /* Copy last error message. */
928 if (keep_msg)
929 lua_xmove(lua->T, T, 1);
930
931 /* Copy data between the coroutines. */
932 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
933 lua_xmove(lua->T, T, 1);
934 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
935
936 /* Destroy old data. */
937 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
938
939 /* The thread is garbage collected by Lua. */
940 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
941
942 /* Fill the struct with the new coroutine values. */
943 lua->Mref = new_ref;
944 lua->T = T;
945 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
946
947 /* Set context. */
948 hlua_sethlua(lua);
949
950 return 1;
951}
952
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100953void hlua_hook(lua_State *L, lua_Debug *ar)
954{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100955 struct hlua *hlua = hlua_gethlua(L);
956
957 /* Lua cannot yield when its returning from a function,
958 * so, we can fix the interrupt hook to 1 instruction,
959 * expecting that the function is finnished.
960 */
961 if (lua_gethookmask(L) & LUA_MASKRET) {
962 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
963 return;
964 }
965
966 /* restore the interrupt condition. */
967 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
968
969 /* If we interrupt the Lua processing in yieldable state, we yield.
970 * If the state is not yieldable, trying yield causes an error.
971 */
972 if (lua_isyieldable(L))
973 WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
974
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +0100975 /* If we cannot yield, update the clock and check the timeout. */
976 tv_update_date(0, 1);
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100977 if (tick_is_expired(hlua->expire, now_ms)) {
978 lua_pushfstring(L, "execution timeout");
979 WILL_LJMP(lua_error(L));
980 }
981
982 /* Try to interrupt the process at the end of the current
983 * unyieldable function.
984 */
985 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100986}
987
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100988/* This function start or resumes the Lua stack execution. If the flag
989 * "yield_allowed" if no set and the LUA stack execution returns a yield
990 * The function return an error.
991 *
992 * The function can returns 4 values:
993 * - HLUA_E_OK : The execution is terminated without any errors.
994 * - HLUA_E_AGAIN : The execution must continue at the next associated
995 * task wakeup.
996 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
997 * the top of the stack.
998 * - HLUA_E_ERR : An error has occured without error message.
999 *
1000 * If an error occured, the stack is renewed and it is ready to run new
1001 * LUA code.
1002 */
1003static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1004{
1005 int ret;
1006 const char *msg;
1007
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001008 HLUA_SET_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001009
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001010 /* If we want to resume the task, then check first the execution timeout.
1011 * if it is reached, we can interrupt the Lua processing.
1012 */
1013 if (tick_is_expired(lua->expire, now_ms))
1014 goto timeout_reached;
1015
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001016resume_execution:
1017
1018 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1019 * instructions. it is used for preventing infinite loops.
1020 */
1021 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1022
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001023 /* Remove all flags except the running flags. */
1024 lua->flags = HLUA_RUN;
1025
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001026 /* Call the function. */
1027 ret = lua_resume(lua->T, gL.T, lua->nargs);
1028 switch (ret) {
1029
1030 case LUA_OK:
1031 ret = HLUA_E_OK;
1032 break;
1033
1034 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001035 /* Check if the execution timeout is expired. It it is the case, we
1036 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001037 */
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001038 if (tick_is_expired(lua->expire, now_ms)) {
1039
1040timeout_reached:
1041
1042 lua_settop(lua->T, 0); /* Empty the stack. */
1043 if (!lua_checkstack(lua->T, 1)) {
1044 ret = HLUA_E_ERR;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001045 break;
1046 }
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001047 lua_pushfstring(lua->T, "execution timeout");
1048 ret = HLUA_E_ERRMSG;
1049 break;
1050 }
1051 /* Process the forced yield. if the general yield is not allowed or
1052 * if no task were associated this the current Lua execution
1053 * coroutine, we resume the execution. Else we want to return in the
1054 * scheduler and we want to be waked up again, to continue the
1055 * current Lua execution. So we schedule our own task.
1056 */
1057 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001058 if (!yield_allowed || !lua->task)
1059 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001060 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001061 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001062 if (!yield_allowed) {
1063 lua_settop(lua->T, 0); /* Empty the stack. */
1064 if (!lua_checkstack(lua->T, 1)) {
1065 ret = HLUA_E_ERR;
1066 break;
1067 }
1068 lua_pushfstring(lua->T, "yield not allowed");
1069 ret = HLUA_E_ERRMSG;
1070 break;
1071 }
1072 ret = HLUA_E_AGAIN;
1073 break;
1074
1075 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001076
1077 /* Special exit case. The traditionnal exit is returned as an error
1078 * because the errors ares the only one mean to return immediately
1079 * from and lua execution.
1080 */
1081 if (lua->flags & HLUA_EXIT) {
1082 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001083 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001084 break;
1085 }
1086
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001087 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001088 if (!lua_checkstack(lua->T, 1)) {
1089 ret = HLUA_E_ERR;
1090 break;
1091 }
1092 msg = lua_tostring(lua->T, -1);
1093 lua_settop(lua->T, 0); /* Empty the stack. */
1094 lua_pop(lua->T, 1);
1095 if (msg)
1096 lua_pushfstring(lua->T, "runtime error: %s", msg);
1097 else
1098 lua_pushfstring(lua->T, "unknown runtime error");
1099 ret = HLUA_E_ERRMSG;
1100 break;
1101
1102 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001103 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001104 lua_settop(lua->T, 0); /* Empty the stack. */
1105 if (!lua_checkstack(lua->T, 1)) {
1106 ret = HLUA_E_ERR;
1107 break;
1108 }
1109 lua_pushfstring(lua->T, "out of memory error");
1110 ret = HLUA_E_ERRMSG;
1111 break;
1112
1113 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001114 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001115 if (!lua_checkstack(lua->T, 1)) {
1116 ret = HLUA_E_ERR;
1117 break;
1118 }
1119 msg = lua_tostring(lua->T, -1);
1120 lua_settop(lua->T, 0); /* Empty the stack. */
1121 lua_pop(lua->T, 1);
1122 if (msg)
1123 lua_pushfstring(lua->T, "message handler error: %s", msg);
1124 else
1125 lua_pushfstring(lua->T, "message handler error");
1126 ret = HLUA_E_ERRMSG;
1127 break;
1128
1129 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001130 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001131 lua_settop(lua->T, 0); /* Empty the stack. */
1132 if (!lua_checkstack(lua->T, 1)) {
1133 ret = HLUA_E_ERR;
1134 break;
1135 }
1136 lua_pushfstring(lua->T, "unknonwn error");
1137 ret = HLUA_E_ERRMSG;
1138 break;
1139 }
1140
1141 switch (ret) {
1142 case HLUA_E_AGAIN:
1143 break;
1144
1145 case HLUA_E_ERRMSG:
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001146 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001147 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001148 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001149 break;
1150
1151 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001152 HLUA_CLR_RUN(lua);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001153 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001154 hlua_ctx_renew(lua, 0);
1155 break;
1156
1157 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001158 HLUA_CLR_RUN(lua);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001159 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160 break;
1161 }
1162
1163 return ret;
1164}
1165
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001166/* This function exit the current code. */
1167__LJMP static int hlua_done(lua_State *L)
1168{
1169 struct hlua *hlua = hlua_gethlua(L);
1170
1171 hlua->flags |= HLUA_EXIT;
1172 WILL_LJMP(lua_error(L));
1173
1174 return 0;
1175}
1176
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001177/* This function is an LUA binding. It provides a function
1178 * for deleting ACL from a referenced ACL file.
1179 */
1180__LJMP static int hlua_del_acl(lua_State *L)
1181{
1182 const char *name;
1183 const char *key;
1184 struct pat_ref *ref;
1185
1186 MAY_LJMP(check_args(L, 2, "del_acl"));
1187
1188 name = MAY_LJMP(luaL_checkstring(L, 1));
1189 key = MAY_LJMP(luaL_checkstring(L, 2));
1190
1191 ref = pat_ref_lookup(name);
1192 if (!ref)
1193 WILL_LJMP(luaL_error(L, "'del_acl': unkown acl file '%s'", name));
1194
1195 pat_ref_delete(ref, key);
1196 return 0;
1197}
1198
1199/* This function is an LUA binding. It provides a function
1200 * for deleting map entry from a referenced map file.
1201 */
1202static int hlua_del_map(lua_State *L)
1203{
1204 const char *name;
1205 const char *key;
1206 struct pat_ref *ref;
1207
1208 MAY_LJMP(check_args(L, 2, "del_map"));
1209
1210 name = MAY_LJMP(luaL_checkstring(L, 1));
1211 key = MAY_LJMP(luaL_checkstring(L, 2));
1212
1213 ref = pat_ref_lookup(name);
1214 if (!ref)
1215 WILL_LJMP(luaL_error(L, "'del_map': unkown acl file '%s'", name));
1216
1217 pat_ref_delete(ref, key);
1218 return 0;
1219}
1220
1221/* This function is an LUA binding. It provides a function
1222 * for adding ACL pattern from a referenced ACL file.
1223 */
1224static int hlua_add_acl(lua_State *L)
1225{
1226 const char *name;
1227 const char *key;
1228 struct pat_ref *ref;
1229
1230 MAY_LJMP(check_args(L, 2, "add_acl"));
1231
1232 name = MAY_LJMP(luaL_checkstring(L, 1));
1233 key = MAY_LJMP(luaL_checkstring(L, 2));
1234
1235 ref = pat_ref_lookup(name);
1236 if (!ref)
1237 WILL_LJMP(luaL_error(L, "'add_acl': unkown acl file '%s'", name));
1238
1239 if (pat_ref_find_elt(ref, key) == NULL)
1240 pat_ref_add(ref, key, NULL, NULL);
1241 return 0;
1242}
1243
1244/* This function is an LUA binding. It provides a function
1245 * for setting map pattern and sample from a referenced map
1246 * file.
1247 */
1248static int hlua_set_map(lua_State *L)
1249{
1250 const char *name;
1251 const char *key;
1252 const char *value;
1253 struct pat_ref *ref;
1254
1255 MAY_LJMP(check_args(L, 3, "set_map"));
1256
1257 name = MAY_LJMP(luaL_checkstring(L, 1));
1258 key = MAY_LJMP(luaL_checkstring(L, 2));
1259 value = MAY_LJMP(luaL_checkstring(L, 3));
1260
1261 ref = pat_ref_lookup(name);
1262 if (!ref)
1263 WILL_LJMP(luaL_error(L, "'set_map': unkown map file '%s'", name));
1264
1265 if (pat_ref_find_elt(ref, key) != NULL)
1266 pat_ref_set(ref, key, value, NULL);
1267 else
1268 pat_ref_add(ref, key, value, NULL);
1269 return 0;
1270}
1271
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001272/* A class is a lot of memory that contain data. This data can be a table,
1273 * an integer or user data. This data is associated with a metatable. This
1274 * metatable have an original version registred in the global context with
1275 * the name of the object (_G[<name>] = <metable> ).
1276 *
1277 * A metable is a table that modify the standard behavior of a standard
1278 * access to the associated data. The entries of this new metatable are
1279 * defined as is:
1280 *
1281 * http://lua-users.org/wiki/MetatableEvents
1282 *
1283 * __index
1284 *
1285 * we access an absent field in a table, the result is nil. This is
1286 * true, but it is not the whole truth. Actually, such access triggers
1287 * the interpreter to look for an __index metamethod: If there is no
1288 * such method, as usually happens, then the access results in nil;
1289 * otherwise, the metamethod will provide the result.
1290 *
1291 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1292 * the key does not appear in the table, but the metatable has an __index
1293 * property:
1294 *
1295 * - if the value is a function, the function is called, passing in the
1296 * table and the key; the return value of that function is returned as
1297 * the result.
1298 *
1299 * - if the value is another table, the value of the key in that table is
1300 * asked for and returned (and if it doesn't exist in that table, but that
1301 * table's metatable has an __index property, then it continues on up)
1302 *
1303 * - Use "rawget(myTable,key)" to skip this metamethod.
1304 *
1305 * http://www.lua.org/pil/13.4.1.html
1306 *
1307 * __newindex
1308 *
1309 * Like __index, but control property assignment.
1310 *
1311 * __mode - Control weak references. A string value with one or both
1312 * of the characters 'k' and 'v' which specifies that the the
1313 * keys and/or values in the table are weak references.
1314 *
1315 * __call - Treat a table like a function. When a table is followed by
1316 * parenthesis such as "myTable( 'foo' )" and the metatable has
1317 * a __call key pointing to a function, that function is invoked
1318 * (passing any specified arguments) and the return value is
1319 * returned.
1320 *
1321 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1322 * called, if the metatable for myTable has a __metatable
1323 * key, the value of that key is returned instead of the
1324 * actual metatable.
1325 *
1326 * __tostring - Control string representation. When the builtin
1327 * "tostring( myTable )" function is called, if the metatable
1328 * for myTable has a __tostring property set to a function,
1329 * that function is invoked (passing myTable to it) and the
1330 * return value is used as the string representation.
1331 *
1332 * __len - Control table length. When the table length is requested using
1333 * the length operator ( '#' ), if the metatable for myTable has
1334 * a __len key pointing to a function, that function is invoked
1335 * (passing myTable to it) and the return value used as the value
1336 * of "#myTable".
1337 *
1338 * __gc - Userdata finalizer code. When userdata is set to be garbage
1339 * collected, if the metatable has a __gc field pointing to a
1340 * function, that function is first invoked, passing the userdata
1341 * to it. The __gc metamethod is not called for tables.
1342 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1343 *
1344 * Special metamethods for redefining standard operators:
1345 * http://www.lua.org/pil/13.1.html
1346 *
1347 * __add "+"
1348 * __sub "-"
1349 * __mul "*"
1350 * __div "/"
1351 * __unm "!"
1352 * __pow "^"
1353 * __concat ".."
1354 *
1355 * Special methods for redfining standar relations
1356 * http://www.lua.org/pil/13.2.html
1357 *
1358 * __eq "=="
1359 * __lt "<"
1360 * __le "<="
1361 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001362
1363/*
1364 *
1365 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001366 * Class Map
1367 *
1368 *
1369 */
1370
1371/* Returns a struct hlua_map if the stack entry "ud" is
1372 * a class session, otherwise it throws an error.
1373 */
1374__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1375{
1376 return (struct map_descriptor *)MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
1377}
1378
1379/* This function is the map constructor. It don't need
1380 * the class Map object. It creates and return a new Map
1381 * object. It must be called only during "body" or "init"
1382 * context because it process some filesystem accesses.
1383 */
1384__LJMP static int hlua_map_new(struct lua_State *L)
1385{
1386 const char *fn;
1387 int match = PAT_MATCH_STR;
1388 struct sample_conv conv;
1389 const char *file = "";
1390 int line = 0;
1391 lua_Debug ar;
1392 char *err = NULL;
1393 struct arg args[2];
1394
1395 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1396 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1397
1398 fn = MAY_LJMP(luaL_checkstring(L, 1));
1399
1400 if (lua_gettop(L) >= 2) {
1401 match = MAY_LJMP(luaL_checkinteger(L, 2));
1402 if (match < 0 || match >= PAT_MATCH_NUM)
1403 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1404 }
1405
1406 /* Get Lua filename and line number. */
1407 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1408 lua_getinfo(L, "Sl", &ar); /* get info about it */
1409 if (ar.currentline > 0) { /* is there info? */
1410 file = ar.short_src;
1411 line = ar.currentline;
1412 }
1413 }
1414
1415 /* fill fake sample_conv struct. */
1416 conv.kw = ""; /* unused. */
1417 conv.process = NULL; /* unused. */
1418 conv.arg_mask = 0; /* unused. */
1419 conv.val_args = NULL; /* unused. */
1420 conv.out_type = SMP_T_STR;
1421 conv.private = (void *)(long)match;
1422 switch (match) {
1423 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1424 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1425 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1426 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1427 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1428 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1429 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001430 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001431 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1432 default:
1433 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1434 }
1435
1436 /* fill fake args. */
1437 args[0].type = ARGT_STR;
1438 args[0].data.str.str = (char *)fn;
1439 args[1].type = ARGT_STOP;
1440
1441 /* load the map. */
1442 if (!sample_load_map(args, &conv, file, line, &err)) {
1443 /* error case: we cant use luaL_error because we must
1444 * free the err variable.
1445 */
1446 luaL_where(L, 1);
1447 lua_pushfstring(L, "'new': %s.", err);
1448 lua_concat(L, 2);
1449 free(err);
1450 WILL_LJMP(lua_error(L));
1451 }
1452
1453 /* create the lua object. */
1454 lua_newtable(L);
1455 lua_pushlightuserdata(L, args[0].data.map);
1456 lua_rawseti(L, -2, 0);
1457
1458 /* Pop a class Map metatable and affect it to the userdata. */
1459 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1460 lua_setmetatable(L, -2);
1461
1462
1463 return 1;
1464}
1465
1466__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1467{
1468 struct map_descriptor *desc;
1469 struct pattern *pat;
1470 struct sample smp;
1471
1472 MAY_LJMP(check_args(L, 2, "lookup"));
1473 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001474 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001475 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001476 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001477 }
1478 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001479 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001480 smp.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001481 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 +02001482 }
1483
1484 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001485 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001486 if (str)
1487 lua_pushstring(L, "");
1488 else
1489 lua_pushnil(L);
1490 return 1;
1491 }
1492
1493 /* The Lua pattern must return a string, so we can't check the returned type */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001494 lua_pushlstring(L, pat->data->u.str.str, pat->data->u.str.len);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001495 return 1;
1496}
1497
1498__LJMP static int hlua_map_lookup(struct lua_State *L)
1499{
1500 return _hlua_map_lookup(L, 0);
1501}
1502
1503__LJMP static int hlua_map_slookup(struct lua_State *L)
1504{
1505 return _hlua_map_lookup(L, 1);
1506}
1507
1508/*
1509 *
1510 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001511 * Class Socket
1512 *
1513 *
1514 */
1515
1516__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1517{
1518 return (struct hlua_socket *)MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
1519}
1520
1521/* This function is the handler called for each I/O on the established
1522 * connection. It is used for notify space avalaible to send or data
1523 * received.
1524 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001525static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001526{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001527 struct stream_interface *si = appctx->owner;
Willy Tarreau50fe03b2014-11-28 13:59:31 +01001528 struct connection *c = objt_conn(si_opposite(si)->end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001529
Willy Tarreau87b09662015-04-03 00:22:06 +02001530 /* Wakeup the main stream if the client connection is closed. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001531 if (!c || channel_output_closed(si_ic(si)) || channel_input_closed(si_oc(si))) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001532 if (appctx->ctx.hlua.socket) {
1533 appctx->ctx.hlua.socket->s = NULL;
1534 appctx->ctx.hlua.socket = NULL;
1535 }
1536 si_shutw(si);
1537 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001538 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001539 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1540 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001541 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001542 }
1543
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001544 /* if the connection is not estabkished, inform the stream that we want
1545 * to be notified whenever the connection completes.
1546 */
1547 if (!(c->flags & CO_FL_CONNECTED)) {
1548 si_applet_cant_get(si);
1549 si_applet_cant_put(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001550 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001551 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001552
1553 /* This function is called after the connect. */
1554 appctx->ctx.hlua.connected = 1;
1555
1556 /* Wake the tasks which wants to write if the buffer have avalaible space. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001557 if (channel_may_recv(si_oc(si)))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001558 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
1559
1560 /* Wake the tasks which wants to read if the buffer contains data. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001561 if (channel_is_empty(si_ic(si)))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001562 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1563}
1564
Willy Tarreau87b09662015-04-03 00:22:06 +02001565/* This function is called when the "struct stream" is destroyed.
1566 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001567 * Wake all the pending signals.
1568 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001569static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001570{
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001571 /* Remove my link in the original object. */
1572 if (appctx->ctx.hlua.socket)
1573 appctx->ctx.hlua.socket->s = NULL;
1574
1575 /* Wake all the task waiting for me. */
1576 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1577 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
1578}
1579
1580/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001581 * uses this object. If the stream does not exists, just quit.
1582 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001583 * pending signal can rest in the read and write lists. destroy
1584 * it.
1585 */
1586__LJMP static int hlua_socket_gc(lua_State *L)
1587{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001588 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001589 struct appctx *appctx;
1590
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001591 MAY_LJMP(check_args(L, 1, "__gc"));
1592
1593 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001594 if (!socket->s)
1595 return 0;
1596
Willy Tarreau87b09662015-04-03 00:22:06 +02001597 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001598 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001599 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001600 socket->s = NULL;
1601 appctx->ctx.hlua.socket = NULL;
1602
1603 return 0;
1604}
1605
1606/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001607 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001608 */
1609__LJMP static int hlua_socket_close(lua_State *L)
1610{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001611 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001612 struct appctx *appctx;
1613
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001614 MAY_LJMP(check_args(L, 1, "close"));
1615
1616 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001617 if (!socket->s)
1618 return 0;
1619
Willy Tarreau87b09662015-04-03 00:22:06 +02001620 /* Close the stream and remove the associated stop task. */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001621 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001622 appctx = objt_appctx(socket->s->si[0].end);
1623 appctx->ctx.hlua.socket = NULL;
1624 socket->s = NULL;
1625
1626 return 0;
1627}
1628
1629/* This Lua function assumes that the stack contain three parameters.
1630 * 1 - USERDATA containing a struct socket
1631 * 2 - INTEGER with values of the macro defined below
1632 * If the integer is -1, we must read at most one line.
1633 * If the integer is -2, we ust read all the data until the
1634 * end of the stream.
1635 * If the integer is positive value, we must read a number of
1636 * bytes corresponding to this value.
1637 */
1638#define HLSR_READ_LINE (-1)
1639#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001640__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001641{
1642 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1643 int wanted = lua_tointeger(L, 2);
1644 struct hlua *hlua = hlua_gethlua(L);
1645 struct appctx *appctx;
1646 int len;
1647 int nblk;
1648 char *blk1;
1649 int len1;
1650 char *blk2;
1651 int len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001652 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001653 struct channel *oc;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001654
1655 /* Check if this lua stack is schedulable. */
1656 if (!hlua || !hlua->task)
1657 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1658 "'frontend', 'backend' or 'task'"));
1659
1660 /* check for connection closed. If some data where read, return it. */
1661 if (!socket->s)
1662 goto connection_closed;
1663
Willy Tarreau94aa6172015-03-13 14:19:06 +01001664 oc = &socket->s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001665 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001666 /* Read line. */
Willy Tarreau81389672015-03-10 12:03:52 +01001667 nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001668 if (nblk < 0) /* Connection close. */
1669 goto connection_closed;
1670 if (nblk == 0) /* No data avalaible. */
1671 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001672
1673 /* remove final \r\n. */
1674 if (nblk == 1) {
1675 if (blk1[len1-1] == '\n') {
1676 len1--;
1677 skip_at_end++;
1678 if (blk1[len1-1] == '\r') {
1679 len1--;
1680 skip_at_end++;
1681 }
1682 }
1683 }
1684 else {
1685 if (blk2[len2-1] == '\n') {
1686 len2--;
1687 skip_at_end++;
1688 if (blk2[len2-1] == '\r') {
1689 len2--;
1690 skip_at_end++;
1691 }
1692 }
1693 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001694 }
1695
1696 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001697 /* Read all the available data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001698 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001699 if (nblk < 0) /* Connection close. */
1700 goto connection_closed;
1701 if (nblk == 0) /* No data avalaible. */
1702 goto connection_empty;
1703 }
1704
1705 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001706 /* Read a block of data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001707 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001708 if (nblk < 0) /* Connection close. */
1709 goto connection_closed;
1710 if (nblk == 0) /* No data avalaible. */
1711 goto connection_empty;
1712
1713 if (len1 > wanted) {
1714 nblk = 1;
1715 len1 = wanted;
1716 } if (nblk == 2 && len1 + len2 > wanted)
1717 len2 = wanted - len1;
1718 }
1719
1720 len = len1;
1721
1722 luaL_addlstring(&socket->b, blk1, len1);
1723 if (nblk == 2) {
1724 len += len2;
1725 luaL_addlstring(&socket->b, blk2, len2);
1726 }
1727
1728 /* Consume data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001729 bo_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001730
1731 /* Don't wait anything. */
Willy Tarreau828824a2015-04-19 17:20:03 +02001732 si_applet_done(&socket->s->si[0]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001733
1734 /* If the pattern reclaim to read all the data
1735 * in the connection, got out.
1736 */
1737 if (wanted == HLSR_READ_ALL)
1738 goto connection_empty;
1739 else if (wanted >= 0 && len < wanted)
1740 goto connection_empty;
1741
1742 /* Return result. */
1743 luaL_pushresult(&socket->b);
1744 return 1;
1745
1746connection_closed:
1747
1748 /* If the buffer containds data. */
1749 if (socket->b.n > 0) {
1750 luaL_pushresult(&socket->b);
1751 return 1;
1752 }
1753 lua_pushnil(L);
1754 lua_pushstring(L, "connection closed.");
1755 return 2;
1756
1757connection_empty:
1758
1759 appctx = objt_appctx(socket->s->si[0].end);
1760 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_read))
1761 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001762 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001763 return 0;
1764}
1765
1766/* This Lus function gets two parameters. The first one can be string
1767 * or a number. If the string is "*l", the user require one line. If
1768 * the string is "*a", the user require all the content of the stream.
1769 * If the value is a number, the user require a number of bytes equal
1770 * to the value. The default value is "*l" (a line).
1771 *
1772 * This paraeter with a variable type is converted in integer. This
1773 * integer takes this values:
1774 * -1 : read a line
1775 * -2 : read all the stream
1776 * >0 : amount if bytes.
1777 *
1778 * The second parameter is optinal. It contains a string that must be
1779 * concatenated with the read data.
1780 */
1781__LJMP static int hlua_socket_receive(struct lua_State *L)
1782{
1783 int wanted = HLSR_READ_LINE;
1784 const char *pattern;
1785 int type;
1786 char *error;
1787 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001788 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001789
1790 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1791 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1792
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001793 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001794
1795 /* check for pattern. */
1796 if (lua_gettop(L) >= 2) {
1797 type = lua_type(L, 2);
1798 if (type == LUA_TSTRING) {
1799 pattern = lua_tostring(L, 2);
1800 if (strcmp(pattern, "*a") == 0)
1801 wanted = HLSR_READ_ALL;
1802 else if (strcmp(pattern, "*l") == 0)
1803 wanted = HLSR_READ_LINE;
1804 else {
1805 wanted = strtoll(pattern, &error, 10);
1806 if (*error != '\0')
1807 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1808 }
1809 }
1810 else if (type == LUA_TNUMBER) {
1811 wanted = lua_tointeger(L, 2);
1812 if (wanted < 0)
1813 WILL_LJMP(luaL_error(L, "Unsupported size."));
1814 }
1815 }
1816
1817 /* Set pattern. */
1818 lua_pushinteger(L, wanted);
1819 lua_replace(L, 2);
1820
1821 /* init bufffer, and fiil it wih prefix. */
1822 luaL_buffinit(L, &socket->b);
1823
1824 /* Check prefix. */
1825 if (lua_gettop(L) >= 3) {
1826 if (lua_type(L, 3) != LUA_TSTRING)
1827 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1828 pattern = lua_tolstring(L, 3, &len);
1829 luaL_addlstring(&socket->b, pattern, len);
1830 }
1831
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001832 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001833}
1834
1835/* Write the Lua input string in the output buffer.
1836 * This fucntion returns a yield if no space are available.
1837 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001838static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001839{
1840 struct hlua_socket *socket;
1841 struct hlua *hlua = hlua_gethlua(L);
1842 struct appctx *appctx;
1843 size_t buf_len;
1844 const char *buf;
1845 int len;
1846 int send_len;
1847 int sent;
1848
1849 /* Check if this lua stack is schedulable. */
1850 if (!hlua || !hlua->task)
1851 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1852 "'frontend', 'backend' or 'task'"));
1853
1854 /* Get object */
1855 socket = MAY_LJMP(hlua_checksocket(L, 1));
1856 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001857 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001858
1859 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001860 if (!socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001861 lua_pushinteger(L, -1);
1862 return 1;
1863 }
1864
1865 /* Update the input buffer data. */
1866 buf += sent;
1867 send_len = buf_len - sent;
1868
1869 /* All the data are sent. */
1870 if (sent >= buf_len)
1871 return 1; /* Implicitly return the length sent. */
1872
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001873 /* Check if the buffer is avalaible because HAProxy doesn't allocate
1874 * the request buffer if its not required.
1875 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001876 if (socket->s->req.buf->size == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001877 if (!stream_alloc_recv_buffer(&socket->s->req)) {
Willy Tarreau350f4872014-11-28 14:42:25 +01001878 socket->s->si[0].flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001879 goto hlua_socket_write_yield_return;
1880 }
1881 }
1882
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001883 /* Check for avalaible space. */
Willy Tarreau94aa6172015-03-13 14:19:06 +01001884 len = buffer_total_space(socket->s->req.buf);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001885 if (len <= 0)
1886 goto hlua_socket_write_yield_return;
1887
1888 /* send data */
1889 if (len < send_len)
1890 send_len = len;
Willy Tarreau94aa6172015-03-13 14:19:06 +01001891 len = bi_putblk(&socket->s->req, buf+sent, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001892
1893 /* "Not enough space" (-1), "Buffer too little to contain
1894 * the data" (-2) are not expected because the available length
1895 * is tested.
1896 * Other unknown error are also not expected.
1897 */
1898 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01001899 if (len == -1)
Willy Tarreau94aa6172015-03-13 14:19:06 +01001900 socket->s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001901
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001902 MAY_LJMP(hlua_socket_close(L));
1903 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001904 lua_pushinteger(L, -1);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001905 return 1;
1906 }
1907
1908 /* update buffers. */
Willy Tarreau828824a2015-04-19 17:20:03 +02001909 si_applet_done(&socket->s->si[0]);
Willy Tarreau94aa6172015-03-13 14:19:06 +01001910 socket->s->req.rex = TICK_ETERNITY;
1911 socket->s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001912
1913 /* Update length sent. */
1914 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001915 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001916
1917 /* All the data buffer is sent ? */
1918 if (sent + len >= buf_len)
1919 return 1;
1920
1921hlua_socket_write_yield_return:
1922 appctx = objt_appctx(socket->s->si[0].end);
1923 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
1924 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001925 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001926 return 0;
1927}
1928
1929/* This function initiate the send of data. It just check the input
1930 * parameters and push an integer in the Lua stack that contain the
1931 * amount of data writed in the buffer. This is used by the function
1932 * "hlua_socket_write_yield" that can yield.
1933 *
1934 * The Lua function gets between 3 and 4 parameters. The first one is
1935 * the associated object. The second is a string buffer. The third is
1936 * a facultative integer that represents where is the buffer position
1937 * of the start of the data that can send. The first byte is the
1938 * position "1". The default value is "1". The fourth argument is a
1939 * facultative integer that represents where is the buffer position
1940 * of the end of the data that can send. The default is the last byte.
1941 */
1942static int hlua_socket_send(struct lua_State *L)
1943{
1944 int i;
1945 int j;
1946 const char *buf;
1947 size_t buf_len;
1948
1949 /* Check number of arguments. */
1950 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
1951 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
1952
1953 /* Get the string. */
1954 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
1955
1956 /* Get and check j. */
1957 if (lua_gettop(L) == 4) {
1958 j = MAY_LJMP(luaL_checkinteger(L, 4));
1959 if (j < 0)
1960 j = buf_len + j + 1;
1961 if (j > buf_len)
1962 j = buf_len + 1;
1963 lua_pop(L, 1);
1964 }
1965 else
1966 j = buf_len;
1967
1968 /* Get and check i. */
1969 if (lua_gettop(L) == 3) {
1970 i = MAY_LJMP(luaL_checkinteger(L, 3));
1971 if (i < 0)
1972 i = buf_len + i + 1;
1973 if (i > buf_len)
1974 i = buf_len + 1;
1975 lua_pop(L, 1);
1976 } else
1977 i = 1;
1978
1979 /* Check bth i and j. */
1980 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001981 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001982 return 1;
1983 }
1984 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001985 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001986 return 1;
1987 }
1988 if (i == 0)
1989 i = 1;
1990 if (j == 0)
1991 j = 1;
1992
1993 /* Pop the string. */
1994 lua_pop(L, 1);
1995
1996 /* Update the buffer length. */
1997 buf += i - 1;
1998 buf_len = j - i + 1;
1999 lua_pushlstring(L, buf, buf_len);
2000
2001 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002002 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002003
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002004 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002005}
2006
Willy Tarreau22b0a682015-06-17 19:43:49 +02002007#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002008__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2009{
2010 static char buffer[SOCKET_INFO_MAX_LEN];
2011 int ret;
2012 int len;
2013 char *p;
2014
2015 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2016 if (ret <= 0) {
2017 lua_pushnil(L);
2018 return 1;
2019 }
2020
2021 if (ret == AF_UNIX) {
2022 lua_pushstring(L, buffer+1);
2023 return 1;
2024 }
2025 else if (ret == AF_INET6) {
2026 buffer[0] = '[';
2027 len = strlen(buffer);
2028 buffer[len] = ']';
2029 len++;
2030 buffer[len] = ':';
2031 len++;
2032 p = buffer;
2033 }
2034 else if (ret == AF_INET) {
2035 p = buffer + 1;
2036 len = strlen(p);
2037 p[len] = ':';
2038 len++;
2039 }
2040 else {
2041 lua_pushnil(L);
2042 return 1;
2043 }
2044
2045 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2046 lua_pushnil(L);
2047 return 1;
2048 }
2049
2050 lua_pushstring(L, p);
2051 return 1;
2052}
2053
2054/* Returns information about the peer of the connection. */
2055__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2056{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002057 struct hlua_socket *socket;
2058 struct connection *conn;
2059
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002060 MAY_LJMP(check_args(L, 1, "getpeername"));
2061
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002062 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002063
2064 /* Check if the tcp object is avalaible. */
2065 if (!socket->s) {
2066 lua_pushnil(L);
2067 return 1;
2068 }
2069
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002070 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002071 if (!conn) {
2072 lua_pushnil(L);
2073 return 1;
2074 }
2075
2076 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
2077 unsigned int salen = sizeof(conn->addr.to);
2078 if (getpeername(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, &salen) == -1) {
2079 lua_pushnil(L);
2080 return 1;
2081 }
2082 conn->flags |= CO_FL_ADDR_TO_SET;
2083 }
2084
2085 return MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2086}
2087
2088/* Returns information about my connection side. */
2089static int hlua_socket_getsockname(struct lua_State *L)
2090{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002091 struct hlua_socket *socket;
2092 struct connection *conn;
2093
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002094 MAY_LJMP(check_args(L, 1, "getsockname"));
2095
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002096 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002097
2098 /* Check if the tcp object is avalaible. */
2099 if (!socket->s) {
2100 lua_pushnil(L);
2101 return 1;
2102 }
2103
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002104 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002105 if (!conn) {
2106 lua_pushnil(L);
2107 return 1;
2108 }
2109
2110 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
2111 unsigned int salen = sizeof(conn->addr.from);
2112 if (getsockname(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, &salen) == -1) {
2113 lua_pushnil(L);
2114 return 1;
2115 }
2116 conn->flags |= CO_FL_ADDR_FROM_SET;
2117 }
2118
2119 return hlua_socket_info(L, &conn->addr.from);
2120}
2121
2122/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002123static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002124 .obj_type = OBJ_TYPE_APPLET,
2125 .name = "<LUA_TCP>",
2126 .fct = hlua_socket_handler,
2127 .release = hlua_socket_release,
2128};
2129
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002130__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002131{
2132 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2133 struct hlua *hlua = hlua_gethlua(L);
2134 struct appctx *appctx;
2135
2136 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002137 if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002138 lua_pushnil(L);
2139 lua_pushstring(L, "Can't connect");
2140 return 2;
2141 }
2142
2143 appctx = objt_appctx(socket->s->si[0].end);
2144
2145 /* Check for connection established. */
2146 if (appctx->ctx.hlua.connected) {
2147 lua_pushinteger(L, 1);
2148 return 1;
2149 }
2150
2151 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2152 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002153 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002154 return 0;
2155}
2156
2157/* This function fail or initite the connection. */
2158__LJMP static int hlua_socket_connect(struct lua_State *L)
2159{
2160 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002161 int port;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002162 const char *ip;
2163 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002164 struct hlua *hlua;
2165 struct appctx *appctx;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002166
2167 MAY_LJMP(check_args(L, 3, "connect"));
2168
2169 /* Get args. */
2170 socket = MAY_LJMP(hlua_checksocket(L, 1));
2171 ip = MAY_LJMP(luaL_checkstring(L, 2));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002172 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002173
Willy Tarreau973a5422015-08-05 21:47:23 +02002174 conn = si_alloc_conn(&socket->s->si[1]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002175 if (!conn)
2176 WILL_LJMP(luaL_error(L, "connect: internal error"));
2177
2178 /* Parse ip address. */
2179 conn->addr.to.ss_family = AF_UNSPEC;
2180 if (!str2ip2(ip, &conn->addr.to, 0))
2181 WILL_LJMP(luaL_error(L, "connect: cannot parse ip address '%s'", ip));
2182
2183 /* Set port. */
2184 if (conn->addr.to.ss_family == AF_INET)
2185 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2186 else if (conn->addr.to.ss_family == AF_INET6)
2187 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2188
2189 /* it is important not to call the wakeup function directly but to
2190 * pass through task_wakeup(), because this one knows how to apply
2191 * priorities to tasks.
2192 */
2193 task_wakeup(socket->s->task, TASK_WOKEN_INIT);
2194
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002195 hlua = hlua_gethlua(L);
2196 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002197
2198 /* inform the stream that we want to be notified whenever the
2199 * connection completes.
2200 */
2201 si_applet_cant_get(&socket->s->si[0]);
2202 si_applet_cant_put(&socket->s->si[0]);
2203
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002204 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2205 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002206 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002207
2208 return 0;
2209}
2210
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002211#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002212__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2213{
2214 struct hlua_socket *socket;
2215
2216 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2217 socket = MAY_LJMP(hlua_checksocket(L, 1));
2218 socket->s->target = &socket_ssl.obj_type;
2219 return MAY_LJMP(hlua_socket_connect(L));
2220}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002221#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002222
2223__LJMP static int hlua_socket_setoption(struct lua_State *L)
2224{
2225 return 0;
2226}
2227
2228__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2229{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002230 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002231 int tmout;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002232
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002233 MAY_LJMP(check_args(L, 2, "settimeout"));
2234
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002235 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002236 tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002237
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002238 socket->s->req.rto = tmout;
2239 socket->s->req.wto = tmout;
2240 socket->s->res.rto = tmout;
2241 socket->s->res.wto = tmout;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002242
2243 return 0;
2244}
2245
2246__LJMP static int hlua_socket_new(lua_State *L)
2247{
2248 struct hlua_socket *socket;
2249 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002250 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002251 struct stream *strm;
Willy Tarreaud420a972015-04-06 00:39:18 +02002252 struct task *task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002253
2254 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002255 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002256 hlua_pusherror(L, "socket: full stack");
2257 goto out_fail_conf;
2258 }
2259
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002260 /* Create the object: obj[0] = userdata. */
2261 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002262 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002263 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002264 memset(socket, 0, sizeof(*socket));
2265
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002266 /* Check if the various memory pools are intialized. */
Willy Tarreau87b09662015-04-03 00:22:06 +02002267 if (!pool2_stream || !pool2_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002268 hlua_pusherror(L, "socket: uninitialized pools.");
2269 goto out_fail_conf;
2270 }
2271
Willy Tarreau87b09662015-04-03 00:22:06 +02002272 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002273 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2274 lua_setmetatable(L, -2);
2275
Willy Tarreaud420a972015-04-06 00:39:18 +02002276 /* Create the applet context */
2277 appctx = appctx_new(&update_applet);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002278 if (!appctx) {
2279 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002280 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002281 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002282
Willy Tarreaud420a972015-04-06 00:39:18 +02002283 appctx->ctx.hlua.socket = socket;
2284 appctx->ctx.hlua.connected = 0;
2285 LIST_INIT(&appctx->ctx.hlua.wake_on_write);
2286 LIST_INIT(&appctx->ctx.hlua.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002287
Willy Tarreaud420a972015-04-06 00:39:18 +02002288 /* Now create a session, task and stream for this applet */
2289 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2290 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002291 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002292 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002293 }
2294
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002295 task = task_new();
2296 if (!task) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002298 goto out_fail_task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002299 }
Willy Tarreaud420a972015-04-06 00:39:18 +02002300 task->nice = 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002301
Willy Tarreau73b65ac2015-04-08 18:26:29 +02002302 strm = stream_new(sess, task, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002303 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002304 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002305 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002306 }
2307
Willy Tarreaud420a972015-04-06 00:39:18 +02002308 /* Configure an empty Lua for the stream. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002309 socket->s = strm;
2310 strm->hlua.T = NULL;
2311 strm->hlua.Tref = LUA_REFNIL;
2312 strm->hlua.Mref = LUA_REFNIL;
2313 strm->hlua.nargs = 0;
2314 strm->hlua.flags = 0;
2315 LIST_INIT(&strm->hlua.com);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002316
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002317 /* Configure "right" stream interface. this "si" is used to connect
2318 * and retrieve data from the server. The connection is initialized
2319 * with the "struct server".
2320 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002321 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002322
2323 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002324 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2325 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002326
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002327 /* Update statistics counters. */
2328 socket_proxy.feconn++; /* beconn will be increased later */
2329 jobs++;
2330 totalconn++;
2331
2332 /* Return yield waiting for connection. */
2333 return 1;
2334
Willy Tarreaud420a972015-04-06 00:39:18 +02002335 out_fail_stream:
2336 task_free(task);
2337 out_fail_task:
Willy Tarreau11c36242015-04-04 15:54:03 +02002338 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002339 out_fail_sess:
2340 appctx_free(appctx);
2341 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002342 WILL_LJMP(lua_error(L));
2343 return 0;
2344}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002345
2346/*
2347 *
2348 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002349 * Class Channel
2350 *
2351 *
2352 */
2353
2354/* Returns the struct hlua_channel join to the class channel in the
2355 * stack entry "ud" or throws an argument error.
2356 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002357__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002358{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002359 return (struct channel *)MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002360}
2361
Willy Tarreau47860ed2015-03-10 14:07:50 +01002362/* Pushes the channel onto the top of the stack. If the stask does not have a
2363 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002364 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002365static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002366{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002367 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002368 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002369 return 0;
2370
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002371 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002372 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002373 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002374
2375 /* Pop a class sesison metatable and affect it to the userdata. */
2376 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2377 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002378 return 1;
2379}
2380
2381/* Duplicate all the data present in the input channel and put it
2382 * in a string LUA variables. Returns -1 and push a nil value in
2383 * the stack if the channel is closed and all the data are consumed,
2384 * returns 0 if no data are available, otherwise it returns the length
2385 * of the builded string.
2386 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002387static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002388{
2389 char *blk1;
2390 char *blk2;
2391 int len1;
2392 int len2;
2393 int ret;
2394 luaL_Buffer b;
2395
Willy Tarreau47860ed2015-03-10 14:07:50 +01002396 ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002397 if (unlikely(ret == 0))
2398 return 0;
2399
2400 if (unlikely(ret < 0)) {
2401 lua_pushnil(L);
2402 return -1;
2403 }
2404
2405 luaL_buffinit(L, &b);
2406 luaL_addlstring(&b, blk1, len1);
2407 if (unlikely(ret == 2))
2408 luaL_addlstring(&b, blk2, len2);
2409 luaL_pushresult(&b);
2410
2411 if (unlikely(ret == 2))
2412 return len1 + len2;
2413 return len1;
2414}
2415
2416/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2417 * a yield. This function keep the data in the buffer.
2418 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002419__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002420{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002421 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002422
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002423 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2424
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002425 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002426 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002427 return 1;
2428}
2429
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002430/* Check arguments for the function "hlua_channel_dup_yield". */
2431__LJMP static int hlua_channel_dup(lua_State *L)
2432{
2433 MAY_LJMP(check_args(L, 1, "dup"));
2434 MAY_LJMP(hlua_checkchannel(L, 1));
2435 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2436}
2437
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002438/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2439 * a yield. This function consumes the data in the buffer. It returns
2440 * a string containing the data or a nil pointer if no data are available
2441 * and the channel is closed.
2442 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002443__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002444{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002445 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002446 int ret;
2447
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002448 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002449
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002450 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002451 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002452 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002453
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002454 if (unlikely(ret == -1))
2455 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002456
Willy Tarreau47860ed2015-03-10 14:07:50 +01002457 chn->buf->i -= ret;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002458 return 1;
2459}
2460
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002461/* Check arguments for the fucntion "hlua_channel_get_yield". */
2462__LJMP static int hlua_channel_get(lua_State *L)
2463{
2464 MAY_LJMP(check_args(L, 1, "get"));
2465 MAY_LJMP(hlua_checkchannel(L, 1));
2466 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2467}
2468
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002469/* This functions consumes and returns one line. If the channel is closed,
2470 * and the last data does not contains a final '\n', the data are returned
2471 * without the final '\n'. When no more data are avalaible, it returns nil
2472 * value.
2473 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002474__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002475{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002476 char *blk1;
2477 char *blk2;
2478 int len1;
2479 int len2;
2480 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002481 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002482 int ret;
2483 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002484
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002485 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2486
Willy Tarreau47860ed2015-03-10 14:07:50 +01002487 ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002488 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002489 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002490
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002491 if (ret == -1) {
2492 lua_pushnil(L);
2493 return 1;
2494 }
2495
2496 luaL_buffinit(L, &b);
2497 luaL_addlstring(&b, blk1, len1);
2498 len = len1;
2499 if (unlikely(ret == 2)) {
2500 luaL_addlstring(&b, blk2, len2);
2501 len += len2;
2502 }
2503 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002504 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002505 return 1;
2506}
2507
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002508/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2509__LJMP static int hlua_channel_getline(lua_State *L)
2510{
2511 MAY_LJMP(check_args(L, 1, "getline"));
2512 MAY_LJMP(hlua_checkchannel(L, 1));
2513 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2514}
2515
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002516/* This function takes a string as input, and append it at the
2517 * input side of channel. If the data is too big, but a space
2518 * is probably available after sending some data, the function
2519 * yield. If the data is bigger than the buffer, or if the
2520 * channel is closed, it returns -1. otherwise, it returns the
2521 * amount of data writed.
2522 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002523__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002524{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002525 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002526 size_t len;
2527 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2528 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2529 int ret;
2530 int max;
2531
Willy Tarreau47860ed2015-03-10 14:07:50 +01002532 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002533 if (max > len - l)
2534 max = len - l;
2535
Willy Tarreau47860ed2015-03-10 14:07:50 +01002536 ret = bi_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002537 if (ret == -2 || ret == -3) {
2538 lua_pushinteger(L, -1);
2539 return 1;
2540 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002541 if (ret == -1) {
2542 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002543 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002544 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002545 l += ret;
2546 lua_pop(L, 1);
2547 lua_pushinteger(L, l);
2548
Willy Tarreau47860ed2015-03-10 14:07:50 +01002549 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2550 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002551 /* There are no space avalaible, and the output buffer is empty.
2552 * in this case, we cannot add more data, so we cannot yield,
2553 * we return the amount of copyied data.
2554 */
2555 return 1;
2556 }
2557 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002558 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002559 return 1;
2560}
2561
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002562/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002563 * of the writed string, or -1 if the channel is closed or if the
2564 * buffer size is too little for the data.
2565 */
2566__LJMP static int hlua_channel_append(lua_State *L)
2567{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002568 size_t len;
2569
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002570 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002571 MAY_LJMP(hlua_checkchannel(L, 1));
2572 MAY_LJMP(luaL_checklstring(L, 2, &len));
2573 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002574 lua_pushinteger(L, 0);
2575
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002576 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002577}
2578
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002579/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002580 * his process by cleaning the buffer. The result is a replacement
2581 * of the current data. It returns the length of the writed string,
2582 * or -1 if the channel is closed or if the buffer size is too
2583 * little for the data.
2584 */
2585__LJMP static int hlua_channel_set(lua_State *L)
2586{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002587 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002588
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002589 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002590 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002591 lua_pushinteger(L, 0);
2592
Willy Tarreau47860ed2015-03-10 14:07:50 +01002593 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002594
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002595 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002596}
2597
2598/* Append data in the output side of the buffer. This data is immediatly
2599 * sent. The fcuntion returns the ammount of data writed. If the buffer
2600 * cannot contains the data, the function yield. The function returns -1
2601 * if the channel is closed.
2602 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002603__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002604{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002605 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002606 size_t len;
2607 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2608 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2609 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002610 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002611
Willy Tarreau47860ed2015-03-10 14:07:50 +01002612 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002613 lua_pushinteger(L, -1);
2614 return 1;
2615 }
2616
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002617 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2618 * the request buffer if its not required.
2619 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002620 if (chn->buf->size == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002621 if (!stream_alloc_recv_buffer(chn)) {
Willy Tarreau47860ed2015-03-10 14:07:50 +01002622 chn_prod(chn)->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002623 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002624 }
2625 }
2626
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002627 /* the writed data will be immediatly sent, so we can check
2628 * the avalaible space without taking in account the reserve.
2629 * The reserve is guaranted for the processing of incoming
2630 * data, because the buffer will be flushed.
2631 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002632 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002633
2634 /* If there are no space avalaible, and the output buffer is empty.
2635 * in this case, we cannot add more data, so we cannot yield,
2636 * we return the amount of copyied data.
2637 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002638 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002639 return 1;
2640
2641 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002642 if (max > len - l)
2643 max = len - l;
2644
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002645 /* The buffer avalaible size may be not contiguous. This test
2646 * detects a non contiguous buffer and realign it.
2647 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002648 if (bi_space_for_replace(chn->buf) < max)
2649 buffer_slow_realign(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002650
2651 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002652 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002653
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002654 /* buffer replace considers that the input part is filled.
2655 * so, I must forward these new data in the output part.
2656 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002657 b_adv(chn->buf, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002658
2659 l += max;
2660 lua_pop(L, 1);
2661 lua_pushinteger(L, l);
2662
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002663 /* If there are no space avalaible, and the output buffer is empty.
2664 * in this case, we cannot add more data, so we cannot yield,
2665 * we return the amount of copyied data.
2666 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002667 max = chn->buf->size - buffer_len(chn->buf);
2668 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002669 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002670
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002671 if (l < len) {
2672 /* If we are waiting for space in the response buffer, we
2673 * must set the flag WAKERESWR. This flag required the task
2674 * wake up if any activity is detected on the response buffer.
2675 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002676 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002677 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002678 else
2679 HLUA_SET_WAKEREQWR(hlua);
2680 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002681 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002682
2683 return 1;
2684}
2685
2686/* Just a wraper of "_hlua_channel_send". This wrapper permits
2687 * yield the LUA process, and resume it without checking the
2688 * input arguments.
2689 */
2690__LJMP static int hlua_channel_send(lua_State *L)
2691{
2692 MAY_LJMP(check_args(L, 2, "send"));
2693 lua_pushinteger(L, 0);
2694
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002695 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002696}
2697
2698/* This function forward and amount of butes. The data pass from
2699 * the input side of the buffer to the output side, and can be
2700 * forwarded. This function never fails.
2701 *
2702 * The Lua function takes an amount of bytes to be forwarded in
2703 * imput. It returns the number of bytes forwarded.
2704 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002705__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002706{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002707 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002708 int len;
2709 int l;
2710 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002711 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002712
2713 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2714 len = MAY_LJMP(luaL_checkinteger(L, 2));
2715 l = MAY_LJMP(luaL_checkinteger(L, -1));
2716
2717 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002718 if (max > chn->buf->i)
2719 max = chn->buf->i;
2720 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002721 l += max;
2722
2723 lua_pop(L, 1);
2724 lua_pushinteger(L, l);
2725
2726 /* Check if it miss bytes to forward. */
2727 if (l < len) {
2728 /* The the input channel or the output channel are closed, we
2729 * must return the amount of data forwarded.
2730 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002731 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002732 return 1;
2733
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002734 /* If we are waiting for space data in the response buffer, we
2735 * must set the flag WAKERESWR. This flag required the task
2736 * wake up if any activity is detected on the response buffer.
2737 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002738 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002739 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002740 else
2741 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002742
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002743 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002744 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002745 }
2746
2747 return 1;
2748}
2749
2750/* Just check the input and prepare the stack for the previous
2751 * function "hlua_channel_forward_yield"
2752 */
2753__LJMP static int hlua_channel_forward(lua_State *L)
2754{
2755 MAY_LJMP(check_args(L, 2, "forward"));
2756 MAY_LJMP(hlua_checkchannel(L, 1));
2757 MAY_LJMP(luaL_checkinteger(L, 2));
2758
2759 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002760 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002761}
2762
2763/* Just returns the number of bytes available in the input
2764 * side of the buffer. This function never fails.
2765 */
2766__LJMP static int hlua_channel_get_in_len(lua_State *L)
2767{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002768 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002769
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002770 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002771 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002772 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002773 return 1;
2774}
2775
2776/* Just returns the number of bytes available in the output
2777 * side of the buffer. This function never fails.
2778 */
2779__LJMP static int hlua_channel_get_out_len(lua_State *L)
2780{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002781 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002782
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002783 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002784 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002785 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002786 return 1;
2787}
2788
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002789/*
2790 *
2791 *
2792 * Class Fetches
2793 *
2794 *
2795 */
2796
2797/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002798 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002799 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002800__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002801{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002802 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002803}
2804
2805/* This function creates and push in the stack a fetch object according
2806 * with a current TXN.
2807 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002808static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002809{
Willy Tarreau7073c472015-04-06 11:15:40 +02002810 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002811
2812 /* Check stack size. */
2813 if (!lua_checkstack(L, 3))
2814 return 0;
2815
2816 /* Create the object: obj[0] = userdata.
2817 * Note that the base of the Fetches object is the
2818 * transaction object.
2819 */
2820 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002821 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002822 lua_rawseti(L, -2, 0);
2823
Willy Tarreau7073c472015-04-06 11:15:40 +02002824 hsmp->s = txn->s;
2825 hsmp->p = txn->p;
Willy Tarreau7073c472015-04-06 11:15:40 +02002826 hsmp->stringsafe = stringsafe;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002827
2828 /* Pop a class sesison metatable and affect it to the userdata. */
2829 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
2830 lua_setmetatable(L, -2);
2831
2832 return 1;
2833}
2834
2835/* This function is an LUA binding. It is called with each sample-fetch.
2836 * It uses closure argument to store the associated sample-fetch. It
2837 * returns only one argument or throws an error. An error is thrown
2838 * only if an error is encountered during the argument parsing. If
2839 * the "sample-fetch" function fails, nil is returned.
2840 */
2841__LJMP static int hlua_run_sample_fetch(lua_State *L)
2842{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002843 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01002844 struct sample_fetch *f;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002845 struct arg args[ARGM_NBARGS + 1];
2846 int i;
2847 struct sample smp;
2848
2849 /* Get closure arguments. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002850 f = (struct sample_fetch *)lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002851
2852 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002853 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002854
2855 /* Get extra arguments. */
2856 for (i = 0; i < lua_gettop(L) - 1; i++) {
2857 if (i >= ARGM_NBARGS)
2858 break;
2859 hlua_lua2arg(L, i + 2, &args[i]);
2860 }
2861 args[i].type = ARGT_STOP;
2862
2863 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002864 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002865
2866 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002867 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002868 lua_pushfstring(L, "error in arguments");
2869 WILL_LJMP(lua_error(L));
2870 }
2871
2872 /* Initialise the sample. */
2873 memset(&smp, 0, sizeof(smp));
2874
2875 /* Run the sample fetch process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02002876 smp.px = hsmp->p;
2877 smp.sess = hsmp->s->sess;
2878 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02002879 smp.opt = 0;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002880 if (!f->process(args, &smp, f->kw, f->private)) {
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002881 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002882 lua_pushstring(L, "");
2883 else
2884 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002885 return 1;
2886 }
2887
2888 /* Convert the returned sample in lua value. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002889 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002890 hlua_smp2lua_str(L, &smp);
2891 else
2892 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002893 return 1;
2894}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002895
2896/*
2897 *
2898 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002899 * Class Converters
2900 *
2901 *
2902 */
2903
2904/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002905 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002906 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002907__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002908{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002909 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002910}
2911
2912/* This function creates and push in the stack a Converters object
2913 * according with a current TXN.
2914 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002915static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002916{
Willy Tarreau7073c472015-04-06 11:15:40 +02002917 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002918
2919 /* Check stack size. */
2920 if (!lua_checkstack(L, 3))
2921 return 0;
2922
2923 /* Create the object: obj[0] = userdata.
2924 * Note that the base of the Converters object is the
2925 * same than the TXN object.
2926 */
2927 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002928 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002929 lua_rawseti(L, -2, 0);
2930
Willy Tarreau7073c472015-04-06 11:15:40 +02002931 hsmp->s = txn->s;
2932 hsmp->p = txn->p;
Willy Tarreau7073c472015-04-06 11:15:40 +02002933 hsmp->stringsafe = stringsafe;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002934
Willy Tarreau87b09662015-04-03 00:22:06 +02002935 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002936 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
2937 lua_setmetatable(L, -2);
2938
2939 return 1;
2940}
2941
2942/* This function is an LUA binding. It is called with each converter.
2943 * It uses closure argument to store the associated converter. It
2944 * returns only one argument or throws an error. An error is thrown
2945 * only if an error is encountered during the argument parsing. If
2946 * the converter function function fails, nil is returned.
2947 */
2948__LJMP static int hlua_run_sample_conv(lua_State *L)
2949{
Willy Tarreauda5f1082015-04-06 11:17:13 +02002950 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002951 struct sample_conv *conv;
2952 struct arg args[ARGM_NBARGS + 1];
2953 int i;
2954 struct sample smp;
2955
2956 /* Get closure arguments. */
2957 conv = (struct sample_conv *)lua_touserdata(L, lua_upvalueindex(1));
2958
2959 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02002960 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002961
2962 /* Get extra arguments. */
2963 for (i = 0; i < lua_gettop(L) - 2; i++) {
2964 if (i >= ARGM_NBARGS)
2965 break;
2966 hlua_lua2arg(L, i + 3, &args[i]);
2967 }
2968 args[i].type = ARGT_STOP;
2969
2970 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02002971 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002972
2973 /* Run the special args checker. */
2974 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
2975 hlua_pusherror(L, "error in arguments");
2976 WILL_LJMP(lua_error(L));
2977 }
2978
2979 /* Initialise the sample. */
2980 if (!hlua_lua2smp(L, 2, &smp)) {
2981 hlua_pusherror(L, "error in the input argument");
2982 WILL_LJMP(lua_error(L));
2983 }
2984
2985 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002986 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002987 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002988 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002989 WILL_LJMP(lua_error(L));
2990 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002991 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
2992 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002993 hlua_pusherror(L, "error during the input argument casting");
2994 WILL_LJMP(lua_error(L));
2995 }
2996
2997 /* Run the sample conversion process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02002998 smp.px = hsmp->p;
2999 smp.sess = hsmp->s->sess;
3000 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02003001 smp.opt = 0;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003002 if (!conv->process(args, &smp, conv->private)) {
Willy Tarreauda5f1082015-04-06 11:17:13 +02003003 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003004 lua_pushstring(L, "");
3005 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003006 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003007 return 1;
3008 }
3009
3010 /* Convert the returned sample in lua value. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003011 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003012 hlua_smp2lua_str(L, &smp);
3013 else
3014 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003015 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003016}
3017
3018/*
3019 *
3020 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003021 * Class HTTP
3022 *
3023 *
3024 */
3025
3026/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003027 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003028 */
3029__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
3030{
3031 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
3032}
3033
3034/* This function creates and push in the stack a HTTP object
3035 * according with a current TXN.
3036 */
3037static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
3038{
Willy Tarreau9a8ad862015-04-06 11:14:06 +02003039 struct hlua_txn *htxn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003040
3041 /* Check stack size. */
3042 if (!lua_checkstack(L, 3))
3043 return 0;
3044
3045 /* Create the object: obj[0] = userdata.
3046 * Note that the base of the Converters object is the
3047 * same than the TXN object.
3048 */
3049 lua_newtable(L);
Willy Tarreau9a8ad862015-04-06 11:14:06 +02003050 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003051 lua_rawseti(L, -2, 0);
3052
Willy Tarreau9a8ad862015-04-06 11:14:06 +02003053 htxn->s = txn->s;
3054 htxn->p = txn->p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003055
Willy Tarreau87b09662015-04-03 00:22:06 +02003056 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003057 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
3058 lua_setmetatable(L, -2);
3059
3060 return 1;
3061}
3062
3063/* This function creates ans returns an array of HTTP headers.
3064 * This function does not fails. It is used as wrapper with the
3065 * 2 following functions.
3066 */
3067__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3068{
3069 const char *cur_ptr, *cur_next, *p;
3070 int old_idx, cur_idx;
3071 struct hdr_idx_elem *cur_hdr;
3072 const char *hn, *hv;
3073 int hnl, hvl;
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003074 int type;
3075 const char *in;
3076 char *out;
3077 int len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003078
3079 /* Create the table. */
3080 lua_newtable(L);
3081
Willy Tarreaueee5b512015-04-03 23:46:31 +02003082 if (!htxn->s->txn)
3083 return 1;
3084
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003085 /* Build array of headers. */
3086 old_idx = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003087 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003088
3089 while (1) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02003090 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003091 if (!cur_idx)
3092 break;
3093 old_idx = cur_idx;
3094
Willy Tarreaueee5b512015-04-03 23:46:31 +02003095 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003096 cur_ptr = cur_next;
3097 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
3098
3099 /* Now we have one full header at cur_ptr of len cur_hdr->len,
3100 * and the next header starts at cur_next. We'll check
3101 * this header in the list as well as against the default
3102 * rule.
3103 */
3104
3105 /* look for ': *'. */
3106 hn = cur_ptr;
3107 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
3108 if (p >= cur_ptr+cur_hdr->len)
3109 continue;
3110 hnl = p - hn;
3111 p++;
3112 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
3113 p++;
3114 if (p >= cur_ptr+cur_hdr->len)
3115 continue;
3116 hv = p;
3117 hvl = cur_ptr+cur_hdr->len-p;
3118
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003119 /* Lowercase the key. Don't check the size of trash, it have
3120 * the size of one buffer and the input data contains in one
3121 * buffer.
3122 */
3123 out = trash.str;
3124 for (in=hn; in<hn+hnl; in++, out++)
3125 *out = tolower(*in);
3126 *out = '\0';
3127
3128 /* Check for existing entry:
3129 * assume that the table is on the top of the stack, and
3130 * push the key in the stack, the function lua_gettable()
3131 * perform the lookup.
3132 */
3133 lua_pushlstring(L, trash.str, hnl);
3134 lua_gettable(L, -2);
3135 type = lua_type(L, -1);
3136
3137 switch (type) {
3138 case LUA_TNIL:
3139 /* Table not found, create it. */
3140 lua_pop(L, 1); /* remove the nil value. */
3141 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
3142 lua_newtable(L); /* create and push empty table. */
3143 lua_pushlstring(L, hv, hvl); /* push header value. */
3144 lua_rawseti(L, -2, 0); /* index header value (pop it). */
3145 lua_rawset(L, -3); /* index new table with header name (pop the values). */
3146 break;
3147
3148 case LUA_TTABLE:
3149 /* Entry found: push the value in the table. */
3150 len = lua_rawlen(L, -1);
3151 lua_pushlstring(L, hv, hvl); /* push header value. */
3152 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
3153 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
3154 break;
3155
3156 default:
3157 /* Other cases are errors. */
3158 hlua_pusherror(L, "internal error during the parsing of headers.");
3159 WILL_LJMP(lua_error(L));
3160 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003161 }
3162
3163 return 1;
3164}
3165
3166__LJMP static int hlua_http_req_get_headers(lua_State *L)
3167{
3168 struct hlua_txn *htxn;
3169
3170 MAY_LJMP(check_args(L, 1, "req_get_headers"));
3171 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3172
Willy Tarreaueee5b512015-04-03 23:46:31 +02003173 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003174}
3175
3176__LJMP static int hlua_http_res_get_headers(lua_State *L)
3177{
3178 struct hlua_txn *htxn;
3179
3180 MAY_LJMP(check_args(L, 1, "res_get_headers"));
3181 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3182
Willy Tarreaueee5b512015-04-03 23:46:31 +02003183 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003184}
3185
3186/* This function replace full header, or just a value in
3187 * the request or in the response. It is a wrapper fir the
3188 * 4 following functions.
3189 */
3190__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
3191 struct http_msg *msg, int action)
3192{
3193 size_t name_len;
3194 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3195 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
3196 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
3197 struct my_regex re;
3198
3199 if (!regex_comp(reg, &re, 1, 1, NULL))
3200 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
3201
3202 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
3203 regex_free(&re);
3204 return 0;
3205}
3206
3207__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
3208{
3209 struct hlua_txn *htxn;
3210
3211 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3212 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3213
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003214 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003215}
3216
3217__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
3218{
3219 struct hlua_txn *htxn;
3220
3221 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
3222 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3223
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003224 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003225}
3226
3227__LJMP static int hlua_http_req_rep_val(lua_State *L)
3228{
3229 struct hlua_txn *htxn;
3230
3231 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3232 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3233
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003234 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003235}
3236
3237__LJMP static int hlua_http_res_rep_val(lua_State *L)
3238{
3239 struct hlua_txn *htxn;
3240
3241 MAY_LJMP(check_args(L, 4, "res_rep_val"));
3242 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3243
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003244 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003245}
3246
3247/* This function deletes all the occurences of an header.
3248 * It is a wrapper for the 2 following functions.
3249 */
3250__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3251{
3252 size_t len;
3253 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3254 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003255 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003256
3257 ctx.idx = 0;
3258 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
3259 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3260 return 0;
3261}
3262
3263__LJMP static int hlua_http_req_del_hdr(lua_State *L)
3264{
3265 struct hlua_txn *htxn;
3266
3267 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3268 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3269
Willy Tarreaueee5b512015-04-03 23:46:31 +02003270 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003271}
3272
3273__LJMP static int hlua_http_res_del_hdr(lua_State *L)
3274{
3275 struct hlua_txn *htxn;
3276
3277 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3278 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3279
Willy Tarreaueee5b512015-04-03 23:46:31 +02003280 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003281}
3282
3283/* This function adds an header. It is a wrapper used by
3284 * the 2 following functions.
3285 */
3286__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3287{
3288 size_t name_len;
3289 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3290 size_t value_len;
3291 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
3292 char *p;
3293
3294 /* Check length. */
3295 trash.len = value_len + name_len + 2;
3296 if (trash.len > trash.size)
3297 return 0;
3298
3299 /* Creates the header string. */
3300 p = trash.str;
3301 memcpy(p, name, name_len);
3302 p += name_len;
3303 *p = ':';
3304 p++;
3305 *p = ' ';
3306 p++;
3307 memcpy(p, value, value_len);
3308
Willy Tarreaueee5b512015-04-03 23:46:31 +02003309 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003310 trash.str, trash.len) != 0);
3311
3312 return 0;
3313}
3314
3315__LJMP static int hlua_http_req_add_hdr(lua_State *L)
3316{
3317 struct hlua_txn *htxn;
3318
3319 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
3320 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3321
Willy Tarreaueee5b512015-04-03 23:46:31 +02003322 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003323}
3324
3325__LJMP static int hlua_http_res_add_hdr(lua_State *L)
3326{
3327 struct hlua_txn *htxn;
3328
3329 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
3330 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3331
Willy Tarreaueee5b512015-04-03 23:46:31 +02003332 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003333}
3334
3335static int hlua_http_req_set_hdr(lua_State *L)
3336{
3337 struct hlua_txn *htxn;
3338
3339 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
3340 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3341
Willy Tarreaueee5b512015-04-03 23:46:31 +02003342 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
3343 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003344}
3345
3346static int hlua_http_res_set_hdr(lua_State *L)
3347{
3348 struct hlua_txn *htxn;
3349
3350 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
3351 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3352
Willy Tarreaueee5b512015-04-03 23:46:31 +02003353 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
3354 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003355}
3356
3357/* This function set the method. */
3358static int hlua_http_req_set_meth(lua_State *L)
3359{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003360 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003361 size_t name_len;
3362 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003363
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003364 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003365 return 1;
3366}
3367
3368/* This function set the method. */
3369static int hlua_http_req_set_path(lua_State *L)
3370{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003371 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003372 size_t name_len;
3373 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003374 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003375 return 1;
3376}
3377
3378/* This function set the query-string. */
3379static int hlua_http_req_set_query(lua_State *L)
3380{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003381 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003382 size_t name_len;
3383 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003384
3385 /* Check length. */
3386 if (name_len > trash.size - 1) {
3387 lua_pushboolean(L, 0);
3388 return 1;
3389 }
3390
3391 /* Add the mark question as prefix. */
3392 chunk_reset(&trash);
3393 trash.str[trash.len++] = '?';
3394 memcpy(trash.str + trash.len, name, name_len);
3395 trash.len += name_len;
3396
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003397 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003398 return 1;
3399}
3400
3401/* This function set the uri. */
3402static int hlua_http_req_set_uri(lua_State *L)
3403{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003404 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003405 size_t name_len;
3406 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003407
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003408 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003409 return 1;
3410}
3411
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02003412/* This function set the response code. */
3413static int hlua_http_res_set_status(lua_State *L)
3414{
3415 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3416 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
3417
3418 http_set_status(code, htxn->s);
3419 return 0;
3420}
3421
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003422/*
3423 *
3424 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003425 * Class TXN
3426 *
3427 *
3428 */
3429
3430/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003431 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003432 */
3433__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
3434{
3435 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
3436}
3437
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02003438__LJMP static int hlua_set_var(lua_State *L)
3439{
3440 struct hlua_txn *htxn;
3441 const char *name;
3442 size_t len;
3443 struct sample smp;
3444
3445 MAY_LJMP(check_args(L, 3, "set_var"));
3446
3447 /* It is useles to retrieve the stream, but this function
3448 * runs only in a stream context.
3449 */
3450 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3451 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3452
3453 /* Converts the third argument in a sample. */
3454 hlua_lua2smp(L, 3, &smp);
3455
3456 /* Store the sample in a variable. */
3457 vars_set_by_name(name, len, htxn->s, &smp);
3458 return 0;
3459}
3460
3461__LJMP static int hlua_get_var(lua_State *L)
3462{
3463 struct hlua_txn *htxn;
3464 const char *name;
3465 size_t len;
3466 struct sample smp;
3467
3468 MAY_LJMP(check_args(L, 2, "get_var"));
3469
3470 /* It is useles to retrieve the stream, but this function
3471 * runs only in a stream context.
3472 */
3473 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3474 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3475
3476 if (!vars_get_by_name(name, len, htxn->s, &smp)) {
3477 lua_pushnil(L);
3478 return 1;
3479 }
3480
3481 return hlua_smp2lua(L, &smp);
3482}
3483
Willy Tarreau59551662015-03-10 14:23:13 +01003484__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003485{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003486 struct hlua *hlua;
3487
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003488 MAY_LJMP(check_args(L, 2, "set_priv"));
3489
Willy Tarreau87b09662015-04-03 00:22:06 +02003490 /* It is useles to retrieve the stream, but this function
3491 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003492 */
3493 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003494 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003495
3496 /* Remove previous value. */
3497 if (hlua->Mref != -1)
3498 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
3499
3500 /* Get and store new value. */
3501 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3502 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3503
3504 return 0;
3505}
3506
Willy Tarreau59551662015-03-10 14:23:13 +01003507__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003508{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003509 struct hlua *hlua;
3510
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003511 MAY_LJMP(check_args(L, 1, "get_priv"));
3512
Willy Tarreau87b09662015-04-03 00:22:06 +02003513 /* It is useles to retrieve the stream, but this function
3514 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003515 */
3516 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003517 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003518
3519 /* Push configuration index in the stack. */
3520 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3521
3522 return 1;
3523}
3524
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003525/* Create stack entry containing a class TXN. This function
3526 * return 0 if the stack does not contains free slots,
3527 * otherwise it returns 1.
3528 */
Willy Tarreau15e91e12015-04-04 00:52:09 +02003529static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003530{
Willy Tarreaude491382015-04-06 11:04:28 +02003531 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003532
3533 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003534 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003535 return 0;
3536
3537 /* NOTE: The allocation never fails. The failure
3538 * throw an error, and the function never returns.
3539 * if the throw is not avalaible, the process is aborted.
3540 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003541 /* Create the object: obj[0] = userdata. */
3542 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02003543 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003544 lua_rawseti(L, -2, 0);
3545
Willy Tarreaude491382015-04-06 11:04:28 +02003546 htxn->s = s;
3547 htxn->p = p;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003548
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003549 /* Create the "f" field that contains a list of fetches. */
3550 lua_pushstring(L, "f");
Willy Tarreaude491382015-04-06 11:04:28 +02003551 if (!hlua_fetches_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003552 return 0;
3553 lua_settable(L, -3);
3554
3555 /* Create the "sf" field that contains a list of stringsafe fetches. */
3556 lua_pushstring(L, "sf");
Willy Tarreaude491382015-04-06 11:04:28 +02003557 if (!hlua_fetches_new(L, htxn, 1))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003558 return 0;
3559 lua_settable(L, -3);
3560
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003561 /* Create the "c" field that contains a list of converters. */
3562 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02003563 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003564 return 0;
3565 lua_settable(L, -3);
3566
3567 /* Create the "sc" field that contains a list of stringsafe converters. */
3568 lua_pushstring(L, "sc");
Willy Tarreaude491382015-04-06 11:04:28 +02003569 if (!hlua_converters_new(L, htxn, 1))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003570 return 0;
3571 lua_settable(L, -3);
3572
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003573 /* Create the "req" field that contains the request channel object. */
3574 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003575 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003576 return 0;
3577 lua_settable(L, -3);
3578
3579 /* Create the "res" field that contains the response channel object. */
3580 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003581 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003582 return 0;
3583 lua_settable(L, -3);
3584
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003585 /* Creates the HTTP object is the current proxy allows http. */
3586 lua_pushstring(L, "http");
3587 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02003588 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003589 return 0;
3590 }
3591 else
3592 lua_pushnil(L);
3593 lua_settable(L, -3);
3594
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003595 /* Pop a class sesison metatable and affect it to the userdata. */
3596 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
3597 lua_setmetatable(L, -2);
3598
3599 return 1;
3600}
3601
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003602__LJMP static int hlua_txn_deflog(lua_State *L)
3603{
3604 const char *msg;
3605 struct hlua_txn *htxn;
3606
3607 MAY_LJMP(check_args(L, 2, "deflog"));
3608 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3609 msg = MAY_LJMP(luaL_checkstring(L, 2));
3610
3611 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
3612 return 0;
3613}
3614
3615__LJMP static int hlua_txn_log(lua_State *L)
3616{
3617 int level;
3618 const char *msg;
3619 struct hlua_txn *htxn;
3620
3621 MAY_LJMP(check_args(L, 3, "log"));
3622 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3623 level = MAY_LJMP(luaL_checkinteger(L, 2));
3624 msg = MAY_LJMP(luaL_checkstring(L, 3));
3625
3626 if (level < 0 || level >= NB_LOG_LEVELS)
3627 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3628
3629 hlua_sendlog(htxn->s->be, level, msg);
3630 return 0;
3631}
3632
3633__LJMP static int hlua_txn_log_debug(lua_State *L)
3634{
3635 const char *msg;
3636 struct hlua_txn *htxn;
3637
3638 MAY_LJMP(check_args(L, 2, "Debug"));
3639 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3640 msg = MAY_LJMP(luaL_checkstring(L, 2));
3641 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
3642 return 0;
3643}
3644
3645__LJMP static int hlua_txn_log_info(lua_State *L)
3646{
3647 const char *msg;
3648 struct hlua_txn *htxn;
3649
3650 MAY_LJMP(check_args(L, 2, "Info"));
3651 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3652 msg = MAY_LJMP(luaL_checkstring(L, 2));
3653 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
3654 return 0;
3655}
3656
3657__LJMP static int hlua_txn_log_warning(lua_State *L)
3658{
3659 const char *msg;
3660 struct hlua_txn *htxn;
3661
3662 MAY_LJMP(check_args(L, 2, "Warning"));
3663 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3664 msg = MAY_LJMP(luaL_checkstring(L, 2));
3665 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
3666 return 0;
3667}
3668
3669__LJMP static int hlua_txn_log_alert(lua_State *L)
3670{
3671 const char *msg;
3672 struct hlua_txn *htxn;
3673
3674 MAY_LJMP(check_args(L, 2, "Alert"));
3675 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3676 msg = MAY_LJMP(luaL_checkstring(L, 2));
3677 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
3678 return 0;
3679}
3680
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003681__LJMP static int hlua_txn_set_loglevel(lua_State *L)
3682{
3683 struct hlua_txn *htxn;
3684 int ll;
3685
3686 MAY_LJMP(check_args(L, 2, "set_loglevel"));
3687 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3688 ll = MAY_LJMP(luaL_checkinteger(L, 2));
3689
3690 if (ll < 0 || ll > 7)
3691 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
3692
3693 htxn->s->logs.level = ll;
3694 return 0;
3695}
3696
3697__LJMP static int hlua_txn_set_tos(lua_State *L)
3698{
3699 struct hlua_txn *htxn;
3700 struct connection *cli_conn;
3701 int tos;
3702
3703 MAY_LJMP(check_args(L, 2, "set_tos"));
3704 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3705 tos = MAY_LJMP(luaL_checkinteger(L, 2));
3706
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003707 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003708 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, tos);
3709
3710 return 0;
3711}
3712
3713__LJMP static int hlua_txn_set_mark(lua_State *L)
3714{
3715#ifdef SO_MARK
3716 struct hlua_txn *htxn;
3717 struct connection *cli_conn;
3718 int mark;
3719
3720 MAY_LJMP(check_args(L, 2, "set_mark"));
3721 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3722 mark = MAY_LJMP(luaL_checkinteger(L, 2));
3723
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003724 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau07081fe2015-04-06 10:59:20 +02003725 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003726#endif
3727 return 0;
3728}
3729
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003730/* This function is an Lua binding that send pending data
3731 * to the client, and close the stream interface.
3732 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02003733__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003734{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003735 struct hlua_txn *htxn;
Willy Tarreau81389672015-03-10 12:03:52 +01003736 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003737
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003738 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003739 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003740
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003741 ic = &htxn->s->req;
3742 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01003743
Willy Tarreau630ef452015-08-28 10:06:15 +02003744 if (htxn->s->txn) {
3745 /* HTTP mode, let's stay in sync with the stream */
3746 bi_fast_delete(ic->buf, htxn->s->txn->req.sov);
3747 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
3748 htxn->s->txn->req.sov = 0;
3749 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
3750 oc->analysers = AN_RES_HTTP_XFER_BODY;
3751 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
3752 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
3753
3754 /* Trim any possible response */
3755 oc->buf->i = 0;
3756 htxn->s->txn->rsp.next = htxn->s->txn->rsp.sov = 0;
3757
3758 /* Note that if we want to support keep-alive, we need
3759 * to bypass the close/shutr_now calls below, but that
3760 * may only be done if the HTTP request was already
3761 * processed and the connection header is known (ie
3762 * not during TCP rules).
3763 */
3764 }
3765
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02003766 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01003767 channel_abort(ic);
3768 channel_auto_close(ic);
3769 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02003770
3771 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01003772 channel_auto_read(oc);
3773 channel_auto_close(oc);
3774 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003775
Willy Tarreau0458b082015-08-28 09:40:04 +02003776 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02003777
3778 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003779 return 0;
3780}
3781
3782__LJMP static int hlua_log(lua_State *L)
3783{
3784 int level;
3785 const char *msg;
3786
3787 MAY_LJMP(check_args(L, 2, "log"));
3788 level = MAY_LJMP(luaL_checkinteger(L, 1));
3789 msg = MAY_LJMP(luaL_checkstring(L, 2));
3790
3791 if (level < 0 || level >= NB_LOG_LEVELS)
3792 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3793
3794 hlua_sendlog(NULL, level, msg);
3795 return 0;
3796}
3797
3798__LJMP static int hlua_log_debug(lua_State *L)
3799{
3800 const char *msg;
3801
3802 MAY_LJMP(check_args(L, 1, "debug"));
3803 msg = MAY_LJMP(luaL_checkstring(L, 1));
3804 hlua_sendlog(NULL, LOG_DEBUG, msg);
3805 return 0;
3806}
3807
3808__LJMP static int hlua_log_info(lua_State *L)
3809{
3810 const char *msg;
3811
3812 MAY_LJMP(check_args(L, 1, "info"));
3813 msg = MAY_LJMP(luaL_checkstring(L, 1));
3814 hlua_sendlog(NULL, LOG_INFO, msg);
3815 return 0;
3816}
3817
3818__LJMP static int hlua_log_warning(lua_State *L)
3819{
3820 const char *msg;
3821
3822 MAY_LJMP(check_args(L, 1, "warning"));
3823 msg = MAY_LJMP(luaL_checkstring(L, 1));
3824 hlua_sendlog(NULL, LOG_WARNING, msg);
3825 return 0;
3826}
3827
3828__LJMP static int hlua_log_alert(lua_State *L)
3829{
3830 const char *msg;
3831
3832 MAY_LJMP(check_args(L, 1, "alert"));
3833 msg = MAY_LJMP(luaL_checkstring(L, 1));
3834 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003835 return 0;
3836}
3837
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003838__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003839{
3840 int wakeup_ms = lua_tointeger(L, -1);
3841 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003842 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003843 return 0;
3844}
3845
3846__LJMP static int hlua_sleep(lua_State *L)
3847{
3848 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003849 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003850
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003851 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003852
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003853 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003854 wakeup_ms = tick_add(now_ms, delay);
3855 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003856
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003857 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3858 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003859}
3860
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003861__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003862{
3863 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003864 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003865
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003866 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003867
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003868 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003869 wakeup_ms = tick_add(now_ms, delay);
3870 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003871
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003872 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3873 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003874}
3875
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003876/* This functionis an LUA binding. it permits to give back
3877 * the hand at the HAProxy scheduler. It is used when the
3878 * LUA processing consumes a lot of time.
3879 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003880__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003881{
3882 return 0;
3883}
3884
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003885__LJMP static int hlua_yield(lua_State *L)
3886{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003887 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
3888 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003889}
3890
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003891/* This function change the nice of the currently executed
3892 * task. It is used set low or high priority at the current
3893 * task.
3894 */
Willy Tarreau59551662015-03-10 14:23:13 +01003895__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003896{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003897 struct hlua *hlua;
3898 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003899
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003900 MAY_LJMP(check_args(L, 1, "set_nice"));
3901 hlua = hlua_gethlua(L);
3902 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003903
3904 /* If he task is not set, I'm in a start mode. */
3905 if (!hlua || !hlua->task)
3906 return 0;
3907
3908 if (nice < -1024)
3909 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003910 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003911 nice = 1024;
3912
3913 hlua->task->nice = nice;
3914 return 0;
3915}
3916
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003917/* This function is used as a calback of a task. It is called by the
3918 * HAProxy task subsystem when the task is awaked. The LUA runtime can
3919 * return an E_AGAIN signal, the emmiter of this signal must set a
3920 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02003921 *
3922 * Task wrapper are longjmp safe because the only one Lua code
3923 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003924 */
3925static struct task *hlua_process_task(struct task *task)
3926{
3927 struct hlua *hlua = task->context;
3928 enum hlua_exec status;
3929
3930 /* We need to remove the task from the wait queue before executing
3931 * the Lua code because we don't know if it needs to wait for
3932 * another timer or not in the case of E_AGAIN.
3933 */
3934 task_delete(task);
3935
Thierry FOURNIERbd413492015-03-03 16:52:26 +01003936 /* If it is the first call to the task, we must initialize the
3937 * execution timeouts.
3938 */
3939 if (!HLUA_IS_RUNNING(hlua))
Camilo Lopez685c0142015-08-02 19:07:28 -04003940 hlua->expire = tick_add_ifset(now_ms, hlua_timeout_task);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01003941
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003942 /* Execute the Lua code. */
3943 status = hlua_ctx_resume(hlua, 1);
3944
3945 switch (status) {
3946 /* finished or yield */
3947 case HLUA_E_OK:
3948 hlua_ctx_destroy(hlua);
3949 task_delete(task);
3950 task_free(task);
3951 break;
3952
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01003953 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
3954 if (hlua->wake_time != TICK_ETERNITY)
3955 task_schedule(task, hlua->wake_time);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003956 break;
3957
3958 /* finished with error. */
3959 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02003960 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003961 hlua_ctx_destroy(hlua);
3962 task_delete(task);
3963 task_free(task);
3964 break;
3965
3966 case HLUA_E_ERR:
3967 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02003968 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003969 hlua_ctx_destroy(hlua);
3970 task_delete(task);
3971 task_free(task);
3972 break;
3973 }
3974 return NULL;
3975}
3976
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01003977/* This function is an LUA binding that register LUA function to be
3978 * executed after the HAProxy configuration parsing and before the
3979 * HAProxy scheduler starts. This function expect only one LUA
3980 * argument that is a function. This function returns nothing, but
3981 * throws if an error is encountered.
3982 */
3983__LJMP static int hlua_register_init(lua_State *L)
3984{
3985 struct hlua_init_function *init;
3986 int ref;
3987
3988 MAY_LJMP(check_args(L, 1, "register_init"));
3989
3990 ref = MAY_LJMP(hlua_checkfunction(L, 1));
3991
3992 init = malloc(sizeof(*init));
3993 if (!init)
3994 WILL_LJMP(luaL_error(L, "lua out of memory error."));
3995
3996 init->function_ref = ref;
3997 LIST_ADDQ(&hlua_init_functions, &init->l);
3998 return 0;
3999}
4000
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004001/* This functio is an LUA binding. It permits to register a task
4002 * executed in parallel of the main HAroxy activity. The task is
4003 * created and it is set in the HAProxy scheduler. It can be called
4004 * from the "init" section, "post init" or during the runtime.
4005 *
4006 * Lua prototype:
4007 *
4008 * <none> core.register_task(<function>)
4009 */
4010static int hlua_register_task(lua_State *L)
4011{
4012 struct hlua *hlua;
4013 struct task *task;
4014 int ref;
4015
4016 MAY_LJMP(check_args(L, 1, "register_task"));
4017
4018 ref = MAY_LJMP(hlua_checkfunction(L, 1));
4019
4020 hlua = malloc(sizeof(*hlua));
4021 if (!hlua)
4022 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4023
4024 task = task_new();
4025 task->context = hlua;
4026 task->process = hlua_process_task;
4027
4028 if (!hlua_ctx_init(hlua, task))
4029 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4030
4031 /* Restore the function in the stack. */
4032 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
4033 hlua->nargs = 0;
4034
4035 /* Schedule task. */
4036 task_schedule(task, now_ms);
4037
4038 return 0;
4039}
4040
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004041/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
4042 * doesn't allow "yield" functions because the HAProxy engine cannot
4043 * resume converters.
4044 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004045static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004046{
4047 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004048 struct stream *stream = smp->strm;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004049
Willy Tarreau87b09662015-04-03 00:22:06 +02004050 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004051 * Lua context can be not initialized. This behavior
4052 * permits to save performances because a systematic
4053 * Lua initialization cause 5% performances loss.
4054 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004055 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004056 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004057 return 0;
4058 }
4059
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004060 /* If it is the first run, initialize the data for the call. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004061 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004062
4063 /* The following Lua calls can fail. */
4064 if (!SET_SAFE_LJMP(stream->hlua.T)) {
4065 SEND_ERR(stream->be, "Lua converter '%s': critical error.\n", fcn->name);
4066 return 0;
4067 }
4068
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004069 /* Check stack available size. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004070 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004071 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004072 return 0;
4073 }
4074
4075 /* Restore the function in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004076 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004077
4078 /* convert input sample and pust-it in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004079 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004080 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004081 return 0;
4082 }
Willy Tarreau87b09662015-04-03 00:22:06 +02004083 hlua_smp2lua(stream->hlua.T, smp);
4084 stream->hlua.nargs = 2;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004085
4086 /* push keywords in the stack. */
4087 if (arg_p) {
4088 for (; arg_p->type != ARGT_STOP; arg_p++) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004089 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004090 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004091 return 0;
4092 }
Willy Tarreau87b09662015-04-03 00:22:06 +02004093 hlua_arg2lua(stream->hlua.T, arg_p);
4094 stream->hlua.nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004095 }
4096 }
4097
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004098 /* We must initialize the execution timeouts. */
Thierry FOURNIER61e96c62015-08-09 13:10:24 +02004099 stream->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004100
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004101 /* At this point the execution is safe. */
4102 RESET_SAFE_LJMP(stream->hlua.T);
4103
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004104 /* Set the currently running flag. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004105 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004106 }
4107
4108 /* Execute the function. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004109 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004110 /* finished. */
4111 case HLUA_E_OK:
4112 /* Convert the returned value in sample. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004113 hlua_lua2smp(stream->hlua.T, -1, smp);
4114 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004115 return 1;
4116
4117 /* yield. */
4118 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004119 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004120 return 0;
4121
4122 /* finished with error. */
4123 case HLUA_E_ERRMSG:
4124 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004125 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
4126 fcn->name, lua_tostring(stream->hlua.T, -1));
Willy Tarreau87b09662015-04-03 00:22:06 +02004127 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004128 return 0;
4129
4130 case HLUA_E_ERR:
4131 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004132 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004133
4134 default:
4135 return 0;
4136 }
4137}
4138
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004139/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
4140 * doesn't allow "yield" functions because the HAProxy engine cannot
4141 * resume sample-fetches.
4142 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004143static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
4144 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004145{
4146 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004147 struct stream *stream = smp->strm;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004148
Willy Tarreau87b09662015-04-03 00:22:06 +02004149 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004150 * Lua context can be not initialized. This behavior
4151 * permits to save performances because a systematic
4152 * Lua initialization cause 5% performances loss.
4153 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004154 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004155 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004156 return 0;
4157 }
4158
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004159 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004160 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004161
4162 /* The following Lua calls can fail. */
4163 if (!SET_SAFE_LJMP(stream->hlua.T)) {
4164 SEND_ERR(smp->px, "Lua sample-fetch '%s': critical error.\n", fcn->name);
4165 return 0;
4166 }
4167
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004168 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004169 if (!lua_checkstack(stream->hlua.T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004170 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004171 return 0;
4172 }
4173
4174 /* Restore the function in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004175 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004176
4177 /* push arguments in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004178 if (!hlua_txn_new(stream->hlua.T, stream, smp->px)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004179 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004180 return 0;
4181 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004182 stream->hlua.nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004183
4184 /* push keywords in the stack. */
4185 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
4186 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004187 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004188 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004189 return 0;
4190 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004191 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004192 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004193 return 0;
4194 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004195 hlua_arg2lua(stream->hlua.T, arg_p);
4196 stream->hlua.nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004197 }
4198
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004199 /* We must initialize the execution timeouts. */
Thierry FOURNIER61e96c62015-08-09 13:10:24 +02004200 stream->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004201
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004202 /* At this point the execution is safe. */
4203 RESET_SAFE_LJMP(stream->hlua.T);
4204
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004205 /* Set the currently running flag. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004206 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004207 }
4208
4209 /* Execute the function. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004210 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004211 /* finished. */
4212 case HLUA_E_OK:
4213 /* Convert the returned value in sample. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004214 hlua_lua2smp(stream->hlua.T, -1, smp);
4215 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004216
4217 /* Set the end of execution flag. */
4218 smp->flags &= ~SMP_F_MAY_CHANGE;
4219 return 1;
4220
4221 /* yield. */
4222 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004223 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004224 return 0;
4225
4226 /* finished with error. */
4227 case HLUA_E_ERRMSG:
4228 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004229 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
4230 fcn->name, lua_tostring(stream->hlua.T, -1));
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004231 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004232 return 0;
4233
4234 case HLUA_E_ERR:
4235 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004236 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004237
4238 default:
4239 return 0;
4240 }
4241}
4242
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004243/* This function is an LUA binding used for registering
4244 * "sample-conv" functions. It expects a converter name used
4245 * in the haproxy configuration file, and an LUA function.
4246 */
4247__LJMP static int hlua_register_converters(lua_State *L)
4248{
4249 struct sample_conv_kw_list *sck;
4250 const char *name;
4251 int ref;
4252 int len;
4253 struct hlua_function *fcn;
4254
4255 MAY_LJMP(check_args(L, 2, "register_converters"));
4256
4257 /* First argument : converter name. */
4258 name = MAY_LJMP(luaL_checkstring(L, 1));
4259
4260 /* Second argument : lua function. */
4261 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4262
4263 /* Allocate and fill the sample fetch keyword struct. */
Willy Tarreau07081fe2015-04-06 10:59:20 +02004264 sck = malloc(sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004265 if (!sck)
4266 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4267 fcn = malloc(sizeof(*fcn));
4268 if (!fcn)
4269 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4270
4271 /* Fill fcn. */
4272 fcn->name = strdup(name);
4273 if (!fcn->name)
4274 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4275 fcn->function_ref = ref;
4276
4277 /* List head */
4278 sck->list.n = sck->list.p = NULL;
4279
4280 /* converter keyword. */
4281 len = strlen("lua.") + strlen(name) + 1;
4282 sck->kw[0].kw = malloc(len);
4283 if (!sck->kw[0].kw)
4284 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4285
4286 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
4287 sck->kw[0].process = hlua_sample_conv_wrapper;
4288 sck->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4289 sck->kw[0].val_args = NULL;
4290 sck->kw[0].in_type = SMP_T_STR;
4291 sck->kw[0].out_type = SMP_T_STR;
4292 sck->kw[0].private = fcn;
4293
4294 /* End of array. */
4295 memset(&sck->kw[1], 0, sizeof(struct sample_conv));
4296
4297 /* Register this new converter */
4298 sample_register_convs(sck);
4299
4300 return 0;
4301}
4302
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004303/* This fucntion is an LUA binding used for registering
4304 * "sample-fetch" functions. It expects a converter name used
4305 * in the haproxy configuration file, and an LUA function.
4306 */
4307__LJMP static int hlua_register_fetches(lua_State *L)
4308{
4309 const char *name;
4310 int ref;
4311 int len;
4312 struct sample_fetch_kw_list *sfk;
4313 struct hlua_function *fcn;
4314
4315 MAY_LJMP(check_args(L, 2, "register_fetches"));
4316
4317 /* First argument : sample-fetch name. */
4318 name = MAY_LJMP(luaL_checkstring(L, 1));
4319
4320 /* Second argument : lua function. */
4321 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4322
4323 /* Allocate and fill the sample fetch keyword struct. */
Willy Tarreau07081fe2015-04-06 10:59:20 +02004324 sfk = malloc(sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004325 if (!sfk)
4326 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4327 fcn = malloc(sizeof(*fcn));
4328 if (!fcn)
4329 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4330
4331 /* Fill fcn. */
4332 fcn->name = strdup(name);
4333 if (!fcn->name)
4334 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4335 fcn->function_ref = ref;
4336
4337 /* List head */
4338 sfk->list.n = sfk->list.p = NULL;
4339
4340 /* sample-fetch keyword. */
4341 len = strlen("lua.") + strlen(name) + 1;
4342 sfk->kw[0].kw = malloc(len);
4343 if (!sfk->kw[0].kw)
4344 return luaL_error(L, "lua out of memory error.");
4345
4346 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
4347 sfk->kw[0].process = hlua_sample_fetch_wrapper;
4348 sfk->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4349 sfk->kw[0].val_args = NULL;
4350 sfk->kw[0].out_type = SMP_T_STR;
4351 sfk->kw[0].use = SMP_USE_HTTP_ANY;
4352 sfk->kw[0].val = 0;
4353 sfk->kw[0].private = fcn;
4354
4355 /* End of array. */
4356 memset(&sfk->kw[1], 0, sizeof(struct sample_fetch));
4357
4358 /* Register this new fetch. */
4359 sample_register_fetches(sfk);
4360
4361 return 0;
4362}
4363
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004364/* This function is a wrapper to execute each LUA function declared
4365 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004366 * return ACT_RET_CONT if the processing is finished (with or without
4367 * error) and return ACT_RET_YIELD if the function must be called again
4368 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004369 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004370static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
4371 struct session *sess, struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004372{
4373 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004374 unsigned int analyzer;
4375
4376 switch (rule->from) {
4377 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; break;
4378 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; break;
4379 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; break;
4380 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; break;
4381 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004382 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004383 return ACT_RET_CONT;
4384 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004385
Willy Tarreau87b09662015-04-03 00:22:06 +02004386 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004387 * Lua context can be not initialized. This behavior
4388 * permits to save performances because a systematic
4389 * Lua initialization cause 5% performances loss.
4390 */
4391 if (!s->hlua.T && !hlua_ctx_init(&s->hlua, s->task)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004392 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004393 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004394 return ACT_RET_CONT;
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004395 }
4396
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004397 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004398 if (!HLUA_IS_RUNNING(&s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004399
4400 /* The following Lua calls can fail. */
4401 if (!SET_SAFE_LJMP(s->hlua.T)) {
4402 SEND_ERR(px, "Lua function '%s': critical error.\n",
4403 rule->arg.hlua_rule->fcn.name);
4404 return ACT_RET_CONT;
4405 }
4406
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004407 /* Check stack available size. */
4408 if (!lua_checkstack(s->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004409 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004410 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004411 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004412 }
4413
4414 /* Restore the function in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004415 lua_rawgeti(s->hlua.T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004416
Willy Tarreau87b09662015-04-03 00:22:06 +02004417 /* Create and and push object stream in the stack. */
Willy Tarreau15e91e12015-04-04 00:52:09 +02004418 if (!hlua_txn_new(s->hlua.T, s, px)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004419 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004420 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004421 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004422 }
4423 s->hlua.nargs = 1;
4424
4425 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004426 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004427 if (!lua_checkstack(s->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004428 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004429 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004430 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004431 }
4432 lua_pushstring(s->hlua.T, *arg);
4433 s->hlua.nargs++;
4434 }
4435
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004436 /* Now the execution is safe. */
4437 RESET_SAFE_LJMP(s->hlua.T);
4438
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004439 /* We must initialize the execution timeouts. */
Thierry FOURNIER61e96c62015-08-09 13:10:24 +02004440 s->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004441
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004442 /* Set the currently running flag. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004443 HLUA_SET_RUN(&s->hlua);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004444 }
4445
4446 /* Execute the function. */
4447 switch (hlua_ctx_resume(&s->hlua, 1)) {
4448 /* finished. */
4449 case HLUA_E_OK:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004450 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004451
4452 /* yield. */
4453 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004454 /* Set timeout in the required channel. */
4455 if (s->hlua.wake_time != TICK_ETERNITY) {
4456 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004457 s->req.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004458 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004459 s->res.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004460 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004461 /* Some actions can be wake up when a "write" event
4462 * is detected on a response channel. This is useful
4463 * only for actions targetted on the requests.
4464 */
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01004465 if (HLUA_IS_WAKERESWR(&s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004466 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01004467 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004468 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004469 }
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01004470 if (HLUA_IS_WAKEREQWR(&s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004471 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004472 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004473
4474 /* finished with error. */
4475 case HLUA_E_ERRMSG:
4476 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004477 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004478 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua.T, -1));
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004479 lua_pop(s->hlua.T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004480 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004481
4482 case HLUA_E_ERR:
4483 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004484 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004485 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004486
4487 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004488 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004489 }
4490}
4491
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004492/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
4493 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004494 *
4495 * This function can fail with an abort() due to an Lua critical error.
4496 * We are in the configuration parsing process of HAProxy, this abort() is
4497 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004498 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004499static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4500 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004501{
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004502 /* Memory for the rule. */
4503 rule->arg.hlua_rule = malloc(sizeof(*rule->arg.hlua_rule));
4504 if (!rule->arg.hlua_rule) {
4505 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02004506 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004507 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004508
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004509 /* The requiered arg is a function name. */
4510 if (!args[*cur_arg]) {
4511 memprintf(err, "expect Lua function name");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02004512 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004513 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004514
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004515 /* Lookup for the symbol, and check if it is a function. */
4516 lua_getglobal(gL.T, args[*cur_arg]);
4517 if (lua_isnil(gL.T, -1)) {
4518 lua_pop(gL.T, 1);
4519 memprintf(err, "Lua function '%s' not found", args[*cur_arg]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +02004520 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004521 }
4522 if (!lua_isfunction(gL.T, -1)) {
4523 lua_pop(gL.T, 1);
4524 memprintf(err, "'%s' is not a function", args[*cur_arg]);
4525 return ACT_RET_PRS_ERR;
4526 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004527
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004528 /* Reference the Lua function and store the reference. */
4529 rule->arg.hlua_rule->fcn.function_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
4530 rule->arg.hlua_rule->fcn.name = strdup(args[*cur_arg]);
4531 if (!rule->arg.hlua_rule->fcn.name) {
4532 memprintf(err, "out of memory error.");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02004533 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004534 }
4535 (*cur_arg)++;
4536
4537 /* TODO: later accept arguments. */
4538 rule->arg.hlua_rule->args = NULL;
4539
Thierry FOURNIER42148732015-09-02 17:17:33 +02004540 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004541 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02004542 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004543}
4544
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004545static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
4546 struct proxy *defpx, const char *file, int line,
4547 char **err, unsigned int *timeout)
4548{
4549 const char *error;
4550
4551 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
4552 if (error && *error != '\0') {
4553 memprintf(err, "%s: invalid timeout", args[0]);
4554 return -1;
4555 }
4556 return 0;
4557}
4558
4559static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
4560 struct proxy *defpx, const char *file, int line,
4561 char **err)
4562{
4563 return hlua_read_timeout(args, section_type, curpx, defpx,
4564 file, line, err, &hlua_timeout_session);
4565}
4566
4567static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
4568 struct proxy *defpx, const char *file, int line,
4569 char **err)
4570{
4571 return hlua_read_timeout(args, section_type, curpx, defpx,
4572 file, line, err, &hlua_timeout_task);
4573}
4574
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004575static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
4576 struct proxy *defpx, const char *file, int line,
4577 char **err)
4578{
4579 char *error;
4580
4581 hlua_nb_instruction = strtoll(args[1], &error, 10);
4582 if (*error != '\0') {
4583 memprintf(err, "%s: invalid number", args[0]);
4584 return -1;
4585 }
4586 return 0;
4587}
4588
Willy Tarreau32f61e22015-03-18 17:54:59 +01004589static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
4590 struct proxy *defpx, const char *file, int line,
4591 char **err)
4592{
4593 char *error;
4594
4595 if (*(args[1]) == 0) {
4596 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
4597 return -1;
4598 }
4599 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
4600 if (*error != '\0') {
4601 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
4602 return -1;
4603 }
4604 return 0;
4605}
4606
4607
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004608/* This function is called by the main configuration key "lua-load". It loads and
4609 * execute an lua file during the parsing of the HAProxy configuration file. It is
4610 * the main lua entry point.
4611 *
4612 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
4613 * occured, otherwise it returns 0.
4614 *
4615 * In some error case, LUA set an error message in top of the stack. This function
4616 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004617 *
4618 * This function can fail with an abort() due to an Lua critical error.
4619 * We are in the configuration parsing process of HAProxy, this abort() is
4620 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004621 */
4622static int hlua_load(char **args, int section_type, struct proxy *curpx,
4623 struct proxy *defpx, const char *file, int line,
4624 char **err)
4625{
4626 int error;
4627
4628 /* Just load and compile the file. */
4629 error = luaL_loadfile(gL.T, args[1]);
4630 if (error) {
4631 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
4632 lua_pop(gL.T, 1);
4633 return -1;
4634 }
4635
4636 /* If no syntax error where detected, execute the code. */
4637 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
4638 switch (error) {
4639 case LUA_OK:
4640 break;
4641 case LUA_ERRRUN:
4642 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
4643 lua_pop(gL.T, 1);
4644 return -1;
4645 case LUA_ERRMEM:
4646 memprintf(err, "lua out of memory error\n");
4647 return -1;
4648 case LUA_ERRERR:
4649 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
4650 lua_pop(gL.T, 1);
4651 return -1;
4652 case LUA_ERRGCMM:
4653 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
4654 lua_pop(gL.T, 1);
4655 return -1;
4656 default:
4657 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
4658 lua_pop(gL.T, 1);
4659 return -1;
4660 }
4661
4662 return 0;
4663}
4664
4665/* configuration keywords declaration */
4666static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004667 { CFG_GLOBAL, "lua-load", hlua_load },
4668 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
4669 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004670 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01004671 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004672 { 0, NULL, NULL },
4673}};
4674
Thierry FOURNIER36481b82015-08-19 09:01:53 +02004675static struct action_kw_list http_req_kws = { { }, {
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004676 { "lua", action_register_lua },
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004677 { NULL, NULL }
4678}};
4679
Thierry FOURNIER36481b82015-08-19 09:01:53 +02004680static struct action_kw_list http_res_kws = { { }, {
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004681 { "lua", action_register_lua },
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004682 { NULL, NULL }
4683}};
4684
Thierry FOURNIER36481b82015-08-19 09:01:53 +02004685static struct action_kw_list tcp_req_cont_kws = { { }, {
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004686 { "lua", action_register_lua },
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004687 { NULL, NULL }
4688}};
4689
Thierry FOURNIER36481b82015-08-19 09:01:53 +02004690static struct action_kw_list tcp_res_cont_kws = { { }, {
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004691 { "lua", action_register_lua },
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004692 { NULL, NULL }
4693}};
4694
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004695/* This function can fail with an abort() due to an Lua critical error.
4696 * We are in the initialisation process of HAProxy, this abort() is
4697 * tolerated.
4698 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004699int hlua_post_init()
4700{
4701 struct hlua_init_function *init;
4702 const char *msg;
4703 enum hlua_exec ret;
4704
4705 list_for_each_entry(init, &hlua_init_functions, l) {
4706 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
4707 ret = hlua_ctx_resume(&gL, 0);
4708 switch (ret) {
4709 case HLUA_E_OK:
4710 lua_pop(gL.T, -1);
4711 return 1;
4712 case HLUA_E_AGAIN:
4713 Alert("lua init: yield not allowed.\n");
4714 return 0;
4715 case HLUA_E_ERRMSG:
4716 msg = lua_tostring(gL.T, -1);
4717 Alert("lua init: %s.\n", msg);
4718 return 0;
4719 case HLUA_E_ERR:
4720 default:
4721 Alert("lua init: unknown runtime error.\n");
4722 return 0;
4723 }
4724 }
4725 return 1;
4726}
4727
Willy Tarreau32f61e22015-03-18 17:54:59 +01004728/* The memory allocator used by the Lua stack. <ud> is a pointer to the
4729 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
4730 * is the previously allocated size or the kind of object in case of a new
4731 * allocation. <nsize> is the requested new size.
4732 */
4733static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
4734{
4735 struct hlua_mem_allocator *zone = ud;
4736
4737 if (nsize == 0) {
4738 /* it's a free */
4739 if (ptr)
4740 zone->allocated -= osize;
4741 free(ptr);
4742 return NULL;
4743 }
4744
4745 if (!ptr) {
4746 /* it's a new allocation */
4747 if (zone->limit && zone->allocated + nsize > zone->limit)
4748 return NULL;
4749
4750 ptr = malloc(nsize);
4751 if (ptr)
4752 zone->allocated += nsize;
4753 return ptr;
4754 }
4755
4756 /* it's a realloc */
4757 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
4758 return NULL;
4759
4760 ptr = realloc(ptr, nsize);
4761 if (ptr)
4762 zone->allocated += nsize - osize;
4763 return ptr;
4764}
4765
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004766/* Ithis function can fail with an abort() due to an Lua critical error.
4767 * We are in the initialisation process of HAProxy, this abort() is
4768 * tolerated.
4769 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01004770void hlua_init(void)
4771{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004772 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004773 int idx;
4774 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004775 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004776 char *p;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004777#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004778 struct srv_kw *kw;
4779 int tmp_error;
4780 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01004781 char *args[] = { /* SSL client configuration. */
4782 "ssl",
4783 "verify",
4784 "none",
4785 "force-sslv3",
4786 NULL
4787 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004788#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004789
Willy Tarreau87b09662015-04-03 00:22:06 +02004790 /* Initialise com signals pool */
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004791 pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED);
4792
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004793 /* Register configuration keywords. */
4794 cfg_register_keywords(&cfg_kws);
4795
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004796 /* Register custom HTTP rules. */
4797 http_req_keywords_register(&http_req_kws);
4798 http_res_keywords_register(&http_res_kws);
4799 tcp_req_cont_keywords_register(&tcp_req_cont_kws);
4800 tcp_res_cont_keywords_register(&tcp_res_cont_kws);
4801
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004802 /* Init main lua stack. */
4803 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004804 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004805 LIST_INIT(&gL.com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004806 gL.T = luaL_newstate();
4807 hlua_sethlua(&gL);
4808 gL.Tref = LUA_REFNIL;
4809 gL.task = NULL;
4810
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004811 /* From this point, until the end of the initialisation fucntion,
4812 * the Lua function can fail with an abort. We are in the initialisation
4813 * process of HAProxy, this abort() is tolerated.
4814 */
4815
Willy Tarreau32f61e22015-03-18 17:54:59 +01004816 /* change the memory allocators to track memory usage */
4817 lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator);
4818
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004819 /* Initialise lua. */
4820 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004821
4822 /*
4823 *
4824 * Create "core" object.
4825 *
4826 */
4827
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01004828 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004829 lua_newtable(gL.T);
4830
4831 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004832 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004833 hlua_class_const_int(gL.T, log_levels[i], i);
4834
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004835 /* Register special functions. */
4836 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004837 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004838 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004839 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01004840 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01004841 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01004842 hlua_class_function(gL.T, "sleep", hlua_sleep);
4843 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01004844 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
4845 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
4846 hlua_class_function(gL.T, "set_map", hlua_set_map);
4847 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01004848 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01004849 hlua_class_function(gL.T, "log", hlua_log);
4850 hlua_class_function(gL.T, "Debug", hlua_log_debug);
4851 hlua_class_function(gL.T, "Info", hlua_log_info);
4852 hlua_class_function(gL.T, "Warning", hlua_log_warning);
4853 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02004854 hlua_class_function(gL.T, "done", hlua_done);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004855
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004856 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004857
4858 /*
4859 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02004860 * Register class Map
4861 *
4862 */
4863
4864 /* This table entry is the object "Map" base. */
4865 lua_newtable(gL.T);
4866
4867 /* register pattern types. */
4868 for (i=0; i<PAT_MATCH_NUM; i++)
4869 hlua_class_const_int(gL.T, pat_match_names[i], i);
4870
4871 /* register constructor. */
4872 hlua_class_function(gL.T, "new", hlua_map_new);
4873
4874 /* Create and fill the metatable. */
4875 lua_newtable(gL.T);
4876
4877 /* Create and fille the __index entry. */
4878 lua_pushstring(gL.T, "__index");
4879 lua_newtable(gL.T);
4880
4881 /* Register . */
4882 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
4883 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
4884
4885 lua_settable(gL.T, -3);
4886
4887 /* Register previous table in the registry with reference and named entry. */
4888 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4889 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4890 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_MAP); /* register class session. */
4891 class_map_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4892
4893 /* Assign the metatable to the mai Map object. */
4894 lua_setmetatable(gL.T, -2);
4895
4896 /* Set a name to the table. */
4897 lua_setglobal(gL.T, "Map");
4898
4899 /*
4900 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01004901 * Register class Channel
4902 *
4903 */
4904
4905 /* Create and fill the metatable. */
4906 lua_newtable(gL.T);
4907
4908 /* Create and fille the __index entry. */
4909 lua_pushstring(gL.T, "__index");
4910 lua_newtable(gL.T);
4911
4912 /* Register . */
4913 hlua_class_function(gL.T, "get", hlua_channel_get);
4914 hlua_class_function(gL.T, "dup", hlua_channel_dup);
4915 hlua_class_function(gL.T, "getline", hlua_channel_getline);
4916 hlua_class_function(gL.T, "set", hlua_channel_set);
4917 hlua_class_function(gL.T, "append", hlua_channel_append);
4918 hlua_class_function(gL.T, "send", hlua_channel_send);
4919 hlua_class_function(gL.T, "forward", hlua_channel_forward);
4920 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
4921 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
4922
4923 lua_settable(gL.T, -3);
4924
4925 /* Register previous table in the registry with reference and named entry. */
4926 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4927 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CHANNEL); /* register class session. */
4928 class_channel_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4929
4930 /*
4931 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004932 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004933 *
4934 */
4935
4936 /* Create and fill the metatable. */
4937 lua_newtable(gL.T);
4938
4939 /* Create and fille the __index entry. */
4940 lua_pushstring(gL.T, "__index");
4941 lua_newtable(gL.T);
4942
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004943 /* Browse existing fetches and create the associated
4944 * object method.
4945 */
4946 sf = NULL;
4947 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
4948
4949 /* Dont register the keywork if the arguments check function are
4950 * not safe during the runtime.
4951 */
4952 if ((sf->val_args != NULL) &&
4953 (sf->val_args != val_payload_lv) &&
4954 (sf->val_args != val_hdr))
4955 continue;
4956
4957 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
4958 * by an underscore.
4959 */
4960 strncpy(trash.str, sf->kw, trash.size);
4961 trash.str[trash.size - 1] = '\0';
4962 for (p = trash.str; *p; p++)
4963 if (*p == '.' || *p == '-' || *p == '+')
4964 *p = '_';
4965
4966 /* Register the function. */
4967 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01004968 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004969 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
4970 lua_settable(gL.T, -3);
4971 }
4972
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004973 lua_settable(gL.T, -3);
4974
4975 /* Register previous table in the registry with reference and named entry. */
4976 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4977 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_FETCHES); /* register class session. */
4978 class_fetches_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4979
4980 /*
4981 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004982 * Register class Converters
4983 *
4984 */
4985
4986 /* Create and fill the metatable. */
4987 lua_newtable(gL.T);
4988
4989 /* Create and fill the __index entry. */
4990 lua_pushstring(gL.T, "__index");
4991 lua_newtable(gL.T);
4992
4993 /* Browse existing converters and create the associated
4994 * object method.
4995 */
4996 sc = NULL;
4997 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
4998 /* Dont register the keywork if the arguments check function are
4999 * not safe during the runtime.
5000 */
5001 if (sc->val_args != NULL)
5002 continue;
5003
5004 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
5005 * by an underscore.
5006 */
5007 strncpy(trash.str, sc->kw, trash.size);
5008 trash.str[trash.size - 1] = '\0';
5009 for (p = trash.str; *p; p++)
5010 if (*p == '.' || *p == '-' || *p == '+')
5011 *p = '_';
5012
5013 /* Register the function. */
5014 lua_pushstring(gL.T, trash.str);
5015 lua_pushlightuserdata(gL.T, sc);
5016 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
5017 lua_settable(gL.T, -3);
5018 }
5019
5020 lua_settable(gL.T, -3);
5021
5022 /* Register previous table in the registry with reference and named entry. */
5023 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5024 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CONVERTERS); /* register class session. */
5025 class_converters_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5026
5027 /*
5028 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005029 * Register class HTTP
5030 *
5031 */
5032
5033 /* Create and fill the metatable. */
5034 lua_newtable(gL.T);
5035
5036 /* Create and fille the __index entry. */
5037 lua_pushstring(gL.T, "__index");
5038 lua_newtable(gL.T);
5039
5040 /* Register Lua functions. */
5041 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
5042 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
5043 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
5044 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
5045 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
5046 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
5047 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
5048 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
5049 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
5050 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
5051
5052 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
5053 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
5054 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
5055 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
5056 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
5057 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005058 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005059
5060 lua_settable(gL.T, -3);
5061
5062 /* Register previous table in the registry with reference and named entry. */
5063 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5064 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_HTTP); /* register class session. */
5065 class_http_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5066
5067 /*
5068 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005069 * Register class TXN
5070 *
5071 */
5072
5073 /* Create and fill the metatable. */
5074 lua_newtable(gL.T);
5075
5076 /* Create and fille the __index entry. */
5077 lua_pushstring(gL.T, "__index");
5078 lua_newtable(gL.T);
5079
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005080 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01005081 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
5082 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005083 hlua_class_function(gL.T, "set_var", hlua_set_var);
5084 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005085 hlua_class_function(gL.T, "done", hlua_txn_done);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005086 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
5087 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
5088 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005089 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
5090 hlua_class_function(gL.T, "log", hlua_txn_log);
5091 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
5092 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
5093 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
5094 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005095
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005096 lua_settable(gL.T, -3);
5097
5098 /* Register previous table in the registry with reference and named entry. */
5099 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5100 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_TXN); /* register class session. */
5101 class_txn_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005102
5103 /*
5104 *
5105 * Register class Socket
5106 *
5107 */
5108
5109 /* Create and fill the metatable. */
5110 lua_newtable(gL.T);
5111
5112 /* Create and fille the __index entry. */
5113 lua_pushstring(gL.T, "__index");
5114 lua_newtable(gL.T);
5115
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005116#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005117 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005118#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005119 hlua_class_function(gL.T, "connect", hlua_socket_connect);
5120 hlua_class_function(gL.T, "send", hlua_socket_send);
5121 hlua_class_function(gL.T, "receive", hlua_socket_receive);
5122 hlua_class_function(gL.T, "close", hlua_socket_close);
5123 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
5124 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
5125 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
5126 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
5127
5128 lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
5129
5130 /* Register the garbage collector entry. */
5131 lua_pushstring(gL.T, "__gc");
5132 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
5133 lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
5134
5135 /* Register previous table in the registry with reference and named entry. */
5136 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5137 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5138 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_SOCKET); /* register class socket. */
5139 class_socket_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class socket. */
5140
5141 /* Proxy and server configuration initialisation. */
5142 memset(&socket_proxy, 0, sizeof(socket_proxy));
5143 init_new_proxy(&socket_proxy);
5144 socket_proxy.parent = NULL;
5145 socket_proxy.last_change = now.tv_sec;
5146 socket_proxy.id = "LUA-SOCKET";
5147 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
5148 socket_proxy.maxconn = 0;
5149 socket_proxy.accept = NULL;
5150 socket_proxy.options2 |= PR_O2_INDEPSTR;
5151 socket_proxy.srv = NULL;
5152 socket_proxy.conn_retries = 0;
5153 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
5154
5155 /* Init TCP server: unchanged parameters */
5156 memset(&socket_tcp, 0, sizeof(socket_tcp));
5157 socket_tcp.next = NULL;
5158 socket_tcp.proxy = &socket_proxy;
5159 socket_tcp.obj_type = OBJ_TYPE_SERVER;
5160 LIST_INIT(&socket_tcp.actconns);
5161 LIST_INIT(&socket_tcp.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02005162 LIST_INIT(&socket_tcp.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02005163 LIST_INIT(&socket_tcp.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02005164 LIST_INIT(&socket_tcp.safe_conns);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005165 socket_tcp.state = SRV_ST_RUNNING; /* early server setup */
5166 socket_tcp.last_change = 0;
5167 socket_tcp.id = "LUA-TCP-CONN";
5168 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5169 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5170 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
5171
5172 /* XXX: Copy default parameter from default server,
5173 * but the default server is not initialized.
5174 */
5175 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
5176 socket_tcp.minconn = socket_proxy.defsrv.minconn;
5177 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
5178 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
5179 socket_tcp.onerror = socket_proxy.defsrv.onerror;
5180 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5181 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
5182 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5183 socket_tcp.uweight = socket_proxy.defsrv.iweight;
5184 socket_tcp.iweight = socket_proxy.defsrv.iweight;
5185
5186 socket_tcp.check.status = HCHK_STATUS_INI;
5187 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
5188 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
5189 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
5190 socket_tcp.check.server = &socket_tcp;
5191
5192 socket_tcp.agent.status = HCHK_STATUS_INI;
5193 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
5194 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
5195 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
5196 socket_tcp.agent.server = &socket_tcp;
5197
5198 socket_tcp.xprt = &raw_sock;
5199
5200#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005201 /* Init TCP server: unchanged parameters */
5202 memset(&socket_ssl, 0, sizeof(socket_ssl));
5203 socket_ssl.next = NULL;
5204 socket_ssl.proxy = &socket_proxy;
5205 socket_ssl.obj_type = OBJ_TYPE_SERVER;
5206 LIST_INIT(&socket_ssl.actconns);
5207 LIST_INIT(&socket_ssl.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02005208 LIST_INIT(&socket_ssl.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02005209 LIST_INIT(&socket_ssl.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02005210 LIST_INIT(&socket_ssl.safe_conns);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005211 socket_ssl.state = SRV_ST_RUNNING; /* early server setup */
5212 socket_ssl.last_change = 0;
5213 socket_ssl.id = "LUA-SSL-CONN";
5214 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5215 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5216 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
5217
5218 /* XXX: Copy default parameter from default server,
5219 * but the default server is not initialized.
5220 */
5221 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
5222 socket_ssl.minconn = socket_proxy.defsrv.minconn;
5223 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
5224 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
5225 socket_ssl.onerror = socket_proxy.defsrv.onerror;
5226 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5227 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
5228 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5229 socket_ssl.uweight = socket_proxy.defsrv.iweight;
5230 socket_ssl.iweight = socket_proxy.defsrv.iweight;
5231
5232 socket_ssl.check.status = HCHK_STATUS_INI;
5233 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
5234 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
5235 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
5236 socket_ssl.check.server = &socket_ssl;
5237
5238 socket_ssl.agent.status = HCHK_STATUS_INI;
5239 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
5240 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
5241 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
5242 socket_ssl.agent.server = &socket_ssl;
5243
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005244 socket_ssl.use_ssl = 1;
5245 socket_ssl.xprt = &ssl_sock;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005246
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005247 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005248 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
5249 /*
5250 *
5251 * If the keyword is not known, we can search in the registered
5252 * server keywords. This is usefull to configure special SSL
5253 * features like client certificates and ssl_verify.
5254 *
5255 */
5256 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
5257 if (tmp_error != 0) {
5258 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
5259 abort(); /* This must be never arrives because the command line
5260 not editable by the user. */
5261 }
5262 idx += kw->skip;
5263 }
5264 }
5265
5266 /* Initialize SSL server. */
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005267 ssl_sock_prepare_srv_ctx(&socket_ssl, &socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005268#endif
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01005269}