blob: 026dd2f3324f051cdd79ef358a84669b0a4b1826 [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
Willy Tarreau22b0a682015-06-17 19:43:49 +02001910#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001911__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
1912{
1913 static char buffer[SOCKET_INFO_MAX_LEN];
1914 int ret;
1915 int len;
1916 char *p;
1917
1918 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
1919 if (ret <= 0) {
1920 lua_pushnil(L);
1921 return 1;
1922 }
1923
1924 if (ret == AF_UNIX) {
1925 lua_pushstring(L, buffer+1);
1926 return 1;
1927 }
1928 else if (ret == AF_INET6) {
1929 buffer[0] = '[';
1930 len = strlen(buffer);
1931 buffer[len] = ']';
1932 len++;
1933 buffer[len] = ':';
1934 len++;
1935 p = buffer;
1936 }
1937 else if (ret == AF_INET) {
1938 p = buffer + 1;
1939 len = strlen(p);
1940 p[len] = ':';
1941 len++;
1942 }
1943 else {
1944 lua_pushnil(L);
1945 return 1;
1946 }
1947
1948 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
1949 lua_pushnil(L);
1950 return 1;
1951 }
1952
1953 lua_pushstring(L, p);
1954 return 1;
1955}
1956
1957/* Returns information about the peer of the connection. */
1958__LJMP static int hlua_socket_getpeername(struct lua_State *L)
1959{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001960 struct hlua_socket *socket;
1961 struct connection *conn;
1962
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001963 MAY_LJMP(check_args(L, 1, "getpeername"));
1964
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001965 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001966
1967 /* Check if the tcp object is avalaible. */
1968 if (!socket->s) {
1969 lua_pushnil(L);
1970 return 1;
1971 }
1972
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001973 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001974 if (!conn) {
1975 lua_pushnil(L);
1976 return 1;
1977 }
1978
1979 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
1980 unsigned int salen = sizeof(conn->addr.to);
1981 if (getpeername(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, &salen) == -1) {
1982 lua_pushnil(L);
1983 return 1;
1984 }
1985 conn->flags |= CO_FL_ADDR_TO_SET;
1986 }
1987
1988 return MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
1989}
1990
1991/* Returns information about my connection side. */
1992static int hlua_socket_getsockname(struct lua_State *L)
1993{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001994 struct hlua_socket *socket;
1995 struct connection *conn;
1996
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001997 MAY_LJMP(check_args(L, 1, "getsockname"));
1998
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001999 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002000
2001 /* Check if the tcp object is avalaible. */
2002 if (!socket->s) {
2003 lua_pushnil(L);
2004 return 1;
2005 }
2006
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002007 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002008 if (!conn) {
2009 lua_pushnil(L);
2010 return 1;
2011 }
2012
2013 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
2014 unsigned int salen = sizeof(conn->addr.from);
2015 if (getsockname(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, &salen) == -1) {
2016 lua_pushnil(L);
2017 return 1;
2018 }
2019 conn->flags |= CO_FL_ADDR_FROM_SET;
2020 }
2021
2022 return hlua_socket_info(L, &conn->addr.from);
2023}
2024
2025/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002026static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002027 .obj_type = OBJ_TYPE_APPLET,
2028 .name = "<LUA_TCP>",
2029 .fct = hlua_socket_handler,
2030 .release = hlua_socket_release,
2031};
2032
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002033__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002034{
2035 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2036 struct hlua *hlua = hlua_gethlua(L);
2037 struct appctx *appctx;
2038
2039 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002040 if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002041 lua_pushnil(L);
2042 lua_pushstring(L, "Can't connect");
2043 return 2;
2044 }
2045
2046 appctx = objt_appctx(socket->s->si[0].end);
2047
2048 /* Check for connection established. */
2049 if (appctx->ctx.hlua.connected) {
2050 lua_pushinteger(L, 1);
2051 return 1;
2052 }
2053
2054 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2055 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002056 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002057 return 0;
2058}
2059
2060/* This function fail or initite the connection. */
2061__LJMP static int hlua_socket_connect(struct lua_State *L)
2062{
2063 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002064 int port;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002065 const char *ip;
2066 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002067 struct hlua *hlua;
2068 struct appctx *appctx;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002069
2070 MAY_LJMP(check_args(L, 3, "connect"));
2071
2072 /* Get args. */
2073 socket = MAY_LJMP(hlua_checksocket(L, 1));
2074 ip = MAY_LJMP(luaL_checkstring(L, 2));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002075 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002076
Willy Tarreau350f4872014-11-28 14:42:25 +01002077 conn = si_alloc_conn(&socket->s->si[1], 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002078 if (!conn)
2079 WILL_LJMP(luaL_error(L, "connect: internal error"));
2080
2081 /* Parse ip address. */
2082 conn->addr.to.ss_family = AF_UNSPEC;
2083 if (!str2ip2(ip, &conn->addr.to, 0))
2084 WILL_LJMP(luaL_error(L, "connect: cannot parse ip address '%s'", ip));
2085
2086 /* Set port. */
2087 if (conn->addr.to.ss_family == AF_INET)
2088 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2089 else if (conn->addr.to.ss_family == AF_INET6)
2090 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2091
2092 /* it is important not to call the wakeup function directly but to
2093 * pass through task_wakeup(), because this one knows how to apply
2094 * priorities to tasks.
2095 */
2096 task_wakeup(socket->s->task, TASK_WOKEN_INIT);
2097
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002098 hlua = hlua_gethlua(L);
2099 appctx = objt_appctx(socket->s->si[0].end);
2100 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2101 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002102 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002103
2104 return 0;
2105}
2106
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002107#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002108__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2109{
2110 struct hlua_socket *socket;
2111
2112 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2113 socket = MAY_LJMP(hlua_checksocket(L, 1));
2114 socket->s->target = &socket_ssl.obj_type;
2115 return MAY_LJMP(hlua_socket_connect(L));
2116}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002117#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002118
2119__LJMP static int hlua_socket_setoption(struct lua_State *L)
2120{
2121 return 0;
2122}
2123
2124__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2125{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002126 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002127 int tmout;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002128
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002129 MAY_LJMP(check_args(L, 2, "settimeout"));
2130
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002131 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002132 tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002133
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002134 socket->s->req.rto = tmout;
2135 socket->s->req.wto = tmout;
2136 socket->s->res.rto = tmout;
2137 socket->s->res.wto = tmout;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002138
2139 return 0;
2140}
2141
2142__LJMP static int hlua_socket_new(lua_State *L)
2143{
2144 struct hlua_socket *socket;
2145 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002146 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002147 struct stream *strm;
Willy Tarreaud420a972015-04-06 00:39:18 +02002148 struct task *task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002149
2150 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002151 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002152 hlua_pusherror(L, "socket: full stack");
2153 goto out_fail_conf;
2154 }
2155
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002156 /* Create the object: obj[0] = userdata. */
2157 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002158 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002159 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002160 memset(socket, 0, sizeof(*socket));
2161
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002162 /* Check if the various memory pools are intialized. */
Willy Tarreau87b09662015-04-03 00:22:06 +02002163 if (!pool2_stream || !pool2_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002164 hlua_pusherror(L, "socket: uninitialized pools.");
2165 goto out_fail_conf;
2166 }
2167
Willy Tarreau87b09662015-04-03 00:22:06 +02002168 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002169 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2170 lua_setmetatable(L, -2);
2171
Willy Tarreaud420a972015-04-06 00:39:18 +02002172 /* Create the applet context */
2173 appctx = appctx_new(&update_applet);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002174 if (!appctx) {
2175 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002176 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002177 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002178
Willy Tarreaud420a972015-04-06 00:39:18 +02002179 appctx->ctx.hlua.socket = socket;
2180 appctx->ctx.hlua.connected = 0;
2181 LIST_INIT(&appctx->ctx.hlua.wake_on_write);
2182 LIST_INIT(&appctx->ctx.hlua.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002183
Willy Tarreaud420a972015-04-06 00:39:18 +02002184 /* Now create a session, task and stream for this applet */
2185 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2186 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002187 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002188 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002189 }
2190
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002191 task = task_new();
2192 if (!task) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002193 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002194 goto out_fail_task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002195 }
Willy Tarreaud420a972015-04-06 00:39:18 +02002196 task->nice = 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002197
Willy Tarreau73b65ac2015-04-08 18:26:29 +02002198 strm = stream_new(sess, task, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002199 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002200 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002201 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002202 }
2203
Willy Tarreaud420a972015-04-06 00:39:18 +02002204 /* Configure an empty Lua for the stream. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002205 socket->s = strm;
2206 strm->hlua.T = NULL;
2207 strm->hlua.Tref = LUA_REFNIL;
2208 strm->hlua.Mref = LUA_REFNIL;
2209 strm->hlua.nargs = 0;
2210 strm->hlua.flags = 0;
2211 LIST_INIT(&strm->hlua.com);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002212
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002213 /* Configure "right" stream interface. this "si" is used to connect
2214 * and retrieve data from the server. The connection is initialized
2215 * with the "struct server".
2216 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002217 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002218
2219 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002220 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2221 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002222
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002223 /* Update statistics counters. */
2224 socket_proxy.feconn++; /* beconn will be increased later */
2225 jobs++;
2226 totalconn++;
2227
2228 /* Return yield waiting for connection. */
2229 return 1;
2230
Willy Tarreaud420a972015-04-06 00:39:18 +02002231 out_fail_stream:
2232 task_free(task);
2233 out_fail_task:
Willy Tarreau11c36242015-04-04 15:54:03 +02002234 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002235 out_fail_sess:
2236 appctx_free(appctx);
2237 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002238 WILL_LJMP(lua_error(L));
2239 return 0;
2240}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002241
2242/*
2243 *
2244 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002245 * Class Channel
2246 *
2247 *
2248 */
2249
2250/* Returns the struct hlua_channel join to the class channel in the
2251 * stack entry "ud" or throws an argument error.
2252 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002253__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002254{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002255 return (struct channel *)MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002256}
2257
Willy Tarreau47860ed2015-03-10 14:07:50 +01002258/* Pushes the channel onto the top of the stack. If the stask does not have a
2259 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002260 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002261static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002262{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002263 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002264 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002265 return 0;
2266
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002267 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002268 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002269 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002270
2271 /* Pop a class sesison metatable and affect it to the userdata. */
2272 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2273 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002274 return 1;
2275}
2276
2277/* Duplicate all the data present in the input channel and put it
2278 * in a string LUA variables. Returns -1 and push a nil value in
2279 * the stack if the channel is closed and all the data are consumed,
2280 * returns 0 if no data are available, otherwise it returns the length
2281 * of the builded string.
2282 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002283static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002284{
2285 char *blk1;
2286 char *blk2;
2287 int len1;
2288 int len2;
2289 int ret;
2290 luaL_Buffer b;
2291
Willy Tarreau47860ed2015-03-10 14:07:50 +01002292 ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002293 if (unlikely(ret == 0))
2294 return 0;
2295
2296 if (unlikely(ret < 0)) {
2297 lua_pushnil(L);
2298 return -1;
2299 }
2300
2301 luaL_buffinit(L, &b);
2302 luaL_addlstring(&b, blk1, len1);
2303 if (unlikely(ret == 2))
2304 luaL_addlstring(&b, blk2, len2);
2305 luaL_pushresult(&b);
2306
2307 if (unlikely(ret == 2))
2308 return len1 + len2;
2309 return len1;
2310}
2311
2312/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2313 * a yield. This function keep the data in the buffer.
2314 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002315__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002316{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002317 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002318
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002319 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2320
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002321 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002322 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002323 return 1;
2324}
2325
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002326/* Check arguments for the function "hlua_channel_dup_yield". */
2327__LJMP static int hlua_channel_dup(lua_State *L)
2328{
2329 MAY_LJMP(check_args(L, 1, "dup"));
2330 MAY_LJMP(hlua_checkchannel(L, 1));
2331 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2332}
2333
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002334/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2335 * a yield. This function consumes the data in the buffer. It returns
2336 * a string containing the data or a nil pointer if no data are available
2337 * and the channel is closed.
2338 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002339__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002340{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002341 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002342 int ret;
2343
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002344 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002345
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002346 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002347 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002348 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002349
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002350 if (unlikely(ret == -1))
2351 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002352
Willy Tarreau47860ed2015-03-10 14:07:50 +01002353 chn->buf->i -= ret;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002354 return 1;
2355}
2356
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002357/* Check arguments for the fucntion "hlua_channel_get_yield". */
2358__LJMP static int hlua_channel_get(lua_State *L)
2359{
2360 MAY_LJMP(check_args(L, 1, "get"));
2361 MAY_LJMP(hlua_checkchannel(L, 1));
2362 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2363}
2364
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002365/* This functions consumes and returns one line. If the channel is closed,
2366 * and the last data does not contains a final '\n', the data are returned
2367 * without the final '\n'. When no more data are avalaible, it returns nil
2368 * value.
2369 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002370__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002371{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002372 char *blk1;
2373 char *blk2;
2374 int len1;
2375 int len2;
2376 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002377 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002378 int ret;
2379 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002380
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002381 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2382
Willy Tarreau47860ed2015-03-10 14:07:50 +01002383 ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002384 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002385 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002386
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002387 if (ret == -1) {
2388 lua_pushnil(L);
2389 return 1;
2390 }
2391
2392 luaL_buffinit(L, &b);
2393 luaL_addlstring(&b, blk1, len1);
2394 len = len1;
2395 if (unlikely(ret == 2)) {
2396 luaL_addlstring(&b, blk2, len2);
2397 len += len2;
2398 }
2399 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002400 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002401 return 1;
2402}
2403
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002404/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2405__LJMP static int hlua_channel_getline(lua_State *L)
2406{
2407 MAY_LJMP(check_args(L, 1, "getline"));
2408 MAY_LJMP(hlua_checkchannel(L, 1));
2409 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2410}
2411
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002412/* This function takes a string as input, and append it at the
2413 * input side of channel. If the data is too big, but a space
2414 * is probably available after sending some data, the function
2415 * yield. If the data is bigger than the buffer, or if the
2416 * channel is closed, it returns -1. otherwise, it returns the
2417 * amount of data writed.
2418 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002419__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002420{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002421 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002422 size_t len;
2423 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2424 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2425 int ret;
2426 int max;
2427
Willy Tarreau47860ed2015-03-10 14:07:50 +01002428 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002429 if (max > len - l)
2430 max = len - l;
2431
Willy Tarreau47860ed2015-03-10 14:07:50 +01002432 ret = bi_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002433 if (ret == -2 || ret == -3) {
2434 lua_pushinteger(L, -1);
2435 return 1;
2436 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002437 if (ret == -1) {
2438 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002439 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002440 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002441 l += ret;
2442 lua_pop(L, 1);
2443 lua_pushinteger(L, l);
2444
Willy Tarreau47860ed2015-03-10 14:07:50 +01002445 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2446 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002447 /* There are no space avalaible, and the output buffer is empty.
2448 * in this case, we cannot add more data, so we cannot yield,
2449 * we return the amount of copyied data.
2450 */
2451 return 1;
2452 }
2453 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002454 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002455 return 1;
2456}
2457
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002458/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002459 * of the writed string, or -1 if the channel is closed or if the
2460 * buffer size is too little for the data.
2461 */
2462__LJMP static int hlua_channel_append(lua_State *L)
2463{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002464 size_t len;
2465
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002466 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002467 MAY_LJMP(hlua_checkchannel(L, 1));
2468 MAY_LJMP(luaL_checklstring(L, 2, &len));
2469 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002470 lua_pushinteger(L, 0);
2471
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002472 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002473}
2474
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002475/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002476 * his process by cleaning the buffer. The result is a replacement
2477 * of the current data. It returns the length of the writed string,
2478 * or -1 if the channel is closed or if the buffer size is too
2479 * little for the data.
2480 */
2481__LJMP static int hlua_channel_set(lua_State *L)
2482{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002483 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002484
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002485 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002486 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002487 lua_pushinteger(L, 0);
2488
Willy Tarreau47860ed2015-03-10 14:07:50 +01002489 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002490
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002491 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002492}
2493
2494/* Append data in the output side of the buffer. This data is immediatly
2495 * sent. The fcuntion returns the ammount of data writed. If the buffer
2496 * cannot contains the data, the function yield. The function returns -1
2497 * if the channel is closed.
2498 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002499__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002500{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002501 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002502 size_t len;
2503 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2504 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2505 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002506 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002507
Willy Tarreau47860ed2015-03-10 14:07:50 +01002508 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002509 lua_pushinteger(L, -1);
2510 return 1;
2511 }
2512
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002513 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2514 * the request buffer if its not required.
2515 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002516 if (chn->buf->size == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002517 if (!stream_alloc_recv_buffer(chn)) {
Willy Tarreau47860ed2015-03-10 14:07:50 +01002518 chn_prod(chn)->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002519 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002520 }
2521 }
2522
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002523 /* the writed data will be immediatly sent, so we can check
2524 * the avalaible space without taking in account the reserve.
2525 * The reserve is guaranted for the processing of incoming
2526 * data, because the buffer will be flushed.
2527 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002528 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002529
2530 /* If there are no space avalaible, and the output buffer is empty.
2531 * in this case, we cannot add more data, so we cannot yield,
2532 * we return the amount of copyied data.
2533 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002534 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002535 return 1;
2536
2537 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002538 if (max > len - l)
2539 max = len - l;
2540
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002541 /* The buffer avalaible size may be not contiguous. This test
2542 * detects a non contiguous buffer and realign it.
2543 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002544 if (bi_space_for_replace(chn->buf) < max)
2545 buffer_slow_realign(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002546
2547 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002548 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002549
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002550 /* buffer replace considers that the input part is filled.
2551 * so, I must forward these new data in the output part.
2552 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002553 b_adv(chn->buf, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002554
2555 l += max;
2556 lua_pop(L, 1);
2557 lua_pushinteger(L, l);
2558
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002559 /* If there are no space avalaible, and the output buffer is empty.
2560 * in this case, we cannot add more data, so we cannot yield,
2561 * we return the amount of copyied data.
2562 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002563 max = chn->buf->size - buffer_len(chn->buf);
2564 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002565 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002566
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002567 if (l < len) {
2568 /* If we are waiting for space in the response buffer, we
2569 * must set the flag WAKERESWR. This flag required the task
2570 * wake up if any activity is detected on the response buffer.
2571 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002572 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002573 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002574 else
2575 HLUA_SET_WAKEREQWR(hlua);
2576 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002577 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002578
2579 return 1;
2580}
2581
2582/* Just a wraper of "_hlua_channel_send". This wrapper permits
2583 * yield the LUA process, and resume it without checking the
2584 * input arguments.
2585 */
2586__LJMP static int hlua_channel_send(lua_State *L)
2587{
2588 MAY_LJMP(check_args(L, 2, "send"));
2589 lua_pushinteger(L, 0);
2590
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002591 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002592}
2593
2594/* This function forward and amount of butes. The data pass from
2595 * the input side of the buffer to the output side, and can be
2596 * forwarded. This function never fails.
2597 *
2598 * The Lua function takes an amount of bytes to be forwarded in
2599 * imput. It returns the number of bytes forwarded.
2600 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002601__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002602{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002603 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002604 int len;
2605 int l;
2606 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002607 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002608
2609 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2610 len = MAY_LJMP(luaL_checkinteger(L, 2));
2611 l = MAY_LJMP(luaL_checkinteger(L, -1));
2612
2613 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002614 if (max > chn->buf->i)
2615 max = chn->buf->i;
2616 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002617 l += max;
2618
2619 lua_pop(L, 1);
2620 lua_pushinteger(L, l);
2621
2622 /* Check if it miss bytes to forward. */
2623 if (l < len) {
2624 /* The the input channel or the output channel are closed, we
2625 * must return the amount of data forwarded.
2626 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002627 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002628 return 1;
2629
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002630 /* If we are waiting for space data in the response buffer, we
2631 * must set the flag WAKERESWR. This flag required the task
2632 * wake up if any activity is detected on the response buffer.
2633 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002634 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002635 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002636 else
2637 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002638
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002639 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002640 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002641 }
2642
2643 return 1;
2644}
2645
2646/* Just check the input and prepare the stack for the previous
2647 * function "hlua_channel_forward_yield"
2648 */
2649__LJMP static int hlua_channel_forward(lua_State *L)
2650{
2651 MAY_LJMP(check_args(L, 2, "forward"));
2652 MAY_LJMP(hlua_checkchannel(L, 1));
2653 MAY_LJMP(luaL_checkinteger(L, 2));
2654
2655 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002656 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002657}
2658
2659/* Just returns the number of bytes available in the input
2660 * side of the buffer. This function never fails.
2661 */
2662__LJMP static int hlua_channel_get_in_len(lua_State *L)
2663{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002664 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002665
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002666 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002667 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002668 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002669 return 1;
2670}
2671
2672/* Just returns the number of bytes available in the output
2673 * side of the buffer. This function never fails.
2674 */
2675__LJMP static int hlua_channel_get_out_len(lua_State *L)
2676{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002677 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002678
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002679 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002680 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002681 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002682 return 1;
2683}
2684
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002685/*
2686 *
2687 *
2688 * Class Fetches
2689 *
2690 *
2691 */
2692
2693/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002694 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002695 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002696__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002697{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002698 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002699}
2700
2701/* This function creates and push in the stack a fetch object according
2702 * with a current TXN.
2703 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002704static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002705{
Willy Tarreau7073c472015-04-06 11:15:40 +02002706 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002707
2708 /* Check stack size. */
2709 if (!lua_checkstack(L, 3))
2710 return 0;
2711
2712 /* Create the object: obj[0] = userdata.
2713 * Note that the base of the Fetches object is the
2714 * transaction object.
2715 */
2716 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002717 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002718 lua_rawseti(L, -2, 0);
2719
Willy Tarreau7073c472015-04-06 11:15:40 +02002720 hsmp->s = txn->s;
2721 hsmp->p = txn->p;
Willy Tarreau7073c472015-04-06 11:15:40 +02002722 hsmp->stringsafe = stringsafe;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002723
2724 /* Pop a class sesison metatable and affect it to the userdata. */
2725 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
2726 lua_setmetatable(L, -2);
2727
2728 return 1;
2729}
2730
2731/* This function is an LUA binding. It is called with each sample-fetch.
2732 * It uses closure argument to store the associated sample-fetch. It
2733 * returns only one argument or throws an error. An error is thrown
2734 * only if an error is encountered during the argument parsing. If
2735 * the "sample-fetch" function fails, nil is returned.
2736 */
2737__LJMP static int hlua_run_sample_fetch(lua_State *L)
2738{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002739 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01002740 struct sample_fetch *f;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002741 struct arg args[ARGM_NBARGS + 1];
2742 int i;
2743 struct sample smp;
2744
2745 /* Get closure arguments. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002746 f = (struct sample_fetch *)lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002747
2748 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002749 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002750
2751 /* Get extra arguments. */
2752 for (i = 0; i < lua_gettop(L) - 1; i++) {
2753 if (i >= ARGM_NBARGS)
2754 break;
2755 hlua_lua2arg(L, i + 2, &args[i]);
2756 }
2757 args[i].type = ARGT_STOP;
2758
2759 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002760 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002761
2762 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002763 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002764 lua_pushfstring(L, "error in arguments");
2765 WILL_LJMP(lua_error(L));
2766 }
2767
2768 /* Initialise the sample. */
2769 memset(&smp, 0, sizeof(smp));
2770
2771 /* Run the sample fetch process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02002772 smp.px = hsmp->p;
2773 smp.sess = hsmp->s->sess;
2774 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02002775 smp.opt = 0;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002776 if (!f->process(args, &smp, f->kw, f->private)) {
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002777 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002778 lua_pushstring(L, "");
2779 else
2780 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002781 return 1;
2782 }
2783
2784 /* Convert the returned sample in lua value. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002785 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002786 hlua_smp2lua_str(L, &smp);
2787 else
2788 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002789 return 1;
2790}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002791
2792/*
2793 *
2794 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002795 * Class Converters
2796 *
2797 *
2798 */
2799
2800/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002801 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002802 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002803__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002804{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002805 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002806}
2807
2808/* This function creates and push in the stack a Converters object
2809 * according with a current TXN.
2810 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002811static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002812{
Willy Tarreau7073c472015-04-06 11:15:40 +02002813 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002814
2815 /* Check stack size. */
2816 if (!lua_checkstack(L, 3))
2817 return 0;
2818
2819 /* Create the object: obj[0] = userdata.
2820 * Note that the base of the Converters object is the
2821 * same than the TXN object.
2822 */
2823 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002824 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002825 lua_rawseti(L, -2, 0);
2826
Willy Tarreau7073c472015-04-06 11:15:40 +02002827 hsmp->s = txn->s;
2828 hsmp->p = txn->p;
Willy Tarreau7073c472015-04-06 11:15:40 +02002829 hsmp->stringsafe = stringsafe;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002830
Willy Tarreau87b09662015-04-03 00:22:06 +02002831 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002832 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
2833 lua_setmetatable(L, -2);
2834
2835 return 1;
2836}
2837
2838/* This function is an LUA binding. It is called with each converter.
2839 * It uses closure argument to store the associated converter. It
2840 * returns only one argument or throws an error. An error is thrown
2841 * only if an error is encountered during the argument parsing. If
2842 * the converter function function fails, nil is returned.
2843 */
2844__LJMP static int hlua_run_sample_conv(lua_State *L)
2845{
Willy Tarreauda5f1082015-04-06 11:17:13 +02002846 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002847 struct sample_conv *conv;
2848 struct arg args[ARGM_NBARGS + 1];
2849 int i;
2850 struct sample smp;
2851
2852 /* Get closure arguments. */
2853 conv = (struct sample_conv *)lua_touserdata(L, lua_upvalueindex(1));
2854
2855 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02002856 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002857
2858 /* Get extra arguments. */
2859 for (i = 0; i < lua_gettop(L) - 2; i++) {
2860 if (i >= ARGM_NBARGS)
2861 break;
2862 hlua_lua2arg(L, i + 3, &args[i]);
2863 }
2864 args[i].type = ARGT_STOP;
2865
2866 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02002867 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002868
2869 /* Run the special args checker. */
2870 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
2871 hlua_pusherror(L, "error in arguments");
2872 WILL_LJMP(lua_error(L));
2873 }
2874
2875 /* Initialise the sample. */
2876 if (!hlua_lua2smp(L, 2, &smp)) {
2877 hlua_pusherror(L, "error in the input argument");
2878 WILL_LJMP(lua_error(L));
2879 }
2880
2881 /* Apply expected cast. */
2882 if (!sample_casts[smp.type][conv->in_type]) {
2883 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
2884 smp_to_type[smp.type], smp_to_type[conv->in_type]);
2885 WILL_LJMP(lua_error(L));
2886 }
2887 if (sample_casts[smp.type][conv->in_type] != c_none &&
2888 !sample_casts[smp.type][conv->in_type](&smp)) {
2889 hlua_pusherror(L, "error during the input argument casting");
2890 WILL_LJMP(lua_error(L));
2891 }
2892
2893 /* Run the sample conversion process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02002894 smp.px = hsmp->p;
2895 smp.sess = hsmp->s->sess;
2896 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02002897 smp.opt = 0;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002898 if (!conv->process(args, &smp, conv->private)) {
Willy Tarreauda5f1082015-04-06 11:17:13 +02002899 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002900 lua_pushstring(L, "");
2901 else
2902 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002903 return 1;
2904 }
2905
2906 /* Convert the returned sample in lua value. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02002907 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002908 hlua_smp2lua_str(L, &smp);
2909 else
2910 hlua_smp2lua(L, &smp);
2911 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002912}
2913
2914/*
2915 *
2916 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002917 * Class HTTP
2918 *
2919 *
2920 */
2921
2922/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002923 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002924 */
2925__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
2926{
2927 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
2928}
2929
2930/* This function creates and push in the stack a HTTP object
2931 * according with a current TXN.
2932 */
2933static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
2934{
Willy Tarreau9a8ad862015-04-06 11:14:06 +02002935 struct hlua_txn *htxn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002936
2937 /* Check stack size. */
2938 if (!lua_checkstack(L, 3))
2939 return 0;
2940
2941 /* Create the object: obj[0] = userdata.
2942 * Note that the base of the Converters object is the
2943 * same than the TXN object.
2944 */
2945 lua_newtable(L);
Willy Tarreau9a8ad862015-04-06 11:14:06 +02002946 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002947 lua_rawseti(L, -2, 0);
2948
Willy Tarreau9a8ad862015-04-06 11:14:06 +02002949 htxn->s = txn->s;
2950 htxn->p = txn->p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002951
Willy Tarreau87b09662015-04-03 00:22:06 +02002952 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002953 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
2954 lua_setmetatable(L, -2);
2955
2956 return 1;
2957}
2958
2959/* This function creates ans returns an array of HTTP headers.
2960 * This function does not fails. It is used as wrapper with the
2961 * 2 following functions.
2962 */
2963__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
2964{
2965 const char *cur_ptr, *cur_next, *p;
2966 int old_idx, cur_idx;
2967 struct hdr_idx_elem *cur_hdr;
2968 const char *hn, *hv;
2969 int hnl, hvl;
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01002970 int type;
2971 const char *in;
2972 char *out;
2973 int len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002974
2975 /* Create the table. */
2976 lua_newtable(L);
2977
Willy Tarreaueee5b512015-04-03 23:46:31 +02002978 if (!htxn->s->txn)
2979 return 1;
2980
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002981 /* Build array of headers. */
2982 old_idx = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002983 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002984
2985 while (1) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002986 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002987 if (!cur_idx)
2988 break;
2989 old_idx = cur_idx;
2990
Willy Tarreaueee5b512015-04-03 23:46:31 +02002991 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002992 cur_ptr = cur_next;
2993 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
2994
2995 /* Now we have one full header at cur_ptr of len cur_hdr->len,
2996 * and the next header starts at cur_next. We'll check
2997 * this header in the list as well as against the default
2998 * rule.
2999 */
3000
3001 /* look for ': *'. */
3002 hn = cur_ptr;
3003 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
3004 if (p >= cur_ptr+cur_hdr->len)
3005 continue;
3006 hnl = p - hn;
3007 p++;
3008 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
3009 p++;
3010 if (p >= cur_ptr+cur_hdr->len)
3011 continue;
3012 hv = p;
3013 hvl = cur_ptr+cur_hdr->len-p;
3014
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003015 /* Lowercase the key. Don't check the size of trash, it have
3016 * the size of one buffer and the input data contains in one
3017 * buffer.
3018 */
3019 out = trash.str;
3020 for (in=hn; in<hn+hnl; in++, out++)
3021 *out = tolower(*in);
3022 *out = '\0';
3023
3024 /* Check for existing entry:
3025 * assume that the table is on the top of the stack, and
3026 * push the key in the stack, the function lua_gettable()
3027 * perform the lookup.
3028 */
3029 lua_pushlstring(L, trash.str, hnl);
3030 lua_gettable(L, -2);
3031 type = lua_type(L, -1);
3032
3033 switch (type) {
3034 case LUA_TNIL:
3035 /* Table not found, create it. */
3036 lua_pop(L, 1); /* remove the nil value. */
3037 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
3038 lua_newtable(L); /* create and push empty table. */
3039 lua_pushlstring(L, hv, hvl); /* push header value. */
3040 lua_rawseti(L, -2, 0); /* index header value (pop it). */
3041 lua_rawset(L, -3); /* index new table with header name (pop the values). */
3042 break;
3043
3044 case LUA_TTABLE:
3045 /* Entry found: push the value in the table. */
3046 len = lua_rawlen(L, -1);
3047 lua_pushlstring(L, hv, hvl); /* push header value. */
3048 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
3049 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
3050 break;
3051
3052 default:
3053 /* Other cases are errors. */
3054 hlua_pusherror(L, "internal error during the parsing of headers.");
3055 WILL_LJMP(lua_error(L));
3056 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003057 }
3058
3059 return 1;
3060}
3061
3062__LJMP static int hlua_http_req_get_headers(lua_State *L)
3063{
3064 struct hlua_txn *htxn;
3065
3066 MAY_LJMP(check_args(L, 1, "req_get_headers"));
3067 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3068
Willy Tarreaueee5b512015-04-03 23:46:31 +02003069 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003070}
3071
3072__LJMP static int hlua_http_res_get_headers(lua_State *L)
3073{
3074 struct hlua_txn *htxn;
3075
3076 MAY_LJMP(check_args(L, 1, "res_get_headers"));
3077 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3078
Willy Tarreaueee5b512015-04-03 23:46:31 +02003079 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003080}
3081
3082/* This function replace full header, or just a value in
3083 * the request or in the response. It is a wrapper fir the
3084 * 4 following functions.
3085 */
3086__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
3087 struct http_msg *msg, int action)
3088{
3089 size_t name_len;
3090 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3091 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
3092 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
3093 struct my_regex re;
3094
3095 if (!regex_comp(reg, &re, 1, 1, NULL))
3096 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
3097
3098 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
3099 regex_free(&re);
3100 return 0;
3101}
3102
3103__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
3104{
3105 struct hlua_txn *htxn;
3106
3107 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3108 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3109
Willy Tarreaueee5b512015-04-03 23:46:31 +02003110 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 +01003111}
3112
3113__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
3114{
3115 struct hlua_txn *htxn;
3116
3117 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
3118 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3119
Willy Tarreaueee5b512015-04-03 23:46:31 +02003120 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 +01003121}
3122
3123__LJMP static int hlua_http_req_rep_val(lua_State *L)
3124{
3125 struct hlua_txn *htxn;
3126
3127 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3128 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3129
Willy Tarreaueee5b512015-04-03 23:46:31 +02003130 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 +01003131}
3132
3133__LJMP static int hlua_http_res_rep_val(lua_State *L)
3134{
3135 struct hlua_txn *htxn;
3136
3137 MAY_LJMP(check_args(L, 4, "res_rep_val"));
3138 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3139
Willy Tarreaueee5b512015-04-03 23:46:31 +02003140 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 +01003141}
3142
3143/* This function deletes all the occurences of an header.
3144 * It is a wrapper for the 2 following functions.
3145 */
3146__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3147{
3148 size_t len;
3149 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3150 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003151 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003152
3153 ctx.idx = 0;
3154 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
3155 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3156 return 0;
3157}
3158
3159__LJMP static int hlua_http_req_del_hdr(lua_State *L)
3160{
3161 struct hlua_txn *htxn;
3162
3163 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3164 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3165
Willy Tarreaueee5b512015-04-03 23:46:31 +02003166 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003167}
3168
3169__LJMP static int hlua_http_res_del_hdr(lua_State *L)
3170{
3171 struct hlua_txn *htxn;
3172
3173 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3174 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3175
Willy Tarreaueee5b512015-04-03 23:46:31 +02003176 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003177}
3178
3179/* This function adds an header. It is a wrapper used by
3180 * the 2 following functions.
3181 */
3182__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3183{
3184 size_t name_len;
3185 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3186 size_t value_len;
3187 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
3188 char *p;
3189
3190 /* Check length. */
3191 trash.len = value_len + name_len + 2;
3192 if (trash.len > trash.size)
3193 return 0;
3194
3195 /* Creates the header string. */
3196 p = trash.str;
3197 memcpy(p, name, name_len);
3198 p += name_len;
3199 *p = ':';
3200 p++;
3201 *p = ' ';
3202 p++;
3203 memcpy(p, value, value_len);
3204
Willy Tarreaueee5b512015-04-03 23:46:31 +02003205 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003206 trash.str, trash.len) != 0);
3207
3208 return 0;
3209}
3210
3211__LJMP static int hlua_http_req_add_hdr(lua_State *L)
3212{
3213 struct hlua_txn *htxn;
3214
3215 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
3216 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3217
Willy Tarreaueee5b512015-04-03 23:46:31 +02003218 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003219}
3220
3221__LJMP static int hlua_http_res_add_hdr(lua_State *L)
3222{
3223 struct hlua_txn *htxn;
3224
3225 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
3226 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3227
Willy Tarreaueee5b512015-04-03 23:46:31 +02003228 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003229}
3230
3231static int hlua_http_req_set_hdr(lua_State *L)
3232{
3233 struct hlua_txn *htxn;
3234
3235 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
3236 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3237
Willy Tarreaueee5b512015-04-03 23:46:31 +02003238 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
3239 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003240}
3241
3242static int hlua_http_res_set_hdr(lua_State *L)
3243{
3244 struct hlua_txn *htxn;
3245
3246 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
3247 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3248
Willy Tarreaueee5b512015-04-03 23:46:31 +02003249 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
3250 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003251}
3252
3253/* This function set the method. */
3254static int hlua_http_req_set_meth(lua_State *L)
3255{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003256 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003257 size_t name_len;
3258 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003259
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003260 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003261 return 1;
3262}
3263
3264/* This function set the method. */
3265static int hlua_http_req_set_path(lua_State *L)
3266{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003267 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003268 size_t name_len;
3269 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003270 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003271 return 1;
3272}
3273
3274/* This function set the query-string. */
3275static int hlua_http_req_set_query(lua_State *L)
3276{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003277 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003278 size_t name_len;
3279 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003280
3281 /* Check length. */
3282 if (name_len > trash.size - 1) {
3283 lua_pushboolean(L, 0);
3284 return 1;
3285 }
3286
3287 /* Add the mark question as prefix. */
3288 chunk_reset(&trash);
3289 trash.str[trash.len++] = '?';
3290 memcpy(trash.str + trash.len, name, name_len);
3291 trash.len += name_len;
3292
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003293 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003294 return 1;
3295}
3296
3297/* This function set the uri. */
3298static int hlua_http_req_set_uri(lua_State *L)
3299{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003300 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003301 size_t name_len;
3302 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003303
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003304 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003305 return 1;
3306}
3307
3308/*
3309 *
3310 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003311 * Class TXN
3312 *
3313 *
3314 */
3315
3316/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003317 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003318 */
3319__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
3320{
3321 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
3322}
3323
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02003324__LJMP static int hlua_set_var(lua_State *L)
3325{
3326 struct hlua_txn *htxn;
3327 const char *name;
3328 size_t len;
3329 struct sample smp;
3330
3331 MAY_LJMP(check_args(L, 3, "set_var"));
3332
3333 /* It is useles to retrieve the stream, but this function
3334 * runs only in a stream context.
3335 */
3336 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3337 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3338
3339 /* Converts the third argument in a sample. */
3340 hlua_lua2smp(L, 3, &smp);
3341
3342 /* Store the sample in a variable. */
3343 vars_set_by_name(name, len, htxn->s, &smp);
3344 return 0;
3345}
3346
3347__LJMP static int hlua_get_var(lua_State *L)
3348{
3349 struct hlua_txn *htxn;
3350 const char *name;
3351 size_t len;
3352 struct sample smp;
3353
3354 MAY_LJMP(check_args(L, 2, "get_var"));
3355
3356 /* It is useles to retrieve the stream, but this function
3357 * runs only in a stream context.
3358 */
3359 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3360 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3361
3362 if (!vars_get_by_name(name, len, htxn->s, &smp)) {
3363 lua_pushnil(L);
3364 return 1;
3365 }
3366
3367 return hlua_smp2lua(L, &smp);
3368}
3369
Willy Tarreau59551662015-03-10 14:23:13 +01003370__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003371{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003372 struct hlua *hlua;
3373
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003374 MAY_LJMP(check_args(L, 2, "set_priv"));
3375
Willy Tarreau87b09662015-04-03 00:22:06 +02003376 /* It is useles to retrieve the stream, but this function
3377 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003378 */
3379 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003380 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003381
3382 /* Remove previous value. */
3383 if (hlua->Mref != -1)
3384 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
3385
3386 /* Get and store new value. */
3387 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3388 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3389
3390 return 0;
3391}
3392
Willy Tarreau59551662015-03-10 14:23:13 +01003393__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003394{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003395 struct hlua *hlua;
3396
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003397 MAY_LJMP(check_args(L, 1, "get_priv"));
3398
Willy Tarreau87b09662015-04-03 00:22:06 +02003399 /* It is useles to retrieve the stream, but this function
3400 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003401 */
3402 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003403 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003404
3405 /* Push configuration index in the stack. */
3406 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3407
3408 return 1;
3409}
3410
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003411/* Create stack entry containing a class TXN. This function
3412 * return 0 if the stack does not contains free slots,
3413 * otherwise it returns 1.
3414 */
Willy Tarreau15e91e12015-04-04 00:52:09 +02003415static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003416{
Willy Tarreaude491382015-04-06 11:04:28 +02003417 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003418
3419 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003420 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003421 return 0;
3422
3423 /* NOTE: The allocation never fails. The failure
3424 * throw an error, and the function never returns.
3425 * if the throw is not avalaible, the process is aborted.
3426 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003427 /* Create the object: obj[0] = userdata. */
3428 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02003429 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003430 lua_rawseti(L, -2, 0);
3431
Willy Tarreaude491382015-04-06 11:04:28 +02003432 htxn->s = s;
3433 htxn->p = p;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003434
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003435 /* Create the "f" field that contains a list of fetches. */
3436 lua_pushstring(L, "f");
Willy Tarreaude491382015-04-06 11:04:28 +02003437 if (!hlua_fetches_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003438 return 0;
3439 lua_settable(L, -3);
3440
3441 /* Create the "sf" field that contains a list of stringsafe fetches. */
3442 lua_pushstring(L, "sf");
Willy Tarreaude491382015-04-06 11:04:28 +02003443 if (!hlua_fetches_new(L, htxn, 1))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003444 return 0;
3445 lua_settable(L, -3);
3446
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003447 /* Create the "c" field that contains a list of converters. */
3448 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02003449 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003450 return 0;
3451 lua_settable(L, -3);
3452
3453 /* Create the "sc" field that contains a list of stringsafe converters. */
3454 lua_pushstring(L, "sc");
Willy Tarreaude491382015-04-06 11:04:28 +02003455 if (!hlua_converters_new(L, htxn, 1))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003456 return 0;
3457 lua_settable(L, -3);
3458
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003459 /* Create the "req" field that contains the request channel object. */
3460 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003461 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003462 return 0;
3463 lua_settable(L, -3);
3464
3465 /* Create the "res" field that contains the response channel object. */
3466 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003467 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003468 return 0;
3469 lua_settable(L, -3);
3470
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003471 /* Creates the HTTP object is the current proxy allows http. */
3472 lua_pushstring(L, "http");
3473 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02003474 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003475 return 0;
3476 }
3477 else
3478 lua_pushnil(L);
3479 lua_settable(L, -3);
3480
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003481 /* Pop a class sesison metatable and affect it to the userdata. */
3482 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
3483 lua_setmetatable(L, -2);
3484
3485 return 1;
3486}
3487
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003488__LJMP static int hlua_txn_deflog(lua_State *L)
3489{
3490 const char *msg;
3491 struct hlua_txn *htxn;
3492
3493 MAY_LJMP(check_args(L, 2, "deflog"));
3494 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3495 msg = MAY_LJMP(luaL_checkstring(L, 2));
3496
3497 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
3498 return 0;
3499}
3500
3501__LJMP static int hlua_txn_log(lua_State *L)
3502{
3503 int level;
3504 const char *msg;
3505 struct hlua_txn *htxn;
3506
3507 MAY_LJMP(check_args(L, 3, "log"));
3508 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3509 level = MAY_LJMP(luaL_checkinteger(L, 2));
3510 msg = MAY_LJMP(luaL_checkstring(L, 3));
3511
3512 if (level < 0 || level >= NB_LOG_LEVELS)
3513 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3514
3515 hlua_sendlog(htxn->s->be, level, msg);
3516 return 0;
3517}
3518
3519__LJMP static int hlua_txn_log_debug(lua_State *L)
3520{
3521 const char *msg;
3522 struct hlua_txn *htxn;
3523
3524 MAY_LJMP(check_args(L, 2, "Debug"));
3525 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3526 msg = MAY_LJMP(luaL_checkstring(L, 2));
3527 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
3528 return 0;
3529}
3530
3531__LJMP static int hlua_txn_log_info(lua_State *L)
3532{
3533 const char *msg;
3534 struct hlua_txn *htxn;
3535
3536 MAY_LJMP(check_args(L, 2, "Info"));
3537 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3538 msg = MAY_LJMP(luaL_checkstring(L, 2));
3539 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
3540 return 0;
3541}
3542
3543__LJMP static int hlua_txn_log_warning(lua_State *L)
3544{
3545 const char *msg;
3546 struct hlua_txn *htxn;
3547
3548 MAY_LJMP(check_args(L, 2, "Warning"));
3549 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3550 msg = MAY_LJMP(luaL_checkstring(L, 2));
3551 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
3552 return 0;
3553}
3554
3555__LJMP static int hlua_txn_log_alert(lua_State *L)
3556{
3557 const char *msg;
3558 struct hlua_txn *htxn;
3559
3560 MAY_LJMP(check_args(L, 2, "Alert"));
3561 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3562 msg = MAY_LJMP(luaL_checkstring(L, 2));
3563 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
3564 return 0;
3565}
3566
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003567__LJMP static int hlua_txn_set_loglevel(lua_State *L)
3568{
3569 struct hlua_txn *htxn;
3570 int ll;
3571
3572 MAY_LJMP(check_args(L, 2, "set_loglevel"));
3573 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3574 ll = MAY_LJMP(luaL_checkinteger(L, 2));
3575
3576 if (ll < 0 || ll > 7)
3577 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
3578
3579 htxn->s->logs.level = ll;
3580 return 0;
3581}
3582
3583__LJMP static int hlua_txn_set_tos(lua_State *L)
3584{
3585 struct hlua_txn *htxn;
3586 struct connection *cli_conn;
3587 int tos;
3588
3589 MAY_LJMP(check_args(L, 2, "set_tos"));
3590 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3591 tos = MAY_LJMP(luaL_checkinteger(L, 2));
3592
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003593 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003594 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, tos);
3595
3596 return 0;
3597}
3598
3599__LJMP static int hlua_txn_set_mark(lua_State *L)
3600{
3601#ifdef SO_MARK
3602 struct hlua_txn *htxn;
3603 struct connection *cli_conn;
3604 int mark;
3605
3606 MAY_LJMP(check_args(L, 2, "set_mark"));
3607 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3608 mark = MAY_LJMP(luaL_checkinteger(L, 2));
3609
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003610 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau07081fe2015-04-06 10:59:20 +02003611 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003612#endif
3613 return 0;
3614}
3615
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003616/* This function is an Lua binding that send pending data
3617 * to the client, and close the stream interface.
3618 */
3619__LJMP static int hlua_txn_close(lua_State *L)
3620{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003621 struct hlua_txn *htxn;
Willy Tarreau81389672015-03-10 12:03:52 +01003622 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003623
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003624 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003625 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003626
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003627 ic = &htxn->s->req;
3628 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01003629
3630 channel_abort(ic);
3631 channel_auto_close(ic);
3632 channel_erase(ic);
3633 channel_auto_read(oc);
3634 channel_auto_close(oc);
3635 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003636
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003637 return 0;
3638}
3639
3640__LJMP static int hlua_log(lua_State *L)
3641{
3642 int level;
3643 const char *msg;
3644
3645 MAY_LJMP(check_args(L, 2, "log"));
3646 level = MAY_LJMP(luaL_checkinteger(L, 1));
3647 msg = MAY_LJMP(luaL_checkstring(L, 2));
3648
3649 if (level < 0 || level >= NB_LOG_LEVELS)
3650 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3651
3652 hlua_sendlog(NULL, level, msg);
3653 return 0;
3654}
3655
3656__LJMP static int hlua_log_debug(lua_State *L)
3657{
3658 const char *msg;
3659
3660 MAY_LJMP(check_args(L, 1, "debug"));
3661 msg = MAY_LJMP(luaL_checkstring(L, 1));
3662 hlua_sendlog(NULL, LOG_DEBUG, msg);
3663 return 0;
3664}
3665
3666__LJMP static int hlua_log_info(lua_State *L)
3667{
3668 const char *msg;
3669
3670 MAY_LJMP(check_args(L, 1, "info"));
3671 msg = MAY_LJMP(luaL_checkstring(L, 1));
3672 hlua_sendlog(NULL, LOG_INFO, msg);
3673 return 0;
3674}
3675
3676__LJMP static int hlua_log_warning(lua_State *L)
3677{
3678 const char *msg;
3679
3680 MAY_LJMP(check_args(L, 1, "warning"));
3681 msg = MAY_LJMP(luaL_checkstring(L, 1));
3682 hlua_sendlog(NULL, LOG_WARNING, msg);
3683 return 0;
3684}
3685
3686__LJMP static int hlua_log_alert(lua_State *L)
3687{
3688 const char *msg;
3689
3690 MAY_LJMP(check_args(L, 1, "alert"));
3691 msg = MAY_LJMP(luaL_checkstring(L, 1));
3692 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003693 return 0;
3694}
3695
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003696__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003697{
3698 int wakeup_ms = lua_tointeger(L, -1);
3699 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003700 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003701 return 0;
3702}
3703
3704__LJMP static int hlua_sleep(lua_State *L)
3705{
3706 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003707 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003708
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003709 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003710
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003711 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003712 wakeup_ms = tick_add(now_ms, delay);
3713 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003714
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003715 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3716 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003717}
3718
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003719__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003720{
3721 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003722 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003723
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003724 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003725
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003726 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003727 wakeup_ms = tick_add(now_ms, delay);
3728 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003729
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003730 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3731 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003732}
3733
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003734/* This functionis an LUA binding. it permits to give back
3735 * the hand at the HAProxy scheduler. It is used when the
3736 * LUA processing consumes a lot of time.
3737 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003738__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003739{
3740 return 0;
3741}
3742
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003743__LJMP static int hlua_yield(lua_State *L)
3744{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003745 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
3746 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003747}
3748
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003749/* This function change the nice of the currently executed
3750 * task. It is used set low or high priority at the current
3751 * task.
3752 */
Willy Tarreau59551662015-03-10 14:23:13 +01003753__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003754{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003755 struct hlua *hlua;
3756 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003757
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003758 MAY_LJMP(check_args(L, 1, "set_nice"));
3759 hlua = hlua_gethlua(L);
3760 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003761
3762 /* If he task is not set, I'm in a start mode. */
3763 if (!hlua || !hlua->task)
3764 return 0;
3765
3766 if (nice < -1024)
3767 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003768 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003769 nice = 1024;
3770
3771 hlua->task->nice = nice;
3772 return 0;
3773}
3774
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003775/* This function is used as a calback of a task. It is called by the
3776 * HAProxy task subsystem when the task is awaked. The LUA runtime can
3777 * return an E_AGAIN signal, the emmiter of this signal must set a
3778 * signal to wake the task.
3779 */
3780static struct task *hlua_process_task(struct task *task)
3781{
3782 struct hlua *hlua = task->context;
3783 enum hlua_exec status;
3784
3785 /* We need to remove the task from the wait queue before executing
3786 * the Lua code because we don't know if it needs to wait for
3787 * another timer or not in the case of E_AGAIN.
3788 */
3789 task_delete(task);
3790
Thierry FOURNIERbd413492015-03-03 16:52:26 +01003791 /* If it is the first call to the task, we must initialize the
3792 * execution timeouts.
3793 */
3794 if (!HLUA_IS_RUNNING(hlua))
3795 hlua->expire = tick_add(now_ms, hlua_timeout_task);
3796
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003797 /* Execute the Lua code. */
3798 status = hlua_ctx_resume(hlua, 1);
3799
3800 switch (status) {
3801 /* finished or yield */
3802 case HLUA_E_OK:
3803 hlua_ctx_destroy(hlua);
3804 task_delete(task);
3805 task_free(task);
3806 break;
3807
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01003808 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
3809 if (hlua->wake_time != TICK_ETERNITY)
3810 task_schedule(task, hlua->wake_time);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003811 break;
3812
3813 /* finished with error. */
3814 case HLUA_E_ERRMSG:
3815 send_log(NULL, LOG_ERR, "Lua task: %s.", lua_tostring(hlua->T, -1));
3816 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3817 Alert("Lua task: %s.\n", lua_tostring(hlua->T, -1));
3818 hlua_ctx_destroy(hlua);
3819 task_delete(task);
3820 task_free(task);
3821 break;
3822
3823 case HLUA_E_ERR:
3824 default:
3825 send_log(NULL, LOG_ERR, "Lua task: unknown error.");
3826 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3827 Alert("Lua task: unknown error.\n");
3828 hlua_ctx_destroy(hlua);
3829 task_delete(task);
3830 task_free(task);
3831 break;
3832 }
3833 return NULL;
3834}
3835
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01003836/* This function is an LUA binding that register LUA function to be
3837 * executed after the HAProxy configuration parsing and before the
3838 * HAProxy scheduler starts. This function expect only one LUA
3839 * argument that is a function. This function returns nothing, but
3840 * throws if an error is encountered.
3841 */
3842__LJMP static int hlua_register_init(lua_State *L)
3843{
3844 struct hlua_init_function *init;
3845 int ref;
3846
3847 MAY_LJMP(check_args(L, 1, "register_init"));
3848
3849 ref = MAY_LJMP(hlua_checkfunction(L, 1));
3850
3851 init = malloc(sizeof(*init));
3852 if (!init)
3853 WILL_LJMP(luaL_error(L, "lua out of memory error."));
3854
3855 init->function_ref = ref;
3856 LIST_ADDQ(&hlua_init_functions, &init->l);
3857 return 0;
3858}
3859
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003860/* This functio is an LUA binding. It permits to register a task
3861 * executed in parallel of the main HAroxy activity. The task is
3862 * created and it is set in the HAProxy scheduler. It can be called
3863 * from the "init" section, "post init" or during the runtime.
3864 *
3865 * Lua prototype:
3866 *
3867 * <none> core.register_task(<function>)
3868 */
3869static int hlua_register_task(lua_State *L)
3870{
3871 struct hlua *hlua;
3872 struct task *task;
3873 int ref;
3874
3875 MAY_LJMP(check_args(L, 1, "register_task"));
3876
3877 ref = MAY_LJMP(hlua_checkfunction(L, 1));
3878
3879 hlua = malloc(sizeof(*hlua));
3880 if (!hlua)
3881 WILL_LJMP(luaL_error(L, "lua out of memory error."));
3882
3883 task = task_new();
3884 task->context = hlua;
3885 task->process = hlua_process_task;
3886
3887 if (!hlua_ctx_init(hlua, task))
3888 WILL_LJMP(luaL_error(L, "lua out of memory error."));
3889
3890 /* Restore the function in the stack. */
3891 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
3892 hlua->nargs = 0;
3893
3894 /* Schedule task. */
3895 task_schedule(task, now_ms);
3896
3897 return 0;
3898}
3899
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003900/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
3901 * doesn't allow "yield" functions because the HAProxy engine cannot
3902 * resume converters.
3903 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003904static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003905{
3906 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003907 struct stream *stream = smp->strm;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003908
Willy Tarreau87b09662015-04-03 00:22:06 +02003909 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01003910 * Lua context can be not initialized. This behavior
3911 * permits to save performances because a systematic
3912 * Lua initialization cause 5% performances loss.
3913 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003914 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
3915 send_log(stream->be, LOG_ERR, "Lua converter '%s': can't initialize Lua context.", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01003916 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3917 Alert("Lua converter '%s': can't initialize Lua context.\n", fcn->name);
3918 return 0;
3919 }
3920
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003921 /* If it is the first run, initialize the data for the call. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003922 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003923 /* Check stack available size. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003924 if (!lua_checkstack(stream->hlua.T, 1)) {
3925 send_log(stream->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003926 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3927 Alert("Lua converter '%s': full stack.\n", fcn->name);
3928 return 0;
3929 }
3930
3931 /* Restore the function in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003932 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003933
3934 /* convert input sample and pust-it in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003935 if (!lua_checkstack(stream->hlua.T, 1)) {
3936 send_log(stream->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003937 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3938 Alert("Lua converter '%s': full stack.\n", fcn->name);
3939 return 0;
3940 }
Willy Tarreau87b09662015-04-03 00:22:06 +02003941 hlua_smp2lua(stream->hlua.T, smp);
3942 stream->hlua.nargs = 2;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003943
3944 /* push keywords in the stack. */
3945 if (arg_p) {
3946 for (; arg_p->type != ARGT_STOP; arg_p++) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003947 if (!lua_checkstack(stream->hlua.T, 1)) {
3948 send_log(stream->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003949 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3950 Alert("Lua converter '%s': full stack.\n", fcn->name);
3951 return 0;
3952 }
Willy Tarreau87b09662015-04-03 00:22:06 +02003953 hlua_arg2lua(stream->hlua.T, arg_p);
3954 stream->hlua.nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003955 }
3956 }
3957
Thierry FOURNIERbd413492015-03-03 16:52:26 +01003958 /* We must initialize the execution timeouts. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003959 stream->hlua.expire = tick_add(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01003960
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003961 /* Set the currently running flag. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003962 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003963 }
3964
3965 /* Execute the function. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003966 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003967 /* finished. */
3968 case HLUA_E_OK:
3969 /* Convert the returned value in sample. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003970 hlua_lua2smp(stream->hlua.T, -1, smp);
3971 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003972 return 1;
3973
3974 /* yield. */
3975 case HLUA_E_AGAIN:
Willy Tarreau87b09662015-04-03 00:22:06 +02003976 send_log(stream->be, LOG_ERR, "Lua converter '%s': cannot use yielded functions.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003977 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3978 Alert("Lua converter '%s': cannot use yielded functions.\n", fcn->name);
3979 return 0;
3980
3981 /* finished with error. */
3982 case HLUA_E_ERRMSG:
3983 /* Display log. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003984 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 +01003985 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
Willy Tarreau87b09662015-04-03 00:22:06 +02003986 Alert("Lua converter '%s': %s.\n", fcn->name, lua_tostring(stream->hlua.T, -1));
3987 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003988 return 0;
3989
3990 case HLUA_E_ERR:
3991 /* Display log. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003992 send_log(stream->be, LOG_ERR, "Lua converter '%s' returns an unknown error.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003993 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3994 Alert("Lua converter '%s' returns an unknown error.\n", fcn->name);
3995
3996 default:
3997 return 0;
3998 }
3999}
4000
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004001/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
4002 * doesn't allow "yield" functions because the HAProxy engine cannot
4003 * resume sample-fetches.
4004 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02004005static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
4006 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004007{
4008 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004009 struct stream *stream = smp->strm;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004010
Willy Tarreau87b09662015-04-03 00:22:06 +02004011 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004012 * Lua context can be not initialized. This behavior
4013 * permits to save performances because a systematic
4014 * Lua initialization cause 5% performances loss.
4015 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004016 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
4017 send_log(stream->be, LOG_ERR, "Lua sample-fetch '%s': can't initialize Lua context.", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004018 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4019 Alert("Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
4020 return 0;
4021 }
4022
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004023 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004024 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004025 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004026 if (!lua_checkstack(stream->hlua.T, 2)) {
4027 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004028 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4029 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
4030 return 0;
4031 }
4032
4033 /* Restore the function in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004034 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004035
4036 /* push arguments in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004037 if (!hlua_txn_new(stream->hlua.T, stream, smp->px)) {
4038 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004039 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4040 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
4041 return 0;
4042 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004043 stream->hlua.nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004044
4045 /* push keywords in the stack. */
4046 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
4047 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004048 if (!lua_checkstack(stream->hlua.T, 1)) {
4049 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004050 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4051 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
4052 return 0;
4053 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004054 if (!lua_checkstack(stream->hlua.T, 1)) {
4055 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004056 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4057 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
4058 return 0;
4059 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004060 hlua_arg2lua(stream->hlua.T, arg_p);
4061 stream->hlua.nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004062 }
4063
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004064 /* We must initialize the execution timeouts. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004065 stream->hlua.expire = tick_add(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004066
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004067 /* Set the currently running flag. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004068 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004069 }
4070
4071 /* Execute the function. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004072 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004073 /* finished. */
4074 case HLUA_E_OK:
4075 /* Convert the returned value in sample. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004076 hlua_lua2smp(stream->hlua.T, -1, smp);
4077 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004078
4079 /* Set the end of execution flag. */
4080 smp->flags &= ~SMP_F_MAY_CHANGE;
4081 return 1;
4082
4083 /* yield. */
4084 case HLUA_E_AGAIN:
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004085 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': cannot use yielded functions.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004086 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4087 Alert("Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
4088 return 0;
4089
4090 /* finished with error. */
4091 case HLUA_E_ERRMSG:
4092 /* Display log. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004093 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 +01004094 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004095 Alert("Lua sample-fetch '%s': %s.\n", fcn->name, lua_tostring(stream->hlua.T, -1));
4096 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004097 return 0;
4098
4099 case HLUA_E_ERR:
4100 /* Display log. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004101 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s' returns an unknown error.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004102 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4103 Alert("Lua sample-fetch '%s': returns an unknown error.\n", fcn->name);
4104
4105 default:
4106 return 0;
4107 }
4108}
4109
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004110/* This function is an LUA binding used for registering
4111 * "sample-conv" functions. It expects a converter name used
4112 * in the haproxy configuration file, and an LUA function.
4113 */
4114__LJMP static int hlua_register_converters(lua_State *L)
4115{
4116 struct sample_conv_kw_list *sck;
4117 const char *name;
4118 int ref;
4119 int len;
4120 struct hlua_function *fcn;
4121
4122 MAY_LJMP(check_args(L, 2, "register_converters"));
4123
4124 /* First argument : converter name. */
4125 name = MAY_LJMP(luaL_checkstring(L, 1));
4126
4127 /* Second argument : lua function. */
4128 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4129
4130 /* Allocate and fill the sample fetch keyword struct. */
Willy Tarreau07081fe2015-04-06 10:59:20 +02004131 sck = malloc(sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004132 if (!sck)
4133 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4134 fcn = malloc(sizeof(*fcn));
4135 if (!fcn)
4136 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4137
4138 /* Fill fcn. */
4139 fcn->name = strdup(name);
4140 if (!fcn->name)
4141 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4142 fcn->function_ref = ref;
4143
4144 /* List head */
4145 sck->list.n = sck->list.p = NULL;
4146
4147 /* converter keyword. */
4148 len = strlen("lua.") + strlen(name) + 1;
4149 sck->kw[0].kw = malloc(len);
4150 if (!sck->kw[0].kw)
4151 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4152
4153 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
4154 sck->kw[0].process = hlua_sample_conv_wrapper;
4155 sck->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4156 sck->kw[0].val_args = NULL;
4157 sck->kw[0].in_type = SMP_T_STR;
4158 sck->kw[0].out_type = SMP_T_STR;
4159 sck->kw[0].private = fcn;
4160
4161 /* End of array. */
4162 memset(&sck->kw[1], 0, sizeof(struct sample_conv));
4163
4164 /* Register this new converter */
4165 sample_register_convs(sck);
4166
4167 return 0;
4168}
4169
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004170/* This fucntion is an LUA binding used for registering
4171 * "sample-fetch" functions. It expects a converter name used
4172 * in the haproxy configuration file, and an LUA function.
4173 */
4174__LJMP static int hlua_register_fetches(lua_State *L)
4175{
4176 const char *name;
4177 int ref;
4178 int len;
4179 struct sample_fetch_kw_list *sfk;
4180 struct hlua_function *fcn;
4181
4182 MAY_LJMP(check_args(L, 2, "register_fetches"));
4183
4184 /* First argument : sample-fetch name. */
4185 name = MAY_LJMP(luaL_checkstring(L, 1));
4186
4187 /* Second argument : lua function. */
4188 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4189
4190 /* Allocate and fill the sample fetch keyword struct. */
Willy Tarreau07081fe2015-04-06 10:59:20 +02004191 sfk = malloc(sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004192 if (!sfk)
4193 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4194 fcn = malloc(sizeof(*fcn));
4195 if (!fcn)
4196 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4197
4198 /* Fill fcn. */
4199 fcn->name = strdup(name);
4200 if (!fcn->name)
4201 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4202 fcn->function_ref = ref;
4203
4204 /* List head */
4205 sfk->list.n = sfk->list.p = NULL;
4206
4207 /* sample-fetch keyword. */
4208 len = strlen("lua.") + strlen(name) + 1;
4209 sfk->kw[0].kw = malloc(len);
4210 if (!sfk->kw[0].kw)
4211 return luaL_error(L, "lua out of memory error.");
4212
4213 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
4214 sfk->kw[0].process = hlua_sample_fetch_wrapper;
4215 sfk->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4216 sfk->kw[0].val_args = NULL;
4217 sfk->kw[0].out_type = SMP_T_STR;
4218 sfk->kw[0].use = SMP_USE_HTTP_ANY;
4219 sfk->kw[0].val = 0;
4220 sfk->kw[0].private = fcn;
4221
4222 /* End of array. */
4223 memset(&sfk->kw[1], 0, sizeof(struct sample_fetch));
4224
4225 /* Register this new fetch. */
4226 sample_register_fetches(sfk);
4227
4228 return 0;
4229}
4230
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004231/* global {tcp|http}-request parser. Return 1 in succes case, else return 0. */
4232static int hlua_parse_rule(const char **args, int *cur_arg, struct proxy *px,
4233 struct hlua_rule **rule_p, char **err)
4234{
4235 struct hlua_rule *rule;
4236
4237 /* Memory for the rule. */
4238 rule = malloc(sizeof(*rule));
4239 if (!rule) {
4240 memprintf(err, "out of memory error");
4241 return 0;
4242 }
4243 *rule_p = rule;
4244
4245 /* The requiered arg is a function name. */
4246 if (!args[*cur_arg]) {
4247 memprintf(err, "expect Lua function name");
4248 return 0;
4249 }
4250
4251 /* Lookup for the symbol, and check if it is a function. */
4252 lua_getglobal(gL.T, args[*cur_arg]);
4253 if (lua_isnil(gL.T, -1)) {
4254 lua_pop(gL.T, 1);
4255 memprintf(err, "Lua function '%s' not found", args[*cur_arg]);
4256 return 0;
4257 }
4258 if (!lua_isfunction(gL.T, -1)) {
4259 lua_pop(gL.T, 1);
4260 memprintf(err, "'%s' is not a function", args[*cur_arg]);
4261 return 0;
4262 }
4263
4264 /* Reference the Lua function and store the reference. */
4265 rule->fcn.function_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
4266 rule->fcn.name = strdup(args[*cur_arg]);
4267 if (!rule->fcn.name) {
4268 memprintf(err, "out of memory error.");
4269 return 0;
4270 }
4271 (*cur_arg)++;
4272
4273 /* TODO: later accept arguments. */
4274 rule->args = NULL;
4275
4276 return 1;
4277}
4278
4279/* This function is a wrapper to execute each LUA function declared
4280 * as an action wrapper during the initialisation period. This function
4281 * return 1 if the processing is finished (with oe without error) and
4282 * return 0 if the function must be called again because the LUA
4283 * returns a yield.
4284 */
4285static int hlua_request_act_wrapper(struct hlua_rule *rule, struct proxy *px,
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004286 struct stream *s, unsigned int analyzer)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004287{
4288 char **arg;
4289
Willy Tarreau87b09662015-04-03 00:22:06 +02004290 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004291 * Lua context can be not initialized. This behavior
4292 * permits to save performances because a systematic
4293 * Lua initialization cause 5% performances loss.
4294 */
4295 if (!s->hlua.T && !hlua_ctx_init(&s->hlua, s->task)) {
4296 send_log(px, LOG_ERR, "Lua action '%s': can't initialize Lua context.", rule->fcn.name);
4297 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4298 Alert("Lua action '%s': can't initialize Lua context.\n", rule->fcn.name);
4299 return 0;
4300 }
4301
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004302 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004303 if (!HLUA_IS_RUNNING(&s->hlua)) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004304 /* Check stack available size. */
4305 if (!lua_checkstack(s->hlua.T, 1)) {
4306 send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name);
4307 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4308 Alert("Lua function '%s': full stack.\n", rule->fcn.name);
4309 return 0;
4310 }
4311
4312 /* Restore the function in the stack. */
4313 lua_rawgeti(s->hlua.T, LUA_REGISTRYINDEX, rule->fcn.function_ref);
4314
Willy Tarreau87b09662015-04-03 00:22:06 +02004315 /* Create and and push object stream in the stack. */
Willy Tarreau15e91e12015-04-04 00:52:09 +02004316 if (!hlua_txn_new(s->hlua.T, s, px)) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004317 send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name);
4318 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4319 Alert("Lua function '%s': full stack.\n", rule->fcn.name);
4320 return 0;
4321 }
4322 s->hlua.nargs = 1;
4323
4324 /* push keywords in the stack. */
4325 for (arg = rule->args; arg && *arg; arg++) {
4326 if (!lua_checkstack(s->hlua.T, 1)) {
4327 send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name);
4328 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4329 Alert("Lua function '%s': full stack.\n", rule->fcn.name);
4330 return 0;
4331 }
4332 lua_pushstring(s->hlua.T, *arg);
4333 s->hlua.nargs++;
4334 }
4335
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004336 /* We must initialize the execution timeouts. */
4337 s->hlua.expire = tick_add(now_ms, hlua_timeout_session);
4338
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004339 /* Set the currently running flag. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004340 HLUA_SET_RUN(&s->hlua);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004341 }
4342
4343 /* Execute the function. */
4344 switch (hlua_ctx_resume(&s->hlua, 1)) {
4345 /* finished. */
4346 case HLUA_E_OK:
4347 return 1;
4348
4349 /* yield. */
4350 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004351 /* Set timeout in the required channel. */
4352 if (s->hlua.wake_time != TICK_ETERNITY) {
4353 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004354 s->req.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004355 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004356 s->res.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004357 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004358 /* Some actions can be wake up when a "write" event
4359 * is detected on a response channel. This is useful
4360 * only for actions targetted on the requests.
4361 */
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01004362 if (HLUA_IS_WAKERESWR(&s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004363 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01004364 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004365 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004366 }
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01004367 if (HLUA_IS_WAKEREQWR(&s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004368 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004369 return 0;
4370
4371 /* finished with error. */
4372 case HLUA_E_ERRMSG:
4373 /* Display log. */
4374 send_log(px, LOG_ERR, "Lua function '%s': %s.", rule->fcn.name, lua_tostring(s->hlua.T, -1));
4375 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4376 Alert("Lua function '%s': %s.\n", rule->fcn.name, lua_tostring(s->hlua.T, -1));
4377 lua_pop(s->hlua.T, 1);
4378 return 1;
4379
4380 case HLUA_E_ERR:
4381 /* Display log. */
4382 send_log(px, LOG_ERR, "Lua function '%s' return an unknown error.", rule->fcn.name);
4383 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4384 Alert("Lua function '%s' return an unknown error.\n", rule->fcn.name);
4385
4386 default:
4387 return 1;
4388 }
4389}
4390
4391/* Lua execution wrapper for "tcp-request". This function uses
4392 * "hlua_request_act_wrapper" for executing the LUA code.
4393 */
4394int hlua_tcp_req_act_wrapper(struct tcp_rule *tcp_rule, struct proxy *px,
Willy Tarreau87b09662015-04-03 00:22:06 +02004395 struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004396{
Thierry FOURNIERfbdb7752015-06-03 19:32:04 +02004397 return hlua_request_act_wrapper((struct hlua_rule *)tcp_rule->act_prm.data[0],
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004398 px, s, AN_REQ_INSPECT_FE);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004399}
4400
4401/* Lua execution wrapper for "tcp-response". This function uses
4402 * "hlua_request_act_wrapper" for executing the LUA code.
4403 */
4404int hlua_tcp_res_act_wrapper(struct tcp_rule *tcp_rule, struct proxy *px,
Willy Tarreau87b09662015-04-03 00:22:06 +02004405 struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004406{
Thierry FOURNIERfbdb7752015-06-03 19:32:04 +02004407 return hlua_request_act_wrapper((struct hlua_rule *)tcp_rule->act_prm.data[0],
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004408 px, s, AN_RES_INSPECT);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004409}
4410
4411/* Lua execution wrapper for http-request.
4412 * This function uses "hlua_request_act_wrapper" for executing
4413 * the LUA code.
4414 */
4415int hlua_http_req_act_wrapper(struct http_req_rule *rule, struct proxy *px,
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004416 struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004417{
4418 return hlua_request_act_wrapper((struct hlua_rule *)rule->arg.data, px,
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004419 s, AN_REQ_HTTP_PROCESS_FE);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004420}
4421
4422/* Lua execution wrapper for http-response.
4423 * This function uses "hlua_request_act_wrapper" for executing
4424 * the LUA code.
4425 */
4426int hlua_http_res_act_wrapper(struct http_res_rule *rule, struct proxy *px,
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004427 struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004428{
4429 return hlua_request_act_wrapper((struct hlua_rule *)rule->arg.data, px,
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004430 s, AN_RES_HTTP_PROCESS_BE);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004431}
4432
4433/* tcp-request <*> configuration wrapper. */
4434static int tcp_req_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4435 struct tcp_rule *rule, char **err)
4436{
Thierry FOURNIERfbdb7752015-06-03 19:32:04 +02004437 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->act_prm.data[0], err))
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01004438 return 0;
Thierry FOURNIER79318d72015-05-29 17:31:12 +02004439 rule->action = TCP_ACT_CUSTOM_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004440 rule->action_ptr = hlua_tcp_req_act_wrapper;
4441 return 1;
4442}
4443
4444/* tcp-response <*> configuration wrapper. */
4445static int tcp_res_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4446 struct tcp_rule *rule, char **err)
4447{
Thierry FOURNIERfbdb7752015-06-03 19:32:04 +02004448 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->act_prm.data[0], err))
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01004449 return 0;
Thierry FOURNIER79318d72015-05-29 17:31:12 +02004450 rule->action = TCP_ACT_CUSTOM_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004451 rule->action_ptr = hlua_tcp_res_act_wrapper;
4452 return 1;
4453}
4454
4455/* http-request <*> configuration wrapper. */
4456static int http_req_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4457 struct http_req_rule *rule, char **err)
4458{
4459 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->arg.data, err))
4460 return -1;
4461 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
4462 rule->action_ptr = hlua_http_req_act_wrapper;
4463 return 1;
4464}
4465
4466/* http-response <*> configuration wrapper. */
4467static int http_res_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4468 struct http_res_rule *rule, char **err)
4469{
4470 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->arg.data, err))
4471 return -1;
4472 rule->action = HTTP_RES_ACT_CUSTOM_CONT;
4473 rule->action_ptr = hlua_http_res_act_wrapper;
4474 return 1;
4475}
4476
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004477static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
4478 struct proxy *defpx, const char *file, int line,
4479 char **err, unsigned int *timeout)
4480{
4481 const char *error;
4482
4483 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
4484 if (error && *error != '\0') {
4485 memprintf(err, "%s: invalid timeout", args[0]);
4486 return -1;
4487 }
4488 return 0;
4489}
4490
4491static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
4492 struct proxy *defpx, const char *file, int line,
4493 char **err)
4494{
4495 return hlua_read_timeout(args, section_type, curpx, defpx,
4496 file, line, err, &hlua_timeout_session);
4497}
4498
4499static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
4500 struct proxy *defpx, const char *file, int line,
4501 char **err)
4502{
4503 return hlua_read_timeout(args, section_type, curpx, defpx,
4504 file, line, err, &hlua_timeout_task);
4505}
4506
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004507static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
4508 struct proxy *defpx, const char *file, int line,
4509 char **err)
4510{
4511 char *error;
4512
4513 hlua_nb_instruction = strtoll(args[1], &error, 10);
4514 if (*error != '\0') {
4515 memprintf(err, "%s: invalid number", args[0]);
4516 return -1;
4517 }
4518 return 0;
4519}
4520
Willy Tarreau32f61e22015-03-18 17:54:59 +01004521static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
4522 struct proxy *defpx, const char *file, int line,
4523 char **err)
4524{
4525 char *error;
4526
4527 if (*(args[1]) == 0) {
4528 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
4529 return -1;
4530 }
4531 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
4532 if (*error != '\0') {
4533 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
4534 return -1;
4535 }
4536 return 0;
4537}
4538
4539
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004540/* This function is called by the main configuration key "lua-load". It loads and
4541 * execute an lua file during the parsing of the HAProxy configuration file. It is
4542 * the main lua entry point.
4543 *
4544 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
4545 * occured, otherwise it returns 0.
4546 *
4547 * In some error case, LUA set an error message in top of the stack. This function
4548 * returns this error message in the HAProxy logs and pop it from the stack.
4549 */
4550static int hlua_load(char **args, int section_type, struct proxy *curpx,
4551 struct proxy *defpx, const char *file, int line,
4552 char **err)
4553{
4554 int error;
4555
4556 /* Just load and compile the file. */
4557 error = luaL_loadfile(gL.T, args[1]);
4558 if (error) {
4559 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
4560 lua_pop(gL.T, 1);
4561 return -1;
4562 }
4563
4564 /* If no syntax error where detected, execute the code. */
4565 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
4566 switch (error) {
4567 case LUA_OK:
4568 break;
4569 case LUA_ERRRUN:
4570 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
4571 lua_pop(gL.T, 1);
4572 return -1;
4573 case LUA_ERRMEM:
4574 memprintf(err, "lua out of memory error\n");
4575 return -1;
4576 case LUA_ERRERR:
4577 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
4578 lua_pop(gL.T, 1);
4579 return -1;
4580 case LUA_ERRGCMM:
4581 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
4582 lua_pop(gL.T, 1);
4583 return -1;
4584 default:
4585 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
4586 lua_pop(gL.T, 1);
4587 return -1;
4588 }
4589
4590 return 0;
4591}
4592
4593/* configuration keywords declaration */
4594static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004595 { CFG_GLOBAL, "lua-load", hlua_load },
4596 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
4597 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004598 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01004599 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004600 { 0, NULL, NULL },
4601}};
4602
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004603static struct http_req_action_kw_list http_req_kws = {"lua", { }, {
4604 { "lua", http_req_action_register_lua },
4605 { NULL, NULL }
4606}};
4607
4608static struct http_res_action_kw_list http_res_kws = {"lua", { }, {
4609 { "lua", http_res_action_register_lua },
4610 { NULL, NULL }
4611}};
4612
4613static struct tcp_action_kw_list tcp_req_cont_kws = {"lua", { }, {
4614 { "lua", tcp_req_action_register_lua },
4615 { NULL, NULL }
4616}};
4617
4618static struct tcp_action_kw_list tcp_res_cont_kws = {"lua", { }, {
4619 { "lua", tcp_res_action_register_lua },
4620 { NULL, NULL }
4621}};
4622
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004623int hlua_post_init()
4624{
4625 struct hlua_init_function *init;
4626 const char *msg;
4627 enum hlua_exec ret;
4628
4629 list_for_each_entry(init, &hlua_init_functions, l) {
4630 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
4631 ret = hlua_ctx_resume(&gL, 0);
4632 switch (ret) {
4633 case HLUA_E_OK:
4634 lua_pop(gL.T, -1);
4635 return 1;
4636 case HLUA_E_AGAIN:
4637 Alert("lua init: yield not allowed.\n");
4638 return 0;
4639 case HLUA_E_ERRMSG:
4640 msg = lua_tostring(gL.T, -1);
4641 Alert("lua init: %s.\n", msg);
4642 return 0;
4643 case HLUA_E_ERR:
4644 default:
4645 Alert("lua init: unknown runtime error.\n");
4646 return 0;
4647 }
4648 }
4649 return 1;
4650}
4651
Willy Tarreau32f61e22015-03-18 17:54:59 +01004652/* The memory allocator used by the Lua stack. <ud> is a pointer to the
4653 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
4654 * is the previously allocated size or the kind of object in case of a new
4655 * allocation. <nsize> is the requested new size.
4656 */
4657static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
4658{
4659 struct hlua_mem_allocator *zone = ud;
4660
4661 if (nsize == 0) {
4662 /* it's a free */
4663 if (ptr)
4664 zone->allocated -= osize;
4665 free(ptr);
4666 return NULL;
4667 }
4668
4669 if (!ptr) {
4670 /* it's a new allocation */
4671 if (zone->limit && zone->allocated + nsize > zone->limit)
4672 return NULL;
4673
4674 ptr = malloc(nsize);
4675 if (ptr)
4676 zone->allocated += nsize;
4677 return ptr;
4678 }
4679
4680 /* it's a realloc */
4681 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
4682 return NULL;
4683
4684 ptr = realloc(ptr, nsize);
4685 if (ptr)
4686 zone->allocated += nsize - osize;
4687 return ptr;
4688}
4689
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01004690void hlua_init(void)
4691{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004692 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004693 int idx;
4694 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004695 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004696 char *p;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004697#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004698 struct srv_kw *kw;
4699 int tmp_error;
4700 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01004701 char *args[] = { /* SSL client configuration. */
4702 "ssl",
4703 "verify",
4704 "none",
4705 "force-sslv3",
4706 NULL
4707 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004708#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004709
Willy Tarreau87b09662015-04-03 00:22:06 +02004710 /* Initialise com signals pool */
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004711 pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED);
4712
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004713 /* Register configuration keywords. */
4714 cfg_register_keywords(&cfg_kws);
4715
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004716 /* Register custom HTTP rules. */
4717 http_req_keywords_register(&http_req_kws);
4718 http_res_keywords_register(&http_res_kws);
4719 tcp_req_cont_keywords_register(&tcp_req_cont_kws);
4720 tcp_res_cont_keywords_register(&tcp_res_cont_kws);
4721
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004722 /* Init main lua stack. */
4723 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004724 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004725 LIST_INIT(&gL.com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004726 gL.T = luaL_newstate();
4727 hlua_sethlua(&gL);
4728 gL.Tref = LUA_REFNIL;
4729 gL.task = NULL;
4730
Willy Tarreau32f61e22015-03-18 17:54:59 +01004731 /* change the memory allocators to track memory usage */
4732 lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator);
4733
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004734 /* Initialise lua. */
4735 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004736
4737 /*
4738 *
4739 * Create "core" object.
4740 *
4741 */
4742
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01004743 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004744 lua_newtable(gL.T);
4745
4746 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004747 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004748 hlua_class_const_int(gL.T, log_levels[i], i);
4749
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004750 /* Register special functions. */
4751 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004752 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004753 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004754 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01004755 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01004756 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01004757 hlua_class_function(gL.T, "sleep", hlua_sleep);
4758 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01004759 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
4760 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
4761 hlua_class_function(gL.T, "set_map", hlua_set_map);
4762 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01004763 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01004764 hlua_class_function(gL.T, "log", hlua_log);
4765 hlua_class_function(gL.T, "Debug", hlua_log_debug);
4766 hlua_class_function(gL.T, "Info", hlua_log_info);
4767 hlua_class_function(gL.T, "Warning", hlua_log_warning);
4768 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004769
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004770 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004771
4772 /*
4773 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02004774 * Register class Map
4775 *
4776 */
4777
4778 /* This table entry is the object "Map" base. */
4779 lua_newtable(gL.T);
4780
4781 /* register pattern types. */
4782 for (i=0; i<PAT_MATCH_NUM; i++)
4783 hlua_class_const_int(gL.T, pat_match_names[i], i);
4784
4785 /* register constructor. */
4786 hlua_class_function(gL.T, "new", hlua_map_new);
4787
4788 /* Create and fill the metatable. */
4789 lua_newtable(gL.T);
4790
4791 /* Create and fille the __index entry. */
4792 lua_pushstring(gL.T, "__index");
4793 lua_newtable(gL.T);
4794
4795 /* Register . */
4796 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
4797 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
4798
4799 lua_settable(gL.T, -3);
4800
4801 /* Register previous table in the registry with reference and named entry. */
4802 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4803 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4804 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_MAP); /* register class session. */
4805 class_map_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4806
4807 /* Assign the metatable to the mai Map object. */
4808 lua_setmetatable(gL.T, -2);
4809
4810 /* Set a name to the table. */
4811 lua_setglobal(gL.T, "Map");
4812
4813 /*
4814 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01004815 * Register class Channel
4816 *
4817 */
4818
4819 /* Create and fill the metatable. */
4820 lua_newtable(gL.T);
4821
4822 /* Create and fille the __index entry. */
4823 lua_pushstring(gL.T, "__index");
4824 lua_newtable(gL.T);
4825
4826 /* Register . */
4827 hlua_class_function(gL.T, "get", hlua_channel_get);
4828 hlua_class_function(gL.T, "dup", hlua_channel_dup);
4829 hlua_class_function(gL.T, "getline", hlua_channel_getline);
4830 hlua_class_function(gL.T, "set", hlua_channel_set);
4831 hlua_class_function(gL.T, "append", hlua_channel_append);
4832 hlua_class_function(gL.T, "send", hlua_channel_send);
4833 hlua_class_function(gL.T, "forward", hlua_channel_forward);
4834 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
4835 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
4836
4837 lua_settable(gL.T, -3);
4838
4839 /* Register previous table in the registry with reference and named entry. */
4840 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4841 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CHANNEL); /* register class session. */
4842 class_channel_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4843
4844 /*
4845 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004846 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004847 *
4848 */
4849
4850 /* Create and fill the metatable. */
4851 lua_newtable(gL.T);
4852
4853 /* Create and fille the __index entry. */
4854 lua_pushstring(gL.T, "__index");
4855 lua_newtable(gL.T);
4856
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004857 /* Browse existing fetches and create the associated
4858 * object method.
4859 */
4860 sf = NULL;
4861 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
4862
4863 /* Dont register the keywork if the arguments check function are
4864 * not safe during the runtime.
4865 */
4866 if ((sf->val_args != NULL) &&
4867 (sf->val_args != val_payload_lv) &&
4868 (sf->val_args != val_hdr))
4869 continue;
4870
4871 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
4872 * by an underscore.
4873 */
4874 strncpy(trash.str, sf->kw, trash.size);
4875 trash.str[trash.size - 1] = '\0';
4876 for (p = trash.str; *p; p++)
4877 if (*p == '.' || *p == '-' || *p == '+')
4878 *p = '_';
4879
4880 /* Register the function. */
4881 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01004882 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004883 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
4884 lua_settable(gL.T, -3);
4885 }
4886
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004887 lua_settable(gL.T, -3);
4888
4889 /* Register previous table in the registry with reference and named entry. */
4890 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4891 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_FETCHES); /* register class session. */
4892 class_fetches_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4893
4894 /*
4895 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004896 * Register class Converters
4897 *
4898 */
4899
4900 /* Create and fill the metatable. */
4901 lua_newtable(gL.T);
4902
4903 /* Create and fill the __index entry. */
4904 lua_pushstring(gL.T, "__index");
4905 lua_newtable(gL.T);
4906
4907 /* Browse existing converters and create the associated
4908 * object method.
4909 */
4910 sc = NULL;
4911 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
4912 /* Dont register the keywork if the arguments check function are
4913 * not safe during the runtime.
4914 */
4915 if (sc->val_args != NULL)
4916 continue;
4917
4918 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
4919 * by an underscore.
4920 */
4921 strncpy(trash.str, sc->kw, trash.size);
4922 trash.str[trash.size - 1] = '\0';
4923 for (p = trash.str; *p; p++)
4924 if (*p == '.' || *p == '-' || *p == '+')
4925 *p = '_';
4926
4927 /* Register the function. */
4928 lua_pushstring(gL.T, trash.str);
4929 lua_pushlightuserdata(gL.T, sc);
4930 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
4931 lua_settable(gL.T, -3);
4932 }
4933
4934 lua_settable(gL.T, -3);
4935
4936 /* Register previous table in the registry with reference and named entry. */
4937 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4938 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CONVERTERS); /* register class session. */
4939 class_converters_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4940
4941 /*
4942 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004943 * Register class HTTP
4944 *
4945 */
4946
4947 /* Create and fill the metatable. */
4948 lua_newtable(gL.T);
4949
4950 /* Create and fille the __index entry. */
4951 lua_pushstring(gL.T, "__index");
4952 lua_newtable(gL.T);
4953
4954 /* Register Lua functions. */
4955 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
4956 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
4957 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
4958 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
4959 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
4960 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
4961 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
4962 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
4963 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
4964 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
4965
4966 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
4967 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
4968 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
4969 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
4970 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
4971 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
4972
4973 lua_settable(gL.T, -3);
4974
4975 /* Register previous table in the registry with reference and named entry. */
4976 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4977 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_HTTP); /* register class session. */
4978 class_http_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4979
4980 /*
4981 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004982 * Register class TXN
4983 *
4984 */
4985
4986 /* Create and fill the metatable. */
4987 lua_newtable(gL.T);
4988
4989 /* Create and fille the __index entry. */
4990 lua_pushstring(gL.T, "__index");
4991 lua_newtable(gL.T);
4992
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004993 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01004994 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
4995 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004996 hlua_class_function(gL.T, "set_var", hlua_set_var);
4997 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01004998 hlua_class_function(gL.T, "close", hlua_txn_close);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01004999 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
5000 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
5001 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005002 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
5003 hlua_class_function(gL.T, "log", hlua_txn_log);
5004 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
5005 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
5006 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
5007 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005008
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005009 lua_settable(gL.T, -3);
5010
5011 /* Register previous table in the registry with reference and named entry. */
5012 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5013 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_TXN); /* register class session. */
5014 class_txn_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005015
5016 /*
5017 *
5018 * Register class Socket
5019 *
5020 */
5021
5022 /* Create and fill the metatable. */
5023 lua_newtable(gL.T);
5024
5025 /* Create and fille the __index entry. */
5026 lua_pushstring(gL.T, "__index");
5027 lua_newtable(gL.T);
5028
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005029#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005030 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005031#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005032 hlua_class_function(gL.T, "connect", hlua_socket_connect);
5033 hlua_class_function(gL.T, "send", hlua_socket_send);
5034 hlua_class_function(gL.T, "receive", hlua_socket_receive);
5035 hlua_class_function(gL.T, "close", hlua_socket_close);
5036 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
5037 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
5038 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
5039 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
5040
5041 lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
5042
5043 /* Register the garbage collector entry. */
5044 lua_pushstring(gL.T, "__gc");
5045 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
5046 lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
5047
5048 /* Register previous table in the registry with reference and named entry. */
5049 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5050 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5051 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_SOCKET); /* register class socket. */
5052 class_socket_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class socket. */
5053
5054 /* Proxy and server configuration initialisation. */
5055 memset(&socket_proxy, 0, sizeof(socket_proxy));
5056 init_new_proxy(&socket_proxy);
5057 socket_proxy.parent = NULL;
5058 socket_proxy.last_change = now.tv_sec;
5059 socket_proxy.id = "LUA-SOCKET";
5060 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
5061 socket_proxy.maxconn = 0;
5062 socket_proxy.accept = NULL;
5063 socket_proxy.options2 |= PR_O2_INDEPSTR;
5064 socket_proxy.srv = NULL;
5065 socket_proxy.conn_retries = 0;
5066 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
5067
5068 /* Init TCP server: unchanged parameters */
5069 memset(&socket_tcp, 0, sizeof(socket_tcp));
5070 socket_tcp.next = NULL;
5071 socket_tcp.proxy = &socket_proxy;
5072 socket_tcp.obj_type = OBJ_TYPE_SERVER;
5073 LIST_INIT(&socket_tcp.actconns);
5074 LIST_INIT(&socket_tcp.pendconns);
5075 socket_tcp.state = SRV_ST_RUNNING; /* early server setup */
5076 socket_tcp.last_change = 0;
5077 socket_tcp.id = "LUA-TCP-CONN";
5078 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5079 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5080 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
5081
5082 /* XXX: Copy default parameter from default server,
5083 * but the default server is not initialized.
5084 */
5085 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
5086 socket_tcp.minconn = socket_proxy.defsrv.minconn;
5087 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
5088 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
5089 socket_tcp.onerror = socket_proxy.defsrv.onerror;
5090 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5091 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
5092 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5093 socket_tcp.uweight = socket_proxy.defsrv.iweight;
5094 socket_tcp.iweight = socket_proxy.defsrv.iweight;
5095
5096 socket_tcp.check.status = HCHK_STATUS_INI;
5097 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
5098 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
5099 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
5100 socket_tcp.check.server = &socket_tcp;
5101
5102 socket_tcp.agent.status = HCHK_STATUS_INI;
5103 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
5104 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
5105 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
5106 socket_tcp.agent.server = &socket_tcp;
5107
5108 socket_tcp.xprt = &raw_sock;
5109
5110#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005111 /* Init TCP server: unchanged parameters */
5112 memset(&socket_ssl, 0, sizeof(socket_ssl));
5113 socket_ssl.next = NULL;
5114 socket_ssl.proxy = &socket_proxy;
5115 socket_ssl.obj_type = OBJ_TYPE_SERVER;
5116 LIST_INIT(&socket_ssl.actconns);
5117 LIST_INIT(&socket_ssl.pendconns);
5118 socket_ssl.state = SRV_ST_RUNNING; /* early server setup */
5119 socket_ssl.last_change = 0;
5120 socket_ssl.id = "LUA-SSL-CONN";
5121 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5122 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5123 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
5124
5125 /* XXX: Copy default parameter from default server,
5126 * but the default server is not initialized.
5127 */
5128 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
5129 socket_ssl.minconn = socket_proxy.defsrv.minconn;
5130 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
5131 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
5132 socket_ssl.onerror = socket_proxy.defsrv.onerror;
5133 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5134 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
5135 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5136 socket_ssl.uweight = socket_proxy.defsrv.iweight;
5137 socket_ssl.iweight = socket_proxy.defsrv.iweight;
5138
5139 socket_ssl.check.status = HCHK_STATUS_INI;
5140 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
5141 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
5142 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
5143 socket_ssl.check.server = &socket_ssl;
5144
5145 socket_ssl.agent.status = HCHK_STATUS_INI;
5146 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
5147 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
5148 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
5149 socket_ssl.agent.server = &socket_ssl;
5150
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005151 socket_ssl.use_ssl = 1;
5152 socket_ssl.xprt = &ssl_sock;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005153
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005154 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005155 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
5156 /*
5157 *
5158 * If the keyword is not known, we can search in the registered
5159 * server keywords. This is usefull to configure special SSL
5160 * features like client certificates and ssl_verify.
5161 *
5162 */
5163 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
5164 if (tmp_error != 0) {
5165 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
5166 abort(); /* This must be never arrives because the command line
5167 not editable by the user. */
5168 }
5169 idx += kw->skip;
5170 }
5171 }
5172
5173 /* Initialize SSL server. */
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005174 ssl_sock_prepare_srv_ctx(&socket_ssl, &socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005175#endif
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01005176}