blob: 9651b7fdb6115264e7c70e342538b04676222ffc [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++) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200817 if (p >= trash.str + trash.size - 1) {
818 /* Break the message if exceed the buffer size. */
819 *(p-4) = ' ';
820 *(p-3) = '.';
821 *(p-2) = '.';
822 *(p-1) = '.';
823 break;
824 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100825 if (isprint(*msg))
826 *p = *msg;
827 else
828 *p = '.';
829 }
830 *p = '\0';
831
Thierry FOURNIER5554e292015-09-09 11:21:37 +0200832 send_log(px, level, "%s\n", trash.str);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100833 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200834 get_localtime(date.tv_sec, &tm);
835 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100836 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
837 (int)getpid(), trash.str);
838 fflush(stderr);
839 }
840}
841
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100842/* This function just ensure that the yield will be always
843 * returned with a timeout and permit to set some flags
844 */
845__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100846 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100847{
848 struct hlua *hlua = hlua_gethlua(L);
849
850 /* Set the wake timeout. If timeout is required, we set
851 * the expiration time.
852 */
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100853 hlua->wake_time = tick_first(timeout, hlua->expire);
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100854
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100855 hlua->flags |= flags;
856
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100857 /* Process the yield. */
858 WILL_LJMP(lua_yieldk(L, nresults, ctx, k));
859}
860
Willy Tarreau87b09662015-04-03 00:22:06 +0200861/* This function initialises the Lua environment stored in the stream.
862 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100863 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200864 *
865 * This function is particular. it initialises a new Lua thread. If the
866 * initialisation fails (example: out of memory error), the lua function
867 * throws an error (longjmp).
868 *
869 * This function manipulates two Lua stack: the main and the thread. Only
870 * the main stack can fail. The thread is not manipulated. This function
871 * MUST NOT manipulate the created thread stack state, because is not
872 * proctected agains error throwed by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100873 */
874int hlua_ctx_init(struct hlua *lua, struct task *task)
875{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200876 if (!SET_SAFE_LJMP(gL.T)) {
877 lua->Tref = LUA_REFNIL;
878 return 0;
879 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100880 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100881 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100882 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100883 lua->T = lua_newthread(gL.T);
884 if (!lua->T) {
885 lua->Tref = LUA_REFNIL;
886 return 0;
887 }
888 hlua_sethlua(lua);
889 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
890 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200891 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100892 return 1;
893}
894
Willy Tarreau87b09662015-04-03 00:22:06 +0200895/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100896 * is destroyed. The destroy also the memory context. The struct "lua"
897 * is not freed.
898 */
899void hlua_ctx_destroy(struct hlua *lua)
900{
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100901 if (!lua->T)
902 return;
903
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100904 /* Purge all the pending signals. */
905 hlua_com_purge(lua);
906
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907 /* The thread is garbage collected by Lua. */
908 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
909 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
910}
911
912/* This function is used to restore the Lua context when a coroutine
913 * fails. This function copy the common memory between old coroutine
914 * and the new coroutine. The old coroutine is destroyed, and its
915 * replaced by the new coroutine.
916 * If the flag "keep_msg" is set, the last entry of the old is assumed
917 * as string error message and it is copied in the new stack.
918 */
919static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
920{
921 lua_State *T;
922 int new_ref;
923
924 /* Renew the main LUA stack doesn't have sense. */
925 if (lua == &gL)
926 return 0;
927
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100928 /* New Lua coroutine. */
929 T = lua_newthread(gL.T);
930 if (!T)
931 return 0;
932
933 /* Copy last error message. */
934 if (keep_msg)
935 lua_xmove(lua->T, T, 1);
936
937 /* Copy data between the coroutines. */
938 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
939 lua_xmove(lua->T, T, 1);
940 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
941
942 /* Destroy old data. */
943 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
944
945 /* The thread is garbage collected by Lua. */
946 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
947
948 /* Fill the struct with the new coroutine values. */
949 lua->Mref = new_ref;
950 lua->T = T;
951 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
952
953 /* Set context. */
954 hlua_sethlua(lua);
955
956 return 1;
957}
958
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100959void hlua_hook(lua_State *L, lua_Debug *ar)
960{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100961 struct hlua *hlua = hlua_gethlua(L);
962
963 /* Lua cannot yield when its returning from a function,
964 * so, we can fix the interrupt hook to 1 instruction,
965 * expecting that the function is finnished.
966 */
967 if (lua_gethookmask(L) & LUA_MASKRET) {
968 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
969 return;
970 }
971
972 /* restore the interrupt condition. */
973 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
974
975 /* If we interrupt the Lua processing in yieldable state, we yield.
976 * If the state is not yieldable, trying yield causes an error.
977 */
978 if (lua_isyieldable(L))
979 WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
980
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +0100981 /* If we cannot yield, update the clock and check the timeout. */
982 tv_update_date(0, 1);
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100983 if (tick_is_expired(hlua->expire, now_ms)) {
984 lua_pushfstring(L, "execution timeout");
985 WILL_LJMP(lua_error(L));
986 }
987
988 /* Try to interrupt the process at the end of the current
989 * unyieldable function.
990 */
991 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100992}
993
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100994/* This function start or resumes the Lua stack execution. If the flag
995 * "yield_allowed" if no set and the LUA stack execution returns a yield
996 * The function return an error.
997 *
998 * The function can returns 4 values:
999 * - HLUA_E_OK : The execution is terminated without any errors.
1000 * - HLUA_E_AGAIN : The execution must continue at the next associated
1001 * task wakeup.
1002 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
1003 * the top of the stack.
1004 * - HLUA_E_ERR : An error has occured without error message.
1005 *
1006 * If an error occured, the stack is renewed and it is ready to run new
1007 * LUA code.
1008 */
1009static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1010{
1011 int ret;
1012 const char *msg;
1013
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001014 HLUA_SET_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001015
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001016 /* If we want to resume the task, then check first the execution timeout.
1017 * if it is reached, we can interrupt the Lua processing.
1018 */
1019 if (tick_is_expired(lua->expire, now_ms))
1020 goto timeout_reached;
1021
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001022resume_execution:
1023
1024 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1025 * instructions. it is used for preventing infinite loops.
1026 */
1027 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1028
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001029 /* Remove all flags except the running flags. */
1030 lua->flags = HLUA_RUN;
1031
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001032 /* Call the function. */
1033 ret = lua_resume(lua->T, gL.T, lua->nargs);
1034 switch (ret) {
1035
1036 case LUA_OK:
1037 ret = HLUA_E_OK;
1038 break;
1039
1040 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001041 /* Check if the execution timeout is expired. It it is the case, we
1042 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001043 */
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001044 if (tick_is_expired(lua->expire, now_ms)) {
1045
1046timeout_reached:
1047
1048 lua_settop(lua->T, 0); /* Empty the stack. */
1049 if (!lua_checkstack(lua->T, 1)) {
1050 ret = HLUA_E_ERR;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001051 break;
1052 }
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001053 lua_pushfstring(lua->T, "execution timeout");
1054 ret = HLUA_E_ERRMSG;
1055 break;
1056 }
1057 /* Process the forced yield. if the general yield is not allowed or
1058 * if no task were associated this the current Lua execution
1059 * coroutine, we resume the execution. Else we want to return in the
1060 * scheduler and we want to be waked up again, to continue the
1061 * current Lua execution. So we schedule our own task.
1062 */
1063 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001064 if (!yield_allowed || !lua->task)
1065 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001066 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001067 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001068 if (!yield_allowed) {
1069 lua_settop(lua->T, 0); /* Empty the stack. */
1070 if (!lua_checkstack(lua->T, 1)) {
1071 ret = HLUA_E_ERR;
1072 break;
1073 }
1074 lua_pushfstring(lua->T, "yield not allowed");
1075 ret = HLUA_E_ERRMSG;
1076 break;
1077 }
1078 ret = HLUA_E_AGAIN;
1079 break;
1080
1081 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001082
1083 /* Special exit case. The traditionnal exit is returned as an error
1084 * because the errors ares the only one mean to return immediately
1085 * from and lua execution.
1086 */
1087 if (lua->flags & HLUA_EXIT) {
1088 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001089 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001090 break;
1091 }
1092
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001093 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001094 if (!lua_checkstack(lua->T, 1)) {
1095 ret = HLUA_E_ERR;
1096 break;
1097 }
1098 msg = lua_tostring(lua->T, -1);
1099 lua_settop(lua->T, 0); /* Empty the stack. */
1100 lua_pop(lua->T, 1);
1101 if (msg)
1102 lua_pushfstring(lua->T, "runtime error: %s", msg);
1103 else
1104 lua_pushfstring(lua->T, "unknown runtime error");
1105 ret = HLUA_E_ERRMSG;
1106 break;
1107
1108 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001109 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001110 lua_settop(lua->T, 0); /* Empty the stack. */
1111 if (!lua_checkstack(lua->T, 1)) {
1112 ret = HLUA_E_ERR;
1113 break;
1114 }
1115 lua_pushfstring(lua->T, "out of memory error");
1116 ret = HLUA_E_ERRMSG;
1117 break;
1118
1119 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001120 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001121 if (!lua_checkstack(lua->T, 1)) {
1122 ret = HLUA_E_ERR;
1123 break;
1124 }
1125 msg = lua_tostring(lua->T, -1);
1126 lua_settop(lua->T, 0); /* Empty the stack. */
1127 lua_pop(lua->T, 1);
1128 if (msg)
1129 lua_pushfstring(lua->T, "message handler error: %s", msg);
1130 else
1131 lua_pushfstring(lua->T, "message handler error");
1132 ret = HLUA_E_ERRMSG;
1133 break;
1134
1135 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001136 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001137 lua_settop(lua->T, 0); /* Empty the stack. */
1138 if (!lua_checkstack(lua->T, 1)) {
1139 ret = HLUA_E_ERR;
1140 break;
1141 }
1142 lua_pushfstring(lua->T, "unknonwn error");
1143 ret = HLUA_E_ERRMSG;
1144 break;
1145 }
1146
1147 switch (ret) {
1148 case HLUA_E_AGAIN:
1149 break;
1150
1151 case HLUA_E_ERRMSG:
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001152 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001153 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001154 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001155 break;
1156
1157 case HLUA_E_ERR:
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 hlua_ctx_renew(lua, 0);
1161 break;
1162
1163 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001164 HLUA_CLR_RUN(lua);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001165 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001166 break;
1167 }
1168
1169 return ret;
1170}
1171
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001172/* This function exit the current code. */
1173__LJMP static int hlua_done(lua_State *L)
1174{
1175 struct hlua *hlua = hlua_gethlua(L);
1176
1177 hlua->flags |= HLUA_EXIT;
1178 WILL_LJMP(lua_error(L));
1179
1180 return 0;
1181}
1182
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001183/* This function is an LUA binding. It provides a function
1184 * for deleting ACL from a referenced ACL file.
1185 */
1186__LJMP static int hlua_del_acl(lua_State *L)
1187{
1188 const char *name;
1189 const char *key;
1190 struct pat_ref *ref;
1191
1192 MAY_LJMP(check_args(L, 2, "del_acl"));
1193
1194 name = MAY_LJMP(luaL_checkstring(L, 1));
1195 key = MAY_LJMP(luaL_checkstring(L, 2));
1196
1197 ref = pat_ref_lookup(name);
1198 if (!ref)
1199 WILL_LJMP(luaL_error(L, "'del_acl': unkown acl file '%s'", name));
1200
1201 pat_ref_delete(ref, key);
1202 return 0;
1203}
1204
1205/* This function is an LUA binding. It provides a function
1206 * for deleting map entry from a referenced map file.
1207 */
1208static int hlua_del_map(lua_State *L)
1209{
1210 const char *name;
1211 const char *key;
1212 struct pat_ref *ref;
1213
1214 MAY_LJMP(check_args(L, 2, "del_map"));
1215
1216 name = MAY_LJMP(luaL_checkstring(L, 1));
1217 key = MAY_LJMP(luaL_checkstring(L, 2));
1218
1219 ref = pat_ref_lookup(name);
1220 if (!ref)
1221 WILL_LJMP(luaL_error(L, "'del_map': unkown acl file '%s'", name));
1222
1223 pat_ref_delete(ref, key);
1224 return 0;
1225}
1226
1227/* This function is an LUA binding. It provides a function
1228 * for adding ACL pattern from a referenced ACL file.
1229 */
1230static int hlua_add_acl(lua_State *L)
1231{
1232 const char *name;
1233 const char *key;
1234 struct pat_ref *ref;
1235
1236 MAY_LJMP(check_args(L, 2, "add_acl"));
1237
1238 name = MAY_LJMP(luaL_checkstring(L, 1));
1239 key = MAY_LJMP(luaL_checkstring(L, 2));
1240
1241 ref = pat_ref_lookup(name);
1242 if (!ref)
1243 WILL_LJMP(luaL_error(L, "'add_acl': unkown acl file '%s'", name));
1244
1245 if (pat_ref_find_elt(ref, key) == NULL)
1246 pat_ref_add(ref, key, NULL, NULL);
1247 return 0;
1248}
1249
1250/* This function is an LUA binding. It provides a function
1251 * for setting map pattern and sample from a referenced map
1252 * file.
1253 */
1254static int hlua_set_map(lua_State *L)
1255{
1256 const char *name;
1257 const char *key;
1258 const char *value;
1259 struct pat_ref *ref;
1260
1261 MAY_LJMP(check_args(L, 3, "set_map"));
1262
1263 name = MAY_LJMP(luaL_checkstring(L, 1));
1264 key = MAY_LJMP(luaL_checkstring(L, 2));
1265 value = MAY_LJMP(luaL_checkstring(L, 3));
1266
1267 ref = pat_ref_lookup(name);
1268 if (!ref)
1269 WILL_LJMP(luaL_error(L, "'set_map': unkown map file '%s'", name));
1270
1271 if (pat_ref_find_elt(ref, key) != NULL)
1272 pat_ref_set(ref, key, value, NULL);
1273 else
1274 pat_ref_add(ref, key, value, NULL);
1275 return 0;
1276}
1277
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001278/* A class is a lot of memory that contain data. This data can be a table,
1279 * an integer or user data. This data is associated with a metatable. This
1280 * metatable have an original version registred in the global context with
1281 * the name of the object (_G[<name>] = <metable> ).
1282 *
1283 * A metable is a table that modify the standard behavior of a standard
1284 * access to the associated data. The entries of this new metatable are
1285 * defined as is:
1286 *
1287 * http://lua-users.org/wiki/MetatableEvents
1288 *
1289 * __index
1290 *
1291 * we access an absent field in a table, the result is nil. This is
1292 * true, but it is not the whole truth. Actually, such access triggers
1293 * the interpreter to look for an __index metamethod: If there is no
1294 * such method, as usually happens, then the access results in nil;
1295 * otherwise, the metamethod will provide the result.
1296 *
1297 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1298 * the key does not appear in the table, but the metatable has an __index
1299 * property:
1300 *
1301 * - if the value is a function, the function is called, passing in the
1302 * table and the key; the return value of that function is returned as
1303 * the result.
1304 *
1305 * - if the value is another table, the value of the key in that table is
1306 * asked for and returned (and if it doesn't exist in that table, but that
1307 * table's metatable has an __index property, then it continues on up)
1308 *
1309 * - Use "rawget(myTable,key)" to skip this metamethod.
1310 *
1311 * http://www.lua.org/pil/13.4.1.html
1312 *
1313 * __newindex
1314 *
1315 * Like __index, but control property assignment.
1316 *
1317 * __mode - Control weak references. A string value with one or both
1318 * of the characters 'k' and 'v' which specifies that the the
1319 * keys and/or values in the table are weak references.
1320 *
1321 * __call - Treat a table like a function. When a table is followed by
1322 * parenthesis such as "myTable( 'foo' )" and the metatable has
1323 * a __call key pointing to a function, that function is invoked
1324 * (passing any specified arguments) and the return value is
1325 * returned.
1326 *
1327 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1328 * called, if the metatable for myTable has a __metatable
1329 * key, the value of that key is returned instead of the
1330 * actual metatable.
1331 *
1332 * __tostring - Control string representation. When the builtin
1333 * "tostring( myTable )" function is called, if the metatable
1334 * for myTable has a __tostring property set to a function,
1335 * that function is invoked (passing myTable to it) and the
1336 * return value is used as the string representation.
1337 *
1338 * __len - Control table length. When the table length is requested using
1339 * the length operator ( '#' ), if the metatable for myTable has
1340 * a __len key pointing to a function, that function is invoked
1341 * (passing myTable to it) and the return value used as the value
1342 * of "#myTable".
1343 *
1344 * __gc - Userdata finalizer code. When userdata is set to be garbage
1345 * collected, if the metatable has a __gc field pointing to a
1346 * function, that function is first invoked, passing the userdata
1347 * to it. The __gc metamethod is not called for tables.
1348 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1349 *
1350 * Special metamethods for redefining standard operators:
1351 * http://www.lua.org/pil/13.1.html
1352 *
1353 * __add "+"
1354 * __sub "-"
1355 * __mul "*"
1356 * __div "/"
1357 * __unm "!"
1358 * __pow "^"
1359 * __concat ".."
1360 *
1361 * Special methods for redfining standar relations
1362 * http://www.lua.org/pil/13.2.html
1363 *
1364 * __eq "=="
1365 * __lt "<"
1366 * __le "<="
1367 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001368
1369/*
1370 *
1371 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001372 * Class Map
1373 *
1374 *
1375 */
1376
1377/* Returns a struct hlua_map if the stack entry "ud" is
1378 * a class session, otherwise it throws an error.
1379 */
1380__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1381{
1382 return (struct map_descriptor *)MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
1383}
1384
1385/* This function is the map constructor. It don't need
1386 * the class Map object. It creates and return a new Map
1387 * object. It must be called only during "body" or "init"
1388 * context because it process some filesystem accesses.
1389 */
1390__LJMP static int hlua_map_new(struct lua_State *L)
1391{
1392 const char *fn;
1393 int match = PAT_MATCH_STR;
1394 struct sample_conv conv;
1395 const char *file = "";
1396 int line = 0;
1397 lua_Debug ar;
1398 char *err = NULL;
1399 struct arg args[2];
1400
1401 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1402 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1403
1404 fn = MAY_LJMP(luaL_checkstring(L, 1));
1405
1406 if (lua_gettop(L) >= 2) {
1407 match = MAY_LJMP(luaL_checkinteger(L, 2));
1408 if (match < 0 || match >= PAT_MATCH_NUM)
1409 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1410 }
1411
1412 /* Get Lua filename and line number. */
1413 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1414 lua_getinfo(L, "Sl", &ar); /* get info about it */
1415 if (ar.currentline > 0) { /* is there info? */
1416 file = ar.short_src;
1417 line = ar.currentline;
1418 }
1419 }
1420
1421 /* fill fake sample_conv struct. */
1422 conv.kw = ""; /* unused. */
1423 conv.process = NULL; /* unused. */
1424 conv.arg_mask = 0; /* unused. */
1425 conv.val_args = NULL; /* unused. */
1426 conv.out_type = SMP_T_STR;
1427 conv.private = (void *)(long)match;
1428 switch (match) {
1429 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1430 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1431 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1432 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1433 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1434 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1435 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001436 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001437 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1438 default:
1439 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1440 }
1441
1442 /* fill fake args. */
1443 args[0].type = ARGT_STR;
1444 args[0].data.str.str = (char *)fn;
1445 args[1].type = ARGT_STOP;
1446
1447 /* load the map. */
1448 if (!sample_load_map(args, &conv, file, line, &err)) {
1449 /* error case: we cant use luaL_error because we must
1450 * free the err variable.
1451 */
1452 luaL_where(L, 1);
1453 lua_pushfstring(L, "'new': %s.", err);
1454 lua_concat(L, 2);
1455 free(err);
1456 WILL_LJMP(lua_error(L));
1457 }
1458
1459 /* create the lua object. */
1460 lua_newtable(L);
1461 lua_pushlightuserdata(L, args[0].data.map);
1462 lua_rawseti(L, -2, 0);
1463
1464 /* Pop a class Map metatable and affect it to the userdata. */
1465 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1466 lua_setmetatable(L, -2);
1467
1468
1469 return 1;
1470}
1471
1472__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1473{
1474 struct map_descriptor *desc;
1475 struct pattern *pat;
1476 struct sample smp;
1477
1478 MAY_LJMP(check_args(L, 2, "lookup"));
1479 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001480 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001481 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001482 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001483 }
1484 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001485 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001486 smp.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001487 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 +02001488 }
1489
1490 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001491 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001492 if (str)
1493 lua_pushstring(L, "");
1494 else
1495 lua_pushnil(L);
1496 return 1;
1497 }
1498
1499 /* The Lua pattern must return a string, so we can't check the returned type */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001500 lua_pushlstring(L, pat->data->u.str.str, pat->data->u.str.len);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001501 return 1;
1502}
1503
1504__LJMP static int hlua_map_lookup(struct lua_State *L)
1505{
1506 return _hlua_map_lookup(L, 0);
1507}
1508
1509__LJMP static int hlua_map_slookup(struct lua_State *L)
1510{
1511 return _hlua_map_lookup(L, 1);
1512}
1513
1514/*
1515 *
1516 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001517 * Class Socket
1518 *
1519 *
1520 */
1521
1522__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1523{
1524 return (struct hlua_socket *)MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
1525}
1526
1527/* This function is the handler called for each I/O on the established
1528 * connection. It is used for notify space avalaible to send or data
1529 * received.
1530 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001531static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001532{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001533 struct stream_interface *si = appctx->owner;
Willy Tarreau50fe03b2014-11-28 13:59:31 +01001534 struct connection *c = objt_conn(si_opposite(si)->end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001535
Willy Tarreau87b09662015-04-03 00:22:06 +02001536 /* Wakeup the main stream if the client connection is closed. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001537 if (!c || channel_output_closed(si_ic(si)) || channel_input_closed(si_oc(si))) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001538 if (appctx->ctx.hlua.socket) {
1539 appctx->ctx.hlua.socket->s = NULL;
1540 appctx->ctx.hlua.socket = NULL;
1541 }
1542 si_shutw(si);
1543 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001544 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001545 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1546 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001547 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001548 }
1549
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001550 /* if the connection is not estabkished, inform the stream that we want
1551 * to be notified whenever the connection completes.
1552 */
1553 if (!(c->flags & CO_FL_CONNECTED)) {
1554 si_applet_cant_get(si);
1555 si_applet_cant_put(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001556 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001557 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001558
1559 /* This function is called after the connect. */
1560 appctx->ctx.hlua.connected = 1;
1561
1562 /* Wake the tasks which wants to write if the buffer have avalaible space. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001563 if (channel_may_recv(si_oc(si)))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001564 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
1565
1566 /* Wake the tasks which wants to read if the buffer contains data. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001567 if (channel_is_empty(si_ic(si)))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001568 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1569}
1570
Willy Tarreau87b09662015-04-03 00:22:06 +02001571/* This function is called when the "struct stream" is destroyed.
1572 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001573 * Wake all the pending signals.
1574 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001575static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001576{
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001577 /* Remove my link in the original object. */
1578 if (appctx->ctx.hlua.socket)
1579 appctx->ctx.hlua.socket->s = NULL;
1580
1581 /* Wake all the task waiting for me. */
1582 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1583 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
1584}
1585
1586/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001587 * uses this object. If the stream does not exists, just quit.
1588 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001589 * pending signal can rest in the read and write lists. destroy
1590 * it.
1591 */
1592__LJMP static int hlua_socket_gc(lua_State *L)
1593{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001594 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001595 struct appctx *appctx;
1596
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001597 MAY_LJMP(check_args(L, 1, "__gc"));
1598
1599 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001600 if (!socket->s)
1601 return 0;
1602
Willy Tarreau87b09662015-04-03 00:22:06 +02001603 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001604 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001605 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001606 socket->s = NULL;
1607 appctx->ctx.hlua.socket = NULL;
1608
1609 return 0;
1610}
1611
1612/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001613 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001614 */
1615__LJMP static int hlua_socket_close(lua_State *L)
1616{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001617 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001618 struct appctx *appctx;
1619
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001620 MAY_LJMP(check_args(L, 1, "close"));
1621
1622 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001623 if (!socket->s)
1624 return 0;
1625
Willy Tarreau87b09662015-04-03 00:22:06 +02001626 /* Close the stream and remove the associated stop task. */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001627 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001628 appctx = objt_appctx(socket->s->si[0].end);
1629 appctx->ctx.hlua.socket = NULL;
1630 socket->s = NULL;
1631
1632 return 0;
1633}
1634
1635/* This Lua function assumes that the stack contain three parameters.
1636 * 1 - USERDATA containing a struct socket
1637 * 2 - INTEGER with values of the macro defined below
1638 * If the integer is -1, we must read at most one line.
1639 * If the integer is -2, we ust read all the data until the
1640 * end of the stream.
1641 * If the integer is positive value, we must read a number of
1642 * bytes corresponding to this value.
1643 */
1644#define HLSR_READ_LINE (-1)
1645#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001646__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001647{
1648 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1649 int wanted = lua_tointeger(L, 2);
1650 struct hlua *hlua = hlua_gethlua(L);
1651 struct appctx *appctx;
1652 int len;
1653 int nblk;
1654 char *blk1;
1655 int len1;
1656 char *blk2;
1657 int len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001658 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001659 struct channel *oc;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001660
1661 /* Check if this lua stack is schedulable. */
1662 if (!hlua || !hlua->task)
1663 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1664 "'frontend', 'backend' or 'task'"));
1665
1666 /* check for connection closed. If some data where read, return it. */
1667 if (!socket->s)
1668 goto connection_closed;
1669
Willy Tarreau94aa6172015-03-13 14:19:06 +01001670 oc = &socket->s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001671 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001672 /* Read line. */
Willy Tarreau81389672015-03-10 12:03:52 +01001673 nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001674 if (nblk < 0) /* Connection close. */
1675 goto connection_closed;
1676 if (nblk == 0) /* No data avalaible. */
1677 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001678
1679 /* remove final \r\n. */
1680 if (nblk == 1) {
1681 if (blk1[len1-1] == '\n') {
1682 len1--;
1683 skip_at_end++;
1684 if (blk1[len1-1] == '\r') {
1685 len1--;
1686 skip_at_end++;
1687 }
1688 }
1689 }
1690 else {
1691 if (blk2[len2-1] == '\n') {
1692 len2--;
1693 skip_at_end++;
1694 if (blk2[len2-1] == '\r') {
1695 len2--;
1696 skip_at_end++;
1697 }
1698 }
1699 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001700 }
1701
1702 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703 /* Read all the available data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001704 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001705 if (nblk < 0) /* Connection close. */
1706 goto connection_closed;
1707 if (nblk == 0) /* No data avalaible. */
1708 goto connection_empty;
1709 }
1710
1711 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001712 /* Read a block of data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001713 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001714 if (nblk < 0) /* Connection close. */
1715 goto connection_closed;
1716 if (nblk == 0) /* No data avalaible. */
1717 goto connection_empty;
1718
1719 if (len1 > wanted) {
1720 nblk = 1;
1721 len1 = wanted;
1722 } if (nblk == 2 && len1 + len2 > wanted)
1723 len2 = wanted - len1;
1724 }
1725
1726 len = len1;
1727
1728 luaL_addlstring(&socket->b, blk1, len1);
1729 if (nblk == 2) {
1730 len += len2;
1731 luaL_addlstring(&socket->b, blk2, len2);
1732 }
1733
1734 /* Consume data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001735 bo_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001736
1737 /* Don't wait anything. */
Willy Tarreau828824a2015-04-19 17:20:03 +02001738 si_applet_done(&socket->s->si[0]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001739
1740 /* If the pattern reclaim to read all the data
1741 * in the connection, got out.
1742 */
1743 if (wanted == HLSR_READ_ALL)
1744 goto connection_empty;
1745 else if (wanted >= 0 && len < wanted)
1746 goto connection_empty;
1747
1748 /* Return result. */
1749 luaL_pushresult(&socket->b);
1750 return 1;
1751
1752connection_closed:
1753
1754 /* If the buffer containds data. */
1755 if (socket->b.n > 0) {
1756 luaL_pushresult(&socket->b);
1757 return 1;
1758 }
1759 lua_pushnil(L);
1760 lua_pushstring(L, "connection closed.");
1761 return 2;
1762
1763connection_empty:
1764
1765 appctx = objt_appctx(socket->s->si[0].end);
1766 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_read))
1767 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001768 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001769 return 0;
1770}
1771
1772/* This Lus function gets two parameters. The first one can be string
1773 * or a number. If the string is "*l", the user require one line. If
1774 * the string is "*a", the user require all the content of the stream.
1775 * If the value is a number, the user require a number of bytes equal
1776 * to the value. The default value is "*l" (a line).
1777 *
1778 * This paraeter with a variable type is converted in integer. This
1779 * integer takes this values:
1780 * -1 : read a line
1781 * -2 : read all the stream
1782 * >0 : amount if bytes.
1783 *
1784 * The second parameter is optinal. It contains a string that must be
1785 * concatenated with the read data.
1786 */
1787__LJMP static int hlua_socket_receive(struct lua_State *L)
1788{
1789 int wanted = HLSR_READ_LINE;
1790 const char *pattern;
1791 int type;
1792 char *error;
1793 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001794 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001795
1796 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1797 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1798
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001799 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001800
1801 /* check for pattern. */
1802 if (lua_gettop(L) >= 2) {
1803 type = lua_type(L, 2);
1804 if (type == LUA_TSTRING) {
1805 pattern = lua_tostring(L, 2);
1806 if (strcmp(pattern, "*a") == 0)
1807 wanted = HLSR_READ_ALL;
1808 else if (strcmp(pattern, "*l") == 0)
1809 wanted = HLSR_READ_LINE;
1810 else {
1811 wanted = strtoll(pattern, &error, 10);
1812 if (*error != '\0')
1813 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1814 }
1815 }
1816 else if (type == LUA_TNUMBER) {
1817 wanted = lua_tointeger(L, 2);
1818 if (wanted < 0)
1819 WILL_LJMP(luaL_error(L, "Unsupported size."));
1820 }
1821 }
1822
1823 /* Set pattern. */
1824 lua_pushinteger(L, wanted);
1825 lua_replace(L, 2);
1826
1827 /* init bufffer, and fiil it wih prefix. */
1828 luaL_buffinit(L, &socket->b);
1829
1830 /* Check prefix. */
1831 if (lua_gettop(L) >= 3) {
1832 if (lua_type(L, 3) != LUA_TSTRING)
1833 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1834 pattern = lua_tolstring(L, 3, &len);
1835 luaL_addlstring(&socket->b, pattern, len);
1836 }
1837
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001838 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001839}
1840
1841/* Write the Lua input string in the output buffer.
1842 * This fucntion returns a yield if no space are available.
1843 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001844static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001845{
1846 struct hlua_socket *socket;
1847 struct hlua *hlua = hlua_gethlua(L);
1848 struct appctx *appctx;
1849 size_t buf_len;
1850 const char *buf;
1851 int len;
1852 int send_len;
1853 int sent;
1854
1855 /* Check if this lua stack is schedulable. */
1856 if (!hlua || !hlua->task)
1857 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1858 "'frontend', 'backend' or 'task'"));
1859
1860 /* Get object */
1861 socket = MAY_LJMP(hlua_checksocket(L, 1));
1862 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001863 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001864
1865 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001866 if (!socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001867 lua_pushinteger(L, -1);
1868 return 1;
1869 }
1870
1871 /* Update the input buffer data. */
1872 buf += sent;
1873 send_len = buf_len - sent;
1874
1875 /* All the data are sent. */
1876 if (sent >= buf_len)
1877 return 1; /* Implicitly return the length sent. */
1878
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001879 /* Check if the buffer is avalaible because HAProxy doesn't allocate
1880 * the request buffer if its not required.
1881 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001882 if (socket->s->req.buf->size == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001883 if (!stream_alloc_recv_buffer(&socket->s->req)) {
Willy Tarreau350f4872014-11-28 14:42:25 +01001884 socket->s->si[0].flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001885 goto hlua_socket_write_yield_return;
1886 }
1887 }
1888
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001889 /* Check for avalaible space. */
Willy Tarreau94aa6172015-03-13 14:19:06 +01001890 len = buffer_total_space(socket->s->req.buf);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001891 if (len <= 0)
1892 goto hlua_socket_write_yield_return;
1893
1894 /* send data */
1895 if (len < send_len)
1896 send_len = len;
Willy Tarreau94aa6172015-03-13 14:19:06 +01001897 len = bi_putblk(&socket->s->req, buf+sent, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001898
1899 /* "Not enough space" (-1), "Buffer too little to contain
1900 * the data" (-2) are not expected because the available length
1901 * is tested.
1902 * Other unknown error are also not expected.
1903 */
1904 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01001905 if (len == -1)
Willy Tarreau94aa6172015-03-13 14:19:06 +01001906 socket->s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001907
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908 MAY_LJMP(hlua_socket_close(L));
1909 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001910 lua_pushinteger(L, -1);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001911 return 1;
1912 }
1913
1914 /* update buffers. */
Willy Tarreau828824a2015-04-19 17:20:03 +02001915 si_applet_done(&socket->s->si[0]);
Willy Tarreau94aa6172015-03-13 14:19:06 +01001916 socket->s->req.rex = TICK_ETERNITY;
1917 socket->s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001918
1919 /* Update length sent. */
1920 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001921 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001922
1923 /* All the data buffer is sent ? */
1924 if (sent + len >= buf_len)
1925 return 1;
1926
1927hlua_socket_write_yield_return:
1928 appctx = objt_appctx(socket->s->si[0].end);
1929 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
1930 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001931 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001932 return 0;
1933}
1934
1935/* This function initiate the send of data. It just check the input
1936 * parameters and push an integer in the Lua stack that contain the
1937 * amount of data writed in the buffer. This is used by the function
1938 * "hlua_socket_write_yield" that can yield.
1939 *
1940 * The Lua function gets between 3 and 4 parameters. The first one is
1941 * the associated object. The second is a string buffer. The third is
1942 * a facultative integer that represents where is the buffer position
1943 * of the start of the data that can send. The first byte is the
1944 * position "1". The default value is "1". The fourth argument is a
1945 * facultative integer that represents where is the buffer position
1946 * of the end of the data that can send. The default is the last byte.
1947 */
1948static int hlua_socket_send(struct lua_State *L)
1949{
1950 int i;
1951 int j;
1952 const char *buf;
1953 size_t buf_len;
1954
1955 /* Check number of arguments. */
1956 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
1957 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
1958
1959 /* Get the string. */
1960 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
1961
1962 /* Get and check j. */
1963 if (lua_gettop(L) == 4) {
1964 j = MAY_LJMP(luaL_checkinteger(L, 4));
1965 if (j < 0)
1966 j = buf_len + j + 1;
1967 if (j > buf_len)
1968 j = buf_len + 1;
1969 lua_pop(L, 1);
1970 }
1971 else
1972 j = buf_len;
1973
1974 /* Get and check i. */
1975 if (lua_gettop(L) == 3) {
1976 i = MAY_LJMP(luaL_checkinteger(L, 3));
1977 if (i < 0)
1978 i = buf_len + i + 1;
1979 if (i > buf_len)
1980 i = buf_len + 1;
1981 lua_pop(L, 1);
1982 } else
1983 i = 1;
1984
1985 /* Check bth i and j. */
1986 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001987 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001988 return 1;
1989 }
1990 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001991 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001992 return 1;
1993 }
1994 if (i == 0)
1995 i = 1;
1996 if (j == 0)
1997 j = 1;
1998
1999 /* Pop the string. */
2000 lua_pop(L, 1);
2001
2002 /* Update the buffer length. */
2003 buf += i - 1;
2004 buf_len = j - i + 1;
2005 lua_pushlstring(L, buf, buf_len);
2006
2007 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002008 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002009
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002010 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002011}
2012
Willy Tarreau22b0a682015-06-17 19:43:49 +02002013#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002014__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2015{
2016 static char buffer[SOCKET_INFO_MAX_LEN];
2017 int ret;
2018 int len;
2019 char *p;
2020
2021 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2022 if (ret <= 0) {
2023 lua_pushnil(L);
2024 return 1;
2025 }
2026
2027 if (ret == AF_UNIX) {
2028 lua_pushstring(L, buffer+1);
2029 return 1;
2030 }
2031 else if (ret == AF_INET6) {
2032 buffer[0] = '[';
2033 len = strlen(buffer);
2034 buffer[len] = ']';
2035 len++;
2036 buffer[len] = ':';
2037 len++;
2038 p = buffer;
2039 }
2040 else if (ret == AF_INET) {
2041 p = buffer + 1;
2042 len = strlen(p);
2043 p[len] = ':';
2044 len++;
2045 }
2046 else {
2047 lua_pushnil(L);
2048 return 1;
2049 }
2050
2051 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2052 lua_pushnil(L);
2053 return 1;
2054 }
2055
2056 lua_pushstring(L, p);
2057 return 1;
2058}
2059
2060/* Returns information about the peer of the connection. */
2061__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2062{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002063 struct hlua_socket *socket;
2064 struct connection *conn;
2065
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002066 MAY_LJMP(check_args(L, 1, "getpeername"));
2067
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002068 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002069
2070 /* Check if the tcp object is avalaible. */
2071 if (!socket->s) {
2072 lua_pushnil(L);
2073 return 1;
2074 }
2075
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002076 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002077 if (!conn) {
2078 lua_pushnil(L);
2079 return 1;
2080 }
2081
2082 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
2083 unsigned int salen = sizeof(conn->addr.to);
2084 if (getpeername(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, &salen) == -1) {
2085 lua_pushnil(L);
2086 return 1;
2087 }
2088 conn->flags |= CO_FL_ADDR_TO_SET;
2089 }
2090
2091 return MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2092}
2093
2094/* Returns information about my connection side. */
2095static int hlua_socket_getsockname(struct lua_State *L)
2096{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002097 struct hlua_socket *socket;
2098 struct connection *conn;
2099
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002100 MAY_LJMP(check_args(L, 1, "getsockname"));
2101
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002102 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002103
2104 /* Check if the tcp object is avalaible. */
2105 if (!socket->s) {
2106 lua_pushnil(L);
2107 return 1;
2108 }
2109
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002110 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002111 if (!conn) {
2112 lua_pushnil(L);
2113 return 1;
2114 }
2115
2116 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
2117 unsigned int salen = sizeof(conn->addr.from);
2118 if (getsockname(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, &salen) == -1) {
2119 lua_pushnil(L);
2120 return 1;
2121 }
2122 conn->flags |= CO_FL_ADDR_FROM_SET;
2123 }
2124
2125 return hlua_socket_info(L, &conn->addr.from);
2126}
2127
2128/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002129static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002130 .obj_type = OBJ_TYPE_APPLET,
2131 .name = "<LUA_TCP>",
2132 .fct = hlua_socket_handler,
2133 .release = hlua_socket_release,
2134};
2135
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002136__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002137{
2138 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2139 struct hlua *hlua = hlua_gethlua(L);
2140 struct appctx *appctx;
2141
2142 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002143 if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002144 lua_pushnil(L);
2145 lua_pushstring(L, "Can't connect");
2146 return 2;
2147 }
2148
2149 appctx = objt_appctx(socket->s->si[0].end);
2150
2151 /* Check for connection established. */
2152 if (appctx->ctx.hlua.connected) {
2153 lua_pushinteger(L, 1);
2154 return 1;
2155 }
2156
2157 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2158 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002159 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002160 return 0;
2161}
2162
2163/* This function fail or initite the connection. */
2164__LJMP static int hlua_socket_connect(struct lua_State *L)
2165{
2166 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002167 int port;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002168 const char *ip;
2169 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002170 struct hlua *hlua;
2171 struct appctx *appctx;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002172
2173 MAY_LJMP(check_args(L, 3, "connect"));
2174
2175 /* Get args. */
2176 socket = MAY_LJMP(hlua_checksocket(L, 1));
2177 ip = MAY_LJMP(luaL_checkstring(L, 2));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002178 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002179
Willy Tarreau973a5422015-08-05 21:47:23 +02002180 conn = si_alloc_conn(&socket->s->si[1]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002181 if (!conn)
2182 WILL_LJMP(luaL_error(L, "connect: internal error"));
2183
2184 /* Parse ip address. */
2185 conn->addr.to.ss_family = AF_UNSPEC;
2186 if (!str2ip2(ip, &conn->addr.to, 0))
2187 WILL_LJMP(luaL_error(L, "connect: cannot parse ip address '%s'", ip));
2188
2189 /* Set port. */
2190 if (conn->addr.to.ss_family == AF_INET)
2191 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2192 else if (conn->addr.to.ss_family == AF_INET6)
2193 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2194
2195 /* it is important not to call the wakeup function directly but to
2196 * pass through task_wakeup(), because this one knows how to apply
2197 * priorities to tasks.
2198 */
2199 task_wakeup(socket->s->task, TASK_WOKEN_INIT);
2200
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002201 hlua = hlua_gethlua(L);
2202 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002203
2204 /* inform the stream that we want to be notified whenever the
2205 * connection completes.
2206 */
2207 si_applet_cant_get(&socket->s->si[0]);
2208 si_applet_cant_put(&socket->s->si[0]);
2209
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002210 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2211 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002212 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002213
2214 return 0;
2215}
2216
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002217#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002218__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2219{
2220 struct hlua_socket *socket;
2221
2222 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2223 socket = MAY_LJMP(hlua_checksocket(L, 1));
2224 socket->s->target = &socket_ssl.obj_type;
2225 return MAY_LJMP(hlua_socket_connect(L));
2226}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002227#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002228
2229__LJMP static int hlua_socket_setoption(struct lua_State *L)
2230{
2231 return 0;
2232}
2233
2234__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2235{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002236 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002237 int tmout;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002238
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002239 MAY_LJMP(check_args(L, 2, "settimeout"));
2240
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002241 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002242 tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002243
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002244 socket->s->req.rto = tmout;
2245 socket->s->req.wto = tmout;
2246 socket->s->res.rto = tmout;
2247 socket->s->res.wto = tmout;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002248
2249 return 0;
2250}
2251
2252__LJMP static int hlua_socket_new(lua_State *L)
2253{
2254 struct hlua_socket *socket;
2255 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002256 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002257 struct stream *strm;
Willy Tarreaud420a972015-04-06 00:39:18 +02002258 struct task *task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002259
2260 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002261 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002262 hlua_pusherror(L, "socket: full stack");
2263 goto out_fail_conf;
2264 }
2265
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002266 /* Create the object: obj[0] = userdata. */
2267 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002268 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002269 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002270 memset(socket, 0, sizeof(*socket));
2271
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002272 /* Check if the various memory pools are intialized. */
Willy Tarreau87b09662015-04-03 00:22:06 +02002273 if (!pool2_stream || !pool2_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002274 hlua_pusherror(L, "socket: uninitialized pools.");
2275 goto out_fail_conf;
2276 }
2277
Willy Tarreau87b09662015-04-03 00:22:06 +02002278 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002279 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2280 lua_setmetatable(L, -2);
2281
Willy Tarreaud420a972015-04-06 00:39:18 +02002282 /* Create the applet context */
2283 appctx = appctx_new(&update_applet);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002284 if (!appctx) {
2285 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002286 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002287 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002288
Willy Tarreaud420a972015-04-06 00:39:18 +02002289 appctx->ctx.hlua.socket = socket;
2290 appctx->ctx.hlua.connected = 0;
2291 LIST_INIT(&appctx->ctx.hlua.wake_on_write);
2292 LIST_INIT(&appctx->ctx.hlua.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002293
Willy Tarreaud420a972015-04-06 00:39:18 +02002294 /* Now create a session, task and stream for this applet */
2295 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2296 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002297 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002298 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002299 }
2300
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002301 task = task_new();
2302 if (!task) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002304 goto out_fail_task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002305 }
Willy Tarreaud420a972015-04-06 00:39:18 +02002306 task->nice = 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307
Willy Tarreau73b65ac2015-04-08 18:26:29 +02002308 strm = stream_new(sess, task, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002309 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002310 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002311 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002312 }
2313
Willy Tarreaud420a972015-04-06 00:39:18 +02002314 /* Configure an empty Lua for the stream. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002315 socket->s = strm;
2316 strm->hlua.T = NULL;
2317 strm->hlua.Tref = LUA_REFNIL;
2318 strm->hlua.Mref = LUA_REFNIL;
2319 strm->hlua.nargs = 0;
2320 strm->hlua.flags = 0;
2321 LIST_INIT(&strm->hlua.com);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002322
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002323 /* Configure "right" stream interface. this "si" is used to connect
2324 * and retrieve data from the server. The connection is initialized
2325 * with the "struct server".
2326 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002327 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002328
2329 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002330 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2331 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002332
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002333 /* Update statistics counters. */
2334 socket_proxy.feconn++; /* beconn will be increased later */
2335 jobs++;
2336 totalconn++;
2337
2338 /* Return yield waiting for connection. */
2339 return 1;
2340
Willy Tarreaud420a972015-04-06 00:39:18 +02002341 out_fail_stream:
2342 task_free(task);
2343 out_fail_task:
Willy Tarreau11c36242015-04-04 15:54:03 +02002344 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002345 out_fail_sess:
2346 appctx_free(appctx);
2347 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002348 WILL_LJMP(lua_error(L));
2349 return 0;
2350}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002351
2352/*
2353 *
2354 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002355 * Class Channel
2356 *
2357 *
2358 */
2359
2360/* Returns the struct hlua_channel join to the class channel in the
2361 * stack entry "ud" or throws an argument error.
2362 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002363__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002364{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002365 return (struct channel *)MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002366}
2367
Willy Tarreau47860ed2015-03-10 14:07:50 +01002368/* Pushes the channel onto the top of the stack. If the stask does not have a
2369 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002370 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002371static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002372{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002373 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002374 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002375 return 0;
2376
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002377 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002378 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002379 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002380
2381 /* Pop a class sesison metatable and affect it to the userdata. */
2382 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2383 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002384 return 1;
2385}
2386
2387/* Duplicate all the data present in the input channel and put it
2388 * in a string LUA variables. Returns -1 and push a nil value in
2389 * the stack if the channel is closed and all the data are consumed,
2390 * returns 0 if no data are available, otherwise it returns the length
2391 * of the builded string.
2392 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002393static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002394{
2395 char *blk1;
2396 char *blk2;
2397 int len1;
2398 int len2;
2399 int ret;
2400 luaL_Buffer b;
2401
Willy Tarreau47860ed2015-03-10 14:07:50 +01002402 ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002403 if (unlikely(ret == 0))
2404 return 0;
2405
2406 if (unlikely(ret < 0)) {
2407 lua_pushnil(L);
2408 return -1;
2409 }
2410
2411 luaL_buffinit(L, &b);
2412 luaL_addlstring(&b, blk1, len1);
2413 if (unlikely(ret == 2))
2414 luaL_addlstring(&b, blk2, len2);
2415 luaL_pushresult(&b);
2416
2417 if (unlikely(ret == 2))
2418 return len1 + len2;
2419 return len1;
2420}
2421
2422/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2423 * a yield. This function keep the data in the buffer.
2424 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002425__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002426{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002427 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002428
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002429 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2430
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002431 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002432 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002433 return 1;
2434}
2435
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002436/* Check arguments for the function "hlua_channel_dup_yield". */
2437__LJMP static int hlua_channel_dup(lua_State *L)
2438{
2439 MAY_LJMP(check_args(L, 1, "dup"));
2440 MAY_LJMP(hlua_checkchannel(L, 1));
2441 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2442}
2443
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002444/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2445 * a yield. This function consumes the data in the buffer. It returns
2446 * a string containing the data or a nil pointer if no data are available
2447 * and the channel is closed.
2448 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002449__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002450{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002451 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002452 int ret;
2453
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002454 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002455
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002456 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002457 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002458 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002459
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002460 if (unlikely(ret == -1))
2461 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002462
Willy Tarreau47860ed2015-03-10 14:07:50 +01002463 chn->buf->i -= ret;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002464 return 1;
2465}
2466
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002467/* Check arguments for the fucntion "hlua_channel_get_yield". */
2468__LJMP static int hlua_channel_get(lua_State *L)
2469{
2470 MAY_LJMP(check_args(L, 1, "get"));
2471 MAY_LJMP(hlua_checkchannel(L, 1));
2472 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2473}
2474
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002475/* This functions consumes and returns one line. If the channel is closed,
2476 * and the last data does not contains a final '\n', the data are returned
2477 * without the final '\n'. When no more data are avalaible, it returns nil
2478 * value.
2479 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002480__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002481{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002482 char *blk1;
2483 char *blk2;
2484 int len1;
2485 int len2;
2486 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002487 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002488 int ret;
2489 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002490
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002491 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2492
Willy Tarreau47860ed2015-03-10 14:07:50 +01002493 ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002494 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002495 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002496
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002497 if (ret == -1) {
2498 lua_pushnil(L);
2499 return 1;
2500 }
2501
2502 luaL_buffinit(L, &b);
2503 luaL_addlstring(&b, blk1, len1);
2504 len = len1;
2505 if (unlikely(ret == 2)) {
2506 luaL_addlstring(&b, blk2, len2);
2507 len += len2;
2508 }
2509 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002510 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002511 return 1;
2512}
2513
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002514/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2515__LJMP static int hlua_channel_getline(lua_State *L)
2516{
2517 MAY_LJMP(check_args(L, 1, "getline"));
2518 MAY_LJMP(hlua_checkchannel(L, 1));
2519 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2520}
2521
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002522/* This function takes a string as input, and append it at the
2523 * input side of channel. If the data is too big, but a space
2524 * is probably available after sending some data, the function
2525 * yield. If the data is bigger than the buffer, or if the
2526 * channel is closed, it returns -1. otherwise, it returns the
2527 * amount of data writed.
2528 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002529__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002530{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002531 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002532 size_t len;
2533 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2534 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2535 int ret;
2536 int max;
2537
Willy Tarreau47860ed2015-03-10 14:07:50 +01002538 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002539 if (max > len - l)
2540 max = len - l;
2541
Willy Tarreau47860ed2015-03-10 14:07:50 +01002542 ret = bi_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002543 if (ret == -2 || ret == -3) {
2544 lua_pushinteger(L, -1);
2545 return 1;
2546 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002547 if (ret == -1) {
2548 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002549 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002550 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002551 l += ret;
2552 lua_pop(L, 1);
2553 lua_pushinteger(L, l);
2554
Willy Tarreau47860ed2015-03-10 14:07:50 +01002555 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2556 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002557 /* There are no space avalaible, and the output buffer is empty.
2558 * in this case, we cannot add more data, so we cannot yield,
2559 * we return the amount of copyied data.
2560 */
2561 return 1;
2562 }
2563 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002564 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002565 return 1;
2566}
2567
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002568/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002569 * of the writed string, or -1 if the channel is closed or if the
2570 * buffer size is too little for the data.
2571 */
2572__LJMP static int hlua_channel_append(lua_State *L)
2573{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002574 size_t len;
2575
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002576 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002577 MAY_LJMP(hlua_checkchannel(L, 1));
2578 MAY_LJMP(luaL_checklstring(L, 2, &len));
2579 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002580 lua_pushinteger(L, 0);
2581
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002582 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002583}
2584
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002585/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002586 * his process by cleaning the buffer. The result is a replacement
2587 * of the current data. It returns the length of the writed string,
2588 * or -1 if the channel is closed or if the buffer size is too
2589 * little for the data.
2590 */
2591__LJMP static int hlua_channel_set(lua_State *L)
2592{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002593 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002594
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002595 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002596 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002597 lua_pushinteger(L, 0);
2598
Willy Tarreau47860ed2015-03-10 14:07:50 +01002599 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002600
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002601 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002602}
2603
2604/* Append data in the output side of the buffer. This data is immediatly
2605 * sent. The fcuntion returns the ammount of data writed. If the buffer
2606 * cannot contains the data, the function yield. The function returns -1
2607 * if the channel is closed.
2608 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002609__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002610{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002611 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002612 size_t len;
2613 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2614 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2615 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002616 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002617
Willy Tarreau47860ed2015-03-10 14:07:50 +01002618 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002619 lua_pushinteger(L, -1);
2620 return 1;
2621 }
2622
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002623 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2624 * the request buffer if its not required.
2625 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002626 if (chn->buf->size == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002627 if (!stream_alloc_recv_buffer(chn)) {
Willy Tarreau47860ed2015-03-10 14:07:50 +01002628 chn_prod(chn)->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002629 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002630 }
2631 }
2632
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002633 /* the writed data will be immediatly sent, so we can check
2634 * the avalaible space without taking in account the reserve.
2635 * The reserve is guaranted for the processing of incoming
2636 * data, because the buffer will be flushed.
2637 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002638 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002639
2640 /* If there are no space avalaible, and the output buffer is empty.
2641 * in this case, we cannot add more data, so we cannot yield,
2642 * we return the amount of copyied data.
2643 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002644 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002645 return 1;
2646
2647 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002648 if (max > len - l)
2649 max = len - l;
2650
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002651 /* The buffer avalaible size may be not contiguous. This test
2652 * detects a non contiguous buffer and realign it.
2653 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002654 if (bi_space_for_replace(chn->buf) < max)
2655 buffer_slow_realign(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002656
2657 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002658 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002659
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002660 /* buffer replace considers that the input part is filled.
2661 * so, I must forward these new data in the output part.
2662 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002663 b_adv(chn->buf, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002664
2665 l += max;
2666 lua_pop(L, 1);
2667 lua_pushinteger(L, l);
2668
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002669 /* If there are no space avalaible, and the output buffer is empty.
2670 * in this case, we cannot add more data, so we cannot yield,
2671 * we return the amount of copyied data.
2672 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002673 max = chn->buf->size - buffer_len(chn->buf);
2674 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002675 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002676
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002677 if (l < len) {
2678 /* If we are waiting for space in the response buffer, we
2679 * must set the flag WAKERESWR. This flag required the task
2680 * wake up if any activity is detected on the response buffer.
2681 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002682 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002683 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002684 else
2685 HLUA_SET_WAKEREQWR(hlua);
2686 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002687 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002688
2689 return 1;
2690}
2691
2692/* Just a wraper of "_hlua_channel_send". This wrapper permits
2693 * yield the LUA process, and resume it without checking the
2694 * input arguments.
2695 */
2696__LJMP static int hlua_channel_send(lua_State *L)
2697{
2698 MAY_LJMP(check_args(L, 2, "send"));
2699 lua_pushinteger(L, 0);
2700
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002701 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002702}
2703
2704/* This function forward and amount of butes. The data pass from
2705 * the input side of the buffer to the output side, and can be
2706 * forwarded. This function never fails.
2707 *
2708 * The Lua function takes an amount of bytes to be forwarded in
2709 * imput. It returns the number of bytes forwarded.
2710 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002711__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002712{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002713 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002714 int len;
2715 int l;
2716 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002717 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002718
2719 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2720 len = MAY_LJMP(luaL_checkinteger(L, 2));
2721 l = MAY_LJMP(luaL_checkinteger(L, -1));
2722
2723 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002724 if (max > chn->buf->i)
2725 max = chn->buf->i;
2726 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002727 l += max;
2728
2729 lua_pop(L, 1);
2730 lua_pushinteger(L, l);
2731
2732 /* Check if it miss bytes to forward. */
2733 if (l < len) {
2734 /* The the input channel or the output channel are closed, we
2735 * must return the amount of data forwarded.
2736 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002737 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002738 return 1;
2739
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002740 /* If we are waiting for space data in the response buffer, we
2741 * must set the flag WAKERESWR. This flag required the task
2742 * wake up if any activity is detected on the response buffer.
2743 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002744 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002745 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002746 else
2747 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002748
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002749 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002750 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002751 }
2752
2753 return 1;
2754}
2755
2756/* Just check the input and prepare the stack for the previous
2757 * function "hlua_channel_forward_yield"
2758 */
2759__LJMP static int hlua_channel_forward(lua_State *L)
2760{
2761 MAY_LJMP(check_args(L, 2, "forward"));
2762 MAY_LJMP(hlua_checkchannel(L, 1));
2763 MAY_LJMP(luaL_checkinteger(L, 2));
2764
2765 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002766 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002767}
2768
2769/* Just returns the number of bytes available in the input
2770 * side of the buffer. This function never fails.
2771 */
2772__LJMP static int hlua_channel_get_in_len(lua_State *L)
2773{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002774 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002775
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002776 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002777 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002778 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002779 return 1;
2780}
2781
2782/* Just returns the number of bytes available in the output
2783 * side of the buffer. This function never fails.
2784 */
2785__LJMP static int hlua_channel_get_out_len(lua_State *L)
2786{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002787 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002788
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002789 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002790 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002791 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002792 return 1;
2793}
2794
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002795/*
2796 *
2797 *
2798 * Class Fetches
2799 *
2800 *
2801 */
2802
2803/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002804 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002805 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002806__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002807{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002808 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002809}
2810
2811/* This function creates and push in the stack a fetch object according
2812 * with a current TXN.
2813 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002814static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002815{
Willy Tarreau7073c472015-04-06 11:15:40 +02002816 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002817
2818 /* Check stack size. */
2819 if (!lua_checkstack(L, 3))
2820 return 0;
2821
2822 /* Create the object: obj[0] = userdata.
2823 * Note that the base of the Fetches object is the
2824 * transaction object.
2825 */
2826 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002827 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002828 lua_rawseti(L, -2, 0);
2829
Willy Tarreau7073c472015-04-06 11:15:40 +02002830 hsmp->s = txn->s;
2831 hsmp->p = txn->p;
Willy Tarreau7073c472015-04-06 11:15:40 +02002832 hsmp->stringsafe = stringsafe;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002833
2834 /* Pop a class sesison metatable and affect it to the userdata. */
2835 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
2836 lua_setmetatable(L, -2);
2837
2838 return 1;
2839}
2840
2841/* This function is an LUA binding. It is called with each sample-fetch.
2842 * It uses closure argument to store the associated sample-fetch. It
2843 * returns only one argument or throws an error. An error is thrown
2844 * only if an error is encountered during the argument parsing. If
2845 * the "sample-fetch" function fails, nil is returned.
2846 */
2847__LJMP static int hlua_run_sample_fetch(lua_State *L)
2848{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002849 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01002850 struct sample_fetch *f;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002851 struct arg args[ARGM_NBARGS + 1];
2852 int i;
2853 struct sample smp;
2854
2855 /* Get closure arguments. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002856 f = (struct sample_fetch *)lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002857
2858 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002859 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002860
2861 /* Get extra arguments. */
2862 for (i = 0; i < lua_gettop(L) - 1; i++) {
2863 if (i >= ARGM_NBARGS)
2864 break;
2865 hlua_lua2arg(L, i + 2, &args[i]);
2866 }
2867 args[i].type = ARGT_STOP;
2868
2869 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002870 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002871
2872 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002873 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002874 lua_pushfstring(L, "error in arguments");
2875 WILL_LJMP(lua_error(L));
2876 }
2877
2878 /* Initialise the sample. */
2879 memset(&smp, 0, sizeof(smp));
2880
2881 /* Run the sample fetch process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02002882 smp.px = hsmp->p;
2883 smp.sess = hsmp->s->sess;
2884 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02002885 smp.opt = 0;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002886 if (!f->process(args, &smp, f->kw, f->private)) {
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002887 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002888 lua_pushstring(L, "");
2889 else
2890 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002891 return 1;
2892 }
2893
2894 /* Convert the returned sample in lua value. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002895 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002896 hlua_smp2lua_str(L, &smp);
2897 else
2898 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002899 return 1;
2900}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901
2902/*
2903 *
2904 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002905 * Class Converters
2906 *
2907 *
2908 */
2909
2910/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002911 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002912 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002913__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002914{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002915 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002916}
2917
2918/* This function creates and push in the stack a Converters object
2919 * according with a current TXN.
2920 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002921static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002922{
Willy Tarreau7073c472015-04-06 11:15:40 +02002923 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002924
2925 /* Check stack size. */
2926 if (!lua_checkstack(L, 3))
2927 return 0;
2928
2929 /* Create the object: obj[0] = userdata.
2930 * Note that the base of the Converters object is the
2931 * same than the TXN object.
2932 */
2933 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002934 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002935 lua_rawseti(L, -2, 0);
2936
Willy Tarreau7073c472015-04-06 11:15:40 +02002937 hsmp->s = txn->s;
2938 hsmp->p = txn->p;
Willy Tarreau7073c472015-04-06 11:15:40 +02002939 hsmp->stringsafe = stringsafe;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002940
Willy Tarreau87b09662015-04-03 00:22:06 +02002941 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002942 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
2943 lua_setmetatable(L, -2);
2944
2945 return 1;
2946}
2947
2948/* This function is an LUA binding. It is called with each converter.
2949 * It uses closure argument to store the associated converter. It
2950 * returns only one argument or throws an error. An error is thrown
2951 * only if an error is encountered during the argument parsing. If
2952 * the converter function function fails, nil is returned.
2953 */
2954__LJMP static int hlua_run_sample_conv(lua_State *L)
2955{
Willy Tarreauda5f1082015-04-06 11:17:13 +02002956 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002957 struct sample_conv *conv;
2958 struct arg args[ARGM_NBARGS + 1];
2959 int i;
2960 struct sample smp;
2961
2962 /* Get closure arguments. */
2963 conv = (struct sample_conv *)lua_touserdata(L, lua_upvalueindex(1));
2964
2965 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02002966 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002967
2968 /* Get extra arguments. */
2969 for (i = 0; i < lua_gettop(L) - 2; i++) {
2970 if (i >= ARGM_NBARGS)
2971 break;
2972 hlua_lua2arg(L, i + 3, &args[i]);
2973 }
2974 args[i].type = ARGT_STOP;
2975
2976 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02002977 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002978
2979 /* Run the special args checker. */
2980 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
2981 hlua_pusherror(L, "error in arguments");
2982 WILL_LJMP(lua_error(L));
2983 }
2984
2985 /* Initialise the sample. */
2986 if (!hlua_lua2smp(L, 2, &smp)) {
2987 hlua_pusherror(L, "error in the input argument");
2988 WILL_LJMP(lua_error(L));
2989 }
2990
2991 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002992 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002993 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002994 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002995 WILL_LJMP(lua_error(L));
2996 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002997 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
2998 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002999 hlua_pusherror(L, "error during the input argument casting");
3000 WILL_LJMP(lua_error(L));
3001 }
3002
3003 /* Run the sample conversion process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02003004 smp.px = hsmp->p;
3005 smp.sess = hsmp->s->sess;
3006 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02003007 smp.opt = 0;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003008 if (!conv->process(args, &smp, conv->private)) {
Willy Tarreauda5f1082015-04-06 11:17:13 +02003009 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003010 lua_pushstring(L, "");
3011 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003012 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003013 return 1;
3014 }
3015
3016 /* Convert the returned sample in lua value. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003017 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003018 hlua_smp2lua_str(L, &smp);
3019 else
3020 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003021 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003022}
3023
3024/*
3025 *
3026 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003027 * Class HTTP
3028 *
3029 *
3030 */
3031
3032/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003033 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003034 */
3035__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
3036{
3037 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
3038}
3039
3040/* This function creates and push in the stack a HTTP object
3041 * according with a current TXN.
3042 */
3043static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
3044{
Willy Tarreau9a8ad862015-04-06 11:14:06 +02003045 struct hlua_txn *htxn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003046
3047 /* Check stack size. */
3048 if (!lua_checkstack(L, 3))
3049 return 0;
3050
3051 /* Create the object: obj[0] = userdata.
3052 * Note that the base of the Converters object is the
3053 * same than the TXN object.
3054 */
3055 lua_newtable(L);
Willy Tarreau9a8ad862015-04-06 11:14:06 +02003056 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003057 lua_rawseti(L, -2, 0);
3058
Willy Tarreau9a8ad862015-04-06 11:14:06 +02003059 htxn->s = txn->s;
3060 htxn->p = txn->p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003061
Willy Tarreau87b09662015-04-03 00:22:06 +02003062 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003063 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
3064 lua_setmetatable(L, -2);
3065
3066 return 1;
3067}
3068
3069/* This function creates ans returns an array of HTTP headers.
3070 * This function does not fails. It is used as wrapper with the
3071 * 2 following functions.
3072 */
3073__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3074{
3075 const char *cur_ptr, *cur_next, *p;
3076 int old_idx, cur_idx;
3077 struct hdr_idx_elem *cur_hdr;
3078 const char *hn, *hv;
3079 int hnl, hvl;
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003080 int type;
3081 const char *in;
3082 char *out;
3083 int len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003084
3085 /* Create the table. */
3086 lua_newtable(L);
3087
Willy Tarreaueee5b512015-04-03 23:46:31 +02003088 if (!htxn->s->txn)
3089 return 1;
3090
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003091 /* Build array of headers. */
3092 old_idx = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003093 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003094
3095 while (1) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02003096 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003097 if (!cur_idx)
3098 break;
3099 old_idx = cur_idx;
3100
Willy Tarreaueee5b512015-04-03 23:46:31 +02003101 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003102 cur_ptr = cur_next;
3103 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
3104
3105 /* Now we have one full header at cur_ptr of len cur_hdr->len,
3106 * and the next header starts at cur_next. We'll check
3107 * this header in the list as well as against the default
3108 * rule.
3109 */
3110
3111 /* look for ': *'. */
3112 hn = cur_ptr;
3113 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
3114 if (p >= cur_ptr+cur_hdr->len)
3115 continue;
3116 hnl = p - hn;
3117 p++;
3118 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
3119 p++;
3120 if (p >= cur_ptr+cur_hdr->len)
3121 continue;
3122 hv = p;
3123 hvl = cur_ptr+cur_hdr->len-p;
3124
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003125 /* Lowercase the key. Don't check the size of trash, it have
3126 * the size of one buffer and the input data contains in one
3127 * buffer.
3128 */
3129 out = trash.str;
3130 for (in=hn; in<hn+hnl; in++, out++)
3131 *out = tolower(*in);
3132 *out = '\0';
3133
3134 /* Check for existing entry:
3135 * assume that the table is on the top of the stack, and
3136 * push the key in the stack, the function lua_gettable()
3137 * perform the lookup.
3138 */
3139 lua_pushlstring(L, trash.str, hnl);
3140 lua_gettable(L, -2);
3141 type = lua_type(L, -1);
3142
3143 switch (type) {
3144 case LUA_TNIL:
3145 /* Table not found, create it. */
3146 lua_pop(L, 1); /* remove the nil value. */
3147 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
3148 lua_newtable(L); /* create and push empty table. */
3149 lua_pushlstring(L, hv, hvl); /* push header value. */
3150 lua_rawseti(L, -2, 0); /* index header value (pop it). */
3151 lua_rawset(L, -3); /* index new table with header name (pop the values). */
3152 break;
3153
3154 case LUA_TTABLE:
3155 /* Entry found: push the value in the table. */
3156 len = lua_rawlen(L, -1);
3157 lua_pushlstring(L, hv, hvl); /* push header value. */
3158 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
3159 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
3160 break;
3161
3162 default:
3163 /* Other cases are errors. */
3164 hlua_pusherror(L, "internal error during the parsing of headers.");
3165 WILL_LJMP(lua_error(L));
3166 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003167 }
3168
3169 return 1;
3170}
3171
3172__LJMP static int hlua_http_req_get_headers(lua_State *L)
3173{
3174 struct hlua_txn *htxn;
3175
3176 MAY_LJMP(check_args(L, 1, "req_get_headers"));
3177 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3178
Willy Tarreaueee5b512015-04-03 23:46:31 +02003179 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003180}
3181
3182__LJMP static int hlua_http_res_get_headers(lua_State *L)
3183{
3184 struct hlua_txn *htxn;
3185
3186 MAY_LJMP(check_args(L, 1, "res_get_headers"));
3187 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3188
Willy Tarreaueee5b512015-04-03 23:46:31 +02003189 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003190}
3191
3192/* This function replace full header, or just a value in
3193 * the request or in the response. It is a wrapper fir the
3194 * 4 following functions.
3195 */
3196__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
3197 struct http_msg *msg, int action)
3198{
3199 size_t name_len;
3200 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3201 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
3202 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
3203 struct my_regex re;
3204
3205 if (!regex_comp(reg, &re, 1, 1, NULL))
3206 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
3207
3208 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
3209 regex_free(&re);
3210 return 0;
3211}
3212
3213__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
3214{
3215 struct hlua_txn *htxn;
3216
3217 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3218 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3219
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003220 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003221}
3222
3223__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
3224{
3225 struct hlua_txn *htxn;
3226
3227 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
3228 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3229
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003230 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003231}
3232
3233__LJMP static int hlua_http_req_rep_val(lua_State *L)
3234{
3235 struct hlua_txn *htxn;
3236
3237 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3238 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3239
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003240 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003241}
3242
3243__LJMP static int hlua_http_res_rep_val(lua_State *L)
3244{
3245 struct hlua_txn *htxn;
3246
3247 MAY_LJMP(check_args(L, 4, "res_rep_val"));
3248 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3249
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003250 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003251}
3252
3253/* This function deletes all the occurences of an header.
3254 * It is a wrapper for the 2 following functions.
3255 */
3256__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3257{
3258 size_t len;
3259 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3260 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003261 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003262
3263 ctx.idx = 0;
3264 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
3265 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3266 return 0;
3267}
3268
3269__LJMP static int hlua_http_req_del_hdr(lua_State *L)
3270{
3271 struct hlua_txn *htxn;
3272
3273 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3274 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3275
Willy Tarreaueee5b512015-04-03 23:46:31 +02003276 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003277}
3278
3279__LJMP static int hlua_http_res_del_hdr(lua_State *L)
3280{
3281 struct hlua_txn *htxn;
3282
3283 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3284 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3285
Willy Tarreaueee5b512015-04-03 23:46:31 +02003286 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003287}
3288
3289/* This function adds an header. It is a wrapper used by
3290 * the 2 following functions.
3291 */
3292__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3293{
3294 size_t name_len;
3295 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3296 size_t value_len;
3297 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
3298 char *p;
3299
3300 /* Check length. */
3301 trash.len = value_len + name_len + 2;
3302 if (trash.len > trash.size)
3303 return 0;
3304
3305 /* Creates the header string. */
3306 p = trash.str;
3307 memcpy(p, name, name_len);
3308 p += name_len;
3309 *p = ':';
3310 p++;
3311 *p = ' ';
3312 p++;
3313 memcpy(p, value, value_len);
3314
Willy Tarreaueee5b512015-04-03 23:46:31 +02003315 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003316 trash.str, trash.len) != 0);
3317
3318 return 0;
3319}
3320
3321__LJMP static int hlua_http_req_add_hdr(lua_State *L)
3322{
3323 struct hlua_txn *htxn;
3324
3325 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
3326 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3327
Willy Tarreaueee5b512015-04-03 23:46:31 +02003328 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003329}
3330
3331__LJMP static int hlua_http_res_add_hdr(lua_State *L)
3332{
3333 struct hlua_txn *htxn;
3334
3335 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
3336 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3337
Willy Tarreaueee5b512015-04-03 23:46:31 +02003338 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003339}
3340
3341static int hlua_http_req_set_hdr(lua_State *L)
3342{
3343 struct hlua_txn *htxn;
3344
3345 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
3346 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3347
Willy Tarreaueee5b512015-04-03 23:46:31 +02003348 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
3349 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003350}
3351
3352static int hlua_http_res_set_hdr(lua_State *L)
3353{
3354 struct hlua_txn *htxn;
3355
3356 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
3357 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3358
Willy Tarreaueee5b512015-04-03 23:46:31 +02003359 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
3360 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003361}
3362
3363/* This function set the method. */
3364static int hlua_http_req_set_meth(lua_State *L)
3365{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003366 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003367 size_t name_len;
3368 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003369
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003370 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003371 return 1;
3372}
3373
3374/* This function set the method. */
3375static int hlua_http_req_set_path(lua_State *L)
3376{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003377 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003378 size_t name_len;
3379 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003380 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003381 return 1;
3382}
3383
3384/* This function set the query-string. */
3385static int hlua_http_req_set_query(lua_State *L)
3386{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003387 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003388 size_t name_len;
3389 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003390
3391 /* Check length. */
3392 if (name_len > trash.size - 1) {
3393 lua_pushboolean(L, 0);
3394 return 1;
3395 }
3396
3397 /* Add the mark question as prefix. */
3398 chunk_reset(&trash);
3399 trash.str[trash.len++] = '?';
3400 memcpy(trash.str + trash.len, name, name_len);
3401 trash.len += name_len;
3402
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003403 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003404 return 1;
3405}
3406
3407/* This function set the uri. */
3408static int hlua_http_req_set_uri(lua_State *L)
3409{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003410 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003411 size_t name_len;
3412 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003413
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003414 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003415 return 1;
3416}
3417
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02003418/* This function set the response code. */
3419static int hlua_http_res_set_status(lua_State *L)
3420{
3421 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3422 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
3423
3424 http_set_status(code, htxn->s);
3425 return 0;
3426}
3427
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003428/*
3429 *
3430 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003431 * Class TXN
3432 *
3433 *
3434 */
3435
3436/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003437 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003438 */
3439__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
3440{
3441 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
3442}
3443
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02003444__LJMP static int hlua_set_var(lua_State *L)
3445{
3446 struct hlua_txn *htxn;
3447 const char *name;
3448 size_t len;
3449 struct sample smp;
3450
3451 MAY_LJMP(check_args(L, 3, "set_var"));
3452
3453 /* It is useles to retrieve the stream, but this function
3454 * runs only in a stream context.
3455 */
3456 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3457 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3458
3459 /* Converts the third argument in a sample. */
3460 hlua_lua2smp(L, 3, &smp);
3461
3462 /* Store the sample in a variable. */
3463 vars_set_by_name(name, len, htxn->s, &smp);
3464 return 0;
3465}
3466
3467__LJMP static int hlua_get_var(lua_State *L)
3468{
3469 struct hlua_txn *htxn;
3470 const char *name;
3471 size_t len;
3472 struct sample smp;
3473
3474 MAY_LJMP(check_args(L, 2, "get_var"));
3475
3476 /* It is useles to retrieve the stream, but this function
3477 * runs only in a stream context.
3478 */
3479 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3480 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3481
3482 if (!vars_get_by_name(name, len, htxn->s, &smp)) {
3483 lua_pushnil(L);
3484 return 1;
3485 }
3486
3487 return hlua_smp2lua(L, &smp);
3488}
3489
Willy Tarreau59551662015-03-10 14:23:13 +01003490__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003491{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003492 struct hlua *hlua;
3493
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003494 MAY_LJMP(check_args(L, 2, "set_priv"));
3495
Willy Tarreau87b09662015-04-03 00:22:06 +02003496 /* It is useles to retrieve the stream, but this function
3497 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003498 */
3499 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003500 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003501
3502 /* Remove previous value. */
3503 if (hlua->Mref != -1)
3504 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
3505
3506 /* Get and store new value. */
3507 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3508 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3509
3510 return 0;
3511}
3512
Willy Tarreau59551662015-03-10 14:23:13 +01003513__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003514{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003515 struct hlua *hlua;
3516
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003517 MAY_LJMP(check_args(L, 1, "get_priv"));
3518
Willy Tarreau87b09662015-04-03 00:22:06 +02003519 /* It is useles to retrieve the stream, but this function
3520 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003521 */
3522 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003523 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003524
3525 /* Push configuration index in the stack. */
3526 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3527
3528 return 1;
3529}
3530
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003531/* Create stack entry containing a class TXN. This function
3532 * return 0 if the stack does not contains free slots,
3533 * otherwise it returns 1.
3534 */
Willy Tarreau15e91e12015-04-04 00:52:09 +02003535static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003536{
Willy Tarreaude491382015-04-06 11:04:28 +02003537 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003538
3539 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003540 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003541 return 0;
3542
3543 /* NOTE: The allocation never fails. The failure
3544 * throw an error, and the function never returns.
3545 * if the throw is not avalaible, the process is aborted.
3546 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003547 /* Create the object: obj[0] = userdata. */
3548 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02003549 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003550 lua_rawseti(L, -2, 0);
3551
Willy Tarreaude491382015-04-06 11:04:28 +02003552 htxn->s = s;
3553 htxn->p = p;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003554
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003555 /* Create the "f" field that contains a list of fetches. */
3556 lua_pushstring(L, "f");
Willy Tarreaude491382015-04-06 11:04:28 +02003557 if (!hlua_fetches_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003558 return 0;
3559 lua_settable(L, -3);
3560
3561 /* Create the "sf" field that contains a list of stringsafe fetches. */
3562 lua_pushstring(L, "sf");
Willy Tarreaude491382015-04-06 11:04:28 +02003563 if (!hlua_fetches_new(L, htxn, 1))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003564 return 0;
3565 lua_settable(L, -3);
3566
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003567 /* Create the "c" field that contains a list of converters. */
3568 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02003569 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003570 return 0;
3571 lua_settable(L, -3);
3572
3573 /* Create the "sc" field that contains a list of stringsafe converters. */
3574 lua_pushstring(L, "sc");
Willy Tarreaude491382015-04-06 11:04:28 +02003575 if (!hlua_converters_new(L, htxn, 1))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003576 return 0;
3577 lua_settable(L, -3);
3578
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003579 /* Create the "req" field that contains the request channel object. */
3580 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003581 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003582 return 0;
3583 lua_settable(L, -3);
3584
3585 /* Create the "res" field that contains the response channel object. */
3586 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003587 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003588 return 0;
3589 lua_settable(L, -3);
3590
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003591 /* Creates the HTTP object is the current proxy allows http. */
3592 lua_pushstring(L, "http");
3593 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02003594 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003595 return 0;
3596 }
3597 else
3598 lua_pushnil(L);
3599 lua_settable(L, -3);
3600
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003601 /* Pop a class sesison metatable and affect it to the userdata. */
3602 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
3603 lua_setmetatable(L, -2);
3604
3605 return 1;
3606}
3607
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003608__LJMP static int hlua_txn_deflog(lua_State *L)
3609{
3610 const char *msg;
3611 struct hlua_txn *htxn;
3612
3613 MAY_LJMP(check_args(L, 2, "deflog"));
3614 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3615 msg = MAY_LJMP(luaL_checkstring(L, 2));
3616
3617 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
3618 return 0;
3619}
3620
3621__LJMP static int hlua_txn_log(lua_State *L)
3622{
3623 int level;
3624 const char *msg;
3625 struct hlua_txn *htxn;
3626
3627 MAY_LJMP(check_args(L, 3, "log"));
3628 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3629 level = MAY_LJMP(luaL_checkinteger(L, 2));
3630 msg = MAY_LJMP(luaL_checkstring(L, 3));
3631
3632 if (level < 0 || level >= NB_LOG_LEVELS)
3633 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3634
3635 hlua_sendlog(htxn->s->be, level, msg);
3636 return 0;
3637}
3638
3639__LJMP static int hlua_txn_log_debug(lua_State *L)
3640{
3641 const char *msg;
3642 struct hlua_txn *htxn;
3643
3644 MAY_LJMP(check_args(L, 2, "Debug"));
3645 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3646 msg = MAY_LJMP(luaL_checkstring(L, 2));
3647 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
3648 return 0;
3649}
3650
3651__LJMP static int hlua_txn_log_info(lua_State *L)
3652{
3653 const char *msg;
3654 struct hlua_txn *htxn;
3655
3656 MAY_LJMP(check_args(L, 2, "Info"));
3657 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3658 msg = MAY_LJMP(luaL_checkstring(L, 2));
3659 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
3660 return 0;
3661}
3662
3663__LJMP static int hlua_txn_log_warning(lua_State *L)
3664{
3665 const char *msg;
3666 struct hlua_txn *htxn;
3667
3668 MAY_LJMP(check_args(L, 2, "Warning"));
3669 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3670 msg = MAY_LJMP(luaL_checkstring(L, 2));
3671 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
3672 return 0;
3673}
3674
3675__LJMP static int hlua_txn_log_alert(lua_State *L)
3676{
3677 const char *msg;
3678 struct hlua_txn *htxn;
3679
3680 MAY_LJMP(check_args(L, 2, "Alert"));
3681 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3682 msg = MAY_LJMP(luaL_checkstring(L, 2));
3683 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
3684 return 0;
3685}
3686
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003687__LJMP static int hlua_txn_set_loglevel(lua_State *L)
3688{
3689 struct hlua_txn *htxn;
3690 int ll;
3691
3692 MAY_LJMP(check_args(L, 2, "set_loglevel"));
3693 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3694 ll = MAY_LJMP(luaL_checkinteger(L, 2));
3695
3696 if (ll < 0 || ll > 7)
3697 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
3698
3699 htxn->s->logs.level = ll;
3700 return 0;
3701}
3702
3703__LJMP static int hlua_txn_set_tos(lua_State *L)
3704{
3705 struct hlua_txn *htxn;
3706 struct connection *cli_conn;
3707 int tos;
3708
3709 MAY_LJMP(check_args(L, 2, "set_tos"));
3710 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3711 tos = MAY_LJMP(luaL_checkinteger(L, 2));
3712
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003713 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003714 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, tos);
3715
3716 return 0;
3717}
3718
3719__LJMP static int hlua_txn_set_mark(lua_State *L)
3720{
3721#ifdef SO_MARK
3722 struct hlua_txn *htxn;
3723 struct connection *cli_conn;
3724 int mark;
3725
3726 MAY_LJMP(check_args(L, 2, "set_mark"));
3727 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3728 mark = MAY_LJMP(luaL_checkinteger(L, 2));
3729
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003730 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau07081fe2015-04-06 10:59:20 +02003731 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003732#endif
3733 return 0;
3734}
3735
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003736/* This function is an Lua binding that send pending data
3737 * to the client, and close the stream interface.
3738 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02003739__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003740{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003741 struct hlua_txn *htxn;
Willy Tarreau81389672015-03-10 12:03:52 +01003742 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003743
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003744 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003745 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003746
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003747 ic = &htxn->s->req;
3748 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01003749
Willy Tarreau630ef452015-08-28 10:06:15 +02003750 if (htxn->s->txn) {
3751 /* HTTP mode, let's stay in sync with the stream */
3752 bi_fast_delete(ic->buf, htxn->s->txn->req.sov);
3753 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
3754 htxn->s->txn->req.sov = 0;
3755 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
3756 oc->analysers = AN_RES_HTTP_XFER_BODY;
3757 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
3758 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
3759
3760 /* Trim any possible response */
3761 oc->buf->i = 0;
3762 htxn->s->txn->rsp.next = htxn->s->txn->rsp.sov = 0;
3763
3764 /* Note that if we want to support keep-alive, we need
3765 * to bypass the close/shutr_now calls below, but that
3766 * may only be done if the HTTP request was already
3767 * processed and the connection header is known (ie
3768 * not during TCP rules).
3769 */
3770 }
3771
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02003772 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01003773 channel_abort(ic);
3774 channel_auto_close(ic);
3775 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02003776
3777 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01003778 channel_auto_read(oc);
3779 channel_auto_close(oc);
3780 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003781
Willy Tarreau0458b082015-08-28 09:40:04 +02003782 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02003783
3784 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003785 return 0;
3786}
3787
3788__LJMP static int hlua_log(lua_State *L)
3789{
3790 int level;
3791 const char *msg;
3792
3793 MAY_LJMP(check_args(L, 2, "log"));
3794 level = MAY_LJMP(luaL_checkinteger(L, 1));
3795 msg = MAY_LJMP(luaL_checkstring(L, 2));
3796
3797 if (level < 0 || level >= NB_LOG_LEVELS)
3798 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3799
3800 hlua_sendlog(NULL, level, msg);
3801 return 0;
3802}
3803
3804__LJMP static int hlua_log_debug(lua_State *L)
3805{
3806 const char *msg;
3807
3808 MAY_LJMP(check_args(L, 1, "debug"));
3809 msg = MAY_LJMP(luaL_checkstring(L, 1));
3810 hlua_sendlog(NULL, LOG_DEBUG, msg);
3811 return 0;
3812}
3813
3814__LJMP static int hlua_log_info(lua_State *L)
3815{
3816 const char *msg;
3817
3818 MAY_LJMP(check_args(L, 1, "info"));
3819 msg = MAY_LJMP(luaL_checkstring(L, 1));
3820 hlua_sendlog(NULL, LOG_INFO, msg);
3821 return 0;
3822}
3823
3824__LJMP static int hlua_log_warning(lua_State *L)
3825{
3826 const char *msg;
3827
3828 MAY_LJMP(check_args(L, 1, "warning"));
3829 msg = MAY_LJMP(luaL_checkstring(L, 1));
3830 hlua_sendlog(NULL, LOG_WARNING, msg);
3831 return 0;
3832}
3833
3834__LJMP static int hlua_log_alert(lua_State *L)
3835{
3836 const char *msg;
3837
3838 MAY_LJMP(check_args(L, 1, "alert"));
3839 msg = MAY_LJMP(luaL_checkstring(L, 1));
3840 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003841 return 0;
3842}
3843
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003844__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003845{
3846 int wakeup_ms = lua_tointeger(L, -1);
3847 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003848 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003849 return 0;
3850}
3851
3852__LJMP static int hlua_sleep(lua_State *L)
3853{
3854 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003855 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003856
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003857 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003858
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003859 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003860 wakeup_ms = tick_add(now_ms, delay);
3861 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003862
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003863 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3864 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003865}
3866
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003867__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003868{
3869 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003870 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003871
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003872 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003873
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003874 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003875 wakeup_ms = tick_add(now_ms, delay);
3876 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003877
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003878 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3879 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003880}
3881
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003882/* This functionis an LUA binding. it permits to give back
3883 * the hand at the HAProxy scheduler. It is used when the
3884 * LUA processing consumes a lot of time.
3885 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003886__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003887{
3888 return 0;
3889}
3890
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003891__LJMP static int hlua_yield(lua_State *L)
3892{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003893 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
3894 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003895}
3896
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003897/* This function change the nice of the currently executed
3898 * task. It is used set low or high priority at the current
3899 * task.
3900 */
Willy Tarreau59551662015-03-10 14:23:13 +01003901__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003902{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003903 struct hlua *hlua;
3904 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003905
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003906 MAY_LJMP(check_args(L, 1, "set_nice"));
3907 hlua = hlua_gethlua(L);
3908 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003909
3910 /* If he task is not set, I'm in a start mode. */
3911 if (!hlua || !hlua->task)
3912 return 0;
3913
3914 if (nice < -1024)
3915 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003916 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003917 nice = 1024;
3918
3919 hlua->task->nice = nice;
3920 return 0;
3921}
3922
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003923/* This function is used as a calback of a task. It is called by the
3924 * HAProxy task subsystem when the task is awaked. The LUA runtime can
3925 * return an E_AGAIN signal, the emmiter of this signal must set a
3926 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02003927 *
3928 * Task wrapper are longjmp safe because the only one Lua code
3929 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003930 */
3931static struct task *hlua_process_task(struct task *task)
3932{
3933 struct hlua *hlua = task->context;
3934 enum hlua_exec status;
3935
3936 /* We need to remove the task from the wait queue before executing
3937 * the Lua code because we don't know if it needs to wait for
3938 * another timer or not in the case of E_AGAIN.
3939 */
3940 task_delete(task);
3941
Thierry FOURNIERbd413492015-03-03 16:52:26 +01003942 /* If it is the first call to the task, we must initialize the
3943 * execution timeouts.
3944 */
3945 if (!HLUA_IS_RUNNING(hlua))
Camilo Lopez685c0142015-08-02 19:07:28 -04003946 hlua->expire = tick_add_ifset(now_ms, hlua_timeout_task);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01003947
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003948 /* Execute the Lua code. */
3949 status = hlua_ctx_resume(hlua, 1);
3950
3951 switch (status) {
3952 /* finished or yield */
3953 case HLUA_E_OK:
3954 hlua_ctx_destroy(hlua);
3955 task_delete(task);
3956 task_free(task);
3957 break;
3958
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01003959 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
3960 if (hlua->wake_time != TICK_ETERNITY)
3961 task_schedule(task, hlua->wake_time);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003962 break;
3963
3964 /* finished with error. */
3965 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02003966 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003967 hlua_ctx_destroy(hlua);
3968 task_delete(task);
3969 task_free(task);
3970 break;
3971
3972 case HLUA_E_ERR:
3973 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02003974 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003975 hlua_ctx_destroy(hlua);
3976 task_delete(task);
3977 task_free(task);
3978 break;
3979 }
3980 return NULL;
3981}
3982
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01003983/* This function is an LUA binding that register LUA function to be
3984 * executed after the HAProxy configuration parsing and before the
3985 * HAProxy scheduler starts. This function expect only one LUA
3986 * argument that is a function. This function returns nothing, but
3987 * throws if an error is encountered.
3988 */
3989__LJMP static int hlua_register_init(lua_State *L)
3990{
3991 struct hlua_init_function *init;
3992 int ref;
3993
3994 MAY_LJMP(check_args(L, 1, "register_init"));
3995
3996 ref = MAY_LJMP(hlua_checkfunction(L, 1));
3997
3998 init = malloc(sizeof(*init));
3999 if (!init)
4000 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4001
4002 init->function_ref = ref;
4003 LIST_ADDQ(&hlua_init_functions, &init->l);
4004 return 0;
4005}
4006
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004007/* This functio is an LUA binding. It permits to register a task
4008 * executed in parallel of the main HAroxy activity. The task is
4009 * created and it is set in the HAProxy scheduler. It can be called
4010 * from the "init" section, "post init" or during the runtime.
4011 *
4012 * Lua prototype:
4013 *
4014 * <none> core.register_task(<function>)
4015 */
4016static int hlua_register_task(lua_State *L)
4017{
4018 struct hlua *hlua;
4019 struct task *task;
4020 int ref;
4021
4022 MAY_LJMP(check_args(L, 1, "register_task"));
4023
4024 ref = MAY_LJMP(hlua_checkfunction(L, 1));
4025
4026 hlua = malloc(sizeof(*hlua));
4027 if (!hlua)
4028 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4029
4030 task = task_new();
4031 task->context = hlua;
4032 task->process = hlua_process_task;
4033
4034 if (!hlua_ctx_init(hlua, task))
4035 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4036
4037 /* Restore the function in the stack. */
4038 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
4039 hlua->nargs = 0;
4040
4041 /* Schedule task. */
4042 task_schedule(task, now_ms);
4043
4044 return 0;
4045}
4046
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004047/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
4048 * doesn't allow "yield" functions because the HAProxy engine cannot
4049 * resume converters.
4050 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004051static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004052{
4053 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004054 struct stream *stream = smp->strm;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004055
Willy Tarreau87b09662015-04-03 00:22:06 +02004056 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004057 * Lua context can be not initialized. This behavior
4058 * permits to save performances because a systematic
4059 * Lua initialization cause 5% performances loss.
4060 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004061 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004062 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004063 return 0;
4064 }
4065
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004066 /* If it is the first run, initialize the data for the call. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004067 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004068
4069 /* The following Lua calls can fail. */
4070 if (!SET_SAFE_LJMP(stream->hlua.T)) {
4071 SEND_ERR(stream->be, "Lua converter '%s': critical error.\n", fcn->name);
4072 return 0;
4073 }
4074
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004075 /* Check stack available size. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004076 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004077 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004078 return 0;
4079 }
4080
4081 /* Restore the function in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004082 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004083
4084 /* convert input sample and pust-it in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004085 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004086 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004087 return 0;
4088 }
Willy Tarreau87b09662015-04-03 00:22:06 +02004089 hlua_smp2lua(stream->hlua.T, smp);
4090 stream->hlua.nargs = 2;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004091
4092 /* push keywords in the stack. */
4093 if (arg_p) {
4094 for (; arg_p->type != ARGT_STOP; arg_p++) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004095 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004096 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004097 return 0;
4098 }
Willy Tarreau87b09662015-04-03 00:22:06 +02004099 hlua_arg2lua(stream->hlua.T, arg_p);
4100 stream->hlua.nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004101 }
4102 }
4103
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004104 /* We must initialize the execution timeouts. */
Thierry FOURNIER61e96c62015-08-09 13:10:24 +02004105 stream->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004106
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004107 /* At this point the execution is safe. */
4108 RESET_SAFE_LJMP(stream->hlua.T);
4109
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004110 /* Set the currently running flag. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004111 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004112 }
4113
4114 /* Execute the function. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004115 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004116 /* finished. */
4117 case HLUA_E_OK:
4118 /* Convert the returned value in sample. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004119 hlua_lua2smp(stream->hlua.T, -1, smp);
4120 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004121 return 1;
4122
4123 /* yield. */
4124 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004125 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004126 return 0;
4127
4128 /* finished with error. */
4129 case HLUA_E_ERRMSG:
4130 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004131 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
4132 fcn->name, lua_tostring(stream->hlua.T, -1));
Willy Tarreau87b09662015-04-03 00:22:06 +02004133 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004134 return 0;
4135
4136 case HLUA_E_ERR:
4137 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004138 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004139
4140 default:
4141 return 0;
4142 }
4143}
4144
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004145/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
4146 * doesn't allow "yield" functions because the HAProxy engine cannot
4147 * resume sample-fetches.
4148 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004149static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
4150 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004151{
4152 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004153 struct stream *stream = smp->strm;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004154
Willy Tarreau87b09662015-04-03 00:22:06 +02004155 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004156 * Lua context can be not initialized. This behavior
4157 * permits to save performances because a systematic
4158 * Lua initialization cause 5% performances loss.
4159 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004160 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004161 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004162 return 0;
4163 }
4164
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004165 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004166 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004167
4168 /* The following Lua calls can fail. */
4169 if (!SET_SAFE_LJMP(stream->hlua.T)) {
4170 SEND_ERR(smp->px, "Lua sample-fetch '%s': critical error.\n", fcn->name);
4171 return 0;
4172 }
4173
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004174 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004175 if (!lua_checkstack(stream->hlua.T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004176 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004177 return 0;
4178 }
4179
4180 /* Restore the function in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004181 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004182
4183 /* push arguments in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004184 if (!hlua_txn_new(stream->hlua.T, stream, smp->px)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004185 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004186 return 0;
4187 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004188 stream->hlua.nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004189
4190 /* push keywords in the stack. */
4191 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
4192 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004193 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004194 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004195 return 0;
4196 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004197 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004198 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004199 return 0;
4200 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004201 hlua_arg2lua(stream->hlua.T, arg_p);
4202 stream->hlua.nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004203 }
4204
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004205 /* We must initialize the execution timeouts. */
Thierry FOURNIER61e96c62015-08-09 13:10:24 +02004206 stream->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004207
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004208 /* At this point the execution is safe. */
4209 RESET_SAFE_LJMP(stream->hlua.T);
4210
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004211 /* Set the currently running flag. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004212 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004213 }
4214
4215 /* Execute the function. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004216 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004217 /* finished. */
4218 case HLUA_E_OK:
4219 /* Convert the returned value in sample. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004220 hlua_lua2smp(stream->hlua.T, -1, smp);
4221 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004222
4223 /* Set the end of execution flag. */
4224 smp->flags &= ~SMP_F_MAY_CHANGE;
4225 return 1;
4226
4227 /* yield. */
4228 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004229 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004230 return 0;
4231
4232 /* finished with error. */
4233 case HLUA_E_ERRMSG:
4234 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004235 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
4236 fcn->name, lua_tostring(stream->hlua.T, -1));
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004237 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004238 return 0;
4239
4240 case HLUA_E_ERR:
4241 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004242 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004243
4244 default:
4245 return 0;
4246 }
4247}
4248
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004249/* This function is an LUA binding used for registering
4250 * "sample-conv" functions. It expects a converter name used
4251 * in the haproxy configuration file, and an LUA function.
4252 */
4253__LJMP static int hlua_register_converters(lua_State *L)
4254{
4255 struct sample_conv_kw_list *sck;
4256 const char *name;
4257 int ref;
4258 int len;
4259 struct hlua_function *fcn;
4260
4261 MAY_LJMP(check_args(L, 2, "register_converters"));
4262
4263 /* First argument : converter name. */
4264 name = MAY_LJMP(luaL_checkstring(L, 1));
4265
4266 /* Second argument : lua function. */
4267 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4268
4269 /* Allocate and fill the sample fetch keyword struct. */
Willy Tarreau07081fe2015-04-06 10:59:20 +02004270 sck = malloc(sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004271 if (!sck)
4272 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4273 fcn = malloc(sizeof(*fcn));
4274 if (!fcn)
4275 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4276
4277 /* Fill fcn. */
4278 fcn->name = strdup(name);
4279 if (!fcn->name)
4280 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4281 fcn->function_ref = ref;
4282
4283 /* List head */
4284 sck->list.n = sck->list.p = NULL;
4285
4286 /* converter keyword. */
4287 len = strlen("lua.") + strlen(name) + 1;
4288 sck->kw[0].kw = malloc(len);
4289 if (!sck->kw[0].kw)
4290 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4291
4292 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
4293 sck->kw[0].process = hlua_sample_conv_wrapper;
4294 sck->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4295 sck->kw[0].val_args = NULL;
4296 sck->kw[0].in_type = SMP_T_STR;
4297 sck->kw[0].out_type = SMP_T_STR;
4298 sck->kw[0].private = fcn;
4299
4300 /* End of array. */
4301 memset(&sck->kw[1], 0, sizeof(struct sample_conv));
4302
4303 /* Register this new converter */
4304 sample_register_convs(sck);
4305
4306 return 0;
4307}
4308
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004309/* This fucntion is an LUA binding used for registering
4310 * "sample-fetch" functions. It expects a converter name used
4311 * in the haproxy configuration file, and an LUA function.
4312 */
4313__LJMP static int hlua_register_fetches(lua_State *L)
4314{
4315 const char *name;
4316 int ref;
4317 int len;
4318 struct sample_fetch_kw_list *sfk;
4319 struct hlua_function *fcn;
4320
4321 MAY_LJMP(check_args(L, 2, "register_fetches"));
4322
4323 /* First argument : sample-fetch name. */
4324 name = MAY_LJMP(luaL_checkstring(L, 1));
4325
4326 /* Second argument : lua function. */
4327 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4328
4329 /* Allocate and fill the sample fetch keyword struct. */
Willy Tarreau07081fe2015-04-06 10:59:20 +02004330 sfk = malloc(sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004331 if (!sfk)
4332 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4333 fcn = malloc(sizeof(*fcn));
4334 if (!fcn)
4335 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4336
4337 /* Fill fcn. */
4338 fcn->name = strdup(name);
4339 if (!fcn->name)
4340 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4341 fcn->function_ref = ref;
4342
4343 /* List head */
4344 sfk->list.n = sfk->list.p = NULL;
4345
4346 /* sample-fetch keyword. */
4347 len = strlen("lua.") + strlen(name) + 1;
4348 sfk->kw[0].kw = malloc(len);
4349 if (!sfk->kw[0].kw)
4350 return luaL_error(L, "lua out of memory error.");
4351
4352 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
4353 sfk->kw[0].process = hlua_sample_fetch_wrapper;
4354 sfk->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4355 sfk->kw[0].val_args = NULL;
4356 sfk->kw[0].out_type = SMP_T_STR;
4357 sfk->kw[0].use = SMP_USE_HTTP_ANY;
4358 sfk->kw[0].val = 0;
4359 sfk->kw[0].private = fcn;
4360
4361 /* End of array. */
4362 memset(&sfk->kw[1], 0, sizeof(struct sample_fetch));
4363
4364 /* Register this new fetch. */
4365 sample_register_fetches(sfk);
4366
4367 return 0;
4368}
4369
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004370/* This function is a wrapper to execute each LUA function declared
4371 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004372 * return ACT_RET_CONT if the processing is finished (with or without
4373 * error) and return ACT_RET_YIELD if the function must be called again
4374 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004375 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004376static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
4377 struct session *sess, struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004378{
4379 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004380 unsigned int analyzer;
4381
4382 switch (rule->from) {
4383 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; break;
4384 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; break;
4385 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; break;
4386 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; break;
4387 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004388 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004389 return ACT_RET_CONT;
4390 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004391
Willy Tarreau87b09662015-04-03 00:22:06 +02004392 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004393 * Lua context can be not initialized. This behavior
4394 * permits to save performances because a systematic
4395 * Lua initialization cause 5% performances loss.
4396 */
4397 if (!s->hlua.T && !hlua_ctx_init(&s->hlua, s->task)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004398 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004399 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004400 return ACT_RET_CONT;
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004401 }
4402
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004403 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004404 if (!HLUA_IS_RUNNING(&s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004405
4406 /* The following Lua calls can fail. */
4407 if (!SET_SAFE_LJMP(s->hlua.T)) {
4408 SEND_ERR(px, "Lua function '%s': critical error.\n",
4409 rule->arg.hlua_rule->fcn.name);
4410 return ACT_RET_CONT;
4411 }
4412
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004413 /* Check stack available size. */
4414 if (!lua_checkstack(s->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004415 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004416 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004417 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004418 }
4419
4420 /* Restore the function in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004421 lua_rawgeti(s->hlua.T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004422
Willy Tarreau87b09662015-04-03 00:22:06 +02004423 /* Create and and push object stream in the stack. */
Willy Tarreau15e91e12015-04-04 00:52:09 +02004424 if (!hlua_txn_new(s->hlua.T, s, px)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004425 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004426 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004427 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004428 }
4429 s->hlua.nargs = 1;
4430
4431 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004432 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004433 if (!lua_checkstack(s->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004434 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004435 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004436 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004437 }
4438 lua_pushstring(s->hlua.T, *arg);
4439 s->hlua.nargs++;
4440 }
4441
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004442 /* Now the execution is safe. */
4443 RESET_SAFE_LJMP(s->hlua.T);
4444
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004445 /* We must initialize the execution timeouts. */
Thierry FOURNIER61e96c62015-08-09 13:10:24 +02004446 s->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004447
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004448 /* Set the currently running flag. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004449 HLUA_SET_RUN(&s->hlua);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004450 }
4451
4452 /* Execute the function. */
4453 switch (hlua_ctx_resume(&s->hlua, 1)) {
4454 /* finished. */
4455 case HLUA_E_OK:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004456 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004457
4458 /* yield. */
4459 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004460 /* Set timeout in the required channel. */
4461 if (s->hlua.wake_time != TICK_ETERNITY) {
4462 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004463 s->req.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004464 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004465 s->res.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004466 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004467 /* Some actions can be wake up when a "write" event
4468 * is detected on a response channel. This is useful
4469 * only for actions targetted on the requests.
4470 */
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01004471 if (HLUA_IS_WAKERESWR(&s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004472 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01004473 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004474 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004475 }
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01004476 if (HLUA_IS_WAKEREQWR(&s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004477 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004478 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004479
4480 /* finished with error. */
4481 case HLUA_E_ERRMSG:
4482 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004483 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004484 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua.T, -1));
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004485 lua_pop(s->hlua.T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004486 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004487
4488 case HLUA_E_ERR:
4489 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004490 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004491 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004492
4493 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004494 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004495 }
4496}
4497
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004498/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
4499 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004500 *
4501 * This function can fail with an abort() due to an Lua critical error.
4502 * We are in the configuration parsing process of HAProxy, this abort() is
4503 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004504 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004505static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4506 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004507{
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004508 /* Memory for the rule. */
4509 rule->arg.hlua_rule = malloc(sizeof(*rule->arg.hlua_rule));
4510 if (!rule->arg.hlua_rule) {
4511 memprintf(err, "out of memory error");
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 /* The requiered arg is a function name. */
4516 if (!args[*cur_arg]) {
4517 memprintf(err, "expect Lua function name");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02004518 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004519 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004520
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004521 /* Lookup for the symbol, and check if it is a function. */
4522 lua_getglobal(gL.T, args[*cur_arg]);
4523 if (lua_isnil(gL.T, -1)) {
4524 lua_pop(gL.T, 1);
4525 memprintf(err, "Lua function '%s' not found", args[*cur_arg]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +02004526 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004527 }
4528 if (!lua_isfunction(gL.T, -1)) {
4529 lua_pop(gL.T, 1);
4530 memprintf(err, "'%s' is not a function", args[*cur_arg]);
4531 return ACT_RET_PRS_ERR;
4532 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004533
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004534 /* Reference the Lua function and store the reference. */
4535 rule->arg.hlua_rule->fcn.function_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
4536 rule->arg.hlua_rule->fcn.name = strdup(args[*cur_arg]);
4537 if (!rule->arg.hlua_rule->fcn.name) {
4538 memprintf(err, "out of memory error.");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02004539 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004540 }
4541 (*cur_arg)++;
4542
4543 /* TODO: later accept arguments. */
4544 rule->arg.hlua_rule->args = NULL;
4545
Thierry FOURNIER42148732015-09-02 17:17:33 +02004546 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004547 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02004548 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004549}
4550
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004551static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
4552 struct proxy *defpx, const char *file, int line,
4553 char **err, unsigned int *timeout)
4554{
4555 const char *error;
4556
4557 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
4558 if (error && *error != '\0') {
4559 memprintf(err, "%s: invalid timeout", args[0]);
4560 return -1;
4561 }
4562 return 0;
4563}
4564
4565static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
4566 struct proxy *defpx, const char *file, int line,
4567 char **err)
4568{
4569 return hlua_read_timeout(args, section_type, curpx, defpx,
4570 file, line, err, &hlua_timeout_session);
4571}
4572
4573static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
4574 struct proxy *defpx, const char *file, int line,
4575 char **err)
4576{
4577 return hlua_read_timeout(args, section_type, curpx, defpx,
4578 file, line, err, &hlua_timeout_task);
4579}
4580
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004581static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
4582 struct proxy *defpx, const char *file, int line,
4583 char **err)
4584{
4585 char *error;
4586
4587 hlua_nb_instruction = strtoll(args[1], &error, 10);
4588 if (*error != '\0') {
4589 memprintf(err, "%s: invalid number", args[0]);
4590 return -1;
4591 }
4592 return 0;
4593}
4594
Willy Tarreau32f61e22015-03-18 17:54:59 +01004595static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
4596 struct proxy *defpx, const char *file, int line,
4597 char **err)
4598{
4599 char *error;
4600
4601 if (*(args[1]) == 0) {
4602 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
4603 return -1;
4604 }
4605 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
4606 if (*error != '\0') {
4607 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
4608 return -1;
4609 }
4610 return 0;
4611}
4612
4613
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004614/* This function is called by the main configuration key "lua-load". It loads and
4615 * execute an lua file during the parsing of the HAProxy configuration file. It is
4616 * the main lua entry point.
4617 *
4618 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
4619 * occured, otherwise it returns 0.
4620 *
4621 * In some error case, LUA set an error message in top of the stack. This function
4622 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004623 *
4624 * This function can fail with an abort() due to an Lua critical error.
4625 * We are in the configuration parsing process of HAProxy, this abort() is
4626 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004627 */
4628static int hlua_load(char **args, int section_type, struct proxy *curpx,
4629 struct proxy *defpx, const char *file, int line,
4630 char **err)
4631{
4632 int error;
4633
4634 /* Just load and compile the file. */
4635 error = luaL_loadfile(gL.T, args[1]);
4636 if (error) {
4637 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
4638 lua_pop(gL.T, 1);
4639 return -1;
4640 }
4641
4642 /* If no syntax error where detected, execute the code. */
4643 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
4644 switch (error) {
4645 case LUA_OK:
4646 break;
4647 case LUA_ERRRUN:
4648 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
4649 lua_pop(gL.T, 1);
4650 return -1;
4651 case LUA_ERRMEM:
4652 memprintf(err, "lua out of memory error\n");
4653 return -1;
4654 case LUA_ERRERR:
4655 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
4656 lua_pop(gL.T, 1);
4657 return -1;
4658 case LUA_ERRGCMM:
4659 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
4660 lua_pop(gL.T, 1);
4661 return -1;
4662 default:
4663 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
4664 lua_pop(gL.T, 1);
4665 return -1;
4666 }
4667
4668 return 0;
4669}
4670
4671/* configuration keywords declaration */
4672static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004673 { CFG_GLOBAL, "lua-load", hlua_load },
4674 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
4675 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004676 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01004677 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004678 { 0, NULL, NULL },
4679}};
4680
Thierry FOURNIER36481b82015-08-19 09:01:53 +02004681static struct action_kw_list http_req_kws = { { }, {
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004682 { "lua", action_register_lua },
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004683 { NULL, NULL }
4684}};
4685
Thierry FOURNIER36481b82015-08-19 09:01:53 +02004686static struct action_kw_list http_res_kws = { { }, {
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004687 { "lua", action_register_lua },
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004688 { NULL, NULL }
4689}};
4690
Thierry FOURNIER36481b82015-08-19 09:01:53 +02004691static struct action_kw_list tcp_req_cont_kws = { { }, {
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004692 { "lua", action_register_lua },
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004693 { NULL, NULL }
4694}};
4695
Thierry FOURNIER36481b82015-08-19 09:01:53 +02004696static struct action_kw_list tcp_res_cont_kws = { { }, {
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004697 { "lua", action_register_lua },
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004698 { NULL, NULL }
4699}};
4700
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004701/* This function can fail with an abort() due to an Lua critical error.
4702 * We are in the initialisation process of HAProxy, this abort() is
4703 * tolerated.
4704 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004705int hlua_post_init()
4706{
4707 struct hlua_init_function *init;
4708 const char *msg;
4709 enum hlua_exec ret;
4710
4711 list_for_each_entry(init, &hlua_init_functions, l) {
4712 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
4713 ret = hlua_ctx_resume(&gL, 0);
4714 switch (ret) {
4715 case HLUA_E_OK:
4716 lua_pop(gL.T, -1);
4717 return 1;
4718 case HLUA_E_AGAIN:
4719 Alert("lua init: yield not allowed.\n");
4720 return 0;
4721 case HLUA_E_ERRMSG:
4722 msg = lua_tostring(gL.T, -1);
4723 Alert("lua init: %s.\n", msg);
4724 return 0;
4725 case HLUA_E_ERR:
4726 default:
4727 Alert("lua init: unknown runtime error.\n");
4728 return 0;
4729 }
4730 }
4731 return 1;
4732}
4733
Willy Tarreau32f61e22015-03-18 17:54:59 +01004734/* The memory allocator used by the Lua stack. <ud> is a pointer to the
4735 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
4736 * is the previously allocated size or the kind of object in case of a new
4737 * allocation. <nsize> is the requested new size.
4738 */
4739static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
4740{
4741 struct hlua_mem_allocator *zone = ud;
4742
4743 if (nsize == 0) {
4744 /* it's a free */
4745 if (ptr)
4746 zone->allocated -= osize;
4747 free(ptr);
4748 return NULL;
4749 }
4750
4751 if (!ptr) {
4752 /* it's a new allocation */
4753 if (zone->limit && zone->allocated + nsize > zone->limit)
4754 return NULL;
4755
4756 ptr = malloc(nsize);
4757 if (ptr)
4758 zone->allocated += nsize;
4759 return ptr;
4760 }
4761
4762 /* it's a realloc */
4763 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
4764 return NULL;
4765
4766 ptr = realloc(ptr, nsize);
4767 if (ptr)
4768 zone->allocated += nsize - osize;
4769 return ptr;
4770}
4771
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004772/* Ithis function can fail with an abort() due to an Lua critical error.
4773 * We are in the initialisation process of HAProxy, this abort() is
4774 * tolerated.
4775 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01004776void hlua_init(void)
4777{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004778 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004779 int idx;
4780 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004781 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004782 char *p;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004783#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004784 struct srv_kw *kw;
4785 int tmp_error;
4786 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01004787 char *args[] = { /* SSL client configuration. */
4788 "ssl",
4789 "verify",
4790 "none",
4791 "force-sslv3",
4792 NULL
4793 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004794#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004795
Willy Tarreau87b09662015-04-03 00:22:06 +02004796 /* Initialise com signals pool */
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004797 pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED);
4798
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004799 /* Register configuration keywords. */
4800 cfg_register_keywords(&cfg_kws);
4801
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004802 /* Register custom HTTP rules. */
4803 http_req_keywords_register(&http_req_kws);
4804 http_res_keywords_register(&http_res_kws);
4805 tcp_req_cont_keywords_register(&tcp_req_cont_kws);
4806 tcp_res_cont_keywords_register(&tcp_res_cont_kws);
4807
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004808 /* Init main lua stack. */
4809 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004810 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004811 LIST_INIT(&gL.com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004812 gL.T = luaL_newstate();
4813 hlua_sethlua(&gL);
4814 gL.Tref = LUA_REFNIL;
4815 gL.task = NULL;
4816
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004817 /* From this point, until the end of the initialisation fucntion,
4818 * the Lua function can fail with an abort. We are in the initialisation
4819 * process of HAProxy, this abort() is tolerated.
4820 */
4821
Willy Tarreau32f61e22015-03-18 17:54:59 +01004822 /* change the memory allocators to track memory usage */
4823 lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator);
4824
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004825 /* Initialise lua. */
4826 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004827
4828 /*
4829 *
4830 * Create "core" object.
4831 *
4832 */
4833
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01004834 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004835 lua_newtable(gL.T);
4836
4837 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004838 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004839 hlua_class_const_int(gL.T, log_levels[i], i);
4840
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004841 /* Register special functions. */
4842 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004843 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004844 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004845 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01004846 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01004847 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01004848 hlua_class_function(gL.T, "sleep", hlua_sleep);
4849 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01004850 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
4851 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
4852 hlua_class_function(gL.T, "set_map", hlua_set_map);
4853 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01004854 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01004855 hlua_class_function(gL.T, "log", hlua_log);
4856 hlua_class_function(gL.T, "Debug", hlua_log_debug);
4857 hlua_class_function(gL.T, "Info", hlua_log_info);
4858 hlua_class_function(gL.T, "Warning", hlua_log_warning);
4859 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02004860 hlua_class_function(gL.T, "done", hlua_done);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004861
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004862 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004863
4864 /*
4865 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02004866 * Register class Map
4867 *
4868 */
4869
4870 /* This table entry is the object "Map" base. */
4871 lua_newtable(gL.T);
4872
4873 /* register pattern types. */
4874 for (i=0; i<PAT_MATCH_NUM; i++)
4875 hlua_class_const_int(gL.T, pat_match_names[i], i);
4876
4877 /* register constructor. */
4878 hlua_class_function(gL.T, "new", hlua_map_new);
4879
4880 /* Create and fill the metatable. */
4881 lua_newtable(gL.T);
4882
4883 /* Create and fille the __index entry. */
4884 lua_pushstring(gL.T, "__index");
4885 lua_newtable(gL.T);
4886
4887 /* Register . */
4888 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
4889 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
4890
4891 lua_settable(gL.T, -3);
4892
4893 /* Register previous table in the registry with reference and named entry. */
4894 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4895 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4896 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_MAP); /* register class session. */
4897 class_map_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4898
4899 /* Assign the metatable to the mai Map object. */
4900 lua_setmetatable(gL.T, -2);
4901
4902 /* Set a name to the table. */
4903 lua_setglobal(gL.T, "Map");
4904
4905 /*
4906 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01004907 * Register class Channel
4908 *
4909 */
4910
4911 /* Create and fill the metatable. */
4912 lua_newtable(gL.T);
4913
4914 /* Create and fille the __index entry. */
4915 lua_pushstring(gL.T, "__index");
4916 lua_newtable(gL.T);
4917
4918 /* Register . */
4919 hlua_class_function(gL.T, "get", hlua_channel_get);
4920 hlua_class_function(gL.T, "dup", hlua_channel_dup);
4921 hlua_class_function(gL.T, "getline", hlua_channel_getline);
4922 hlua_class_function(gL.T, "set", hlua_channel_set);
4923 hlua_class_function(gL.T, "append", hlua_channel_append);
4924 hlua_class_function(gL.T, "send", hlua_channel_send);
4925 hlua_class_function(gL.T, "forward", hlua_channel_forward);
4926 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
4927 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
4928
4929 lua_settable(gL.T, -3);
4930
4931 /* Register previous table in the registry with reference and named entry. */
4932 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4933 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CHANNEL); /* register class session. */
4934 class_channel_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4935
4936 /*
4937 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004938 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004939 *
4940 */
4941
4942 /* Create and fill the metatable. */
4943 lua_newtable(gL.T);
4944
4945 /* Create and fille the __index entry. */
4946 lua_pushstring(gL.T, "__index");
4947 lua_newtable(gL.T);
4948
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004949 /* Browse existing fetches and create the associated
4950 * object method.
4951 */
4952 sf = NULL;
4953 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
4954
4955 /* Dont register the keywork if the arguments check function are
4956 * not safe during the runtime.
4957 */
4958 if ((sf->val_args != NULL) &&
4959 (sf->val_args != val_payload_lv) &&
4960 (sf->val_args != val_hdr))
4961 continue;
4962
4963 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
4964 * by an underscore.
4965 */
4966 strncpy(trash.str, sf->kw, trash.size);
4967 trash.str[trash.size - 1] = '\0';
4968 for (p = trash.str; *p; p++)
4969 if (*p == '.' || *p == '-' || *p == '+')
4970 *p = '_';
4971
4972 /* Register the function. */
4973 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01004974 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004975 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
4976 lua_settable(gL.T, -3);
4977 }
4978
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004979 lua_settable(gL.T, -3);
4980
4981 /* Register previous table in the registry with reference and named entry. */
4982 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4983 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_FETCHES); /* register class session. */
4984 class_fetches_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4985
4986 /*
4987 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004988 * Register class Converters
4989 *
4990 */
4991
4992 /* Create and fill the metatable. */
4993 lua_newtable(gL.T);
4994
4995 /* Create and fill the __index entry. */
4996 lua_pushstring(gL.T, "__index");
4997 lua_newtable(gL.T);
4998
4999 /* Browse existing converters and create the associated
5000 * object method.
5001 */
5002 sc = NULL;
5003 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
5004 /* Dont register the keywork if the arguments check function are
5005 * not safe during the runtime.
5006 */
5007 if (sc->val_args != NULL)
5008 continue;
5009
5010 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
5011 * by an underscore.
5012 */
5013 strncpy(trash.str, sc->kw, trash.size);
5014 trash.str[trash.size - 1] = '\0';
5015 for (p = trash.str; *p; p++)
5016 if (*p == '.' || *p == '-' || *p == '+')
5017 *p = '_';
5018
5019 /* Register the function. */
5020 lua_pushstring(gL.T, trash.str);
5021 lua_pushlightuserdata(gL.T, sc);
5022 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
5023 lua_settable(gL.T, -3);
5024 }
5025
5026 lua_settable(gL.T, -3);
5027
5028 /* Register previous table in the registry with reference and named entry. */
5029 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5030 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CONVERTERS); /* register class session. */
5031 class_converters_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5032
5033 /*
5034 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005035 * Register class HTTP
5036 *
5037 */
5038
5039 /* Create and fill the metatable. */
5040 lua_newtable(gL.T);
5041
5042 /* Create and fille the __index entry. */
5043 lua_pushstring(gL.T, "__index");
5044 lua_newtable(gL.T);
5045
5046 /* Register Lua functions. */
5047 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
5048 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
5049 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
5050 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
5051 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
5052 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
5053 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
5054 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
5055 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
5056 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
5057
5058 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
5059 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
5060 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
5061 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
5062 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
5063 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005064 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005065
5066 lua_settable(gL.T, -3);
5067
5068 /* Register previous table in the registry with reference and named entry. */
5069 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5070 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_HTTP); /* register class session. */
5071 class_http_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5072
5073 /*
5074 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005075 * Register class TXN
5076 *
5077 */
5078
5079 /* Create and fill the metatable. */
5080 lua_newtable(gL.T);
5081
5082 /* Create and fille the __index entry. */
5083 lua_pushstring(gL.T, "__index");
5084 lua_newtable(gL.T);
5085
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005086 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01005087 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
5088 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005089 hlua_class_function(gL.T, "set_var", hlua_set_var);
5090 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005091 hlua_class_function(gL.T, "done", hlua_txn_done);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005092 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
5093 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
5094 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005095 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
5096 hlua_class_function(gL.T, "log", hlua_txn_log);
5097 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
5098 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
5099 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
5100 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005101
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005102 lua_settable(gL.T, -3);
5103
5104 /* Register previous table in the registry with reference and named entry. */
5105 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5106 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_TXN); /* register class session. */
5107 class_txn_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005108
5109 /*
5110 *
5111 * Register class Socket
5112 *
5113 */
5114
5115 /* Create and fill the metatable. */
5116 lua_newtable(gL.T);
5117
5118 /* Create and fille the __index entry. */
5119 lua_pushstring(gL.T, "__index");
5120 lua_newtable(gL.T);
5121
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005122#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005123 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005124#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005125 hlua_class_function(gL.T, "connect", hlua_socket_connect);
5126 hlua_class_function(gL.T, "send", hlua_socket_send);
5127 hlua_class_function(gL.T, "receive", hlua_socket_receive);
5128 hlua_class_function(gL.T, "close", hlua_socket_close);
5129 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
5130 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
5131 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
5132 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
5133
5134 lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
5135
5136 /* Register the garbage collector entry. */
5137 lua_pushstring(gL.T, "__gc");
5138 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
5139 lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
5140
5141 /* Register previous table in the registry with reference and named entry. */
5142 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5143 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5144 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_SOCKET); /* register class socket. */
5145 class_socket_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class socket. */
5146
5147 /* Proxy and server configuration initialisation. */
5148 memset(&socket_proxy, 0, sizeof(socket_proxy));
5149 init_new_proxy(&socket_proxy);
5150 socket_proxy.parent = NULL;
5151 socket_proxy.last_change = now.tv_sec;
5152 socket_proxy.id = "LUA-SOCKET";
5153 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
5154 socket_proxy.maxconn = 0;
5155 socket_proxy.accept = NULL;
5156 socket_proxy.options2 |= PR_O2_INDEPSTR;
5157 socket_proxy.srv = NULL;
5158 socket_proxy.conn_retries = 0;
5159 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
5160
5161 /* Init TCP server: unchanged parameters */
5162 memset(&socket_tcp, 0, sizeof(socket_tcp));
5163 socket_tcp.next = NULL;
5164 socket_tcp.proxy = &socket_proxy;
5165 socket_tcp.obj_type = OBJ_TYPE_SERVER;
5166 LIST_INIT(&socket_tcp.actconns);
5167 LIST_INIT(&socket_tcp.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02005168 LIST_INIT(&socket_tcp.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02005169 LIST_INIT(&socket_tcp.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02005170 LIST_INIT(&socket_tcp.safe_conns);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005171 socket_tcp.state = SRV_ST_RUNNING; /* early server setup */
5172 socket_tcp.last_change = 0;
5173 socket_tcp.id = "LUA-TCP-CONN";
5174 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5175 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5176 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
5177
5178 /* XXX: Copy default parameter from default server,
5179 * but the default server is not initialized.
5180 */
5181 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
5182 socket_tcp.minconn = socket_proxy.defsrv.minconn;
5183 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
5184 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
5185 socket_tcp.onerror = socket_proxy.defsrv.onerror;
5186 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5187 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
5188 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5189 socket_tcp.uweight = socket_proxy.defsrv.iweight;
5190 socket_tcp.iweight = socket_proxy.defsrv.iweight;
5191
5192 socket_tcp.check.status = HCHK_STATUS_INI;
5193 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
5194 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
5195 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
5196 socket_tcp.check.server = &socket_tcp;
5197
5198 socket_tcp.agent.status = HCHK_STATUS_INI;
5199 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
5200 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
5201 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
5202 socket_tcp.agent.server = &socket_tcp;
5203
5204 socket_tcp.xprt = &raw_sock;
5205
5206#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005207 /* Init TCP server: unchanged parameters */
5208 memset(&socket_ssl, 0, sizeof(socket_ssl));
5209 socket_ssl.next = NULL;
5210 socket_ssl.proxy = &socket_proxy;
5211 socket_ssl.obj_type = OBJ_TYPE_SERVER;
5212 LIST_INIT(&socket_ssl.actconns);
5213 LIST_INIT(&socket_ssl.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02005214 LIST_INIT(&socket_ssl.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02005215 LIST_INIT(&socket_ssl.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02005216 LIST_INIT(&socket_ssl.safe_conns);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005217 socket_ssl.state = SRV_ST_RUNNING; /* early server setup */
5218 socket_ssl.last_change = 0;
5219 socket_ssl.id = "LUA-SSL-CONN";
5220 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5221 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5222 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
5223
5224 /* XXX: Copy default parameter from default server,
5225 * but the default server is not initialized.
5226 */
5227 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
5228 socket_ssl.minconn = socket_proxy.defsrv.minconn;
5229 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
5230 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
5231 socket_ssl.onerror = socket_proxy.defsrv.onerror;
5232 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5233 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
5234 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5235 socket_ssl.uweight = socket_proxy.defsrv.iweight;
5236 socket_ssl.iweight = socket_proxy.defsrv.iweight;
5237
5238 socket_ssl.check.status = HCHK_STATUS_INI;
5239 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
5240 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
5241 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
5242 socket_ssl.check.server = &socket_ssl;
5243
5244 socket_ssl.agent.status = HCHK_STATUS_INI;
5245 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
5246 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
5247 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
5248 socket_ssl.agent.server = &socket_ssl;
5249
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005250 socket_ssl.use_ssl = 1;
5251 socket_ssl.xprt = &ssl_sock;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005252
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005253 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005254 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
5255 /*
5256 *
5257 * If the keyword is not known, we can search in the registered
5258 * server keywords. This is usefull to configure special SSL
5259 * features like client certificates and ssl_verify.
5260 *
5261 */
5262 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
5263 if (tmp_error != 0) {
5264 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
5265 abort(); /* This must be never arrives because the command line
5266 not editable by the user. */
5267 }
5268 idx += kw->skip;
5269 }
5270 }
5271
5272 /* Initialize SSL server. */
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005273 ssl_sock_prepare_srv_ctx(&socket_ssl, &socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005274#endif
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01005275}