blob: 320e72900c1b7a5b7e8c601d62eac224e59fba16 [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>
4
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01005#include <lauxlib.h>
6#include <lua.h>
7#include <lualib.h>
8
Thierry FOURNIER463119c2015-03-10 00:35:36 +01009#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
10#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010011#endif
12
Thierry FOURNIER380d0932015-01-23 14:27:52 +010013#include <ebpttree.h>
14
15#include <common/cfgparse.h>
16
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010017#include <types/connection.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010018#include <types/hlua.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010019#include <types/proto_tcp.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010020#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 FOURNIER380d0932015-01-23 14:27:52 +010054/* The main Lua execution context. */
55struct hlua gL;
56
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010057/* This is the memory pool containing all the signal structs. These
58 * struct are used to store each requiered signal between two tasks.
59 */
60struct pool_head *pool2_hlua_com;
61
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010062/* Used for Socket connection. */
63static struct proxy socket_proxy;
64static struct server socket_tcp;
65#ifdef USE_OPENSSL
66static struct server socket_ssl;
67#endif
68
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010069/* List head of the function called at the initialisation time. */
70struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
71
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010072/* The following variables contains the reference of the different
73 * Lua classes. These references are useful for identify metadata
74 * associated with an object.
75 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010076static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010077static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +010078static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +010079static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +010080static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +010081static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +020082static int class_map_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010083
Thierry FOURNIERbd413492015-03-03 16:52:26 +010084/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +020085 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +010086 * short timeout. Lua linked with tasks doesn't have a timeout
87 * because a task may remain alive during all the haproxy execution.
88 */
89static unsigned int hlua_timeout_session = 4000; /* session timeout. */
90static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
91
Thierry FOURNIERee9f8022015-03-03 17:37:37 +010092/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
93 * it is used for preventing infinite loops.
94 *
95 * I test the scheer with an infinite loop containing one incrementation
96 * and one test. I run this loop between 10 seconds, I raise a ceil of
97 * 710M loops from one interrupt each 9000 instructions, so I fix the value
98 * to one interrupt each 10 000 instructions.
99 *
100 * configured | Number of
101 * instructions | loops executed
102 * between two | in milions
103 * forced yields |
104 * ---------------+---------------
105 * 10 | 160
106 * 500 | 670
107 * 1000 | 680
108 * 5000 | 700
109 * 7000 | 700
110 * 8000 | 700
111 * 9000 | 710 <- ceil
112 * 10000 | 710
113 * 100000 | 710
114 * 1000000 | 710
115 *
116 */
117static unsigned int hlua_nb_instruction = 10000;
118
Willy Tarreau32f61e22015-03-18 17:54:59 +0100119/* Descriptor for the memory allocation state. If limit is not null, it will
120 * be enforced on any memory allocation.
121 */
122struct hlua_mem_allocator {
123 size_t allocated;
124 size_t limit;
125};
126
127static struct hlua_mem_allocator hlua_global_allocator;
128
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100129/* These functions converts types between HAProxy internal args or
130 * sample and LUA types. Another function permits to check if the
131 * LUA stack contains arguments according with an required ARG_T
132 * format.
133 */
134static int hlua_arg2lua(lua_State *L, const struct arg *arg);
135static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100136__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
137 unsigned int mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100138static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100139static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100140static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
141
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100142/* Used to check an Lua function type in the stack. It creates and
143 * returns a reference of the function. This function throws an
144 * error if the rgument is not a "function".
145 */
146__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
147{
148 if (!lua_isfunction(L, argno)) {
149 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1));
150 WILL_LJMP(luaL_argerror(L, argno, msg));
151 }
152 lua_pushvalue(L, argno);
153 return luaL_ref(L, LUA_REGISTRYINDEX);
154}
155
156/* The three following functions are useful for adding entries
157 * in a table. These functions takes a string and respectively an
158 * integer, a string or a function and add it to the table in the
159 * top of the stack.
160 *
161 * These functions throws an error if no more stack size is
162 * available.
163 */
164__LJMP static inline void hlua_class_const_int(lua_State *L, const char *name,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100165 int value)
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100166{
167 if (!lua_checkstack(L, 2))
168 WILL_LJMP(luaL_error(L, "full stack"));
169 lua_pushstring(L, name);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100170 lua_pushinteger(L, value);
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100171 lua_settable(L, -3);
172}
173__LJMP static inline void hlua_class_const_str(lua_State *L, const char *name,
174 const char *value)
175{
176 if (!lua_checkstack(L, 2))
177 WILL_LJMP(luaL_error(L, "full stack"));
178 lua_pushstring(L, name);
179 lua_pushstring(L, value);
180 lua_settable(L, -3);
181}
182__LJMP static inline void hlua_class_function(lua_State *L, const char *name,
183 int (*function)(lua_State *L))
184{
185 if (!lua_checkstack(L, 2))
186 WILL_LJMP(luaL_error(L, "full stack"));
187 lua_pushstring(L, name);
188 lua_pushcclosure(L, function, 0);
189 lua_settable(L, -3);
190}
191
192/* This function check the number of arguments available in the
193 * stack. If the number of arguments available is not the same
194 * then <nb> an error is throwed.
195 */
196__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
197{
198 if (lua_gettop(L) == nb)
199 return;
200 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
201}
202
203/* Return true if the data in stack[<ud>] is an object of
204 * type <class_ref>.
205 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +0100206static int hlua_metaistype(lua_State *L, int ud, int class_ref)
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100207{
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100208 if (!lua_getmetatable(L, ud))
209 return 0;
210
211 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
212 if (!lua_rawequal(L, -1, -2)) {
213 lua_pop(L, 2);
214 return 0;
215 }
216
217 lua_pop(L, 2);
218 return 1;
219}
220
221/* Return an object of the expected type, or throws an error. */
222__LJMP static void *hlua_checkudata(lua_State *L, int ud, int class_ref)
223{
Thierry FOURNIER2297bc22015-03-11 17:43:33 +0100224 void *p;
225
226 /* Check if the stack entry is an array. */
227 if (!lua_istable(L, ud))
228 WILL_LJMP(luaL_argerror(L, ud, NULL));
229 /* Check if the metadata have the expected type. */
230 if (!hlua_metaistype(L, ud, class_ref))
231 WILL_LJMP(luaL_argerror(L, ud, NULL));
232 /* Push on the stack at the entry [0] of the table. */
233 lua_rawgeti(L, ud, 0);
234 /* Check if this entry is userdata. */
235 p = lua_touserdata(L, -1);
236 if (!p)
237 WILL_LJMP(luaL_argerror(L, ud, NULL));
238 /* Remove the entry returned by lua_rawgeti(). */
239 lua_pop(L, 1);
240 /* Return the associated struct. */
241 return p;
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100242}
243
244/* This fucntion push an error string prefixed by the file name
245 * and the line number where the error is encountered.
246 */
247static int hlua_pusherror(lua_State *L, const char *fmt, ...)
248{
249 va_list argp;
250 va_start(argp, fmt);
251 luaL_where(L, 1);
252 lua_pushvfstring(L, fmt, argp);
253 va_end(argp);
254 lua_concat(L, 2);
255 return 1;
256}
257
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100258/* This function register a new signal. "lua" is the current lua
259 * execution context. It contains a pointer to the associated task.
260 * "link" is a list head attached to an other task that must be wake
261 * the lua task if an event occurs. This is useful with external
262 * events like TCP I/O or sleep functions. This funcion allocate
263 * memory for the signal.
264 */
265static int hlua_com_new(struct hlua *lua, struct list *link)
266{
267 struct hlua_com *com = pool_alloc2(pool2_hlua_com);
268 if (!com)
269 return 0;
270 LIST_ADDQ(&lua->com, &com->purge_me);
271 LIST_ADDQ(link, &com->wake_me);
272 com->task = lua->task;
273 return 1;
274}
275
276/* This function purge all the pending signals when the LUA execution
277 * is finished. This prevent than a coprocess try to wake a deleted
278 * task. This function remove the memory associated to the signal.
279 */
280static void hlua_com_purge(struct hlua *lua)
281{
282 struct hlua_com *com, *back;
283
284 /* Delete all pending communication signals. */
285 list_for_each_entry_safe(com, back, &lua->com, purge_me) {
286 LIST_DEL(&com->purge_me);
287 LIST_DEL(&com->wake_me);
288 pool_free2(pool2_hlua_com, com);
289 }
290}
291
292/* This function sends signals. It wakes all the tasks attached
293 * to a list head, and remove the signal, and free the used
294 * memory.
295 */
296static void hlua_com_wake(struct list *wake)
297{
298 struct hlua_com *com, *back;
299
300 /* Wake task and delete all pending communication signals. */
301 list_for_each_entry_safe(com, back, wake, wake_me) {
302 LIST_DEL(&com->purge_me);
303 LIST_DEL(&com->wake_me);
304 task_wakeup(com->task, TASK_WOKEN_MSG);
305 pool_free2(pool2_hlua_com, com);
306 }
307}
308
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100309/* This functions is used with sample fetch and converters. It
310 * converts the HAProxy configuration argument in a lua stack
311 * values.
312 *
313 * It takes an array of "arg", and each entry of the array is
314 * converted and pushed in the LUA stack.
315 */
316static int hlua_arg2lua(lua_State *L, const struct arg *arg)
317{
318 switch (arg->type) {
319 case ARGT_SINT:
320 lua_pushinteger(L, arg->data.sint);
321 break;
322
323 case ARGT_UINT:
324 case ARGT_TIME:
325 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100326 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100327 break;
328
329 case ARGT_STR:
330 lua_pushlstring(L, arg->data.str.str, arg->data.str.len);
331 break;
332
333 case ARGT_IPV4:
334 case ARGT_IPV6:
335 case ARGT_MSK4:
336 case ARGT_MSK6:
337 case ARGT_FE:
338 case ARGT_BE:
339 case ARGT_TAB:
340 case ARGT_SRV:
341 case ARGT_USR:
342 case ARGT_MAP:
343 default:
344 lua_pushnil(L);
345 break;
346 }
347 return 1;
348}
349
350/* This function take one entrie in an LUA stack at the index "ud",
351 * and try to convert it in an HAProxy argument entry. This is useful
352 * with sample fetch wrappers. The input arguments are gived to the
353 * lua wrapper and converted as arg list by thi function.
354 */
355static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
356{
357 switch (lua_type(L, ud)) {
358
359 case LUA_TNUMBER:
360 case LUA_TBOOLEAN:
361 arg->type = ARGT_SINT;
362 arg->data.sint = lua_tointeger(L, ud);
363 break;
364
365 case LUA_TSTRING:
366 arg->type = ARGT_STR;
367 arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len);
368 break;
369
370 case LUA_TUSERDATA:
371 case LUA_TNIL:
372 case LUA_TTABLE:
373 case LUA_TFUNCTION:
374 case LUA_TTHREAD:
375 case LUA_TLIGHTUSERDATA:
376 arg->type = ARGT_SINT;
377 arg->data.uint = 0;
378 break;
379 }
380 return 1;
381}
382
383/* the following functions are used to convert a struct sample
384 * in Lua type. This useful to convert the return of the
385 * fetchs or converters.
386 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100387static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100388{
389 switch (smp->type) {
390 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100391 case SMP_T_BOOL:
392 case SMP_T_UINT:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100393 lua_pushinteger(L, smp->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100394 break;
395
396 case SMP_T_BIN:
397 case SMP_T_STR:
398 lua_pushlstring(L, smp->data.str.str, smp->data.str.len);
399 break;
400
401 case SMP_T_METH:
402 switch (smp->data.meth.meth) {
403 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
404 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
405 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
406 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
407 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
408 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
409 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
410 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
411 case HTTP_METH_OTHER:
412 lua_pushlstring(L, smp->data.meth.str.str, smp->data.meth.str.len);
413 break;
414 default:
415 lua_pushnil(L);
416 break;
417 }
418 break;
419
420 case SMP_T_IPV4:
421 case SMP_T_IPV6:
422 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100423 if (sample_casts[smp->type][SMP_T_STR] &&
424 sample_casts[smp->type][SMP_T_STR](smp))
425 lua_pushlstring(L, smp->data.str.str, smp->data.str.len);
426 else
427 lua_pushnil(L);
428 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100429 default:
430 lua_pushnil(L);
431 break;
432 }
433 return 1;
434}
435
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100436/* the following functions are used to convert a struct sample
437 * in Lua strings. This is useful to convert the return of the
438 * fetchs or converters.
439 */
440static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
441{
442 switch (smp->type) {
443
444 case SMP_T_BIN:
445 case SMP_T_STR:
446 lua_pushlstring(L, smp->data.str.str, smp->data.str.len);
447 break;
448
449 case SMP_T_METH:
450 switch (smp->data.meth.meth) {
451 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
452 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
453 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
454 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
455 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
456 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
457 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
458 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
459 case HTTP_METH_OTHER:
460 lua_pushlstring(L, smp->data.meth.str.str, smp->data.meth.str.len);
461 break;
462 default:
463 lua_pushstring(L, "");
464 break;
465 }
466 break;
467
468 case SMP_T_SINT:
469 case SMP_T_BOOL:
470 case SMP_T_UINT:
471 case SMP_T_IPV4:
472 case SMP_T_IPV6:
473 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
474 if (sample_casts[smp->type][SMP_T_STR] &&
475 sample_casts[smp->type][SMP_T_STR](smp))
476 lua_pushlstring(L, smp->data.str.str, smp->data.str.len);
477 else
478 lua_pushstring(L, "");
479 break;
480 default:
481 lua_pushstring(L, "");
482 break;
483 }
484 return 1;
485}
486
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100487/* the following functions are used to convert an Lua type in a
488 * struct sample. This is useful to provide data from a converter
489 * to the LUA code.
490 */
491static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
492{
493 switch (lua_type(L, ud)) {
494
495 case LUA_TNUMBER:
496 smp->type = SMP_T_SINT;
497 smp->data.sint = lua_tointeger(L, ud);
498 break;
499
500
501 case LUA_TBOOLEAN:
502 smp->type = SMP_T_BOOL;
503 smp->data.uint = lua_toboolean(L, ud);
504 break;
505
506 case LUA_TSTRING:
507 smp->type = SMP_T_STR;
508 smp->flags |= SMP_F_CONST;
509 smp->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.str.len);
510 break;
511
512 case LUA_TUSERDATA:
513 case LUA_TNIL:
514 case LUA_TTABLE:
515 case LUA_TFUNCTION:
516 case LUA_TTHREAD:
517 case LUA_TLIGHTUSERDATA:
518 smp->type = SMP_T_BOOL;
519 smp->data.uint = 0;
520 break;
521 }
522 return 1;
523}
524
525/* This function check the "argp" builded by another conversion function
526 * is in accord with the expected argp defined by the "mask". The fucntion
527 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100528 *
529 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
530 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100531 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100532__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
533 unsigned int mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100534{
535 int min_arg;
536 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100537 struct proxy *px;
538 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100539
540 idx = 0;
541 min_arg = ARGM(mask);
542 mask >>= ARGM_BITS;
543
544 while (1) {
545
546 /* Check oversize. */
547 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100548 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100549 }
550
551 /* Check for mandatory arguments. */
552 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100553 if (idx < min_arg) {
554
555 /* If miss other argument than the first one, we return an error. */
556 if (idx > 0)
557 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
558
559 /* If first argument have a certain type, some default values
560 * may be used. See the function smp_resolve_args().
561 */
562 switch (mask & ARGT_MASK) {
563
564 case ARGT_FE:
565 if (!(p->cap & PR_CAP_FE))
566 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
567 argp[idx].data.prx = p;
568 argp[idx].type = ARGT_FE;
569 argp[idx+1].type = ARGT_STOP;
570 break;
571
572 case ARGT_BE:
573 if (!(p->cap & PR_CAP_BE))
574 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
575 argp[idx].data.prx = p;
576 argp[idx].type = ARGT_BE;
577 argp[idx+1].type = ARGT_STOP;
578 break;
579
580 case ARGT_TAB:
581 argp[idx].data.prx = p;
582 argp[idx].type = ARGT_TAB;
583 argp[idx+1].type = ARGT_STOP;
584 break;
585
586 default:
587 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
588 break;
589 }
590 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100591 return 0;
592 }
593
594 /* Check for exceed the number of requiered argument. */
595 if ((mask & ARGT_MASK) == ARGT_STOP &&
596 argp[idx].type != ARGT_STOP) {
597 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
598 }
599
600 if ((mask & ARGT_MASK) == ARGT_STOP &&
601 argp[idx].type == ARGT_STOP) {
602 return 0;
603 }
604
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100605 /* Convert some argument types. */
606 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100607 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100608 if (argp[idx].type != ARGT_SINT)
609 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
610 argp[idx].type = ARGT_SINT;
611 break;
612
613 case ARGT_UINT:
614 if (argp[idx].type != ARGT_SINT)
615 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
616 argp[idx].type = ARGT_SINT;
617 break;
618
619 case ARGT_TIME:
620 if (argp[idx].type != ARGT_SINT)
621 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
622 argp[idx].type = ARGT_SINT;
623 break;
624
625 case ARGT_SIZE:
626 if (argp[idx].type != ARGT_SINT)
627 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
628 argp[idx].type = ARGT_SINT;
629 break;
630
631 case ARGT_FE:
632 if (argp[idx].type != ARGT_STR)
633 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
634 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
635 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200636 argp[idx].data.prx = proxy_fe_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100637 if (!argp[idx].data.prx)
638 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
639 argp[idx].type = ARGT_FE;
640 break;
641
642 case ARGT_BE:
643 if (argp[idx].type != ARGT_STR)
644 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
645 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
646 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200647 argp[idx].data.prx = proxy_be_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100648 if (!argp[idx].data.prx)
649 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
650 argp[idx].type = ARGT_BE;
651 break;
652
653 case ARGT_TAB:
654 if (argp[idx].type != ARGT_STR)
655 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
656 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
657 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200658 argp[idx].data.prx = proxy_tbl_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100659 if (!argp[idx].data.prx)
660 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
661 argp[idx].type = ARGT_TAB;
662 break;
663
664 case ARGT_SRV:
665 if (argp[idx].type != ARGT_STR)
666 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
667 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
668 trash.str[argp[idx].data.str.len] = 0;
669 sname = strrchr(trash.str, '/');
670 if (sname) {
671 *sname++ = '\0';
672 pname = trash.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200673 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100674 if (!px)
675 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
676 }
677 else {
678 sname = trash.str;
679 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100680 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100681 argp[idx].data.srv = findserver(px, sname);
682 if (!argp[idx].data.srv)
683 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
684 argp[idx].type = ARGT_SRV;
685 break;
686
687 case ARGT_IPV4:
688 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
689 trash.str[argp[idx].data.str.len] = 0;
690 if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4))
691 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
692 argp[idx].type = ARGT_IPV4;
693 break;
694
695 case ARGT_MSK4:
696 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
697 trash.str[argp[idx].data.str.len] = 0;
698 if (!str2mask(trash.str, &argp[idx].data.ipv4))
699 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
700 argp[idx].type = ARGT_MSK4;
701 break;
702
703 case ARGT_IPV6:
704 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
705 trash.str[argp[idx].data.str.len] = 0;
706 if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6))
707 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
708 argp[idx].type = ARGT_IPV6;
709 break;
710
711 case ARGT_MSK6:
712 case ARGT_MAP:
713 case ARGT_REG:
714 case ARGT_USR:
715 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100716 break;
717 }
718
719 /* Check for type of argument. */
720 if ((mask & ARGT_MASK) != argp[idx].type) {
721 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
722 arg_type_names[(mask & ARGT_MASK)],
723 arg_type_names[argp[idx].type & ARGT_MASK]);
724 WILL_LJMP(luaL_argerror(L, first + idx, msg));
725 }
726
727 /* Next argument. */
728 mask >>= ARGT_BITS;
729 idx++;
730 }
731}
732
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100733/*
734 * The following functions are used to make correspondance between the the
735 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100736 *
737 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100738 * - hlua_sethlua : create the association between hlua context and lua_state.
739 */
740static inline struct hlua *hlua_gethlua(lua_State *L)
741{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100742 struct hlua **hlua = lua_getextraspace(L);
743 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100744}
745static inline void hlua_sethlua(struct hlua *hlua)
746{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100747 struct hlua **hlua_store = lua_getextraspace(hlua->T);
748 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100749}
750
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100751/* This function is used to send logs. It try to send on screen (stderr)
752 * and on the default syslog server.
753 */
754static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
755{
756 struct tm tm;
757 char *p;
758
759 /* Cleanup the log message. */
760 p = trash.str;
761 for (; *msg != '\0'; msg++, p++) {
762 if (p >= trash.str + trash.size - 1)
763 return;
764 if (isprint(*msg))
765 *p = *msg;
766 else
767 *p = '.';
768 }
769 *p = '\0';
770
771 send_log(px, level, "%s", trash.str);
772 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
773 get_localtime(date.tv_sec, &tm);
774 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
775 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
776 (int)getpid(), trash.str);
777 fflush(stderr);
778 }
779}
780
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100781/* This function just ensure that the yield will be always
782 * returned with a timeout and permit to set some flags
783 */
784__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100785 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100786{
787 struct hlua *hlua = hlua_gethlua(L);
788
789 /* Set the wake timeout. If timeout is required, we set
790 * the expiration time.
791 */
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100792 hlua->wake_time = tick_first(timeout, hlua->expire);
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100793
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100794 hlua->flags |= flags;
795
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100796 /* Process the yield. */
797 WILL_LJMP(lua_yieldk(L, nresults, ctx, k));
798}
799
Willy Tarreau87b09662015-04-03 00:22:06 +0200800/* This function initialises the Lua environment stored in the stream.
801 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100802 * an LUA coroutine. It can not be use to crete the main LUA context.
803 */
804int hlua_ctx_init(struct hlua *lua, struct task *task)
805{
806 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100807 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100808 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100809 lua->T = lua_newthread(gL.T);
810 if (!lua->T) {
811 lua->Tref = LUA_REFNIL;
812 return 0;
813 }
814 hlua_sethlua(lua);
815 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
816 lua->task = task;
817 return 1;
818}
819
Willy Tarreau87b09662015-04-03 00:22:06 +0200820/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100821 * is destroyed. The destroy also the memory context. The struct "lua"
822 * is not freed.
823 */
824void hlua_ctx_destroy(struct hlua *lua)
825{
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100826 if (!lua->T)
827 return;
828
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100829 /* Purge all the pending signals. */
830 hlua_com_purge(lua);
831
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100832 /* The thread is garbage collected by Lua. */
833 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
834 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
835}
836
837/* This function is used to restore the Lua context when a coroutine
838 * fails. This function copy the common memory between old coroutine
839 * and the new coroutine. The old coroutine is destroyed, and its
840 * replaced by the new coroutine.
841 * If the flag "keep_msg" is set, the last entry of the old is assumed
842 * as string error message and it is copied in the new stack.
843 */
844static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
845{
846 lua_State *T;
847 int new_ref;
848
849 /* Renew the main LUA stack doesn't have sense. */
850 if (lua == &gL)
851 return 0;
852
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100853 /* New Lua coroutine. */
854 T = lua_newthread(gL.T);
855 if (!T)
856 return 0;
857
858 /* Copy last error message. */
859 if (keep_msg)
860 lua_xmove(lua->T, T, 1);
861
862 /* Copy data between the coroutines. */
863 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
864 lua_xmove(lua->T, T, 1);
865 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
866
867 /* Destroy old data. */
868 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
869
870 /* The thread is garbage collected by Lua. */
871 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
872
873 /* Fill the struct with the new coroutine values. */
874 lua->Mref = new_ref;
875 lua->T = T;
876 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
877
878 /* Set context. */
879 hlua_sethlua(lua);
880
881 return 1;
882}
883
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100884void hlua_hook(lua_State *L, lua_Debug *ar)
885{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100886 struct hlua *hlua = hlua_gethlua(L);
887
888 /* Lua cannot yield when its returning from a function,
889 * so, we can fix the interrupt hook to 1 instruction,
890 * expecting that the function is finnished.
891 */
892 if (lua_gethookmask(L) & LUA_MASKRET) {
893 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
894 return;
895 }
896
897 /* restore the interrupt condition. */
898 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
899
900 /* If we interrupt the Lua processing in yieldable state, we yield.
901 * If the state is not yieldable, trying yield causes an error.
902 */
903 if (lua_isyieldable(L))
904 WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
905
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +0100906 /* If we cannot yield, update the clock and check the timeout. */
907 tv_update_date(0, 1);
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100908 if (tick_is_expired(hlua->expire, now_ms)) {
909 lua_pushfstring(L, "execution timeout");
910 WILL_LJMP(lua_error(L));
911 }
912
913 /* Try to interrupt the process at the end of the current
914 * unyieldable function.
915 */
916 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100917}
918
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100919/* This function start or resumes the Lua stack execution. If the flag
920 * "yield_allowed" if no set and the LUA stack execution returns a yield
921 * The function return an error.
922 *
923 * The function can returns 4 values:
924 * - HLUA_E_OK : The execution is terminated without any errors.
925 * - HLUA_E_AGAIN : The execution must continue at the next associated
926 * task wakeup.
927 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
928 * the top of the stack.
929 * - HLUA_E_ERR : An error has occured without error message.
930 *
931 * If an error occured, the stack is renewed and it is ready to run new
932 * LUA code.
933 */
934static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
935{
936 int ret;
937 const char *msg;
938
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100939 HLUA_SET_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100940
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100941 /* If we want to resume the task, then check first the execution timeout.
942 * if it is reached, we can interrupt the Lua processing.
943 */
944 if (tick_is_expired(lua->expire, now_ms))
945 goto timeout_reached;
946
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100947resume_execution:
948
949 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
950 * instructions. it is used for preventing infinite loops.
951 */
952 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
953
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +0100954 /* Remove all flags except the running flags. */
955 lua->flags = HLUA_RUN;
956
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100957 /* Call the function. */
958 ret = lua_resume(lua->T, gL.T, lua->nargs);
959 switch (ret) {
960
961 case LUA_OK:
962 ret = HLUA_E_OK;
963 break;
964
965 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100966 /* Check if the execution timeout is expired. It it is the case, we
967 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100968 */
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100969 if (tick_is_expired(lua->expire, now_ms)) {
970
971timeout_reached:
972
973 lua_settop(lua->T, 0); /* Empty the stack. */
974 if (!lua_checkstack(lua->T, 1)) {
975 ret = HLUA_E_ERR;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100976 break;
977 }
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100978 lua_pushfstring(lua->T, "execution timeout");
979 ret = HLUA_E_ERRMSG;
980 break;
981 }
982 /* Process the forced yield. if the general yield is not allowed or
983 * if no task were associated this the current Lua execution
984 * coroutine, we resume the execution. Else we want to return in the
985 * scheduler and we want to be waked up again, to continue the
986 * current Lua execution. So we schedule our own task.
987 */
988 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100989 if (!yield_allowed || !lua->task)
990 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100991 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100992 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100993 if (!yield_allowed) {
994 lua_settop(lua->T, 0); /* Empty the stack. */
995 if (!lua_checkstack(lua->T, 1)) {
996 ret = HLUA_E_ERR;
997 break;
998 }
999 lua_pushfstring(lua->T, "yield not allowed");
1000 ret = HLUA_E_ERRMSG;
1001 break;
1002 }
1003 ret = HLUA_E_AGAIN;
1004 break;
1005
1006 case LUA_ERRRUN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001007 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001008 if (!lua_checkstack(lua->T, 1)) {
1009 ret = HLUA_E_ERR;
1010 break;
1011 }
1012 msg = lua_tostring(lua->T, -1);
1013 lua_settop(lua->T, 0); /* Empty the stack. */
1014 lua_pop(lua->T, 1);
1015 if (msg)
1016 lua_pushfstring(lua->T, "runtime error: %s", msg);
1017 else
1018 lua_pushfstring(lua->T, "unknown runtime error");
1019 ret = HLUA_E_ERRMSG;
1020 break;
1021
1022 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001023 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001024 lua_settop(lua->T, 0); /* Empty the stack. */
1025 if (!lua_checkstack(lua->T, 1)) {
1026 ret = HLUA_E_ERR;
1027 break;
1028 }
1029 lua_pushfstring(lua->T, "out of memory error");
1030 ret = HLUA_E_ERRMSG;
1031 break;
1032
1033 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001034 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001035 if (!lua_checkstack(lua->T, 1)) {
1036 ret = HLUA_E_ERR;
1037 break;
1038 }
1039 msg = lua_tostring(lua->T, -1);
1040 lua_settop(lua->T, 0); /* Empty the stack. */
1041 lua_pop(lua->T, 1);
1042 if (msg)
1043 lua_pushfstring(lua->T, "message handler error: %s", msg);
1044 else
1045 lua_pushfstring(lua->T, "message handler error");
1046 ret = HLUA_E_ERRMSG;
1047 break;
1048
1049 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001050 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001051 lua_settop(lua->T, 0); /* Empty the stack. */
1052 if (!lua_checkstack(lua->T, 1)) {
1053 ret = HLUA_E_ERR;
1054 break;
1055 }
1056 lua_pushfstring(lua->T, "unknonwn error");
1057 ret = HLUA_E_ERRMSG;
1058 break;
1059 }
1060
1061 switch (ret) {
1062 case HLUA_E_AGAIN:
1063 break;
1064
1065 case HLUA_E_ERRMSG:
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001066 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001067 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001068 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001069 break;
1070
1071 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001072 HLUA_CLR_RUN(lua);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001073 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001074 hlua_ctx_renew(lua, 0);
1075 break;
1076
1077 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001078 HLUA_CLR_RUN(lua);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001079 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001080 break;
1081 }
1082
1083 return ret;
1084}
1085
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001086/* This function is an LUA binding. It provides a function
1087 * for deleting ACL from a referenced ACL file.
1088 */
1089__LJMP static int hlua_del_acl(lua_State *L)
1090{
1091 const char *name;
1092 const char *key;
1093 struct pat_ref *ref;
1094
1095 MAY_LJMP(check_args(L, 2, "del_acl"));
1096
1097 name = MAY_LJMP(luaL_checkstring(L, 1));
1098 key = MAY_LJMP(luaL_checkstring(L, 2));
1099
1100 ref = pat_ref_lookup(name);
1101 if (!ref)
1102 WILL_LJMP(luaL_error(L, "'del_acl': unkown acl file '%s'", name));
1103
1104 pat_ref_delete(ref, key);
1105 return 0;
1106}
1107
1108/* This function is an LUA binding. It provides a function
1109 * for deleting map entry from a referenced map file.
1110 */
1111static int hlua_del_map(lua_State *L)
1112{
1113 const char *name;
1114 const char *key;
1115 struct pat_ref *ref;
1116
1117 MAY_LJMP(check_args(L, 2, "del_map"));
1118
1119 name = MAY_LJMP(luaL_checkstring(L, 1));
1120 key = MAY_LJMP(luaL_checkstring(L, 2));
1121
1122 ref = pat_ref_lookup(name);
1123 if (!ref)
1124 WILL_LJMP(luaL_error(L, "'del_map': unkown acl file '%s'", name));
1125
1126 pat_ref_delete(ref, key);
1127 return 0;
1128}
1129
1130/* This function is an LUA binding. It provides a function
1131 * for adding ACL pattern from a referenced ACL file.
1132 */
1133static int hlua_add_acl(lua_State *L)
1134{
1135 const char *name;
1136 const char *key;
1137 struct pat_ref *ref;
1138
1139 MAY_LJMP(check_args(L, 2, "add_acl"));
1140
1141 name = MAY_LJMP(luaL_checkstring(L, 1));
1142 key = MAY_LJMP(luaL_checkstring(L, 2));
1143
1144 ref = pat_ref_lookup(name);
1145 if (!ref)
1146 WILL_LJMP(luaL_error(L, "'add_acl': unkown acl file '%s'", name));
1147
1148 if (pat_ref_find_elt(ref, key) == NULL)
1149 pat_ref_add(ref, key, NULL, NULL);
1150 return 0;
1151}
1152
1153/* This function is an LUA binding. It provides a function
1154 * for setting map pattern and sample from a referenced map
1155 * file.
1156 */
1157static int hlua_set_map(lua_State *L)
1158{
1159 const char *name;
1160 const char *key;
1161 const char *value;
1162 struct pat_ref *ref;
1163
1164 MAY_LJMP(check_args(L, 3, "set_map"));
1165
1166 name = MAY_LJMP(luaL_checkstring(L, 1));
1167 key = MAY_LJMP(luaL_checkstring(L, 2));
1168 value = MAY_LJMP(luaL_checkstring(L, 3));
1169
1170 ref = pat_ref_lookup(name);
1171 if (!ref)
1172 WILL_LJMP(luaL_error(L, "'set_map': unkown map file '%s'", name));
1173
1174 if (pat_ref_find_elt(ref, key) != NULL)
1175 pat_ref_set(ref, key, value, NULL);
1176 else
1177 pat_ref_add(ref, key, value, NULL);
1178 return 0;
1179}
1180
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001181/* A class is a lot of memory that contain data. This data can be a table,
1182 * an integer or user data. This data is associated with a metatable. This
1183 * metatable have an original version registred in the global context with
1184 * the name of the object (_G[<name>] = <metable> ).
1185 *
1186 * A metable is a table that modify the standard behavior of a standard
1187 * access to the associated data. The entries of this new metatable are
1188 * defined as is:
1189 *
1190 * http://lua-users.org/wiki/MetatableEvents
1191 *
1192 * __index
1193 *
1194 * we access an absent field in a table, the result is nil. This is
1195 * true, but it is not the whole truth. Actually, such access triggers
1196 * the interpreter to look for an __index metamethod: If there is no
1197 * such method, as usually happens, then the access results in nil;
1198 * otherwise, the metamethod will provide the result.
1199 *
1200 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1201 * the key does not appear in the table, but the metatable has an __index
1202 * property:
1203 *
1204 * - if the value is a function, the function is called, passing in the
1205 * table and the key; the return value of that function is returned as
1206 * the result.
1207 *
1208 * - if the value is another table, the value of the key in that table is
1209 * asked for and returned (and if it doesn't exist in that table, but that
1210 * table's metatable has an __index property, then it continues on up)
1211 *
1212 * - Use "rawget(myTable,key)" to skip this metamethod.
1213 *
1214 * http://www.lua.org/pil/13.4.1.html
1215 *
1216 * __newindex
1217 *
1218 * Like __index, but control property assignment.
1219 *
1220 * __mode - Control weak references. A string value with one or both
1221 * of the characters 'k' and 'v' which specifies that the the
1222 * keys and/or values in the table are weak references.
1223 *
1224 * __call - Treat a table like a function. When a table is followed by
1225 * parenthesis such as "myTable( 'foo' )" and the metatable has
1226 * a __call key pointing to a function, that function is invoked
1227 * (passing any specified arguments) and the return value is
1228 * returned.
1229 *
1230 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1231 * called, if the metatable for myTable has a __metatable
1232 * key, the value of that key is returned instead of the
1233 * actual metatable.
1234 *
1235 * __tostring - Control string representation. When the builtin
1236 * "tostring( myTable )" function is called, if the metatable
1237 * for myTable has a __tostring property set to a function,
1238 * that function is invoked (passing myTable to it) and the
1239 * return value is used as the string representation.
1240 *
1241 * __len - Control table length. When the table length is requested using
1242 * the length operator ( '#' ), if the metatable for myTable has
1243 * a __len key pointing to a function, that function is invoked
1244 * (passing myTable to it) and the return value used as the value
1245 * of "#myTable".
1246 *
1247 * __gc - Userdata finalizer code. When userdata is set to be garbage
1248 * collected, if the metatable has a __gc field pointing to a
1249 * function, that function is first invoked, passing the userdata
1250 * to it. The __gc metamethod is not called for tables.
1251 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1252 *
1253 * Special metamethods for redefining standard operators:
1254 * http://www.lua.org/pil/13.1.html
1255 *
1256 * __add "+"
1257 * __sub "-"
1258 * __mul "*"
1259 * __div "/"
1260 * __unm "!"
1261 * __pow "^"
1262 * __concat ".."
1263 *
1264 * Special methods for redfining standar relations
1265 * http://www.lua.org/pil/13.2.html
1266 *
1267 * __eq "=="
1268 * __lt "<"
1269 * __le "<="
1270 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001271
1272/*
1273 *
1274 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001275 * Class Map
1276 *
1277 *
1278 */
1279
1280/* Returns a struct hlua_map if the stack entry "ud" is
1281 * a class session, otherwise it throws an error.
1282 */
1283__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1284{
1285 return (struct map_descriptor *)MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
1286}
1287
1288/* This function is the map constructor. It don't need
1289 * the class Map object. It creates and return a new Map
1290 * object. It must be called only during "body" or "init"
1291 * context because it process some filesystem accesses.
1292 */
1293__LJMP static int hlua_map_new(struct lua_State *L)
1294{
1295 const char *fn;
1296 int match = PAT_MATCH_STR;
1297 struct sample_conv conv;
1298 const char *file = "";
1299 int line = 0;
1300 lua_Debug ar;
1301 char *err = NULL;
1302 struct arg args[2];
1303
1304 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1305 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1306
1307 fn = MAY_LJMP(luaL_checkstring(L, 1));
1308
1309 if (lua_gettop(L) >= 2) {
1310 match = MAY_LJMP(luaL_checkinteger(L, 2));
1311 if (match < 0 || match >= PAT_MATCH_NUM)
1312 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1313 }
1314
1315 /* Get Lua filename and line number. */
1316 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1317 lua_getinfo(L, "Sl", &ar); /* get info about it */
1318 if (ar.currentline > 0) { /* is there info? */
1319 file = ar.short_src;
1320 line = ar.currentline;
1321 }
1322 }
1323
1324 /* fill fake sample_conv struct. */
1325 conv.kw = ""; /* unused. */
1326 conv.process = NULL; /* unused. */
1327 conv.arg_mask = 0; /* unused. */
1328 conv.val_args = NULL; /* unused. */
1329 conv.out_type = SMP_T_STR;
1330 conv.private = (void *)(long)match;
1331 switch (match) {
1332 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1333 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1334 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1335 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1336 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1337 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1338 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
1339 case PAT_MATCH_INT: conv.in_type = SMP_T_UINT; break;
1340 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1341 default:
1342 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1343 }
1344
1345 /* fill fake args. */
1346 args[0].type = ARGT_STR;
1347 args[0].data.str.str = (char *)fn;
1348 args[1].type = ARGT_STOP;
1349
1350 /* load the map. */
1351 if (!sample_load_map(args, &conv, file, line, &err)) {
1352 /* error case: we cant use luaL_error because we must
1353 * free the err variable.
1354 */
1355 luaL_where(L, 1);
1356 lua_pushfstring(L, "'new': %s.", err);
1357 lua_concat(L, 2);
1358 free(err);
1359 WILL_LJMP(lua_error(L));
1360 }
1361
1362 /* create the lua object. */
1363 lua_newtable(L);
1364 lua_pushlightuserdata(L, args[0].data.map);
1365 lua_rawseti(L, -2, 0);
1366
1367 /* Pop a class Map metatable and affect it to the userdata. */
1368 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1369 lua_setmetatable(L, -2);
1370
1371
1372 return 1;
1373}
1374
1375__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1376{
1377 struct map_descriptor *desc;
1378 struct pattern *pat;
1379 struct sample smp;
1380
1381 MAY_LJMP(check_args(L, 2, "lookup"));
1382 desc = MAY_LJMP(hlua_checkmap(L, 1));
1383 if (desc->pat.expect_type == SMP_T_UINT) {
1384 smp.type = SMP_T_UINT;
1385 smp.data.uint = MAY_LJMP(luaL_checkinteger(L, 2));
1386 }
1387 else {
1388 smp.type = SMP_T_STR;
1389 smp.flags = SMP_F_CONST;
1390 smp.data.str.str = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.str.len));
1391 }
1392
1393 pat = pattern_exec_match(&desc->pat, &smp, 1);
1394 if (!pat || !pat->smp) {
1395 if (str)
1396 lua_pushstring(L, "");
1397 else
1398 lua_pushnil(L);
1399 return 1;
1400 }
1401
1402 /* The Lua pattern must return a string, so we can't check the returned type */
1403 lua_pushlstring(L, pat->smp->data.str.str, pat->smp->data.str.len);
1404 return 1;
1405}
1406
1407__LJMP static int hlua_map_lookup(struct lua_State *L)
1408{
1409 return _hlua_map_lookup(L, 0);
1410}
1411
1412__LJMP static int hlua_map_slookup(struct lua_State *L)
1413{
1414 return _hlua_map_lookup(L, 1);
1415}
1416
1417/*
1418 *
1419 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001420 * Class Socket
1421 *
1422 *
1423 */
1424
1425__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1426{
1427 return (struct hlua_socket *)MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
1428}
1429
1430/* This function is the handler called for each I/O on the established
1431 * connection. It is used for notify space avalaible to send or data
1432 * received.
1433 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001434static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001435{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001436 struct stream_interface *si = appctx->owner;
Willy Tarreau50fe03b2014-11-28 13:59:31 +01001437 struct connection *c = objt_conn(si_opposite(si)->end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001438
Willy Tarreau87b09662015-04-03 00:22:06 +02001439 /* Wakeup the main stream if the client connection is closed. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001440 if (!c || channel_output_closed(si_ic(si)) || channel_input_closed(si_oc(si))) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001441 if (appctx->ctx.hlua.socket) {
1442 appctx->ctx.hlua.socket->s = NULL;
1443 appctx->ctx.hlua.socket = NULL;
1444 }
1445 si_shutw(si);
1446 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001447 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001448 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1449 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001450 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001451 }
1452
1453 if (!(c->flags & CO_FL_CONNECTED))
Willy Tarreaud4da1962015-04-20 01:31:23 +02001454 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001455
1456 /* This function is called after the connect. */
1457 appctx->ctx.hlua.connected = 1;
1458
1459 /* Wake the tasks which wants to write if the buffer have avalaible space. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001460 if (channel_may_recv(si_oc(si)))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001461 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
1462
1463 /* Wake the tasks which wants to read if the buffer contains data. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001464 if (channel_is_empty(si_ic(si)))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001465 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1466}
1467
Willy Tarreau87b09662015-04-03 00:22:06 +02001468/* This function is called when the "struct stream" is destroyed.
1469 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001470 * Wake all the pending signals.
1471 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001472static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001473{
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001474 /* Remove my link in the original object. */
1475 if (appctx->ctx.hlua.socket)
1476 appctx->ctx.hlua.socket->s = NULL;
1477
1478 /* Wake all the task waiting for me. */
1479 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1480 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
1481}
1482
1483/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001484 * uses this object. If the stream does not exists, just quit.
1485 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001486 * pending signal can rest in the read and write lists. destroy
1487 * it.
1488 */
1489__LJMP static int hlua_socket_gc(lua_State *L)
1490{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001491 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001492 struct appctx *appctx;
1493
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001494 MAY_LJMP(check_args(L, 1, "__gc"));
1495
1496 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001497 if (!socket->s)
1498 return 0;
1499
Willy Tarreau87b09662015-04-03 00:22:06 +02001500 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001501 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001502 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001503 socket->s = NULL;
1504 appctx->ctx.hlua.socket = NULL;
1505
1506 return 0;
1507}
1508
1509/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001510 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001511 */
1512__LJMP static int hlua_socket_close(lua_State *L)
1513{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001514 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001515 struct appctx *appctx;
1516
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001517 MAY_LJMP(check_args(L, 1, "close"));
1518
1519 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001520 if (!socket->s)
1521 return 0;
1522
Willy Tarreau87b09662015-04-03 00:22:06 +02001523 /* Close the stream and remove the associated stop task. */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001524 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001525 appctx = objt_appctx(socket->s->si[0].end);
1526 appctx->ctx.hlua.socket = NULL;
1527 socket->s = NULL;
1528
1529 return 0;
1530}
1531
1532/* This Lua function assumes that the stack contain three parameters.
1533 * 1 - USERDATA containing a struct socket
1534 * 2 - INTEGER with values of the macro defined below
1535 * If the integer is -1, we must read at most one line.
1536 * If the integer is -2, we ust read all the data until the
1537 * end of the stream.
1538 * If the integer is positive value, we must read a number of
1539 * bytes corresponding to this value.
1540 */
1541#define HLSR_READ_LINE (-1)
1542#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001543__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001544{
1545 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1546 int wanted = lua_tointeger(L, 2);
1547 struct hlua *hlua = hlua_gethlua(L);
1548 struct appctx *appctx;
1549 int len;
1550 int nblk;
1551 char *blk1;
1552 int len1;
1553 char *blk2;
1554 int len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001555 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001556 struct channel *oc;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001557
1558 /* Check if this lua stack is schedulable. */
1559 if (!hlua || !hlua->task)
1560 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1561 "'frontend', 'backend' or 'task'"));
1562
1563 /* check for connection closed. If some data where read, return it. */
1564 if (!socket->s)
1565 goto connection_closed;
1566
Willy Tarreau94aa6172015-03-13 14:19:06 +01001567 oc = &socket->s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001568 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001569 /* Read line. */
Willy Tarreau81389672015-03-10 12:03:52 +01001570 nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001571 if (nblk < 0) /* Connection close. */
1572 goto connection_closed;
1573 if (nblk == 0) /* No data avalaible. */
1574 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001575
1576 /* remove final \r\n. */
1577 if (nblk == 1) {
1578 if (blk1[len1-1] == '\n') {
1579 len1--;
1580 skip_at_end++;
1581 if (blk1[len1-1] == '\r') {
1582 len1--;
1583 skip_at_end++;
1584 }
1585 }
1586 }
1587 else {
1588 if (blk2[len2-1] == '\n') {
1589 len2--;
1590 skip_at_end++;
1591 if (blk2[len2-1] == '\r') {
1592 len2--;
1593 skip_at_end++;
1594 }
1595 }
1596 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001597 }
1598
1599 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001600 /* Read all the available data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001601 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001602 if (nblk < 0) /* Connection close. */
1603 goto connection_closed;
1604 if (nblk == 0) /* No data avalaible. */
1605 goto connection_empty;
1606 }
1607
1608 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001609 /* Read a block of data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001610 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001611 if (nblk < 0) /* Connection close. */
1612 goto connection_closed;
1613 if (nblk == 0) /* No data avalaible. */
1614 goto connection_empty;
1615
1616 if (len1 > wanted) {
1617 nblk = 1;
1618 len1 = wanted;
1619 } if (nblk == 2 && len1 + len2 > wanted)
1620 len2 = wanted - len1;
1621 }
1622
1623 len = len1;
1624
1625 luaL_addlstring(&socket->b, blk1, len1);
1626 if (nblk == 2) {
1627 len += len2;
1628 luaL_addlstring(&socket->b, blk2, len2);
1629 }
1630
1631 /* Consume data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001632 bo_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001633
1634 /* Don't wait anything. */
Willy Tarreau828824a2015-04-19 17:20:03 +02001635 si_applet_done(&socket->s->si[0]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001636
1637 /* If the pattern reclaim to read all the data
1638 * in the connection, got out.
1639 */
1640 if (wanted == HLSR_READ_ALL)
1641 goto connection_empty;
1642 else if (wanted >= 0 && len < wanted)
1643 goto connection_empty;
1644
1645 /* Return result. */
1646 luaL_pushresult(&socket->b);
1647 return 1;
1648
1649connection_closed:
1650
1651 /* If the buffer containds data. */
1652 if (socket->b.n > 0) {
1653 luaL_pushresult(&socket->b);
1654 return 1;
1655 }
1656 lua_pushnil(L);
1657 lua_pushstring(L, "connection closed.");
1658 return 2;
1659
1660connection_empty:
1661
1662 appctx = objt_appctx(socket->s->si[0].end);
1663 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_read))
1664 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001665 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001666 return 0;
1667}
1668
1669/* This Lus function gets two parameters. The first one can be string
1670 * or a number. If the string is "*l", the user require one line. If
1671 * the string is "*a", the user require all the content of the stream.
1672 * If the value is a number, the user require a number of bytes equal
1673 * to the value. The default value is "*l" (a line).
1674 *
1675 * This paraeter with a variable type is converted in integer. This
1676 * integer takes this values:
1677 * -1 : read a line
1678 * -2 : read all the stream
1679 * >0 : amount if bytes.
1680 *
1681 * The second parameter is optinal. It contains a string that must be
1682 * concatenated with the read data.
1683 */
1684__LJMP static int hlua_socket_receive(struct lua_State *L)
1685{
1686 int wanted = HLSR_READ_LINE;
1687 const char *pattern;
1688 int type;
1689 char *error;
1690 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001691 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001692
1693 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1694 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1695
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001696 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001697
1698 /* check for pattern. */
1699 if (lua_gettop(L) >= 2) {
1700 type = lua_type(L, 2);
1701 if (type == LUA_TSTRING) {
1702 pattern = lua_tostring(L, 2);
1703 if (strcmp(pattern, "*a") == 0)
1704 wanted = HLSR_READ_ALL;
1705 else if (strcmp(pattern, "*l") == 0)
1706 wanted = HLSR_READ_LINE;
1707 else {
1708 wanted = strtoll(pattern, &error, 10);
1709 if (*error != '\0')
1710 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1711 }
1712 }
1713 else if (type == LUA_TNUMBER) {
1714 wanted = lua_tointeger(L, 2);
1715 if (wanted < 0)
1716 WILL_LJMP(luaL_error(L, "Unsupported size."));
1717 }
1718 }
1719
1720 /* Set pattern. */
1721 lua_pushinteger(L, wanted);
1722 lua_replace(L, 2);
1723
1724 /* init bufffer, and fiil it wih prefix. */
1725 luaL_buffinit(L, &socket->b);
1726
1727 /* Check prefix. */
1728 if (lua_gettop(L) >= 3) {
1729 if (lua_type(L, 3) != LUA_TSTRING)
1730 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1731 pattern = lua_tolstring(L, 3, &len);
1732 luaL_addlstring(&socket->b, pattern, len);
1733 }
1734
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001735 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001736}
1737
1738/* Write the Lua input string in the output buffer.
1739 * This fucntion returns a yield if no space are available.
1740 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001741static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001742{
1743 struct hlua_socket *socket;
1744 struct hlua *hlua = hlua_gethlua(L);
1745 struct appctx *appctx;
1746 size_t buf_len;
1747 const char *buf;
1748 int len;
1749 int send_len;
1750 int sent;
1751
1752 /* Check if this lua stack is schedulable. */
1753 if (!hlua || !hlua->task)
1754 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1755 "'frontend', 'backend' or 'task'"));
1756
1757 /* Get object */
1758 socket = MAY_LJMP(hlua_checksocket(L, 1));
1759 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001760 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001761
1762 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001763 if (!socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001764 lua_pushinteger(L, -1);
1765 return 1;
1766 }
1767
1768 /* Update the input buffer data. */
1769 buf += sent;
1770 send_len = buf_len - sent;
1771
1772 /* All the data are sent. */
1773 if (sent >= buf_len)
1774 return 1; /* Implicitly return the length sent. */
1775
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001776 /* Check if the buffer is avalaible because HAProxy doesn't allocate
1777 * the request buffer if its not required.
1778 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001779 if (socket->s->req.buf->size == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001780 if (!stream_alloc_recv_buffer(&socket->s->req)) {
Willy Tarreau350f4872014-11-28 14:42:25 +01001781 socket->s->si[0].flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001782 goto hlua_socket_write_yield_return;
1783 }
1784 }
1785
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001786 /* Check for avalaible space. */
Willy Tarreau94aa6172015-03-13 14:19:06 +01001787 len = buffer_total_space(socket->s->req.buf);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001788 if (len <= 0)
1789 goto hlua_socket_write_yield_return;
1790
1791 /* send data */
1792 if (len < send_len)
1793 send_len = len;
Willy Tarreau94aa6172015-03-13 14:19:06 +01001794 len = bi_putblk(&socket->s->req, buf+sent, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001795
1796 /* "Not enough space" (-1), "Buffer too little to contain
1797 * the data" (-2) are not expected because the available length
1798 * is tested.
1799 * Other unknown error are also not expected.
1800 */
1801 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01001802 if (len == -1)
Willy Tarreau94aa6172015-03-13 14:19:06 +01001803 socket->s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001804
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001805 MAY_LJMP(hlua_socket_close(L));
1806 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001807 lua_pushinteger(L, -1);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001808 return 1;
1809 }
1810
1811 /* update buffers. */
Willy Tarreau828824a2015-04-19 17:20:03 +02001812 si_applet_done(&socket->s->si[0]);
Willy Tarreau94aa6172015-03-13 14:19:06 +01001813 socket->s->req.rex = TICK_ETERNITY;
1814 socket->s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001815
1816 /* Update length sent. */
1817 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001818 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001819
1820 /* All the data buffer is sent ? */
1821 if (sent + len >= buf_len)
1822 return 1;
1823
1824hlua_socket_write_yield_return:
1825 appctx = objt_appctx(socket->s->si[0].end);
1826 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
1827 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001828 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001829 return 0;
1830}
1831
1832/* This function initiate the send of data. It just check the input
1833 * parameters and push an integer in the Lua stack that contain the
1834 * amount of data writed in the buffer. This is used by the function
1835 * "hlua_socket_write_yield" that can yield.
1836 *
1837 * The Lua function gets between 3 and 4 parameters. The first one is
1838 * the associated object. The second is a string buffer. The third is
1839 * a facultative integer that represents where is the buffer position
1840 * of the start of the data that can send. The first byte is the
1841 * position "1". The default value is "1". The fourth argument is a
1842 * facultative integer that represents where is the buffer position
1843 * of the end of the data that can send. The default is the last byte.
1844 */
1845static int hlua_socket_send(struct lua_State *L)
1846{
1847 int i;
1848 int j;
1849 const char *buf;
1850 size_t buf_len;
1851
1852 /* Check number of arguments. */
1853 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
1854 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
1855
1856 /* Get the string. */
1857 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
1858
1859 /* Get and check j. */
1860 if (lua_gettop(L) == 4) {
1861 j = MAY_LJMP(luaL_checkinteger(L, 4));
1862 if (j < 0)
1863 j = buf_len + j + 1;
1864 if (j > buf_len)
1865 j = buf_len + 1;
1866 lua_pop(L, 1);
1867 }
1868 else
1869 j = buf_len;
1870
1871 /* Get and check i. */
1872 if (lua_gettop(L) == 3) {
1873 i = MAY_LJMP(luaL_checkinteger(L, 3));
1874 if (i < 0)
1875 i = buf_len + i + 1;
1876 if (i > buf_len)
1877 i = buf_len + 1;
1878 lua_pop(L, 1);
1879 } else
1880 i = 1;
1881
1882 /* Check bth i and j. */
1883 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001884 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001885 return 1;
1886 }
1887 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001888 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001889 return 1;
1890 }
1891 if (i == 0)
1892 i = 1;
1893 if (j == 0)
1894 j = 1;
1895
1896 /* Pop the string. */
1897 lua_pop(L, 1);
1898
1899 /* Update the buffer length. */
1900 buf += i - 1;
1901 buf_len = j - i + 1;
1902 lua_pushlstring(L, buf, buf_len);
1903
1904 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001905 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001907 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908}
1909
1910#define SOCKET_INFO_EXPANDED_FORM "[0000:0000:0000:0000:0000:0000:0000:0000]:12345"
1911static char _socket_info_expanded_form[] = SOCKET_INFO_EXPANDED_FORM;
1912#define SOCKET_INFO_MAX_LEN (sizeof(_socket_info_expanded_form))
1913__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
1914{
1915 static char buffer[SOCKET_INFO_MAX_LEN];
1916 int ret;
1917 int len;
1918 char *p;
1919
1920 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
1921 if (ret <= 0) {
1922 lua_pushnil(L);
1923 return 1;
1924 }
1925
1926 if (ret == AF_UNIX) {
1927 lua_pushstring(L, buffer+1);
1928 return 1;
1929 }
1930 else if (ret == AF_INET6) {
1931 buffer[0] = '[';
1932 len = strlen(buffer);
1933 buffer[len] = ']';
1934 len++;
1935 buffer[len] = ':';
1936 len++;
1937 p = buffer;
1938 }
1939 else if (ret == AF_INET) {
1940 p = buffer + 1;
1941 len = strlen(p);
1942 p[len] = ':';
1943 len++;
1944 }
1945 else {
1946 lua_pushnil(L);
1947 return 1;
1948 }
1949
1950 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
1951 lua_pushnil(L);
1952 return 1;
1953 }
1954
1955 lua_pushstring(L, p);
1956 return 1;
1957}
1958
1959/* Returns information about the peer of the connection. */
1960__LJMP static int hlua_socket_getpeername(struct lua_State *L)
1961{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001962 struct hlua_socket *socket;
1963 struct connection *conn;
1964
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001965 MAY_LJMP(check_args(L, 1, "getpeername"));
1966
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001967 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001968
1969 /* Check if the tcp object is avalaible. */
1970 if (!socket->s) {
1971 lua_pushnil(L);
1972 return 1;
1973 }
1974
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001975 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001976 if (!conn) {
1977 lua_pushnil(L);
1978 return 1;
1979 }
1980
1981 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
1982 unsigned int salen = sizeof(conn->addr.to);
1983 if (getpeername(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, &salen) == -1) {
1984 lua_pushnil(L);
1985 return 1;
1986 }
1987 conn->flags |= CO_FL_ADDR_TO_SET;
1988 }
1989
1990 return MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
1991}
1992
1993/* Returns information about my connection side. */
1994static int hlua_socket_getsockname(struct lua_State *L)
1995{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001996 struct hlua_socket *socket;
1997 struct connection *conn;
1998
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001999 MAY_LJMP(check_args(L, 1, "getsockname"));
2000
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002001 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002002
2003 /* Check if the tcp object is avalaible. */
2004 if (!socket->s) {
2005 lua_pushnil(L);
2006 return 1;
2007 }
2008
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002009 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002010 if (!conn) {
2011 lua_pushnil(L);
2012 return 1;
2013 }
2014
2015 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
2016 unsigned int salen = sizeof(conn->addr.from);
2017 if (getsockname(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, &salen) == -1) {
2018 lua_pushnil(L);
2019 return 1;
2020 }
2021 conn->flags |= CO_FL_ADDR_FROM_SET;
2022 }
2023
2024 return hlua_socket_info(L, &conn->addr.from);
2025}
2026
2027/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002028static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002029 .obj_type = OBJ_TYPE_APPLET,
2030 .name = "<LUA_TCP>",
2031 .fct = hlua_socket_handler,
2032 .release = hlua_socket_release,
2033};
2034
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002035__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002036{
2037 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2038 struct hlua *hlua = hlua_gethlua(L);
2039 struct appctx *appctx;
2040
2041 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002042 if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002043 lua_pushnil(L);
2044 lua_pushstring(L, "Can't connect");
2045 return 2;
2046 }
2047
2048 appctx = objt_appctx(socket->s->si[0].end);
2049
2050 /* Check for connection established. */
2051 if (appctx->ctx.hlua.connected) {
2052 lua_pushinteger(L, 1);
2053 return 1;
2054 }
2055
2056 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2057 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002058 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002059 return 0;
2060}
2061
2062/* This function fail or initite the connection. */
2063__LJMP static int hlua_socket_connect(struct lua_State *L)
2064{
2065 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002066 int port;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002067 const char *ip;
2068 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002069 struct hlua *hlua;
2070 struct appctx *appctx;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002071
2072 MAY_LJMP(check_args(L, 3, "connect"));
2073
2074 /* Get args. */
2075 socket = MAY_LJMP(hlua_checksocket(L, 1));
2076 ip = MAY_LJMP(luaL_checkstring(L, 2));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002077 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002078
Willy Tarreau350f4872014-11-28 14:42:25 +01002079 conn = si_alloc_conn(&socket->s->si[1], 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002080 if (!conn)
2081 WILL_LJMP(luaL_error(L, "connect: internal error"));
2082
2083 /* Parse ip address. */
2084 conn->addr.to.ss_family = AF_UNSPEC;
2085 if (!str2ip2(ip, &conn->addr.to, 0))
2086 WILL_LJMP(luaL_error(L, "connect: cannot parse ip address '%s'", ip));
2087
2088 /* Set port. */
2089 if (conn->addr.to.ss_family == AF_INET)
2090 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2091 else if (conn->addr.to.ss_family == AF_INET6)
2092 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2093
2094 /* it is important not to call the wakeup function directly but to
2095 * pass through task_wakeup(), because this one knows how to apply
2096 * priorities to tasks.
2097 */
2098 task_wakeup(socket->s->task, TASK_WOKEN_INIT);
2099
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002100 hlua = hlua_gethlua(L);
2101 appctx = objt_appctx(socket->s->si[0].end);
2102 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2103 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002104 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002105
2106 return 0;
2107}
2108
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002109#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002110__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2111{
2112 struct hlua_socket *socket;
2113
2114 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2115 socket = MAY_LJMP(hlua_checksocket(L, 1));
2116 socket->s->target = &socket_ssl.obj_type;
2117 return MAY_LJMP(hlua_socket_connect(L));
2118}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002119#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002120
2121__LJMP static int hlua_socket_setoption(struct lua_State *L)
2122{
2123 return 0;
2124}
2125
2126__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2127{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002128 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002129 int tmout;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002130
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002131 MAY_LJMP(check_args(L, 2, "settimeout"));
2132
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002133 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002134 tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002135
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002136 socket->s->req.rto = tmout;
2137 socket->s->req.wto = tmout;
2138 socket->s->res.rto = tmout;
2139 socket->s->res.wto = tmout;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002140
2141 return 0;
2142}
2143
2144__LJMP static int hlua_socket_new(lua_State *L)
2145{
2146 struct hlua_socket *socket;
2147 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002148 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002149 struct stream *strm;
Willy Tarreaud420a972015-04-06 00:39:18 +02002150 struct task *task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002151
2152 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002153 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002154 hlua_pusherror(L, "socket: full stack");
2155 goto out_fail_conf;
2156 }
2157
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002158 /* Create the object: obj[0] = userdata. */
2159 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002160 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002161 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002162 memset(socket, 0, sizeof(*socket));
2163
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002164 /* Check if the various memory pools are intialized. */
Willy Tarreau87b09662015-04-03 00:22:06 +02002165 if (!pool2_stream || !pool2_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002166 hlua_pusherror(L, "socket: uninitialized pools.");
2167 goto out_fail_conf;
2168 }
2169
Willy Tarreau87b09662015-04-03 00:22:06 +02002170 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002171 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2172 lua_setmetatable(L, -2);
2173
Willy Tarreaud420a972015-04-06 00:39:18 +02002174 /* Create the applet context */
2175 appctx = appctx_new(&update_applet);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002176 if (!appctx) {
2177 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002178 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002179 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002180
Willy Tarreaud420a972015-04-06 00:39:18 +02002181 appctx->ctx.hlua.socket = socket;
2182 appctx->ctx.hlua.connected = 0;
2183 LIST_INIT(&appctx->ctx.hlua.wake_on_write);
2184 LIST_INIT(&appctx->ctx.hlua.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002185
Willy Tarreaud420a972015-04-06 00:39:18 +02002186 /* Now create a session, task and stream for this applet */
2187 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2188 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002189 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002190 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002191 }
2192
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002193 task = task_new();
2194 if (!task) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002195 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002196 goto out_fail_task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002197 }
Willy Tarreaud420a972015-04-06 00:39:18 +02002198 task->nice = 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002199
Willy Tarreau73b65ac2015-04-08 18:26:29 +02002200 strm = stream_new(sess, task, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002201 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002202 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002203 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002204 }
2205
Willy Tarreaud420a972015-04-06 00:39:18 +02002206 /* Configure an empty Lua for the stream. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002207 socket->s = strm;
2208 strm->hlua.T = NULL;
2209 strm->hlua.Tref = LUA_REFNIL;
2210 strm->hlua.Mref = LUA_REFNIL;
2211 strm->hlua.nargs = 0;
2212 strm->hlua.flags = 0;
2213 LIST_INIT(&strm->hlua.com);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002214
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002215 /* Configure "right" stream interface. this "si" is used to connect
2216 * and retrieve data from the server. The connection is initialized
2217 * with the "struct server".
2218 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002219 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002220
2221 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002222 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2223 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002224
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002225 /* Update statistics counters. */
2226 socket_proxy.feconn++; /* beconn will be increased later */
2227 jobs++;
2228 totalconn++;
2229
2230 /* Return yield waiting for connection. */
2231 return 1;
2232
Willy Tarreaud420a972015-04-06 00:39:18 +02002233 out_fail_stream:
2234 task_free(task);
2235 out_fail_task:
Willy Tarreau11c36242015-04-04 15:54:03 +02002236 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002237 out_fail_sess:
2238 appctx_free(appctx);
2239 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002240 WILL_LJMP(lua_error(L));
2241 return 0;
2242}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002243
2244/*
2245 *
2246 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002247 * Class Channel
2248 *
2249 *
2250 */
2251
2252/* Returns the struct hlua_channel join to the class channel in the
2253 * stack entry "ud" or throws an argument error.
2254 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002255__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002256{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002257 return (struct channel *)MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002258}
2259
Willy Tarreau47860ed2015-03-10 14:07:50 +01002260/* Pushes the channel onto the top of the stack. If the stask does not have a
2261 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002262 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002263static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002264{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002265 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002266 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002267 return 0;
2268
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002269 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002270 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002271 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002272
2273 /* Pop a class sesison metatable and affect it to the userdata. */
2274 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2275 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002276 return 1;
2277}
2278
2279/* Duplicate all the data present in the input channel and put it
2280 * in a string LUA variables. Returns -1 and push a nil value in
2281 * the stack if the channel is closed and all the data are consumed,
2282 * returns 0 if no data are available, otherwise it returns the length
2283 * of the builded string.
2284 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002285static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002286{
2287 char *blk1;
2288 char *blk2;
2289 int len1;
2290 int len2;
2291 int ret;
2292 luaL_Buffer b;
2293
Willy Tarreau47860ed2015-03-10 14:07:50 +01002294 ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002295 if (unlikely(ret == 0))
2296 return 0;
2297
2298 if (unlikely(ret < 0)) {
2299 lua_pushnil(L);
2300 return -1;
2301 }
2302
2303 luaL_buffinit(L, &b);
2304 luaL_addlstring(&b, blk1, len1);
2305 if (unlikely(ret == 2))
2306 luaL_addlstring(&b, blk2, len2);
2307 luaL_pushresult(&b);
2308
2309 if (unlikely(ret == 2))
2310 return len1 + len2;
2311 return len1;
2312}
2313
2314/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2315 * a yield. This function keep the data in the buffer.
2316 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002317__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002318{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002319 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002320
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002321 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2322
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002323 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002324 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002325 return 1;
2326}
2327
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002328/* Check arguments for the function "hlua_channel_dup_yield". */
2329__LJMP static int hlua_channel_dup(lua_State *L)
2330{
2331 MAY_LJMP(check_args(L, 1, "dup"));
2332 MAY_LJMP(hlua_checkchannel(L, 1));
2333 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2334}
2335
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002336/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2337 * a yield. This function consumes the data in the buffer. It returns
2338 * a string containing the data or a nil pointer if no data are available
2339 * and the channel is closed.
2340 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002341__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002342{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002343 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002344 int ret;
2345
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002346 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002347
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002348 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002349 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002350 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002351
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002352 if (unlikely(ret == -1))
2353 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002354
Willy Tarreau47860ed2015-03-10 14:07:50 +01002355 chn->buf->i -= ret;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002356 return 1;
2357}
2358
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002359/* Check arguments for the fucntion "hlua_channel_get_yield". */
2360__LJMP static int hlua_channel_get(lua_State *L)
2361{
2362 MAY_LJMP(check_args(L, 1, "get"));
2363 MAY_LJMP(hlua_checkchannel(L, 1));
2364 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2365}
2366
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002367/* This functions consumes and returns one line. If the channel is closed,
2368 * and the last data does not contains a final '\n', the data are returned
2369 * without the final '\n'. When no more data are avalaible, it returns nil
2370 * value.
2371 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002372__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002373{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002374 char *blk1;
2375 char *blk2;
2376 int len1;
2377 int len2;
2378 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002379 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002380 int ret;
2381 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002382
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002383 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2384
Willy Tarreau47860ed2015-03-10 14:07:50 +01002385 ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002386 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002387 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002388
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002389 if (ret == -1) {
2390 lua_pushnil(L);
2391 return 1;
2392 }
2393
2394 luaL_buffinit(L, &b);
2395 luaL_addlstring(&b, blk1, len1);
2396 len = len1;
2397 if (unlikely(ret == 2)) {
2398 luaL_addlstring(&b, blk2, len2);
2399 len += len2;
2400 }
2401 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002402 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002403 return 1;
2404}
2405
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002406/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2407__LJMP static int hlua_channel_getline(lua_State *L)
2408{
2409 MAY_LJMP(check_args(L, 1, "getline"));
2410 MAY_LJMP(hlua_checkchannel(L, 1));
2411 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2412}
2413
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002414/* This function takes a string as input, and append it at the
2415 * input side of channel. If the data is too big, but a space
2416 * is probably available after sending some data, the function
2417 * yield. If the data is bigger than the buffer, or if the
2418 * channel is closed, it returns -1. otherwise, it returns the
2419 * amount of data writed.
2420 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002421__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002422{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002423 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002424 size_t len;
2425 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2426 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2427 int ret;
2428 int max;
2429
Willy Tarreau47860ed2015-03-10 14:07:50 +01002430 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002431 if (max > len - l)
2432 max = len - l;
2433
Willy Tarreau47860ed2015-03-10 14:07:50 +01002434 ret = bi_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002435 if (ret == -2 || ret == -3) {
2436 lua_pushinteger(L, -1);
2437 return 1;
2438 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002439 if (ret == -1) {
2440 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002441 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002442 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002443 l += ret;
2444 lua_pop(L, 1);
2445 lua_pushinteger(L, l);
2446
Willy Tarreau47860ed2015-03-10 14:07:50 +01002447 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2448 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002449 /* There are no space avalaible, and the output buffer is empty.
2450 * in this case, we cannot add more data, so we cannot yield,
2451 * we return the amount of copyied data.
2452 */
2453 return 1;
2454 }
2455 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002456 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002457 return 1;
2458}
2459
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002460/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002461 * of the writed string, or -1 if the channel is closed or if the
2462 * buffer size is too little for the data.
2463 */
2464__LJMP static int hlua_channel_append(lua_State *L)
2465{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002466 size_t len;
2467
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002468 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002469 MAY_LJMP(hlua_checkchannel(L, 1));
2470 MAY_LJMP(luaL_checklstring(L, 2, &len));
2471 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002472 lua_pushinteger(L, 0);
2473
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002474 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002475}
2476
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002477/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002478 * his process by cleaning the buffer. The result is a replacement
2479 * of the current data. It returns the length of the writed string,
2480 * or -1 if the channel is closed or if the buffer size is too
2481 * little for the data.
2482 */
2483__LJMP static int hlua_channel_set(lua_State *L)
2484{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002485 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002486
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002487 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002488 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002489 lua_pushinteger(L, 0);
2490
Willy Tarreau47860ed2015-03-10 14:07:50 +01002491 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002492
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002493 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002494}
2495
2496/* Append data in the output side of the buffer. This data is immediatly
2497 * sent. The fcuntion returns the ammount of data writed. If the buffer
2498 * cannot contains the data, the function yield. The function returns -1
2499 * if the channel is closed.
2500 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002501__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002502{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002503 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002504 size_t len;
2505 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2506 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2507 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002508 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002509
Willy Tarreau47860ed2015-03-10 14:07:50 +01002510 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002511 lua_pushinteger(L, -1);
2512 return 1;
2513 }
2514
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002515 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2516 * the request buffer if its not required.
2517 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002518 if (chn->buf->size == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002519 if (!stream_alloc_recv_buffer(chn)) {
Willy Tarreau47860ed2015-03-10 14:07:50 +01002520 chn_prod(chn)->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002521 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002522 }
2523 }
2524
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002525 /* the writed data will be immediatly sent, so we can check
2526 * the avalaible space without taking in account the reserve.
2527 * The reserve is guaranted for the processing of incoming
2528 * data, because the buffer will be flushed.
2529 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002530 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002531
2532 /* If there are no space avalaible, and the output buffer is empty.
2533 * in this case, we cannot add more data, so we cannot yield,
2534 * we return the amount of copyied data.
2535 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002536 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002537 return 1;
2538
2539 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002540 if (max > len - l)
2541 max = len - l;
2542
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002543 /* The buffer avalaible size may be not contiguous. This test
2544 * detects a non contiguous buffer and realign it.
2545 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002546 if (bi_space_for_replace(chn->buf) < max)
2547 buffer_slow_realign(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002548
2549 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002550 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002551
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002552 /* buffer replace considers that the input part is filled.
2553 * so, I must forward these new data in the output part.
2554 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002555 b_adv(chn->buf, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002556
2557 l += max;
2558 lua_pop(L, 1);
2559 lua_pushinteger(L, l);
2560
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002561 /* If there are no space avalaible, and the output buffer is empty.
2562 * in this case, we cannot add more data, so we cannot yield,
2563 * we return the amount of copyied data.
2564 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002565 max = chn->buf->size - buffer_len(chn->buf);
2566 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002567 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002568
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002569 if (l < len) {
2570 /* If we are waiting for space in the response buffer, we
2571 * must set the flag WAKERESWR. This flag required the task
2572 * wake up if any activity is detected on the response buffer.
2573 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002574 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002575 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002576 else
2577 HLUA_SET_WAKEREQWR(hlua);
2578 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002579 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002580
2581 return 1;
2582}
2583
2584/* Just a wraper of "_hlua_channel_send". This wrapper permits
2585 * yield the LUA process, and resume it without checking the
2586 * input arguments.
2587 */
2588__LJMP static int hlua_channel_send(lua_State *L)
2589{
2590 MAY_LJMP(check_args(L, 2, "send"));
2591 lua_pushinteger(L, 0);
2592
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002593 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002594}
2595
2596/* This function forward and amount of butes. The data pass from
2597 * the input side of the buffer to the output side, and can be
2598 * forwarded. This function never fails.
2599 *
2600 * The Lua function takes an amount of bytes to be forwarded in
2601 * imput. It returns the number of bytes forwarded.
2602 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002603__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002604{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002605 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002606 int len;
2607 int l;
2608 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002609 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002610
2611 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2612 len = MAY_LJMP(luaL_checkinteger(L, 2));
2613 l = MAY_LJMP(luaL_checkinteger(L, -1));
2614
2615 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002616 if (max > chn->buf->i)
2617 max = chn->buf->i;
2618 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002619 l += max;
2620
2621 lua_pop(L, 1);
2622 lua_pushinteger(L, l);
2623
2624 /* Check if it miss bytes to forward. */
2625 if (l < len) {
2626 /* The the input channel or the output channel are closed, we
2627 * must return the amount of data forwarded.
2628 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002629 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002630 return 1;
2631
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002632 /* If we are waiting for space data in the response buffer, we
2633 * must set the flag WAKERESWR. This flag required the task
2634 * wake up if any activity is detected on the response buffer.
2635 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002636 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002637 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002638 else
2639 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002640
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002641 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002642 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002643 }
2644
2645 return 1;
2646}
2647
2648/* Just check the input and prepare the stack for the previous
2649 * function "hlua_channel_forward_yield"
2650 */
2651__LJMP static int hlua_channel_forward(lua_State *L)
2652{
2653 MAY_LJMP(check_args(L, 2, "forward"));
2654 MAY_LJMP(hlua_checkchannel(L, 1));
2655 MAY_LJMP(luaL_checkinteger(L, 2));
2656
2657 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002658 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002659}
2660
2661/* Just returns the number of bytes available in the input
2662 * side of the buffer. This function never fails.
2663 */
2664__LJMP static int hlua_channel_get_in_len(lua_State *L)
2665{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002666 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002667
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002668 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002669 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002670 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002671 return 1;
2672}
2673
2674/* Just returns the number of bytes available in the output
2675 * side of the buffer. This function never fails.
2676 */
2677__LJMP static int hlua_channel_get_out_len(lua_State *L)
2678{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002679 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002680
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002681 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002682 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002683 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002684 return 1;
2685}
2686
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002687/*
2688 *
2689 *
2690 * Class Fetches
2691 *
2692 *
2693 */
2694
2695/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002696 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002697 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002698__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002699{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002700 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002701}
2702
2703/* This function creates and push in the stack a fetch object according
2704 * with a current TXN.
2705 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002706static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002707{
Willy Tarreau7073c472015-04-06 11:15:40 +02002708 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002709
2710 /* Check stack size. */
2711 if (!lua_checkstack(L, 3))
2712 return 0;
2713
2714 /* Create the object: obj[0] = userdata.
2715 * Note that the base of the Fetches object is the
2716 * transaction object.
2717 */
2718 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002719 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002720 lua_rawseti(L, -2, 0);
2721
Willy Tarreau7073c472015-04-06 11:15:40 +02002722 hsmp->s = txn->s;
2723 hsmp->p = txn->p;
Willy Tarreau7073c472015-04-06 11:15:40 +02002724 hsmp->stringsafe = stringsafe;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002725
2726 /* Pop a class sesison metatable and affect it to the userdata. */
2727 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
2728 lua_setmetatable(L, -2);
2729
2730 return 1;
2731}
2732
2733/* This function is an LUA binding. It is called with each sample-fetch.
2734 * It uses closure argument to store the associated sample-fetch. It
2735 * returns only one argument or throws an error. An error is thrown
2736 * only if an error is encountered during the argument parsing. If
2737 * the "sample-fetch" function fails, nil is returned.
2738 */
2739__LJMP static int hlua_run_sample_fetch(lua_State *L)
2740{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002741 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01002742 struct sample_fetch *f;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002743 struct arg args[ARGM_NBARGS + 1];
2744 int i;
2745 struct sample smp;
2746
2747 /* Get closure arguments. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002748 f = (struct sample_fetch *)lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002749
2750 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002751 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002752
2753 /* Get extra arguments. */
2754 for (i = 0; i < lua_gettop(L) - 1; i++) {
2755 if (i >= ARGM_NBARGS)
2756 break;
2757 hlua_lua2arg(L, i + 2, &args[i]);
2758 }
2759 args[i].type = ARGT_STOP;
2760
2761 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002762 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002763
2764 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002765 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002766 lua_pushfstring(L, "error in arguments");
2767 WILL_LJMP(lua_error(L));
2768 }
2769
2770 /* Initialise the sample. */
2771 memset(&smp, 0, sizeof(smp));
2772
2773 /* Run the sample fetch process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02002774 smp.px = hsmp->p;
2775 smp.sess = hsmp->s->sess;
2776 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02002777 smp.opt = 0;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002778 if (!f->process(args, &smp, f->kw, f->private)) {
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002779 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002780 lua_pushstring(L, "");
2781 else
2782 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002783 return 1;
2784 }
2785
2786 /* Convert the returned sample in lua value. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002787 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002788 hlua_smp2lua_str(L, &smp);
2789 else
2790 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002791 return 1;
2792}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002793
2794/*
2795 *
2796 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002797 * Class Converters
2798 *
2799 *
2800 */
2801
2802/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002803 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002804 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002805__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002806{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002807 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002808}
2809
2810/* This function creates and push in the stack a Converters object
2811 * according with a current TXN.
2812 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002813static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002814{
Willy Tarreau7073c472015-04-06 11:15:40 +02002815 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002816
2817 /* Check stack size. */
2818 if (!lua_checkstack(L, 3))
2819 return 0;
2820
2821 /* Create the object: obj[0] = userdata.
2822 * Note that the base of the Converters object is the
2823 * same than the TXN object.
2824 */
2825 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002826 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002827 lua_rawseti(L, -2, 0);
2828
Willy Tarreau7073c472015-04-06 11:15:40 +02002829 hsmp->s = txn->s;
2830 hsmp->p = txn->p;
Willy Tarreau7073c472015-04-06 11:15:40 +02002831 hsmp->stringsafe = stringsafe;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002832
Willy Tarreau87b09662015-04-03 00:22:06 +02002833 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002834 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
2835 lua_setmetatable(L, -2);
2836
2837 return 1;
2838}
2839
2840/* This function is an LUA binding. It is called with each converter.
2841 * It uses closure argument to store the associated converter. It
2842 * returns only one argument or throws an error. An error is thrown
2843 * only if an error is encountered during the argument parsing. If
2844 * the converter function function fails, nil is returned.
2845 */
2846__LJMP static int hlua_run_sample_conv(lua_State *L)
2847{
Willy Tarreauda5f1082015-04-06 11:17:13 +02002848 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002849 struct sample_conv *conv;
2850 struct arg args[ARGM_NBARGS + 1];
2851 int i;
2852 struct sample smp;
2853
2854 /* Get closure arguments. */
2855 conv = (struct sample_conv *)lua_touserdata(L, lua_upvalueindex(1));
2856
2857 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02002858 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002859
2860 /* Get extra arguments. */
2861 for (i = 0; i < lua_gettop(L) - 2; i++) {
2862 if (i >= ARGM_NBARGS)
2863 break;
2864 hlua_lua2arg(L, i + 3, &args[i]);
2865 }
2866 args[i].type = ARGT_STOP;
2867
2868 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02002869 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002870
2871 /* Run the special args checker. */
2872 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
2873 hlua_pusherror(L, "error in arguments");
2874 WILL_LJMP(lua_error(L));
2875 }
2876
2877 /* Initialise the sample. */
2878 if (!hlua_lua2smp(L, 2, &smp)) {
2879 hlua_pusherror(L, "error in the input argument");
2880 WILL_LJMP(lua_error(L));
2881 }
2882
2883 /* Apply expected cast. */
2884 if (!sample_casts[smp.type][conv->in_type]) {
2885 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
2886 smp_to_type[smp.type], smp_to_type[conv->in_type]);
2887 WILL_LJMP(lua_error(L));
2888 }
2889 if (sample_casts[smp.type][conv->in_type] != c_none &&
2890 !sample_casts[smp.type][conv->in_type](&smp)) {
2891 hlua_pusherror(L, "error during the input argument casting");
2892 WILL_LJMP(lua_error(L));
2893 }
2894
2895 /* Run the sample conversion process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02002896 smp.px = hsmp->p;
2897 smp.sess = hsmp->s->sess;
2898 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02002899 smp.opt = 0;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002900 if (!conv->process(args, &smp, conv->private)) {
Willy Tarreauda5f1082015-04-06 11:17:13 +02002901 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002902 lua_pushstring(L, "");
2903 else
2904 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002905 return 1;
2906 }
2907
2908 /* Convert the returned sample in lua value. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02002909 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002910 hlua_smp2lua_str(L, &smp);
2911 else
2912 hlua_smp2lua(L, &smp);
2913 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002914}
2915
2916/*
2917 *
2918 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002919 * Class HTTP
2920 *
2921 *
2922 */
2923
2924/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002925 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002926 */
2927__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
2928{
2929 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
2930}
2931
2932/* This function creates and push in the stack a HTTP object
2933 * according with a current TXN.
2934 */
2935static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
2936{
Willy Tarreau9a8ad862015-04-06 11:14:06 +02002937 struct hlua_txn *htxn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002938
2939 /* Check stack size. */
2940 if (!lua_checkstack(L, 3))
2941 return 0;
2942
2943 /* Create the object: obj[0] = userdata.
2944 * Note that the base of the Converters object is the
2945 * same than the TXN object.
2946 */
2947 lua_newtable(L);
Willy Tarreau9a8ad862015-04-06 11:14:06 +02002948 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002949 lua_rawseti(L, -2, 0);
2950
Willy Tarreau9a8ad862015-04-06 11:14:06 +02002951 htxn->s = txn->s;
2952 htxn->p = txn->p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002953
Willy Tarreau87b09662015-04-03 00:22:06 +02002954 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002955 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
2956 lua_setmetatable(L, -2);
2957
2958 return 1;
2959}
2960
2961/* This function creates ans returns an array of HTTP headers.
2962 * This function does not fails. It is used as wrapper with the
2963 * 2 following functions.
2964 */
2965__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
2966{
2967 const char *cur_ptr, *cur_next, *p;
2968 int old_idx, cur_idx;
2969 struct hdr_idx_elem *cur_hdr;
2970 const char *hn, *hv;
2971 int hnl, hvl;
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01002972 int type;
2973 const char *in;
2974 char *out;
2975 int len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002976
2977 /* Create the table. */
2978 lua_newtable(L);
2979
Willy Tarreaueee5b512015-04-03 23:46:31 +02002980 if (!htxn->s->txn)
2981 return 1;
2982
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002983 /* Build array of headers. */
2984 old_idx = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002985 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002986
2987 while (1) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002988 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002989 if (!cur_idx)
2990 break;
2991 old_idx = cur_idx;
2992
Willy Tarreaueee5b512015-04-03 23:46:31 +02002993 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002994 cur_ptr = cur_next;
2995 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
2996
2997 /* Now we have one full header at cur_ptr of len cur_hdr->len,
2998 * and the next header starts at cur_next. We'll check
2999 * this header in the list as well as against the default
3000 * rule.
3001 */
3002
3003 /* look for ': *'. */
3004 hn = cur_ptr;
3005 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
3006 if (p >= cur_ptr+cur_hdr->len)
3007 continue;
3008 hnl = p - hn;
3009 p++;
3010 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
3011 p++;
3012 if (p >= cur_ptr+cur_hdr->len)
3013 continue;
3014 hv = p;
3015 hvl = cur_ptr+cur_hdr->len-p;
3016
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003017 /* Lowercase the key. Don't check the size of trash, it have
3018 * the size of one buffer and the input data contains in one
3019 * buffer.
3020 */
3021 out = trash.str;
3022 for (in=hn; in<hn+hnl; in++, out++)
3023 *out = tolower(*in);
3024 *out = '\0';
3025
3026 /* Check for existing entry:
3027 * assume that the table is on the top of the stack, and
3028 * push the key in the stack, the function lua_gettable()
3029 * perform the lookup.
3030 */
3031 lua_pushlstring(L, trash.str, hnl);
3032 lua_gettable(L, -2);
3033 type = lua_type(L, -1);
3034
3035 switch (type) {
3036 case LUA_TNIL:
3037 /* Table not found, create it. */
3038 lua_pop(L, 1); /* remove the nil value. */
3039 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
3040 lua_newtable(L); /* create and push empty table. */
3041 lua_pushlstring(L, hv, hvl); /* push header value. */
3042 lua_rawseti(L, -2, 0); /* index header value (pop it). */
3043 lua_rawset(L, -3); /* index new table with header name (pop the values). */
3044 break;
3045
3046 case LUA_TTABLE:
3047 /* Entry found: push the value in the table. */
3048 len = lua_rawlen(L, -1);
3049 lua_pushlstring(L, hv, hvl); /* push header value. */
3050 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
3051 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
3052 break;
3053
3054 default:
3055 /* Other cases are errors. */
3056 hlua_pusherror(L, "internal error during the parsing of headers.");
3057 WILL_LJMP(lua_error(L));
3058 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003059 }
3060
3061 return 1;
3062}
3063
3064__LJMP static int hlua_http_req_get_headers(lua_State *L)
3065{
3066 struct hlua_txn *htxn;
3067
3068 MAY_LJMP(check_args(L, 1, "req_get_headers"));
3069 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3070
Willy Tarreaueee5b512015-04-03 23:46:31 +02003071 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003072}
3073
3074__LJMP static int hlua_http_res_get_headers(lua_State *L)
3075{
3076 struct hlua_txn *htxn;
3077
3078 MAY_LJMP(check_args(L, 1, "res_get_headers"));
3079 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3080
Willy Tarreaueee5b512015-04-03 23:46:31 +02003081 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003082}
3083
3084/* This function replace full header, or just a value in
3085 * the request or in the response. It is a wrapper fir the
3086 * 4 following functions.
3087 */
3088__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
3089 struct http_msg *msg, int action)
3090{
3091 size_t name_len;
3092 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3093 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
3094 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
3095 struct my_regex re;
3096
3097 if (!regex_comp(reg, &re, 1, 1, NULL))
3098 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
3099
3100 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
3101 regex_free(&re);
3102 return 0;
3103}
3104
3105__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
3106{
3107 struct hlua_txn *htxn;
3108
3109 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3110 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3111
Willy Tarreaueee5b512015-04-03 23:46:31 +02003112 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, HTTP_REQ_ACT_REPLACE_HDR));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003113}
3114
3115__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
3116{
3117 struct hlua_txn *htxn;
3118
3119 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
3120 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3121
Willy Tarreaueee5b512015-04-03 23:46:31 +02003122 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, HTTP_RES_ACT_REPLACE_HDR));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003123}
3124
3125__LJMP static int hlua_http_req_rep_val(lua_State *L)
3126{
3127 struct hlua_txn *htxn;
3128
3129 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3130 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3131
Willy Tarreaueee5b512015-04-03 23:46:31 +02003132 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, HTTP_REQ_ACT_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003133}
3134
3135__LJMP static int hlua_http_res_rep_val(lua_State *L)
3136{
3137 struct hlua_txn *htxn;
3138
3139 MAY_LJMP(check_args(L, 4, "res_rep_val"));
3140 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3141
Willy Tarreaueee5b512015-04-03 23:46:31 +02003142 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, HTTP_RES_ACT_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003143}
3144
3145/* This function deletes all the occurences of an header.
3146 * It is a wrapper for the 2 following functions.
3147 */
3148__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3149{
3150 size_t len;
3151 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3152 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003153 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003154
3155 ctx.idx = 0;
3156 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
3157 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3158 return 0;
3159}
3160
3161__LJMP static int hlua_http_req_del_hdr(lua_State *L)
3162{
3163 struct hlua_txn *htxn;
3164
3165 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3166 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3167
Willy Tarreaueee5b512015-04-03 23:46:31 +02003168 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003169}
3170
3171__LJMP static int hlua_http_res_del_hdr(lua_State *L)
3172{
3173 struct hlua_txn *htxn;
3174
3175 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3176 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3177
Willy Tarreaueee5b512015-04-03 23:46:31 +02003178 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003179}
3180
3181/* This function adds an header. It is a wrapper used by
3182 * the 2 following functions.
3183 */
3184__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3185{
3186 size_t name_len;
3187 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3188 size_t value_len;
3189 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
3190 char *p;
3191
3192 /* Check length. */
3193 trash.len = value_len + name_len + 2;
3194 if (trash.len > trash.size)
3195 return 0;
3196
3197 /* Creates the header string. */
3198 p = trash.str;
3199 memcpy(p, name, name_len);
3200 p += name_len;
3201 *p = ':';
3202 p++;
3203 *p = ' ';
3204 p++;
3205 memcpy(p, value, value_len);
3206
Willy Tarreaueee5b512015-04-03 23:46:31 +02003207 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003208 trash.str, trash.len) != 0);
3209
3210 return 0;
3211}
3212
3213__LJMP static int hlua_http_req_add_hdr(lua_State *L)
3214{
3215 struct hlua_txn *htxn;
3216
3217 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
3218 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3219
Willy Tarreaueee5b512015-04-03 23:46:31 +02003220 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003221}
3222
3223__LJMP static int hlua_http_res_add_hdr(lua_State *L)
3224{
3225 struct hlua_txn *htxn;
3226
3227 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
3228 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3229
Willy Tarreaueee5b512015-04-03 23:46:31 +02003230 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003231}
3232
3233static int hlua_http_req_set_hdr(lua_State *L)
3234{
3235 struct hlua_txn *htxn;
3236
3237 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
3238 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3239
Willy Tarreaueee5b512015-04-03 23:46:31 +02003240 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
3241 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003242}
3243
3244static int hlua_http_res_set_hdr(lua_State *L)
3245{
3246 struct hlua_txn *htxn;
3247
3248 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
3249 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3250
Willy Tarreaueee5b512015-04-03 23:46:31 +02003251 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
3252 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003253}
3254
3255/* This function set the method. */
3256static int hlua_http_req_set_meth(lua_State *L)
3257{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003258 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003259 size_t name_len;
3260 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003261
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003262 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003263 return 1;
3264}
3265
3266/* This function set the method. */
3267static int hlua_http_req_set_path(lua_State *L)
3268{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003269 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003270 size_t name_len;
3271 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003272 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003273 return 1;
3274}
3275
3276/* This function set the query-string. */
3277static int hlua_http_req_set_query(lua_State *L)
3278{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003279 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003280 size_t name_len;
3281 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003282
3283 /* Check length. */
3284 if (name_len > trash.size - 1) {
3285 lua_pushboolean(L, 0);
3286 return 1;
3287 }
3288
3289 /* Add the mark question as prefix. */
3290 chunk_reset(&trash);
3291 trash.str[trash.len++] = '?';
3292 memcpy(trash.str + trash.len, name, name_len);
3293 trash.len += name_len;
3294
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003295 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003296 return 1;
3297}
3298
3299/* This function set the uri. */
3300static int hlua_http_req_set_uri(lua_State *L)
3301{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003302 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003303 size_t name_len;
3304 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003305
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003306 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003307 return 1;
3308}
3309
3310/*
3311 *
3312 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003313 * Class TXN
3314 *
3315 *
3316 */
3317
3318/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003319 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003320 */
3321__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
3322{
3323 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
3324}
3325
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02003326__LJMP static int hlua_set_var(lua_State *L)
3327{
3328 struct hlua_txn *htxn;
3329 const char *name;
3330 size_t len;
3331 struct sample smp;
3332
3333 MAY_LJMP(check_args(L, 3, "set_var"));
3334
3335 /* It is useles to retrieve the stream, but this function
3336 * runs only in a stream context.
3337 */
3338 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3339 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3340
3341 /* Converts the third argument in a sample. */
3342 hlua_lua2smp(L, 3, &smp);
3343
3344 /* Store the sample in a variable. */
3345 vars_set_by_name(name, len, htxn->s, &smp);
3346 return 0;
3347}
3348
3349__LJMP static int hlua_get_var(lua_State *L)
3350{
3351 struct hlua_txn *htxn;
3352 const char *name;
3353 size_t len;
3354 struct sample smp;
3355
3356 MAY_LJMP(check_args(L, 2, "get_var"));
3357
3358 /* It is useles to retrieve the stream, but this function
3359 * runs only in a stream context.
3360 */
3361 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3362 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3363
3364 if (!vars_get_by_name(name, len, htxn->s, &smp)) {
3365 lua_pushnil(L);
3366 return 1;
3367 }
3368
3369 return hlua_smp2lua(L, &smp);
3370}
3371
Willy Tarreau59551662015-03-10 14:23:13 +01003372__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003373{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003374 struct hlua *hlua;
3375
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003376 MAY_LJMP(check_args(L, 2, "set_priv"));
3377
Willy Tarreau87b09662015-04-03 00:22:06 +02003378 /* It is useles to retrieve the stream, but this function
3379 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003380 */
3381 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003382 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003383
3384 /* Remove previous value. */
3385 if (hlua->Mref != -1)
3386 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
3387
3388 /* Get and store new value. */
3389 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3390 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3391
3392 return 0;
3393}
3394
Willy Tarreau59551662015-03-10 14:23:13 +01003395__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003396{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003397 struct hlua *hlua;
3398
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003399 MAY_LJMP(check_args(L, 1, "get_priv"));
3400
Willy Tarreau87b09662015-04-03 00:22:06 +02003401 /* It is useles to retrieve the stream, but this function
3402 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003403 */
3404 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003405 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003406
3407 /* Push configuration index in the stack. */
3408 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3409
3410 return 1;
3411}
3412
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003413/* Create stack entry containing a class TXN. This function
3414 * return 0 if the stack does not contains free slots,
3415 * otherwise it returns 1.
3416 */
Willy Tarreau15e91e12015-04-04 00:52:09 +02003417static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003418{
Willy Tarreaude491382015-04-06 11:04:28 +02003419 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003420
3421 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003422 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003423 return 0;
3424
3425 /* NOTE: The allocation never fails. The failure
3426 * throw an error, and the function never returns.
3427 * if the throw is not avalaible, the process is aborted.
3428 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003429 /* Create the object: obj[0] = userdata. */
3430 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02003431 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003432 lua_rawseti(L, -2, 0);
3433
Willy Tarreaude491382015-04-06 11:04:28 +02003434 htxn->s = s;
3435 htxn->p = p;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003436
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003437 /* Create the "f" field that contains a list of fetches. */
3438 lua_pushstring(L, "f");
Willy Tarreaude491382015-04-06 11:04:28 +02003439 if (!hlua_fetches_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003440 return 0;
3441 lua_settable(L, -3);
3442
3443 /* Create the "sf" field that contains a list of stringsafe fetches. */
3444 lua_pushstring(L, "sf");
Willy Tarreaude491382015-04-06 11:04:28 +02003445 if (!hlua_fetches_new(L, htxn, 1))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003446 return 0;
3447 lua_settable(L, -3);
3448
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003449 /* Create the "c" field that contains a list of converters. */
3450 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02003451 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003452 return 0;
3453 lua_settable(L, -3);
3454
3455 /* Create the "sc" field that contains a list of stringsafe converters. */
3456 lua_pushstring(L, "sc");
Willy Tarreaude491382015-04-06 11:04:28 +02003457 if (!hlua_converters_new(L, htxn, 1))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003458 return 0;
3459 lua_settable(L, -3);
3460
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003461 /* Create the "req" field that contains the request channel object. */
3462 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003463 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003464 return 0;
3465 lua_settable(L, -3);
3466
3467 /* Create the "res" field that contains the response channel object. */
3468 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003469 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003470 return 0;
3471 lua_settable(L, -3);
3472
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003473 /* Creates the HTTP object is the current proxy allows http. */
3474 lua_pushstring(L, "http");
3475 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02003476 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003477 return 0;
3478 }
3479 else
3480 lua_pushnil(L);
3481 lua_settable(L, -3);
3482
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003483 /* Pop a class sesison metatable and affect it to the userdata. */
3484 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
3485 lua_setmetatable(L, -2);
3486
3487 return 1;
3488}
3489
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003490__LJMP static int hlua_txn_deflog(lua_State *L)
3491{
3492 const char *msg;
3493 struct hlua_txn *htxn;
3494
3495 MAY_LJMP(check_args(L, 2, "deflog"));
3496 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3497 msg = MAY_LJMP(luaL_checkstring(L, 2));
3498
3499 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
3500 return 0;
3501}
3502
3503__LJMP static int hlua_txn_log(lua_State *L)
3504{
3505 int level;
3506 const char *msg;
3507 struct hlua_txn *htxn;
3508
3509 MAY_LJMP(check_args(L, 3, "log"));
3510 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3511 level = MAY_LJMP(luaL_checkinteger(L, 2));
3512 msg = MAY_LJMP(luaL_checkstring(L, 3));
3513
3514 if (level < 0 || level >= NB_LOG_LEVELS)
3515 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3516
3517 hlua_sendlog(htxn->s->be, level, msg);
3518 return 0;
3519}
3520
3521__LJMP static int hlua_txn_log_debug(lua_State *L)
3522{
3523 const char *msg;
3524 struct hlua_txn *htxn;
3525
3526 MAY_LJMP(check_args(L, 2, "Debug"));
3527 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3528 msg = MAY_LJMP(luaL_checkstring(L, 2));
3529 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
3530 return 0;
3531}
3532
3533__LJMP static int hlua_txn_log_info(lua_State *L)
3534{
3535 const char *msg;
3536 struct hlua_txn *htxn;
3537
3538 MAY_LJMP(check_args(L, 2, "Info"));
3539 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3540 msg = MAY_LJMP(luaL_checkstring(L, 2));
3541 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
3542 return 0;
3543}
3544
3545__LJMP static int hlua_txn_log_warning(lua_State *L)
3546{
3547 const char *msg;
3548 struct hlua_txn *htxn;
3549
3550 MAY_LJMP(check_args(L, 2, "Warning"));
3551 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3552 msg = MAY_LJMP(luaL_checkstring(L, 2));
3553 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
3554 return 0;
3555}
3556
3557__LJMP static int hlua_txn_log_alert(lua_State *L)
3558{
3559 const char *msg;
3560 struct hlua_txn *htxn;
3561
3562 MAY_LJMP(check_args(L, 2, "Alert"));
3563 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3564 msg = MAY_LJMP(luaL_checkstring(L, 2));
3565 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
3566 return 0;
3567}
3568
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003569__LJMP static int hlua_txn_set_loglevel(lua_State *L)
3570{
3571 struct hlua_txn *htxn;
3572 int ll;
3573
3574 MAY_LJMP(check_args(L, 2, "set_loglevel"));
3575 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3576 ll = MAY_LJMP(luaL_checkinteger(L, 2));
3577
3578 if (ll < 0 || ll > 7)
3579 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
3580
3581 htxn->s->logs.level = ll;
3582 return 0;
3583}
3584
3585__LJMP static int hlua_txn_set_tos(lua_State *L)
3586{
3587 struct hlua_txn *htxn;
3588 struct connection *cli_conn;
3589 int tos;
3590
3591 MAY_LJMP(check_args(L, 2, "set_tos"));
3592 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3593 tos = MAY_LJMP(luaL_checkinteger(L, 2));
3594
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003595 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003596 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, tos);
3597
3598 return 0;
3599}
3600
3601__LJMP static int hlua_txn_set_mark(lua_State *L)
3602{
3603#ifdef SO_MARK
3604 struct hlua_txn *htxn;
3605 struct connection *cli_conn;
3606 int mark;
3607
3608 MAY_LJMP(check_args(L, 2, "set_mark"));
3609 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3610 mark = MAY_LJMP(luaL_checkinteger(L, 2));
3611
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003612 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau07081fe2015-04-06 10:59:20 +02003613 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003614#endif
3615 return 0;
3616}
3617
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003618/* This function is an Lua binding that send pending data
3619 * to the client, and close the stream interface.
3620 */
3621__LJMP static int hlua_txn_close(lua_State *L)
3622{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003623 struct hlua_txn *htxn;
Willy Tarreau81389672015-03-10 12:03:52 +01003624 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003625
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003626 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003627 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003628
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003629 ic = &htxn->s->req;
3630 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01003631
3632 channel_abort(ic);
3633 channel_auto_close(ic);
3634 channel_erase(ic);
3635 channel_auto_read(oc);
3636 channel_auto_close(oc);
3637 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003638
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003639 return 0;
3640}
3641
3642__LJMP static int hlua_log(lua_State *L)
3643{
3644 int level;
3645 const char *msg;
3646
3647 MAY_LJMP(check_args(L, 2, "log"));
3648 level = MAY_LJMP(luaL_checkinteger(L, 1));
3649 msg = MAY_LJMP(luaL_checkstring(L, 2));
3650
3651 if (level < 0 || level >= NB_LOG_LEVELS)
3652 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3653
3654 hlua_sendlog(NULL, level, msg);
3655 return 0;
3656}
3657
3658__LJMP static int hlua_log_debug(lua_State *L)
3659{
3660 const char *msg;
3661
3662 MAY_LJMP(check_args(L, 1, "debug"));
3663 msg = MAY_LJMP(luaL_checkstring(L, 1));
3664 hlua_sendlog(NULL, LOG_DEBUG, msg);
3665 return 0;
3666}
3667
3668__LJMP static int hlua_log_info(lua_State *L)
3669{
3670 const char *msg;
3671
3672 MAY_LJMP(check_args(L, 1, "info"));
3673 msg = MAY_LJMP(luaL_checkstring(L, 1));
3674 hlua_sendlog(NULL, LOG_INFO, msg);
3675 return 0;
3676}
3677
3678__LJMP static int hlua_log_warning(lua_State *L)
3679{
3680 const char *msg;
3681
3682 MAY_LJMP(check_args(L, 1, "warning"));
3683 msg = MAY_LJMP(luaL_checkstring(L, 1));
3684 hlua_sendlog(NULL, LOG_WARNING, msg);
3685 return 0;
3686}
3687
3688__LJMP static int hlua_log_alert(lua_State *L)
3689{
3690 const char *msg;
3691
3692 MAY_LJMP(check_args(L, 1, "alert"));
3693 msg = MAY_LJMP(luaL_checkstring(L, 1));
3694 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003695 return 0;
3696}
3697
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003698__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003699{
3700 int wakeup_ms = lua_tointeger(L, -1);
3701 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003702 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003703 return 0;
3704}
3705
3706__LJMP static int hlua_sleep(lua_State *L)
3707{
3708 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003709 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003710
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003711 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003712
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003713 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003714 wakeup_ms = tick_add(now_ms, delay);
3715 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003716
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003717 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3718 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003719}
3720
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003721__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003722{
3723 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003724 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003725
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003726 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003727
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003728 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003729 wakeup_ms = tick_add(now_ms, delay);
3730 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003731
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003732 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3733 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003734}
3735
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003736/* This functionis an LUA binding. it permits to give back
3737 * the hand at the HAProxy scheduler. It is used when the
3738 * LUA processing consumes a lot of time.
3739 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003740__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003741{
3742 return 0;
3743}
3744
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003745__LJMP static int hlua_yield(lua_State *L)
3746{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003747 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
3748 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003749}
3750
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003751/* This function change the nice of the currently executed
3752 * task. It is used set low or high priority at the current
3753 * task.
3754 */
Willy Tarreau59551662015-03-10 14:23:13 +01003755__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003756{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003757 struct hlua *hlua;
3758 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003759
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003760 MAY_LJMP(check_args(L, 1, "set_nice"));
3761 hlua = hlua_gethlua(L);
3762 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003763
3764 /* If he task is not set, I'm in a start mode. */
3765 if (!hlua || !hlua->task)
3766 return 0;
3767
3768 if (nice < -1024)
3769 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003770 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003771 nice = 1024;
3772
3773 hlua->task->nice = nice;
3774 return 0;
3775}
3776
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003777/* This function is used as a calback of a task. It is called by the
3778 * HAProxy task subsystem when the task is awaked. The LUA runtime can
3779 * return an E_AGAIN signal, the emmiter of this signal must set a
3780 * signal to wake the task.
3781 */
3782static struct task *hlua_process_task(struct task *task)
3783{
3784 struct hlua *hlua = task->context;
3785 enum hlua_exec status;
3786
3787 /* We need to remove the task from the wait queue before executing
3788 * the Lua code because we don't know if it needs to wait for
3789 * another timer or not in the case of E_AGAIN.
3790 */
3791 task_delete(task);
3792
Thierry FOURNIERbd413492015-03-03 16:52:26 +01003793 /* If it is the first call to the task, we must initialize the
3794 * execution timeouts.
3795 */
3796 if (!HLUA_IS_RUNNING(hlua))
3797 hlua->expire = tick_add(now_ms, hlua_timeout_task);
3798
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003799 /* Execute the Lua code. */
3800 status = hlua_ctx_resume(hlua, 1);
3801
3802 switch (status) {
3803 /* finished or yield */
3804 case HLUA_E_OK:
3805 hlua_ctx_destroy(hlua);
3806 task_delete(task);
3807 task_free(task);
3808 break;
3809
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01003810 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
3811 if (hlua->wake_time != TICK_ETERNITY)
3812 task_schedule(task, hlua->wake_time);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003813 break;
3814
3815 /* finished with error. */
3816 case HLUA_E_ERRMSG:
3817 send_log(NULL, LOG_ERR, "Lua task: %s.", lua_tostring(hlua->T, -1));
3818 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3819 Alert("Lua task: %s.\n", lua_tostring(hlua->T, -1));
3820 hlua_ctx_destroy(hlua);
3821 task_delete(task);
3822 task_free(task);
3823 break;
3824
3825 case HLUA_E_ERR:
3826 default:
3827 send_log(NULL, LOG_ERR, "Lua task: unknown error.");
3828 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3829 Alert("Lua task: unknown error.\n");
3830 hlua_ctx_destroy(hlua);
3831 task_delete(task);
3832 task_free(task);
3833 break;
3834 }
3835 return NULL;
3836}
3837
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01003838/* This function is an LUA binding that register LUA function to be
3839 * executed after the HAProxy configuration parsing and before the
3840 * HAProxy scheduler starts. This function expect only one LUA
3841 * argument that is a function. This function returns nothing, but
3842 * throws if an error is encountered.
3843 */
3844__LJMP static int hlua_register_init(lua_State *L)
3845{
3846 struct hlua_init_function *init;
3847 int ref;
3848
3849 MAY_LJMP(check_args(L, 1, "register_init"));
3850
3851 ref = MAY_LJMP(hlua_checkfunction(L, 1));
3852
3853 init = malloc(sizeof(*init));
3854 if (!init)
3855 WILL_LJMP(luaL_error(L, "lua out of memory error."));
3856
3857 init->function_ref = ref;
3858 LIST_ADDQ(&hlua_init_functions, &init->l);
3859 return 0;
3860}
3861
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003862/* This functio is an LUA binding. It permits to register a task
3863 * executed in parallel of the main HAroxy activity. The task is
3864 * created and it is set in the HAProxy scheduler. It can be called
3865 * from the "init" section, "post init" or during the runtime.
3866 *
3867 * Lua prototype:
3868 *
3869 * <none> core.register_task(<function>)
3870 */
3871static int hlua_register_task(lua_State *L)
3872{
3873 struct hlua *hlua;
3874 struct task *task;
3875 int ref;
3876
3877 MAY_LJMP(check_args(L, 1, "register_task"));
3878
3879 ref = MAY_LJMP(hlua_checkfunction(L, 1));
3880
3881 hlua = malloc(sizeof(*hlua));
3882 if (!hlua)
3883 WILL_LJMP(luaL_error(L, "lua out of memory error."));
3884
3885 task = task_new();
3886 task->context = hlua;
3887 task->process = hlua_process_task;
3888
3889 if (!hlua_ctx_init(hlua, task))
3890 WILL_LJMP(luaL_error(L, "lua out of memory error."));
3891
3892 /* Restore the function in the stack. */
3893 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
3894 hlua->nargs = 0;
3895
3896 /* Schedule task. */
3897 task_schedule(task, now_ms);
3898
3899 return 0;
3900}
3901
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003902/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
3903 * doesn't allow "yield" functions because the HAProxy engine cannot
3904 * resume converters.
3905 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003906static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003907{
3908 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003909 struct stream *stream = smp->strm;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003910
Willy Tarreau87b09662015-04-03 00:22:06 +02003911 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01003912 * Lua context can be not initialized. This behavior
3913 * permits to save performances because a systematic
3914 * Lua initialization cause 5% performances loss.
3915 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003916 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
3917 send_log(stream->be, LOG_ERR, "Lua converter '%s': can't initialize Lua context.", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01003918 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3919 Alert("Lua converter '%s': can't initialize Lua context.\n", fcn->name);
3920 return 0;
3921 }
3922
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003923 /* If it is the first run, initialize the data for the call. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003924 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003925 /* Check stack available size. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003926 if (!lua_checkstack(stream->hlua.T, 1)) {
3927 send_log(stream->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003928 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3929 Alert("Lua converter '%s': full stack.\n", fcn->name);
3930 return 0;
3931 }
3932
3933 /* Restore the function in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003934 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003935
3936 /* convert input sample and pust-it in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003937 if (!lua_checkstack(stream->hlua.T, 1)) {
3938 send_log(stream->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003939 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3940 Alert("Lua converter '%s': full stack.\n", fcn->name);
3941 return 0;
3942 }
Willy Tarreau87b09662015-04-03 00:22:06 +02003943 hlua_smp2lua(stream->hlua.T, smp);
3944 stream->hlua.nargs = 2;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003945
3946 /* push keywords in the stack. */
3947 if (arg_p) {
3948 for (; arg_p->type != ARGT_STOP; arg_p++) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003949 if (!lua_checkstack(stream->hlua.T, 1)) {
3950 send_log(stream->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003951 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3952 Alert("Lua converter '%s': full stack.\n", fcn->name);
3953 return 0;
3954 }
Willy Tarreau87b09662015-04-03 00:22:06 +02003955 hlua_arg2lua(stream->hlua.T, arg_p);
3956 stream->hlua.nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003957 }
3958 }
3959
Thierry FOURNIERbd413492015-03-03 16:52:26 +01003960 /* We must initialize the execution timeouts. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003961 stream->hlua.expire = tick_add(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01003962
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003963 /* Set the currently running flag. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003964 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003965 }
3966
3967 /* Execute the function. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003968 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003969 /* finished. */
3970 case HLUA_E_OK:
3971 /* Convert the returned value in sample. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003972 hlua_lua2smp(stream->hlua.T, -1, smp);
3973 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003974 return 1;
3975
3976 /* yield. */
3977 case HLUA_E_AGAIN:
Willy Tarreau87b09662015-04-03 00:22:06 +02003978 send_log(stream->be, LOG_ERR, "Lua converter '%s': cannot use yielded functions.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003979 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3980 Alert("Lua converter '%s': cannot use yielded functions.\n", fcn->name);
3981 return 0;
3982
3983 /* finished with error. */
3984 case HLUA_E_ERRMSG:
3985 /* Display log. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003986 send_log(stream->be, LOG_ERR, "Lua converter '%s': %s.", fcn->name, lua_tostring(stream->hlua.T, -1));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003987 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
Willy Tarreau87b09662015-04-03 00:22:06 +02003988 Alert("Lua converter '%s': %s.\n", fcn->name, lua_tostring(stream->hlua.T, -1));
3989 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003990 return 0;
3991
3992 case HLUA_E_ERR:
3993 /* Display log. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003994 send_log(stream->be, LOG_ERR, "Lua converter '%s' returns an unknown error.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003995 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3996 Alert("Lua converter '%s' returns an unknown error.\n", fcn->name);
3997
3998 default:
3999 return 0;
4000 }
4001}
4002
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004003/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
4004 * doesn't allow "yield" functions because the HAProxy engine cannot
4005 * resume sample-fetches.
4006 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004007static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
4008 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004009{
4010 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004011 struct stream *stream = smp->strm;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004012
Willy Tarreau87b09662015-04-03 00:22:06 +02004013 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004014 * Lua context can be not initialized. This behavior
4015 * permits to save performances because a systematic
4016 * Lua initialization cause 5% performances loss.
4017 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004018 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
4019 send_log(stream->be, LOG_ERR, "Lua sample-fetch '%s': can't initialize Lua context.", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004020 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4021 Alert("Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
4022 return 0;
4023 }
4024
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004025 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004026 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004027 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004028 if (!lua_checkstack(stream->hlua.T, 2)) {
4029 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004030 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4031 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
4032 return 0;
4033 }
4034
4035 /* Restore the function in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004036 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004037
4038 /* push arguments in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004039 if (!hlua_txn_new(stream->hlua.T, stream, smp->px)) {
4040 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004041 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4042 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
4043 return 0;
4044 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004045 stream->hlua.nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004046
4047 /* push keywords in the stack. */
4048 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
4049 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004050 if (!lua_checkstack(stream->hlua.T, 1)) {
4051 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004052 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4053 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
4054 return 0;
4055 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004056 if (!lua_checkstack(stream->hlua.T, 1)) {
4057 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004058 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4059 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
4060 return 0;
4061 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004062 hlua_arg2lua(stream->hlua.T, arg_p);
4063 stream->hlua.nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004064 }
4065
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004066 /* We must initialize the execution timeouts. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004067 stream->hlua.expire = tick_add(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004068
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004069 /* Set the currently running flag. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004070 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004071 }
4072
4073 /* Execute the function. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004074 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004075 /* finished. */
4076 case HLUA_E_OK:
4077 /* Convert the returned value in sample. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004078 hlua_lua2smp(stream->hlua.T, -1, smp);
4079 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004080
4081 /* Set the end of execution flag. */
4082 smp->flags &= ~SMP_F_MAY_CHANGE;
4083 return 1;
4084
4085 /* yield. */
4086 case HLUA_E_AGAIN:
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004087 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': cannot use yielded functions.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004088 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4089 Alert("Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
4090 return 0;
4091
4092 /* finished with error. */
4093 case HLUA_E_ERRMSG:
4094 /* Display log. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004095 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': %s.", fcn->name, lua_tostring(stream->hlua.T, -1));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004096 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004097 Alert("Lua sample-fetch '%s': %s.\n", fcn->name, lua_tostring(stream->hlua.T, -1));
4098 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004099 return 0;
4100
4101 case HLUA_E_ERR:
4102 /* Display log. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004103 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s' returns an unknown error.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004104 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4105 Alert("Lua sample-fetch '%s': returns an unknown error.\n", fcn->name);
4106
4107 default:
4108 return 0;
4109 }
4110}
4111
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004112/* This function is an LUA binding used for registering
4113 * "sample-conv" functions. It expects a converter name used
4114 * in the haproxy configuration file, and an LUA function.
4115 */
4116__LJMP static int hlua_register_converters(lua_State *L)
4117{
4118 struct sample_conv_kw_list *sck;
4119 const char *name;
4120 int ref;
4121 int len;
4122 struct hlua_function *fcn;
4123
4124 MAY_LJMP(check_args(L, 2, "register_converters"));
4125
4126 /* First argument : converter name. */
4127 name = MAY_LJMP(luaL_checkstring(L, 1));
4128
4129 /* Second argument : lua function. */
4130 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4131
4132 /* Allocate and fill the sample fetch keyword struct. */
Willy Tarreau07081fe2015-04-06 10:59:20 +02004133 sck = malloc(sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004134 if (!sck)
4135 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4136 fcn = malloc(sizeof(*fcn));
4137 if (!fcn)
4138 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4139
4140 /* Fill fcn. */
4141 fcn->name = strdup(name);
4142 if (!fcn->name)
4143 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4144 fcn->function_ref = ref;
4145
4146 /* List head */
4147 sck->list.n = sck->list.p = NULL;
4148
4149 /* converter keyword. */
4150 len = strlen("lua.") + strlen(name) + 1;
4151 sck->kw[0].kw = malloc(len);
4152 if (!sck->kw[0].kw)
4153 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4154
4155 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
4156 sck->kw[0].process = hlua_sample_conv_wrapper;
4157 sck->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4158 sck->kw[0].val_args = NULL;
4159 sck->kw[0].in_type = SMP_T_STR;
4160 sck->kw[0].out_type = SMP_T_STR;
4161 sck->kw[0].private = fcn;
4162
4163 /* End of array. */
4164 memset(&sck->kw[1], 0, sizeof(struct sample_conv));
4165
4166 /* Register this new converter */
4167 sample_register_convs(sck);
4168
4169 return 0;
4170}
4171
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004172/* This fucntion is an LUA binding used for registering
4173 * "sample-fetch" functions. It expects a converter name used
4174 * in the haproxy configuration file, and an LUA function.
4175 */
4176__LJMP static int hlua_register_fetches(lua_State *L)
4177{
4178 const char *name;
4179 int ref;
4180 int len;
4181 struct sample_fetch_kw_list *sfk;
4182 struct hlua_function *fcn;
4183
4184 MAY_LJMP(check_args(L, 2, "register_fetches"));
4185
4186 /* First argument : sample-fetch name. */
4187 name = MAY_LJMP(luaL_checkstring(L, 1));
4188
4189 /* Second argument : lua function. */
4190 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4191
4192 /* Allocate and fill the sample fetch keyword struct. */
Willy Tarreau07081fe2015-04-06 10:59:20 +02004193 sfk = malloc(sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004194 if (!sfk)
4195 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4196 fcn = malloc(sizeof(*fcn));
4197 if (!fcn)
4198 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4199
4200 /* Fill fcn. */
4201 fcn->name = strdup(name);
4202 if (!fcn->name)
4203 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4204 fcn->function_ref = ref;
4205
4206 /* List head */
4207 sfk->list.n = sfk->list.p = NULL;
4208
4209 /* sample-fetch keyword. */
4210 len = strlen("lua.") + strlen(name) + 1;
4211 sfk->kw[0].kw = malloc(len);
4212 if (!sfk->kw[0].kw)
4213 return luaL_error(L, "lua out of memory error.");
4214
4215 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
4216 sfk->kw[0].process = hlua_sample_fetch_wrapper;
4217 sfk->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4218 sfk->kw[0].val_args = NULL;
4219 sfk->kw[0].out_type = SMP_T_STR;
4220 sfk->kw[0].use = SMP_USE_HTTP_ANY;
4221 sfk->kw[0].val = 0;
4222 sfk->kw[0].private = fcn;
4223
4224 /* End of array. */
4225 memset(&sfk->kw[1], 0, sizeof(struct sample_fetch));
4226
4227 /* Register this new fetch. */
4228 sample_register_fetches(sfk);
4229
4230 return 0;
4231}
4232
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004233/* global {tcp|http}-request parser. Return 1 in succes case, else return 0. */
4234static int hlua_parse_rule(const char **args, int *cur_arg, struct proxy *px,
4235 struct hlua_rule **rule_p, char **err)
4236{
4237 struct hlua_rule *rule;
4238
4239 /* Memory for the rule. */
4240 rule = malloc(sizeof(*rule));
4241 if (!rule) {
4242 memprintf(err, "out of memory error");
4243 return 0;
4244 }
4245 *rule_p = rule;
4246
4247 /* The requiered arg is a function name. */
4248 if (!args[*cur_arg]) {
4249 memprintf(err, "expect Lua function name");
4250 return 0;
4251 }
4252
4253 /* Lookup for the symbol, and check if it is a function. */
4254 lua_getglobal(gL.T, args[*cur_arg]);
4255 if (lua_isnil(gL.T, -1)) {
4256 lua_pop(gL.T, 1);
4257 memprintf(err, "Lua function '%s' not found", args[*cur_arg]);
4258 return 0;
4259 }
4260 if (!lua_isfunction(gL.T, -1)) {
4261 lua_pop(gL.T, 1);
4262 memprintf(err, "'%s' is not a function", args[*cur_arg]);
4263 return 0;
4264 }
4265
4266 /* Reference the Lua function and store the reference. */
4267 rule->fcn.function_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
4268 rule->fcn.name = strdup(args[*cur_arg]);
4269 if (!rule->fcn.name) {
4270 memprintf(err, "out of memory error.");
4271 return 0;
4272 }
4273 (*cur_arg)++;
4274
4275 /* TODO: later accept arguments. */
4276 rule->args = NULL;
4277
4278 return 1;
4279}
4280
4281/* This function is a wrapper to execute each LUA function declared
4282 * as an action wrapper during the initialisation period. This function
4283 * return 1 if the processing is finished (with oe without error) and
4284 * return 0 if the function must be called again because the LUA
4285 * returns a yield.
4286 */
4287static int hlua_request_act_wrapper(struct hlua_rule *rule, struct proxy *px,
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004288 struct stream *s, unsigned int analyzer)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004289{
4290 char **arg;
4291
Willy Tarreau87b09662015-04-03 00:22:06 +02004292 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004293 * Lua context can be not initialized. This behavior
4294 * permits to save performances because a systematic
4295 * Lua initialization cause 5% performances loss.
4296 */
4297 if (!s->hlua.T && !hlua_ctx_init(&s->hlua, s->task)) {
4298 send_log(px, LOG_ERR, "Lua action '%s': can't initialize Lua context.", rule->fcn.name);
4299 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4300 Alert("Lua action '%s': can't initialize Lua context.\n", rule->fcn.name);
4301 return 0;
4302 }
4303
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004304 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004305 if (!HLUA_IS_RUNNING(&s->hlua)) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004306 /* Check stack available size. */
4307 if (!lua_checkstack(s->hlua.T, 1)) {
4308 send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name);
4309 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4310 Alert("Lua function '%s': full stack.\n", rule->fcn.name);
4311 return 0;
4312 }
4313
4314 /* Restore the function in the stack. */
4315 lua_rawgeti(s->hlua.T, LUA_REGISTRYINDEX, rule->fcn.function_ref);
4316
Willy Tarreau87b09662015-04-03 00:22:06 +02004317 /* Create and and push object stream in the stack. */
Willy Tarreau15e91e12015-04-04 00:52:09 +02004318 if (!hlua_txn_new(s->hlua.T, s, px)) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004319 send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name);
4320 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4321 Alert("Lua function '%s': full stack.\n", rule->fcn.name);
4322 return 0;
4323 }
4324 s->hlua.nargs = 1;
4325
4326 /* push keywords in the stack. */
4327 for (arg = rule->args; arg && *arg; arg++) {
4328 if (!lua_checkstack(s->hlua.T, 1)) {
4329 send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name);
4330 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4331 Alert("Lua function '%s': full stack.\n", rule->fcn.name);
4332 return 0;
4333 }
4334 lua_pushstring(s->hlua.T, *arg);
4335 s->hlua.nargs++;
4336 }
4337
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004338 /* We must initialize the execution timeouts. */
4339 s->hlua.expire = tick_add(now_ms, hlua_timeout_session);
4340
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004341 /* Set the currently running flag. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004342 HLUA_SET_RUN(&s->hlua);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004343 }
4344
4345 /* Execute the function. */
4346 switch (hlua_ctx_resume(&s->hlua, 1)) {
4347 /* finished. */
4348 case HLUA_E_OK:
4349 return 1;
4350
4351 /* yield. */
4352 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004353 /* Set timeout in the required channel. */
4354 if (s->hlua.wake_time != TICK_ETERNITY) {
4355 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004356 s->req.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004357 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004358 s->res.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004359 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004360 /* Some actions can be wake up when a "write" event
4361 * is detected on a response channel. This is useful
4362 * only for actions targetted on the requests.
4363 */
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01004364 if (HLUA_IS_WAKERESWR(&s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004365 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01004366 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004367 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004368 }
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01004369 if (HLUA_IS_WAKEREQWR(&s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004370 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004371 return 0;
4372
4373 /* finished with error. */
4374 case HLUA_E_ERRMSG:
4375 /* Display log. */
4376 send_log(px, LOG_ERR, "Lua function '%s': %s.", rule->fcn.name, lua_tostring(s->hlua.T, -1));
4377 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4378 Alert("Lua function '%s': %s.\n", rule->fcn.name, lua_tostring(s->hlua.T, -1));
4379 lua_pop(s->hlua.T, 1);
4380 return 1;
4381
4382 case HLUA_E_ERR:
4383 /* Display log. */
4384 send_log(px, LOG_ERR, "Lua function '%s' return an unknown error.", rule->fcn.name);
4385 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4386 Alert("Lua function '%s' return an unknown error.\n", rule->fcn.name);
4387
4388 default:
4389 return 1;
4390 }
4391}
4392
4393/* Lua execution wrapper for "tcp-request". This function uses
4394 * "hlua_request_act_wrapper" for executing the LUA code.
4395 */
4396int hlua_tcp_req_act_wrapper(struct tcp_rule *tcp_rule, struct proxy *px,
Willy Tarreau87b09662015-04-03 00:22:06 +02004397 struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004398{
Thierry FOURNIERfbdb7752015-06-03 19:32:04 +02004399 return hlua_request_act_wrapper((struct hlua_rule *)tcp_rule->act_prm.data[0],
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004400 px, s, AN_REQ_INSPECT_FE);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004401}
4402
4403/* Lua execution wrapper for "tcp-response". This function uses
4404 * "hlua_request_act_wrapper" for executing the LUA code.
4405 */
4406int hlua_tcp_res_act_wrapper(struct tcp_rule *tcp_rule, struct proxy *px,
Willy Tarreau87b09662015-04-03 00:22:06 +02004407 struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004408{
Thierry FOURNIERfbdb7752015-06-03 19:32:04 +02004409 return hlua_request_act_wrapper((struct hlua_rule *)tcp_rule->act_prm.data[0],
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004410 px, s, AN_RES_INSPECT);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004411}
4412
4413/* Lua execution wrapper for http-request.
4414 * This function uses "hlua_request_act_wrapper" for executing
4415 * the LUA code.
4416 */
4417int hlua_http_req_act_wrapper(struct http_req_rule *rule, struct proxy *px,
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004418 struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004419{
4420 return hlua_request_act_wrapper((struct hlua_rule *)rule->arg.data, px,
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004421 s, AN_REQ_HTTP_PROCESS_FE);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004422}
4423
4424/* Lua execution wrapper for http-response.
4425 * This function uses "hlua_request_act_wrapper" for executing
4426 * the LUA code.
4427 */
4428int hlua_http_res_act_wrapper(struct http_res_rule *rule, struct proxy *px,
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004429 struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004430{
4431 return hlua_request_act_wrapper((struct hlua_rule *)rule->arg.data, px,
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004432 s, AN_RES_HTTP_PROCESS_BE);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004433}
4434
4435/* tcp-request <*> configuration wrapper. */
4436static int tcp_req_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4437 struct tcp_rule *rule, char **err)
4438{
Thierry FOURNIERfbdb7752015-06-03 19:32:04 +02004439 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->act_prm.data[0], err))
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01004440 return 0;
Thierry FOURNIER79318d72015-05-29 17:31:12 +02004441 rule->action = TCP_ACT_CUSTOM_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004442 rule->action_ptr = hlua_tcp_req_act_wrapper;
4443 return 1;
4444}
4445
4446/* tcp-response <*> configuration wrapper. */
4447static int tcp_res_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4448 struct tcp_rule *rule, char **err)
4449{
Thierry FOURNIERfbdb7752015-06-03 19:32:04 +02004450 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->act_prm.data[0], err))
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01004451 return 0;
Thierry FOURNIER79318d72015-05-29 17:31:12 +02004452 rule->action = TCP_ACT_CUSTOM_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004453 rule->action_ptr = hlua_tcp_res_act_wrapper;
4454 return 1;
4455}
4456
4457/* http-request <*> configuration wrapper. */
4458static int http_req_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4459 struct http_req_rule *rule, char **err)
4460{
4461 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->arg.data, err))
4462 return -1;
4463 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
4464 rule->action_ptr = hlua_http_req_act_wrapper;
4465 return 1;
4466}
4467
4468/* http-response <*> configuration wrapper. */
4469static int http_res_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4470 struct http_res_rule *rule, char **err)
4471{
4472 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->arg.data, err))
4473 return -1;
4474 rule->action = HTTP_RES_ACT_CUSTOM_CONT;
4475 rule->action_ptr = hlua_http_res_act_wrapper;
4476 return 1;
4477}
4478
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004479static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
4480 struct proxy *defpx, const char *file, int line,
4481 char **err, unsigned int *timeout)
4482{
4483 const char *error;
4484
4485 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
4486 if (error && *error != '\0') {
4487 memprintf(err, "%s: invalid timeout", args[0]);
4488 return -1;
4489 }
4490 return 0;
4491}
4492
4493static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
4494 struct proxy *defpx, const char *file, int line,
4495 char **err)
4496{
4497 return hlua_read_timeout(args, section_type, curpx, defpx,
4498 file, line, err, &hlua_timeout_session);
4499}
4500
4501static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
4502 struct proxy *defpx, const char *file, int line,
4503 char **err)
4504{
4505 return hlua_read_timeout(args, section_type, curpx, defpx,
4506 file, line, err, &hlua_timeout_task);
4507}
4508
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004509static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
4510 struct proxy *defpx, const char *file, int line,
4511 char **err)
4512{
4513 char *error;
4514
4515 hlua_nb_instruction = strtoll(args[1], &error, 10);
4516 if (*error != '\0') {
4517 memprintf(err, "%s: invalid number", args[0]);
4518 return -1;
4519 }
4520 return 0;
4521}
4522
Willy Tarreau32f61e22015-03-18 17:54:59 +01004523static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
4524 struct proxy *defpx, const char *file, int line,
4525 char **err)
4526{
4527 char *error;
4528
4529 if (*(args[1]) == 0) {
4530 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
4531 return -1;
4532 }
4533 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
4534 if (*error != '\0') {
4535 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
4536 return -1;
4537 }
4538 return 0;
4539}
4540
4541
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004542/* This function is called by the main configuration key "lua-load". It loads and
4543 * execute an lua file during the parsing of the HAProxy configuration file. It is
4544 * the main lua entry point.
4545 *
4546 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
4547 * occured, otherwise it returns 0.
4548 *
4549 * In some error case, LUA set an error message in top of the stack. This function
4550 * returns this error message in the HAProxy logs and pop it from the stack.
4551 */
4552static int hlua_load(char **args, int section_type, struct proxy *curpx,
4553 struct proxy *defpx, const char *file, int line,
4554 char **err)
4555{
4556 int error;
4557
4558 /* Just load and compile the file. */
4559 error = luaL_loadfile(gL.T, args[1]);
4560 if (error) {
4561 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
4562 lua_pop(gL.T, 1);
4563 return -1;
4564 }
4565
4566 /* If no syntax error where detected, execute the code. */
4567 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
4568 switch (error) {
4569 case LUA_OK:
4570 break;
4571 case LUA_ERRRUN:
4572 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
4573 lua_pop(gL.T, 1);
4574 return -1;
4575 case LUA_ERRMEM:
4576 memprintf(err, "lua out of memory error\n");
4577 return -1;
4578 case LUA_ERRERR:
4579 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
4580 lua_pop(gL.T, 1);
4581 return -1;
4582 case LUA_ERRGCMM:
4583 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
4584 lua_pop(gL.T, 1);
4585 return -1;
4586 default:
4587 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
4588 lua_pop(gL.T, 1);
4589 return -1;
4590 }
4591
4592 return 0;
4593}
4594
4595/* configuration keywords declaration */
4596static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004597 { CFG_GLOBAL, "lua-load", hlua_load },
4598 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
4599 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004600 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01004601 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004602 { 0, NULL, NULL },
4603}};
4604
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004605static struct http_req_action_kw_list http_req_kws = {"lua", { }, {
4606 { "lua", http_req_action_register_lua },
4607 { NULL, NULL }
4608}};
4609
4610static struct http_res_action_kw_list http_res_kws = {"lua", { }, {
4611 { "lua", http_res_action_register_lua },
4612 { NULL, NULL }
4613}};
4614
4615static struct tcp_action_kw_list tcp_req_cont_kws = {"lua", { }, {
4616 { "lua", tcp_req_action_register_lua },
4617 { NULL, NULL }
4618}};
4619
4620static struct tcp_action_kw_list tcp_res_cont_kws = {"lua", { }, {
4621 { "lua", tcp_res_action_register_lua },
4622 { NULL, NULL }
4623}};
4624
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004625int hlua_post_init()
4626{
4627 struct hlua_init_function *init;
4628 const char *msg;
4629 enum hlua_exec ret;
4630
4631 list_for_each_entry(init, &hlua_init_functions, l) {
4632 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
4633 ret = hlua_ctx_resume(&gL, 0);
4634 switch (ret) {
4635 case HLUA_E_OK:
4636 lua_pop(gL.T, -1);
4637 return 1;
4638 case HLUA_E_AGAIN:
4639 Alert("lua init: yield not allowed.\n");
4640 return 0;
4641 case HLUA_E_ERRMSG:
4642 msg = lua_tostring(gL.T, -1);
4643 Alert("lua init: %s.\n", msg);
4644 return 0;
4645 case HLUA_E_ERR:
4646 default:
4647 Alert("lua init: unknown runtime error.\n");
4648 return 0;
4649 }
4650 }
4651 return 1;
4652}
4653
Willy Tarreau32f61e22015-03-18 17:54:59 +01004654/* The memory allocator used by the Lua stack. <ud> is a pointer to the
4655 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
4656 * is the previously allocated size or the kind of object in case of a new
4657 * allocation. <nsize> is the requested new size.
4658 */
4659static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
4660{
4661 struct hlua_mem_allocator *zone = ud;
4662
4663 if (nsize == 0) {
4664 /* it's a free */
4665 if (ptr)
4666 zone->allocated -= osize;
4667 free(ptr);
4668 return NULL;
4669 }
4670
4671 if (!ptr) {
4672 /* it's a new allocation */
4673 if (zone->limit && zone->allocated + nsize > zone->limit)
4674 return NULL;
4675
4676 ptr = malloc(nsize);
4677 if (ptr)
4678 zone->allocated += nsize;
4679 return ptr;
4680 }
4681
4682 /* it's a realloc */
4683 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
4684 return NULL;
4685
4686 ptr = realloc(ptr, nsize);
4687 if (ptr)
4688 zone->allocated += nsize - osize;
4689 return ptr;
4690}
4691
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01004692void hlua_init(void)
4693{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004694 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004695 int idx;
4696 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004697 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004698 char *p;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004699#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004700 struct srv_kw *kw;
4701 int tmp_error;
4702 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01004703 char *args[] = { /* SSL client configuration. */
4704 "ssl",
4705 "verify",
4706 "none",
4707 "force-sslv3",
4708 NULL
4709 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004710#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004711
Willy Tarreau87b09662015-04-03 00:22:06 +02004712 /* Initialise com signals pool */
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004713 pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED);
4714
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004715 /* Register configuration keywords. */
4716 cfg_register_keywords(&cfg_kws);
4717
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004718 /* Register custom HTTP rules. */
4719 http_req_keywords_register(&http_req_kws);
4720 http_res_keywords_register(&http_res_kws);
4721 tcp_req_cont_keywords_register(&tcp_req_cont_kws);
4722 tcp_res_cont_keywords_register(&tcp_res_cont_kws);
4723
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004724 /* Init main lua stack. */
4725 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004726 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004727 LIST_INIT(&gL.com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004728 gL.T = luaL_newstate();
4729 hlua_sethlua(&gL);
4730 gL.Tref = LUA_REFNIL;
4731 gL.task = NULL;
4732
Willy Tarreau32f61e22015-03-18 17:54:59 +01004733 /* change the memory allocators to track memory usage */
4734 lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator);
4735
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004736 /* Initialise lua. */
4737 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004738
4739 /*
4740 *
4741 * Create "core" object.
4742 *
4743 */
4744
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01004745 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004746 lua_newtable(gL.T);
4747
4748 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004749 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004750 hlua_class_const_int(gL.T, log_levels[i], i);
4751
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004752 /* Register special functions. */
4753 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004754 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004755 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004756 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01004757 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01004758 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01004759 hlua_class_function(gL.T, "sleep", hlua_sleep);
4760 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01004761 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
4762 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
4763 hlua_class_function(gL.T, "set_map", hlua_set_map);
4764 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01004765 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01004766 hlua_class_function(gL.T, "log", hlua_log);
4767 hlua_class_function(gL.T, "Debug", hlua_log_debug);
4768 hlua_class_function(gL.T, "Info", hlua_log_info);
4769 hlua_class_function(gL.T, "Warning", hlua_log_warning);
4770 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004771
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004772 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004773
4774 /*
4775 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02004776 * Register class Map
4777 *
4778 */
4779
4780 /* This table entry is the object "Map" base. */
4781 lua_newtable(gL.T);
4782
4783 /* register pattern types. */
4784 for (i=0; i<PAT_MATCH_NUM; i++)
4785 hlua_class_const_int(gL.T, pat_match_names[i], i);
4786
4787 /* register constructor. */
4788 hlua_class_function(gL.T, "new", hlua_map_new);
4789
4790 /* Create and fill the metatable. */
4791 lua_newtable(gL.T);
4792
4793 /* Create and fille the __index entry. */
4794 lua_pushstring(gL.T, "__index");
4795 lua_newtable(gL.T);
4796
4797 /* Register . */
4798 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
4799 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
4800
4801 lua_settable(gL.T, -3);
4802
4803 /* Register previous table in the registry with reference and named entry. */
4804 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4805 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4806 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_MAP); /* register class session. */
4807 class_map_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4808
4809 /* Assign the metatable to the mai Map object. */
4810 lua_setmetatable(gL.T, -2);
4811
4812 /* Set a name to the table. */
4813 lua_setglobal(gL.T, "Map");
4814
4815 /*
4816 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01004817 * Register class Channel
4818 *
4819 */
4820
4821 /* Create and fill the metatable. */
4822 lua_newtable(gL.T);
4823
4824 /* Create and fille the __index entry. */
4825 lua_pushstring(gL.T, "__index");
4826 lua_newtable(gL.T);
4827
4828 /* Register . */
4829 hlua_class_function(gL.T, "get", hlua_channel_get);
4830 hlua_class_function(gL.T, "dup", hlua_channel_dup);
4831 hlua_class_function(gL.T, "getline", hlua_channel_getline);
4832 hlua_class_function(gL.T, "set", hlua_channel_set);
4833 hlua_class_function(gL.T, "append", hlua_channel_append);
4834 hlua_class_function(gL.T, "send", hlua_channel_send);
4835 hlua_class_function(gL.T, "forward", hlua_channel_forward);
4836 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
4837 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
4838
4839 lua_settable(gL.T, -3);
4840
4841 /* Register previous table in the registry with reference and named entry. */
4842 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4843 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CHANNEL); /* register class session. */
4844 class_channel_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4845
4846 /*
4847 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004848 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004849 *
4850 */
4851
4852 /* Create and fill the metatable. */
4853 lua_newtable(gL.T);
4854
4855 /* Create and fille the __index entry. */
4856 lua_pushstring(gL.T, "__index");
4857 lua_newtable(gL.T);
4858
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004859 /* Browse existing fetches and create the associated
4860 * object method.
4861 */
4862 sf = NULL;
4863 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
4864
4865 /* Dont register the keywork if the arguments check function are
4866 * not safe during the runtime.
4867 */
4868 if ((sf->val_args != NULL) &&
4869 (sf->val_args != val_payload_lv) &&
4870 (sf->val_args != val_hdr))
4871 continue;
4872
4873 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
4874 * by an underscore.
4875 */
4876 strncpy(trash.str, sf->kw, trash.size);
4877 trash.str[trash.size - 1] = '\0';
4878 for (p = trash.str; *p; p++)
4879 if (*p == '.' || *p == '-' || *p == '+')
4880 *p = '_';
4881
4882 /* Register the function. */
4883 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01004884 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004885 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
4886 lua_settable(gL.T, -3);
4887 }
4888
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004889 lua_settable(gL.T, -3);
4890
4891 /* Register previous table in the registry with reference and named entry. */
4892 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4893 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_FETCHES); /* register class session. */
4894 class_fetches_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4895
4896 /*
4897 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004898 * Register class Converters
4899 *
4900 */
4901
4902 /* Create and fill the metatable. */
4903 lua_newtable(gL.T);
4904
4905 /* Create and fill the __index entry. */
4906 lua_pushstring(gL.T, "__index");
4907 lua_newtable(gL.T);
4908
4909 /* Browse existing converters and create the associated
4910 * object method.
4911 */
4912 sc = NULL;
4913 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
4914 /* Dont register the keywork if the arguments check function are
4915 * not safe during the runtime.
4916 */
4917 if (sc->val_args != NULL)
4918 continue;
4919
4920 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
4921 * by an underscore.
4922 */
4923 strncpy(trash.str, sc->kw, trash.size);
4924 trash.str[trash.size - 1] = '\0';
4925 for (p = trash.str; *p; p++)
4926 if (*p == '.' || *p == '-' || *p == '+')
4927 *p = '_';
4928
4929 /* Register the function. */
4930 lua_pushstring(gL.T, trash.str);
4931 lua_pushlightuserdata(gL.T, sc);
4932 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
4933 lua_settable(gL.T, -3);
4934 }
4935
4936 lua_settable(gL.T, -3);
4937
4938 /* Register previous table in the registry with reference and named entry. */
4939 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4940 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CONVERTERS); /* register class session. */
4941 class_converters_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4942
4943 /*
4944 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004945 * Register class HTTP
4946 *
4947 */
4948
4949 /* Create and fill the metatable. */
4950 lua_newtable(gL.T);
4951
4952 /* Create and fille the __index entry. */
4953 lua_pushstring(gL.T, "__index");
4954 lua_newtable(gL.T);
4955
4956 /* Register Lua functions. */
4957 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
4958 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
4959 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
4960 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
4961 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
4962 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
4963 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
4964 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
4965 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
4966 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
4967
4968 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
4969 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
4970 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
4971 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
4972 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
4973 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
4974
4975 lua_settable(gL.T, -3);
4976
4977 /* Register previous table in the registry with reference and named entry. */
4978 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4979 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_HTTP); /* register class session. */
4980 class_http_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4981
4982 /*
4983 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004984 * Register class TXN
4985 *
4986 */
4987
4988 /* Create and fill the metatable. */
4989 lua_newtable(gL.T);
4990
4991 /* Create and fille the __index entry. */
4992 lua_pushstring(gL.T, "__index");
4993 lua_newtable(gL.T);
4994
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004995 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01004996 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
4997 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004998 hlua_class_function(gL.T, "set_var", hlua_set_var);
4999 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005000 hlua_class_function(gL.T, "close", hlua_txn_close);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005001 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
5002 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
5003 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005004 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
5005 hlua_class_function(gL.T, "log", hlua_txn_log);
5006 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
5007 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
5008 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
5009 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005010
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005011 lua_settable(gL.T, -3);
5012
5013 /* Register previous table in the registry with reference and named entry. */
5014 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5015 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_TXN); /* register class session. */
5016 class_txn_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005017
5018 /*
5019 *
5020 * Register class Socket
5021 *
5022 */
5023
5024 /* Create and fill the metatable. */
5025 lua_newtable(gL.T);
5026
5027 /* Create and fille the __index entry. */
5028 lua_pushstring(gL.T, "__index");
5029 lua_newtable(gL.T);
5030
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005031#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005032 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005033#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005034 hlua_class_function(gL.T, "connect", hlua_socket_connect);
5035 hlua_class_function(gL.T, "send", hlua_socket_send);
5036 hlua_class_function(gL.T, "receive", hlua_socket_receive);
5037 hlua_class_function(gL.T, "close", hlua_socket_close);
5038 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
5039 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
5040 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
5041 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
5042
5043 lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
5044
5045 /* Register the garbage collector entry. */
5046 lua_pushstring(gL.T, "__gc");
5047 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
5048 lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
5049
5050 /* Register previous table in the registry with reference and named entry. */
5051 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5052 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5053 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_SOCKET); /* register class socket. */
5054 class_socket_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class socket. */
5055
5056 /* Proxy and server configuration initialisation. */
5057 memset(&socket_proxy, 0, sizeof(socket_proxy));
5058 init_new_proxy(&socket_proxy);
5059 socket_proxy.parent = NULL;
5060 socket_proxy.last_change = now.tv_sec;
5061 socket_proxy.id = "LUA-SOCKET";
5062 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
5063 socket_proxy.maxconn = 0;
5064 socket_proxy.accept = NULL;
5065 socket_proxy.options2 |= PR_O2_INDEPSTR;
5066 socket_proxy.srv = NULL;
5067 socket_proxy.conn_retries = 0;
5068 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
5069
5070 /* Init TCP server: unchanged parameters */
5071 memset(&socket_tcp, 0, sizeof(socket_tcp));
5072 socket_tcp.next = NULL;
5073 socket_tcp.proxy = &socket_proxy;
5074 socket_tcp.obj_type = OBJ_TYPE_SERVER;
5075 LIST_INIT(&socket_tcp.actconns);
5076 LIST_INIT(&socket_tcp.pendconns);
5077 socket_tcp.state = SRV_ST_RUNNING; /* early server setup */
5078 socket_tcp.last_change = 0;
5079 socket_tcp.id = "LUA-TCP-CONN";
5080 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5081 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5082 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
5083
5084 /* XXX: Copy default parameter from default server,
5085 * but the default server is not initialized.
5086 */
5087 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
5088 socket_tcp.minconn = socket_proxy.defsrv.minconn;
5089 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
5090 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
5091 socket_tcp.onerror = socket_proxy.defsrv.onerror;
5092 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5093 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
5094 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5095 socket_tcp.uweight = socket_proxy.defsrv.iweight;
5096 socket_tcp.iweight = socket_proxy.defsrv.iweight;
5097
5098 socket_tcp.check.status = HCHK_STATUS_INI;
5099 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
5100 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
5101 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
5102 socket_tcp.check.server = &socket_tcp;
5103
5104 socket_tcp.agent.status = HCHK_STATUS_INI;
5105 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
5106 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
5107 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
5108 socket_tcp.agent.server = &socket_tcp;
5109
5110 socket_tcp.xprt = &raw_sock;
5111
5112#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005113 /* Init TCP server: unchanged parameters */
5114 memset(&socket_ssl, 0, sizeof(socket_ssl));
5115 socket_ssl.next = NULL;
5116 socket_ssl.proxy = &socket_proxy;
5117 socket_ssl.obj_type = OBJ_TYPE_SERVER;
5118 LIST_INIT(&socket_ssl.actconns);
5119 LIST_INIT(&socket_ssl.pendconns);
5120 socket_ssl.state = SRV_ST_RUNNING; /* early server setup */
5121 socket_ssl.last_change = 0;
5122 socket_ssl.id = "LUA-SSL-CONN";
5123 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5124 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5125 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
5126
5127 /* XXX: Copy default parameter from default server,
5128 * but the default server is not initialized.
5129 */
5130 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
5131 socket_ssl.minconn = socket_proxy.defsrv.minconn;
5132 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
5133 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
5134 socket_ssl.onerror = socket_proxy.defsrv.onerror;
5135 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5136 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
5137 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5138 socket_ssl.uweight = socket_proxy.defsrv.iweight;
5139 socket_ssl.iweight = socket_proxy.defsrv.iweight;
5140
5141 socket_ssl.check.status = HCHK_STATUS_INI;
5142 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
5143 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
5144 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
5145 socket_ssl.check.server = &socket_ssl;
5146
5147 socket_ssl.agent.status = HCHK_STATUS_INI;
5148 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
5149 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
5150 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
5151 socket_ssl.agent.server = &socket_ssl;
5152
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005153 socket_ssl.use_ssl = 1;
5154 socket_ssl.xprt = &ssl_sock;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005155
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005156 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005157 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
5158 /*
5159 *
5160 * If the keyword is not known, we can search in the registered
5161 * server keywords. This is usefull to configure special SSL
5162 * features like client certificates and ssl_verify.
5163 *
5164 */
5165 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
5166 if (tmp_error != 0) {
5167 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
5168 abort(); /* This must be never arrives because the command line
5169 not editable by the user. */
5170 }
5171 idx += kw->skip;
5172 }
5173 }
5174
5175 /* Initialize SSL server. */
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005176 ssl_sock_prepare_srv_ctx(&socket_ssl, &socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005177#endif
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01005178}