blob: 32b6f1ae9a09cc6545a300b336d8c881f2869ded [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 */
926 if (lua_status(lua->T) == LUA_OK)
927 lua_gc(lua->T, LUA_GCCOLLECT, 0);
928 else
929 lua_gc(gL.T, LUA_GCCOLLECT, 0);
930
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200931 lua->T = NULL;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100932}
933
934/* This function is used to restore the Lua context when a coroutine
935 * fails. This function copy the common memory between old coroutine
936 * and the new coroutine. The old coroutine is destroyed, and its
937 * replaced by the new coroutine.
938 * If the flag "keep_msg" is set, the last entry of the old is assumed
939 * as string error message and it is copied in the new stack.
940 */
941static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
942{
943 lua_State *T;
944 int new_ref;
945
946 /* Renew the main LUA stack doesn't have sense. */
947 if (lua == &gL)
948 return 0;
949
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100950 /* New Lua coroutine. */
951 T = lua_newthread(gL.T);
952 if (!T)
953 return 0;
954
955 /* Copy last error message. */
956 if (keep_msg)
957 lua_xmove(lua->T, T, 1);
958
959 /* Copy data between the coroutines. */
960 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
961 lua_xmove(lua->T, T, 1);
962 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
963
964 /* Destroy old data. */
965 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
966
967 /* The thread is garbage collected by Lua. */
968 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
969
970 /* Fill the struct with the new coroutine values. */
971 lua->Mref = new_ref;
972 lua->T = T;
973 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
974
975 /* Set context. */
976 hlua_sethlua(lua);
977
978 return 1;
979}
980
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100981void hlua_hook(lua_State *L, lua_Debug *ar)
982{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100983 struct hlua *hlua = hlua_gethlua(L);
984
985 /* Lua cannot yield when its returning from a function,
986 * so, we can fix the interrupt hook to 1 instruction,
987 * expecting that the function is finnished.
988 */
989 if (lua_gethookmask(L) & LUA_MASKRET) {
990 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
991 return;
992 }
993
994 /* restore the interrupt condition. */
995 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
996
997 /* If we interrupt the Lua processing in yieldable state, we yield.
998 * If the state is not yieldable, trying yield causes an error.
999 */
1000 if (lua_isyieldable(L))
1001 WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
1002
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001003 /* If we cannot yield, update the clock and check the timeout. */
1004 tv_update_date(0, 1);
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001005 if (tick_is_expired(hlua->expire, now_ms)) {
1006 lua_pushfstring(L, "execution timeout");
1007 WILL_LJMP(lua_error(L));
1008 }
1009
1010 /* Try to interrupt the process at the end of the current
1011 * unyieldable function.
1012 */
1013 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001014}
1015
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001016/* This function start or resumes the Lua stack execution. If the flag
1017 * "yield_allowed" if no set and the LUA stack execution returns a yield
1018 * The function return an error.
1019 *
1020 * The function can returns 4 values:
1021 * - HLUA_E_OK : The execution is terminated without any errors.
1022 * - HLUA_E_AGAIN : The execution must continue at the next associated
1023 * task wakeup.
1024 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
1025 * the top of the stack.
1026 * - HLUA_E_ERR : An error has occured without error message.
1027 *
1028 * If an error occured, the stack is renewed and it is ready to run new
1029 * LUA code.
1030 */
1031static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1032{
1033 int ret;
1034 const char *msg;
1035
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001036 HLUA_SET_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001037
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001038 /* If we want to resume the task, then check first the execution timeout.
1039 * if it is reached, we can interrupt the Lua processing.
1040 */
1041 if (tick_is_expired(lua->expire, now_ms))
1042 goto timeout_reached;
1043
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001044resume_execution:
1045
1046 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1047 * instructions. it is used for preventing infinite loops.
1048 */
1049 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1050
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001051 /* Remove all flags except the running flags. */
1052 lua->flags = HLUA_RUN;
1053
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001054 /* Call the function. */
1055 ret = lua_resume(lua->T, gL.T, lua->nargs);
1056 switch (ret) {
1057
1058 case LUA_OK:
1059 ret = HLUA_E_OK;
1060 break;
1061
1062 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001063 /* Check if the execution timeout is expired. It it is the case, we
1064 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001065 */
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001066 if (tick_is_expired(lua->expire, now_ms)) {
1067
1068timeout_reached:
1069
1070 lua_settop(lua->T, 0); /* Empty the stack. */
1071 if (!lua_checkstack(lua->T, 1)) {
1072 ret = HLUA_E_ERR;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001073 break;
1074 }
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001075 lua_pushfstring(lua->T, "execution timeout");
1076 ret = HLUA_E_ERRMSG;
1077 break;
1078 }
1079 /* Process the forced yield. if the general yield is not allowed or
1080 * if no task were associated this the current Lua execution
1081 * coroutine, we resume the execution. Else we want to return in the
1082 * scheduler and we want to be waked up again, to continue the
1083 * current Lua execution. So we schedule our own task.
1084 */
1085 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001086 if (!yield_allowed || !lua->task)
1087 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001088 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001089 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001090 if (!yield_allowed) {
1091 lua_settop(lua->T, 0); /* Empty the stack. */
1092 if (!lua_checkstack(lua->T, 1)) {
1093 ret = HLUA_E_ERR;
1094 break;
1095 }
1096 lua_pushfstring(lua->T, "yield not allowed");
1097 ret = HLUA_E_ERRMSG;
1098 break;
1099 }
1100 ret = HLUA_E_AGAIN;
1101 break;
1102
1103 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001104
1105 /* Special exit case. The traditionnal exit is returned as an error
1106 * because the errors ares the only one mean to return immediately
1107 * from and lua execution.
1108 */
1109 if (lua->flags & HLUA_EXIT) {
1110 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001111 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001112 break;
1113 }
1114
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001115 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001116 if (!lua_checkstack(lua->T, 1)) {
1117 ret = HLUA_E_ERR;
1118 break;
1119 }
1120 msg = lua_tostring(lua->T, -1);
1121 lua_settop(lua->T, 0); /* Empty the stack. */
1122 lua_pop(lua->T, 1);
1123 if (msg)
1124 lua_pushfstring(lua->T, "runtime error: %s", msg);
1125 else
1126 lua_pushfstring(lua->T, "unknown runtime error");
1127 ret = HLUA_E_ERRMSG;
1128 break;
1129
1130 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001131 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001132 lua_settop(lua->T, 0); /* Empty the stack. */
1133 if (!lua_checkstack(lua->T, 1)) {
1134 ret = HLUA_E_ERR;
1135 break;
1136 }
1137 lua_pushfstring(lua->T, "out of memory error");
1138 ret = HLUA_E_ERRMSG;
1139 break;
1140
1141 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001142 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001143 if (!lua_checkstack(lua->T, 1)) {
1144 ret = HLUA_E_ERR;
1145 break;
1146 }
1147 msg = lua_tostring(lua->T, -1);
1148 lua_settop(lua->T, 0); /* Empty the stack. */
1149 lua_pop(lua->T, 1);
1150 if (msg)
1151 lua_pushfstring(lua->T, "message handler error: %s", msg);
1152 else
1153 lua_pushfstring(lua->T, "message handler error");
1154 ret = HLUA_E_ERRMSG;
1155 break;
1156
1157 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001158 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001159 lua_settop(lua->T, 0); /* Empty the stack. */
1160 if (!lua_checkstack(lua->T, 1)) {
1161 ret = HLUA_E_ERR;
1162 break;
1163 }
1164 lua_pushfstring(lua->T, "unknonwn error");
1165 ret = HLUA_E_ERRMSG;
1166 break;
1167 }
1168
1169 switch (ret) {
1170 case HLUA_E_AGAIN:
1171 break;
1172
1173 case HLUA_E_ERRMSG:
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001174 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001175 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001176 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001177 break;
1178
1179 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001180 HLUA_CLR_RUN(lua);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001181 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001182 hlua_ctx_renew(lua, 0);
1183 break;
1184
1185 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001186 HLUA_CLR_RUN(lua);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001187 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001188 break;
1189 }
1190
1191 return ret;
1192}
1193
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001194/* This function exit the current code. */
1195__LJMP static int hlua_done(lua_State *L)
1196{
1197 struct hlua *hlua = hlua_gethlua(L);
1198
1199 hlua->flags |= HLUA_EXIT;
1200 WILL_LJMP(lua_error(L));
1201
1202 return 0;
1203}
1204
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001205/* This function is an LUA binding. It provides a function
1206 * for deleting ACL from a referenced ACL file.
1207 */
1208__LJMP static int hlua_del_acl(lua_State *L)
1209{
1210 const char *name;
1211 const char *key;
1212 struct pat_ref *ref;
1213
1214 MAY_LJMP(check_args(L, 2, "del_acl"));
1215
1216 name = MAY_LJMP(luaL_checkstring(L, 1));
1217 key = MAY_LJMP(luaL_checkstring(L, 2));
1218
1219 ref = pat_ref_lookup(name);
1220 if (!ref)
1221 WILL_LJMP(luaL_error(L, "'del_acl': unkown acl file '%s'", name));
1222
1223 pat_ref_delete(ref, key);
1224 return 0;
1225}
1226
1227/* This function is an LUA binding. It provides a function
1228 * for deleting map entry from a referenced map file.
1229 */
1230static int hlua_del_map(lua_State *L)
1231{
1232 const char *name;
1233 const char *key;
1234 struct pat_ref *ref;
1235
1236 MAY_LJMP(check_args(L, 2, "del_map"));
1237
1238 name = MAY_LJMP(luaL_checkstring(L, 1));
1239 key = MAY_LJMP(luaL_checkstring(L, 2));
1240
1241 ref = pat_ref_lookup(name);
1242 if (!ref)
1243 WILL_LJMP(luaL_error(L, "'del_map': unkown acl file '%s'", name));
1244
1245 pat_ref_delete(ref, key);
1246 return 0;
1247}
1248
1249/* This function is an LUA binding. It provides a function
1250 * for adding ACL pattern from a referenced ACL file.
1251 */
1252static int hlua_add_acl(lua_State *L)
1253{
1254 const char *name;
1255 const char *key;
1256 struct pat_ref *ref;
1257
1258 MAY_LJMP(check_args(L, 2, "add_acl"));
1259
1260 name = MAY_LJMP(luaL_checkstring(L, 1));
1261 key = MAY_LJMP(luaL_checkstring(L, 2));
1262
1263 ref = pat_ref_lookup(name);
1264 if (!ref)
1265 WILL_LJMP(luaL_error(L, "'add_acl': unkown acl file '%s'", name));
1266
1267 if (pat_ref_find_elt(ref, key) == NULL)
1268 pat_ref_add(ref, key, NULL, NULL);
1269 return 0;
1270}
1271
1272/* This function is an LUA binding. It provides a function
1273 * for setting map pattern and sample from a referenced map
1274 * file.
1275 */
1276static int hlua_set_map(lua_State *L)
1277{
1278 const char *name;
1279 const char *key;
1280 const char *value;
1281 struct pat_ref *ref;
1282
1283 MAY_LJMP(check_args(L, 3, "set_map"));
1284
1285 name = MAY_LJMP(luaL_checkstring(L, 1));
1286 key = MAY_LJMP(luaL_checkstring(L, 2));
1287 value = MAY_LJMP(luaL_checkstring(L, 3));
1288
1289 ref = pat_ref_lookup(name);
1290 if (!ref)
1291 WILL_LJMP(luaL_error(L, "'set_map': unkown map file '%s'", name));
1292
1293 if (pat_ref_find_elt(ref, key) != NULL)
1294 pat_ref_set(ref, key, value, NULL);
1295 else
1296 pat_ref_add(ref, key, value, NULL);
1297 return 0;
1298}
1299
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001300/* A class is a lot of memory that contain data. This data can be a table,
1301 * an integer or user data. This data is associated with a metatable. This
1302 * metatable have an original version registred in the global context with
1303 * the name of the object (_G[<name>] = <metable> ).
1304 *
1305 * A metable is a table that modify the standard behavior of a standard
1306 * access to the associated data. The entries of this new metatable are
1307 * defined as is:
1308 *
1309 * http://lua-users.org/wiki/MetatableEvents
1310 *
1311 * __index
1312 *
1313 * we access an absent field in a table, the result is nil. This is
1314 * true, but it is not the whole truth. Actually, such access triggers
1315 * the interpreter to look for an __index metamethod: If there is no
1316 * such method, as usually happens, then the access results in nil;
1317 * otherwise, the metamethod will provide the result.
1318 *
1319 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1320 * the key does not appear in the table, but the metatable has an __index
1321 * property:
1322 *
1323 * - if the value is a function, the function is called, passing in the
1324 * table and the key; the return value of that function is returned as
1325 * the result.
1326 *
1327 * - if the value is another table, the value of the key in that table is
1328 * asked for and returned (and if it doesn't exist in that table, but that
1329 * table's metatable has an __index property, then it continues on up)
1330 *
1331 * - Use "rawget(myTable,key)" to skip this metamethod.
1332 *
1333 * http://www.lua.org/pil/13.4.1.html
1334 *
1335 * __newindex
1336 *
1337 * Like __index, but control property assignment.
1338 *
1339 * __mode - Control weak references. A string value with one or both
1340 * of the characters 'k' and 'v' which specifies that the the
1341 * keys and/or values in the table are weak references.
1342 *
1343 * __call - Treat a table like a function. When a table is followed by
1344 * parenthesis such as "myTable( 'foo' )" and the metatable has
1345 * a __call key pointing to a function, that function is invoked
1346 * (passing any specified arguments) and the return value is
1347 * returned.
1348 *
1349 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1350 * called, if the metatable for myTable has a __metatable
1351 * key, the value of that key is returned instead of the
1352 * actual metatable.
1353 *
1354 * __tostring - Control string representation. When the builtin
1355 * "tostring( myTable )" function is called, if the metatable
1356 * for myTable has a __tostring property set to a function,
1357 * that function is invoked (passing myTable to it) and the
1358 * return value is used as the string representation.
1359 *
1360 * __len - Control table length. When the table length is requested using
1361 * the length operator ( '#' ), if the metatable for myTable has
1362 * a __len key pointing to a function, that function is invoked
1363 * (passing myTable to it) and the return value used as the value
1364 * of "#myTable".
1365 *
1366 * __gc - Userdata finalizer code. When userdata is set to be garbage
1367 * collected, if the metatable has a __gc field pointing to a
1368 * function, that function is first invoked, passing the userdata
1369 * to it. The __gc metamethod is not called for tables.
1370 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1371 *
1372 * Special metamethods for redefining standard operators:
1373 * http://www.lua.org/pil/13.1.html
1374 *
1375 * __add "+"
1376 * __sub "-"
1377 * __mul "*"
1378 * __div "/"
1379 * __unm "!"
1380 * __pow "^"
1381 * __concat ".."
1382 *
1383 * Special methods for redfining standar relations
1384 * http://www.lua.org/pil/13.2.html
1385 *
1386 * __eq "=="
1387 * __lt "<"
1388 * __le "<="
1389 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001390
1391/*
1392 *
1393 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001394 * Class Map
1395 *
1396 *
1397 */
1398
1399/* Returns a struct hlua_map if the stack entry "ud" is
1400 * a class session, otherwise it throws an error.
1401 */
1402__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1403{
1404 return (struct map_descriptor *)MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
1405}
1406
1407/* This function is the map constructor. It don't need
1408 * the class Map object. It creates and return a new Map
1409 * object. It must be called only during "body" or "init"
1410 * context because it process some filesystem accesses.
1411 */
1412__LJMP static int hlua_map_new(struct lua_State *L)
1413{
1414 const char *fn;
1415 int match = PAT_MATCH_STR;
1416 struct sample_conv conv;
1417 const char *file = "";
1418 int line = 0;
1419 lua_Debug ar;
1420 char *err = NULL;
1421 struct arg args[2];
1422
1423 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1424 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1425
1426 fn = MAY_LJMP(luaL_checkstring(L, 1));
1427
1428 if (lua_gettop(L) >= 2) {
1429 match = MAY_LJMP(luaL_checkinteger(L, 2));
1430 if (match < 0 || match >= PAT_MATCH_NUM)
1431 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1432 }
1433
1434 /* Get Lua filename and line number. */
1435 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1436 lua_getinfo(L, "Sl", &ar); /* get info about it */
1437 if (ar.currentline > 0) { /* is there info? */
1438 file = ar.short_src;
1439 line = ar.currentline;
1440 }
1441 }
1442
1443 /* fill fake sample_conv struct. */
1444 conv.kw = ""; /* unused. */
1445 conv.process = NULL; /* unused. */
1446 conv.arg_mask = 0; /* unused. */
1447 conv.val_args = NULL; /* unused. */
1448 conv.out_type = SMP_T_STR;
1449 conv.private = (void *)(long)match;
1450 switch (match) {
1451 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1452 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1453 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1454 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1455 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1456 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1457 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001458 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001459 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1460 default:
1461 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1462 }
1463
1464 /* fill fake args. */
1465 args[0].type = ARGT_STR;
1466 args[0].data.str.str = (char *)fn;
1467 args[1].type = ARGT_STOP;
1468
1469 /* load the map. */
1470 if (!sample_load_map(args, &conv, file, line, &err)) {
1471 /* error case: we cant use luaL_error because we must
1472 * free the err variable.
1473 */
1474 luaL_where(L, 1);
1475 lua_pushfstring(L, "'new': %s.", err);
1476 lua_concat(L, 2);
1477 free(err);
1478 WILL_LJMP(lua_error(L));
1479 }
1480
1481 /* create the lua object. */
1482 lua_newtable(L);
1483 lua_pushlightuserdata(L, args[0].data.map);
1484 lua_rawseti(L, -2, 0);
1485
1486 /* Pop a class Map metatable and affect it to the userdata. */
1487 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1488 lua_setmetatable(L, -2);
1489
1490
1491 return 1;
1492}
1493
1494__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1495{
1496 struct map_descriptor *desc;
1497 struct pattern *pat;
1498 struct sample smp;
1499
1500 MAY_LJMP(check_args(L, 2, "lookup"));
1501 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001502 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001503 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001504 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001505 }
1506 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001507 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001508 smp.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001509 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 +02001510 }
1511
1512 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001513 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001514 if (str)
1515 lua_pushstring(L, "");
1516 else
1517 lua_pushnil(L);
1518 return 1;
1519 }
1520
1521 /* The Lua pattern must return a string, so we can't check the returned type */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001522 lua_pushlstring(L, pat->data->u.str.str, pat->data->u.str.len);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001523 return 1;
1524}
1525
1526__LJMP static int hlua_map_lookup(struct lua_State *L)
1527{
1528 return _hlua_map_lookup(L, 0);
1529}
1530
1531__LJMP static int hlua_map_slookup(struct lua_State *L)
1532{
1533 return _hlua_map_lookup(L, 1);
1534}
1535
1536/*
1537 *
1538 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001539 * Class Socket
1540 *
1541 *
1542 */
1543
1544__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1545{
1546 return (struct hlua_socket *)MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
1547}
1548
1549/* This function is the handler called for each I/O on the established
1550 * connection. It is used for notify space avalaible to send or data
1551 * received.
1552 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001553static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001554{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001555 struct stream_interface *si = appctx->owner;
Willy Tarreau50fe03b2014-11-28 13:59:31 +01001556 struct connection *c = objt_conn(si_opposite(si)->end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001557
Willy Tarreau87b09662015-04-03 00:22:06 +02001558 /* Wakeup the main stream if the client connection is closed. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001559 if (!c || channel_output_closed(si_ic(si)) || channel_input_closed(si_oc(si))) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001560 if (appctx->ctx.hlua.socket) {
1561 appctx->ctx.hlua.socket->s = NULL;
1562 appctx->ctx.hlua.socket = NULL;
1563 }
1564 si_shutw(si);
1565 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001566 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001567 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1568 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001569 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001570 }
1571
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001572 /* if the connection is not estabkished, inform the stream that we want
1573 * to be notified whenever the connection completes.
1574 */
1575 if (!(c->flags & CO_FL_CONNECTED)) {
1576 si_applet_cant_get(si);
1577 si_applet_cant_put(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001578 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001579 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001580
1581 /* This function is called after the connect. */
1582 appctx->ctx.hlua.connected = 1;
1583
1584 /* Wake the tasks which wants to write if the buffer have avalaible space. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001585 if (channel_may_recv(si_oc(si)))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001586 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
1587
1588 /* Wake the tasks which wants to read if the buffer contains data. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001589 if (channel_is_empty(si_ic(si)))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001590 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1591}
1592
Willy Tarreau87b09662015-04-03 00:22:06 +02001593/* This function is called when the "struct stream" is destroyed.
1594 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001595 * Wake all the pending signals.
1596 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001597static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001598{
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001599 /* Remove my link in the original object. */
1600 if (appctx->ctx.hlua.socket)
1601 appctx->ctx.hlua.socket->s = NULL;
1602
1603 /* Wake all the task waiting for me. */
1604 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1605 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
1606}
1607
1608/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001609 * uses this object. If the stream does not exists, just quit.
1610 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001611 * pending signal can rest in the read and write lists. destroy
1612 * it.
1613 */
1614__LJMP static int hlua_socket_gc(lua_State *L)
1615{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001616 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001617 struct appctx *appctx;
1618
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001619 MAY_LJMP(check_args(L, 1, "__gc"));
1620
1621 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001622 if (!socket->s)
1623 return 0;
1624
Willy Tarreau87b09662015-04-03 00:22:06 +02001625 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001626 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001627 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001628 socket->s = NULL;
1629 appctx->ctx.hlua.socket = NULL;
1630
1631 return 0;
1632}
1633
1634/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001635 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001636 */
1637__LJMP static int hlua_socket_close(lua_State *L)
1638{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001639 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001640 struct appctx *appctx;
1641
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001642 MAY_LJMP(check_args(L, 1, "close"));
1643
1644 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001645 if (!socket->s)
1646 return 0;
1647
Willy Tarreau87b09662015-04-03 00:22:06 +02001648 /* Close the stream and remove the associated stop task. */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001649 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001650 appctx = objt_appctx(socket->s->si[0].end);
1651 appctx->ctx.hlua.socket = NULL;
1652 socket->s = NULL;
1653
1654 return 0;
1655}
1656
1657/* This Lua function assumes that the stack contain three parameters.
1658 * 1 - USERDATA containing a struct socket
1659 * 2 - INTEGER with values of the macro defined below
1660 * If the integer is -1, we must read at most one line.
1661 * If the integer is -2, we ust read all the data until the
1662 * end of the stream.
1663 * If the integer is positive value, we must read a number of
1664 * bytes corresponding to this value.
1665 */
1666#define HLSR_READ_LINE (-1)
1667#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001668__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001669{
1670 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1671 int wanted = lua_tointeger(L, 2);
1672 struct hlua *hlua = hlua_gethlua(L);
1673 struct appctx *appctx;
1674 int len;
1675 int nblk;
1676 char *blk1;
1677 int len1;
1678 char *blk2;
1679 int len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001680 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001681 struct channel *oc;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001682
1683 /* Check if this lua stack is schedulable. */
1684 if (!hlua || !hlua->task)
1685 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1686 "'frontend', 'backend' or 'task'"));
1687
1688 /* check for connection closed. If some data where read, return it. */
1689 if (!socket->s)
1690 goto connection_closed;
1691
Willy Tarreau94aa6172015-03-13 14:19:06 +01001692 oc = &socket->s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001693 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001694 /* Read line. */
Willy Tarreau81389672015-03-10 12:03:52 +01001695 nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001696 if (nblk < 0) /* Connection close. */
1697 goto connection_closed;
1698 if (nblk == 0) /* No data avalaible. */
1699 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001700
1701 /* remove final \r\n. */
1702 if (nblk == 1) {
1703 if (blk1[len1-1] == '\n') {
1704 len1--;
1705 skip_at_end++;
1706 if (blk1[len1-1] == '\r') {
1707 len1--;
1708 skip_at_end++;
1709 }
1710 }
1711 }
1712 else {
1713 if (blk2[len2-1] == '\n') {
1714 len2--;
1715 skip_at_end++;
1716 if (blk2[len2-1] == '\r') {
1717 len2--;
1718 skip_at_end++;
1719 }
1720 }
1721 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001722 }
1723
1724 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001725 /* Read all the available data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001726 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001727 if (nblk < 0) /* Connection close. */
1728 goto connection_closed;
1729 if (nblk == 0) /* No data avalaible. */
1730 goto connection_empty;
1731 }
1732
1733 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001734 /* Read a block of 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 if (len1 > wanted) {
1742 nblk = 1;
1743 len1 = wanted;
1744 } if (nblk == 2 && len1 + len2 > wanted)
1745 len2 = wanted - len1;
1746 }
1747
1748 len = len1;
1749
1750 luaL_addlstring(&socket->b, blk1, len1);
1751 if (nblk == 2) {
1752 len += len2;
1753 luaL_addlstring(&socket->b, blk2, len2);
1754 }
1755
1756 /* Consume data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001757 bo_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001758
1759 /* Don't wait anything. */
Willy Tarreaude70fa12015-09-26 11:25:05 +02001760 stream_int_notify(&socket->s->si[0]);
1761 stream_int_update_applet(&socket->s->si[0]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001762
1763 /* If the pattern reclaim to read all the data
1764 * in the connection, got out.
1765 */
1766 if (wanted == HLSR_READ_ALL)
1767 goto connection_empty;
1768 else if (wanted >= 0 && len < wanted)
1769 goto connection_empty;
1770
1771 /* Return result. */
1772 luaL_pushresult(&socket->b);
1773 return 1;
1774
1775connection_closed:
1776
1777 /* If the buffer containds data. */
1778 if (socket->b.n > 0) {
1779 luaL_pushresult(&socket->b);
1780 return 1;
1781 }
1782 lua_pushnil(L);
1783 lua_pushstring(L, "connection closed.");
1784 return 2;
1785
1786connection_empty:
1787
1788 appctx = objt_appctx(socket->s->si[0].end);
1789 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_read))
1790 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001791 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001792 return 0;
1793}
1794
1795/* This Lus function gets two parameters. The first one can be string
1796 * or a number. If the string is "*l", the user require one line. If
1797 * the string is "*a", the user require all the content of the stream.
1798 * If the value is a number, the user require a number of bytes equal
1799 * to the value. The default value is "*l" (a line).
1800 *
1801 * This paraeter with a variable type is converted in integer. This
1802 * integer takes this values:
1803 * -1 : read a line
1804 * -2 : read all the stream
1805 * >0 : amount if bytes.
1806 *
1807 * The second parameter is optinal. It contains a string that must be
1808 * concatenated with the read data.
1809 */
1810__LJMP static int hlua_socket_receive(struct lua_State *L)
1811{
1812 int wanted = HLSR_READ_LINE;
1813 const char *pattern;
1814 int type;
1815 char *error;
1816 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001817 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001818
1819 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1820 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1821
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001822 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001823
1824 /* check for pattern. */
1825 if (lua_gettop(L) >= 2) {
1826 type = lua_type(L, 2);
1827 if (type == LUA_TSTRING) {
1828 pattern = lua_tostring(L, 2);
1829 if (strcmp(pattern, "*a") == 0)
1830 wanted = HLSR_READ_ALL;
1831 else if (strcmp(pattern, "*l") == 0)
1832 wanted = HLSR_READ_LINE;
1833 else {
1834 wanted = strtoll(pattern, &error, 10);
1835 if (*error != '\0')
1836 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1837 }
1838 }
1839 else if (type == LUA_TNUMBER) {
1840 wanted = lua_tointeger(L, 2);
1841 if (wanted < 0)
1842 WILL_LJMP(luaL_error(L, "Unsupported size."));
1843 }
1844 }
1845
1846 /* Set pattern. */
1847 lua_pushinteger(L, wanted);
1848 lua_replace(L, 2);
1849
1850 /* init bufffer, and fiil it wih prefix. */
1851 luaL_buffinit(L, &socket->b);
1852
1853 /* Check prefix. */
1854 if (lua_gettop(L) >= 3) {
1855 if (lua_type(L, 3) != LUA_TSTRING)
1856 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1857 pattern = lua_tolstring(L, 3, &len);
1858 luaL_addlstring(&socket->b, pattern, len);
1859 }
1860
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001861 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001862}
1863
1864/* Write the Lua input string in the output buffer.
1865 * This fucntion returns a yield if no space are available.
1866 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001867static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001868{
1869 struct hlua_socket *socket;
1870 struct hlua *hlua = hlua_gethlua(L);
1871 struct appctx *appctx;
1872 size_t buf_len;
1873 const char *buf;
1874 int len;
1875 int send_len;
1876 int sent;
1877
1878 /* Check if this lua stack is schedulable. */
1879 if (!hlua || !hlua->task)
1880 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1881 "'frontend', 'backend' or 'task'"));
1882
1883 /* Get object */
1884 socket = MAY_LJMP(hlua_checksocket(L, 1));
1885 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001886 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001887
1888 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001889 if (!socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001890 lua_pushinteger(L, -1);
1891 return 1;
1892 }
1893
1894 /* Update the input buffer data. */
1895 buf += sent;
1896 send_len = buf_len - sent;
1897
1898 /* All the data are sent. */
1899 if (sent >= buf_len)
1900 return 1; /* Implicitly return the length sent. */
1901
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001902 /* Check if the buffer is avalaible because HAProxy doesn't allocate
1903 * the request buffer if its not required.
1904 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001905 if (socket->s->req.buf->size == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001906 if (!stream_alloc_recv_buffer(&socket->s->req)) {
Willy Tarreau350f4872014-11-28 14:42:25 +01001907 socket->s->si[0].flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001908 goto hlua_socket_write_yield_return;
1909 }
1910 }
1911
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001912 /* Check for avalaible space. */
Willy Tarreau94aa6172015-03-13 14:19:06 +01001913 len = buffer_total_space(socket->s->req.buf);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001914 if (len <= 0)
1915 goto hlua_socket_write_yield_return;
1916
1917 /* send data */
1918 if (len < send_len)
1919 send_len = len;
Willy Tarreau94aa6172015-03-13 14:19:06 +01001920 len = bi_putblk(&socket->s->req, buf+sent, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001921
1922 /* "Not enough space" (-1), "Buffer too little to contain
1923 * the data" (-2) are not expected because the available length
1924 * is tested.
1925 * Other unknown error are also not expected.
1926 */
1927 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01001928 if (len == -1)
Willy Tarreau94aa6172015-03-13 14:19:06 +01001929 socket->s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001930
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001931 MAY_LJMP(hlua_socket_close(L));
1932 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001933 lua_pushinteger(L, -1);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001934 return 1;
1935 }
1936
1937 /* update buffers. */
Willy Tarreaude70fa12015-09-26 11:25:05 +02001938 stream_int_notify(&socket->s->si[0]);
1939 stream_int_update_applet(&socket->s->si[0]);
1940
Willy Tarreau94aa6172015-03-13 14:19:06 +01001941 socket->s->req.rex = TICK_ETERNITY;
1942 socket->s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001943
1944 /* Update length sent. */
1945 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001946 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001947
1948 /* All the data buffer is sent ? */
1949 if (sent + len >= buf_len)
1950 return 1;
1951
1952hlua_socket_write_yield_return:
1953 appctx = objt_appctx(socket->s->si[0].end);
1954 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
1955 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001956 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001957 return 0;
1958}
1959
1960/* This function initiate the send of data. It just check the input
1961 * parameters and push an integer in the Lua stack that contain the
1962 * amount of data writed in the buffer. This is used by the function
1963 * "hlua_socket_write_yield" that can yield.
1964 *
1965 * The Lua function gets between 3 and 4 parameters. The first one is
1966 * the associated object. The second is a string buffer. The third is
1967 * a facultative integer that represents where is the buffer position
1968 * of the start of the data that can send. The first byte is the
1969 * position "1". The default value is "1". The fourth argument is a
1970 * facultative integer that represents where is the buffer position
1971 * of the end of the data that can send. The default is the last byte.
1972 */
1973static int hlua_socket_send(struct lua_State *L)
1974{
1975 int i;
1976 int j;
1977 const char *buf;
1978 size_t buf_len;
1979
1980 /* Check number of arguments. */
1981 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
1982 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
1983
1984 /* Get the string. */
1985 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
1986
1987 /* Get and check j. */
1988 if (lua_gettop(L) == 4) {
1989 j = MAY_LJMP(luaL_checkinteger(L, 4));
1990 if (j < 0)
1991 j = buf_len + j + 1;
1992 if (j > buf_len)
1993 j = buf_len + 1;
1994 lua_pop(L, 1);
1995 }
1996 else
1997 j = buf_len;
1998
1999 /* Get and check i. */
2000 if (lua_gettop(L) == 3) {
2001 i = MAY_LJMP(luaL_checkinteger(L, 3));
2002 if (i < 0)
2003 i = buf_len + i + 1;
2004 if (i > buf_len)
2005 i = buf_len + 1;
2006 lua_pop(L, 1);
2007 } else
2008 i = 1;
2009
2010 /* Check bth i and j. */
2011 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002012 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002013 return 1;
2014 }
2015 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002016 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002017 return 1;
2018 }
2019 if (i == 0)
2020 i = 1;
2021 if (j == 0)
2022 j = 1;
2023
2024 /* Pop the string. */
2025 lua_pop(L, 1);
2026
2027 /* Update the buffer length. */
2028 buf += i - 1;
2029 buf_len = j - i + 1;
2030 lua_pushlstring(L, buf, buf_len);
2031
2032 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002033 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002034
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002035 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002036}
2037
Willy Tarreau22b0a682015-06-17 19:43:49 +02002038#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002039__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2040{
2041 static char buffer[SOCKET_INFO_MAX_LEN];
2042 int ret;
2043 int len;
2044 char *p;
2045
2046 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2047 if (ret <= 0) {
2048 lua_pushnil(L);
2049 return 1;
2050 }
2051
2052 if (ret == AF_UNIX) {
2053 lua_pushstring(L, buffer+1);
2054 return 1;
2055 }
2056 else if (ret == AF_INET6) {
2057 buffer[0] = '[';
2058 len = strlen(buffer);
2059 buffer[len] = ']';
2060 len++;
2061 buffer[len] = ':';
2062 len++;
2063 p = buffer;
2064 }
2065 else if (ret == AF_INET) {
2066 p = buffer + 1;
2067 len = strlen(p);
2068 p[len] = ':';
2069 len++;
2070 }
2071 else {
2072 lua_pushnil(L);
2073 return 1;
2074 }
2075
2076 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2077 lua_pushnil(L);
2078 return 1;
2079 }
2080
2081 lua_pushstring(L, p);
2082 return 1;
2083}
2084
2085/* Returns information about the peer of the connection. */
2086__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2087{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002088 struct hlua_socket *socket;
2089 struct connection *conn;
2090
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002091 MAY_LJMP(check_args(L, 1, "getpeername"));
2092
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002093 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002094
2095 /* Check if the tcp object is avalaible. */
2096 if (!socket->s) {
2097 lua_pushnil(L);
2098 return 1;
2099 }
2100
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002101 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002102 if (!conn) {
2103 lua_pushnil(L);
2104 return 1;
2105 }
2106
2107 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
2108 unsigned int salen = sizeof(conn->addr.to);
2109 if (getpeername(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, &salen) == -1) {
2110 lua_pushnil(L);
2111 return 1;
2112 }
2113 conn->flags |= CO_FL_ADDR_TO_SET;
2114 }
2115
2116 return MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2117}
2118
2119/* Returns information about my connection side. */
2120static int hlua_socket_getsockname(struct lua_State *L)
2121{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002122 struct hlua_socket *socket;
2123 struct connection *conn;
2124
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002125 MAY_LJMP(check_args(L, 1, "getsockname"));
2126
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002127 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002128
2129 /* Check if the tcp object is avalaible. */
2130 if (!socket->s) {
2131 lua_pushnil(L);
2132 return 1;
2133 }
2134
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002135 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002136 if (!conn) {
2137 lua_pushnil(L);
2138 return 1;
2139 }
2140
2141 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
2142 unsigned int salen = sizeof(conn->addr.from);
2143 if (getsockname(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, &salen) == -1) {
2144 lua_pushnil(L);
2145 return 1;
2146 }
2147 conn->flags |= CO_FL_ADDR_FROM_SET;
2148 }
2149
2150 return hlua_socket_info(L, &conn->addr.from);
2151}
2152
2153/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002154static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002155 .obj_type = OBJ_TYPE_APPLET,
2156 .name = "<LUA_TCP>",
2157 .fct = hlua_socket_handler,
2158 .release = hlua_socket_release,
2159};
2160
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002161__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002162{
2163 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2164 struct hlua *hlua = hlua_gethlua(L);
2165 struct appctx *appctx;
2166
2167 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002168 if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002169 lua_pushnil(L);
2170 lua_pushstring(L, "Can't connect");
2171 return 2;
2172 }
2173
2174 appctx = objt_appctx(socket->s->si[0].end);
2175
2176 /* Check for connection established. */
2177 if (appctx->ctx.hlua.connected) {
2178 lua_pushinteger(L, 1);
2179 return 1;
2180 }
2181
2182 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2183 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002184 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002185 return 0;
2186}
2187
2188/* This function fail or initite the connection. */
2189__LJMP static int hlua_socket_connect(struct lua_State *L)
2190{
2191 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002192 int port;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002193 const char *ip;
2194 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002195 struct hlua *hlua;
2196 struct appctx *appctx;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002197
2198 MAY_LJMP(check_args(L, 3, "connect"));
2199
2200 /* Get args. */
2201 socket = MAY_LJMP(hlua_checksocket(L, 1));
2202 ip = MAY_LJMP(luaL_checkstring(L, 2));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002203 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002204
Willy Tarreau973a5422015-08-05 21:47:23 +02002205 conn = si_alloc_conn(&socket->s->si[1]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002206 if (!conn)
2207 WILL_LJMP(luaL_error(L, "connect: internal error"));
2208
Willy Tarreau3adac082015-09-26 17:51:09 +02002209 /* needed for the connection not to be closed */
2210 conn->target = socket->s->target;
2211
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002212 /* Parse ip address. */
2213 conn->addr.to.ss_family = AF_UNSPEC;
2214 if (!str2ip2(ip, &conn->addr.to, 0))
2215 WILL_LJMP(luaL_error(L, "connect: cannot parse ip address '%s'", ip));
2216
2217 /* Set port. */
2218 if (conn->addr.to.ss_family == AF_INET)
2219 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2220 else if (conn->addr.to.ss_family == AF_INET6)
2221 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2222
2223 /* it is important not to call the wakeup function directly but to
2224 * pass through task_wakeup(), because this one knows how to apply
2225 * priorities to tasks.
2226 */
2227 task_wakeup(socket->s->task, TASK_WOKEN_INIT);
2228
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002229 hlua = hlua_gethlua(L);
2230 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002231
2232 /* inform the stream that we want to be notified whenever the
2233 * connection completes.
2234 */
2235 si_applet_cant_get(&socket->s->si[0]);
2236 si_applet_cant_put(&socket->s->si[0]);
2237
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002238 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2239 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002240 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002241
2242 return 0;
2243}
2244
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002245#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002246__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2247{
2248 struct hlua_socket *socket;
2249
2250 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2251 socket = MAY_LJMP(hlua_checksocket(L, 1));
2252 socket->s->target = &socket_ssl.obj_type;
2253 return MAY_LJMP(hlua_socket_connect(L));
2254}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002255#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002256
2257__LJMP static int hlua_socket_setoption(struct lua_State *L)
2258{
2259 return 0;
2260}
2261
2262__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2263{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002264 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002265 int tmout;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002266
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002267 MAY_LJMP(check_args(L, 2, "settimeout"));
2268
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002269 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002270 tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002271
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002272 socket->s->req.rto = tmout;
2273 socket->s->req.wto = tmout;
2274 socket->s->res.rto = tmout;
2275 socket->s->res.wto = tmout;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002276
2277 return 0;
2278}
2279
2280__LJMP static int hlua_socket_new(lua_State *L)
2281{
2282 struct hlua_socket *socket;
2283 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002284 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002285 struct stream *strm;
Willy Tarreaud420a972015-04-06 00:39:18 +02002286 struct task *task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002287
2288 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002289 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002290 hlua_pusherror(L, "socket: full stack");
2291 goto out_fail_conf;
2292 }
2293
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002294 /* Create the object: obj[0] = userdata. */
2295 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002296 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002297 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002298 memset(socket, 0, sizeof(*socket));
2299
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002300 /* Check if the various memory pools are intialized. */
Willy Tarreau87b09662015-04-03 00:22:06 +02002301 if (!pool2_stream || !pool2_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002302 hlua_pusherror(L, "socket: uninitialized pools.");
2303 goto out_fail_conf;
2304 }
2305
Willy Tarreau87b09662015-04-03 00:22:06 +02002306 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2308 lua_setmetatable(L, -2);
2309
Willy Tarreaud420a972015-04-06 00:39:18 +02002310 /* Create the applet context */
2311 appctx = appctx_new(&update_applet);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002312 if (!appctx) {
2313 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002314 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002315 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002316
Willy Tarreaud420a972015-04-06 00:39:18 +02002317 appctx->ctx.hlua.socket = socket;
2318 appctx->ctx.hlua.connected = 0;
2319 LIST_INIT(&appctx->ctx.hlua.wake_on_write);
2320 LIST_INIT(&appctx->ctx.hlua.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002321
Willy Tarreaud420a972015-04-06 00:39:18 +02002322 /* Now create a session, task and stream for this applet */
2323 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2324 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002325 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002326 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002327 }
2328
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002329 task = task_new();
2330 if (!task) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002331 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002332 goto out_fail_task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002333 }
Willy Tarreaud420a972015-04-06 00:39:18 +02002334 task->nice = 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002335
Willy Tarreau73b65ac2015-04-08 18:26:29 +02002336 strm = stream_new(sess, task, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002337 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002338 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002339 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002340 }
2341
Willy Tarreaud420a972015-04-06 00:39:18 +02002342 /* Configure an empty Lua for the stream. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002343 socket->s = strm;
2344 strm->hlua.T = NULL;
2345 strm->hlua.Tref = LUA_REFNIL;
2346 strm->hlua.Mref = LUA_REFNIL;
2347 strm->hlua.nargs = 0;
2348 strm->hlua.flags = 0;
2349 LIST_INIT(&strm->hlua.com);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002351 /* Configure "right" stream interface. this "si" is used to connect
2352 * and retrieve data from the server. The connection is initialized
2353 * with the "struct server".
2354 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002355 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002356
2357 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002358 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2359 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002360
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002361 /* Update statistics counters. */
2362 socket_proxy.feconn++; /* beconn will be increased later */
2363 jobs++;
2364 totalconn++;
2365
2366 /* Return yield waiting for connection. */
2367 return 1;
2368
Willy Tarreaud420a972015-04-06 00:39:18 +02002369 out_fail_stream:
2370 task_free(task);
2371 out_fail_task:
Willy Tarreau11c36242015-04-04 15:54:03 +02002372 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002373 out_fail_sess:
2374 appctx_free(appctx);
2375 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002376 WILL_LJMP(lua_error(L));
2377 return 0;
2378}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002379
2380/*
2381 *
2382 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002383 * Class Channel
2384 *
2385 *
2386 */
2387
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002388/* The state between the channel data and the HTTP parser state can be
2389 * unconsistent, so reset the parser and call it again. Warning, this
2390 * action not revalidate the request and not send a 400 if the modified
2391 * resuest is not valid.
2392 *
2393 * This function never fails. If dir is 0 we are a request, if it is 1
2394 * its a response.
2395 */
2396static void hlua_resynchonize_proto(struct stream *stream, int dir)
2397{
2398 /* Protocol HTTP. */
2399 if (stream->be->mode == PR_MODE_HTTP) {
2400
2401 if (dir == 0)
2402 http_txn_reset_req(stream->txn);
2403 else if (dir == 1)
2404 http_txn_reset_res(stream->txn);
2405
2406 if (stream->txn->hdr_idx.v)
2407 hdr_idx_init(&stream->txn->hdr_idx);
2408
2409 if (dir == 0)
2410 http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx);
2411 else if (dir == 1)
2412 http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx);
2413 }
2414}
2415
2416/* Check the protocole integrity after the Lua manipulations.
2417 * Close the stream and returns 0 if fails, otherwise returns 1.
2418 */
2419static int hlua_check_proto(struct stream *stream, int dir)
2420{
2421 const struct chunk msg = { .len = 0 };
2422
Willy Tarreau9af89f72015-09-26 11:50:08 +02002423 /* Protocol HTTP. The message parsing state must match the request or
2424 * response state. The problem that may happen is that Lua modifies
2425 * the request or response message *after* it was parsed, and corrupted
2426 * it so that it could not be processed anymore. We just need to verify
2427 * if the parser is still expected to run or not.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002428 */
2429 if (stream->be->mode == PR_MODE_HTTP) {
Willy Tarreau9af89f72015-09-26 11:50:08 +02002430 if (dir == 0 &&
2431 !(stream->req.analysers & AN_REQ_WAIT_HTTP) &&
2432 stream->txn->req.msg_state < HTTP_MSG_BODY) {
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002433 stream_int_retnclose(&stream->si[0], &msg);
2434 return 0;
2435 }
Willy Tarreau9af89f72015-09-26 11:50:08 +02002436 else if (dir == 1 &&
2437 !(stream->res.analysers & AN_RES_WAIT_HTTP) &&
2438 stream->txn->rsp.msg_state < HTTP_MSG_BODY) {
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002439 stream_int_retnclose(&stream->si[0], &msg);
2440 return 0;
2441 }
2442 }
2443 return 1;
2444}
2445
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002446/* Returns the struct hlua_channel join to the class channel in the
2447 * stack entry "ud" or throws an argument error.
2448 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002449__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002450{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002451 return (struct channel *)MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002452}
2453
Willy Tarreau47860ed2015-03-10 14:07:50 +01002454/* Pushes the channel onto the top of the stack. If the stask does not have a
2455 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002456 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002457static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002458{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002459 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002460 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002461 return 0;
2462
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002463 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002464 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002465 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002466
2467 /* Pop a class sesison metatable and affect it to the userdata. */
2468 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2469 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002470 return 1;
2471}
2472
2473/* Duplicate all the data present in the input channel and put it
2474 * in a string LUA variables. Returns -1 and push a nil value in
2475 * the stack if the channel is closed and all the data are consumed,
2476 * returns 0 if no data are available, otherwise it returns the length
2477 * of the builded string.
2478 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002479static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002480{
2481 char *blk1;
2482 char *blk2;
2483 int len1;
2484 int len2;
2485 int ret;
2486 luaL_Buffer b;
2487
Willy Tarreau47860ed2015-03-10 14:07:50 +01002488 ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002489 if (unlikely(ret == 0))
2490 return 0;
2491
2492 if (unlikely(ret < 0)) {
2493 lua_pushnil(L);
2494 return -1;
2495 }
2496
2497 luaL_buffinit(L, &b);
2498 luaL_addlstring(&b, blk1, len1);
2499 if (unlikely(ret == 2))
2500 luaL_addlstring(&b, blk2, len2);
2501 luaL_pushresult(&b);
2502
2503 if (unlikely(ret == 2))
2504 return len1 + len2;
2505 return len1;
2506}
2507
2508/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2509 * a yield. This function keep the data in the buffer.
2510 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002511__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002512{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002513 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002514
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002515 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2516
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002517 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002518 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002519 return 1;
2520}
2521
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002522/* Check arguments for the function "hlua_channel_dup_yield". */
2523__LJMP static int hlua_channel_dup(lua_State *L)
2524{
2525 MAY_LJMP(check_args(L, 1, "dup"));
2526 MAY_LJMP(hlua_checkchannel(L, 1));
2527 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2528}
2529
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002530/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2531 * a yield. This function consumes the data in the buffer. It returns
2532 * a string containing the data or a nil pointer if no data are available
2533 * and the channel is closed.
2534 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002535__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002536{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002537 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002538 int ret;
2539
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002540 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002541
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002542 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002543 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002544 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002545
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002546 if (unlikely(ret == -1))
2547 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002548
Willy Tarreau47860ed2015-03-10 14:07:50 +01002549 chn->buf->i -= ret;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002550 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002551 return 1;
2552}
2553
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002554/* Check arguments for the fucntion "hlua_channel_get_yield". */
2555__LJMP static int hlua_channel_get(lua_State *L)
2556{
2557 MAY_LJMP(check_args(L, 1, "get"));
2558 MAY_LJMP(hlua_checkchannel(L, 1));
2559 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2560}
2561
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002562/* This functions consumes and returns one line. If the channel is closed,
2563 * and the last data does not contains a final '\n', the data are returned
2564 * without the final '\n'. When no more data are avalaible, it returns nil
2565 * value.
2566 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002567__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002568{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002569 char *blk1;
2570 char *blk2;
2571 int len1;
2572 int len2;
2573 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002574 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002575 int ret;
2576 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002577
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002578 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2579
Willy Tarreau47860ed2015-03-10 14:07:50 +01002580 ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002581 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002582 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002583
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002584 if (ret == -1) {
2585 lua_pushnil(L);
2586 return 1;
2587 }
2588
2589 luaL_buffinit(L, &b);
2590 luaL_addlstring(&b, blk1, len1);
2591 len = len1;
2592 if (unlikely(ret == 2)) {
2593 luaL_addlstring(&b, blk2, len2);
2594 len += len2;
2595 }
2596 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002597 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002598 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002599 return 1;
2600}
2601
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002602/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2603__LJMP static int hlua_channel_getline(lua_State *L)
2604{
2605 MAY_LJMP(check_args(L, 1, "getline"));
2606 MAY_LJMP(hlua_checkchannel(L, 1));
2607 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2608}
2609
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002610/* This function takes a string as input, and append it at the
2611 * input side of channel. If the data is too big, but a space
2612 * is probably available after sending some data, the function
2613 * yield. If the data is bigger than the buffer, or if the
2614 * channel is closed, it returns -1. otherwise, it returns the
2615 * amount of data writed.
2616 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002617__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002618{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002619 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002620 size_t len;
2621 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2622 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2623 int ret;
2624 int max;
2625
Willy Tarreau47860ed2015-03-10 14:07:50 +01002626 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002627 if (max > len - l)
2628 max = len - l;
2629
Willy Tarreau47860ed2015-03-10 14:07:50 +01002630 ret = bi_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002631 if (ret == -2 || ret == -3) {
2632 lua_pushinteger(L, -1);
2633 return 1;
2634 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002635 if (ret == -1) {
2636 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002637 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002638 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002639 l += ret;
2640 lua_pop(L, 1);
2641 lua_pushinteger(L, l);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002642 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002643
Willy Tarreau47860ed2015-03-10 14:07:50 +01002644 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2645 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002646 /* There are no space avalaible, and the output buffer is empty.
2647 * in this case, we cannot add more data, so we cannot yield,
2648 * we return the amount of copyied data.
2649 */
2650 return 1;
2651 }
2652 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002653 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002654 return 1;
2655}
2656
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002657/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002658 * of the writed string, or -1 if the channel is closed or if the
2659 * buffer size is too little for the data.
2660 */
2661__LJMP static int hlua_channel_append(lua_State *L)
2662{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002663 size_t len;
2664
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002665 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002666 MAY_LJMP(hlua_checkchannel(L, 1));
2667 MAY_LJMP(luaL_checklstring(L, 2, &len));
2668 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002669 lua_pushinteger(L, 0);
2670
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002671 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002672}
2673
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002674/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002675 * his process by cleaning the buffer. The result is a replacement
2676 * of the current data. It returns the length of the writed string,
2677 * or -1 if the channel is closed or if the buffer size is too
2678 * little for the data.
2679 */
2680__LJMP static int hlua_channel_set(lua_State *L)
2681{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002682 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002683
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002684 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002685 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002686 lua_pushinteger(L, 0);
2687
Willy Tarreau47860ed2015-03-10 14:07:50 +01002688 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002689
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002690 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002691}
2692
2693/* Append data in the output side of the buffer. This data is immediatly
2694 * sent. The fcuntion returns the ammount of data writed. If the buffer
2695 * cannot contains the data, the function yield. The function returns -1
2696 * if the channel is closed.
2697 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002698__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002699{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002700 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002701 size_t len;
2702 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2703 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2704 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002705 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002706
Willy Tarreau47860ed2015-03-10 14:07:50 +01002707 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002708 lua_pushinteger(L, -1);
2709 return 1;
2710 }
2711
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002712 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2713 * the request buffer if its not required.
2714 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002715 if (chn->buf->size == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002716 if (!stream_alloc_recv_buffer(chn)) {
Willy Tarreau47860ed2015-03-10 14:07:50 +01002717 chn_prod(chn)->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002718 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002719 }
2720 }
2721
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002722 /* the writed data will be immediatly sent, so we can check
2723 * the avalaible space without taking in account the reserve.
2724 * The reserve is guaranted for the processing of incoming
2725 * data, because the buffer will be flushed.
2726 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002727 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002728
2729 /* If there are no space avalaible, and the output buffer is empty.
2730 * in this case, we cannot add more data, so we cannot yield,
2731 * we return the amount of copyied data.
2732 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002733 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002734 return 1;
2735
2736 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002737 if (max > len - l)
2738 max = len - l;
2739
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002740 /* The buffer avalaible size may be not contiguous. This test
2741 * detects a non contiguous buffer and realign it.
2742 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002743 if (bi_space_for_replace(chn->buf) < max)
2744 buffer_slow_realign(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002745
2746 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002747 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002748
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002749 /* buffer replace considers that the input part is filled.
2750 * so, I must forward these new data in the output part.
2751 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002752 b_adv(chn->buf, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002753
2754 l += max;
2755 lua_pop(L, 1);
2756 lua_pushinteger(L, l);
2757
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002758 /* If there are no space avalaible, and the output buffer is empty.
2759 * in this case, we cannot add more data, so we cannot yield,
2760 * we return the amount of copyied data.
2761 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002762 max = chn->buf->size - buffer_len(chn->buf);
2763 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002764 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002765
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002766 if (l < len) {
2767 /* If we are waiting for space in the response buffer, we
2768 * must set the flag WAKERESWR. This flag required the task
2769 * wake up if any activity is detected on the response buffer.
2770 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002771 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002772 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002773 else
2774 HLUA_SET_WAKEREQWR(hlua);
2775 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002776 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002777
2778 return 1;
2779}
2780
2781/* Just a wraper of "_hlua_channel_send". This wrapper permits
2782 * yield the LUA process, and resume it without checking the
2783 * input arguments.
2784 */
2785__LJMP static int hlua_channel_send(lua_State *L)
2786{
2787 MAY_LJMP(check_args(L, 2, "send"));
2788 lua_pushinteger(L, 0);
2789
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002790 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002791}
2792
2793/* This function forward and amount of butes. The data pass from
2794 * the input side of the buffer to the output side, and can be
2795 * forwarded. This function never fails.
2796 *
2797 * The Lua function takes an amount of bytes to be forwarded in
2798 * imput. It returns the number of bytes forwarded.
2799 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002800__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002801{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002802 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002803 int len;
2804 int l;
2805 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002806 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002807
2808 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2809 len = MAY_LJMP(luaL_checkinteger(L, 2));
2810 l = MAY_LJMP(luaL_checkinteger(L, -1));
2811
2812 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002813 if (max > chn->buf->i)
2814 max = chn->buf->i;
2815 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002816 l += max;
2817
2818 lua_pop(L, 1);
2819 lua_pushinteger(L, l);
2820
2821 /* Check if it miss bytes to forward. */
2822 if (l < len) {
2823 /* The the input channel or the output channel are closed, we
2824 * must return the amount of data forwarded.
2825 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002826 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002827 return 1;
2828
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002829 /* If we are waiting for space data in the response buffer, we
2830 * must set the flag WAKERESWR. This flag required the task
2831 * wake up if any activity is detected on the response buffer.
2832 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002833 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002834 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002835 else
2836 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002837
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002838 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002839 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002840 }
2841
2842 return 1;
2843}
2844
2845/* Just check the input and prepare the stack for the previous
2846 * function "hlua_channel_forward_yield"
2847 */
2848__LJMP static int hlua_channel_forward(lua_State *L)
2849{
2850 MAY_LJMP(check_args(L, 2, "forward"));
2851 MAY_LJMP(hlua_checkchannel(L, 1));
2852 MAY_LJMP(luaL_checkinteger(L, 2));
2853
2854 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002855 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002856}
2857
2858/* Just returns the number of bytes available in the input
2859 * side of the buffer. This function never fails.
2860 */
2861__LJMP static int hlua_channel_get_in_len(lua_State *L)
2862{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002863 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002864
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002865 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002866 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002867 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002868 return 1;
2869}
2870
2871/* Just returns the number of bytes available in the output
2872 * side of the buffer. This function never fails.
2873 */
2874__LJMP static int hlua_channel_get_out_len(lua_State *L)
2875{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002876 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002877
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002878 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002879 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002880 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002881 return 1;
2882}
2883
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002884/*
2885 *
2886 *
2887 * Class Fetches
2888 *
2889 *
2890 */
2891
2892/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002893 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002894 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002895__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002896{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002897 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002898}
2899
2900/* This function creates and push in the stack a fetch object according
2901 * with a current TXN.
2902 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002903static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002904{
Willy Tarreau7073c472015-04-06 11:15:40 +02002905 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002906
2907 /* Check stack size. */
2908 if (!lua_checkstack(L, 3))
2909 return 0;
2910
2911 /* Create the object: obj[0] = userdata.
2912 * Note that the base of the Fetches object is the
2913 * transaction object.
2914 */
2915 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002916 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002917 lua_rawseti(L, -2, 0);
2918
Willy Tarreau7073c472015-04-06 11:15:40 +02002919 hsmp->s = txn->s;
2920 hsmp->p = txn->p;
Willy Tarreau7073c472015-04-06 11:15:40 +02002921 hsmp->stringsafe = stringsafe;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002922
2923 /* Pop a class sesison metatable and affect it to the userdata. */
2924 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
2925 lua_setmetatable(L, -2);
2926
2927 return 1;
2928}
2929
2930/* This function is an LUA binding. It is called with each sample-fetch.
2931 * It uses closure argument to store the associated sample-fetch. It
2932 * returns only one argument or throws an error. An error is thrown
2933 * only if an error is encountered during the argument parsing. If
2934 * the "sample-fetch" function fails, nil is returned.
2935 */
2936__LJMP static int hlua_run_sample_fetch(lua_State *L)
2937{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002938 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01002939 struct sample_fetch *f;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002940 struct arg args[ARGM_NBARGS + 1];
2941 int i;
2942 struct sample smp;
2943
2944 /* Get closure arguments. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002945 f = (struct sample_fetch *)lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002946
2947 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002948 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002949
2950 /* Get extra arguments. */
2951 for (i = 0; i < lua_gettop(L) - 1; i++) {
2952 if (i >= ARGM_NBARGS)
2953 break;
2954 hlua_lua2arg(L, i + 2, &args[i]);
2955 }
2956 args[i].type = ARGT_STOP;
2957
2958 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002959 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002960
2961 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002962 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002963 lua_pushfstring(L, "error in arguments");
2964 WILL_LJMP(lua_error(L));
2965 }
2966
2967 /* Initialise the sample. */
2968 memset(&smp, 0, sizeof(smp));
2969
2970 /* Run the sample fetch process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02002971 smp.px = hsmp->p;
2972 smp.sess = hsmp->s->sess;
2973 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02002974 smp.opt = 0;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002975 if (!f->process(args, &smp, f->kw, f->private)) {
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002976 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002977 lua_pushstring(L, "");
2978 else
2979 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002980 return 1;
2981 }
2982
2983 /* Convert the returned sample in lua value. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002984 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002985 hlua_smp2lua_str(L, &smp);
2986 else
2987 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002988 return 1;
2989}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002990
2991/*
2992 *
2993 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002994 * Class Converters
2995 *
2996 *
2997 */
2998
2999/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003000 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003001 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003002__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003003{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003004 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003005}
3006
3007/* This function creates and push in the stack a Converters object
3008 * according with a current TXN.
3009 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003010static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003011{
Willy Tarreau7073c472015-04-06 11:15:40 +02003012 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003013
3014 /* Check stack size. */
3015 if (!lua_checkstack(L, 3))
3016 return 0;
3017
3018 /* Create the object: obj[0] = userdata.
3019 * Note that the base of the Converters object is the
3020 * same than the TXN object.
3021 */
3022 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003023 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003024 lua_rawseti(L, -2, 0);
3025
Willy Tarreau7073c472015-04-06 11:15:40 +02003026 hsmp->s = txn->s;
3027 hsmp->p = txn->p;
Willy Tarreau7073c472015-04-06 11:15:40 +02003028 hsmp->stringsafe = stringsafe;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003029
Willy Tarreau87b09662015-04-03 00:22:06 +02003030 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003031 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3032 lua_setmetatable(L, -2);
3033
3034 return 1;
3035}
3036
3037/* This function is an LUA binding. It is called with each converter.
3038 * It uses closure argument to store the associated converter. It
3039 * returns only one argument or throws an error. An error is thrown
3040 * only if an error is encountered during the argument parsing. If
3041 * the converter function function fails, nil is returned.
3042 */
3043__LJMP static int hlua_run_sample_conv(lua_State *L)
3044{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003045 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003046 struct sample_conv *conv;
3047 struct arg args[ARGM_NBARGS + 1];
3048 int i;
3049 struct sample smp;
3050
3051 /* Get closure arguments. */
3052 conv = (struct sample_conv *)lua_touserdata(L, lua_upvalueindex(1));
3053
3054 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003055 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003056
3057 /* Get extra arguments. */
3058 for (i = 0; i < lua_gettop(L) - 2; i++) {
3059 if (i >= ARGM_NBARGS)
3060 break;
3061 hlua_lua2arg(L, i + 3, &args[i]);
3062 }
3063 args[i].type = ARGT_STOP;
3064
3065 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003066 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003067
3068 /* Run the special args checker. */
3069 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3070 hlua_pusherror(L, "error in arguments");
3071 WILL_LJMP(lua_error(L));
3072 }
3073
3074 /* Initialise the sample. */
3075 if (!hlua_lua2smp(L, 2, &smp)) {
3076 hlua_pusherror(L, "error in the input argument");
3077 WILL_LJMP(lua_error(L));
3078 }
3079
3080 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003081 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003082 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003083 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003084 WILL_LJMP(lua_error(L));
3085 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003086 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3087 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003088 hlua_pusherror(L, "error during the input argument casting");
3089 WILL_LJMP(lua_error(L));
3090 }
3091
3092 /* Run the sample conversion process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02003093 smp.px = hsmp->p;
3094 smp.sess = hsmp->s->sess;
3095 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02003096 smp.opt = 0;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003097 if (!conv->process(args, &smp, conv->private)) {
Willy Tarreauda5f1082015-04-06 11:17:13 +02003098 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003099 lua_pushstring(L, "");
3100 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003101 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003102 return 1;
3103 }
3104
3105 /* Convert the returned sample in lua value. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003106 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003107 hlua_smp2lua_str(L, &smp);
3108 else
3109 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003110 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003111}
3112
3113/*
3114 *
3115 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003116 * Class HTTP
3117 *
3118 *
3119 */
3120
3121/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003122 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003123 */
3124__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
3125{
3126 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
3127}
3128
3129/* This function creates and push in the stack a HTTP object
3130 * according with a current TXN.
3131 */
3132static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
3133{
Willy Tarreau9a8ad862015-04-06 11:14:06 +02003134 struct hlua_txn *htxn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003135
3136 /* Check stack size. */
3137 if (!lua_checkstack(L, 3))
3138 return 0;
3139
3140 /* Create the object: obj[0] = userdata.
3141 * Note that the base of the Converters object is the
3142 * same than the TXN object.
3143 */
3144 lua_newtable(L);
Willy Tarreau9a8ad862015-04-06 11:14:06 +02003145 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003146 lua_rawseti(L, -2, 0);
3147
Willy Tarreau9a8ad862015-04-06 11:14:06 +02003148 htxn->s = txn->s;
3149 htxn->p = txn->p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003150
Willy Tarreau87b09662015-04-03 00:22:06 +02003151 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003152 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
3153 lua_setmetatable(L, -2);
3154
3155 return 1;
3156}
3157
3158/* This function creates ans returns an array of HTTP headers.
3159 * This function does not fails. It is used as wrapper with the
3160 * 2 following functions.
3161 */
3162__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3163{
3164 const char *cur_ptr, *cur_next, *p;
3165 int old_idx, cur_idx;
3166 struct hdr_idx_elem *cur_hdr;
3167 const char *hn, *hv;
3168 int hnl, hvl;
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003169 int type;
3170 const char *in;
3171 char *out;
3172 int len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003173
3174 /* Create the table. */
3175 lua_newtable(L);
3176
Willy Tarreaueee5b512015-04-03 23:46:31 +02003177 if (!htxn->s->txn)
3178 return 1;
3179
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003180 /* Build array of headers. */
3181 old_idx = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003182 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003183
3184 while (1) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02003185 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003186 if (!cur_idx)
3187 break;
3188 old_idx = cur_idx;
3189
Willy Tarreaueee5b512015-04-03 23:46:31 +02003190 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003191 cur_ptr = cur_next;
3192 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
3193
3194 /* Now we have one full header at cur_ptr of len cur_hdr->len,
3195 * and the next header starts at cur_next. We'll check
3196 * this header in the list as well as against the default
3197 * rule.
3198 */
3199
3200 /* look for ': *'. */
3201 hn = cur_ptr;
3202 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
3203 if (p >= cur_ptr+cur_hdr->len)
3204 continue;
3205 hnl = p - hn;
3206 p++;
3207 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
3208 p++;
3209 if (p >= cur_ptr+cur_hdr->len)
3210 continue;
3211 hv = p;
3212 hvl = cur_ptr+cur_hdr->len-p;
3213
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003214 /* Lowercase the key. Don't check the size of trash, it have
3215 * the size of one buffer and the input data contains in one
3216 * buffer.
3217 */
3218 out = trash.str;
3219 for (in=hn; in<hn+hnl; in++, out++)
3220 *out = tolower(*in);
3221 *out = '\0';
3222
3223 /* Check for existing entry:
3224 * assume that the table is on the top of the stack, and
3225 * push the key in the stack, the function lua_gettable()
3226 * perform the lookup.
3227 */
3228 lua_pushlstring(L, trash.str, hnl);
3229 lua_gettable(L, -2);
3230 type = lua_type(L, -1);
3231
3232 switch (type) {
3233 case LUA_TNIL:
3234 /* Table not found, create it. */
3235 lua_pop(L, 1); /* remove the nil value. */
3236 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
3237 lua_newtable(L); /* create and push empty table. */
3238 lua_pushlstring(L, hv, hvl); /* push header value. */
3239 lua_rawseti(L, -2, 0); /* index header value (pop it). */
3240 lua_rawset(L, -3); /* index new table with header name (pop the values). */
3241 break;
3242
3243 case LUA_TTABLE:
3244 /* Entry found: push the value in the table. */
3245 len = lua_rawlen(L, -1);
3246 lua_pushlstring(L, hv, hvl); /* push header value. */
3247 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
3248 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
3249 break;
3250
3251 default:
3252 /* Other cases are errors. */
3253 hlua_pusherror(L, "internal error during the parsing of headers.");
3254 WILL_LJMP(lua_error(L));
3255 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003256 }
3257
3258 return 1;
3259}
3260
3261__LJMP static int hlua_http_req_get_headers(lua_State *L)
3262{
3263 struct hlua_txn *htxn;
3264
3265 MAY_LJMP(check_args(L, 1, "req_get_headers"));
3266 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3267
Willy Tarreaueee5b512015-04-03 23:46:31 +02003268 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003269}
3270
3271__LJMP static int hlua_http_res_get_headers(lua_State *L)
3272{
3273 struct hlua_txn *htxn;
3274
3275 MAY_LJMP(check_args(L, 1, "res_get_headers"));
3276 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3277
Willy Tarreaueee5b512015-04-03 23:46:31 +02003278 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003279}
3280
3281/* This function replace full header, or just a value in
3282 * the request or in the response. It is a wrapper fir the
3283 * 4 following functions.
3284 */
3285__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
3286 struct http_msg *msg, int action)
3287{
3288 size_t name_len;
3289 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3290 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
3291 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
3292 struct my_regex re;
3293
3294 if (!regex_comp(reg, &re, 1, 1, NULL))
3295 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
3296
3297 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
3298 regex_free(&re);
3299 return 0;
3300}
3301
3302__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
3303{
3304 struct hlua_txn *htxn;
3305
3306 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3307 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3308
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003309 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003310}
3311
3312__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
3313{
3314 struct hlua_txn *htxn;
3315
3316 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
3317 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3318
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003319 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003320}
3321
3322__LJMP static int hlua_http_req_rep_val(lua_State *L)
3323{
3324 struct hlua_txn *htxn;
3325
3326 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3327 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3328
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003329 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003330}
3331
3332__LJMP static int hlua_http_res_rep_val(lua_State *L)
3333{
3334 struct hlua_txn *htxn;
3335
3336 MAY_LJMP(check_args(L, 4, "res_rep_val"));
3337 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3338
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003339 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003340}
3341
3342/* This function deletes all the occurences of an header.
3343 * It is a wrapper for the 2 following functions.
3344 */
3345__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3346{
3347 size_t len;
3348 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3349 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003350 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003351
3352 ctx.idx = 0;
3353 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
3354 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3355 return 0;
3356}
3357
3358__LJMP static int hlua_http_req_del_hdr(lua_State *L)
3359{
3360 struct hlua_txn *htxn;
3361
3362 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3363 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3364
Willy Tarreaueee5b512015-04-03 23:46:31 +02003365 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003366}
3367
3368__LJMP static int hlua_http_res_del_hdr(lua_State *L)
3369{
3370 struct hlua_txn *htxn;
3371
3372 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3373 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3374
Willy Tarreaueee5b512015-04-03 23:46:31 +02003375 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003376}
3377
3378/* This function adds an header. It is a wrapper used by
3379 * the 2 following functions.
3380 */
3381__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3382{
3383 size_t name_len;
3384 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3385 size_t value_len;
3386 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
3387 char *p;
3388
3389 /* Check length. */
3390 trash.len = value_len + name_len + 2;
3391 if (trash.len > trash.size)
3392 return 0;
3393
3394 /* Creates the header string. */
3395 p = trash.str;
3396 memcpy(p, name, name_len);
3397 p += name_len;
3398 *p = ':';
3399 p++;
3400 *p = ' ';
3401 p++;
3402 memcpy(p, value, value_len);
3403
Willy Tarreaueee5b512015-04-03 23:46:31 +02003404 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003405 trash.str, trash.len) != 0);
3406
3407 return 0;
3408}
3409
3410__LJMP static int hlua_http_req_add_hdr(lua_State *L)
3411{
3412 struct hlua_txn *htxn;
3413
3414 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
3415 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3416
Willy Tarreaueee5b512015-04-03 23:46:31 +02003417 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003418}
3419
3420__LJMP static int hlua_http_res_add_hdr(lua_State *L)
3421{
3422 struct hlua_txn *htxn;
3423
3424 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
3425 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3426
Willy Tarreaueee5b512015-04-03 23:46:31 +02003427 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003428}
3429
3430static int hlua_http_req_set_hdr(lua_State *L)
3431{
3432 struct hlua_txn *htxn;
3433
3434 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
3435 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3436
Willy Tarreaueee5b512015-04-03 23:46:31 +02003437 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
3438 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003439}
3440
3441static int hlua_http_res_set_hdr(lua_State *L)
3442{
3443 struct hlua_txn *htxn;
3444
3445 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
3446 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3447
Willy Tarreaueee5b512015-04-03 23:46:31 +02003448 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
3449 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003450}
3451
3452/* This function set the method. */
3453static int hlua_http_req_set_meth(lua_State *L)
3454{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003455 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003456 size_t name_len;
3457 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003458
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003459 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003460 return 1;
3461}
3462
3463/* This function set the method. */
3464static int hlua_http_req_set_path(lua_State *L)
3465{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003466 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003467 size_t name_len;
3468 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003469 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003470 return 1;
3471}
3472
3473/* This function set the query-string. */
3474static int hlua_http_req_set_query(lua_State *L)
3475{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003476 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003477 size_t name_len;
3478 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003479
3480 /* Check length. */
3481 if (name_len > trash.size - 1) {
3482 lua_pushboolean(L, 0);
3483 return 1;
3484 }
3485
3486 /* Add the mark question as prefix. */
3487 chunk_reset(&trash);
3488 trash.str[trash.len++] = '?';
3489 memcpy(trash.str + trash.len, name, name_len);
3490 trash.len += name_len;
3491
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003492 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003493 return 1;
3494}
3495
3496/* This function set the uri. */
3497static int hlua_http_req_set_uri(lua_State *L)
3498{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003499 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003500 size_t name_len;
3501 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003502
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003503 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003504 return 1;
3505}
3506
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02003507/* This function set the response code. */
3508static int hlua_http_res_set_status(lua_State *L)
3509{
3510 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3511 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
3512
3513 http_set_status(code, htxn->s);
3514 return 0;
3515}
3516
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003517/*
3518 *
3519 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003520 * Class TXN
3521 *
3522 *
3523 */
3524
3525/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003526 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003527 */
3528__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
3529{
3530 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
3531}
3532
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02003533__LJMP static int hlua_set_var(lua_State *L)
3534{
3535 struct hlua_txn *htxn;
3536 const char *name;
3537 size_t len;
3538 struct sample smp;
3539
3540 MAY_LJMP(check_args(L, 3, "set_var"));
3541
3542 /* It is useles to retrieve the stream, but this function
3543 * runs only in a stream context.
3544 */
3545 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3546 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3547
3548 /* Converts the third argument in a sample. */
3549 hlua_lua2smp(L, 3, &smp);
3550
3551 /* Store the sample in a variable. */
3552 vars_set_by_name(name, len, htxn->s, &smp);
3553 return 0;
3554}
3555
3556__LJMP static int hlua_get_var(lua_State *L)
3557{
3558 struct hlua_txn *htxn;
3559 const char *name;
3560 size_t len;
3561 struct sample smp;
3562
3563 MAY_LJMP(check_args(L, 2, "get_var"));
3564
3565 /* It is useles to retrieve the stream, but this function
3566 * runs only in a stream context.
3567 */
3568 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3569 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3570
3571 if (!vars_get_by_name(name, len, htxn->s, &smp)) {
3572 lua_pushnil(L);
3573 return 1;
3574 }
3575
3576 return hlua_smp2lua(L, &smp);
3577}
3578
Willy Tarreau59551662015-03-10 14:23:13 +01003579__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003580{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003581 struct hlua *hlua;
3582
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003583 MAY_LJMP(check_args(L, 2, "set_priv"));
3584
Willy Tarreau87b09662015-04-03 00:22:06 +02003585 /* It is useles to retrieve the stream, but this function
3586 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003587 */
3588 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003589 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003590
3591 /* Remove previous value. */
3592 if (hlua->Mref != -1)
3593 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
3594
3595 /* Get and store new value. */
3596 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3597 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3598
3599 return 0;
3600}
3601
Willy Tarreau59551662015-03-10 14:23:13 +01003602__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003603{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003604 struct hlua *hlua;
3605
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003606 MAY_LJMP(check_args(L, 1, "get_priv"));
3607
Willy Tarreau87b09662015-04-03 00:22:06 +02003608 /* It is useles to retrieve the stream, but this function
3609 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003610 */
3611 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003612 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003613
3614 /* Push configuration index in the stack. */
3615 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3616
3617 return 1;
3618}
3619
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003620/* Create stack entry containing a class TXN. This function
3621 * return 0 if the stack does not contains free slots,
3622 * otherwise it returns 1.
3623 */
Willy Tarreau15e91e12015-04-04 00:52:09 +02003624static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003625{
Willy Tarreaude491382015-04-06 11:04:28 +02003626 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003627
3628 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003629 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003630 return 0;
3631
3632 /* NOTE: The allocation never fails. The failure
3633 * throw an error, and the function never returns.
3634 * if the throw is not avalaible, the process is aborted.
3635 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003636 /* Create the object: obj[0] = userdata. */
3637 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02003638 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003639 lua_rawseti(L, -2, 0);
3640
Willy Tarreaude491382015-04-06 11:04:28 +02003641 htxn->s = s;
3642 htxn->p = p;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003643
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003644 /* Create the "f" field that contains a list of fetches. */
3645 lua_pushstring(L, "f");
Willy Tarreaude491382015-04-06 11:04:28 +02003646 if (!hlua_fetches_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003647 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003648 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003649
3650 /* Create the "sf" field that contains a list of stringsafe fetches. */
3651 lua_pushstring(L, "sf");
Willy Tarreaude491382015-04-06 11:04:28 +02003652 if (!hlua_fetches_new(L, htxn, 1))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003653 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003654 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003655
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003656 /* Create the "c" field that contains a list of converters. */
3657 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02003658 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003659 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003660 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003661
3662 /* Create the "sc" field that contains a list of stringsafe converters. */
3663 lua_pushstring(L, "sc");
Willy Tarreaude491382015-04-06 11:04:28 +02003664 if (!hlua_converters_new(L, htxn, 1))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003665 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003666 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003667
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003668 /* Create the "req" field that contains the request channel object. */
3669 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003670 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003671 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003672 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003673
3674 /* Create the "res" field that contains the response channel object. */
3675 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003676 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003677 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003678 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003679
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003680 /* Creates the HTTP object is the current proxy allows http. */
3681 lua_pushstring(L, "http");
3682 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02003683 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003684 return 0;
3685 }
3686 else
3687 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02003688 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003689
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003690 /* Pop a class sesison metatable and affect it to the userdata. */
3691 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
3692 lua_setmetatable(L, -2);
3693
3694 return 1;
3695}
3696
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003697__LJMP static int hlua_txn_deflog(lua_State *L)
3698{
3699 const char *msg;
3700 struct hlua_txn *htxn;
3701
3702 MAY_LJMP(check_args(L, 2, "deflog"));
3703 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3704 msg = MAY_LJMP(luaL_checkstring(L, 2));
3705
3706 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
3707 return 0;
3708}
3709
3710__LJMP static int hlua_txn_log(lua_State *L)
3711{
3712 int level;
3713 const char *msg;
3714 struct hlua_txn *htxn;
3715
3716 MAY_LJMP(check_args(L, 3, "log"));
3717 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3718 level = MAY_LJMP(luaL_checkinteger(L, 2));
3719 msg = MAY_LJMP(luaL_checkstring(L, 3));
3720
3721 if (level < 0 || level >= NB_LOG_LEVELS)
3722 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3723
3724 hlua_sendlog(htxn->s->be, level, msg);
3725 return 0;
3726}
3727
3728__LJMP static int hlua_txn_log_debug(lua_State *L)
3729{
3730 const char *msg;
3731 struct hlua_txn *htxn;
3732
3733 MAY_LJMP(check_args(L, 2, "Debug"));
3734 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3735 msg = MAY_LJMP(luaL_checkstring(L, 2));
3736 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
3737 return 0;
3738}
3739
3740__LJMP static int hlua_txn_log_info(lua_State *L)
3741{
3742 const char *msg;
3743 struct hlua_txn *htxn;
3744
3745 MAY_LJMP(check_args(L, 2, "Info"));
3746 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3747 msg = MAY_LJMP(luaL_checkstring(L, 2));
3748 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
3749 return 0;
3750}
3751
3752__LJMP static int hlua_txn_log_warning(lua_State *L)
3753{
3754 const char *msg;
3755 struct hlua_txn *htxn;
3756
3757 MAY_LJMP(check_args(L, 2, "Warning"));
3758 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3759 msg = MAY_LJMP(luaL_checkstring(L, 2));
3760 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
3761 return 0;
3762}
3763
3764__LJMP static int hlua_txn_log_alert(lua_State *L)
3765{
3766 const char *msg;
3767 struct hlua_txn *htxn;
3768
3769 MAY_LJMP(check_args(L, 2, "Alert"));
3770 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3771 msg = MAY_LJMP(luaL_checkstring(L, 2));
3772 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
3773 return 0;
3774}
3775
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003776__LJMP static int hlua_txn_set_loglevel(lua_State *L)
3777{
3778 struct hlua_txn *htxn;
3779 int ll;
3780
3781 MAY_LJMP(check_args(L, 2, "set_loglevel"));
3782 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3783 ll = MAY_LJMP(luaL_checkinteger(L, 2));
3784
3785 if (ll < 0 || ll > 7)
3786 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
3787
3788 htxn->s->logs.level = ll;
3789 return 0;
3790}
3791
3792__LJMP static int hlua_txn_set_tos(lua_State *L)
3793{
3794 struct hlua_txn *htxn;
3795 struct connection *cli_conn;
3796 int tos;
3797
3798 MAY_LJMP(check_args(L, 2, "set_tos"));
3799 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3800 tos = MAY_LJMP(luaL_checkinteger(L, 2));
3801
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003802 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003803 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, tos);
3804
3805 return 0;
3806}
3807
3808__LJMP static int hlua_txn_set_mark(lua_State *L)
3809{
3810#ifdef SO_MARK
3811 struct hlua_txn *htxn;
3812 struct connection *cli_conn;
3813 int mark;
3814
3815 MAY_LJMP(check_args(L, 2, "set_mark"));
3816 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3817 mark = MAY_LJMP(luaL_checkinteger(L, 2));
3818
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003819 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau07081fe2015-04-06 10:59:20 +02003820 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003821#endif
3822 return 0;
3823}
3824
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003825/* This function is an Lua binding that send pending data
3826 * to the client, and close the stream interface.
3827 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02003828__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003829{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003830 struct hlua_txn *htxn;
Willy Tarreau81389672015-03-10 12:03:52 +01003831 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003832
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003833 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003834 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003835
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003836 ic = &htxn->s->req;
3837 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01003838
Willy Tarreau630ef452015-08-28 10:06:15 +02003839 if (htxn->s->txn) {
3840 /* HTTP mode, let's stay in sync with the stream */
3841 bi_fast_delete(ic->buf, htxn->s->txn->req.sov);
3842 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
3843 htxn->s->txn->req.sov = 0;
3844 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
3845 oc->analysers = AN_RES_HTTP_XFER_BODY;
3846 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
3847 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
3848
3849 /* Trim any possible response */
3850 oc->buf->i = 0;
3851 htxn->s->txn->rsp.next = htxn->s->txn->rsp.sov = 0;
3852
3853 /* Note that if we want to support keep-alive, we need
3854 * to bypass the close/shutr_now calls below, but that
3855 * may only be done if the HTTP request was already
3856 * processed and the connection header is known (ie
3857 * not during TCP rules).
3858 */
3859 }
3860
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02003861 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01003862 channel_abort(ic);
3863 channel_auto_close(ic);
3864 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02003865
3866 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01003867 channel_auto_read(oc);
3868 channel_auto_close(oc);
3869 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003870
Willy Tarreau0458b082015-08-28 09:40:04 +02003871 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02003872
3873 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003874 return 0;
3875}
3876
3877__LJMP static int hlua_log(lua_State *L)
3878{
3879 int level;
3880 const char *msg;
3881
3882 MAY_LJMP(check_args(L, 2, "log"));
3883 level = MAY_LJMP(luaL_checkinteger(L, 1));
3884 msg = MAY_LJMP(luaL_checkstring(L, 2));
3885
3886 if (level < 0 || level >= NB_LOG_LEVELS)
3887 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3888
3889 hlua_sendlog(NULL, level, msg);
3890 return 0;
3891}
3892
3893__LJMP static int hlua_log_debug(lua_State *L)
3894{
3895 const char *msg;
3896
3897 MAY_LJMP(check_args(L, 1, "debug"));
3898 msg = MAY_LJMP(luaL_checkstring(L, 1));
3899 hlua_sendlog(NULL, LOG_DEBUG, msg);
3900 return 0;
3901}
3902
3903__LJMP static int hlua_log_info(lua_State *L)
3904{
3905 const char *msg;
3906
3907 MAY_LJMP(check_args(L, 1, "info"));
3908 msg = MAY_LJMP(luaL_checkstring(L, 1));
3909 hlua_sendlog(NULL, LOG_INFO, msg);
3910 return 0;
3911}
3912
3913__LJMP static int hlua_log_warning(lua_State *L)
3914{
3915 const char *msg;
3916
3917 MAY_LJMP(check_args(L, 1, "warning"));
3918 msg = MAY_LJMP(luaL_checkstring(L, 1));
3919 hlua_sendlog(NULL, LOG_WARNING, msg);
3920 return 0;
3921}
3922
3923__LJMP static int hlua_log_alert(lua_State *L)
3924{
3925 const char *msg;
3926
3927 MAY_LJMP(check_args(L, 1, "alert"));
3928 msg = MAY_LJMP(luaL_checkstring(L, 1));
3929 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003930 return 0;
3931}
3932
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003933__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003934{
3935 int wakeup_ms = lua_tointeger(L, -1);
3936 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003937 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003938 return 0;
3939}
3940
3941__LJMP static int hlua_sleep(lua_State *L)
3942{
3943 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003944 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003945
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003946 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003947
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003948 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003949 wakeup_ms = tick_add(now_ms, delay);
3950 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003951
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003952 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3953 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003954}
3955
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003956__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003957{
3958 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003959 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003960
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003961 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003962
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003963 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003964 wakeup_ms = tick_add(now_ms, delay);
3965 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003966
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003967 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3968 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003969}
3970
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003971/* This functionis an LUA binding. it permits to give back
3972 * the hand at the HAProxy scheduler. It is used when the
3973 * LUA processing consumes a lot of time.
3974 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003975__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003976{
3977 return 0;
3978}
3979
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003980__LJMP static int hlua_yield(lua_State *L)
3981{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003982 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
3983 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003984}
3985
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003986/* This function change the nice of the currently executed
3987 * task. It is used set low or high priority at the current
3988 * task.
3989 */
Willy Tarreau59551662015-03-10 14:23:13 +01003990__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003991{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003992 struct hlua *hlua;
3993 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003994
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003995 MAY_LJMP(check_args(L, 1, "set_nice"));
3996 hlua = hlua_gethlua(L);
3997 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003998
3999 /* If he task is not set, I'm in a start mode. */
4000 if (!hlua || !hlua->task)
4001 return 0;
4002
4003 if (nice < -1024)
4004 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004005 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01004006 nice = 1024;
4007
4008 hlua->task->nice = nice;
4009 return 0;
4010}
4011
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004012/* This function is used as a calback of a task. It is called by the
4013 * HAProxy task subsystem when the task is awaked. The LUA runtime can
4014 * return an E_AGAIN signal, the emmiter of this signal must set a
4015 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004016 *
4017 * Task wrapper are longjmp safe because the only one Lua code
4018 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004019 */
4020static struct task *hlua_process_task(struct task *task)
4021{
4022 struct hlua *hlua = task->context;
4023 enum hlua_exec status;
4024
4025 /* We need to remove the task from the wait queue before executing
4026 * the Lua code because we don't know if it needs to wait for
4027 * another timer or not in the case of E_AGAIN.
4028 */
4029 task_delete(task);
4030
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004031 /* If it is the first call to the task, we must initialize the
4032 * execution timeouts.
4033 */
4034 if (!HLUA_IS_RUNNING(hlua))
Camilo Lopez685c0142015-08-02 19:07:28 -04004035 hlua->expire = tick_add_ifset(now_ms, hlua_timeout_task);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004036
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004037 /* Execute the Lua code. */
4038 status = hlua_ctx_resume(hlua, 1);
4039
4040 switch (status) {
4041 /* finished or yield */
4042 case HLUA_E_OK:
4043 hlua_ctx_destroy(hlua);
4044 task_delete(task);
4045 task_free(task);
4046 break;
4047
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004048 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
4049 if (hlua->wake_time != TICK_ETERNITY)
4050 task_schedule(task, hlua->wake_time);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004051 break;
4052
4053 /* finished with error. */
4054 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004055 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004056 hlua_ctx_destroy(hlua);
4057 task_delete(task);
4058 task_free(task);
4059 break;
4060
4061 case HLUA_E_ERR:
4062 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004063 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004064 hlua_ctx_destroy(hlua);
4065 task_delete(task);
4066 task_free(task);
4067 break;
4068 }
4069 return NULL;
4070}
4071
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004072/* This function is an LUA binding that register LUA function to be
4073 * executed after the HAProxy configuration parsing and before the
4074 * HAProxy scheduler starts. This function expect only one LUA
4075 * argument that is a function. This function returns nothing, but
4076 * throws if an error is encountered.
4077 */
4078__LJMP static int hlua_register_init(lua_State *L)
4079{
4080 struct hlua_init_function *init;
4081 int ref;
4082
4083 MAY_LJMP(check_args(L, 1, "register_init"));
4084
4085 ref = MAY_LJMP(hlua_checkfunction(L, 1));
4086
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004087 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004088 if (!init)
4089 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4090
4091 init->function_ref = ref;
4092 LIST_ADDQ(&hlua_init_functions, &init->l);
4093 return 0;
4094}
4095
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004096/* This functio is an LUA binding. It permits to register a task
4097 * executed in parallel of the main HAroxy activity. The task is
4098 * created and it is set in the HAProxy scheduler. It can be called
4099 * from the "init" section, "post init" or during the runtime.
4100 *
4101 * Lua prototype:
4102 *
4103 * <none> core.register_task(<function>)
4104 */
4105static int hlua_register_task(lua_State *L)
4106{
4107 struct hlua *hlua;
4108 struct task *task;
4109 int ref;
4110
4111 MAY_LJMP(check_args(L, 1, "register_task"));
4112
4113 ref = MAY_LJMP(hlua_checkfunction(L, 1));
4114
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004115 hlua = calloc(1, sizeof(*hlua));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004116 if (!hlua)
4117 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4118
4119 task = task_new();
4120 task->context = hlua;
4121 task->process = hlua_process_task;
4122
4123 if (!hlua_ctx_init(hlua, task))
4124 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4125
4126 /* Restore the function in the stack. */
4127 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
4128 hlua->nargs = 0;
4129
4130 /* Schedule task. */
4131 task_schedule(task, now_ms);
4132
4133 return 0;
4134}
4135
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004136/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
4137 * doesn't allow "yield" functions because the HAProxy engine cannot
4138 * resume converters.
4139 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004140static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004141{
4142 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004143 struct stream *stream = smp->strm;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004144
Willy Tarreau87b09662015-04-03 00:22:06 +02004145 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004146 * Lua context can be not initialized. This behavior
4147 * permits to save performances because a systematic
4148 * Lua initialization cause 5% performances loss.
4149 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004150 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004151 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004152 return 0;
4153 }
4154
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004155 /* If it is the first run, initialize the data for the call. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004156 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004157
4158 /* The following Lua calls can fail. */
4159 if (!SET_SAFE_LJMP(stream->hlua.T)) {
4160 SEND_ERR(stream->be, "Lua converter '%s': critical error.\n", fcn->name);
4161 return 0;
4162 }
4163
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004164 /* Check stack available size. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004165 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004166 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004167 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004168 return 0;
4169 }
4170
4171 /* Restore the function in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004172 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004173
4174 /* convert input sample and pust-it in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004175 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004176 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004177 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004178 return 0;
4179 }
Willy Tarreau87b09662015-04-03 00:22:06 +02004180 hlua_smp2lua(stream->hlua.T, smp);
4181 stream->hlua.nargs = 2;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004182
4183 /* push keywords in the stack. */
4184 if (arg_p) {
4185 for (; arg_p->type != ARGT_STOP; arg_p++) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004186 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004187 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004188 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004189 return 0;
4190 }
Willy Tarreau87b09662015-04-03 00:22:06 +02004191 hlua_arg2lua(stream->hlua.T, arg_p);
4192 stream->hlua.nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004193 }
4194 }
4195
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004196 /* We must initialize the execution timeouts. */
Thierry FOURNIER61e96c62015-08-09 13:10:24 +02004197 stream->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004198
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004199 /* At this point the execution is safe. */
4200 RESET_SAFE_LJMP(stream->hlua.T);
4201
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004202 /* Set the currently running flag. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004203 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004204 }
4205
4206 /* Execute the function. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004207 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004208 /* finished. */
4209 case HLUA_E_OK:
4210 /* Convert the returned value in sample. */
Willy Tarreau87b09662015-04-03 00:22:06 +02004211 hlua_lua2smp(stream->hlua.T, -1, smp);
4212 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004213 return 1;
4214
4215 /* yield. */
4216 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004217 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004218 return 0;
4219
4220 /* finished with error. */
4221 case HLUA_E_ERRMSG:
4222 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004223 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
4224 fcn->name, lua_tostring(stream->hlua.T, -1));
Willy Tarreau87b09662015-04-03 00:22:06 +02004225 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004226 return 0;
4227
4228 case HLUA_E_ERR:
4229 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004230 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004231
4232 default:
4233 return 0;
4234 }
4235}
4236
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004237/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
4238 * doesn't allow "yield" functions because the HAProxy engine cannot
4239 * resume sample-fetches.
4240 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004241static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
4242 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004243{
4244 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004245 struct stream *stream = smp->strm;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004246
Willy Tarreau87b09662015-04-03 00:22:06 +02004247 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004248 * Lua context can be not initialized. This behavior
4249 * permits to save performances because a systematic
4250 * Lua initialization cause 5% performances loss.
4251 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004252 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004253 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004254 return 0;
4255 }
4256
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004257 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004258 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004259
4260 /* The following Lua calls can fail. */
4261 if (!SET_SAFE_LJMP(stream->hlua.T)) {
4262 SEND_ERR(smp->px, "Lua sample-fetch '%s': critical error.\n", fcn->name);
4263 return 0;
4264 }
4265
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004266 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004267 if (!lua_checkstack(stream->hlua.T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004268 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004269 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004270 return 0;
4271 }
4272
4273 /* Restore the function in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004274 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004275
4276 /* push arguments in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004277 if (!hlua_txn_new(stream->hlua.T, stream, smp->px)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004278 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004279 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004280 return 0;
4281 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004282 stream->hlua.nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004283
4284 /* push keywords in the stack. */
4285 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
4286 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004287 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004288 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004289 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004290 return 0;
4291 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004292 if (!lua_checkstack(stream->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004293 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004294 RESET_SAFE_LJMP(stream->hlua.T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004295 return 0;
4296 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004297 hlua_arg2lua(stream->hlua.T, arg_p);
4298 stream->hlua.nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004299 }
4300
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004301 /* We must initialize the execution timeouts. */
Thierry FOURNIER61e96c62015-08-09 13:10:24 +02004302 stream->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004303
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004304 /* At this point the execution is safe. */
4305 RESET_SAFE_LJMP(stream->hlua.T);
4306
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004307 /* Set the currently running flag. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004308 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004309 }
4310
4311 /* Execute the function. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004312 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004313 /* finished. */
4314 case HLUA_E_OK:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004315 if (!hlua_check_proto(stream, !(smp->opt & SMP_OPT_DIR_REQ)))
4316 return 0;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004317 /* Convert the returned value in sample. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004318 hlua_lua2smp(stream->hlua.T, -1, smp);
4319 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004320
4321 /* Set the end of execution flag. */
4322 smp->flags &= ~SMP_F_MAY_CHANGE;
4323 return 1;
4324
4325 /* yield. */
4326 case HLUA_E_AGAIN:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004327 hlua_check_proto(stream, !(smp->opt & SMP_OPT_DIR_REQ));
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004328 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004329 return 0;
4330
4331 /* finished with error. */
4332 case HLUA_E_ERRMSG:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004333 hlua_check_proto(stream, !(smp->opt & SMP_OPT_DIR_REQ));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004334 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004335 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
4336 fcn->name, lua_tostring(stream->hlua.T, -1));
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004337 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004338 return 0;
4339
4340 case HLUA_E_ERR:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004341 hlua_check_proto(stream, !(smp->opt & SMP_OPT_DIR_REQ));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004342 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004343 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004344
4345 default:
4346 return 0;
4347 }
4348}
4349
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004350/* This function is an LUA binding used for registering
4351 * "sample-conv" functions. It expects a converter name used
4352 * in the haproxy configuration file, and an LUA function.
4353 */
4354__LJMP static int hlua_register_converters(lua_State *L)
4355{
4356 struct sample_conv_kw_list *sck;
4357 const char *name;
4358 int ref;
4359 int len;
4360 struct hlua_function *fcn;
4361
4362 MAY_LJMP(check_args(L, 2, "register_converters"));
4363
4364 /* First argument : converter name. */
4365 name = MAY_LJMP(luaL_checkstring(L, 1));
4366
4367 /* Second argument : lua function. */
4368 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4369
4370 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004371 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004372 if (!sck)
4373 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004374 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004375 if (!fcn)
4376 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4377
4378 /* Fill fcn. */
4379 fcn->name = strdup(name);
4380 if (!fcn->name)
4381 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4382 fcn->function_ref = ref;
4383
4384 /* List head */
4385 sck->list.n = sck->list.p = NULL;
4386
4387 /* converter keyword. */
4388 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004389 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004390 if (!sck->kw[0].kw)
4391 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4392
4393 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
4394 sck->kw[0].process = hlua_sample_conv_wrapper;
4395 sck->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4396 sck->kw[0].val_args = NULL;
4397 sck->kw[0].in_type = SMP_T_STR;
4398 sck->kw[0].out_type = SMP_T_STR;
4399 sck->kw[0].private = fcn;
4400
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004401 /* Register this new converter */
4402 sample_register_convs(sck);
4403
4404 return 0;
4405}
4406
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004407/* This fucntion is an LUA binding used for registering
4408 * "sample-fetch" functions. It expects a converter name used
4409 * in the haproxy configuration file, and an LUA function.
4410 */
4411__LJMP static int hlua_register_fetches(lua_State *L)
4412{
4413 const char *name;
4414 int ref;
4415 int len;
4416 struct sample_fetch_kw_list *sfk;
4417 struct hlua_function *fcn;
4418
4419 MAY_LJMP(check_args(L, 2, "register_fetches"));
4420
4421 /* First argument : sample-fetch name. */
4422 name = MAY_LJMP(luaL_checkstring(L, 1));
4423
4424 /* Second argument : lua function. */
4425 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4426
4427 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004428 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004429 if (!sfk)
4430 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004431 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004432 if (!fcn)
4433 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4434
4435 /* Fill fcn. */
4436 fcn->name = strdup(name);
4437 if (!fcn->name)
4438 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4439 fcn->function_ref = ref;
4440
4441 /* List head */
4442 sfk->list.n = sfk->list.p = NULL;
4443
4444 /* sample-fetch keyword. */
4445 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004446 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004447 if (!sfk->kw[0].kw)
4448 return luaL_error(L, "lua out of memory error.");
4449
4450 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
4451 sfk->kw[0].process = hlua_sample_fetch_wrapper;
4452 sfk->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4453 sfk->kw[0].val_args = NULL;
4454 sfk->kw[0].out_type = SMP_T_STR;
4455 sfk->kw[0].use = SMP_USE_HTTP_ANY;
4456 sfk->kw[0].val = 0;
4457 sfk->kw[0].private = fcn;
4458
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004459 /* Register this new fetch. */
4460 sample_register_fetches(sfk);
4461
4462 return 0;
4463}
4464
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004465/* This function is a wrapper to execute each LUA function declared
4466 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004467 * return ACT_RET_CONT if the processing is finished (with or without
4468 * error) and return ACT_RET_YIELD if the function must be called again
4469 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004470 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004471static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
4472 struct session *sess, struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004473{
4474 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004475 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004476 int dir;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004477
4478 switch (rule->from) {
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004479 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = 0; break;
4480 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = 1; break;
4481 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = 0; break;
4482 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = 1; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004483 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004484 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004485 return ACT_RET_CONT;
4486 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004487
Willy Tarreau87b09662015-04-03 00:22:06 +02004488 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004489 * Lua context can be not initialized. This behavior
4490 * permits to save performances because a systematic
4491 * Lua initialization cause 5% performances loss.
4492 */
4493 if (!s->hlua.T && !hlua_ctx_init(&s->hlua, s->task)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004494 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004495 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004496 return ACT_RET_CONT;
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004497 }
4498
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004499 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004500 if (!HLUA_IS_RUNNING(&s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004501
4502 /* The following Lua calls can fail. */
4503 if (!SET_SAFE_LJMP(s->hlua.T)) {
4504 SEND_ERR(px, "Lua function '%s': critical error.\n",
4505 rule->arg.hlua_rule->fcn.name);
4506 return ACT_RET_CONT;
4507 }
4508
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004509 /* Check stack available size. */
4510 if (!lua_checkstack(s->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004511 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004512 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004513 RESET_SAFE_LJMP(s->hlua.T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004514 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004515 }
4516
4517 /* Restore the function in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004518 lua_rawgeti(s->hlua.T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004519
Willy Tarreau87b09662015-04-03 00:22:06 +02004520 /* Create and and push object stream in the stack. */
Willy Tarreau15e91e12015-04-04 00:52:09 +02004521 if (!hlua_txn_new(s->hlua.T, s, px)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004522 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004523 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004524 RESET_SAFE_LJMP(s->hlua.T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004525 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004526 }
4527 s->hlua.nargs = 1;
4528
4529 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004530 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004531 if (!lua_checkstack(s->hlua.T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004532 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004533 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER10e5bc72015-09-25 23:51:34 +02004534 RESET_SAFE_LJMP(s->hlua.T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004535 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004536 }
4537 lua_pushstring(s->hlua.T, *arg);
4538 s->hlua.nargs++;
4539 }
4540
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004541 /* Now the execution is safe. */
4542 RESET_SAFE_LJMP(s->hlua.T);
4543
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004544 /* We must initialize the execution timeouts. */
Thierry FOURNIER61e96c62015-08-09 13:10:24 +02004545 s->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004546
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004547 /* Set the currently running flag. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004548 HLUA_SET_RUN(&s->hlua);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004549 }
4550
4551 /* Execute the function. */
4552 switch (hlua_ctx_resume(&s->hlua, 1)) {
4553 /* finished. */
4554 case HLUA_E_OK:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004555 if (!hlua_check_proto(s, dir))
4556 return ACT_RET_ERR;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004557 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004558
4559 /* yield. */
4560 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004561 /* Set timeout in the required channel. */
4562 if (s->hlua.wake_time != TICK_ETERNITY) {
4563 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004564 s->req.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004565 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004566 s->res.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004567 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004568 /* Some actions can be wake up when a "write" event
4569 * is detected on a response channel. This is useful
4570 * only for actions targetted on the requests.
4571 */
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01004572 if (HLUA_IS_WAKERESWR(&s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004573 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01004574 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004575 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004576 }
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01004577 if (HLUA_IS_WAKEREQWR(&s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004578 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004579 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004580
4581 /* finished with error. */
4582 case HLUA_E_ERRMSG:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004583 if (!hlua_check_proto(s, dir))
4584 return ACT_RET_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004585 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004586 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004587 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua.T, -1));
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004588 lua_pop(s->hlua.T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004589 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004590
4591 case HLUA_E_ERR:
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02004592 if (!hlua_check_proto(s, dir))
4593 return ACT_RET_ERR;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004594 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02004595 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004596 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004597
4598 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004599 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004600 }
4601}
4602
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004603/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
4604 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004605 *
4606 * This function can fail with an abort() due to an Lua critical error.
4607 * We are in the configuration parsing process of HAProxy, this abort() is
4608 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004609 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004610static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4611 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004612{
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004613 struct hlua_function *fcn = (struct hlua_function *)rule->kw->private;
4614
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004615 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004616 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004617 if (!rule->arg.hlua_rule) {
4618 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02004619 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004620 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004621
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004622 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004623 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004624
4625 /* TODO: later accept arguments. */
4626 rule->arg.hlua_rule->args = NULL;
4627
Thierry FOURNIER42148732015-09-02 17:17:33 +02004628 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02004629 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02004630 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004631}
4632
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004633/* This function is an LUA binding used for registering
4634 * "sample-conv" functions. It expects a converter name used
4635 * in the haproxy configuration file, and an LUA function.
4636 */
4637__LJMP static int hlua_register_action(lua_State *L)
4638{
4639 struct action_kw_list *akl;
4640 const char *name;
4641 int ref;
4642 int len;
4643 struct hlua_function *fcn;
4644
4645 MAY_LJMP(check_args(L, 3, "register_service"));
4646
4647 /* First argument : converter name. */
4648 name = MAY_LJMP(luaL_checkstring(L, 1));
4649
4650 /* Second argument : environment. */
4651 if (lua_type(L, 2) != LUA_TTABLE)
4652 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
4653
4654 /* Third argument : lua function. */
4655 ref = MAY_LJMP(hlua_checkfunction(L, 3));
4656
4657 /* browse the second argulent as an array. */
4658 lua_pushnil(L);
4659 while (lua_next(L, 2) != 0) {
4660 if (lua_type(L, -1) != LUA_TSTRING)
4661 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
4662
4663 /* Check required environment. Only accepted "http" or "tcp". */
4664 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004665 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004666 if (!akl)
4667 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004668 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004669 if (!fcn)
4670 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4671
4672 /* Fill fcn. */
4673 fcn->name = strdup(name);
4674 if (!fcn->name)
4675 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4676 fcn->function_ref = ref;
4677
4678 /* List head */
4679 akl->list.n = akl->list.p = NULL;
4680
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004681 /* action keyword. */
4682 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02004683 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004684 if (!akl->kw[0].kw)
4685 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4686
4687 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
4688
4689 akl->kw[0].match_pfx = 0;
4690 akl->kw[0].private = fcn;
4691 akl->kw[0].parse = action_register_lua;
4692
4693 /* select the action registering point. */
4694 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
4695 tcp_req_cont_keywords_register(akl);
4696 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
4697 tcp_res_cont_keywords_register(akl);
4698 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
4699 http_req_keywords_register(akl);
4700 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
4701 http_res_keywords_register(akl);
4702 else
4703 WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. "
4704 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
4705 "are expected.", lua_tostring(L, -1)));
4706
4707 /* pop the environment string. */
4708 lua_pop(L, 1);
4709 }
4710
4711 return 0;
4712}
4713
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004714static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
4715 struct proxy *defpx, const char *file, int line,
4716 char **err, unsigned int *timeout)
4717{
4718 const char *error;
4719
4720 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
4721 if (error && *error != '\0') {
4722 memprintf(err, "%s: invalid timeout", args[0]);
4723 return -1;
4724 }
4725 return 0;
4726}
4727
4728static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
4729 struct proxy *defpx, const char *file, int line,
4730 char **err)
4731{
4732 return hlua_read_timeout(args, section_type, curpx, defpx,
4733 file, line, err, &hlua_timeout_session);
4734}
4735
4736static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
4737 struct proxy *defpx, const char *file, int line,
4738 char **err)
4739{
4740 return hlua_read_timeout(args, section_type, curpx, defpx,
4741 file, line, err, &hlua_timeout_task);
4742}
4743
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004744static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
4745 struct proxy *defpx, const char *file, int line,
4746 char **err)
4747{
4748 char *error;
4749
4750 hlua_nb_instruction = strtoll(args[1], &error, 10);
4751 if (*error != '\0') {
4752 memprintf(err, "%s: invalid number", args[0]);
4753 return -1;
4754 }
4755 return 0;
4756}
4757
Willy Tarreau32f61e22015-03-18 17:54:59 +01004758static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
4759 struct proxy *defpx, const char *file, int line,
4760 char **err)
4761{
4762 char *error;
4763
4764 if (*(args[1]) == 0) {
4765 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
4766 return -1;
4767 }
4768 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
4769 if (*error != '\0') {
4770 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
4771 return -1;
4772 }
4773 return 0;
4774}
4775
4776
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004777/* This function is called by the main configuration key "lua-load". It loads and
4778 * execute an lua file during the parsing of the HAProxy configuration file. It is
4779 * the main lua entry point.
4780 *
4781 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
4782 * occured, otherwise it returns 0.
4783 *
4784 * In some error case, LUA set an error message in top of the stack. This function
4785 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004786 *
4787 * This function can fail with an abort() due to an Lua critical error.
4788 * We are in the configuration parsing process of HAProxy, this abort() is
4789 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004790 */
4791static int hlua_load(char **args, int section_type, struct proxy *curpx,
4792 struct proxy *defpx, const char *file, int line,
4793 char **err)
4794{
4795 int error;
4796
4797 /* Just load and compile the file. */
4798 error = luaL_loadfile(gL.T, args[1]);
4799 if (error) {
4800 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
4801 lua_pop(gL.T, 1);
4802 return -1;
4803 }
4804
4805 /* If no syntax error where detected, execute the code. */
4806 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
4807 switch (error) {
4808 case LUA_OK:
4809 break;
4810 case LUA_ERRRUN:
4811 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
4812 lua_pop(gL.T, 1);
4813 return -1;
4814 case LUA_ERRMEM:
4815 memprintf(err, "lua out of memory error\n");
4816 return -1;
4817 case LUA_ERRERR:
4818 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
4819 lua_pop(gL.T, 1);
4820 return -1;
4821 case LUA_ERRGCMM:
4822 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
4823 lua_pop(gL.T, 1);
4824 return -1;
4825 default:
4826 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
4827 lua_pop(gL.T, 1);
4828 return -1;
4829 }
4830
4831 return 0;
4832}
4833
4834/* configuration keywords declaration */
4835static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004836 { CFG_GLOBAL, "lua-load", hlua_load },
4837 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
4838 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004839 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01004840 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004841 { 0, NULL, NULL },
4842}};
4843
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004844/* This function can fail with an abort() due to an Lua critical error.
4845 * We are in the initialisation process of HAProxy, this abort() is
4846 * tolerated.
4847 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004848int hlua_post_init()
4849{
4850 struct hlua_init_function *init;
4851 const char *msg;
4852 enum hlua_exec ret;
4853
4854 list_for_each_entry(init, &hlua_init_functions, l) {
4855 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
4856 ret = hlua_ctx_resume(&gL, 0);
4857 switch (ret) {
4858 case HLUA_E_OK:
4859 lua_pop(gL.T, -1);
4860 return 1;
4861 case HLUA_E_AGAIN:
4862 Alert("lua init: yield not allowed.\n");
4863 return 0;
4864 case HLUA_E_ERRMSG:
4865 msg = lua_tostring(gL.T, -1);
4866 Alert("lua init: %s.\n", msg);
4867 return 0;
4868 case HLUA_E_ERR:
4869 default:
4870 Alert("lua init: unknown runtime error.\n");
4871 return 0;
4872 }
4873 }
4874 return 1;
4875}
4876
Willy Tarreau32f61e22015-03-18 17:54:59 +01004877/* The memory allocator used by the Lua stack. <ud> is a pointer to the
4878 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
4879 * is the previously allocated size or the kind of object in case of a new
4880 * allocation. <nsize> is the requested new size.
4881 */
4882static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
4883{
4884 struct hlua_mem_allocator *zone = ud;
4885
4886 if (nsize == 0) {
4887 /* it's a free */
4888 if (ptr)
4889 zone->allocated -= osize;
4890 free(ptr);
4891 return NULL;
4892 }
4893
4894 if (!ptr) {
4895 /* it's a new allocation */
4896 if (zone->limit && zone->allocated + nsize > zone->limit)
4897 return NULL;
4898
4899 ptr = malloc(nsize);
4900 if (ptr)
4901 zone->allocated += nsize;
4902 return ptr;
4903 }
4904
4905 /* it's a realloc */
4906 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
4907 return NULL;
4908
4909 ptr = realloc(ptr, nsize);
4910 if (ptr)
4911 zone->allocated += nsize - osize;
4912 return ptr;
4913}
4914
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004915/* Ithis function can fail with an abort() due to an Lua critical error.
4916 * We are in the initialisation process of HAProxy, this abort() is
4917 * tolerated.
4918 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01004919void hlua_init(void)
4920{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004921 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004922 int idx;
4923 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004924 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004925 char *p;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004926#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004927 struct srv_kw *kw;
4928 int tmp_error;
4929 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01004930 char *args[] = { /* SSL client configuration. */
4931 "ssl",
4932 "verify",
4933 "none",
4934 "force-sslv3",
4935 NULL
4936 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004937#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004938
Willy Tarreau87b09662015-04-03 00:22:06 +02004939 /* Initialise com signals pool */
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004940 pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED);
4941
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004942 /* Register configuration keywords. */
4943 cfg_register_keywords(&cfg_kws);
4944
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004945 /* Init main lua stack. */
4946 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004947 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004948 LIST_INIT(&gL.com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004949 gL.T = luaL_newstate();
4950 hlua_sethlua(&gL);
4951 gL.Tref = LUA_REFNIL;
4952 gL.task = NULL;
4953
Thierry FOURNIERbabae282015-09-17 11:36:37 +02004954 /* From this point, until the end of the initialisation fucntion,
4955 * the Lua function can fail with an abort. We are in the initialisation
4956 * process of HAProxy, this abort() is tolerated.
4957 */
4958
Willy Tarreau32f61e22015-03-18 17:54:59 +01004959 /* change the memory allocators to track memory usage */
4960 lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator);
4961
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004962 /* Initialise lua. */
4963 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004964
4965 /*
4966 *
4967 * Create "core" object.
4968 *
4969 */
4970
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01004971 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004972 lua_newtable(gL.T);
4973
4974 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004975 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004976 hlua_class_const_int(gL.T, log_levels[i], i);
4977
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004978 /* Register special functions. */
4979 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004980 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004981 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004982 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02004983 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01004984 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01004985 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01004986 hlua_class_function(gL.T, "sleep", hlua_sleep);
4987 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01004988 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
4989 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
4990 hlua_class_function(gL.T, "set_map", hlua_set_map);
4991 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01004992 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01004993 hlua_class_function(gL.T, "log", hlua_log);
4994 hlua_class_function(gL.T, "Debug", hlua_log_debug);
4995 hlua_class_function(gL.T, "Info", hlua_log_info);
4996 hlua_class_function(gL.T, "Warning", hlua_log_warning);
4997 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02004998 hlua_class_function(gL.T, "done", hlua_done);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004999
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01005000 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005001
5002 /*
5003 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02005004 * Register class Map
5005 *
5006 */
5007
5008 /* This table entry is the object "Map" base. */
5009 lua_newtable(gL.T);
5010
5011 /* register pattern types. */
5012 for (i=0; i<PAT_MATCH_NUM; i++)
5013 hlua_class_const_int(gL.T, pat_match_names[i], i);
5014
5015 /* register constructor. */
5016 hlua_class_function(gL.T, "new", hlua_map_new);
5017
5018 /* Create and fill the metatable. */
5019 lua_newtable(gL.T);
5020
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005021 /* Create the __tostring identifier */
5022 lua_pushstring(gL.T, "__tostring");
5023 lua_pushstring(gL.T, CLASS_MAP);
5024 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5025 lua_rawset(gL.T, -3);
5026
Thierry FOURNIER3def3932015-04-07 11:27:54 +02005027 /* Create and fille the __index entry. */
5028 lua_pushstring(gL.T, "__index");
5029 lua_newtable(gL.T);
5030
5031 /* Register . */
5032 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
5033 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
5034
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005035 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02005036
5037 /* Register previous table in the registry with reference and named entry. */
5038 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5039 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5040 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_MAP); /* register class session. */
5041 class_map_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5042
5043 /* Assign the metatable to the mai Map object. */
5044 lua_setmetatable(gL.T, -2);
5045
5046 /* Set a name to the table. */
5047 lua_setglobal(gL.T, "Map");
5048
5049 /*
5050 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01005051 * Register class Channel
5052 *
5053 */
5054
5055 /* Create and fill the metatable. */
5056 lua_newtable(gL.T);
5057
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005058 /* Create the __tostring identifier */
5059 lua_pushstring(gL.T, "__tostring");
5060 lua_pushstring(gL.T, CLASS_CHANNEL);
5061 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5062 lua_rawset(gL.T, -3);
5063
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01005064 /* Create and fille the __index entry. */
5065 lua_pushstring(gL.T, "__index");
5066 lua_newtable(gL.T);
5067
5068 /* Register . */
5069 hlua_class_function(gL.T, "get", hlua_channel_get);
5070 hlua_class_function(gL.T, "dup", hlua_channel_dup);
5071 hlua_class_function(gL.T, "getline", hlua_channel_getline);
5072 hlua_class_function(gL.T, "set", hlua_channel_set);
5073 hlua_class_function(gL.T, "append", hlua_channel_append);
5074 hlua_class_function(gL.T, "send", hlua_channel_send);
5075 hlua_class_function(gL.T, "forward", hlua_channel_forward);
5076 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
5077 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
5078
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005079 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01005080
5081 /* Register previous table in the registry with reference and named entry. */
5082 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5083 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CHANNEL); /* register class session. */
5084 class_channel_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5085
5086 /*
5087 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005088 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005089 *
5090 */
5091
5092 /* Create and fill the metatable. */
5093 lua_newtable(gL.T);
5094
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005095 /* Create the __tostring identifier */
5096 lua_pushstring(gL.T, "__tostring");
5097 lua_pushstring(gL.T, CLASS_FETCHES);
5098 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5099 lua_rawset(gL.T, -3);
5100
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005101 /* Create and fille the __index entry. */
5102 lua_pushstring(gL.T, "__index");
5103 lua_newtable(gL.T);
5104
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01005105 /* Browse existing fetches and create the associated
5106 * object method.
5107 */
5108 sf = NULL;
5109 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
5110
5111 /* Dont register the keywork if the arguments check function are
5112 * not safe during the runtime.
5113 */
5114 if ((sf->val_args != NULL) &&
5115 (sf->val_args != val_payload_lv) &&
5116 (sf->val_args != val_hdr))
5117 continue;
5118
5119 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
5120 * by an underscore.
5121 */
5122 strncpy(trash.str, sf->kw, trash.size);
5123 trash.str[trash.size - 1] = '\0';
5124 for (p = trash.str; *p; p++)
5125 if (*p == '.' || *p == '-' || *p == '+')
5126 *p = '_';
5127
5128 /* Register the function. */
5129 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01005130 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01005131 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005132 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01005133 }
5134
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005135 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005136
5137 /* Register previous table in the registry with reference and named entry. */
5138 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5139 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_FETCHES); /* register class session. */
5140 class_fetches_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5141
5142 /*
5143 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005144 * Register class Converters
5145 *
5146 */
5147
5148 /* Create and fill the metatable. */
5149 lua_newtable(gL.T);
5150
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005151 /* Create the __tostring identifier */
5152 lua_pushstring(gL.T, "__tostring");
5153 lua_pushstring(gL.T, CLASS_CONVERTERS);
5154 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5155 lua_rawset(gL.T, -3);
5156
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005157 /* Create and fill the __index entry. */
5158 lua_pushstring(gL.T, "__index");
5159 lua_newtable(gL.T);
5160
5161 /* Browse existing converters and create the associated
5162 * object method.
5163 */
5164 sc = NULL;
5165 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
5166 /* Dont register the keywork if the arguments check function are
5167 * not safe during the runtime.
5168 */
5169 if (sc->val_args != NULL)
5170 continue;
5171
5172 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
5173 * by an underscore.
5174 */
5175 strncpy(trash.str, sc->kw, trash.size);
5176 trash.str[trash.size - 1] = '\0';
5177 for (p = trash.str; *p; p++)
5178 if (*p == '.' || *p == '-' || *p == '+')
5179 *p = '_';
5180
5181 /* Register the function. */
5182 lua_pushstring(gL.T, trash.str);
5183 lua_pushlightuserdata(gL.T, sc);
5184 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005185 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005186 }
5187
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005188 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005189
5190 /* Register previous table in the registry with reference and named entry. */
5191 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5192 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CONVERTERS); /* register class session. */
5193 class_converters_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5194
5195 /*
5196 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005197 * Register class HTTP
5198 *
5199 */
5200
5201 /* Create and fill the metatable. */
5202 lua_newtable(gL.T);
5203
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005204 /* Create the __tostring identifier */
5205 lua_pushstring(gL.T, "__tostring");
5206 lua_pushstring(gL.T, CLASS_HTTP);
5207 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5208 lua_rawset(gL.T, -3);
5209
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005210 /* Create and fille the __index entry. */
5211 lua_pushstring(gL.T, "__index");
5212 lua_newtable(gL.T);
5213
5214 /* Register Lua functions. */
5215 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
5216 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
5217 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
5218 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
5219 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
5220 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
5221 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
5222 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
5223 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
5224 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
5225
5226 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
5227 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
5228 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
5229 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
5230 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
5231 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005232 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005233
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005234 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005235
5236 /* Register previous table in the registry with reference and named entry. */
5237 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5238 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_HTTP); /* register class session. */
5239 class_http_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
5240
5241 /*
5242 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005243 * Register class TXN
5244 *
5245 */
5246
5247 /* Create and fill the metatable. */
5248 lua_newtable(gL.T);
5249
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005250 /* Create the __tostring identifier */
5251 lua_pushstring(gL.T, "__tostring");
5252 lua_pushstring(gL.T, CLASS_TXN);
5253 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5254 lua_rawset(gL.T, -3);
5255
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005256 /* Create and fille the __index entry. */
5257 lua_pushstring(gL.T, "__index");
5258 lua_newtable(gL.T);
5259
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005260 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01005261 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
5262 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005263 hlua_class_function(gL.T, "set_var", hlua_set_var);
5264 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005265 hlua_class_function(gL.T, "done", hlua_txn_done);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005266 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
5267 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
5268 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005269 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
5270 hlua_class_function(gL.T, "log", hlua_txn_log);
5271 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
5272 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
5273 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
5274 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005275
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005276 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005277
5278 /* Register previous table in the registry with reference and named entry. */
5279 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5280 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_TXN); /* register class session. */
5281 class_txn_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005282
5283 /*
5284 *
5285 * Register class Socket
5286 *
5287 */
5288
5289 /* Create and fill the metatable. */
5290 lua_newtable(gL.T);
5291
Thierry FOURNIERd2a3dcc2015-09-18 07:35:06 +02005292 /* Create the __tostring identifier */
5293 lua_pushstring(gL.T, "__tostring");
5294 lua_pushstring(gL.T, CLASS_SOCKET);
5295 lua_pushcclosure(gL.T, hlua_dump_object, 1);
5296 lua_rawset(gL.T, -3);
5297
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005298 /* Create and fille the __index entry. */
5299 lua_pushstring(gL.T, "__index");
5300 lua_newtable(gL.T);
5301
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005302#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005303 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005304#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005305 hlua_class_function(gL.T, "connect", hlua_socket_connect);
5306 hlua_class_function(gL.T, "send", hlua_socket_send);
5307 hlua_class_function(gL.T, "receive", hlua_socket_receive);
5308 hlua_class_function(gL.T, "close", hlua_socket_close);
5309 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
5310 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
5311 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
5312 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
5313
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005314 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005315
5316 /* Register the garbage collector entry. */
5317 lua_pushstring(gL.T, "__gc");
5318 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005319 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005320
5321 /* Register previous table in the registry with reference and named entry. */
5322 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5323 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5324 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_SOCKET); /* register class socket. */
5325 class_socket_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class socket. */
5326
5327 /* Proxy and server configuration initialisation. */
5328 memset(&socket_proxy, 0, sizeof(socket_proxy));
5329 init_new_proxy(&socket_proxy);
5330 socket_proxy.parent = NULL;
5331 socket_proxy.last_change = now.tv_sec;
5332 socket_proxy.id = "LUA-SOCKET";
5333 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
5334 socket_proxy.maxconn = 0;
5335 socket_proxy.accept = NULL;
5336 socket_proxy.options2 |= PR_O2_INDEPSTR;
5337 socket_proxy.srv = NULL;
5338 socket_proxy.conn_retries = 0;
5339 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
5340
5341 /* Init TCP server: unchanged parameters */
5342 memset(&socket_tcp, 0, sizeof(socket_tcp));
5343 socket_tcp.next = NULL;
5344 socket_tcp.proxy = &socket_proxy;
5345 socket_tcp.obj_type = OBJ_TYPE_SERVER;
5346 LIST_INIT(&socket_tcp.actconns);
5347 LIST_INIT(&socket_tcp.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02005348 LIST_INIT(&socket_tcp.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02005349 LIST_INIT(&socket_tcp.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02005350 LIST_INIT(&socket_tcp.safe_conns);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005351 socket_tcp.state = SRV_ST_RUNNING; /* early server setup */
5352 socket_tcp.last_change = 0;
5353 socket_tcp.id = "LUA-TCP-CONN";
5354 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5355 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5356 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
5357
5358 /* XXX: Copy default parameter from default server,
5359 * but the default server is not initialized.
5360 */
5361 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
5362 socket_tcp.minconn = socket_proxy.defsrv.minconn;
5363 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
5364 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
5365 socket_tcp.onerror = socket_proxy.defsrv.onerror;
5366 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5367 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
5368 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5369 socket_tcp.uweight = socket_proxy.defsrv.iweight;
5370 socket_tcp.iweight = socket_proxy.defsrv.iweight;
5371
5372 socket_tcp.check.status = HCHK_STATUS_INI;
5373 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
5374 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
5375 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
5376 socket_tcp.check.server = &socket_tcp;
5377
5378 socket_tcp.agent.status = HCHK_STATUS_INI;
5379 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
5380 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
5381 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
5382 socket_tcp.agent.server = &socket_tcp;
5383
5384 socket_tcp.xprt = &raw_sock;
5385
5386#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005387 /* Init TCP server: unchanged parameters */
5388 memset(&socket_ssl, 0, sizeof(socket_ssl));
5389 socket_ssl.next = NULL;
5390 socket_ssl.proxy = &socket_proxy;
5391 socket_ssl.obj_type = OBJ_TYPE_SERVER;
5392 LIST_INIT(&socket_ssl.actconns);
5393 LIST_INIT(&socket_ssl.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02005394 LIST_INIT(&socket_ssl.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02005395 LIST_INIT(&socket_ssl.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02005396 LIST_INIT(&socket_ssl.safe_conns);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005397 socket_ssl.state = SRV_ST_RUNNING; /* early server setup */
5398 socket_ssl.last_change = 0;
5399 socket_ssl.id = "LUA-SSL-CONN";
5400 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5401 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5402 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
5403
5404 /* XXX: Copy default parameter from default server,
5405 * but the default server is not initialized.
5406 */
5407 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
5408 socket_ssl.minconn = socket_proxy.defsrv.minconn;
5409 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
5410 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
5411 socket_ssl.onerror = socket_proxy.defsrv.onerror;
5412 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5413 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
5414 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5415 socket_ssl.uweight = socket_proxy.defsrv.iweight;
5416 socket_ssl.iweight = socket_proxy.defsrv.iweight;
5417
5418 socket_ssl.check.status = HCHK_STATUS_INI;
5419 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
5420 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
5421 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
5422 socket_ssl.check.server = &socket_ssl;
5423
5424 socket_ssl.agent.status = HCHK_STATUS_INI;
5425 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
5426 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
5427 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
5428 socket_ssl.agent.server = &socket_ssl;
5429
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005430 socket_ssl.use_ssl = 1;
5431 socket_ssl.xprt = &ssl_sock;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005432
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005433 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005434 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
5435 /*
5436 *
5437 * If the keyword is not known, we can search in the registered
5438 * server keywords. This is usefull to configure special SSL
5439 * features like client certificates and ssl_verify.
5440 *
5441 */
5442 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
5443 if (tmp_error != 0) {
5444 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
5445 abort(); /* This must be never arrives because the command line
5446 not editable by the user. */
5447 }
5448 idx += kw->skip;
5449 }
5450 }
5451
5452 /* Initialize SSL server. */
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005453 ssl_sock_prepare_srv_ctx(&socket_ssl, &socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005454#endif
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01005455}