blob: b893a47b6595e540f2c20006de5c9782f041cc2b [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 FOURNIER84e73c82015-09-25 22:13:32 +0200236 lua_rawset(L, -3);
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100237}
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);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +0200245 lua_rawset(L, -3);
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100246}
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);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +0200254 lua_rawset(L, -3);
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100255}
256
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +0200257__LJMP static int hlua_dump_object(struct lua_State *L)
258{
259 const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1));
260 lua_pushfstring(L, "HAProxy class %s", name);
261 return 1;
262}
263
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100264/* This function check the number of arguments available in the
265 * stack. If the number of arguments available is not the same
266 * then <nb> an error is throwed.
267 */
268__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
269{
270 if (lua_gettop(L) == nb)
271 return;
272 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
273}
274
275/* Return true if the data in stack[<ud>] is an object of
276 * type <class_ref>.
277 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +0100278static int hlua_metaistype(lua_State *L, int ud, int class_ref)
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100279{
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100280 if (!lua_getmetatable(L, ud))
281 return 0;
282
283 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
284 if (!lua_rawequal(L, -1, -2)) {
285 lua_pop(L, 2);
286 return 0;
287 }
288
289 lua_pop(L, 2);
290 return 1;
291}
292
293/* Return an object of the expected type, or throws an error. */
294__LJMP static void *hlua_checkudata(lua_State *L, int ud, int class_ref)
295{
Thierry FOURNIER2297bc22015-03-11 17:43:33 +0100296 void *p;
297
298 /* Check if the stack entry is an array. */
299 if (!lua_istable(L, ud))
300 WILL_LJMP(luaL_argerror(L, ud, NULL));
301 /* Check if the metadata have the expected type. */
302 if (!hlua_metaistype(L, ud, class_ref))
303 WILL_LJMP(luaL_argerror(L, ud, NULL));
304 /* Push on the stack at the entry [0] of the table. */
305 lua_rawgeti(L, ud, 0);
306 /* Check if this entry is userdata. */
307 p = lua_touserdata(L, -1);
308 if (!p)
309 WILL_LJMP(luaL_argerror(L, ud, NULL));
310 /* Remove the entry returned by lua_rawgeti(). */
311 lua_pop(L, 1);
312 /* Return the associated struct. */
313 return p;
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100314}
315
316/* This fucntion push an error string prefixed by the file name
317 * and the line number where the error is encountered.
318 */
319static int hlua_pusherror(lua_State *L, const char *fmt, ...)
320{
321 va_list argp;
322 va_start(argp, fmt);
323 luaL_where(L, 1);
324 lua_pushvfstring(L, fmt, argp);
325 va_end(argp);
326 lua_concat(L, 2);
327 return 1;
328}
329
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100330/* This function register a new signal. "lua" is the current lua
331 * execution context. It contains a pointer to the associated task.
332 * "link" is a list head attached to an other task that must be wake
333 * the lua task if an event occurs. This is useful with external
334 * events like TCP I/O or sleep functions. This funcion allocate
335 * memory for the signal.
336 */
337static int hlua_com_new(struct hlua *lua, struct list *link)
338{
339 struct hlua_com *com = pool_alloc2(pool2_hlua_com);
340 if (!com)
341 return 0;
342 LIST_ADDQ(&lua->com, &com->purge_me);
343 LIST_ADDQ(link, &com->wake_me);
344 com->task = lua->task;
345 return 1;
346}
347
348/* This function purge all the pending signals when the LUA execution
349 * is finished. This prevent than a coprocess try to wake a deleted
350 * task. This function remove the memory associated to the signal.
351 */
352static void hlua_com_purge(struct hlua *lua)
353{
354 struct hlua_com *com, *back;
355
356 /* Delete all pending communication signals. */
357 list_for_each_entry_safe(com, back, &lua->com, purge_me) {
358 LIST_DEL(&com->purge_me);
359 LIST_DEL(&com->wake_me);
360 pool_free2(pool2_hlua_com, com);
361 }
362}
363
364/* This function sends signals. It wakes all the tasks attached
365 * to a list head, and remove the signal, and free the used
366 * memory.
367 */
368static void hlua_com_wake(struct list *wake)
369{
370 struct hlua_com *com, *back;
371
372 /* Wake task and delete all pending communication signals. */
373 list_for_each_entry_safe(com, back, wake, wake_me) {
374 LIST_DEL(&com->purge_me);
375 LIST_DEL(&com->wake_me);
376 task_wakeup(com->task, TASK_WOKEN_MSG);
377 pool_free2(pool2_hlua_com, com);
378 }
379}
380
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100381/* This functions is used with sample fetch and converters. It
382 * converts the HAProxy configuration argument in a lua stack
383 * values.
384 *
385 * It takes an array of "arg", and each entry of the array is
386 * converted and pushed in the LUA stack.
387 */
388static int hlua_arg2lua(lua_State *L, const struct arg *arg)
389{
390 switch (arg->type) {
391 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100392 case ARGT_TIME:
393 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100394 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100395 break;
396
397 case ARGT_STR:
398 lua_pushlstring(L, arg->data.str.str, arg->data.str.len);
399 break;
400
401 case ARGT_IPV4:
402 case ARGT_IPV6:
403 case ARGT_MSK4:
404 case ARGT_MSK6:
405 case ARGT_FE:
406 case ARGT_BE:
407 case ARGT_TAB:
408 case ARGT_SRV:
409 case ARGT_USR:
410 case ARGT_MAP:
411 default:
412 lua_pushnil(L);
413 break;
414 }
415 return 1;
416}
417
418/* This function take one entrie in an LUA stack at the index "ud",
419 * and try to convert it in an HAProxy argument entry. This is useful
420 * with sample fetch wrappers. The input arguments are gived to the
421 * lua wrapper and converted as arg list by thi function.
422 */
423static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
424{
425 switch (lua_type(L, ud)) {
426
427 case LUA_TNUMBER:
428 case LUA_TBOOLEAN:
429 arg->type = ARGT_SINT;
430 arg->data.sint = lua_tointeger(L, ud);
431 break;
432
433 case LUA_TSTRING:
434 arg->type = ARGT_STR;
435 arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len);
436 break;
437
438 case LUA_TUSERDATA:
439 case LUA_TNIL:
440 case LUA_TTABLE:
441 case LUA_TFUNCTION:
442 case LUA_TTHREAD:
443 case LUA_TLIGHTUSERDATA:
444 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200445 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100446 break;
447 }
448 return 1;
449}
450
451/* the following functions are used to convert a struct sample
452 * in Lua type. This useful to convert the return of the
453 * fetchs or converters.
454 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100455static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100456{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200457 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100458 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100459 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200460 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100461 break;
462
463 case SMP_T_BIN:
464 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200465 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100466 break;
467
468 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200469 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100470 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
471 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
472 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
473 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
474 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
475 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
476 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
477 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
478 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200479 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100480 break;
481 default:
482 lua_pushnil(L);
483 break;
484 }
485 break;
486
487 case SMP_T_IPV4:
488 case SMP_T_IPV6:
489 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200490 if (sample_casts[smp->data.type][SMP_T_STR] &&
491 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200492 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100493 else
494 lua_pushnil(L);
495 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100496 default:
497 lua_pushnil(L);
498 break;
499 }
500 return 1;
501}
502
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100503/* the following functions are used to convert a struct sample
504 * in Lua strings. This is useful to convert the return of the
505 * fetchs or converters.
506 */
507static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
508{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200509 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100510
511 case SMP_T_BIN:
512 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200513 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100514 break;
515
516 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200517 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100518 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
519 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
520 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
521 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
522 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
523 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
524 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
525 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
526 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200527 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100528 break;
529 default:
530 lua_pushstring(L, "");
531 break;
532 }
533 break;
534
535 case SMP_T_SINT:
536 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100537 case SMP_T_IPV4:
538 case SMP_T_IPV6:
539 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200540 if (sample_casts[smp->data.type][SMP_T_STR] &&
541 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200542 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100543 else
544 lua_pushstring(L, "");
545 break;
546 default:
547 lua_pushstring(L, "");
548 break;
549 }
550 return 1;
551}
552
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100553/* the following functions are used to convert an Lua type in a
554 * struct sample. This is useful to provide data from a converter
555 * to the LUA code.
556 */
557static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
558{
559 switch (lua_type(L, ud)) {
560
561 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200562 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200563 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100564 break;
565
566
567 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200568 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200569 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570 break;
571
572 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200573 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100574 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200575 smp->data.u.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100576 break;
577
578 case LUA_TUSERDATA:
579 case LUA_TNIL:
580 case LUA_TTABLE:
581 case LUA_TFUNCTION:
582 case LUA_TTHREAD:
583 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200584 case LUA_TNONE:
585 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200586 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200587 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100588 break;
589 }
590 return 1;
591}
592
593/* This function check the "argp" builded by another conversion function
594 * is in accord with the expected argp defined by the "mask". The fucntion
595 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100596 *
597 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
598 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100599 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100600__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
601 unsigned int mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100602{
603 int min_arg;
604 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100605 struct proxy *px;
606 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100607
608 idx = 0;
609 min_arg = ARGM(mask);
610 mask >>= ARGM_BITS;
611
612 while (1) {
613
614 /* Check oversize. */
615 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100616 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100617 }
618
619 /* Check for mandatory arguments. */
620 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100621 if (idx < min_arg) {
622
623 /* If miss other argument than the first one, we return an error. */
624 if (idx > 0)
625 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
626
627 /* If first argument have a certain type, some default values
628 * may be used. See the function smp_resolve_args().
629 */
630 switch (mask & ARGT_MASK) {
631
632 case ARGT_FE:
633 if (!(p->cap & PR_CAP_FE))
634 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
635 argp[idx].data.prx = p;
636 argp[idx].type = ARGT_FE;
637 argp[idx+1].type = ARGT_STOP;
638 break;
639
640 case ARGT_BE:
641 if (!(p->cap & PR_CAP_BE))
642 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
643 argp[idx].data.prx = p;
644 argp[idx].type = ARGT_BE;
645 argp[idx+1].type = ARGT_STOP;
646 break;
647
648 case ARGT_TAB:
649 argp[idx].data.prx = p;
650 argp[idx].type = ARGT_TAB;
651 argp[idx+1].type = ARGT_STOP;
652 break;
653
654 default:
655 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
656 break;
657 }
658 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100659 return 0;
660 }
661
662 /* Check for exceed the number of requiered argument. */
663 if ((mask & ARGT_MASK) == ARGT_STOP &&
664 argp[idx].type != ARGT_STOP) {
665 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
666 }
667
668 if ((mask & ARGT_MASK) == ARGT_STOP &&
669 argp[idx].type == ARGT_STOP) {
670 return 0;
671 }
672
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100673 /* Convert some argument types. */
674 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100675 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100676 if (argp[idx].type != ARGT_SINT)
677 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
678 argp[idx].type = ARGT_SINT;
679 break;
680
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100681 case ARGT_TIME:
682 if (argp[idx].type != ARGT_SINT)
683 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200684 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100685 break;
686
687 case ARGT_SIZE:
688 if (argp[idx].type != ARGT_SINT)
689 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200690 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100691 break;
692
693 case ARGT_FE:
694 if (argp[idx].type != ARGT_STR)
695 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
696 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
697 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200698 argp[idx].data.prx = proxy_fe_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100699 if (!argp[idx].data.prx)
700 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
701 argp[idx].type = ARGT_FE;
702 break;
703
704 case ARGT_BE:
705 if (argp[idx].type != ARGT_STR)
706 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
707 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
708 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200709 argp[idx].data.prx = proxy_be_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100710 if (!argp[idx].data.prx)
711 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
712 argp[idx].type = ARGT_BE;
713 break;
714
715 case ARGT_TAB:
716 if (argp[idx].type != ARGT_STR)
717 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
718 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
719 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200720 argp[idx].data.prx = proxy_tbl_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100721 if (!argp[idx].data.prx)
722 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
723 argp[idx].type = ARGT_TAB;
724 break;
725
726 case ARGT_SRV:
727 if (argp[idx].type != ARGT_STR)
728 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
729 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
730 trash.str[argp[idx].data.str.len] = 0;
731 sname = strrchr(trash.str, '/');
732 if (sname) {
733 *sname++ = '\0';
734 pname = trash.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200735 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100736 if (!px)
737 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
738 }
739 else {
740 sname = trash.str;
741 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100742 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100743 argp[idx].data.srv = findserver(px, sname);
744 if (!argp[idx].data.srv)
745 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
746 argp[idx].type = ARGT_SRV;
747 break;
748
749 case ARGT_IPV4:
750 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
751 trash.str[argp[idx].data.str.len] = 0;
752 if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4))
753 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
754 argp[idx].type = ARGT_IPV4;
755 break;
756
757 case ARGT_MSK4:
758 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
759 trash.str[argp[idx].data.str.len] = 0;
760 if (!str2mask(trash.str, &argp[idx].data.ipv4))
761 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
762 argp[idx].type = ARGT_MSK4;
763 break;
764
765 case ARGT_IPV6:
766 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
767 trash.str[argp[idx].data.str.len] = 0;
768 if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6))
769 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
770 argp[idx].type = ARGT_IPV6;
771 break;
772
773 case ARGT_MSK6:
774 case ARGT_MAP:
775 case ARGT_REG:
776 case ARGT_USR:
777 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100778 break;
779 }
780
781 /* Check for type of argument. */
782 if ((mask & ARGT_MASK) != argp[idx].type) {
783 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
784 arg_type_names[(mask & ARGT_MASK)],
785 arg_type_names[argp[idx].type & ARGT_MASK]);
786 WILL_LJMP(luaL_argerror(L, first + idx, msg));
787 }
788
789 /* Next argument. */
790 mask >>= ARGT_BITS;
791 idx++;
792 }
793}
794
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100795/*
796 * The following functions are used to make correspondance between the the
797 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100798 *
799 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100800 * - hlua_sethlua : create the association between hlua context and lua_state.
801 */
802static inline struct hlua *hlua_gethlua(lua_State *L)
803{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100804 struct hlua **hlua = lua_getextraspace(L);
805 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100806}
807static inline void hlua_sethlua(struct hlua *hlua)
808{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100809 struct hlua **hlua_store = lua_getextraspace(hlua->T);
810 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100811}
812
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100813/* This function is used to send logs. It try to send on screen (stderr)
814 * and on the default syslog server.
815 */
816static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
817{
818 struct tm tm;
819 char *p;
820
821 /* Cleanup the log message. */
822 p = trash.str;
823 for (; *msg != '\0'; msg++, p++) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200824 if (p >= trash.str + trash.size - 1) {
825 /* Break the message if exceed the buffer size. */
826 *(p-4) = ' ';
827 *(p-3) = '.';
828 *(p-2) = '.';
829 *(p-1) = '.';
830 break;
831 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100832 if (isprint(*msg))
833 *p = *msg;
834 else
835 *p = '.';
836 }
837 *p = '\0';
838
Thierry FOURNIER5554e292015-09-09 11:21:37 +0200839 send_log(px, level, "%s\n", trash.str);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100840 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200841 get_localtime(date.tv_sec, &tm);
842 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100843 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
844 (int)getpid(), trash.str);
845 fflush(stderr);
846 }
847}
848
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100849/* This function just ensure that the yield will be always
850 * returned with a timeout and permit to set some flags
851 */
852__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100853 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100854{
855 struct hlua *hlua = hlua_gethlua(L);
856
857 /* Set the wake timeout. If timeout is required, we set
858 * the expiration time.
859 */
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100860 hlua->wake_time = tick_first(timeout, hlua->expire);
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100861
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100862 hlua->flags |= flags;
863
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100864 /* Process the yield. */
865 WILL_LJMP(lua_yieldk(L, nresults, ctx, k));
866}
867
Willy Tarreau87b09662015-04-03 00:22:06 +0200868/* This function initialises the Lua environment stored in the stream.
869 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100870 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200871 *
872 * This function is particular. it initialises a new Lua thread. If the
873 * initialisation fails (example: out of memory error), the lua function
874 * throws an error (longjmp).
875 *
876 * This function manipulates two Lua stack: the main and the thread. Only
877 * the main stack can fail. The thread is not manipulated. This function
878 * MUST NOT manipulate the created thread stack state, because is not
879 * proctected agains error throwed by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100880 */
881int hlua_ctx_init(struct hlua *lua, struct task *task)
882{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200883 if (!SET_SAFE_LJMP(gL.T)) {
884 lua->Tref = LUA_REFNIL;
885 return 0;
886 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100887 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100888 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100889 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100890 lua->T = lua_newthread(gL.T);
891 if (!lua->T) {
892 lua->Tref = LUA_REFNIL;
893 return 0;
894 }
895 hlua_sethlua(lua);
896 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
897 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200898 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100899 return 1;
900}
901
Willy Tarreau87b09662015-04-03 00:22:06 +0200902/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100903 * is destroyed. The destroy also the memory context. The struct "lua"
904 * is not freed.
905 */
906void hlua_ctx_destroy(struct hlua *lua)
907{
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100908 if (!lua->T)
909 return;
910
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100911 /* Purge all the pending signals. */
912 hlua_com_purge(lua);
913
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100914 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
915 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200916
917 /* Forces a garbage collecting process. If the Lua program is finished
918 * without error, we run the GC on the thread pointer. Its freed all
919 * the unused memory.
920 * If the thread is finnish with an error or is currently yielded,
921 * it seems that the GC applied on the thread doesn't clean anything,
922 * so e run the GC on the main thread.
923 * NOTE: maybe this action locks all the Lua threads untiml the en of
924 * the garbage collection.
925 */
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +0200926 lua_gc(lua->T, LUA_GCCOLLECT, 0);
927 if (lua_status(lua->T) != LUA_OK)
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200928 lua_gc(gL.T, LUA_GCCOLLECT, 0);
929
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200930 lua->T = NULL;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100931}
932
933/* This function is used to restore the Lua context when a coroutine
934 * fails. This function copy the common memory between old coroutine
935 * and the new coroutine. The old coroutine is destroyed, and its
936 * replaced by the new coroutine.
937 * If the flag "keep_msg" is set, the last entry of the old is assumed
938 * as string error message and it is copied in the new stack.
939 */
940static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
941{
942 lua_State *T;
943 int new_ref;
944
945 /* Renew the main LUA stack doesn't have sense. */
946 if (lua == &gL)
947 return 0;
948
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100949 /* New Lua coroutine. */
950 T = lua_newthread(gL.T);
951 if (!T)
952 return 0;
953
954 /* Copy last error message. */
955 if (keep_msg)
956 lua_xmove(lua->T, T, 1);
957
958 /* Copy data between the coroutines. */
959 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
960 lua_xmove(lua->T, T, 1);
961 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
962
963 /* Destroy old data. */
964 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
965
966 /* The thread is garbage collected by Lua. */
967 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
968
969 /* Fill the struct with the new coroutine values. */
970 lua->Mref = new_ref;
971 lua->T = T;
972 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
973
974 /* Set context. */
975 hlua_sethlua(lua);
976
977 return 1;
978}
979
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100980void hlua_hook(lua_State *L, lua_Debug *ar)
981{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100982 struct hlua *hlua = hlua_gethlua(L);
983
984 /* Lua cannot yield when its returning from a function,
985 * so, we can fix the interrupt hook to 1 instruction,
986 * expecting that the function is finnished.
987 */
988 if (lua_gethookmask(L) & LUA_MASKRET) {
989 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
990 return;
991 }
992
993 /* restore the interrupt condition. */
994 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
995
996 /* If we interrupt the Lua processing in yieldable state, we yield.
997 * If the state is not yieldable, trying yield causes an error.
998 */
999 if (lua_isyieldable(L))
1000 WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
1001
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001002 /* If we cannot yield, update the clock and check the timeout. */
1003 tv_update_date(0, 1);
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001004 if (tick_is_expired(hlua->expire, now_ms)) {
1005 lua_pushfstring(L, "execution timeout");
1006 WILL_LJMP(lua_error(L));
1007 }
1008
1009 /* Try to interrupt the process at the end of the current
1010 * unyieldable function.
1011 */
1012 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001013}
1014
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001015/* This function start or resumes the Lua stack execution. If the flag
1016 * "yield_allowed" if no set and the LUA stack execution returns a yield
1017 * The function return an error.
1018 *
1019 * The function can returns 4 values:
1020 * - HLUA_E_OK : The execution is terminated without any errors.
1021 * - HLUA_E_AGAIN : The execution must continue at the next associated
1022 * task wakeup.
1023 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
1024 * the top of the stack.
1025 * - HLUA_E_ERR : An error has occured without error message.
1026 *
1027 * If an error occured, the stack is renewed and it is ready to run new
1028 * LUA code.
1029 */
1030static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1031{
1032 int ret;
1033 const char *msg;
1034
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001035 HLUA_SET_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001036
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001037 /* If we want to resume the task, then check first the execution timeout.
1038 * if it is reached, we can interrupt the Lua processing.
1039 */
1040 if (tick_is_expired(lua->expire, now_ms))
1041 goto timeout_reached;
1042
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001043resume_execution:
1044
1045 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1046 * instructions. it is used for preventing infinite loops.
1047 */
1048 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1049
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001050 /* Remove all flags except the running flags. */
1051 lua->flags = HLUA_RUN;
1052
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001053 /* Call the function. */
1054 ret = lua_resume(lua->T, gL.T, lua->nargs);
1055 switch (ret) {
1056
1057 case LUA_OK:
1058 ret = HLUA_E_OK;
1059 break;
1060
1061 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001062 /* Check if the execution timeout is expired. It it is the case, we
1063 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001064 */
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001065 if (tick_is_expired(lua->expire, now_ms)) {
1066
1067timeout_reached:
1068
1069 lua_settop(lua->T, 0); /* Empty the stack. */
1070 if (!lua_checkstack(lua->T, 1)) {
1071 ret = HLUA_E_ERR;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001072 break;
1073 }
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001074 lua_pushfstring(lua->T, "execution timeout");
1075 ret = HLUA_E_ERRMSG;
1076 break;
1077 }
1078 /* Process the forced yield. if the general yield is not allowed or
1079 * if no task were associated this the current Lua execution
1080 * coroutine, we resume the execution. Else we want to return in the
1081 * scheduler and we want to be waked up again, to continue the
1082 * current Lua execution. So we schedule our own task.
1083 */
1084 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001085 if (!yield_allowed || !lua->task)
1086 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001087 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001088 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001089 if (!yield_allowed) {
1090 lua_settop(lua->T, 0); /* Empty the stack. */
1091 if (!lua_checkstack(lua->T, 1)) {
1092 ret = HLUA_E_ERR;
1093 break;
1094 }
1095 lua_pushfstring(lua->T, "yield not allowed");
1096 ret = HLUA_E_ERRMSG;
1097 break;
1098 }
1099 ret = HLUA_E_AGAIN;
1100 break;
1101
1102 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001103
1104 /* Special exit case. The traditionnal exit is returned as an error
1105 * because the errors ares the only one mean to return immediately
1106 * from and lua execution.
1107 */
1108 if (lua->flags & HLUA_EXIT) {
1109 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001110 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001111 break;
1112 }
1113
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001114 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001115 if (!lua_checkstack(lua->T, 1)) {
1116 ret = HLUA_E_ERR;
1117 break;
1118 }
1119 msg = lua_tostring(lua->T, -1);
1120 lua_settop(lua->T, 0); /* Empty the stack. */
1121 lua_pop(lua->T, 1);
1122 if (msg)
1123 lua_pushfstring(lua->T, "runtime error: %s", msg);
1124 else
1125 lua_pushfstring(lua->T, "unknown runtime error");
1126 ret = HLUA_E_ERRMSG;
1127 break;
1128
1129 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001130 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001131 lua_settop(lua->T, 0); /* Empty the stack. */
1132 if (!lua_checkstack(lua->T, 1)) {
1133 ret = HLUA_E_ERR;
1134 break;
1135 }
1136 lua_pushfstring(lua->T, "out of memory error");
1137 ret = HLUA_E_ERRMSG;
1138 break;
1139
1140 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001141 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001142 if (!lua_checkstack(lua->T, 1)) {
1143 ret = HLUA_E_ERR;
1144 break;
1145 }
1146 msg = lua_tostring(lua->T, -1);
1147 lua_settop(lua->T, 0); /* Empty the stack. */
1148 lua_pop(lua->T, 1);
1149 if (msg)
1150 lua_pushfstring(lua->T, "message handler error: %s", msg);
1151 else
1152 lua_pushfstring(lua->T, "message handler error");
1153 ret = HLUA_E_ERRMSG;
1154 break;
1155
1156 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001157 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001158 lua_settop(lua->T, 0); /* Empty the stack. */
1159 if (!lua_checkstack(lua->T, 1)) {
1160 ret = HLUA_E_ERR;
1161 break;
1162 }
1163 lua_pushfstring(lua->T, "unknonwn error");
1164 ret = HLUA_E_ERRMSG;
1165 break;
1166 }
1167
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001168 /* This GC permits to destroy some object when a Lua timeout strikes. */
1169 if (ret != HLUA_E_AGAIN)
1170 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1171
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001172 switch (ret) {
1173 case HLUA_E_AGAIN:
1174 break;
1175
1176 case HLUA_E_ERRMSG:
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001177 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001178 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001179 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001180 break;
1181
1182 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001183 HLUA_CLR_RUN(lua);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001184 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001185 hlua_ctx_renew(lua, 0);
1186 break;
1187
1188 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001189 HLUA_CLR_RUN(lua);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001190 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001191 break;
1192 }
1193
1194 return ret;
1195}
1196
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001197/* This function exit the current code. */
1198__LJMP static int hlua_done(lua_State *L)
1199{
1200 struct hlua *hlua = hlua_gethlua(L);
1201
1202 hlua->flags |= HLUA_EXIT;
1203 WILL_LJMP(lua_error(L));
1204
1205 return 0;
1206}
1207
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001208/* This function is an LUA binding. It provides a function
1209 * for deleting ACL from a referenced ACL file.
1210 */
1211__LJMP static int hlua_del_acl(lua_State *L)
1212{
1213 const char *name;
1214 const char *key;
1215 struct pat_ref *ref;
1216
1217 MAY_LJMP(check_args(L, 2, "del_acl"));
1218
1219 name = MAY_LJMP(luaL_checkstring(L, 1));
1220 key = MAY_LJMP(luaL_checkstring(L, 2));
1221
1222 ref = pat_ref_lookup(name);
1223 if (!ref)
1224 WILL_LJMP(luaL_error(L, "'del_acl': unkown acl file '%s'", name));
1225
1226 pat_ref_delete(ref, key);
1227 return 0;
1228}
1229
1230/* This function is an LUA binding. It provides a function
1231 * for deleting map entry from a referenced map file.
1232 */
1233static int hlua_del_map(lua_State *L)
1234{
1235 const char *name;
1236 const char *key;
1237 struct pat_ref *ref;
1238
1239 MAY_LJMP(check_args(L, 2, "del_map"));
1240
1241 name = MAY_LJMP(luaL_checkstring(L, 1));
1242 key = MAY_LJMP(luaL_checkstring(L, 2));
1243
1244 ref = pat_ref_lookup(name);
1245 if (!ref)
1246 WILL_LJMP(luaL_error(L, "'del_map': unkown acl file '%s'", name));
1247
1248 pat_ref_delete(ref, key);
1249 return 0;
1250}
1251
1252/* This function is an LUA binding. It provides a function
1253 * for adding ACL pattern from a referenced ACL file.
1254 */
1255static int hlua_add_acl(lua_State *L)
1256{
1257 const char *name;
1258 const char *key;
1259 struct pat_ref *ref;
1260
1261 MAY_LJMP(check_args(L, 2, "add_acl"));
1262
1263 name = MAY_LJMP(luaL_checkstring(L, 1));
1264 key = MAY_LJMP(luaL_checkstring(L, 2));
1265
1266 ref = pat_ref_lookup(name);
1267 if (!ref)
1268 WILL_LJMP(luaL_error(L, "'add_acl': unkown acl file '%s'", name));
1269
1270 if (pat_ref_find_elt(ref, key) == NULL)
1271 pat_ref_add(ref, key, NULL, NULL);
1272 return 0;
1273}
1274
1275/* This function is an LUA binding. It provides a function
1276 * for setting map pattern and sample from a referenced map
1277 * file.
1278 */
1279static int hlua_set_map(lua_State *L)
1280{
1281 const char *name;
1282 const char *key;
1283 const char *value;
1284 struct pat_ref *ref;
1285
1286 MAY_LJMP(check_args(L, 3, "set_map"));
1287
1288 name = MAY_LJMP(luaL_checkstring(L, 1));
1289 key = MAY_LJMP(luaL_checkstring(L, 2));
1290 value = MAY_LJMP(luaL_checkstring(L, 3));
1291
1292 ref = pat_ref_lookup(name);
1293 if (!ref)
1294 WILL_LJMP(luaL_error(L, "'set_map': unkown map file '%s'", name));
1295
1296 if (pat_ref_find_elt(ref, key) != NULL)
1297 pat_ref_set(ref, key, value, NULL);
1298 else
1299 pat_ref_add(ref, key, value, NULL);
1300 return 0;
1301}
1302
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001303/* A class is a lot of memory that contain data. This data can be a table,
1304 * an integer or user data. This data is associated with a metatable. This
1305 * metatable have an original version registred in the global context with
1306 * the name of the object (_G[<name>] = <metable> ).
1307 *
1308 * A metable is a table that modify the standard behavior of a standard
1309 * access to the associated data. The entries of this new metatable are
1310 * defined as is:
1311 *
1312 * http://lua-users.org/wiki/MetatableEvents
1313 *
1314 * __index
1315 *
1316 * we access an absent field in a table, the result is nil. This is
1317 * true, but it is not the whole truth. Actually, such access triggers
1318 * the interpreter to look for an __index metamethod: If there is no
1319 * such method, as usually happens, then the access results in nil;
1320 * otherwise, the metamethod will provide the result.
1321 *
1322 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1323 * the key does not appear in the table, but the metatable has an __index
1324 * property:
1325 *
1326 * - if the value is a function, the function is called, passing in the
1327 * table and the key; the return value of that function is returned as
1328 * the result.
1329 *
1330 * - if the value is another table, the value of the key in that table is
1331 * asked for and returned (and if it doesn't exist in that table, but that
1332 * table's metatable has an __index property, then it continues on up)
1333 *
1334 * - Use "rawget(myTable,key)" to skip this metamethod.
1335 *
1336 * http://www.lua.org/pil/13.4.1.html
1337 *
1338 * __newindex
1339 *
1340 * Like __index, but control property assignment.
1341 *
1342 * __mode - Control weak references. A string value with one or both
1343 * of the characters 'k' and 'v' which specifies that the the
1344 * keys and/or values in the table are weak references.
1345 *
1346 * __call - Treat a table like a function. When a table is followed by
1347 * parenthesis such as "myTable( 'foo' )" and the metatable has
1348 * a __call key pointing to a function, that function is invoked
1349 * (passing any specified arguments) and the return value is
1350 * returned.
1351 *
1352 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1353 * called, if the metatable for myTable has a __metatable
1354 * key, the value of that key is returned instead of the
1355 * actual metatable.
1356 *
1357 * __tostring - Control string representation. When the builtin
1358 * "tostring( myTable )" function is called, if the metatable
1359 * for myTable has a __tostring property set to a function,
1360 * that function is invoked (passing myTable to it) and the
1361 * return value is used as the string representation.
1362 *
1363 * __len - Control table length. When the table length is requested using
1364 * the length operator ( '#' ), if the metatable for myTable has
1365 * a __len key pointing to a function, that function is invoked
1366 * (passing myTable to it) and the return value used as the value
1367 * of "#myTable".
1368 *
1369 * __gc - Userdata finalizer code. When userdata is set to be garbage
1370 * collected, if the metatable has a __gc field pointing to a
1371 * function, that function is first invoked, passing the userdata
1372 * to it. The __gc metamethod is not called for tables.
1373 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1374 *
1375 * Special metamethods for redefining standard operators:
1376 * http://www.lua.org/pil/13.1.html
1377 *
1378 * __add "+"
1379 * __sub "-"
1380 * __mul "*"
1381 * __div "/"
1382 * __unm "!"
1383 * __pow "^"
1384 * __concat ".."
1385 *
1386 * Special methods for redfining standar relations
1387 * http://www.lua.org/pil/13.2.html
1388 *
1389 * __eq "=="
1390 * __lt "<"
1391 * __le "<="
1392 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001393
1394/*
1395 *
1396 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001397 * Class Map
1398 *
1399 *
1400 */
1401
1402/* Returns a struct hlua_map if the stack entry "ud" is
1403 * a class session, otherwise it throws an error.
1404 */
1405__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1406{
1407 return (struct map_descriptor *)MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
1408}
1409
1410/* This function is the map constructor. It don't need
1411 * the class Map object. It creates and return a new Map
1412 * object. It must be called only during "body" or "init"
1413 * context because it process some filesystem accesses.
1414 */
1415__LJMP static int hlua_map_new(struct lua_State *L)
1416{
1417 const char *fn;
1418 int match = PAT_MATCH_STR;
1419 struct sample_conv conv;
1420 const char *file = "";
1421 int line = 0;
1422 lua_Debug ar;
1423 char *err = NULL;
1424 struct arg args[2];
1425
1426 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1427 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1428
1429 fn = MAY_LJMP(luaL_checkstring(L, 1));
1430
1431 if (lua_gettop(L) >= 2) {
1432 match = MAY_LJMP(luaL_checkinteger(L, 2));
1433 if (match < 0 || match >= PAT_MATCH_NUM)
1434 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1435 }
1436
1437 /* Get Lua filename and line number. */
1438 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1439 lua_getinfo(L, "Sl", &ar); /* get info about it */
1440 if (ar.currentline > 0) { /* is there info? */
1441 file = ar.short_src;
1442 line = ar.currentline;
1443 }
1444 }
1445
1446 /* fill fake sample_conv struct. */
1447 conv.kw = ""; /* unused. */
1448 conv.process = NULL; /* unused. */
1449 conv.arg_mask = 0; /* unused. */
1450 conv.val_args = NULL; /* unused. */
1451 conv.out_type = SMP_T_STR;
1452 conv.private = (void *)(long)match;
1453 switch (match) {
1454 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1455 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1456 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1457 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1458 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1459 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1460 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001461 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001462 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1463 default:
1464 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1465 }
1466
1467 /* fill fake args. */
1468 args[0].type = ARGT_STR;
1469 args[0].data.str.str = (char *)fn;
1470 args[1].type = ARGT_STOP;
1471
1472 /* load the map. */
1473 if (!sample_load_map(args, &conv, file, line, &err)) {
1474 /* error case: we cant use luaL_error because we must
1475 * free the err variable.
1476 */
1477 luaL_where(L, 1);
1478 lua_pushfstring(L, "'new': %s.", err);
1479 lua_concat(L, 2);
1480 free(err);
1481 WILL_LJMP(lua_error(L));
1482 }
1483
1484 /* create the lua object. */
1485 lua_newtable(L);
1486 lua_pushlightuserdata(L, args[0].data.map);
1487 lua_rawseti(L, -2, 0);
1488
1489 /* Pop a class Map metatable and affect it to the userdata. */
1490 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1491 lua_setmetatable(L, -2);
1492
1493
1494 return 1;
1495}
1496
1497__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1498{
1499 struct map_descriptor *desc;
1500 struct pattern *pat;
1501 struct sample smp;
1502
1503 MAY_LJMP(check_args(L, 2, "lookup"));
1504 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001505 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001506 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001507 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001508 }
1509 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001510 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001511 smp.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001512 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 +02001513 }
1514
1515 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001516 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001517 if (str)
1518 lua_pushstring(L, "");
1519 else
1520 lua_pushnil(L);
1521 return 1;
1522 }
1523
1524 /* The Lua pattern must return a string, so we can't check the returned type */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001525 lua_pushlstring(L, pat->data->u.str.str, pat->data->u.str.len);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001526 return 1;
1527}
1528
1529__LJMP static int hlua_map_lookup(struct lua_State *L)
1530{
1531 return _hlua_map_lookup(L, 0);
1532}
1533
1534__LJMP static int hlua_map_slookup(struct lua_State *L)
1535{
1536 return _hlua_map_lookup(L, 1);
1537}
1538
1539/*
1540 *
1541 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001542 * Class Socket
1543 *
1544 *
1545 */
1546
1547__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1548{
1549 return (struct hlua_socket *)MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
1550}
1551
1552/* This function is the handler called for each I/O on the established
1553 * connection. It is used for notify space avalaible to send or data
1554 * received.
1555 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001556static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001557{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001558 struct stream_interface *si = appctx->owner;
Willy Tarreau50fe03b2014-11-28 13:59:31 +01001559 struct connection *c = objt_conn(si_opposite(si)->end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001560
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001561 /* If the connection object is not avalaible, close all the
1562 * streams and wakeup everithing waiting for.
1563 */
1564 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001565 si_shutw(si);
1566 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001567 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001568 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1569 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001570 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001571 }
1572
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001573 /* If we cant write, wakeup the pending write signals. */
1574 if (channel_output_closed(si_ic(si)))
1575 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
1576
1577 /* If we cant read, wakeup the pending read signals. */
1578 if (channel_input_closed(si_oc(si)))
1579 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1580
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001581 /* if the connection is not estabkished, inform the stream that we want
1582 * to be notified whenever the connection completes.
1583 */
1584 if (!(c->flags & CO_FL_CONNECTED)) {
1585 si_applet_cant_get(si);
1586 si_applet_cant_put(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001587 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001588 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001589
1590 /* This function is called after the connect. */
1591 appctx->ctx.hlua.connected = 1;
1592
1593 /* Wake the tasks which wants to write if the buffer have avalaible space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001594 if (channel_may_recv(si_ic(si)))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001595 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
1596
1597 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001598 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001599 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1600}
1601
Willy Tarreau87b09662015-04-03 00:22:06 +02001602/* This function is called when the "struct stream" is destroyed.
1603 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001604 * Wake all the pending signals.
1605 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001606static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001607{
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001608 /* Remove my link in the original object. */
1609 if (appctx->ctx.hlua.socket)
1610 appctx->ctx.hlua.socket->s = NULL;
1611
1612 /* Wake all the task waiting for me. */
1613 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1614 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
1615}
1616
1617/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001618 * uses this object. If the stream does not exists, just quit.
1619 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001620 * pending signal can rest in the read and write lists. destroy
1621 * it.
1622 */
1623__LJMP static int hlua_socket_gc(lua_State *L)
1624{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001625 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001626 struct appctx *appctx;
1627
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001628 MAY_LJMP(check_args(L, 1, "__gc"));
1629
1630 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001631 if (!socket->s)
1632 return 0;
1633
Willy Tarreau87b09662015-04-03 00:22:06 +02001634 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001635 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001636 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001637 socket->s = NULL;
1638 appctx->ctx.hlua.socket = NULL;
1639
1640 return 0;
1641}
1642
1643/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001644 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001645 */
1646__LJMP static int hlua_socket_close(lua_State *L)
1647{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001648 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001649 struct appctx *appctx;
1650
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001651 MAY_LJMP(check_args(L, 1, "close"));
1652
1653 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001654 if (!socket->s)
1655 return 0;
1656
Willy Tarreau87b09662015-04-03 00:22:06 +02001657 /* Close the stream and remove the associated stop task. */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001658 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001659 appctx = objt_appctx(socket->s->si[0].end);
1660 appctx->ctx.hlua.socket = NULL;
1661 socket->s = NULL;
1662
1663 return 0;
1664}
1665
1666/* This Lua function assumes that the stack contain three parameters.
1667 * 1 - USERDATA containing a struct socket
1668 * 2 - INTEGER with values of the macro defined below
1669 * If the integer is -1, we must read at most one line.
1670 * If the integer is -2, we ust read all the data until the
1671 * end of the stream.
1672 * If the integer is positive value, we must read a number of
1673 * bytes corresponding to this value.
1674 */
1675#define HLSR_READ_LINE (-1)
1676#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001677__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001678{
1679 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1680 int wanted = lua_tointeger(L, 2);
1681 struct hlua *hlua = hlua_gethlua(L);
1682 struct appctx *appctx;
1683 int len;
1684 int nblk;
1685 char *blk1;
1686 int len1;
1687 char *blk2;
1688 int len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001689 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001690 struct channel *oc;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001691
1692 /* Check if this lua stack is schedulable. */
1693 if (!hlua || !hlua->task)
1694 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1695 "'frontend', 'backend' or 'task'"));
1696
1697 /* check for connection closed. If some data where read, return it. */
1698 if (!socket->s)
1699 goto connection_closed;
1700
Willy Tarreau94aa6172015-03-13 14:19:06 +01001701 oc = &socket->s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001702 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001703 /* Read line. */
Willy Tarreau81389672015-03-10 12:03:52 +01001704 nblk = bo_getline_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;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001709
1710 /* remove final \r\n. */
1711 if (nblk == 1) {
1712 if (blk1[len1-1] == '\n') {
1713 len1--;
1714 skip_at_end++;
1715 if (blk1[len1-1] == '\r') {
1716 len1--;
1717 skip_at_end++;
1718 }
1719 }
1720 }
1721 else {
1722 if (blk2[len2-1] == '\n') {
1723 len2--;
1724 skip_at_end++;
1725 if (blk2[len2-1] == '\r') {
1726 len2--;
1727 skip_at_end++;
1728 }
1729 }
1730 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001731 }
1732
1733 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001734 /* Read all the available data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001735 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001736 if (nblk < 0) /* Connection close. */
1737 goto connection_closed;
1738 if (nblk == 0) /* No data avalaible. */
1739 goto connection_empty;
1740 }
1741
1742 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001743 /* Read a block of data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001744 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001745 if (nblk < 0) /* Connection close. */
1746 goto connection_closed;
1747 if (nblk == 0) /* No data avalaible. */
1748 goto connection_empty;
1749
1750 if (len1 > wanted) {
1751 nblk = 1;
1752 len1 = wanted;
1753 } if (nblk == 2 && len1 + len2 > wanted)
1754 len2 = wanted - len1;
1755 }
1756
1757 len = len1;
1758
1759 luaL_addlstring(&socket->b, blk1, len1);
1760 if (nblk == 2) {
1761 len += len2;
1762 luaL_addlstring(&socket->b, blk2, len2);
1763 }
1764
1765 /* Consume data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001766 bo_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001767
1768 /* Don't wait anything. */
Willy Tarreaude70fa12015-09-26 11:25:05 +02001769 stream_int_notify(&socket->s->si[0]);
1770 stream_int_update_applet(&socket->s->si[0]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001771
1772 /* If the pattern reclaim to read all the data
1773 * in the connection, got out.
1774 */
1775 if (wanted == HLSR_READ_ALL)
1776 goto connection_empty;
1777 else if (wanted >= 0 && len < wanted)
1778 goto connection_empty;
1779
1780 /* Return result. */
1781 luaL_pushresult(&socket->b);
1782 return 1;
1783
1784connection_closed:
1785
1786 /* If the buffer containds data. */
1787 if (socket->b.n > 0) {
1788 luaL_pushresult(&socket->b);
1789 return 1;
1790 }
1791 lua_pushnil(L);
1792 lua_pushstring(L, "connection closed.");
1793 return 2;
1794
1795connection_empty:
1796
1797 appctx = objt_appctx(socket->s->si[0].end);
1798 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_read))
1799 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001800 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001801 return 0;
1802}
1803
1804/* This Lus function gets two parameters. The first one can be string
1805 * or a number. If the string is "*l", the user require one line. If
1806 * the string is "*a", the user require all the content of the stream.
1807 * If the value is a number, the user require a number of bytes equal
1808 * to the value. The default value is "*l" (a line).
1809 *
1810 * This paraeter with a variable type is converted in integer. This
1811 * integer takes this values:
1812 * -1 : read a line
1813 * -2 : read all the stream
1814 * >0 : amount if bytes.
1815 *
1816 * The second parameter is optinal. It contains a string that must be
1817 * concatenated with the read data.
1818 */
1819__LJMP static int hlua_socket_receive(struct lua_State *L)
1820{
1821 int wanted = HLSR_READ_LINE;
1822 const char *pattern;
1823 int type;
1824 char *error;
1825 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001826 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001827
1828 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1829 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1830
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001831 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001832
1833 /* check for pattern. */
1834 if (lua_gettop(L) >= 2) {
1835 type = lua_type(L, 2);
1836 if (type == LUA_TSTRING) {
1837 pattern = lua_tostring(L, 2);
1838 if (strcmp(pattern, "*a") == 0)
1839 wanted = HLSR_READ_ALL;
1840 else if (strcmp(pattern, "*l") == 0)
1841 wanted = HLSR_READ_LINE;
1842 else {
1843 wanted = strtoll(pattern, &error, 10);
1844 if (*error != '\0')
1845 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1846 }
1847 }
1848 else if (type == LUA_TNUMBER) {
1849 wanted = lua_tointeger(L, 2);
1850 if (wanted < 0)
1851 WILL_LJMP(luaL_error(L, "Unsupported size."));
1852 }
1853 }
1854
1855 /* Set pattern. */
1856 lua_pushinteger(L, wanted);
1857 lua_replace(L, 2);
1858
1859 /* init bufffer, and fiil it wih prefix. */
1860 luaL_buffinit(L, &socket->b);
1861
1862 /* Check prefix. */
1863 if (lua_gettop(L) >= 3) {
1864 if (lua_type(L, 3) != LUA_TSTRING)
1865 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1866 pattern = lua_tolstring(L, 3, &len);
1867 luaL_addlstring(&socket->b, pattern, len);
1868 }
1869
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001870 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001871}
1872
1873/* Write the Lua input string in the output buffer.
1874 * This fucntion returns a yield if no space are available.
1875 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001876static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001877{
1878 struct hlua_socket *socket;
1879 struct hlua *hlua = hlua_gethlua(L);
1880 struct appctx *appctx;
1881 size_t buf_len;
1882 const char *buf;
1883 int len;
1884 int send_len;
1885 int sent;
1886
1887 /* Check if this lua stack is schedulable. */
1888 if (!hlua || !hlua->task)
1889 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1890 "'frontend', 'backend' or 'task'"));
1891
1892 /* Get object */
1893 socket = MAY_LJMP(hlua_checksocket(L, 1));
1894 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001895 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001896
1897 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001898 if (!socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001899 lua_pushinteger(L, -1);
1900 return 1;
1901 }
1902
1903 /* Update the input buffer data. */
1904 buf += sent;
1905 send_len = buf_len - sent;
1906
1907 /* All the data are sent. */
1908 if (sent >= buf_len)
1909 return 1; /* Implicitly return the length sent. */
1910
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001911 /* Check if the buffer is avalaible because HAProxy doesn't allocate
1912 * the request buffer if its not required.
1913 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001914 if (socket->s->req.buf->size == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001915 if (!stream_alloc_recv_buffer(&socket->s->req)) {
Willy Tarreau350f4872014-11-28 14:42:25 +01001916 socket->s->si[0].flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001917 goto hlua_socket_write_yield_return;
1918 }
1919 }
1920
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001921 /* Check for avalaible space. */
Willy Tarreau94aa6172015-03-13 14:19:06 +01001922 len = buffer_total_space(socket->s->req.buf);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001923 if (len <= 0)
1924 goto hlua_socket_write_yield_return;
1925
1926 /* send data */
1927 if (len < send_len)
1928 send_len = len;
Willy Tarreau94aa6172015-03-13 14:19:06 +01001929 len = bi_putblk(&socket->s->req, buf+sent, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001930
1931 /* "Not enough space" (-1), "Buffer too little to contain
1932 * the data" (-2) are not expected because the available length
1933 * is tested.
1934 * Other unknown error are also not expected.
1935 */
1936 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01001937 if (len == -1)
Willy Tarreau94aa6172015-03-13 14:19:06 +01001938 socket->s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001939
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001940 MAY_LJMP(hlua_socket_close(L));
1941 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001942 lua_pushinteger(L, -1);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001943 return 1;
1944 }
1945
1946 /* update buffers. */
Willy Tarreaude70fa12015-09-26 11:25:05 +02001947 stream_int_notify(&socket->s->si[0]);
1948 stream_int_update_applet(&socket->s->si[0]);
1949
Willy Tarreau94aa6172015-03-13 14:19:06 +01001950 socket->s->req.rex = TICK_ETERNITY;
1951 socket->s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001952
1953 /* Update length sent. */
1954 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001955 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001956
1957 /* All the data buffer is sent ? */
1958 if (sent + len >= buf_len)
1959 return 1;
1960
1961hlua_socket_write_yield_return:
1962 appctx = objt_appctx(socket->s->si[0].end);
1963 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
1964 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001965 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001966 return 0;
1967}
1968
1969/* This function initiate the send of data. It just check the input
1970 * parameters and push an integer in the Lua stack that contain the
1971 * amount of data writed in the buffer. This is used by the function
1972 * "hlua_socket_write_yield" that can yield.
1973 *
1974 * The Lua function gets between 3 and 4 parameters. The first one is
1975 * the associated object. The second is a string buffer. The third is
1976 * a facultative integer that represents where is the buffer position
1977 * of the start of the data that can send. The first byte is the
1978 * position "1". The default value is "1". The fourth argument is a
1979 * facultative integer that represents where is the buffer position
1980 * of the end of the data that can send. The default is the last byte.
1981 */
1982static int hlua_socket_send(struct lua_State *L)
1983{
1984 int i;
1985 int j;
1986 const char *buf;
1987 size_t buf_len;
1988
1989 /* Check number of arguments. */
1990 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
1991 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
1992
1993 /* Get the string. */
1994 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
1995
1996 /* Get and check j. */
1997 if (lua_gettop(L) == 4) {
1998 j = MAY_LJMP(luaL_checkinteger(L, 4));
1999 if (j < 0)
2000 j = buf_len + j + 1;
2001 if (j > buf_len)
2002 j = buf_len + 1;
2003 lua_pop(L, 1);
2004 }
2005 else
2006 j = buf_len;
2007
2008 /* Get and check i. */
2009 if (lua_gettop(L) == 3) {
2010 i = MAY_LJMP(luaL_checkinteger(L, 3));
2011 if (i < 0)
2012 i = buf_len + i + 1;
2013 if (i > buf_len)
2014 i = buf_len + 1;
2015 lua_pop(L, 1);
2016 } else
2017 i = 1;
2018
2019 /* Check bth i and j. */
2020 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002021 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002022 return 1;
2023 }
2024 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002025 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002026 return 1;
2027 }
2028 if (i == 0)
2029 i = 1;
2030 if (j == 0)
2031 j = 1;
2032
2033 /* Pop the string. */
2034 lua_pop(L, 1);
2035
2036 /* Update the buffer length. */
2037 buf += i - 1;
2038 buf_len = j - i + 1;
2039 lua_pushlstring(L, buf, buf_len);
2040
2041 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002042 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002043
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002044 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002045}
2046
Willy Tarreau22b0a682015-06-17 19:43:49 +02002047#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2049{
2050 static char buffer[SOCKET_INFO_MAX_LEN];
2051 int ret;
2052 int len;
2053 char *p;
2054
2055 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2056 if (ret <= 0) {
2057 lua_pushnil(L);
2058 return 1;
2059 }
2060
2061 if (ret == AF_UNIX) {
2062 lua_pushstring(L, buffer+1);
2063 return 1;
2064 }
2065 else if (ret == AF_INET6) {
2066 buffer[0] = '[';
2067 len = strlen(buffer);
2068 buffer[len] = ']';
2069 len++;
2070 buffer[len] = ':';
2071 len++;
2072 p = buffer;
2073 }
2074 else if (ret == AF_INET) {
2075 p = buffer + 1;
2076 len = strlen(p);
2077 p[len] = ':';
2078 len++;
2079 }
2080 else {
2081 lua_pushnil(L);
2082 return 1;
2083 }
2084
2085 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2086 lua_pushnil(L);
2087 return 1;
2088 }
2089
2090 lua_pushstring(L, p);
2091 return 1;
2092}
2093
2094/* Returns information about the peer of the connection. */
2095__LJMP static int hlua_socket_getpeername(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, "getpeername"));
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_TO_SET)) {
2117 unsigned int salen = sizeof(conn->addr.to);
2118 if (getpeername(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, &salen) == -1) {
2119 lua_pushnil(L);
2120 return 1;
2121 }
2122 conn->flags |= CO_FL_ADDR_TO_SET;
2123 }
2124
2125 return MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2126}
2127
2128/* Returns information about my connection side. */
2129static int hlua_socket_getsockname(struct lua_State *L)
2130{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002131 struct hlua_socket *socket;
2132 struct connection *conn;
2133
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002134 MAY_LJMP(check_args(L, 1, "getsockname"));
2135
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002136 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002137
2138 /* Check if the tcp object is avalaible. */
2139 if (!socket->s) {
2140 lua_pushnil(L);
2141 return 1;
2142 }
2143
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002144 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002145 if (!conn) {
2146 lua_pushnil(L);
2147 return 1;
2148 }
2149
2150 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
2151 unsigned int salen = sizeof(conn->addr.from);
2152 if (getsockname(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, &salen) == -1) {
2153 lua_pushnil(L);
2154 return 1;
2155 }
2156 conn->flags |= CO_FL_ADDR_FROM_SET;
2157 }
2158
2159 return hlua_socket_info(L, &conn->addr.from);
2160}
2161
2162/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002163static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002164 .obj_type = OBJ_TYPE_APPLET,
2165 .name = "<LUA_TCP>",
2166 .fct = hlua_socket_handler,
2167 .release = hlua_socket_release,
2168};
2169
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002170__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002171{
2172 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2173 struct hlua *hlua = hlua_gethlua(L);
2174 struct appctx *appctx;
2175
2176 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002177 if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002178 lua_pushnil(L);
2179 lua_pushstring(L, "Can't connect");
2180 return 2;
2181 }
2182
2183 appctx = objt_appctx(socket->s->si[0].end);
2184
2185 /* Check for connection established. */
2186 if (appctx->ctx.hlua.connected) {
2187 lua_pushinteger(L, 1);
2188 return 1;
2189 }
2190
2191 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2192 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002193 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002194 return 0;
2195}
2196
2197/* This function fail or initite the connection. */
2198__LJMP static int hlua_socket_connect(struct lua_State *L)
2199{
2200 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002201 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002202 const char *ip;
2203 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002204 struct hlua *hlua;
2205 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002206 int low, high;
2207 struct sockaddr_storage *addr;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002208
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002209 if (lua_gettop(L) < 2)
2210 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002211
2212 /* Get args. */
2213 socket = MAY_LJMP(hlua_checksocket(L, 1));
2214 ip = MAY_LJMP(luaL_checkstring(L, 2));
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002215 if (lua_gettop(L) >= 3)
2216 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002217
Willy Tarreau973a5422015-08-05 21:47:23 +02002218 conn = si_alloc_conn(&socket->s->si[1]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002219 if (!conn)
2220 WILL_LJMP(luaL_error(L, "connect: internal error"));
2221
Willy Tarreau3adac082015-09-26 17:51:09 +02002222 /* needed for the connection not to be closed */
2223 conn->target = socket->s->target;
2224
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002225 /* Parse ip address. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002226 addr = str2sa_range(ip, &low, &high, NULL, NULL, NULL, 0);
2227 if (!addr)
2228 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
2229 if (low != high)
2230 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
2231 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002232
2233 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002234 if (low == 0) {
2235 if (conn->addr.to.ss_family == AF_INET) {
2236 if (port == -1)
2237 WILL_LJMP(luaL_error(L, "connect: port missing"));
2238 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2239 } else if (conn->addr.to.ss_family == AF_INET6) {
2240 if (port == -1)
2241 WILL_LJMP(luaL_error(L, "connect: port missing"));
2242 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2243 }
2244 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002245
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002246 hlua = hlua_gethlua(L);
2247 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002248
2249 /* inform the stream that we want to be notified whenever the
2250 * connection completes.
2251 */
2252 si_applet_cant_get(&socket->s->si[0]);
2253 si_applet_cant_put(&socket->s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002254 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002255
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002256 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2257 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002258 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002259
2260 return 0;
2261}
2262
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002263#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002264__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2265{
2266 struct hlua_socket *socket;
2267
2268 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2269 socket = MAY_LJMP(hlua_checksocket(L, 1));
2270 socket->s->target = &socket_ssl.obj_type;
2271 return MAY_LJMP(hlua_socket_connect(L));
2272}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002273#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002274
2275__LJMP static int hlua_socket_setoption(struct lua_State *L)
2276{
2277 return 0;
2278}
2279
2280__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2281{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002282 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002283 int tmout;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002284
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002285 MAY_LJMP(check_args(L, 2, "settimeout"));
2286
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002287 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002288 tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002289
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002290 socket->s->req.rto = tmout;
2291 socket->s->req.wto = tmout;
2292 socket->s->res.rto = tmout;
2293 socket->s->res.wto = tmout;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294
2295 return 0;
2296}
2297
2298__LJMP static int hlua_socket_new(lua_State *L)
2299{
2300 struct hlua_socket *socket;
2301 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002302 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002303 struct stream *strm;
Willy Tarreaud420a972015-04-06 00:39:18 +02002304 struct task *task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002305
2306 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002307 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002308 hlua_pusherror(L, "socket: full stack");
2309 goto out_fail_conf;
2310 }
2311
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002312 /* Create the object: obj[0] = userdata. */
2313 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002314 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002315 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002316 memset(socket, 0, sizeof(*socket));
2317
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002318 /* Check if the various memory pools are intialized. */
Willy Tarreau87b09662015-04-03 00:22:06 +02002319 if (!pool2_stream || !pool2_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002320 hlua_pusherror(L, "socket: uninitialized pools.");
2321 goto out_fail_conf;
2322 }
2323
Willy Tarreau87b09662015-04-03 00:22:06 +02002324 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002325 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2326 lua_setmetatable(L, -2);
2327
Willy Tarreaud420a972015-04-06 00:39:18 +02002328 /* Create the applet context */
2329 appctx = appctx_new(&update_applet);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002330 if (!appctx) {
2331 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002332 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002333 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002334
Willy Tarreaud420a972015-04-06 00:39:18 +02002335 appctx->ctx.hlua.socket = socket;
2336 appctx->ctx.hlua.connected = 0;
2337 LIST_INIT(&appctx->ctx.hlua.wake_on_write);
2338 LIST_INIT(&appctx->ctx.hlua.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002339
Willy Tarreaud420a972015-04-06 00:39:18 +02002340 /* Now create a session, task and stream for this applet */
2341 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2342 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002343 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002344 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002345 }
2346
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002347 task = task_new();
2348 if (!task) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002349 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002350 goto out_fail_task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002351 }
Willy Tarreaud420a972015-04-06 00:39:18 +02002352 task->nice = 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002353
Willy Tarreau73b65ac2015-04-08 18:26:29 +02002354 strm = stream_new(sess, task, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002355 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002356 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002357 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002358 }
2359
Willy Tarreaud420a972015-04-06 00:39:18 +02002360 /* Configure an empty Lua for the stream. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002361 socket->s = strm;
2362 strm->hlua.T = NULL;
2363 strm->hlua.Tref = LUA_REFNIL;
2364 strm->hlua.Mref = LUA_REFNIL;
2365 strm->hlua.nargs = 0;
2366 strm->hlua.flags = 0;
2367 LIST_INIT(&strm->hlua.com);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002368
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002369 /* Configure "right" stream interface. this "si" is used to connect
2370 * and retrieve data from the server. The connection is initialized
2371 * with the "struct server".
2372 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002373 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002374
2375 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002376 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2377 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002379 /* Update statistics counters. */
2380 socket_proxy.feconn++; /* beconn will be increased later */
2381 jobs++;
2382 totalconn++;
2383
2384 /* Return yield waiting for connection. */
2385 return 1;
2386
Willy Tarreaud420a972015-04-06 00:39:18 +02002387 out_fail_stream:
2388 task_free(task);
2389 out_fail_task:
Willy Tarreau11c36242015-04-04 15:54:03 +02002390 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002391 out_fail_sess:
2392 appctx_free(appctx);
2393 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002394 WILL_LJMP(lua_error(L));
2395 return 0;
2396}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002397
2398/*
2399 *
2400 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002401 * Class Channel
2402 *
2403 *
2404 */
2405
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002406/* The state between the channel data and the HTTP parser state can be
2407 * unconsistent, so reset the parser and call it again. Warning, this
2408 * action not revalidate the request and not send a 400 if the modified
2409 * resuest is not valid.
2410 *
2411 * This function never fails. If dir is 0 we are a request, if it is 1
2412 * its a response.
2413 */
2414static void hlua_resynchonize_proto(struct stream *stream, int dir)
2415{
2416 /* Protocol HTTP. */
2417 if (stream->be->mode == PR_MODE_HTTP) {
2418
2419 if (dir == 0)
2420 http_txn_reset_req(stream->txn);
2421 else if (dir == 1)
2422 http_txn_reset_res(stream->txn);
2423
2424 if (stream->txn->hdr_idx.v)
2425 hdr_idx_init(&stream->txn->hdr_idx);
2426
2427 if (dir == 0)
2428 http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx);
2429 else if (dir == 1)
2430 http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx);
2431 }
2432}
2433
2434/* Check the protocole integrity after the Lua manipulations.
2435 * Close the stream and returns 0 if fails, otherwise returns 1.
2436 */
2437static int hlua_check_proto(struct stream *stream, int dir)
2438{
2439 const struct chunk msg = { .len = 0 };
2440
Willy Tarreau9af89f72015-09-26 11:50:08 +02002441 /* Protocol HTTP. The message parsing state must match the request or
2442 * response state. The problem that may happen is that Lua modifies
2443 * the request or response message *after* it was parsed, and corrupted
2444 * it so that it could not be processed anymore. We just need to verify
2445 * if the parser is still expected to run or not.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002446 */
2447 if (stream->be->mode == PR_MODE_HTTP) {
Willy Tarreau9af89f72015-09-26 11:50:08 +02002448 if (dir == 0 &&
2449 !(stream->req.analysers & AN_REQ_WAIT_HTTP) &&
2450 stream->txn->req.msg_state < HTTP_MSG_BODY) {
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002451 stream_int_retnclose(&stream->si[0], &msg);
2452 return 0;
2453 }
Willy Tarreau9af89f72015-09-26 11:50:08 +02002454 else if (dir == 1 &&
2455 !(stream->res.analysers & AN_RES_WAIT_HTTP) &&
2456 stream->txn->rsp.msg_state < HTTP_MSG_BODY) {
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002457 stream_int_retnclose(&stream->si[0], &msg);
2458 return 0;
2459 }
2460 }
2461 return 1;
2462}
2463
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002464/* Returns the struct hlua_channel join to the class channel in the
2465 * stack entry "ud" or throws an argument error.
2466 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002467__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002468{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002469 return (struct channel *)MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002470}
2471
Willy Tarreau47860ed2015-03-10 14:07:50 +01002472/* Pushes the channel onto the top of the stack. If the stask does not have a
2473 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002474 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002475static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002476{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002477 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002478 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002479 return 0;
2480
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002481 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002482 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002483 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002484
2485 /* Pop a class sesison metatable and affect it to the userdata. */
2486 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2487 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002488 return 1;
2489}
2490
2491/* Duplicate all the data present in the input channel and put it
2492 * in a string LUA variables. Returns -1 and push a nil value in
2493 * the stack if the channel is closed and all the data are consumed,
2494 * returns 0 if no data are available, otherwise it returns the length
2495 * of the builded string.
2496 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002497static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002498{
2499 char *blk1;
2500 char *blk2;
2501 int len1;
2502 int len2;
2503 int ret;
2504 luaL_Buffer b;
2505
Willy Tarreau47860ed2015-03-10 14:07:50 +01002506 ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002507 if (unlikely(ret == 0))
2508 return 0;
2509
2510 if (unlikely(ret < 0)) {
2511 lua_pushnil(L);
2512 return -1;
2513 }
2514
2515 luaL_buffinit(L, &b);
2516 luaL_addlstring(&b, blk1, len1);
2517 if (unlikely(ret == 2))
2518 luaL_addlstring(&b, blk2, len2);
2519 luaL_pushresult(&b);
2520
2521 if (unlikely(ret == 2))
2522 return len1 + len2;
2523 return len1;
2524}
2525
2526/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2527 * a yield. This function keep the data in the buffer.
2528 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002529__LJMP static int hlua_channel_dup_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;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002532
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002533 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2534
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002535 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002536 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002537 return 1;
2538}
2539
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002540/* Check arguments for the function "hlua_channel_dup_yield". */
2541__LJMP static int hlua_channel_dup(lua_State *L)
2542{
2543 MAY_LJMP(check_args(L, 1, "dup"));
2544 MAY_LJMP(hlua_checkchannel(L, 1));
2545 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2546}
2547
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002548/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2549 * a yield. This function consumes the data in the buffer. It returns
2550 * a string containing the data or a nil pointer if no data are available
2551 * and the channel is closed.
2552 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002553__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002554{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002555 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002556 int ret;
2557
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002558 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002559
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002560 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002561 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002562 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002563
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002564 if (unlikely(ret == -1))
2565 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002566
Willy Tarreau47860ed2015-03-10 14:07:50 +01002567 chn->buf->i -= ret;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002568 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002569 return 1;
2570}
2571
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002572/* Check arguments for the fucntion "hlua_channel_get_yield". */
2573__LJMP static int hlua_channel_get(lua_State *L)
2574{
2575 MAY_LJMP(check_args(L, 1, "get"));
2576 MAY_LJMP(hlua_checkchannel(L, 1));
2577 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2578}
2579
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002580/* This functions consumes and returns one line. If the channel is closed,
2581 * and the last data does not contains a final '\n', the data are returned
2582 * without the final '\n'. When no more data are avalaible, it returns nil
2583 * value.
2584 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002585__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002586{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002587 char *blk1;
2588 char *blk2;
2589 int len1;
2590 int len2;
2591 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002592 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002593 int ret;
2594 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002595
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002596 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2597
Willy Tarreau47860ed2015-03-10 14:07:50 +01002598 ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002599 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002600 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002601
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002602 if (ret == -1) {
2603 lua_pushnil(L);
2604 return 1;
2605 }
2606
2607 luaL_buffinit(L, &b);
2608 luaL_addlstring(&b, blk1, len1);
2609 len = len1;
2610 if (unlikely(ret == 2)) {
2611 luaL_addlstring(&b, blk2, len2);
2612 len += len2;
2613 }
2614 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002615 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002616 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002617 return 1;
2618}
2619
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002620/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2621__LJMP static int hlua_channel_getline(lua_State *L)
2622{
2623 MAY_LJMP(check_args(L, 1, "getline"));
2624 MAY_LJMP(hlua_checkchannel(L, 1));
2625 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2626}
2627
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002628/* This function takes a string as input, and append it at the
2629 * input side of channel. If the data is too big, but a space
2630 * is probably available after sending some data, the function
2631 * yield. If the data is bigger than the buffer, or if the
2632 * channel is closed, it returns -1. otherwise, it returns the
2633 * amount of data writed.
2634 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002635__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002636{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002637 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002638 size_t len;
2639 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2640 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2641 int ret;
2642 int max;
2643
Willy Tarreau47860ed2015-03-10 14:07:50 +01002644 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002645 if (max > len - l)
2646 max = len - l;
2647
Willy Tarreau47860ed2015-03-10 14:07:50 +01002648 ret = bi_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002649 if (ret == -2 || ret == -3) {
2650 lua_pushinteger(L, -1);
2651 return 1;
2652 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002653 if (ret == -1) {
2654 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002655 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002656 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002657 l += ret;
2658 lua_pop(L, 1);
2659 lua_pushinteger(L, l);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002660 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002661
Willy Tarreau47860ed2015-03-10 14:07:50 +01002662 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2663 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002664 /* There are no space avalaible, and the output buffer is empty.
2665 * in this case, we cannot add more data, so we cannot yield,
2666 * we return the amount of copyied data.
2667 */
2668 return 1;
2669 }
2670 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002671 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002672 return 1;
2673}
2674
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002675/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002676 * of the writed string, or -1 if the channel is closed or if the
2677 * buffer size is too little for the data.
2678 */
2679__LJMP static int hlua_channel_append(lua_State *L)
2680{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002681 size_t len;
2682
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002683 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002684 MAY_LJMP(hlua_checkchannel(L, 1));
2685 MAY_LJMP(luaL_checklstring(L, 2, &len));
2686 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002687 lua_pushinteger(L, 0);
2688
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002689 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002690}
2691
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002692/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002693 * his process by cleaning the buffer. The result is a replacement
2694 * of the current data. It returns the length of the writed string,
2695 * or -1 if the channel is closed or if the buffer size is too
2696 * little for the data.
2697 */
2698__LJMP static int hlua_channel_set(lua_State *L)
2699{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002700 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002701
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002702 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002703 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002704 lua_pushinteger(L, 0);
2705
Willy Tarreau47860ed2015-03-10 14:07:50 +01002706 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002707
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002708 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002709}
2710
2711/* Append data in the output side of the buffer. This data is immediatly
2712 * sent. The fcuntion returns the ammount of data writed. If the buffer
2713 * cannot contains the data, the function yield. The function returns -1
2714 * if the channel is closed.
2715 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002716__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002717{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002718 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002719 size_t len;
2720 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2721 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2722 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002723 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002724
Willy Tarreau47860ed2015-03-10 14:07:50 +01002725 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002726 lua_pushinteger(L, -1);
2727 return 1;
2728 }
2729
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002730 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2731 * the request buffer if its not required.
2732 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002733 if (chn->buf->size == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002734 if (!stream_alloc_recv_buffer(chn)) {
Willy Tarreau47860ed2015-03-10 14:07:50 +01002735 chn_prod(chn)->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002736 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002737 }
2738 }
2739
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002740 /* the writed data will be immediatly sent, so we can check
2741 * the avalaible space without taking in account the reserve.
2742 * The reserve is guaranted for the processing of incoming
2743 * data, because the buffer will be flushed.
2744 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002745 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002746
2747 /* If there are no space avalaible, and the output buffer is empty.
2748 * in this case, we cannot add more data, so we cannot yield,
2749 * we return the amount of copyied data.
2750 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002751 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002752 return 1;
2753
2754 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002755 if (max > len - l)
2756 max = len - l;
2757
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002758 /* The buffer avalaible size may be not contiguous. This test
2759 * detects a non contiguous buffer and realign it.
2760 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002761 if (bi_space_for_replace(chn->buf) < max)
2762 buffer_slow_realign(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002763
2764 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002765 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002766
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002767 /* buffer replace considers that the input part is filled.
2768 * so, I must forward these new data in the output part.
2769 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002770 b_adv(chn->buf, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002771
2772 l += max;
2773 lua_pop(L, 1);
2774 lua_pushinteger(L, l);
2775
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002776 /* If there are no space avalaible, and the output buffer is empty.
2777 * in this case, we cannot add more data, so we cannot yield,
2778 * we return the amount of copyied data.
2779 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002780 max = chn->buf->size - buffer_len(chn->buf);
2781 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002782 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002783
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002784 if (l < len) {
2785 /* If we are waiting for space in the response buffer, we
2786 * must set the flag WAKERESWR. This flag required the task
2787 * wake up if any activity is detected on the response buffer.
2788 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002789 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002790 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002791 else
2792 HLUA_SET_WAKEREQWR(hlua);
2793 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002794 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002795
2796 return 1;
2797}
2798
2799/* Just a wraper of "_hlua_channel_send". This wrapper permits
2800 * yield the LUA process, and resume it without checking the
2801 * input arguments.
2802 */
2803__LJMP static int hlua_channel_send(lua_State *L)
2804{
2805 MAY_LJMP(check_args(L, 2, "send"));
2806 lua_pushinteger(L, 0);
2807
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002808 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002809}
2810
2811/* This function forward and amount of butes. The data pass from
2812 * the input side of the buffer to the output side, and can be
2813 * forwarded. This function never fails.
2814 *
2815 * The Lua function takes an amount of bytes to be forwarded in
2816 * imput. It returns the number of bytes forwarded.
2817 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002818__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002819{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002820 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002821 int len;
2822 int l;
2823 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002824 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002825
2826 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2827 len = MAY_LJMP(luaL_checkinteger(L, 2));
2828 l = MAY_LJMP(luaL_checkinteger(L, -1));
2829
2830 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002831 if (max > chn->buf->i)
2832 max = chn->buf->i;
2833 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002834 l += max;
2835
2836 lua_pop(L, 1);
2837 lua_pushinteger(L, l);
2838
2839 /* Check if it miss bytes to forward. */
2840 if (l < len) {
2841 /* The the input channel or the output channel are closed, we
2842 * must return the amount of data forwarded.
2843 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002844 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002845 return 1;
2846
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002847 /* If we are waiting for space data in the response buffer, we
2848 * must set the flag WAKERESWR. This flag required the task
2849 * wake up if any activity is detected on the response buffer.
2850 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002851 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002852 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002853 else
2854 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002855
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002856 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002857 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002858 }
2859
2860 return 1;
2861}
2862
2863/* Just check the input and prepare the stack for the previous
2864 * function "hlua_channel_forward_yield"
2865 */
2866__LJMP static int hlua_channel_forward(lua_State *L)
2867{
2868 MAY_LJMP(check_args(L, 2, "forward"));
2869 MAY_LJMP(hlua_checkchannel(L, 1));
2870 MAY_LJMP(luaL_checkinteger(L, 2));
2871
2872 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002873 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002874}
2875
2876/* Just returns the number of bytes available in the input
2877 * side of the buffer. This function never fails.
2878 */
2879__LJMP static int hlua_channel_get_in_len(lua_State *L)
2880{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002881 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002882
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002883 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002884 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002885 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002886 return 1;
2887}
2888
2889/* Just returns the number of bytes available in the output
2890 * side of the buffer. This function never fails.
2891 */
2892__LJMP static int hlua_channel_get_out_len(lua_State *L)
2893{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002894 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002895
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002896 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002897 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002898 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002899 return 1;
2900}
2901
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002902/*
2903 *
2904 *
2905 * Class Fetches
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 FOURNIERbb53c7b2015-03-11 18:28:02 +01002912 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002913__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002914{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002915 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002916}
2917
2918/* This function creates and push in the stack a fetch object according
2919 * with a current TXN.
2920 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002921static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002922{
Willy Tarreau7073c472015-04-06 11:15:40 +02002923 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +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 Fetches object is the
2931 * transaction object.
2932 */
2933 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002934 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +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 FOURNIERbb53c7b2015-03-11 18:28:02 +01002940
2941 /* Pop a class sesison metatable and affect it to the userdata. */
2942 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
2943 lua_setmetatable(L, -2);
2944
2945 return 1;
2946}
2947
2948/* This function is an LUA binding. It is called with each sample-fetch.
2949 * It uses closure argument to store the associated sample-fetch. 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 "sample-fetch" function fails, nil is returned.
2953 */
2954__LJMP static int hlua_run_sample_fetch(lua_State *L)
2955{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002956 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01002957 struct sample_fetch *f;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002958 struct arg args[ARGM_NBARGS + 1];
2959 int i;
2960 struct sample smp;
2961
2962 /* Get closure arguments. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002963 f = (struct sample_fetch *)lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002964
2965 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002966 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002967
2968 /* Get extra arguments. */
2969 for (i = 0; i < lua_gettop(L) - 1; i++) {
2970 if (i >= ARGM_NBARGS)
2971 break;
2972 hlua_lua2arg(L, i + 2, &args[i]);
2973 }
2974 args[i].type = ARGT_STOP;
2975
2976 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002977 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002978
2979 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002980 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002981 lua_pushfstring(L, "error in arguments");
2982 WILL_LJMP(lua_error(L));
2983 }
2984
2985 /* Initialise the sample. */
2986 memset(&smp, 0, sizeof(smp));
2987
2988 /* Run the sample fetch process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02002989 smp.px = hsmp->p;
2990 smp.sess = hsmp->s->sess;
2991 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02002992 smp.opt = 0;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002993 if (!f->process(args, &smp, f->kw, f->private)) {
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002994 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002995 lua_pushstring(L, "");
2996 else
2997 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002998 return 1;
2999 }
3000
3001 /* Convert the returned sample in lua value. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003002 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003003 hlua_smp2lua_str(L, &smp);
3004 else
3005 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003006 return 1;
3007}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003008
3009/*
3010 *
3011 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003012 * Class Converters
3013 *
3014 *
3015 */
3016
3017/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003018 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003019 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003020__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003021{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003022 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003023}
3024
3025/* This function creates and push in the stack a Converters object
3026 * according with a current TXN.
3027 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003028static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003029{
Willy Tarreau7073c472015-04-06 11:15:40 +02003030 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003031
3032 /* Check stack size. */
3033 if (!lua_checkstack(L, 3))
3034 return 0;
3035
3036 /* Create the object: obj[0] = userdata.
3037 * Note that the base of the Converters object is the
3038 * same than the TXN object.
3039 */
3040 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003041 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003042 lua_rawseti(L, -2, 0);
3043
Willy Tarreau7073c472015-04-06 11:15:40 +02003044 hsmp->s = txn->s;
3045 hsmp->p = txn->p;
Willy Tarreau7073c472015-04-06 11:15:40 +02003046 hsmp->stringsafe = stringsafe;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003047
Willy Tarreau87b09662015-04-03 00:22:06 +02003048 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003049 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3050 lua_setmetatable(L, -2);
3051
3052 return 1;
3053}
3054
3055/* This function is an LUA binding. It is called with each converter.
3056 * It uses closure argument to store the associated converter. It
3057 * returns only one argument or throws an error. An error is thrown
3058 * only if an error is encountered during the argument parsing. If
3059 * the converter function function fails, nil is returned.
3060 */
3061__LJMP static int hlua_run_sample_conv(lua_State *L)
3062{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003063 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003064 struct sample_conv *conv;
3065 struct arg args[ARGM_NBARGS + 1];
3066 int i;
3067 struct sample smp;
3068
3069 /* Get closure arguments. */
3070 conv = (struct sample_conv *)lua_touserdata(L, lua_upvalueindex(1));
3071
3072 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003073 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003074
3075 /* Get extra arguments. */
3076 for (i = 0; i < lua_gettop(L) - 2; i++) {
3077 if (i >= ARGM_NBARGS)
3078 break;
3079 hlua_lua2arg(L, i + 3, &args[i]);
3080 }
3081 args[i].type = ARGT_STOP;
3082
3083 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003084 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003085
3086 /* Run the special args checker. */
3087 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3088 hlua_pusherror(L, "error in arguments");
3089 WILL_LJMP(lua_error(L));
3090 }
3091
3092 /* Initialise the sample. */
3093 if (!hlua_lua2smp(L, 2, &smp)) {
3094 hlua_pusherror(L, "error in the input argument");
3095 WILL_LJMP(lua_error(L));
3096 }
3097
3098 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003099 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003100 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003101 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003102 WILL_LJMP(lua_error(L));
3103 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003104 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3105 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003106 hlua_pusherror(L, "error during the input argument casting");
3107 WILL_LJMP(lua_error(L));
3108 }
3109
3110 /* Run the sample conversion process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02003111 smp.px = hsmp->p;
3112 smp.sess = hsmp->s->sess;
3113 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02003114 smp.opt = 0;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003115 if (!conv->process(args, &smp, conv->private)) {
Willy Tarreauda5f1082015-04-06 11:17:13 +02003116 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003117 lua_pushstring(L, "");
3118 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003119 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003120 return 1;
3121 }
3122
3123 /* Convert the returned sample in lua value. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003124 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003125 hlua_smp2lua_str(L, &smp);
3126 else
3127 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003128 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003129}
3130
3131/*
3132 *
3133 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003134 * Class HTTP
3135 *
3136 *
3137 */
3138
3139/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003140 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003141 */
3142__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
3143{
3144 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
3145}
3146
3147/* This function creates and push in the stack a HTTP object
3148 * according with a current TXN.
3149 */
3150static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
3151{
Willy Tarreau9a8ad862015-04-06 11:14:06 +02003152 struct hlua_txn *htxn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003153
3154 /* Check stack size. */
3155 if (!lua_checkstack(L, 3))
3156 return 0;
3157
3158 /* Create the object: obj[0] = userdata.
3159 * Note that the base of the Converters object is the
3160 * same than the TXN object.
3161 */
3162 lua_newtable(L);
Willy Tarreau9a8ad862015-04-06 11:14:06 +02003163 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003164 lua_rawseti(L, -2, 0);
3165
Willy Tarreau9a8ad862015-04-06 11:14:06 +02003166 htxn->s = txn->s;
3167 htxn->p = txn->p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003168
Willy Tarreau87b09662015-04-03 00:22:06 +02003169 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003170 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
3171 lua_setmetatable(L, -2);
3172
3173 return 1;
3174}
3175
3176/* This function creates ans returns an array of HTTP headers.
3177 * This function does not fails. It is used as wrapper with the
3178 * 2 following functions.
3179 */
3180__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3181{
3182 const char *cur_ptr, *cur_next, *p;
3183 int old_idx, cur_idx;
3184 struct hdr_idx_elem *cur_hdr;
3185 const char *hn, *hv;
3186 int hnl, hvl;
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003187 int type;
3188 const char *in;
3189 char *out;
3190 int len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003191
3192 /* Create the table. */
3193 lua_newtable(L);
3194
Willy Tarreaueee5b512015-04-03 23:46:31 +02003195 if (!htxn->s->txn)
3196 return 1;
3197
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003198 /* Build array of headers. */
3199 old_idx = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003200 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003201
3202 while (1) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02003203 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003204 if (!cur_idx)
3205 break;
3206 old_idx = cur_idx;
3207
Willy Tarreaueee5b512015-04-03 23:46:31 +02003208 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003209 cur_ptr = cur_next;
3210 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
3211
3212 /* Now we have one full header at cur_ptr of len cur_hdr->len,
3213 * and the next header starts at cur_next. We'll check
3214 * this header in the list as well as against the default
3215 * rule.
3216 */
3217
3218 /* look for ': *'. */
3219 hn = cur_ptr;
3220 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
3221 if (p >= cur_ptr+cur_hdr->len)
3222 continue;
3223 hnl = p - hn;
3224 p++;
3225 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
3226 p++;
3227 if (p >= cur_ptr+cur_hdr->len)
3228 continue;
3229 hv = p;
3230 hvl = cur_ptr+cur_hdr->len-p;
3231
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003232 /* Lowercase the key. Don't check the size of trash, it have
3233 * the size of one buffer and the input data contains in one
3234 * buffer.
3235 */
3236 out = trash.str;
3237 for (in=hn; in<hn+hnl; in++, out++)
3238 *out = tolower(*in);
3239 *out = '\0';
3240
3241 /* Check for existing entry:
3242 * assume that the table is on the top of the stack, and
3243 * push the key in the stack, the function lua_gettable()
3244 * perform the lookup.
3245 */
3246 lua_pushlstring(L, trash.str, hnl);
3247 lua_gettable(L, -2);
3248 type = lua_type(L, -1);
3249
3250 switch (type) {
3251 case LUA_TNIL:
3252 /* Table not found, create it. */
3253 lua_pop(L, 1); /* remove the nil value. */
3254 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
3255 lua_newtable(L); /* create and push empty table. */
3256 lua_pushlstring(L, hv, hvl); /* push header value. */
3257 lua_rawseti(L, -2, 0); /* index header value (pop it). */
3258 lua_rawset(L, -3); /* index new table with header name (pop the values). */
3259 break;
3260
3261 case LUA_TTABLE:
3262 /* Entry found: push the value in the table. */
3263 len = lua_rawlen(L, -1);
3264 lua_pushlstring(L, hv, hvl); /* push header value. */
3265 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
3266 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
3267 break;
3268
3269 default:
3270 /* Other cases are errors. */
3271 hlua_pusherror(L, "internal error during the parsing of headers.");
3272 WILL_LJMP(lua_error(L));
3273 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003274 }
3275
3276 return 1;
3277}
3278
3279__LJMP static int hlua_http_req_get_headers(lua_State *L)
3280{
3281 struct hlua_txn *htxn;
3282
3283 MAY_LJMP(check_args(L, 1, "req_get_headers"));
3284 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3285
Willy Tarreaueee5b512015-04-03 23:46:31 +02003286 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003287}
3288
3289__LJMP static int hlua_http_res_get_headers(lua_State *L)
3290{
3291 struct hlua_txn *htxn;
3292
3293 MAY_LJMP(check_args(L, 1, "res_get_headers"));
3294 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3295
Willy Tarreaueee5b512015-04-03 23:46:31 +02003296 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003297}
3298
3299/* This function replace full header, or just a value in
3300 * the request or in the response. It is a wrapper fir the
3301 * 4 following functions.
3302 */
3303__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
3304 struct http_msg *msg, int action)
3305{
3306 size_t name_len;
3307 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3308 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
3309 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
3310 struct my_regex re;
3311
3312 if (!regex_comp(reg, &re, 1, 1, NULL))
3313 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
3314
3315 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
3316 regex_free(&re);
3317 return 0;
3318}
3319
3320__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
3321{
3322 struct hlua_txn *htxn;
3323
3324 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3325 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3326
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003327 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003328}
3329
3330__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
3331{
3332 struct hlua_txn *htxn;
3333
3334 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
3335 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3336
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003337 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003338}
3339
3340__LJMP static int hlua_http_req_rep_val(lua_State *L)
3341{
3342 struct hlua_txn *htxn;
3343
3344 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3345 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3346
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003347 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003348}
3349
3350__LJMP static int hlua_http_res_rep_val(lua_State *L)
3351{
3352 struct hlua_txn *htxn;
3353
3354 MAY_LJMP(check_args(L, 4, "res_rep_val"));
3355 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3356
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003357 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003358}
3359
3360/* This function deletes all the occurences of an header.
3361 * It is a wrapper for the 2 following functions.
3362 */
3363__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3364{
3365 size_t len;
3366 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3367 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003368 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003369
3370 ctx.idx = 0;
3371 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
3372 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3373 return 0;
3374}
3375
3376__LJMP static int hlua_http_req_del_hdr(lua_State *L)
3377{
3378 struct hlua_txn *htxn;
3379
3380 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3381 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3382
Willy Tarreaueee5b512015-04-03 23:46:31 +02003383 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003384}
3385
3386__LJMP static int hlua_http_res_del_hdr(lua_State *L)
3387{
3388 struct hlua_txn *htxn;
3389
3390 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3391 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3392
Willy Tarreaueee5b512015-04-03 23:46:31 +02003393 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003394}
3395
3396/* This function adds an header. It is a wrapper used by
3397 * the 2 following functions.
3398 */
3399__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3400{
3401 size_t name_len;
3402 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3403 size_t value_len;
3404 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
3405 char *p;
3406
3407 /* Check length. */
3408 trash.len = value_len + name_len + 2;
3409 if (trash.len > trash.size)
3410 return 0;
3411
3412 /* Creates the header string. */
3413 p = trash.str;
3414 memcpy(p, name, name_len);
3415 p += name_len;
3416 *p = ':';
3417 p++;
3418 *p = ' ';
3419 p++;
3420 memcpy(p, value, value_len);
3421
Willy Tarreaueee5b512015-04-03 23:46:31 +02003422 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003423 trash.str, trash.len) != 0);
3424
3425 return 0;
3426}
3427
3428__LJMP static int hlua_http_req_add_hdr(lua_State *L)
3429{
3430 struct hlua_txn *htxn;
3431
3432 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
3433 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3434
Willy Tarreaueee5b512015-04-03 23:46:31 +02003435 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003436}
3437
3438__LJMP static int hlua_http_res_add_hdr(lua_State *L)
3439{
3440 struct hlua_txn *htxn;
3441
3442 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
3443 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3444
Willy Tarreaueee5b512015-04-03 23:46:31 +02003445 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003446}
3447
3448static int hlua_http_req_set_hdr(lua_State *L)
3449{
3450 struct hlua_txn *htxn;
3451
3452 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
3453 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3454
Willy Tarreaueee5b512015-04-03 23:46:31 +02003455 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
3456 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003457}
3458
3459static int hlua_http_res_set_hdr(lua_State *L)
3460{
3461 struct hlua_txn *htxn;
3462
3463 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
3464 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3465
Willy Tarreaueee5b512015-04-03 23:46:31 +02003466 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
3467 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003468}
3469
3470/* This function set the method. */
3471static int hlua_http_req_set_meth(lua_State *L)
3472{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003473 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003474 size_t name_len;
3475 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003476
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003477 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003478 return 1;
3479}
3480
3481/* This function set the method. */
3482static int hlua_http_req_set_path(lua_State *L)
3483{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003484 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003485 size_t name_len;
3486 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003487 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003488 return 1;
3489}
3490
3491/* This function set the query-string. */
3492static int hlua_http_req_set_query(lua_State *L)
3493{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003494 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003495 size_t name_len;
3496 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003497
3498 /* Check length. */
3499 if (name_len > trash.size - 1) {
3500 lua_pushboolean(L, 0);
3501 return 1;
3502 }
3503
3504 /* Add the mark question as prefix. */
3505 chunk_reset(&trash);
3506 trash.str[trash.len++] = '?';
3507 memcpy(trash.str + trash.len, name, name_len);
3508 trash.len += name_len;
3509
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003510 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003511 return 1;
3512}
3513
3514/* This function set the uri. */
3515static int hlua_http_req_set_uri(lua_State *L)
3516{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003517 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003518 size_t name_len;
3519 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003520
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003521 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003522 return 1;
3523}
3524
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02003525/* This function set the response code. */
3526static int hlua_http_res_set_status(lua_State *L)
3527{
3528 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3529 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
3530
3531 http_set_status(code, htxn->s);
3532 return 0;
3533}
3534
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003535/*
3536 *
3537 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003538 * Class TXN
3539 *
3540 *
3541 */
3542
3543/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003544 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003545 */
3546__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
3547{
3548 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
3549}
3550
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02003551__LJMP static int hlua_set_var(lua_State *L)
3552{
3553 struct hlua_txn *htxn;
3554 const char *name;
3555 size_t len;
3556 struct sample smp;
3557
3558 MAY_LJMP(check_args(L, 3, "set_var"));
3559
3560 /* It is useles to retrieve the stream, but this function
3561 * runs only in a stream context.
3562 */
3563 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3564 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3565
3566 /* Converts the third argument in a sample. */
3567 hlua_lua2smp(L, 3, &smp);
3568
3569 /* Store the sample in a variable. */
3570 vars_set_by_name(name, len, htxn->s, &smp);
3571 return 0;
3572}
3573
3574__LJMP static int hlua_get_var(lua_State *L)
3575{
3576 struct hlua_txn *htxn;
3577 const char *name;
3578 size_t len;
3579 struct sample smp;
3580
3581 MAY_LJMP(check_args(L, 2, "get_var"));
3582
3583 /* It is useles to retrieve the stream, but this function
3584 * runs only in a stream context.
3585 */
3586 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3587 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3588
3589 if (!vars_get_by_name(name, len, htxn->s, &smp)) {
3590 lua_pushnil(L);
3591 return 1;
3592 }
3593
3594 return hlua_smp2lua(L, &smp);
3595}
3596
Willy Tarreau59551662015-03-10 14:23:13 +01003597__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003598{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003599 struct hlua *hlua;
3600
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003601 MAY_LJMP(check_args(L, 2, "set_priv"));
3602
Willy Tarreau87b09662015-04-03 00:22:06 +02003603 /* It is useles to retrieve the stream, but this function
3604 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003605 */
3606 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003607 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003608
3609 /* Remove previous value. */
3610 if (hlua->Mref != -1)
3611 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
3612
3613 /* Get and store new value. */
3614 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3615 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3616
3617 return 0;
3618}
3619
Willy Tarreau59551662015-03-10 14:23:13 +01003620__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003621{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003622 struct hlua *hlua;
3623
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003624 MAY_LJMP(check_args(L, 1, "get_priv"));
3625
Willy Tarreau87b09662015-04-03 00:22:06 +02003626 /* It is useles to retrieve the stream, but this function
3627 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003628 */
3629 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003630 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003631
3632 /* Push configuration index in the stack. */
3633 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3634
3635 return 1;
3636}
3637
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003638/* Create stack entry containing a class TXN. This function
3639 * return 0 if the stack does not contains free slots,
3640 * otherwise it returns 1.
3641 */
Willy Tarreau15e91e12015-04-04 00:52:09 +02003642static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003643{
Willy Tarreaude491382015-04-06 11:04:28 +02003644 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003645
3646 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003647 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003648 return 0;
3649
3650 /* NOTE: The allocation never fails. The failure
3651 * throw an error, and the function never returns.
3652 * if the throw is not avalaible, the process is aborted.
3653 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003654 /* Create the object: obj[0] = userdata. */
3655 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02003656 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003657 lua_rawseti(L, -2, 0);
3658
Willy Tarreaude491382015-04-06 11:04:28 +02003659 htxn->s = s;
3660 htxn->p = p;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003661
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003662 /* Create the "f" field that contains a list of fetches. */
3663 lua_pushstring(L, "f");
Willy Tarreaude491382015-04-06 11:04:28 +02003664 if (!hlua_fetches_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003665 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003666 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003667
3668 /* Create the "sf" field that contains a list of stringsafe fetches. */
3669 lua_pushstring(L, "sf");
Willy Tarreaude491382015-04-06 11:04:28 +02003670 if (!hlua_fetches_new(L, htxn, 1))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003671 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003672 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003673
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003674 /* Create the "c" field that contains a list of converters. */
3675 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02003676 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003677 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003678 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003679
3680 /* Create the "sc" field that contains a list of stringsafe converters. */
3681 lua_pushstring(L, "sc");
Willy Tarreaude491382015-04-06 11:04:28 +02003682 if (!hlua_converters_new(L, htxn, 1))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003683 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003684 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003685
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003686 /* Create the "req" field that contains the request channel object. */
3687 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003688 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003689 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003690 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003691
3692 /* Create the "res" field that contains the response channel object. */
3693 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003694 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003695 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003696 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003697
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003698 /* Creates the HTTP object is the current proxy allows http. */
3699 lua_pushstring(L, "http");
3700 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02003701 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003702 return 0;
3703 }
3704 else
3705 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003706 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003707
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003708 /* Pop a class sesison metatable and affect it to the userdata. */
3709 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
3710 lua_setmetatable(L, -2);
3711
3712 return 1;
3713}
3714
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003715__LJMP static int hlua_txn_deflog(lua_State *L)
3716{
3717 const char *msg;
3718 struct hlua_txn *htxn;
3719
3720 MAY_LJMP(check_args(L, 2, "deflog"));
3721 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3722 msg = MAY_LJMP(luaL_checkstring(L, 2));
3723
3724 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
3725 return 0;
3726}
3727
3728__LJMP static int hlua_txn_log(lua_State *L)
3729{
3730 int level;
3731 const char *msg;
3732 struct hlua_txn *htxn;
3733
3734 MAY_LJMP(check_args(L, 3, "log"));
3735 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3736 level = MAY_LJMP(luaL_checkinteger(L, 2));
3737 msg = MAY_LJMP(luaL_checkstring(L, 3));
3738
3739 if (level < 0 || level >= NB_LOG_LEVELS)
3740 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3741
3742 hlua_sendlog(htxn->s->be, level, msg);
3743 return 0;
3744}
3745
3746__LJMP static int hlua_txn_log_debug(lua_State *L)
3747{
3748 const char *msg;
3749 struct hlua_txn *htxn;
3750
3751 MAY_LJMP(check_args(L, 2, "Debug"));
3752 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3753 msg = MAY_LJMP(luaL_checkstring(L, 2));
3754 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
3755 return 0;
3756}
3757
3758__LJMP static int hlua_txn_log_info(lua_State *L)
3759{
3760 const char *msg;
3761 struct hlua_txn *htxn;
3762
3763 MAY_LJMP(check_args(L, 2, "Info"));
3764 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3765 msg = MAY_LJMP(luaL_checkstring(L, 2));
3766 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
3767 return 0;
3768}
3769
3770__LJMP static int hlua_txn_log_warning(lua_State *L)
3771{
3772 const char *msg;
3773 struct hlua_txn *htxn;
3774
3775 MAY_LJMP(check_args(L, 2, "Warning"));
3776 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3777 msg = MAY_LJMP(luaL_checkstring(L, 2));
3778 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
3779 return 0;
3780}
3781
3782__LJMP static int hlua_txn_log_alert(lua_State *L)
3783{
3784 const char *msg;
3785 struct hlua_txn *htxn;
3786
3787 MAY_LJMP(check_args(L, 2, "Alert"));
3788 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3789 msg = MAY_LJMP(luaL_checkstring(L, 2));
3790 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
3791 return 0;
3792}
3793
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003794__LJMP static int hlua_txn_set_loglevel(lua_State *L)
3795{
3796 struct hlua_txn *htxn;
3797 int ll;
3798
3799 MAY_LJMP(check_args(L, 2, "set_loglevel"));
3800 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3801 ll = MAY_LJMP(luaL_checkinteger(L, 2));
3802
3803 if (ll < 0 || ll > 7)
3804 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
3805
3806 htxn->s->logs.level = ll;
3807 return 0;
3808}
3809
3810__LJMP static int hlua_txn_set_tos(lua_State *L)
3811{
3812 struct hlua_txn *htxn;
3813 struct connection *cli_conn;
3814 int tos;
3815
3816 MAY_LJMP(check_args(L, 2, "set_tos"));
3817 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3818 tos = MAY_LJMP(luaL_checkinteger(L, 2));
3819
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003820 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003821 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, tos);
3822
3823 return 0;
3824}
3825
3826__LJMP static int hlua_txn_set_mark(lua_State *L)
3827{
3828#ifdef SO_MARK
3829 struct hlua_txn *htxn;
3830 struct connection *cli_conn;
3831 int mark;
3832
3833 MAY_LJMP(check_args(L, 2, "set_mark"));
3834 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3835 mark = MAY_LJMP(luaL_checkinteger(L, 2));
3836
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003837 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau07081fe2015-04-06 10:59:20 +02003838 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003839#endif
3840 return 0;
3841}
3842
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003843/* This function is an Lua binding that send pending data
3844 * to the client, and close the stream interface.
3845 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02003846__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003847{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003848 struct hlua_txn *htxn;
Willy Tarreau81389672015-03-10 12:03:52 +01003849 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003850
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003851 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003852 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003853
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003854 ic = &htxn->s->req;
3855 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01003856
Willy Tarreau630ef452015-08-28 10:06:15 +02003857 if (htxn->s->txn) {
3858 /* HTTP mode, let's stay in sync with the stream */
3859 bi_fast_delete(ic->buf, htxn->s->txn->req.sov);
3860 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
3861 htxn->s->txn->req.sov = 0;
3862 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
3863 oc->analysers = AN_RES_HTTP_XFER_BODY;
3864 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
3865 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
3866
3867 /* Trim any possible response */
3868 oc->buf->i = 0;
3869 htxn->s->txn->rsp.next = htxn->s->txn->rsp.sov = 0;
3870
3871 /* Note that if we want to support keep-alive, we need
3872 * to bypass the close/shutr_now calls below, but that
3873 * may only be done if the HTTP request was already
3874 * processed and the connection header is known (ie
3875 * not during TCP rules).
3876 */
3877 }
3878
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02003879 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01003880 channel_abort(ic);
3881 channel_auto_close(ic);
3882 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02003883
3884 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01003885 channel_auto_read(oc);
3886 channel_auto_close(oc);
3887 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003888
Willy Tarreau0458b082015-08-28 09:40:04 +02003889 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02003890
3891 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003892 return 0;
3893}
3894
3895__LJMP static int hlua_log(lua_State *L)
3896{
3897 int level;
3898 const char *msg;
3899
3900 MAY_LJMP(check_args(L, 2, "log"));
3901 level = MAY_LJMP(luaL_checkinteger(L, 1));
3902 msg = MAY_LJMP(luaL_checkstring(L, 2));
3903
3904 if (level < 0 || level >= NB_LOG_LEVELS)
3905 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3906
3907 hlua_sendlog(NULL, level, msg);
3908 return 0;
3909}
3910
3911__LJMP static int hlua_log_debug(lua_State *L)
3912{
3913 const char *msg;
3914
3915 MAY_LJMP(check_args(L, 1, "debug"));
3916 msg = MAY_LJMP(luaL_checkstring(L, 1));
3917 hlua_sendlog(NULL, LOG_DEBUG, msg);
3918 return 0;
3919}
3920
3921__LJMP static int hlua_log_info(lua_State *L)
3922{
3923 const char *msg;
3924
3925 MAY_LJMP(check_args(L, 1, "info"));
3926 msg = MAY_LJMP(luaL_checkstring(L, 1));
3927 hlua_sendlog(NULL, LOG_INFO, msg);
3928 return 0;
3929}
3930
3931__LJMP static int hlua_log_warning(lua_State *L)
3932{
3933 const char *msg;
3934
3935 MAY_LJMP(check_args(L, 1, "warning"));
3936 msg = MAY_LJMP(luaL_checkstring(L, 1));
3937 hlua_sendlog(NULL, LOG_WARNING, msg);
3938 return 0;
3939}
3940
3941__LJMP static int hlua_log_alert(lua_State *L)
3942{
3943 const char *msg;
3944
3945 MAY_LJMP(check_args(L, 1, "alert"));
3946 msg = MAY_LJMP(luaL_checkstring(L, 1));
3947 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003948 return 0;
3949}
3950
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003951__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003952{
3953 int wakeup_ms = lua_tointeger(L, -1);
3954 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003955 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003956 return 0;
3957}
3958
3959__LJMP static int hlua_sleep(lua_State *L)
3960{
3961 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003962 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003963
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003964 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003965
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003966 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003967 wakeup_ms = tick_add(now_ms, delay);
3968 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003969
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003970 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3971 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003972}
3973
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003974__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003975{
3976 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003977 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003978
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003979 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003980
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003981 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003982 wakeup_ms = tick_add(now_ms, delay);
3983 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003984
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003985 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3986 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003987}
3988
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003989/* This functionis an LUA binding. it permits to give back
3990 * the hand at the HAProxy scheduler. It is used when the
3991 * LUA processing consumes a lot of time.
3992 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003993__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003994{
3995 return 0;
3996}
3997
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003998__LJMP static int hlua_yield(lua_State *L)
3999{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01004000 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
4001 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01004002}
4003
Thierry FOURNIER37196f42015-02-16 19:34:56 +01004004/* This function change the nice of the currently executed
4005 * task. It is used set low or high priority at the current
4006 * task.
4007 */
Willy Tarreau59551662015-03-10 14:23:13 +01004008__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01004009{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004010 struct hlua *hlua;
4011 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01004012
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004013 MAY_LJMP(check_args(L, 1, "set_nice"));
4014 hlua = hlua_gethlua(L);
4015 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01004016
4017 /* If he task is not set, I'm in a start mode. */
4018 if (!hlua || !hlua->task)
4019 return 0;
4020
4021 if (nice < -1024)
4022 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004023 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01004024 nice = 1024;
4025
4026 hlua->task->nice = nice;
4027 return 0;
4028}
4029
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004030/* This function is used as a calback of a task. It is called by the
4031 * HAProxy task subsystem when the task is awaked. The LUA runtime can
4032 * return an E_AGAIN signal, the emmiter of this signal must set a
4033 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004034 *
4035 * Task wrapper are longjmp safe because the only one Lua code
4036 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004037 */
4038static struct task *hlua_process_task(struct task *task)
4039{
4040 struct hlua *hlua = task->context;
4041 enum hlua_exec status;
4042
4043 /* We need to remove the task from the wait queue before executing
4044 * the Lua code because we don't know if it needs to wait for
4045 * another timer or not in the case of E_AGAIN.
4046 */
4047 task_delete(task);
4048
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004049 /* If it is the first call to the task, we must initialize the
4050 * execution timeouts.
4051 */
4052 if (!HLUA_IS_RUNNING(hlua))
Camilo Lopez685c0142015-08-02 19:07:28 -04004053 hlua->expire = tick_add_ifset(now_ms, hlua_timeout_task);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004054
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004055 /* Execute the Lua code. */
4056 status = hlua_ctx_resume(hlua, 1);
4057
4058 switch (status) {
4059 /* finished or yield */
4060 case HLUA_E_OK:
4061 hlua_ctx_destroy(hlua);
4062 task_delete(task);
4063 task_free(task);
4064 break;
4065
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004066 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
4067 if (hlua->wake_time != TICK_ETERNITY)
4068 task_schedule(task, hlua->wake_time);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004069 break;
4070
4071 /* finished with error. */
4072 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004073 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004074 hlua_ctx_destroy(hlua);
4075 task_delete(task);
4076 task_free(task);
4077 break;
4078
4079 case HLUA_E_ERR:
4080 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004081 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004082 hlua_ctx_destroy(hlua);
4083 task_delete(task);
4084 task_free(task);
4085 break;
4086 }
4087 return NULL;
4088}
4089
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004090/* This function is an LUA binding that register LUA function to be
4091 * executed after the HAProxy configuration parsing and before the
4092 * HAProxy scheduler starts. This function expect only one LUA
4093 * argument that is a function. This function returns nothing, but
4094 * throws if an error is encountered.
4095 */
4096__LJMP static int hlua_register_init(lua_State *L)
4097{
4098 struct hlua_init_function *init;
4099 int ref;
4100
4101 MAY_LJMP(check_args(L, 1, "register_init"));
4102
4103 ref = MAY_LJMP(hlua_checkfunction(L, 1));
4104
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004105 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004106 if (!init)
4107 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4108
4109 init->function_ref = ref;
4110 LIST_ADDQ(&hlua_init_functions, &init->l);
4111 return 0;
4112}
4113
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004114/* This functio is an LUA binding. It permits to register a task
4115 * executed in parallel of the main HAroxy activity. The task is
4116 * created and it is set in the HAProxy scheduler. It can be called
4117 * from the "init" section, "post init" or during the runtime.
4118 *
4119 * Lua prototype:
4120 *
4121 * <none> core.register_task(<function>)
4122 */
4123static int hlua_register_task(lua_State *L)
4124{
4125 struct hlua *hlua;
4126 struct task *task;
4127 int ref;
4128
4129 MAY_LJMP(check_args(L, 1, "register_task"));
4130
4131 ref = MAY_LJMP(hlua_checkfunction(L, 1));
4132
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004133 hlua = calloc(1, sizeof(*hlua));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004134 if (!hlua)
4135 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4136
4137 task = task_new();
4138 task->context = hlua;
4139 task->process = hlua_process_task;
4140
4141 if (!hlua_ctx_init(hlua, task))
4142 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4143
4144 /* Restore the function in the stack. */
4145 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
4146 hlua->nargs = 0;
4147
4148 /* Schedule task. */
4149 task_schedule(task, now_ms);
4150
4151 return 0;
4152}
4153
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004154/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
4155 * doesn't allow "yield" functions because the HAProxy engine cannot
4156 * resume converters.
4157 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004158static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004159{
4160 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004161 struct stream *stream = smp->strm;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004162
Willy Tarreau87b09662015-04-03 00:22:06 +02004163 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004164 * Lua context can be not initialized. This behavior
4165 * permits to save performances because a systematic
4166 * Lua initialization cause 5% performances loss.
4167 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004168 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004169 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004170 return 0;
4171 }
4172
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004173 /* If it is the first run, initialize the data for the call. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004174 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004175
4176 /* The following Lua calls can fail. */
4177 if (!SET_SAFE_LJMP(stream->hlua.T)) {
4178 SEND_ERR(stream->be, "Lua converter '%s': critical error.\n", fcn->name);
4179 return 0;
4180 }
4181
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004182 /* Check stack available size. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004183 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004184 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004185 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004186 return 0;
4187 }
4188
4189 /* Restore the function in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004190 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004191
4192 /* convert input sample and pust-it in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004193 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004194 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004195 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004196 return 0;
4197 }
Willy Tarreau87b09662015-04-03 00:22:06 +02004198 hlua_smp2lua(stream->hlua.T, smp);
4199 stream->hlua.nargs = 2;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004200
4201 /* push keywords in the stack. */
4202 if (arg_p) {
4203 for (; arg_p->type != ARGT_STOP; arg_p++) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004204 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004205 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004206 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004207 return 0;
4208 }
Willy Tarreau87b09662015-04-03 00:22:06 +02004209 hlua_arg2lua(stream->hlua.T, arg_p);
4210 stream->hlua.nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004211 }
4212 }
4213
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004214 /* We must initialize the execution timeouts. */
Thierry FOURNIER61e96c62015-08-09 13:10:24 +02004215 stream->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004216
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004217 /* At this point the execution is safe. */
4218 RESET_SAFE_LJMP(stream->hlua.T);
4219
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004220 /* Set the currently running flag. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004221 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004222 }
4223
4224 /* Execute the function. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004225 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004226 /* finished. */
4227 case HLUA_E_OK:
4228 /* Convert the returned value in sample. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004229 hlua_lua2smp(stream->hlua.T, -1, smp);
4230 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004231 return 1;
4232
4233 /* yield. */
4234 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004235 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004236 return 0;
4237
4238 /* finished with error. */
4239 case HLUA_E_ERRMSG:
4240 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004241 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
4242 fcn->name, lua_tostring(stream->hlua.T, -1));
Willy Tarreau87b09662015-04-03 00:22:06 +02004243 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004244 return 0;
4245
4246 case HLUA_E_ERR:
4247 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004248 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004249
4250 default:
4251 return 0;
4252 }
4253}
4254
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004255/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
4256 * doesn't allow "yield" functions because the HAProxy engine cannot
4257 * resume sample-fetches.
4258 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004259static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
4260 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004261{
4262 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004263 struct stream *stream = smp->strm;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004264
Willy Tarreau87b09662015-04-03 00:22:06 +02004265 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004266 * Lua context can be not initialized. This behavior
4267 * permits to save performances because a systematic
4268 * Lua initialization cause 5% performances loss.
4269 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004270 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004271 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004272 return 0;
4273 }
4274
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004275 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004276 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004277
4278 /* The following Lua calls can fail. */
4279 if (!SET_SAFE_LJMP(stream->hlua.T)) {
4280 SEND_ERR(smp->px, "Lua sample-fetch '%s': critical error.\n", fcn->name);
4281 return 0;
4282 }
4283
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004284 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004285 if (!lua_checkstack(stream->hlua.T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004286 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004287 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004288 return 0;
4289 }
4290
4291 /* Restore the function in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004292 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004293
4294 /* push arguments in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004295 if (!hlua_txn_new(stream->hlua.T, stream, smp->px)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004296 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004297 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004298 return 0;
4299 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004300 stream->hlua.nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004301
4302 /* push keywords in the stack. */
4303 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
4304 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004305 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004306 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004307 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004308 return 0;
4309 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004310 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004311 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004312 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004313 return 0;
4314 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004315 hlua_arg2lua(stream->hlua.T, arg_p);
4316 stream->hlua.nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004317 }
4318
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004319 /* We must initialize the execution timeouts. */
Thierry FOURNIER61e96c62015-08-09 13:10:24 +02004320 stream->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004321
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004322 /* At this point the execution is safe. */
4323 RESET_SAFE_LJMP(stream->hlua.T);
4324
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004325 /* Set the currently running flag. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004326 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004327 }
4328
4329 /* Execute the function. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004330 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004331 /* finished. */
4332 case HLUA_E_OK:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004333 if (!hlua_check_proto(stream, !(smp->opt & SMP_OPT_DIR_REQ)))
4334 return 0;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004335 /* Convert the returned value in sample. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004336 hlua_lua2smp(stream->hlua.T, -1, smp);
4337 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004338
4339 /* Set the end of execution flag. */
4340 smp->flags &= ~SMP_F_MAY_CHANGE;
4341 return 1;
4342
4343 /* yield. */
4344 case HLUA_E_AGAIN:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004345 hlua_check_proto(stream, !(smp->opt & SMP_OPT_DIR_REQ));
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004346 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004347 return 0;
4348
4349 /* finished with error. */
4350 case HLUA_E_ERRMSG:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004351 hlua_check_proto(stream, !(smp->opt & SMP_OPT_DIR_REQ));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004352 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004353 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
4354 fcn->name, lua_tostring(stream->hlua.T, -1));
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004355 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004356 return 0;
4357
4358 case HLUA_E_ERR:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004359 hlua_check_proto(stream, !(smp->opt & SMP_OPT_DIR_REQ));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004360 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004361 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004362
4363 default:
4364 return 0;
4365 }
4366}
4367
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004368/* This function is an LUA binding used for registering
4369 * "sample-conv" functions. It expects a converter name used
4370 * in the haproxy configuration file, and an LUA function.
4371 */
4372__LJMP static int hlua_register_converters(lua_State *L)
4373{
4374 struct sample_conv_kw_list *sck;
4375 const char *name;
4376 int ref;
4377 int len;
4378 struct hlua_function *fcn;
4379
4380 MAY_LJMP(check_args(L, 2, "register_converters"));
4381
4382 /* First argument : converter name. */
4383 name = MAY_LJMP(luaL_checkstring(L, 1));
4384
4385 /* Second argument : lua function. */
4386 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4387
4388 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004389 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004390 if (!sck)
4391 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004392 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004393 if (!fcn)
4394 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4395
4396 /* Fill fcn. */
4397 fcn->name = strdup(name);
4398 if (!fcn->name)
4399 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4400 fcn->function_ref = ref;
4401
4402 /* List head */
4403 sck->list.n = sck->list.p = NULL;
4404
4405 /* converter keyword. */
4406 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004407 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004408 if (!sck->kw[0].kw)
4409 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4410
4411 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
4412 sck->kw[0].process = hlua_sample_conv_wrapper;
4413 sck->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4414 sck->kw[0].val_args = NULL;
4415 sck->kw[0].in_type = SMP_T_STR;
4416 sck->kw[0].out_type = SMP_T_STR;
4417 sck->kw[0].private = fcn;
4418
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004419 /* Register this new converter */
4420 sample_register_convs(sck);
4421
4422 return 0;
4423}
4424
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004425/* This fucntion is an LUA binding used for registering
4426 * "sample-fetch" functions. It expects a converter name used
4427 * in the haproxy configuration file, and an LUA function.
4428 */
4429__LJMP static int hlua_register_fetches(lua_State *L)
4430{
4431 const char *name;
4432 int ref;
4433 int len;
4434 struct sample_fetch_kw_list *sfk;
4435 struct hlua_function *fcn;
4436
4437 MAY_LJMP(check_args(L, 2, "register_fetches"));
4438
4439 /* First argument : sample-fetch name. */
4440 name = MAY_LJMP(luaL_checkstring(L, 1));
4441
4442 /* Second argument : lua function. */
4443 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4444
4445 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004446 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004447 if (!sfk)
4448 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004449 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004450 if (!fcn)
4451 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4452
4453 /* Fill fcn. */
4454 fcn->name = strdup(name);
4455 if (!fcn->name)
4456 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4457 fcn->function_ref = ref;
4458
4459 /* List head */
4460 sfk->list.n = sfk->list.p = NULL;
4461
4462 /* sample-fetch keyword. */
4463 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004464 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004465 if (!sfk->kw[0].kw)
4466 return luaL_error(L, "lua out of memory error.");
4467
4468 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
4469 sfk->kw[0].process = hlua_sample_fetch_wrapper;
4470 sfk->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4471 sfk->kw[0].val_args = NULL;
4472 sfk->kw[0].out_type = SMP_T_STR;
4473 sfk->kw[0].use = SMP_USE_HTTP_ANY;
4474 sfk->kw[0].val = 0;
4475 sfk->kw[0].private = fcn;
4476
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004477 /* Register this new fetch. */
4478 sample_register_fetches(sfk);
4479
4480 return 0;
4481}
4482
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004483/* This function is a wrapper to execute each LUA function declared
4484 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004485 * return ACT_RET_CONT if the processing is finished (with or without
4486 * error) and return ACT_RET_YIELD if the function must be called again
4487 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004488 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004489static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02004490 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004491{
4492 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004493 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004494 int dir;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004495
4496 switch (rule->from) {
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004497 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = 0; break;
4498 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = 1; break;
4499 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = 0; break;
4500 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = 1; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004501 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004502 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004503 return ACT_RET_CONT;
4504 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004505
Willy Tarreau87b09662015-04-03 00:22:06 +02004506 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004507 * Lua context can be not initialized. This behavior
4508 * permits to save performances because a systematic
4509 * Lua initialization cause 5% performances loss.
4510 */
4511 if (!s->hlua.T && !hlua_ctx_init(&s->hlua, s->task)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004512 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004513 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004514 return ACT_RET_CONT;
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004515 }
4516
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004517 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004518 if (!HLUA_IS_RUNNING(&s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004519
4520 /* The following Lua calls can fail. */
4521 if (!SET_SAFE_LJMP(s->hlua.T)) {
4522 SEND_ERR(px, "Lua function '%s': critical error.\n",
4523 rule->arg.hlua_rule->fcn.name);
4524 return ACT_RET_CONT;
4525 }
4526
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004527 /* Check stack available size. */
4528 if (!lua_checkstack(s->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004529 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004530 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004531 RESET_SAFE_LJMP(s->hlua.T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004532 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004533 }
4534
4535 /* Restore the function in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004536 lua_rawgeti(s->hlua.T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004537
Willy Tarreau87b09662015-04-03 00:22:06 +02004538 /* Create and and push object stream in the stack. */
Willy Tarreau15e91e12015-04-04 00:52:09 +02004539 if (!hlua_txn_new(s->hlua.T, s, px)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004540 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004541 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004542 RESET_SAFE_LJMP(s->hlua.T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004543 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004544 }
4545 s->hlua.nargs = 1;
4546
4547 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004548 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004549 if (!lua_checkstack(s->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004550 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004551 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004552 RESET_SAFE_LJMP(s->hlua.T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004553 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004554 }
4555 lua_pushstring(s->hlua.T, *arg);
4556 s->hlua.nargs++;
4557 }
4558
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004559 /* Now the execution is safe. */
4560 RESET_SAFE_LJMP(s->hlua.T);
4561
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004562 /* We must initialize the execution timeouts. */
Thierry FOURNIER61e96c62015-08-09 13:10:24 +02004563 s->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004564
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004565 /* Set the currently running flag. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004566 HLUA_SET_RUN(&s->hlua);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004567 }
4568
4569 /* Execute the function. */
Willy Tarreau528192d2015-09-27 10:48:01 +02004570 switch (hlua_ctx_resume(&s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004571 /* finished. */
4572 case HLUA_E_OK:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004573 if (!hlua_check_proto(s, dir))
4574 return ACT_RET_ERR;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004575 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004576
4577 /* yield. */
4578 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004579 /* Set timeout in the required channel. */
4580 if (s->hlua.wake_time != TICK_ETERNITY) {
4581 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004582 s->req.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004583 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004584 s->res.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004585 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004586 /* Some actions can be wake up when a "write" event
4587 * is detected on a response channel. This is useful
4588 * only for actions targetted on the requests.
4589 */
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01004590 if (HLUA_IS_WAKERESWR(&s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004591 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01004592 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004593 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004594 }
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01004595 if (HLUA_IS_WAKEREQWR(&s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004596 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004597 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004598
4599 /* finished with error. */
4600 case HLUA_E_ERRMSG:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004601 if (!hlua_check_proto(s, dir))
4602 return ACT_RET_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004603 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004604 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004605 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua.T, -1));
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004606 lua_pop(s->hlua.T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004607 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004608
4609 case HLUA_E_ERR:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004610 if (!hlua_check_proto(s, dir))
4611 return ACT_RET_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004612 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004613 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004614 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004615
4616 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004617 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004618 }
4619}
4620
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004621/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
4622 * succes case, else return ACT_RET_PRS_ERR.
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 FOURNIER258d8aa2015-02-16 20:23:40 +01004627 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004628static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4629 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004630{
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004631 struct hlua_function *fcn = (struct hlua_function *)rule->kw->private;
4632
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004633 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004634 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004635 if (!rule->arg.hlua_rule) {
4636 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02004637 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004638 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004639
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004640 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004641 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004642
4643 /* TODO: later accept arguments. */
4644 rule->arg.hlua_rule->args = NULL;
4645
Thierry FOURNIER42148732015-09-02 17:17:33 +02004646 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004647 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02004648 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004649}
4650
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004651/* This function is an LUA binding used for registering
4652 * "sample-conv" functions. It expects a converter name used
4653 * in the haproxy configuration file, and an LUA function.
4654 */
4655__LJMP static int hlua_register_action(lua_State *L)
4656{
4657 struct action_kw_list *akl;
4658 const char *name;
4659 int ref;
4660 int len;
4661 struct hlua_function *fcn;
4662
4663 MAY_LJMP(check_args(L, 3, "register_service"));
4664
4665 /* First argument : converter name. */
4666 name = MAY_LJMP(luaL_checkstring(L, 1));
4667
4668 /* Second argument : environment. */
4669 if (lua_type(L, 2) != LUA_TTABLE)
4670 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
4671
4672 /* Third argument : lua function. */
4673 ref = MAY_LJMP(hlua_checkfunction(L, 3));
4674
4675 /* browse the second argulent as an array. */
4676 lua_pushnil(L);
4677 while (lua_next(L, 2) != 0) {
4678 if (lua_type(L, -1) != LUA_TSTRING)
4679 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
4680
4681 /* Check required environment. Only accepted "http" or "tcp". */
4682 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004683 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004684 if (!akl)
4685 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004686 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004687 if (!fcn)
4688 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4689
4690 /* Fill fcn. */
4691 fcn->name = strdup(name);
4692 if (!fcn->name)
4693 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4694 fcn->function_ref = ref;
4695
4696 /* List head */
4697 akl->list.n = akl->list.p = NULL;
4698
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004699 /* action keyword. */
4700 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004701 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004702 if (!akl->kw[0].kw)
4703 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4704
4705 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
4706
4707 akl->kw[0].match_pfx = 0;
4708 akl->kw[0].private = fcn;
4709 akl->kw[0].parse = action_register_lua;
4710
4711 /* select the action registering point. */
4712 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
4713 tcp_req_cont_keywords_register(akl);
4714 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
4715 tcp_res_cont_keywords_register(akl);
4716 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
4717 http_req_keywords_register(akl);
4718 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
4719 http_res_keywords_register(akl);
4720 else
4721 WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. "
4722 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
4723 "are expected.", lua_tostring(L, -1)));
4724
4725 /* pop the environment string. */
4726 lua_pop(L, 1);
4727 }
4728
4729 return 0;
4730}
4731
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004732static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
4733 struct proxy *defpx, const char *file, int line,
4734 char **err, unsigned int *timeout)
4735{
4736 const char *error;
4737
4738 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
4739 if (error && *error != '\0') {
4740 memprintf(err, "%s: invalid timeout", args[0]);
4741 return -1;
4742 }
4743 return 0;
4744}
4745
4746static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
4747 struct proxy *defpx, const char *file, int line,
4748 char **err)
4749{
4750 return hlua_read_timeout(args, section_type, curpx, defpx,
4751 file, line, err, &hlua_timeout_session);
4752}
4753
4754static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
4755 struct proxy *defpx, const char *file, int line,
4756 char **err)
4757{
4758 return hlua_read_timeout(args, section_type, curpx, defpx,
4759 file, line, err, &hlua_timeout_task);
4760}
4761
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004762static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
4763 struct proxy *defpx, const char *file, int line,
4764 char **err)
4765{
4766 char *error;
4767
4768 hlua_nb_instruction = strtoll(args[1], &error, 10);
4769 if (*error != '\0') {
4770 memprintf(err, "%s: invalid number", args[0]);
4771 return -1;
4772 }
4773 return 0;
4774}
4775
Willy Tarreau32f61e22015-03-18 17:54:59 +01004776static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
4777 struct proxy *defpx, const char *file, int line,
4778 char **err)
4779{
4780 char *error;
4781
4782 if (*(args[1]) == 0) {
4783 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
4784 return -1;
4785 }
4786 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
4787 if (*error != '\0') {
4788 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
4789 return -1;
4790 }
4791 return 0;
4792}
4793
4794
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004795/* This function is called by the main configuration key "lua-load". It loads and
4796 * execute an lua file during the parsing of the HAProxy configuration file. It is
4797 * the main lua entry point.
4798 *
4799 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
4800 * occured, otherwise it returns 0.
4801 *
4802 * In some error case, LUA set an error message in top of the stack. This function
4803 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004804 *
4805 * This function can fail with an abort() due to an Lua critical error.
4806 * We are in the configuration parsing process of HAProxy, this abort() is
4807 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004808 */
4809static int hlua_load(char **args, int section_type, struct proxy *curpx,
4810 struct proxy *defpx, const char *file, int line,
4811 char **err)
4812{
4813 int error;
4814
4815 /* Just load and compile the file. */
4816 error = luaL_loadfile(gL.T, args[1]);
4817 if (error) {
4818 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
4819 lua_pop(gL.T, 1);
4820 return -1;
4821 }
4822
4823 /* If no syntax error where detected, execute the code. */
4824 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
4825 switch (error) {
4826 case LUA_OK:
4827 break;
4828 case LUA_ERRRUN:
4829 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
4830 lua_pop(gL.T, 1);
4831 return -1;
4832 case LUA_ERRMEM:
4833 memprintf(err, "lua out of memory error\n");
4834 return -1;
4835 case LUA_ERRERR:
4836 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
4837 lua_pop(gL.T, 1);
4838 return -1;
4839 case LUA_ERRGCMM:
4840 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
4841 lua_pop(gL.T, 1);
4842 return -1;
4843 default:
4844 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
4845 lua_pop(gL.T, 1);
4846 return -1;
4847 }
4848
4849 return 0;
4850}
4851
4852/* configuration keywords declaration */
4853static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004854 { CFG_GLOBAL, "lua-load", hlua_load },
4855 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
4856 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004857 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01004858 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004859 { 0, NULL, NULL },
4860}};
4861
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004862/* This function can fail with an abort() due to an Lua critical error.
4863 * We are in the initialisation process of HAProxy, this abort() is
4864 * tolerated.
4865 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004866int hlua_post_init()
4867{
4868 struct hlua_init_function *init;
4869 const char *msg;
4870 enum hlua_exec ret;
4871
4872 list_for_each_entry(init, &hlua_init_functions, l) {
4873 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
4874 ret = hlua_ctx_resume(&gL, 0);
4875 switch (ret) {
4876 case HLUA_E_OK:
4877 lua_pop(gL.T, -1);
4878 return 1;
4879 case HLUA_E_AGAIN:
4880 Alert("lua init: yield not allowed.\n");
4881 return 0;
4882 case HLUA_E_ERRMSG:
4883 msg = lua_tostring(gL.T, -1);
4884 Alert("lua init: %s.\n", msg);
4885 return 0;
4886 case HLUA_E_ERR:
4887 default:
4888 Alert("lua init: unknown runtime error.\n");
4889 return 0;
4890 }
4891 }
4892 return 1;
4893}
4894
Willy Tarreau32f61e22015-03-18 17:54:59 +01004895/* The memory allocator used by the Lua stack. <ud> is a pointer to the
4896 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
4897 * is the previously allocated size or the kind of object in case of a new
4898 * allocation. <nsize> is the requested new size.
4899 */
4900static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
4901{
4902 struct hlua_mem_allocator *zone = ud;
4903
4904 if (nsize == 0) {
4905 /* it's a free */
4906 if (ptr)
4907 zone->allocated -= osize;
4908 free(ptr);
4909 return NULL;
4910 }
4911
4912 if (!ptr) {
4913 /* it's a new allocation */
4914 if (zone->limit && zone->allocated + nsize > zone->limit)
4915 return NULL;
4916
4917 ptr = malloc(nsize);
4918 if (ptr)
4919 zone->allocated += nsize;
4920 return ptr;
4921 }
4922
4923 /* it's a realloc */
4924 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
4925 return NULL;
4926
4927 ptr = realloc(ptr, nsize);
4928 if (ptr)
4929 zone->allocated += nsize - osize;
4930 return ptr;
4931}
4932
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004933/* Ithis function can fail with an abort() due to an Lua critical error.
4934 * We are in the initialisation process of HAProxy, this abort() is
4935 * tolerated.
4936 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01004937void hlua_init(void)
4938{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004939 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004940 int idx;
4941 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004942 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004943 char *p;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004944#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004945 struct srv_kw *kw;
4946 int tmp_error;
4947 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01004948 char *args[] = { /* SSL client configuration. */
4949 "ssl",
4950 "verify",
4951 "none",
4952 "force-sslv3",
4953 NULL
4954 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004955#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004956
Willy Tarreau87b09662015-04-03 00:22:06 +02004957 /* Initialise com signals pool */
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004958 pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED);
4959
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004960 /* Register configuration keywords. */
4961 cfg_register_keywords(&cfg_kws);
4962
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004963 /* Init main lua stack. */
4964 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004965 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004966 LIST_INIT(&gL.com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004967 gL.T = luaL_newstate();
4968 hlua_sethlua(&gL);
4969 gL.Tref = LUA_REFNIL;
4970 gL.task = NULL;
4971
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004972 /* From this point, until the end of the initialisation fucntion,
4973 * the Lua function can fail with an abort. We are in the initialisation
4974 * process of HAProxy, this abort() is tolerated.
4975 */
4976
Willy Tarreau32f61e22015-03-18 17:54:59 +01004977 /* change the memory allocators to track memory usage */
4978 lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator);
4979
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004980 /* Initialise lua. */
4981 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004982
4983 /*
4984 *
4985 * Create "core" object.
4986 *
4987 */
4988
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01004989 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004990 lua_newtable(gL.T);
4991
4992 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004993 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004994 hlua_class_const_int(gL.T, log_levels[i], i);
4995
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004996 /* Register special functions. */
4997 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004998 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004999 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005000 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02005001 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005002 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01005003 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005004 hlua_class_function(gL.T, "sleep", hlua_sleep);
5005 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01005006 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
5007 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
5008 hlua_class_function(gL.T, "set_map", hlua_set_map);
5009 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005010 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005011 hlua_class_function(gL.T, "log", hlua_log);
5012 hlua_class_function(gL.T, "Debug", hlua_log_debug);
5013 hlua_class_function(gL.T, "Info", hlua_log_info);
5014 hlua_class_function(gL.T, "Warning", hlua_log_warning);
5015 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02005016 hlua_class_function(gL.T, "done", hlua_done);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005017
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01005018 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005019
5020 /*
5021 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02005022 * Register class Map
5023 *
5024 */
5025
5026 /* This table entry is the object "Map" base. */
5027 lua_newtable(gL.T);
5028
5029 /* register pattern types. */
5030 for (i=0; i<PAT_MATCH_NUM; i++)
5031 hlua_class_const_int(gL.T, pat_match_names[i], i);
5032
5033 /* register constructor. */
5034 hlua_class_function(gL.T, "new", hlua_map_new);
5035
5036 /* Create and fill the metatable. */
5037 lua_newtable(gL.T);
5038
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005039 /* Create the __tostring identifier */
5040 lua_pushstring(gL.T, "__tostring");
5041 lua_pushstring(gL.T, CLASS_MAP);
5042 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5043 lua_rawset(gL.T, -3);
5044
Thierry FOURNIER3def3932015-04-07 11:27:54 +02005045 /* Create and fille the __index entry. */
5046 lua_pushstring(gL.T, "__index");
5047 lua_newtable(gL.T);
5048
5049 /* Register . */
5050 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
5051 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
5052
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005053 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02005054
5055 /* Register previous table in the registry with reference and named entry. */
5056 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5057 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5058 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_MAP); /* register class session. */
5059 class_map_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5060
5061 /* Assign the metatable to the mai Map object. */
5062 lua_setmetatable(gL.T, -2);
5063
5064 /* Set a name to the table. */
5065 lua_setglobal(gL.T, "Map");
5066
5067 /*
5068 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01005069 * Register class Channel
5070 *
5071 */
5072
5073 /* Create and fill the metatable. */
5074 lua_newtable(gL.T);
5075
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005076 /* Create the __tostring identifier */
5077 lua_pushstring(gL.T, "__tostring");
5078 lua_pushstring(gL.T, CLASS_CHANNEL);
5079 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5080 lua_rawset(gL.T, -3);
5081
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01005082 /* Create and fille the __index entry. */
5083 lua_pushstring(gL.T, "__index");
5084 lua_newtable(gL.T);
5085
5086 /* Register . */
5087 hlua_class_function(gL.T, "get", hlua_channel_get);
5088 hlua_class_function(gL.T, "dup", hlua_channel_dup);
5089 hlua_class_function(gL.T, "getline", hlua_channel_getline);
5090 hlua_class_function(gL.T, "set", hlua_channel_set);
5091 hlua_class_function(gL.T, "append", hlua_channel_append);
5092 hlua_class_function(gL.T, "send", hlua_channel_send);
5093 hlua_class_function(gL.T, "forward", hlua_channel_forward);
5094 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
5095 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
5096
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005097 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01005098
5099 /* Register previous table in the registry with reference and named entry. */
5100 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5101 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CHANNEL); /* register class session. */
5102 class_channel_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5103
5104 /*
5105 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005106 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005107 *
5108 */
5109
5110 /* Create and fill the metatable. */
5111 lua_newtable(gL.T);
5112
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005113 /* Create the __tostring identifier */
5114 lua_pushstring(gL.T, "__tostring");
5115 lua_pushstring(gL.T, CLASS_FETCHES);
5116 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5117 lua_rawset(gL.T, -3);
5118
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005119 /* Create and fille the __index entry. */
5120 lua_pushstring(gL.T, "__index");
5121 lua_newtable(gL.T);
5122
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01005123 /* Browse existing fetches and create the associated
5124 * object method.
5125 */
5126 sf = NULL;
5127 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
5128
5129 /* Dont register the keywork if the arguments check function are
5130 * not safe during the runtime.
5131 */
5132 if ((sf->val_args != NULL) &&
5133 (sf->val_args != val_payload_lv) &&
5134 (sf->val_args != val_hdr))
5135 continue;
5136
5137 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
5138 * by an underscore.
5139 */
5140 strncpy(trash.str, sf->kw, trash.size);
5141 trash.str[trash.size - 1] = '\0';
5142 for (p = trash.str; *p; p++)
5143 if (*p == '.' || *p == '-' || *p == '+')
5144 *p = '_';
5145
5146 /* Register the function. */
5147 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01005148 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01005149 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005150 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01005151 }
5152
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005153 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005154
5155 /* Register previous table in the registry with reference and named entry. */
5156 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5157 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_FETCHES); /* register class session. */
5158 class_fetches_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5159
5160 /*
5161 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005162 * Register class Converters
5163 *
5164 */
5165
5166 /* Create and fill the metatable. */
5167 lua_newtable(gL.T);
5168
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005169 /* Create the __tostring identifier */
5170 lua_pushstring(gL.T, "__tostring");
5171 lua_pushstring(gL.T, CLASS_CONVERTERS);
5172 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5173 lua_rawset(gL.T, -3);
5174
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005175 /* Create and fill the __index entry. */
5176 lua_pushstring(gL.T, "__index");
5177 lua_newtable(gL.T);
5178
5179 /* Browse existing converters and create the associated
5180 * object method.
5181 */
5182 sc = NULL;
5183 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
5184 /* Dont register the keywork if the arguments check function are
5185 * not safe during the runtime.
5186 */
5187 if (sc->val_args != NULL)
5188 continue;
5189
5190 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
5191 * by an underscore.
5192 */
5193 strncpy(trash.str, sc->kw, trash.size);
5194 trash.str[trash.size - 1] = '\0';
5195 for (p = trash.str; *p; p++)
5196 if (*p == '.' || *p == '-' || *p == '+')
5197 *p = '_';
5198
5199 /* Register the function. */
5200 lua_pushstring(gL.T, trash.str);
5201 lua_pushlightuserdata(gL.T, sc);
5202 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005203 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005204 }
5205
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005206 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005207
5208 /* Register previous table in the registry with reference and named entry. */
5209 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5210 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CONVERTERS); /* register class session. */
5211 class_converters_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5212
5213 /*
5214 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005215 * Register class HTTP
5216 *
5217 */
5218
5219 /* Create and fill the metatable. */
5220 lua_newtable(gL.T);
5221
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005222 /* Create the __tostring identifier */
5223 lua_pushstring(gL.T, "__tostring");
5224 lua_pushstring(gL.T, CLASS_HTTP);
5225 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5226 lua_rawset(gL.T, -3);
5227
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005228 /* Create and fille the __index entry. */
5229 lua_pushstring(gL.T, "__index");
5230 lua_newtable(gL.T);
5231
5232 /* Register Lua functions. */
5233 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
5234 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
5235 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
5236 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
5237 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
5238 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
5239 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
5240 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
5241 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
5242 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
5243
5244 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
5245 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
5246 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
5247 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
5248 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
5249 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005250 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005251
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005252 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005253
5254 /* Register previous table in the registry with reference and named entry. */
5255 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5256 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_HTTP); /* register class session. */
5257 class_http_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5258
5259 /*
5260 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005261 * Register class TXN
5262 *
5263 */
5264
5265 /* Create and fill the metatable. */
5266 lua_newtable(gL.T);
5267
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005268 /* Create the __tostring identifier */
5269 lua_pushstring(gL.T, "__tostring");
5270 lua_pushstring(gL.T, CLASS_TXN);
5271 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5272 lua_rawset(gL.T, -3);
5273
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005274 /* Create and fille the __index entry. */
5275 lua_pushstring(gL.T, "__index");
5276 lua_newtable(gL.T);
5277
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005278 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01005279 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
5280 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005281 hlua_class_function(gL.T, "set_var", hlua_set_var);
5282 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005283 hlua_class_function(gL.T, "done", hlua_txn_done);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005284 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
5285 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
5286 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005287 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
5288 hlua_class_function(gL.T, "log", hlua_txn_log);
5289 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
5290 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
5291 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
5292 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005293
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005294 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005295
5296 /* Register previous table in the registry with reference and named entry. */
5297 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5298 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_TXN); /* register class session. */
5299 class_txn_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005300
5301 /*
5302 *
5303 * Register class Socket
5304 *
5305 */
5306
5307 /* Create and fill the metatable. */
5308 lua_newtable(gL.T);
5309
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005310 /* Create the __tostring identifier */
5311 lua_pushstring(gL.T, "__tostring");
5312 lua_pushstring(gL.T, CLASS_SOCKET);
5313 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5314 lua_rawset(gL.T, -3);
5315
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005316 /* Create and fille the __index entry. */
5317 lua_pushstring(gL.T, "__index");
5318 lua_newtable(gL.T);
5319
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005320#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005321 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005322#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005323 hlua_class_function(gL.T, "connect", hlua_socket_connect);
5324 hlua_class_function(gL.T, "send", hlua_socket_send);
5325 hlua_class_function(gL.T, "receive", hlua_socket_receive);
5326 hlua_class_function(gL.T, "close", hlua_socket_close);
5327 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
5328 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
5329 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
5330 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
5331
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005332 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005333
5334 /* Register the garbage collector entry. */
5335 lua_pushstring(gL.T, "__gc");
5336 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005337 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005338
5339 /* Register previous table in the registry with reference and named entry. */
5340 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5341 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5342 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_SOCKET); /* register class socket. */
5343 class_socket_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class socket. */
5344
5345 /* Proxy and server configuration initialisation. */
5346 memset(&socket_proxy, 0, sizeof(socket_proxy));
5347 init_new_proxy(&socket_proxy);
5348 socket_proxy.parent = NULL;
5349 socket_proxy.last_change = now.tv_sec;
5350 socket_proxy.id = "LUA-SOCKET";
5351 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
5352 socket_proxy.maxconn = 0;
5353 socket_proxy.accept = NULL;
5354 socket_proxy.options2 |= PR_O2_INDEPSTR;
5355 socket_proxy.srv = NULL;
5356 socket_proxy.conn_retries = 0;
5357 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
5358
5359 /* Init TCP server: unchanged parameters */
5360 memset(&socket_tcp, 0, sizeof(socket_tcp));
5361 socket_tcp.next = NULL;
5362 socket_tcp.proxy = &socket_proxy;
5363 socket_tcp.obj_type = OBJ_TYPE_SERVER;
5364 LIST_INIT(&socket_tcp.actconns);
5365 LIST_INIT(&socket_tcp.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02005366 LIST_INIT(&socket_tcp.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02005367 LIST_INIT(&socket_tcp.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02005368 LIST_INIT(&socket_tcp.safe_conns);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005369 socket_tcp.state = SRV_ST_RUNNING; /* early server setup */
5370 socket_tcp.last_change = 0;
5371 socket_tcp.id = "LUA-TCP-CONN";
5372 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5373 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5374 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
5375
5376 /* XXX: Copy default parameter from default server,
5377 * but the default server is not initialized.
5378 */
5379 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
5380 socket_tcp.minconn = socket_proxy.defsrv.minconn;
5381 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
5382 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
5383 socket_tcp.onerror = socket_proxy.defsrv.onerror;
5384 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5385 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
5386 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5387 socket_tcp.uweight = socket_proxy.defsrv.iweight;
5388 socket_tcp.iweight = socket_proxy.defsrv.iweight;
5389
5390 socket_tcp.check.status = HCHK_STATUS_INI;
5391 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
5392 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
5393 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
5394 socket_tcp.check.server = &socket_tcp;
5395
5396 socket_tcp.agent.status = HCHK_STATUS_INI;
5397 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
5398 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
5399 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
5400 socket_tcp.agent.server = &socket_tcp;
5401
5402 socket_tcp.xprt = &raw_sock;
5403
5404#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005405 /* Init TCP server: unchanged parameters */
5406 memset(&socket_ssl, 0, sizeof(socket_ssl));
5407 socket_ssl.next = NULL;
5408 socket_ssl.proxy = &socket_proxy;
5409 socket_ssl.obj_type = OBJ_TYPE_SERVER;
5410 LIST_INIT(&socket_ssl.actconns);
5411 LIST_INIT(&socket_ssl.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02005412 LIST_INIT(&socket_ssl.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02005413 LIST_INIT(&socket_ssl.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02005414 LIST_INIT(&socket_ssl.safe_conns);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005415 socket_ssl.state = SRV_ST_RUNNING; /* early server setup */
5416 socket_ssl.last_change = 0;
5417 socket_ssl.id = "LUA-SSL-CONN";
5418 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5419 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5420 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
5421
5422 /* XXX: Copy default parameter from default server,
5423 * but the default server is not initialized.
5424 */
5425 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
5426 socket_ssl.minconn = socket_proxy.defsrv.minconn;
5427 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
5428 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
5429 socket_ssl.onerror = socket_proxy.defsrv.onerror;
5430 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5431 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
5432 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5433 socket_ssl.uweight = socket_proxy.defsrv.iweight;
5434 socket_ssl.iweight = socket_proxy.defsrv.iweight;
5435
5436 socket_ssl.check.status = HCHK_STATUS_INI;
5437 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
5438 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
5439 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
5440 socket_ssl.check.server = &socket_ssl;
5441
5442 socket_ssl.agent.status = HCHK_STATUS_INI;
5443 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
5444 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
5445 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
5446 socket_ssl.agent.server = &socket_ssl;
5447
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005448 socket_ssl.use_ssl = 1;
5449 socket_ssl.xprt = &ssl_sock;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005450
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005451 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005452 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
5453 /*
5454 *
5455 * If the keyword is not known, we can search in the registered
5456 * server keywords. This is usefull to configure special SSL
5457 * features like client certificates and ssl_verify.
5458 *
5459 */
5460 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
5461 if (tmp_error != 0) {
5462 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
5463 abort(); /* This must be never arrives because the command line
5464 not editable by the user. */
5465 }
5466 idx += kw->skip;
5467 }
5468 }
5469
5470 /* Initialize SSL server. */
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005471 ssl_sock_prepare_srv_ctx(&socket_ssl, &socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005472#endif
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01005473}