blob: 6fe3797bc6555fbb5f788ad59c0b9495c44ae191 [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:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100320 case ARGT_TIME:
321 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100322 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100323 break;
324
325 case ARGT_STR:
326 lua_pushlstring(L, arg->data.str.str, arg->data.str.len);
327 break;
328
329 case ARGT_IPV4:
330 case ARGT_IPV6:
331 case ARGT_MSK4:
332 case ARGT_MSK6:
333 case ARGT_FE:
334 case ARGT_BE:
335 case ARGT_TAB:
336 case ARGT_SRV:
337 case ARGT_USR:
338 case ARGT_MAP:
339 default:
340 lua_pushnil(L);
341 break;
342 }
343 return 1;
344}
345
346/* This function take one entrie in an LUA stack at the index "ud",
347 * and try to convert it in an HAProxy argument entry. This is useful
348 * with sample fetch wrappers. The input arguments are gived to the
349 * lua wrapper and converted as arg list by thi function.
350 */
351static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
352{
353 switch (lua_type(L, ud)) {
354
355 case LUA_TNUMBER:
356 case LUA_TBOOLEAN:
357 arg->type = ARGT_SINT;
358 arg->data.sint = lua_tointeger(L, ud);
359 break;
360
361 case LUA_TSTRING:
362 arg->type = ARGT_STR;
363 arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len);
364 break;
365
366 case LUA_TUSERDATA:
367 case LUA_TNIL:
368 case LUA_TTABLE:
369 case LUA_TFUNCTION:
370 case LUA_TTHREAD:
371 case LUA_TLIGHTUSERDATA:
372 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200373 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100374 break;
375 }
376 return 1;
377}
378
379/* the following functions are used to convert a struct sample
380 * in Lua type. This useful to convert the return of the
381 * fetchs or converters.
382 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100383static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100384{
385 switch (smp->type) {
386 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100387 case SMP_T_BOOL:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100388 lua_pushinteger(L, smp->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100389 break;
390
391 case SMP_T_BIN:
392 case SMP_T_STR:
393 lua_pushlstring(L, smp->data.str.str, smp->data.str.len);
394 break;
395
396 case SMP_T_METH:
397 switch (smp->data.meth.meth) {
398 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
399 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
400 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
401 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
402 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
403 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
404 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
405 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
406 case HTTP_METH_OTHER:
407 lua_pushlstring(L, smp->data.meth.str.str, smp->data.meth.str.len);
408 break;
409 default:
410 lua_pushnil(L);
411 break;
412 }
413 break;
414
415 case SMP_T_IPV4:
416 case SMP_T_IPV6:
417 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100418 if (sample_casts[smp->type][SMP_T_STR] &&
419 sample_casts[smp->type][SMP_T_STR](smp))
420 lua_pushlstring(L, smp->data.str.str, smp->data.str.len);
421 else
422 lua_pushnil(L);
423 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100424 default:
425 lua_pushnil(L);
426 break;
427 }
428 return 1;
429}
430
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100431/* the following functions are used to convert a struct sample
432 * in Lua strings. This is useful to convert the return of the
433 * fetchs or converters.
434 */
435static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
436{
437 switch (smp->type) {
438
439 case SMP_T_BIN:
440 case SMP_T_STR:
441 lua_pushlstring(L, smp->data.str.str, smp->data.str.len);
442 break;
443
444 case SMP_T_METH:
445 switch (smp->data.meth.meth) {
446 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
447 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
448 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
449 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
450 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
451 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
452 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
453 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
454 case HTTP_METH_OTHER:
455 lua_pushlstring(L, smp->data.meth.str.str, smp->data.meth.str.len);
456 break;
457 default:
458 lua_pushstring(L, "");
459 break;
460 }
461 break;
462
463 case SMP_T_SINT:
464 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100465 case SMP_T_IPV4:
466 case SMP_T_IPV6:
467 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
468 if (sample_casts[smp->type][SMP_T_STR] &&
469 sample_casts[smp->type][SMP_T_STR](smp))
470 lua_pushlstring(L, smp->data.str.str, smp->data.str.len);
471 else
472 lua_pushstring(L, "");
473 break;
474 default:
475 lua_pushstring(L, "");
476 break;
477 }
478 return 1;
479}
480
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100481/* the following functions are used to convert an Lua type in a
482 * struct sample. This is useful to provide data from a converter
483 * to the LUA code.
484 */
485static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
486{
487 switch (lua_type(L, ud)) {
488
489 case LUA_TNUMBER:
490 smp->type = SMP_T_SINT;
491 smp->data.sint = lua_tointeger(L, ud);
492 break;
493
494
495 case LUA_TBOOLEAN:
496 smp->type = SMP_T_BOOL;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200497 smp->data.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100498 break;
499
500 case LUA_TSTRING:
501 smp->type = SMP_T_STR;
502 smp->flags |= SMP_F_CONST;
503 smp->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.str.len);
504 break;
505
506 case LUA_TUSERDATA:
507 case LUA_TNIL:
508 case LUA_TTABLE:
509 case LUA_TFUNCTION:
510 case LUA_TTHREAD:
511 case LUA_TLIGHTUSERDATA:
512 smp->type = SMP_T_BOOL;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200513 smp->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100514 break;
515 }
516 return 1;
517}
518
519/* This function check the "argp" builded by another conversion function
520 * is in accord with the expected argp defined by the "mask". The fucntion
521 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100522 *
523 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
524 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100525 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100526__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
527 unsigned int mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100528{
529 int min_arg;
530 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100531 struct proxy *px;
532 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100533
534 idx = 0;
535 min_arg = ARGM(mask);
536 mask >>= ARGM_BITS;
537
538 while (1) {
539
540 /* Check oversize. */
541 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100542 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100543 }
544
545 /* Check for mandatory arguments. */
546 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100547 if (idx < min_arg) {
548
549 /* If miss other argument than the first one, we return an error. */
550 if (idx > 0)
551 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
552
553 /* If first argument have a certain type, some default values
554 * may be used. See the function smp_resolve_args().
555 */
556 switch (mask & ARGT_MASK) {
557
558 case ARGT_FE:
559 if (!(p->cap & PR_CAP_FE))
560 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
561 argp[idx].data.prx = p;
562 argp[idx].type = ARGT_FE;
563 argp[idx+1].type = ARGT_STOP;
564 break;
565
566 case ARGT_BE:
567 if (!(p->cap & PR_CAP_BE))
568 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
569 argp[idx].data.prx = p;
570 argp[idx].type = ARGT_BE;
571 argp[idx+1].type = ARGT_STOP;
572 break;
573
574 case ARGT_TAB:
575 argp[idx].data.prx = p;
576 argp[idx].type = ARGT_TAB;
577 argp[idx+1].type = ARGT_STOP;
578 break;
579
580 default:
581 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
582 break;
583 }
584 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100585 return 0;
586 }
587
588 /* Check for exceed the number of requiered argument. */
589 if ((mask & ARGT_MASK) == ARGT_STOP &&
590 argp[idx].type != ARGT_STOP) {
591 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
592 }
593
594 if ((mask & ARGT_MASK) == ARGT_STOP &&
595 argp[idx].type == ARGT_STOP) {
596 return 0;
597 }
598
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100599 /* Convert some argument types. */
600 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100601 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100602 if (argp[idx].type != ARGT_SINT)
603 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
604 argp[idx].type = ARGT_SINT;
605 break;
606
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100607 case ARGT_TIME:
608 if (argp[idx].type != ARGT_SINT)
609 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200610 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100611 break;
612
613 case ARGT_SIZE:
614 if (argp[idx].type != ARGT_SINT)
615 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200616 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100617 break;
618
619 case ARGT_FE:
620 if (argp[idx].type != ARGT_STR)
621 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
622 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
623 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200624 argp[idx].data.prx = proxy_fe_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100625 if (!argp[idx].data.prx)
626 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
627 argp[idx].type = ARGT_FE;
628 break;
629
630 case ARGT_BE:
631 if (argp[idx].type != ARGT_STR)
632 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
633 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
634 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200635 argp[idx].data.prx = proxy_be_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100636 if (!argp[idx].data.prx)
637 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
638 argp[idx].type = ARGT_BE;
639 break;
640
641 case ARGT_TAB:
642 if (argp[idx].type != ARGT_STR)
643 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
644 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
645 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200646 argp[idx].data.prx = proxy_tbl_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100647 if (!argp[idx].data.prx)
648 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
649 argp[idx].type = ARGT_TAB;
650 break;
651
652 case ARGT_SRV:
653 if (argp[idx].type != ARGT_STR)
654 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
655 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
656 trash.str[argp[idx].data.str.len] = 0;
657 sname = strrchr(trash.str, '/');
658 if (sname) {
659 *sname++ = '\0';
660 pname = trash.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200661 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100662 if (!px)
663 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
664 }
665 else {
666 sname = trash.str;
667 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100668 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100669 argp[idx].data.srv = findserver(px, sname);
670 if (!argp[idx].data.srv)
671 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
672 argp[idx].type = ARGT_SRV;
673 break;
674
675 case ARGT_IPV4:
676 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
677 trash.str[argp[idx].data.str.len] = 0;
678 if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4))
679 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
680 argp[idx].type = ARGT_IPV4;
681 break;
682
683 case ARGT_MSK4:
684 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
685 trash.str[argp[idx].data.str.len] = 0;
686 if (!str2mask(trash.str, &argp[idx].data.ipv4))
687 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
688 argp[idx].type = ARGT_MSK4;
689 break;
690
691 case ARGT_IPV6:
692 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
693 trash.str[argp[idx].data.str.len] = 0;
694 if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6))
695 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
696 argp[idx].type = ARGT_IPV6;
697 break;
698
699 case ARGT_MSK6:
700 case ARGT_MAP:
701 case ARGT_REG:
702 case ARGT_USR:
703 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100704 break;
705 }
706
707 /* Check for type of argument. */
708 if ((mask & ARGT_MASK) != argp[idx].type) {
709 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
710 arg_type_names[(mask & ARGT_MASK)],
711 arg_type_names[argp[idx].type & ARGT_MASK]);
712 WILL_LJMP(luaL_argerror(L, first + idx, msg));
713 }
714
715 /* Next argument. */
716 mask >>= ARGT_BITS;
717 idx++;
718 }
719}
720
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100721/*
722 * The following functions are used to make correspondance between the the
723 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100724 *
725 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100726 * - hlua_sethlua : create the association between hlua context and lua_state.
727 */
728static inline struct hlua *hlua_gethlua(lua_State *L)
729{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100730 struct hlua **hlua = lua_getextraspace(L);
731 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100732}
733static inline void hlua_sethlua(struct hlua *hlua)
734{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100735 struct hlua **hlua_store = lua_getextraspace(hlua->T);
736 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100737}
738
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100739/* This function is used to send logs. It try to send on screen (stderr)
740 * and on the default syslog server.
741 */
742static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
743{
744 struct tm tm;
745 char *p;
746
747 /* Cleanup the log message. */
748 p = trash.str;
749 for (; *msg != '\0'; msg++, p++) {
750 if (p >= trash.str + trash.size - 1)
751 return;
752 if (isprint(*msg))
753 *p = *msg;
754 else
755 *p = '.';
756 }
757 *p = '\0';
758
759 send_log(px, level, "%s", trash.str);
760 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
761 get_localtime(date.tv_sec, &tm);
762 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
763 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
764 (int)getpid(), trash.str);
765 fflush(stderr);
766 }
767}
768
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100769/* This function just ensure that the yield will be always
770 * returned with a timeout and permit to set some flags
771 */
772__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100773 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100774{
775 struct hlua *hlua = hlua_gethlua(L);
776
777 /* Set the wake timeout. If timeout is required, we set
778 * the expiration time.
779 */
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100780 hlua->wake_time = tick_first(timeout, hlua->expire);
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100781
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100782 hlua->flags |= flags;
783
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100784 /* Process the yield. */
785 WILL_LJMP(lua_yieldk(L, nresults, ctx, k));
786}
787
Willy Tarreau87b09662015-04-03 00:22:06 +0200788/* This function initialises the Lua environment stored in the stream.
789 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100790 * an LUA coroutine. It can not be use to crete the main LUA context.
791 */
792int hlua_ctx_init(struct hlua *lua, struct task *task)
793{
794 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100795 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100796 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100797 lua->T = lua_newthread(gL.T);
798 if (!lua->T) {
799 lua->Tref = LUA_REFNIL;
800 return 0;
801 }
802 hlua_sethlua(lua);
803 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
804 lua->task = task;
805 return 1;
806}
807
Willy Tarreau87b09662015-04-03 00:22:06 +0200808/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100809 * is destroyed. The destroy also the memory context. The struct "lua"
810 * is not freed.
811 */
812void hlua_ctx_destroy(struct hlua *lua)
813{
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100814 if (!lua->T)
815 return;
816
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100817 /* Purge all the pending signals. */
818 hlua_com_purge(lua);
819
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100820 /* The thread is garbage collected by Lua. */
821 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
822 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
823}
824
825/* This function is used to restore the Lua context when a coroutine
826 * fails. This function copy the common memory between old coroutine
827 * and the new coroutine. The old coroutine is destroyed, and its
828 * replaced by the new coroutine.
829 * If the flag "keep_msg" is set, the last entry of the old is assumed
830 * as string error message and it is copied in the new stack.
831 */
832static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
833{
834 lua_State *T;
835 int new_ref;
836
837 /* Renew the main LUA stack doesn't have sense. */
838 if (lua == &gL)
839 return 0;
840
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100841 /* New Lua coroutine. */
842 T = lua_newthread(gL.T);
843 if (!T)
844 return 0;
845
846 /* Copy last error message. */
847 if (keep_msg)
848 lua_xmove(lua->T, T, 1);
849
850 /* Copy data between the coroutines. */
851 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
852 lua_xmove(lua->T, T, 1);
853 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
854
855 /* Destroy old data. */
856 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
857
858 /* The thread is garbage collected by Lua. */
859 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
860
861 /* Fill the struct with the new coroutine values. */
862 lua->Mref = new_ref;
863 lua->T = T;
864 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
865
866 /* Set context. */
867 hlua_sethlua(lua);
868
869 return 1;
870}
871
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100872void hlua_hook(lua_State *L, lua_Debug *ar)
873{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100874 struct hlua *hlua = hlua_gethlua(L);
875
876 /* Lua cannot yield when its returning from a function,
877 * so, we can fix the interrupt hook to 1 instruction,
878 * expecting that the function is finnished.
879 */
880 if (lua_gethookmask(L) & LUA_MASKRET) {
881 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
882 return;
883 }
884
885 /* restore the interrupt condition. */
886 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
887
888 /* If we interrupt the Lua processing in yieldable state, we yield.
889 * If the state is not yieldable, trying yield causes an error.
890 */
891 if (lua_isyieldable(L))
892 WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
893
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +0100894 /* If we cannot yield, update the clock and check the timeout. */
895 tv_update_date(0, 1);
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100896 if (tick_is_expired(hlua->expire, now_ms)) {
897 lua_pushfstring(L, "execution timeout");
898 WILL_LJMP(lua_error(L));
899 }
900
901 /* Try to interrupt the process at the end of the current
902 * unyieldable function.
903 */
904 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100905}
906
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907/* This function start or resumes the Lua stack execution. If the flag
908 * "yield_allowed" if no set and the LUA stack execution returns a yield
909 * The function return an error.
910 *
911 * The function can returns 4 values:
912 * - HLUA_E_OK : The execution is terminated without any errors.
913 * - HLUA_E_AGAIN : The execution must continue at the next associated
914 * task wakeup.
915 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
916 * the top of the stack.
917 * - HLUA_E_ERR : An error has occured without error message.
918 *
919 * If an error occured, the stack is renewed and it is ready to run new
920 * LUA code.
921 */
922static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
923{
924 int ret;
925 const char *msg;
926
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100927 HLUA_SET_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100928
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100929 /* If we want to resume the task, then check first the execution timeout.
930 * if it is reached, we can interrupt the Lua processing.
931 */
932 if (tick_is_expired(lua->expire, now_ms))
933 goto timeout_reached;
934
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100935resume_execution:
936
937 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
938 * instructions. it is used for preventing infinite loops.
939 */
940 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
941
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +0100942 /* Remove all flags except the running flags. */
943 lua->flags = HLUA_RUN;
944
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100945 /* Call the function. */
946 ret = lua_resume(lua->T, gL.T, lua->nargs);
947 switch (ret) {
948
949 case LUA_OK:
950 ret = HLUA_E_OK;
951 break;
952
953 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100954 /* Check if the execution timeout is expired. It it is the case, we
955 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100956 */
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100957 if (tick_is_expired(lua->expire, now_ms)) {
958
959timeout_reached:
960
961 lua_settop(lua->T, 0); /* Empty the stack. */
962 if (!lua_checkstack(lua->T, 1)) {
963 ret = HLUA_E_ERR;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100964 break;
965 }
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +0100966 lua_pushfstring(lua->T, "execution timeout");
967 ret = HLUA_E_ERRMSG;
968 break;
969 }
970 /* Process the forced yield. if the general yield is not allowed or
971 * if no task were associated this the current Lua execution
972 * coroutine, we resume the execution. Else we want to return in the
973 * scheduler and we want to be waked up again, to continue the
974 * current Lua execution. So we schedule our own task.
975 */
976 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100977 if (!yield_allowed || !lua->task)
978 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100979 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100980 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100981 if (!yield_allowed) {
982 lua_settop(lua->T, 0); /* Empty the stack. */
983 if (!lua_checkstack(lua->T, 1)) {
984 ret = HLUA_E_ERR;
985 break;
986 }
987 lua_pushfstring(lua->T, "yield not allowed");
988 ret = HLUA_E_ERRMSG;
989 break;
990 }
991 ret = HLUA_E_AGAIN;
992 break;
993
994 case LUA_ERRRUN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100995 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100996 if (!lua_checkstack(lua->T, 1)) {
997 ret = HLUA_E_ERR;
998 break;
999 }
1000 msg = lua_tostring(lua->T, -1);
1001 lua_settop(lua->T, 0); /* Empty the stack. */
1002 lua_pop(lua->T, 1);
1003 if (msg)
1004 lua_pushfstring(lua->T, "runtime error: %s", msg);
1005 else
1006 lua_pushfstring(lua->T, "unknown runtime error");
1007 ret = HLUA_E_ERRMSG;
1008 break;
1009
1010 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001011 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001012 lua_settop(lua->T, 0); /* Empty the stack. */
1013 if (!lua_checkstack(lua->T, 1)) {
1014 ret = HLUA_E_ERR;
1015 break;
1016 }
1017 lua_pushfstring(lua->T, "out of memory error");
1018 ret = HLUA_E_ERRMSG;
1019 break;
1020
1021 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001022 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001023 if (!lua_checkstack(lua->T, 1)) {
1024 ret = HLUA_E_ERR;
1025 break;
1026 }
1027 msg = lua_tostring(lua->T, -1);
1028 lua_settop(lua->T, 0); /* Empty the stack. */
1029 lua_pop(lua->T, 1);
1030 if (msg)
1031 lua_pushfstring(lua->T, "message handler error: %s", msg);
1032 else
1033 lua_pushfstring(lua->T, "message handler error");
1034 ret = HLUA_E_ERRMSG;
1035 break;
1036
1037 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001038 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001039 lua_settop(lua->T, 0); /* Empty the stack. */
1040 if (!lua_checkstack(lua->T, 1)) {
1041 ret = HLUA_E_ERR;
1042 break;
1043 }
1044 lua_pushfstring(lua->T, "unknonwn error");
1045 ret = HLUA_E_ERRMSG;
1046 break;
1047 }
1048
1049 switch (ret) {
1050 case HLUA_E_AGAIN:
1051 break;
1052
1053 case HLUA_E_ERRMSG:
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001054 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001055 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001056 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001057 break;
1058
1059 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001060 HLUA_CLR_RUN(lua);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001061 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001062 hlua_ctx_renew(lua, 0);
1063 break;
1064
1065 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001066 HLUA_CLR_RUN(lua);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001067 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001068 break;
1069 }
1070
1071 return ret;
1072}
1073
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001074/* This function is an LUA binding. It provides a function
1075 * for deleting ACL from a referenced ACL file.
1076 */
1077__LJMP static int hlua_del_acl(lua_State *L)
1078{
1079 const char *name;
1080 const char *key;
1081 struct pat_ref *ref;
1082
1083 MAY_LJMP(check_args(L, 2, "del_acl"));
1084
1085 name = MAY_LJMP(luaL_checkstring(L, 1));
1086 key = MAY_LJMP(luaL_checkstring(L, 2));
1087
1088 ref = pat_ref_lookup(name);
1089 if (!ref)
1090 WILL_LJMP(luaL_error(L, "'del_acl': unkown acl file '%s'", name));
1091
1092 pat_ref_delete(ref, key);
1093 return 0;
1094}
1095
1096/* This function is an LUA binding. It provides a function
1097 * for deleting map entry from a referenced map file.
1098 */
1099static int hlua_del_map(lua_State *L)
1100{
1101 const char *name;
1102 const char *key;
1103 struct pat_ref *ref;
1104
1105 MAY_LJMP(check_args(L, 2, "del_map"));
1106
1107 name = MAY_LJMP(luaL_checkstring(L, 1));
1108 key = MAY_LJMP(luaL_checkstring(L, 2));
1109
1110 ref = pat_ref_lookup(name);
1111 if (!ref)
1112 WILL_LJMP(luaL_error(L, "'del_map': unkown acl file '%s'", name));
1113
1114 pat_ref_delete(ref, key);
1115 return 0;
1116}
1117
1118/* This function is an LUA binding. It provides a function
1119 * for adding ACL pattern from a referenced ACL file.
1120 */
1121static int hlua_add_acl(lua_State *L)
1122{
1123 const char *name;
1124 const char *key;
1125 struct pat_ref *ref;
1126
1127 MAY_LJMP(check_args(L, 2, "add_acl"));
1128
1129 name = MAY_LJMP(luaL_checkstring(L, 1));
1130 key = MAY_LJMP(luaL_checkstring(L, 2));
1131
1132 ref = pat_ref_lookup(name);
1133 if (!ref)
1134 WILL_LJMP(luaL_error(L, "'add_acl': unkown acl file '%s'", name));
1135
1136 if (pat_ref_find_elt(ref, key) == NULL)
1137 pat_ref_add(ref, key, NULL, NULL);
1138 return 0;
1139}
1140
1141/* This function is an LUA binding. It provides a function
1142 * for setting map pattern and sample from a referenced map
1143 * file.
1144 */
1145static int hlua_set_map(lua_State *L)
1146{
1147 const char *name;
1148 const char *key;
1149 const char *value;
1150 struct pat_ref *ref;
1151
1152 MAY_LJMP(check_args(L, 3, "set_map"));
1153
1154 name = MAY_LJMP(luaL_checkstring(L, 1));
1155 key = MAY_LJMP(luaL_checkstring(L, 2));
1156 value = MAY_LJMP(luaL_checkstring(L, 3));
1157
1158 ref = pat_ref_lookup(name);
1159 if (!ref)
1160 WILL_LJMP(luaL_error(L, "'set_map': unkown map file '%s'", name));
1161
1162 if (pat_ref_find_elt(ref, key) != NULL)
1163 pat_ref_set(ref, key, value, NULL);
1164 else
1165 pat_ref_add(ref, key, value, NULL);
1166 return 0;
1167}
1168
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001169/* A class is a lot of memory that contain data. This data can be a table,
1170 * an integer or user data. This data is associated with a metatable. This
1171 * metatable have an original version registred in the global context with
1172 * the name of the object (_G[<name>] = <metable> ).
1173 *
1174 * A metable is a table that modify the standard behavior of a standard
1175 * access to the associated data. The entries of this new metatable are
1176 * defined as is:
1177 *
1178 * http://lua-users.org/wiki/MetatableEvents
1179 *
1180 * __index
1181 *
1182 * we access an absent field in a table, the result is nil. This is
1183 * true, but it is not the whole truth. Actually, such access triggers
1184 * the interpreter to look for an __index metamethod: If there is no
1185 * such method, as usually happens, then the access results in nil;
1186 * otherwise, the metamethod will provide the result.
1187 *
1188 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1189 * the key does not appear in the table, but the metatable has an __index
1190 * property:
1191 *
1192 * - if the value is a function, the function is called, passing in the
1193 * table and the key; the return value of that function is returned as
1194 * the result.
1195 *
1196 * - if the value is another table, the value of the key in that table is
1197 * asked for and returned (and if it doesn't exist in that table, but that
1198 * table's metatable has an __index property, then it continues on up)
1199 *
1200 * - Use "rawget(myTable,key)" to skip this metamethod.
1201 *
1202 * http://www.lua.org/pil/13.4.1.html
1203 *
1204 * __newindex
1205 *
1206 * Like __index, but control property assignment.
1207 *
1208 * __mode - Control weak references. A string value with one or both
1209 * of the characters 'k' and 'v' which specifies that the the
1210 * keys and/or values in the table are weak references.
1211 *
1212 * __call - Treat a table like a function. When a table is followed by
1213 * parenthesis such as "myTable( 'foo' )" and the metatable has
1214 * a __call key pointing to a function, that function is invoked
1215 * (passing any specified arguments) and the return value is
1216 * returned.
1217 *
1218 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1219 * called, if the metatable for myTable has a __metatable
1220 * key, the value of that key is returned instead of the
1221 * actual metatable.
1222 *
1223 * __tostring - Control string representation. When the builtin
1224 * "tostring( myTable )" function is called, if the metatable
1225 * for myTable has a __tostring property set to a function,
1226 * that function is invoked (passing myTable to it) and the
1227 * return value is used as the string representation.
1228 *
1229 * __len - Control table length. When the table length is requested using
1230 * the length operator ( '#' ), if the metatable for myTable has
1231 * a __len key pointing to a function, that function is invoked
1232 * (passing myTable to it) and the return value used as the value
1233 * of "#myTable".
1234 *
1235 * __gc - Userdata finalizer code. When userdata is set to be garbage
1236 * collected, if the metatable has a __gc field pointing to a
1237 * function, that function is first invoked, passing the userdata
1238 * to it. The __gc metamethod is not called for tables.
1239 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1240 *
1241 * Special metamethods for redefining standard operators:
1242 * http://www.lua.org/pil/13.1.html
1243 *
1244 * __add "+"
1245 * __sub "-"
1246 * __mul "*"
1247 * __div "/"
1248 * __unm "!"
1249 * __pow "^"
1250 * __concat ".."
1251 *
1252 * Special methods for redfining standar relations
1253 * http://www.lua.org/pil/13.2.html
1254 *
1255 * __eq "=="
1256 * __lt "<"
1257 * __le "<="
1258 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001259
1260/*
1261 *
1262 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001263 * Class Map
1264 *
1265 *
1266 */
1267
1268/* Returns a struct hlua_map if the stack entry "ud" is
1269 * a class session, otherwise it throws an error.
1270 */
1271__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1272{
1273 return (struct map_descriptor *)MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
1274}
1275
1276/* This function is the map constructor. It don't need
1277 * the class Map object. It creates and return a new Map
1278 * object. It must be called only during "body" or "init"
1279 * context because it process some filesystem accesses.
1280 */
1281__LJMP static int hlua_map_new(struct lua_State *L)
1282{
1283 const char *fn;
1284 int match = PAT_MATCH_STR;
1285 struct sample_conv conv;
1286 const char *file = "";
1287 int line = 0;
1288 lua_Debug ar;
1289 char *err = NULL;
1290 struct arg args[2];
1291
1292 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1293 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1294
1295 fn = MAY_LJMP(luaL_checkstring(L, 1));
1296
1297 if (lua_gettop(L) >= 2) {
1298 match = MAY_LJMP(luaL_checkinteger(L, 2));
1299 if (match < 0 || match >= PAT_MATCH_NUM)
1300 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1301 }
1302
1303 /* Get Lua filename and line number. */
1304 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1305 lua_getinfo(L, "Sl", &ar); /* get info about it */
1306 if (ar.currentline > 0) { /* is there info? */
1307 file = ar.short_src;
1308 line = ar.currentline;
1309 }
1310 }
1311
1312 /* fill fake sample_conv struct. */
1313 conv.kw = ""; /* unused. */
1314 conv.process = NULL; /* unused. */
1315 conv.arg_mask = 0; /* unused. */
1316 conv.val_args = NULL; /* unused. */
1317 conv.out_type = SMP_T_STR;
1318 conv.private = (void *)(long)match;
1319 switch (match) {
1320 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1321 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1322 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1323 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1324 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1325 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1326 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001327 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001328 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1329 default:
1330 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1331 }
1332
1333 /* fill fake args. */
1334 args[0].type = ARGT_STR;
1335 args[0].data.str.str = (char *)fn;
1336 args[1].type = ARGT_STOP;
1337
1338 /* load the map. */
1339 if (!sample_load_map(args, &conv, file, line, &err)) {
1340 /* error case: we cant use luaL_error because we must
1341 * free the err variable.
1342 */
1343 luaL_where(L, 1);
1344 lua_pushfstring(L, "'new': %s.", err);
1345 lua_concat(L, 2);
1346 free(err);
1347 WILL_LJMP(lua_error(L));
1348 }
1349
1350 /* create the lua object. */
1351 lua_newtable(L);
1352 lua_pushlightuserdata(L, args[0].data.map);
1353 lua_rawseti(L, -2, 0);
1354
1355 /* Pop a class Map metatable and affect it to the userdata. */
1356 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1357 lua_setmetatable(L, -2);
1358
1359
1360 return 1;
1361}
1362
1363__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1364{
1365 struct map_descriptor *desc;
1366 struct pattern *pat;
1367 struct sample smp;
1368
1369 MAY_LJMP(check_args(L, 2, "lookup"));
1370 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001371 if (desc->pat.expect_type == SMP_T_SINT) {
1372 smp.type = SMP_T_SINT;
1373 smp.data.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001374 }
1375 else {
1376 smp.type = SMP_T_STR;
1377 smp.flags = SMP_F_CONST;
1378 smp.data.str.str = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.str.len));
1379 }
1380
1381 pat = pattern_exec_match(&desc->pat, &smp, 1);
1382 if (!pat || !pat->smp) {
1383 if (str)
1384 lua_pushstring(L, "");
1385 else
1386 lua_pushnil(L);
1387 return 1;
1388 }
1389
1390 /* The Lua pattern must return a string, so we can't check the returned type */
1391 lua_pushlstring(L, pat->smp->data.str.str, pat->smp->data.str.len);
1392 return 1;
1393}
1394
1395__LJMP static int hlua_map_lookup(struct lua_State *L)
1396{
1397 return _hlua_map_lookup(L, 0);
1398}
1399
1400__LJMP static int hlua_map_slookup(struct lua_State *L)
1401{
1402 return _hlua_map_lookup(L, 1);
1403}
1404
1405/*
1406 *
1407 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001408 * Class Socket
1409 *
1410 *
1411 */
1412
1413__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1414{
1415 return (struct hlua_socket *)MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
1416}
1417
1418/* This function is the handler called for each I/O on the established
1419 * connection. It is used for notify space avalaible to send or data
1420 * received.
1421 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001422static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001423{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001424 struct stream_interface *si = appctx->owner;
Willy Tarreau50fe03b2014-11-28 13:59:31 +01001425 struct connection *c = objt_conn(si_opposite(si)->end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001426
Willy Tarreau87b09662015-04-03 00:22:06 +02001427 /* Wakeup the main stream if the client connection is closed. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001428 if (!c || channel_output_closed(si_ic(si)) || channel_input_closed(si_oc(si))) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001429 if (appctx->ctx.hlua.socket) {
1430 appctx->ctx.hlua.socket->s = NULL;
1431 appctx->ctx.hlua.socket = NULL;
1432 }
1433 si_shutw(si);
1434 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001435 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001436 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1437 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001438 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001439 }
1440
1441 if (!(c->flags & CO_FL_CONNECTED))
Willy Tarreaud4da1962015-04-20 01:31:23 +02001442 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001443
1444 /* This function is called after the connect. */
1445 appctx->ctx.hlua.connected = 1;
1446
1447 /* Wake the tasks which wants to write if the buffer have avalaible space. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001448 if (channel_may_recv(si_oc(si)))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001449 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
1450
1451 /* Wake the tasks which wants to read if the buffer contains data. */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001452 if (channel_is_empty(si_ic(si)))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001453 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1454}
1455
Willy Tarreau87b09662015-04-03 00:22:06 +02001456/* This function is called when the "struct stream" is destroyed.
1457 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001458 * Wake all the pending signals.
1459 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001460static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001461{
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001462 /* Remove my link in the original object. */
1463 if (appctx->ctx.hlua.socket)
1464 appctx->ctx.hlua.socket->s = NULL;
1465
1466 /* Wake all the task waiting for me. */
1467 hlua_com_wake(&appctx->ctx.hlua.wake_on_read);
1468 hlua_com_wake(&appctx->ctx.hlua.wake_on_write);
1469}
1470
1471/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001472 * uses this object. If the stream does not exists, just quit.
1473 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001474 * pending signal can rest in the read and write lists. destroy
1475 * it.
1476 */
1477__LJMP static int hlua_socket_gc(lua_State *L)
1478{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001479 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001480 struct appctx *appctx;
1481
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001482 MAY_LJMP(check_args(L, 1, "__gc"));
1483
1484 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001485 if (!socket->s)
1486 return 0;
1487
Willy Tarreau87b09662015-04-03 00:22:06 +02001488 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001489 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001490 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001491 socket->s = NULL;
1492 appctx->ctx.hlua.socket = NULL;
1493
1494 return 0;
1495}
1496
1497/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001498 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001499 */
1500__LJMP static int hlua_socket_close(lua_State *L)
1501{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001502 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001503 struct appctx *appctx;
1504
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001505 MAY_LJMP(check_args(L, 1, "close"));
1506
1507 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001508 if (!socket->s)
1509 return 0;
1510
Willy Tarreau87b09662015-04-03 00:22:06 +02001511 /* Close the stream and remove the associated stop task. */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001512 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001513 appctx = objt_appctx(socket->s->si[0].end);
1514 appctx->ctx.hlua.socket = NULL;
1515 socket->s = NULL;
1516
1517 return 0;
1518}
1519
1520/* This Lua function assumes that the stack contain three parameters.
1521 * 1 - USERDATA containing a struct socket
1522 * 2 - INTEGER with values of the macro defined below
1523 * If the integer is -1, we must read at most one line.
1524 * If the integer is -2, we ust read all the data until the
1525 * end of the stream.
1526 * If the integer is positive value, we must read a number of
1527 * bytes corresponding to this value.
1528 */
1529#define HLSR_READ_LINE (-1)
1530#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001531__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001532{
1533 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1534 int wanted = lua_tointeger(L, 2);
1535 struct hlua *hlua = hlua_gethlua(L);
1536 struct appctx *appctx;
1537 int len;
1538 int nblk;
1539 char *blk1;
1540 int len1;
1541 char *blk2;
1542 int len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001543 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001544 struct channel *oc;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001545
1546 /* Check if this lua stack is schedulable. */
1547 if (!hlua || !hlua->task)
1548 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1549 "'frontend', 'backend' or 'task'"));
1550
1551 /* check for connection closed. If some data where read, return it. */
1552 if (!socket->s)
1553 goto connection_closed;
1554
Willy Tarreau94aa6172015-03-13 14:19:06 +01001555 oc = &socket->s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001556 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001557 /* Read line. */
Willy Tarreau81389672015-03-10 12:03:52 +01001558 nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001559 if (nblk < 0) /* Connection close. */
1560 goto connection_closed;
1561 if (nblk == 0) /* No data avalaible. */
1562 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001563
1564 /* remove final \r\n. */
1565 if (nblk == 1) {
1566 if (blk1[len1-1] == '\n') {
1567 len1--;
1568 skip_at_end++;
1569 if (blk1[len1-1] == '\r') {
1570 len1--;
1571 skip_at_end++;
1572 }
1573 }
1574 }
1575 else {
1576 if (blk2[len2-1] == '\n') {
1577 len2--;
1578 skip_at_end++;
1579 if (blk2[len2-1] == '\r') {
1580 len2--;
1581 skip_at_end++;
1582 }
1583 }
1584 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001585 }
1586
1587 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001588 /* Read all the available data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001589 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001590 if (nblk < 0) /* Connection close. */
1591 goto connection_closed;
1592 if (nblk == 0) /* No data avalaible. */
1593 goto connection_empty;
1594 }
1595
1596 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001597 /* Read a block of data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001598 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001599 if (nblk < 0) /* Connection close. */
1600 goto connection_closed;
1601 if (nblk == 0) /* No data avalaible. */
1602 goto connection_empty;
1603
1604 if (len1 > wanted) {
1605 nblk = 1;
1606 len1 = wanted;
1607 } if (nblk == 2 && len1 + len2 > wanted)
1608 len2 = wanted - len1;
1609 }
1610
1611 len = len1;
1612
1613 luaL_addlstring(&socket->b, blk1, len1);
1614 if (nblk == 2) {
1615 len += len2;
1616 luaL_addlstring(&socket->b, blk2, len2);
1617 }
1618
1619 /* Consume data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001620 bo_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001621
1622 /* Don't wait anything. */
Willy Tarreau828824a2015-04-19 17:20:03 +02001623 si_applet_done(&socket->s->si[0]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001624
1625 /* If the pattern reclaim to read all the data
1626 * in the connection, got out.
1627 */
1628 if (wanted == HLSR_READ_ALL)
1629 goto connection_empty;
1630 else if (wanted >= 0 && len < wanted)
1631 goto connection_empty;
1632
1633 /* Return result. */
1634 luaL_pushresult(&socket->b);
1635 return 1;
1636
1637connection_closed:
1638
1639 /* If the buffer containds data. */
1640 if (socket->b.n > 0) {
1641 luaL_pushresult(&socket->b);
1642 return 1;
1643 }
1644 lua_pushnil(L);
1645 lua_pushstring(L, "connection closed.");
1646 return 2;
1647
1648connection_empty:
1649
1650 appctx = objt_appctx(socket->s->si[0].end);
1651 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_read))
1652 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001653 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001654 return 0;
1655}
1656
1657/* This Lus function gets two parameters. The first one can be string
1658 * or a number. If the string is "*l", the user require one line. If
1659 * the string is "*a", the user require all the content of the stream.
1660 * If the value is a number, the user require a number of bytes equal
1661 * to the value. The default value is "*l" (a line).
1662 *
1663 * This paraeter with a variable type is converted in integer. This
1664 * integer takes this values:
1665 * -1 : read a line
1666 * -2 : read all the stream
1667 * >0 : amount if bytes.
1668 *
1669 * The second parameter is optinal. It contains a string that must be
1670 * concatenated with the read data.
1671 */
1672__LJMP static int hlua_socket_receive(struct lua_State *L)
1673{
1674 int wanted = HLSR_READ_LINE;
1675 const char *pattern;
1676 int type;
1677 char *error;
1678 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001679 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001680
1681 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1682 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1683
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001684 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685
1686 /* check for pattern. */
1687 if (lua_gettop(L) >= 2) {
1688 type = lua_type(L, 2);
1689 if (type == LUA_TSTRING) {
1690 pattern = lua_tostring(L, 2);
1691 if (strcmp(pattern, "*a") == 0)
1692 wanted = HLSR_READ_ALL;
1693 else if (strcmp(pattern, "*l") == 0)
1694 wanted = HLSR_READ_LINE;
1695 else {
1696 wanted = strtoll(pattern, &error, 10);
1697 if (*error != '\0')
1698 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1699 }
1700 }
1701 else if (type == LUA_TNUMBER) {
1702 wanted = lua_tointeger(L, 2);
1703 if (wanted < 0)
1704 WILL_LJMP(luaL_error(L, "Unsupported size."));
1705 }
1706 }
1707
1708 /* Set pattern. */
1709 lua_pushinteger(L, wanted);
1710 lua_replace(L, 2);
1711
1712 /* init bufffer, and fiil it wih prefix. */
1713 luaL_buffinit(L, &socket->b);
1714
1715 /* Check prefix. */
1716 if (lua_gettop(L) >= 3) {
1717 if (lua_type(L, 3) != LUA_TSTRING)
1718 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1719 pattern = lua_tolstring(L, 3, &len);
1720 luaL_addlstring(&socket->b, pattern, len);
1721 }
1722
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001723 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001724}
1725
1726/* Write the Lua input string in the output buffer.
1727 * This fucntion returns a yield if no space are available.
1728 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001729static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001730{
1731 struct hlua_socket *socket;
1732 struct hlua *hlua = hlua_gethlua(L);
1733 struct appctx *appctx;
1734 size_t buf_len;
1735 const char *buf;
1736 int len;
1737 int send_len;
1738 int sent;
1739
1740 /* Check if this lua stack is schedulable. */
1741 if (!hlua || !hlua->task)
1742 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1743 "'frontend', 'backend' or 'task'"));
1744
1745 /* Get object */
1746 socket = MAY_LJMP(hlua_checksocket(L, 1));
1747 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001748 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001749
1750 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001751 if (!socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001752 lua_pushinteger(L, -1);
1753 return 1;
1754 }
1755
1756 /* Update the input buffer data. */
1757 buf += sent;
1758 send_len = buf_len - sent;
1759
1760 /* All the data are sent. */
1761 if (sent >= buf_len)
1762 return 1; /* Implicitly return the length sent. */
1763
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001764 /* Check if the buffer is avalaible because HAProxy doesn't allocate
1765 * the request buffer if its not required.
1766 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001767 if (socket->s->req.buf->size == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001768 if (!stream_alloc_recv_buffer(&socket->s->req)) {
Willy Tarreau350f4872014-11-28 14:42:25 +01001769 socket->s->si[0].flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001770 goto hlua_socket_write_yield_return;
1771 }
1772 }
1773
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001774 /* Check for avalaible space. */
Willy Tarreau94aa6172015-03-13 14:19:06 +01001775 len = buffer_total_space(socket->s->req.buf);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001776 if (len <= 0)
1777 goto hlua_socket_write_yield_return;
1778
1779 /* send data */
1780 if (len < send_len)
1781 send_len = len;
Willy Tarreau94aa6172015-03-13 14:19:06 +01001782 len = bi_putblk(&socket->s->req, buf+sent, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001783
1784 /* "Not enough space" (-1), "Buffer too little to contain
1785 * the data" (-2) are not expected because the available length
1786 * is tested.
1787 * Other unknown error are also not expected.
1788 */
1789 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01001790 if (len == -1)
Willy Tarreau94aa6172015-03-13 14:19:06 +01001791 socket->s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001792
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001793 MAY_LJMP(hlua_socket_close(L));
1794 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001795 lua_pushinteger(L, -1);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001796 return 1;
1797 }
1798
1799 /* update buffers. */
Willy Tarreau828824a2015-04-19 17:20:03 +02001800 si_applet_done(&socket->s->si[0]);
Willy Tarreau94aa6172015-03-13 14:19:06 +01001801 socket->s->req.rex = TICK_ETERNITY;
1802 socket->s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001803
1804 /* Update length sent. */
1805 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001806 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001807
1808 /* All the data buffer is sent ? */
1809 if (sent + len >= buf_len)
1810 return 1;
1811
1812hlua_socket_write_yield_return:
1813 appctx = objt_appctx(socket->s->si[0].end);
1814 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
1815 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001816 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001817 return 0;
1818}
1819
1820/* This function initiate the send of data. It just check the input
1821 * parameters and push an integer in the Lua stack that contain the
1822 * amount of data writed in the buffer. This is used by the function
1823 * "hlua_socket_write_yield" that can yield.
1824 *
1825 * The Lua function gets between 3 and 4 parameters. The first one is
1826 * the associated object. The second is a string buffer. The third is
1827 * a facultative integer that represents where is the buffer position
1828 * of the start of the data that can send. The first byte is the
1829 * position "1". The default value is "1". The fourth argument is a
1830 * facultative integer that represents where is the buffer position
1831 * of the end of the data that can send. The default is the last byte.
1832 */
1833static int hlua_socket_send(struct lua_State *L)
1834{
1835 int i;
1836 int j;
1837 const char *buf;
1838 size_t buf_len;
1839
1840 /* Check number of arguments. */
1841 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
1842 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
1843
1844 /* Get the string. */
1845 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
1846
1847 /* Get and check j. */
1848 if (lua_gettop(L) == 4) {
1849 j = MAY_LJMP(luaL_checkinteger(L, 4));
1850 if (j < 0)
1851 j = buf_len + j + 1;
1852 if (j > buf_len)
1853 j = buf_len + 1;
1854 lua_pop(L, 1);
1855 }
1856 else
1857 j = buf_len;
1858
1859 /* Get and check i. */
1860 if (lua_gettop(L) == 3) {
1861 i = MAY_LJMP(luaL_checkinteger(L, 3));
1862 if (i < 0)
1863 i = buf_len + i + 1;
1864 if (i > buf_len)
1865 i = buf_len + 1;
1866 lua_pop(L, 1);
1867 } else
1868 i = 1;
1869
1870 /* Check bth i and j. */
1871 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001872 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001873 return 1;
1874 }
1875 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001876 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001877 return 1;
1878 }
1879 if (i == 0)
1880 i = 1;
1881 if (j == 0)
1882 j = 1;
1883
1884 /* Pop the string. */
1885 lua_pop(L, 1);
1886
1887 /* Update the buffer length. */
1888 buf += i - 1;
1889 buf_len = j - i + 1;
1890 lua_pushlstring(L, buf, buf_len);
1891
1892 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001893 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001894
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001895 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001896}
1897
Willy Tarreau22b0a682015-06-17 19:43:49 +02001898#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001899__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
1900{
1901 static char buffer[SOCKET_INFO_MAX_LEN];
1902 int ret;
1903 int len;
1904 char *p;
1905
1906 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
1907 if (ret <= 0) {
1908 lua_pushnil(L);
1909 return 1;
1910 }
1911
1912 if (ret == AF_UNIX) {
1913 lua_pushstring(L, buffer+1);
1914 return 1;
1915 }
1916 else if (ret == AF_INET6) {
1917 buffer[0] = '[';
1918 len = strlen(buffer);
1919 buffer[len] = ']';
1920 len++;
1921 buffer[len] = ':';
1922 len++;
1923 p = buffer;
1924 }
1925 else if (ret == AF_INET) {
1926 p = buffer + 1;
1927 len = strlen(p);
1928 p[len] = ':';
1929 len++;
1930 }
1931 else {
1932 lua_pushnil(L);
1933 return 1;
1934 }
1935
1936 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
1937 lua_pushnil(L);
1938 return 1;
1939 }
1940
1941 lua_pushstring(L, p);
1942 return 1;
1943}
1944
1945/* Returns information about the peer of the connection. */
1946__LJMP static int hlua_socket_getpeername(struct lua_State *L)
1947{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001948 struct hlua_socket *socket;
1949 struct connection *conn;
1950
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001951 MAY_LJMP(check_args(L, 1, "getpeername"));
1952
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001953 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001954
1955 /* Check if the tcp object is avalaible. */
1956 if (!socket->s) {
1957 lua_pushnil(L);
1958 return 1;
1959 }
1960
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001961 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001962 if (!conn) {
1963 lua_pushnil(L);
1964 return 1;
1965 }
1966
1967 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
1968 unsigned int salen = sizeof(conn->addr.to);
1969 if (getpeername(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, &salen) == -1) {
1970 lua_pushnil(L);
1971 return 1;
1972 }
1973 conn->flags |= CO_FL_ADDR_TO_SET;
1974 }
1975
1976 return MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
1977}
1978
1979/* Returns information about my connection side. */
1980static int hlua_socket_getsockname(struct lua_State *L)
1981{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001982 struct hlua_socket *socket;
1983 struct connection *conn;
1984
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001985 MAY_LJMP(check_args(L, 1, "getsockname"));
1986
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001987 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001988
1989 /* Check if the tcp object is avalaible. */
1990 if (!socket->s) {
1991 lua_pushnil(L);
1992 return 1;
1993 }
1994
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001995 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001996 if (!conn) {
1997 lua_pushnil(L);
1998 return 1;
1999 }
2000
2001 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
2002 unsigned int salen = sizeof(conn->addr.from);
2003 if (getsockname(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, &salen) == -1) {
2004 lua_pushnil(L);
2005 return 1;
2006 }
2007 conn->flags |= CO_FL_ADDR_FROM_SET;
2008 }
2009
2010 return hlua_socket_info(L, &conn->addr.from);
2011}
2012
2013/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002014static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002015 .obj_type = OBJ_TYPE_APPLET,
2016 .name = "<LUA_TCP>",
2017 .fct = hlua_socket_handler,
2018 .release = hlua_socket_release,
2019};
2020
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002021__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002022{
2023 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2024 struct hlua *hlua = hlua_gethlua(L);
2025 struct appctx *appctx;
2026
2027 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002028 if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002029 lua_pushnil(L);
2030 lua_pushstring(L, "Can't connect");
2031 return 2;
2032 }
2033
2034 appctx = objt_appctx(socket->s->si[0].end);
2035
2036 /* Check for connection established. */
2037 if (appctx->ctx.hlua.connected) {
2038 lua_pushinteger(L, 1);
2039 return 1;
2040 }
2041
2042 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2043 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002044 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002045 return 0;
2046}
2047
2048/* This function fail or initite the connection. */
2049__LJMP static int hlua_socket_connect(struct lua_State *L)
2050{
2051 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002052 int port;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002053 const char *ip;
2054 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002055 struct hlua *hlua;
2056 struct appctx *appctx;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002057
2058 MAY_LJMP(check_args(L, 3, "connect"));
2059
2060 /* Get args. */
2061 socket = MAY_LJMP(hlua_checksocket(L, 1));
2062 ip = MAY_LJMP(luaL_checkstring(L, 2));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002063 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002064
Willy Tarreau350f4872014-11-28 14:42:25 +01002065 conn = si_alloc_conn(&socket->s->si[1], 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002066 if (!conn)
2067 WILL_LJMP(luaL_error(L, "connect: internal error"));
2068
2069 /* Parse ip address. */
2070 conn->addr.to.ss_family = AF_UNSPEC;
2071 if (!str2ip2(ip, &conn->addr.to, 0))
2072 WILL_LJMP(luaL_error(L, "connect: cannot parse ip address '%s'", ip));
2073
2074 /* Set port. */
2075 if (conn->addr.to.ss_family == AF_INET)
2076 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2077 else if (conn->addr.to.ss_family == AF_INET6)
2078 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2079
2080 /* it is important not to call the wakeup function directly but to
2081 * pass through task_wakeup(), because this one knows how to apply
2082 * priorities to tasks.
2083 */
2084 task_wakeup(socket->s->task, TASK_WOKEN_INIT);
2085
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002086 hlua = hlua_gethlua(L);
2087 appctx = objt_appctx(socket->s->si[0].end);
2088 if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write))
2089 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002090 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002091
2092 return 0;
2093}
2094
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002095#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002096__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2097{
2098 struct hlua_socket *socket;
2099
2100 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2101 socket = MAY_LJMP(hlua_checksocket(L, 1));
2102 socket->s->target = &socket_ssl.obj_type;
2103 return MAY_LJMP(hlua_socket_connect(L));
2104}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002105#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002106
2107__LJMP static int hlua_socket_setoption(struct lua_State *L)
2108{
2109 return 0;
2110}
2111
2112__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2113{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002114 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002115 int tmout;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002116
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002117 MAY_LJMP(check_args(L, 2, "settimeout"));
2118
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002119 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002120 tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002121
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002122 socket->s->req.rto = tmout;
2123 socket->s->req.wto = tmout;
2124 socket->s->res.rto = tmout;
2125 socket->s->res.wto = tmout;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002126
2127 return 0;
2128}
2129
2130__LJMP static int hlua_socket_new(lua_State *L)
2131{
2132 struct hlua_socket *socket;
2133 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002134 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002135 struct stream *strm;
Willy Tarreaud420a972015-04-06 00:39:18 +02002136 struct task *task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002137
2138 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002139 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002140 hlua_pusherror(L, "socket: full stack");
2141 goto out_fail_conf;
2142 }
2143
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002144 /* Create the object: obj[0] = userdata. */
2145 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002146 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002147 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002148 memset(socket, 0, sizeof(*socket));
2149
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002150 /* Check if the various memory pools are intialized. */
Willy Tarreau87b09662015-04-03 00:22:06 +02002151 if (!pool2_stream || !pool2_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002152 hlua_pusherror(L, "socket: uninitialized pools.");
2153 goto out_fail_conf;
2154 }
2155
Willy Tarreau87b09662015-04-03 00:22:06 +02002156 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002157 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2158 lua_setmetatable(L, -2);
2159
Willy Tarreaud420a972015-04-06 00:39:18 +02002160 /* Create the applet context */
2161 appctx = appctx_new(&update_applet);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002162 if (!appctx) {
2163 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002164 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002165 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002166
Willy Tarreaud420a972015-04-06 00:39:18 +02002167 appctx->ctx.hlua.socket = socket;
2168 appctx->ctx.hlua.connected = 0;
2169 LIST_INIT(&appctx->ctx.hlua.wake_on_write);
2170 LIST_INIT(&appctx->ctx.hlua.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002171
Willy Tarreaud420a972015-04-06 00:39:18 +02002172 /* Now create a session, task and stream for this applet */
2173 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2174 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002175 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002176 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002177 }
2178
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002179 task = task_new();
2180 if (!task) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002181 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002182 goto out_fail_task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002183 }
Willy Tarreaud420a972015-04-06 00:39:18 +02002184 task->nice = 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002185
Willy Tarreau73b65ac2015-04-08 18:26:29 +02002186 strm = stream_new(sess, task, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002187 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002188 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002189 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002190 }
2191
Willy Tarreaud420a972015-04-06 00:39:18 +02002192 /* Configure an empty Lua for the stream. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002193 socket->s = strm;
2194 strm->hlua.T = NULL;
2195 strm->hlua.Tref = LUA_REFNIL;
2196 strm->hlua.Mref = LUA_REFNIL;
2197 strm->hlua.nargs = 0;
2198 strm->hlua.flags = 0;
2199 LIST_INIT(&strm->hlua.com);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002200
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002201 /* Configure "right" stream interface. this "si" is used to connect
2202 * and retrieve data from the server. The connection is initialized
2203 * with the "struct server".
2204 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002205 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002206
2207 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002208 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2209 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002210
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002211 /* Update statistics counters. */
2212 socket_proxy.feconn++; /* beconn will be increased later */
2213 jobs++;
2214 totalconn++;
2215
2216 /* Return yield waiting for connection. */
2217 return 1;
2218
Willy Tarreaud420a972015-04-06 00:39:18 +02002219 out_fail_stream:
2220 task_free(task);
2221 out_fail_task:
Willy Tarreau11c36242015-04-04 15:54:03 +02002222 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002223 out_fail_sess:
2224 appctx_free(appctx);
2225 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002226 WILL_LJMP(lua_error(L));
2227 return 0;
2228}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002229
2230/*
2231 *
2232 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002233 * Class Channel
2234 *
2235 *
2236 */
2237
2238/* Returns the struct hlua_channel join to the class channel in the
2239 * stack entry "ud" or throws an argument error.
2240 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002241__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002242{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002243 return (struct channel *)MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002244}
2245
Willy Tarreau47860ed2015-03-10 14:07:50 +01002246/* Pushes the channel onto the top of the stack. If the stask does not have a
2247 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002248 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002249static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002250{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002251 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002252 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002253 return 0;
2254
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002255 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002256 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002257 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002258
2259 /* Pop a class sesison metatable and affect it to the userdata. */
2260 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2261 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002262 return 1;
2263}
2264
2265/* Duplicate all the data present in the input channel and put it
2266 * in a string LUA variables. Returns -1 and push a nil value in
2267 * the stack if the channel is closed and all the data are consumed,
2268 * returns 0 if no data are available, otherwise it returns the length
2269 * of the builded string.
2270 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002271static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002272{
2273 char *blk1;
2274 char *blk2;
2275 int len1;
2276 int len2;
2277 int ret;
2278 luaL_Buffer b;
2279
Willy Tarreau47860ed2015-03-10 14:07:50 +01002280 ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002281 if (unlikely(ret == 0))
2282 return 0;
2283
2284 if (unlikely(ret < 0)) {
2285 lua_pushnil(L);
2286 return -1;
2287 }
2288
2289 luaL_buffinit(L, &b);
2290 luaL_addlstring(&b, blk1, len1);
2291 if (unlikely(ret == 2))
2292 luaL_addlstring(&b, blk2, len2);
2293 luaL_pushresult(&b);
2294
2295 if (unlikely(ret == 2))
2296 return len1 + len2;
2297 return len1;
2298}
2299
2300/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2301 * a yield. This function keep the data in the buffer.
2302 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002303__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002304{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002305 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002306
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002307 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2308
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002309 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002310 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002311 return 1;
2312}
2313
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002314/* Check arguments for the function "hlua_channel_dup_yield". */
2315__LJMP static int hlua_channel_dup(lua_State *L)
2316{
2317 MAY_LJMP(check_args(L, 1, "dup"));
2318 MAY_LJMP(hlua_checkchannel(L, 1));
2319 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2320}
2321
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002322/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2323 * a yield. This function consumes the data in the buffer. It returns
2324 * a string containing the data or a nil pointer if no data are available
2325 * and the channel is closed.
2326 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002327__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002328{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002329 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002330 int ret;
2331
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002332 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002333
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002334 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002335 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002336 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002337
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002338 if (unlikely(ret == -1))
2339 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002340
Willy Tarreau47860ed2015-03-10 14:07:50 +01002341 chn->buf->i -= ret;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002342 return 1;
2343}
2344
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002345/* Check arguments for the fucntion "hlua_channel_get_yield". */
2346__LJMP static int hlua_channel_get(lua_State *L)
2347{
2348 MAY_LJMP(check_args(L, 1, "get"));
2349 MAY_LJMP(hlua_checkchannel(L, 1));
2350 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2351}
2352
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002353/* This functions consumes and returns one line. If the channel is closed,
2354 * and the last data does not contains a final '\n', the data are returned
2355 * without the final '\n'. When no more data are avalaible, it returns nil
2356 * value.
2357 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002358__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002359{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002360 char *blk1;
2361 char *blk2;
2362 int len1;
2363 int len2;
2364 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002365 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002366 int ret;
2367 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002368
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002369 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2370
Willy Tarreau47860ed2015-03-10 14:07:50 +01002371 ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002372 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002373 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002374
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002375 if (ret == -1) {
2376 lua_pushnil(L);
2377 return 1;
2378 }
2379
2380 luaL_buffinit(L, &b);
2381 luaL_addlstring(&b, blk1, len1);
2382 len = len1;
2383 if (unlikely(ret == 2)) {
2384 luaL_addlstring(&b, blk2, len2);
2385 len += len2;
2386 }
2387 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002388 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002389 return 1;
2390}
2391
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002392/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2393__LJMP static int hlua_channel_getline(lua_State *L)
2394{
2395 MAY_LJMP(check_args(L, 1, "getline"));
2396 MAY_LJMP(hlua_checkchannel(L, 1));
2397 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2398}
2399
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002400/* This function takes a string as input, and append it at the
2401 * input side of channel. If the data is too big, but a space
2402 * is probably available after sending some data, the function
2403 * yield. If the data is bigger than the buffer, or if the
2404 * channel is closed, it returns -1. otherwise, it returns the
2405 * amount of data writed.
2406 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002407__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002408{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002409 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002410 size_t len;
2411 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2412 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2413 int ret;
2414 int max;
2415
Willy Tarreau47860ed2015-03-10 14:07:50 +01002416 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002417 if (max > len - l)
2418 max = len - l;
2419
Willy Tarreau47860ed2015-03-10 14:07:50 +01002420 ret = bi_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002421 if (ret == -2 || ret == -3) {
2422 lua_pushinteger(L, -1);
2423 return 1;
2424 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002425 if (ret == -1) {
2426 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002427 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002428 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002429 l += ret;
2430 lua_pop(L, 1);
2431 lua_pushinteger(L, l);
2432
Willy Tarreau47860ed2015-03-10 14:07:50 +01002433 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2434 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002435 /* There are no space avalaible, and the output buffer is empty.
2436 * in this case, we cannot add more data, so we cannot yield,
2437 * we return the amount of copyied data.
2438 */
2439 return 1;
2440 }
2441 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002442 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002443 return 1;
2444}
2445
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002446/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002447 * of the writed string, or -1 if the channel is closed or if the
2448 * buffer size is too little for the data.
2449 */
2450__LJMP static int hlua_channel_append(lua_State *L)
2451{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002452 size_t len;
2453
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002454 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002455 MAY_LJMP(hlua_checkchannel(L, 1));
2456 MAY_LJMP(luaL_checklstring(L, 2, &len));
2457 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002458 lua_pushinteger(L, 0);
2459
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002460 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002461}
2462
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002463/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002464 * his process by cleaning the buffer. The result is a replacement
2465 * of the current data. It returns the length of the writed string,
2466 * or -1 if the channel is closed or if the buffer size is too
2467 * little for the data.
2468 */
2469__LJMP static int hlua_channel_set(lua_State *L)
2470{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002471 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002472
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002473 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002474 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002475 lua_pushinteger(L, 0);
2476
Willy Tarreau47860ed2015-03-10 14:07:50 +01002477 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002478
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002479 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002480}
2481
2482/* Append data in the output side of the buffer. This data is immediatly
2483 * sent. The fcuntion returns the ammount of data writed. If the buffer
2484 * cannot contains the data, the function yield. The function returns -1
2485 * if the channel is closed.
2486 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002487__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002488{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002489 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002490 size_t len;
2491 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2492 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2493 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002494 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002495
Willy Tarreau47860ed2015-03-10 14:07:50 +01002496 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002497 lua_pushinteger(L, -1);
2498 return 1;
2499 }
2500
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002501 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2502 * the request buffer if its not required.
2503 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002504 if (chn->buf->size == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002505 if (!stream_alloc_recv_buffer(chn)) {
Willy Tarreau47860ed2015-03-10 14:07:50 +01002506 chn_prod(chn)->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002507 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002508 }
2509 }
2510
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002511 /* the writed data will be immediatly sent, so we can check
2512 * the avalaible space without taking in account the reserve.
2513 * The reserve is guaranted for the processing of incoming
2514 * data, because the buffer will be flushed.
2515 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002516 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002517
2518 /* If there are no space avalaible, and the output buffer is empty.
2519 * in this case, we cannot add more data, so we cannot yield,
2520 * we return the amount of copyied data.
2521 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002522 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002523 return 1;
2524
2525 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002526 if (max > len - l)
2527 max = len - l;
2528
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002529 /* The buffer avalaible size may be not contiguous. This test
2530 * detects a non contiguous buffer and realign it.
2531 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002532 if (bi_space_for_replace(chn->buf) < max)
2533 buffer_slow_realign(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002534
2535 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002536 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002537
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002538 /* buffer replace considers that the input part is filled.
2539 * so, I must forward these new data in the output part.
2540 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002541 b_adv(chn->buf, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002542
2543 l += max;
2544 lua_pop(L, 1);
2545 lua_pushinteger(L, l);
2546
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002547 /* If there are no space avalaible, and the output buffer is empty.
2548 * in this case, we cannot add more data, so we cannot yield,
2549 * we return the amount of copyied data.
2550 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002551 max = chn->buf->size - buffer_len(chn->buf);
2552 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002553 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002554
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002555 if (l < len) {
2556 /* If we are waiting for space in the response buffer, we
2557 * must set the flag WAKERESWR. This flag required the task
2558 * wake up if any activity is detected on the response buffer.
2559 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002560 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002561 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002562 else
2563 HLUA_SET_WAKEREQWR(hlua);
2564 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002565 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002566
2567 return 1;
2568}
2569
2570/* Just a wraper of "_hlua_channel_send". This wrapper permits
2571 * yield the LUA process, and resume it without checking the
2572 * input arguments.
2573 */
2574__LJMP static int hlua_channel_send(lua_State *L)
2575{
2576 MAY_LJMP(check_args(L, 2, "send"));
2577 lua_pushinteger(L, 0);
2578
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002579 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002580}
2581
2582/* This function forward and amount of butes. The data pass from
2583 * the input side of the buffer to the output side, and can be
2584 * forwarded. This function never fails.
2585 *
2586 * The Lua function takes an amount of bytes to be forwarded in
2587 * imput. It returns the number of bytes forwarded.
2588 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002589__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002590{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002591 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002592 int len;
2593 int l;
2594 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002595 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002596
2597 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2598 len = MAY_LJMP(luaL_checkinteger(L, 2));
2599 l = MAY_LJMP(luaL_checkinteger(L, -1));
2600
2601 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002602 if (max > chn->buf->i)
2603 max = chn->buf->i;
2604 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002605 l += max;
2606
2607 lua_pop(L, 1);
2608 lua_pushinteger(L, l);
2609
2610 /* Check if it miss bytes to forward. */
2611 if (l < len) {
2612 /* The the input channel or the output channel are closed, we
2613 * must return the amount of data forwarded.
2614 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002615 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002616 return 1;
2617
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002618 /* If we are waiting for space data in the response buffer, we
2619 * must set the flag WAKERESWR. This flag required the task
2620 * wake up if any activity is detected on the response buffer.
2621 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002622 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002623 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002624 else
2625 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002626
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002627 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002628 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002629 }
2630
2631 return 1;
2632}
2633
2634/* Just check the input and prepare the stack for the previous
2635 * function "hlua_channel_forward_yield"
2636 */
2637__LJMP static int hlua_channel_forward(lua_State *L)
2638{
2639 MAY_LJMP(check_args(L, 2, "forward"));
2640 MAY_LJMP(hlua_checkchannel(L, 1));
2641 MAY_LJMP(luaL_checkinteger(L, 2));
2642
2643 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002644 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002645}
2646
2647/* Just returns the number of bytes available in the input
2648 * side of the buffer. This function never fails.
2649 */
2650__LJMP static int hlua_channel_get_in_len(lua_State *L)
2651{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002652 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002653
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002654 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002655 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002656 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002657 return 1;
2658}
2659
2660/* Just returns the number of bytes available in the output
2661 * side of the buffer. This function never fails.
2662 */
2663__LJMP static int hlua_channel_get_out_len(lua_State *L)
2664{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002665 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002666
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002667 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002668 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002669 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002670 return 1;
2671}
2672
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002673/*
2674 *
2675 *
2676 * Class Fetches
2677 *
2678 *
2679 */
2680
2681/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002682 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002683 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002684__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002685{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002686 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002687}
2688
2689/* This function creates and push in the stack a fetch object according
2690 * with a current TXN.
2691 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002692static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002693{
Willy Tarreau7073c472015-04-06 11:15:40 +02002694 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002695
2696 /* Check stack size. */
2697 if (!lua_checkstack(L, 3))
2698 return 0;
2699
2700 /* Create the object: obj[0] = userdata.
2701 * Note that the base of the Fetches object is the
2702 * transaction object.
2703 */
2704 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002705 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002706 lua_rawseti(L, -2, 0);
2707
Willy Tarreau7073c472015-04-06 11:15:40 +02002708 hsmp->s = txn->s;
2709 hsmp->p = txn->p;
Willy Tarreau7073c472015-04-06 11:15:40 +02002710 hsmp->stringsafe = stringsafe;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002711
2712 /* Pop a class sesison metatable and affect it to the userdata. */
2713 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
2714 lua_setmetatable(L, -2);
2715
2716 return 1;
2717}
2718
2719/* This function is an LUA binding. It is called with each sample-fetch.
2720 * It uses closure argument to store the associated sample-fetch. It
2721 * returns only one argument or throws an error. An error is thrown
2722 * only if an error is encountered during the argument parsing. If
2723 * the "sample-fetch" function fails, nil is returned.
2724 */
2725__LJMP static int hlua_run_sample_fetch(lua_State *L)
2726{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002727 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01002728 struct sample_fetch *f;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002729 struct arg args[ARGM_NBARGS + 1];
2730 int i;
2731 struct sample smp;
2732
2733 /* Get closure arguments. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002734 f = (struct sample_fetch *)lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002735
2736 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002737 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002738
2739 /* Get extra arguments. */
2740 for (i = 0; i < lua_gettop(L) - 1; i++) {
2741 if (i >= ARGM_NBARGS)
2742 break;
2743 hlua_lua2arg(L, i + 2, &args[i]);
2744 }
2745 args[i].type = ARGT_STOP;
2746
2747 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002748 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002749
2750 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01002751 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002752 lua_pushfstring(L, "error in arguments");
2753 WILL_LJMP(lua_error(L));
2754 }
2755
2756 /* Initialise the sample. */
2757 memset(&smp, 0, sizeof(smp));
2758
2759 /* Run the sample fetch process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02002760 smp.px = hsmp->p;
2761 smp.sess = hsmp->s->sess;
2762 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02002763 smp.opt = 0;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002764 if (!f->process(args, &smp, f->kw, f->private)) {
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002765 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002766 lua_pushstring(L, "");
2767 else
2768 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002769 return 1;
2770 }
2771
2772 /* Convert the returned sample in lua value. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002773 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002774 hlua_smp2lua_str(L, &smp);
2775 else
2776 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002777 return 1;
2778}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002779
2780/*
2781 *
2782 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002783 * Class Converters
2784 *
2785 *
2786 */
2787
2788/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002789 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002790 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002791__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002792{
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002793 return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002794}
2795
2796/* This function creates and push in the stack a Converters object
2797 * according with a current TXN.
2798 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002799static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, int stringsafe)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002800{
Willy Tarreau7073c472015-04-06 11:15:40 +02002801 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002802
2803 /* Check stack size. */
2804 if (!lua_checkstack(L, 3))
2805 return 0;
2806
2807 /* Create the object: obj[0] = userdata.
2808 * Note that the base of the Converters object is the
2809 * same than the TXN object.
2810 */
2811 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002812 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002813 lua_rawseti(L, -2, 0);
2814
Willy Tarreau7073c472015-04-06 11:15:40 +02002815 hsmp->s = txn->s;
2816 hsmp->p = txn->p;
Willy Tarreau7073c472015-04-06 11:15:40 +02002817 hsmp->stringsafe = stringsafe;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002818
Willy Tarreau87b09662015-04-03 00:22:06 +02002819 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002820 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
2821 lua_setmetatable(L, -2);
2822
2823 return 1;
2824}
2825
2826/* This function is an LUA binding. It is called with each converter.
2827 * It uses closure argument to store the associated converter. It
2828 * returns only one argument or throws an error. An error is thrown
2829 * only if an error is encountered during the argument parsing. If
2830 * the converter function function fails, nil is returned.
2831 */
2832__LJMP static int hlua_run_sample_conv(lua_State *L)
2833{
Willy Tarreauda5f1082015-04-06 11:17:13 +02002834 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002835 struct sample_conv *conv;
2836 struct arg args[ARGM_NBARGS + 1];
2837 int i;
2838 struct sample smp;
2839
2840 /* Get closure arguments. */
2841 conv = (struct sample_conv *)lua_touserdata(L, lua_upvalueindex(1));
2842
2843 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02002844 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002845
2846 /* Get extra arguments. */
2847 for (i = 0; i < lua_gettop(L) - 2; i++) {
2848 if (i >= ARGM_NBARGS)
2849 break;
2850 hlua_lua2arg(L, i + 3, &args[i]);
2851 }
2852 args[i].type = ARGT_STOP;
2853
2854 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02002855 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002856
2857 /* Run the special args checker. */
2858 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
2859 hlua_pusherror(L, "error in arguments");
2860 WILL_LJMP(lua_error(L));
2861 }
2862
2863 /* Initialise the sample. */
2864 if (!hlua_lua2smp(L, 2, &smp)) {
2865 hlua_pusherror(L, "error in the input argument");
2866 WILL_LJMP(lua_error(L));
2867 }
2868
2869 /* Apply expected cast. */
2870 if (!sample_casts[smp.type][conv->in_type]) {
2871 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
2872 smp_to_type[smp.type], smp_to_type[conv->in_type]);
2873 WILL_LJMP(lua_error(L));
2874 }
2875 if (sample_casts[smp.type][conv->in_type] != c_none &&
2876 !sample_casts[smp.type][conv->in_type](&smp)) {
2877 hlua_pusherror(L, "error during the input argument casting");
2878 WILL_LJMP(lua_error(L));
2879 }
2880
2881 /* Run the sample conversion process. */
Thierry FOURNIER6879ad32015-05-11 11:54:58 +02002882 smp.px = hsmp->p;
2883 smp.sess = hsmp->s->sess;
2884 smp.strm = hsmp->s;
Thierry FOURNIER1d33b882015-05-11 15:25:29 +02002885 smp.opt = 0;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002886 if (!conv->process(args, &smp, conv->private)) {
Willy Tarreauda5f1082015-04-06 11:17:13 +02002887 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002888 lua_pushstring(L, "");
2889 else
2890 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002891 return 1;
2892 }
2893
2894 /* Convert the returned sample in lua value. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02002895 if (hsmp->stringsafe)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002896 hlua_smp2lua_str(L, &smp);
2897 else
2898 hlua_smp2lua(L, &smp);
2899 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01002900}
2901
2902/*
2903 *
2904 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002905 * Class HTTP
2906 *
2907 *
2908 */
2909
2910/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002911 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002912 */
2913__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
2914{
2915 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
2916}
2917
2918/* This function creates and push in the stack a HTTP object
2919 * according with a current TXN.
2920 */
2921static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
2922{
Willy Tarreau9a8ad862015-04-06 11:14:06 +02002923 struct hlua_txn *htxn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002924
2925 /* Check stack size. */
2926 if (!lua_checkstack(L, 3))
2927 return 0;
2928
2929 /* Create the object: obj[0] = userdata.
2930 * Note that the base of the Converters object is the
2931 * same than the TXN object.
2932 */
2933 lua_newtable(L);
Willy Tarreau9a8ad862015-04-06 11:14:06 +02002934 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002935 lua_rawseti(L, -2, 0);
2936
Willy Tarreau9a8ad862015-04-06 11:14:06 +02002937 htxn->s = txn->s;
2938 htxn->p = txn->p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002939
Willy Tarreau87b09662015-04-03 00:22:06 +02002940 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002941 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
2942 lua_setmetatable(L, -2);
2943
2944 return 1;
2945}
2946
2947/* This function creates ans returns an array of HTTP headers.
2948 * This function does not fails. It is used as wrapper with the
2949 * 2 following functions.
2950 */
2951__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
2952{
2953 const char *cur_ptr, *cur_next, *p;
2954 int old_idx, cur_idx;
2955 struct hdr_idx_elem *cur_hdr;
2956 const char *hn, *hv;
2957 int hnl, hvl;
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01002958 int type;
2959 const char *in;
2960 char *out;
2961 int len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002962
2963 /* Create the table. */
2964 lua_newtable(L);
2965
Willy Tarreaueee5b512015-04-03 23:46:31 +02002966 if (!htxn->s->txn)
2967 return 1;
2968
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002969 /* Build array of headers. */
2970 old_idx = 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002971 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002972
2973 while (1) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002974 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002975 if (!cur_idx)
2976 break;
2977 old_idx = cur_idx;
2978
Willy Tarreaueee5b512015-04-03 23:46:31 +02002979 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002980 cur_ptr = cur_next;
2981 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
2982
2983 /* Now we have one full header at cur_ptr of len cur_hdr->len,
2984 * and the next header starts at cur_next. We'll check
2985 * this header in the list as well as against the default
2986 * rule.
2987 */
2988
2989 /* look for ': *'. */
2990 hn = cur_ptr;
2991 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
2992 if (p >= cur_ptr+cur_hdr->len)
2993 continue;
2994 hnl = p - hn;
2995 p++;
2996 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
2997 p++;
2998 if (p >= cur_ptr+cur_hdr->len)
2999 continue;
3000 hv = p;
3001 hvl = cur_ptr+cur_hdr->len-p;
3002
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003003 /* Lowercase the key. Don't check the size of trash, it have
3004 * the size of one buffer and the input data contains in one
3005 * buffer.
3006 */
3007 out = trash.str;
3008 for (in=hn; in<hn+hnl; in++, out++)
3009 *out = tolower(*in);
3010 *out = '\0';
3011
3012 /* Check for existing entry:
3013 * assume that the table is on the top of the stack, and
3014 * push the key in the stack, the function lua_gettable()
3015 * perform the lookup.
3016 */
3017 lua_pushlstring(L, trash.str, hnl);
3018 lua_gettable(L, -2);
3019 type = lua_type(L, -1);
3020
3021 switch (type) {
3022 case LUA_TNIL:
3023 /* Table not found, create it. */
3024 lua_pop(L, 1); /* remove the nil value. */
3025 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
3026 lua_newtable(L); /* create and push empty table. */
3027 lua_pushlstring(L, hv, hvl); /* push header value. */
3028 lua_rawseti(L, -2, 0); /* index header value (pop it). */
3029 lua_rawset(L, -3); /* index new table with header name (pop the values). */
3030 break;
3031
3032 case LUA_TTABLE:
3033 /* Entry found: push the value in the table. */
3034 len = lua_rawlen(L, -1);
3035 lua_pushlstring(L, hv, hvl); /* push header value. */
3036 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
3037 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
3038 break;
3039
3040 default:
3041 /* Other cases are errors. */
3042 hlua_pusherror(L, "internal error during the parsing of headers.");
3043 WILL_LJMP(lua_error(L));
3044 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003045 }
3046
3047 return 1;
3048}
3049
3050__LJMP static int hlua_http_req_get_headers(lua_State *L)
3051{
3052 struct hlua_txn *htxn;
3053
3054 MAY_LJMP(check_args(L, 1, "req_get_headers"));
3055 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3056
Willy Tarreaueee5b512015-04-03 23:46:31 +02003057 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003058}
3059
3060__LJMP static int hlua_http_res_get_headers(lua_State *L)
3061{
3062 struct hlua_txn *htxn;
3063
3064 MAY_LJMP(check_args(L, 1, "res_get_headers"));
3065 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3066
Willy Tarreaueee5b512015-04-03 23:46:31 +02003067 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003068}
3069
3070/* This function replace full header, or just a value in
3071 * the request or in the response. It is a wrapper fir the
3072 * 4 following functions.
3073 */
3074__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
3075 struct http_msg *msg, int action)
3076{
3077 size_t name_len;
3078 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3079 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
3080 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
3081 struct my_regex re;
3082
3083 if (!regex_comp(reg, &re, 1, 1, NULL))
3084 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
3085
3086 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
3087 regex_free(&re);
3088 return 0;
3089}
3090
3091__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
3092{
3093 struct hlua_txn *htxn;
3094
3095 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3096 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3097
Willy Tarreaueee5b512015-04-03 23:46:31 +02003098 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 +01003099}
3100
3101__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
3102{
3103 struct hlua_txn *htxn;
3104
3105 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
3106 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3107
Willy Tarreaueee5b512015-04-03 23:46:31 +02003108 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 +01003109}
3110
3111__LJMP static int hlua_http_req_rep_val(lua_State *L)
3112{
3113 struct hlua_txn *htxn;
3114
3115 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
3116 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3117
Willy Tarreaueee5b512015-04-03 23:46:31 +02003118 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 +01003119}
3120
3121__LJMP static int hlua_http_res_rep_val(lua_State *L)
3122{
3123 struct hlua_txn *htxn;
3124
3125 MAY_LJMP(check_args(L, 4, "res_rep_val"));
3126 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3127
Willy Tarreaueee5b512015-04-03 23:46:31 +02003128 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 +01003129}
3130
3131/* This function deletes all the occurences of an header.
3132 * It is a wrapper for the 2 following functions.
3133 */
3134__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3135{
3136 size_t len;
3137 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3138 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003139 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003140
3141 ctx.idx = 0;
3142 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
3143 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3144 return 0;
3145}
3146
3147__LJMP static int hlua_http_req_del_hdr(lua_State *L)
3148{
3149 struct hlua_txn *htxn;
3150
3151 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3152 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3153
Willy Tarreaueee5b512015-04-03 23:46:31 +02003154 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003155}
3156
3157__LJMP static int hlua_http_res_del_hdr(lua_State *L)
3158{
3159 struct hlua_txn *htxn;
3160
3161 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
3162 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3163
Willy Tarreaueee5b512015-04-03 23:46:31 +02003164 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003165}
3166
3167/* This function adds an header. It is a wrapper used by
3168 * the 2 following functions.
3169 */
3170__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
3171{
3172 size_t name_len;
3173 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
3174 size_t value_len;
3175 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
3176 char *p;
3177
3178 /* Check length. */
3179 trash.len = value_len + name_len + 2;
3180 if (trash.len > trash.size)
3181 return 0;
3182
3183 /* Creates the header string. */
3184 p = trash.str;
3185 memcpy(p, name, name_len);
3186 p += name_len;
3187 *p = ':';
3188 p++;
3189 *p = ' ';
3190 p++;
3191 memcpy(p, value, value_len);
3192
Willy Tarreaueee5b512015-04-03 23:46:31 +02003193 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003194 trash.str, trash.len) != 0);
3195
3196 return 0;
3197}
3198
3199__LJMP static int hlua_http_req_add_hdr(lua_State *L)
3200{
3201 struct hlua_txn *htxn;
3202
3203 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
3204 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3205
Willy Tarreaueee5b512015-04-03 23:46:31 +02003206 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003207}
3208
3209__LJMP static int hlua_http_res_add_hdr(lua_State *L)
3210{
3211 struct hlua_txn *htxn;
3212
3213 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
3214 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3215
Willy Tarreaueee5b512015-04-03 23:46:31 +02003216 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003217}
3218
3219static int hlua_http_req_set_hdr(lua_State *L)
3220{
3221 struct hlua_txn *htxn;
3222
3223 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
3224 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3225
Willy Tarreaueee5b512015-04-03 23:46:31 +02003226 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
3227 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003228}
3229
3230static int hlua_http_res_set_hdr(lua_State *L)
3231{
3232 struct hlua_txn *htxn;
3233
3234 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
3235 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
3236
Willy Tarreaueee5b512015-04-03 23:46:31 +02003237 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
3238 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003239}
3240
3241/* This function set the method. */
3242static int hlua_http_req_set_meth(lua_State *L)
3243{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003244 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003245 size_t name_len;
3246 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003247
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003248 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003249 return 1;
3250}
3251
3252/* This function set the method. */
3253static int hlua_http_req_set_path(lua_State *L)
3254{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003255 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003256 size_t name_len;
3257 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003258 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003259 return 1;
3260}
3261
3262/* This function set the query-string. */
3263static int hlua_http_req_set_query(lua_State *L)
3264{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003265 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003266 size_t name_len;
3267 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003268
3269 /* Check length. */
3270 if (name_len > trash.size - 1) {
3271 lua_pushboolean(L, 0);
3272 return 1;
3273 }
3274
3275 /* Add the mark question as prefix. */
3276 chunk_reset(&trash);
3277 trash.str[trash.len++] = '?';
3278 memcpy(trash.str + trash.len, name, name_len);
3279 trash.len += name_len;
3280
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003281 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003282 return 1;
3283}
3284
3285/* This function set the uri. */
3286static int hlua_http_req_set_uri(lua_State *L)
3287{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02003288 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003289 size_t name_len;
3290 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003291
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003292 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003293 return 1;
3294}
3295
3296/*
3297 *
3298 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003299 * Class TXN
3300 *
3301 *
3302 */
3303
3304/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003305 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003306 */
3307__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
3308{
3309 return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
3310}
3311
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02003312__LJMP static int hlua_set_var(lua_State *L)
3313{
3314 struct hlua_txn *htxn;
3315 const char *name;
3316 size_t len;
3317 struct sample smp;
3318
3319 MAY_LJMP(check_args(L, 3, "set_var"));
3320
3321 /* It is useles to retrieve the stream, but this function
3322 * runs only in a stream context.
3323 */
3324 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3325 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3326
3327 /* Converts the third argument in a sample. */
3328 hlua_lua2smp(L, 3, &smp);
3329
3330 /* Store the sample in a variable. */
3331 vars_set_by_name(name, len, htxn->s, &smp);
3332 return 0;
3333}
3334
3335__LJMP static int hlua_get_var(lua_State *L)
3336{
3337 struct hlua_txn *htxn;
3338 const char *name;
3339 size_t len;
3340 struct sample smp;
3341
3342 MAY_LJMP(check_args(L, 2, "get_var"));
3343
3344 /* It is useles to retrieve the stream, but this function
3345 * runs only in a stream context.
3346 */
3347 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3348 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3349
3350 if (!vars_get_by_name(name, len, htxn->s, &smp)) {
3351 lua_pushnil(L);
3352 return 1;
3353 }
3354
3355 return hlua_smp2lua(L, &smp);
3356}
3357
Willy Tarreau59551662015-03-10 14:23:13 +01003358__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003359{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003360 struct hlua *hlua;
3361
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003362 MAY_LJMP(check_args(L, 2, "set_priv"));
3363
Willy Tarreau87b09662015-04-03 00:22:06 +02003364 /* It is useles to retrieve the stream, but this function
3365 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003366 */
3367 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003368 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003369
3370 /* Remove previous value. */
3371 if (hlua->Mref != -1)
3372 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
3373
3374 /* Get and store new value. */
3375 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3376 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3377
3378 return 0;
3379}
3380
Willy Tarreau59551662015-03-10 14:23:13 +01003381__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003382{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003383 struct hlua *hlua;
3384
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003385 MAY_LJMP(check_args(L, 1, "get_priv"));
3386
Willy Tarreau87b09662015-04-03 00:22:06 +02003387 /* It is useles to retrieve the stream, but this function
3388 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003389 */
3390 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003391 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01003392
3393 /* Push configuration index in the stack. */
3394 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3395
3396 return 1;
3397}
3398
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003399/* Create stack entry containing a class TXN. This function
3400 * return 0 if the stack does not contains free slots,
3401 * otherwise it returns 1.
3402 */
Willy Tarreau15e91e12015-04-04 00:52:09 +02003403static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003404{
Willy Tarreaude491382015-04-06 11:04:28 +02003405 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003406
3407 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003408 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003409 return 0;
3410
3411 /* NOTE: The allocation never fails. The failure
3412 * throw an error, and the function never returns.
3413 * if the throw is not avalaible, the process is aborted.
3414 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003415 /* Create the object: obj[0] = userdata. */
3416 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02003417 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01003418 lua_rawseti(L, -2, 0);
3419
Willy Tarreaude491382015-04-06 11:04:28 +02003420 htxn->s = s;
3421 htxn->p = p;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003422
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003423 /* Create the "f" field that contains a list of fetches. */
3424 lua_pushstring(L, "f");
Willy Tarreaude491382015-04-06 11:04:28 +02003425 if (!hlua_fetches_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003426 return 0;
3427 lua_settable(L, -3);
3428
3429 /* Create the "sf" field that contains a list of stringsafe fetches. */
3430 lua_pushstring(L, "sf");
Willy Tarreaude491382015-04-06 11:04:28 +02003431 if (!hlua_fetches_new(L, htxn, 1))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003432 return 0;
3433 lua_settable(L, -3);
3434
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003435 /* Create the "c" field that contains a list of converters. */
3436 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02003437 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003438 return 0;
3439 lua_settable(L, -3);
3440
3441 /* Create the "sc" field that contains a list of stringsafe converters. */
3442 lua_pushstring(L, "sc");
Willy Tarreaude491382015-04-06 11:04:28 +02003443 if (!hlua_converters_new(L, htxn, 1))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003444 return 0;
3445 lua_settable(L, -3);
3446
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003447 /* Create the "req" field that contains the request channel object. */
3448 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003449 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003450 return 0;
3451 lua_settable(L, -3);
3452
3453 /* Create the "res" field that contains the response channel object. */
3454 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01003455 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01003456 return 0;
3457 lua_settable(L, -3);
3458
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003459 /* Creates the HTTP object is the current proxy allows http. */
3460 lua_pushstring(L, "http");
3461 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02003462 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003463 return 0;
3464 }
3465 else
3466 lua_pushnil(L);
3467 lua_settable(L, -3);
3468
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01003469 /* Pop a class sesison metatable and affect it to the userdata. */
3470 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
3471 lua_setmetatable(L, -2);
3472
3473 return 1;
3474}
3475
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003476__LJMP static int hlua_txn_deflog(lua_State *L)
3477{
3478 const char *msg;
3479 struct hlua_txn *htxn;
3480
3481 MAY_LJMP(check_args(L, 2, "deflog"));
3482 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3483 msg = MAY_LJMP(luaL_checkstring(L, 2));
3484
3485 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
3486 return 0;
3487}
3488
3489__LJMP static int hlua_txn_log(lua_State *L)
3490{
3491 int level;
3492 const char *msg;
3493 struct hlua_txn *htxn;
3494
3495 MAY_LJMP(check_args(L, 3, "log"));
3496 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3497 level = MAY_LJMP(luaL_checkinteger(L, 2));
3498 msg = MAY_LJMP(luaL_checkstring(L, 3));
3499
3500 if (level < 0 || level >= NB_LOG_LEVELS)
3501 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3502
3503 hlua_sendlog(htxn->s->be, level, msg);
3504 return 0;
3505}
3506
3507__LJMP static int hlua_txn_log_debug(lua_State *L)
3508{
3509 const char *msg;
3510 struct hlua_txn *htxn;
3511
3512 MAY_LJMP(check_args(L, 2, "Debug"));
3513 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3514 msg = MAY_LJMP(luaL_checkstring(L, 2));
3515 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
3516 return 0;
3517}
3518
3519__LJMP static int hlua_txn_log_info(lua_State *L)
3520{
3521 const char *msg;
3522 struct hlua_txn *htxn;
3523
3524 MAY_LJMP(check_args(L, 2, "Info"));
3525 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3526 msg = MAY_LJMP(luaL_checkstring(L, 2));
3527 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
3528 return 0;
3529}
3530
3531__LJMP static int hlua_txn_log_warning(lua_State *L)
3532{
3533 const char *msg;
3534 struct hlua_txn *htxn;
3535
3536 MAY_LJMP(check_args(L, 2, "Warning"));
3537 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3538 msg = MAY_LJMP(luaL_checkstring(L, 2));
3539 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
3540 return 0;
3541}
3542
3543__LJMP static int hlua_txn_log_alert(lua_State *L)
3544{
3545 const char *msg;
3546 struct hlua_txn *htxn;
3547
3548 MAY_LJMP(check_args(L, 2, "Alert"));
3549 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3550 msg = MAY_LJMP(luaL_checkstring(L, 2));
3551 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
3552 return 0;
3553}
3554
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003555__LJMP static int hlua_txn_set_loglevel(lua_State *L)
3556{
3557 struct hlua_txn *htxn;
3558 int ll;
3559
3560 MAY_LJMP(check_args(L, 2, "set_loglevel"));
3561 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3562 ll = MAY_LJMP(luaL_checkinteger(L, 2));
3563
3564 if (ll < 0 || ll > 7)
3565 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
3566
3567 htxn->s->logs.level = ll;
3568 return 0;
3569}
3570
3571__LJMP static int hlua_txn_set_tos(lua_State *L)
3572{
3573 struct hlua_txn *htxn;
3574 struct connection *cli_conn;
3575 int tos;
3576
3577 MAY_LJMP(check_args(L, 2, "set_tos"));
3578 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3579 tos = MAY_LJMP(luaL_checkinteger(L, 2));
3580
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003581 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003582 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, tos);
3583
3584 return 0;
3585}
3586
3587__LJMP static int hlua_txn_set_mark(lua_State *L)
3588{
3589#ifdef SO_MARK
3590 struct hlua_txn *htxn;
3591 struct connection *cli_conn;
3592 int mark;
3593
3594 MAY_LJMP(check_args(L, 2, "set_mark"));
3595 htxn = MAY_LJMP(hlua_checktxn(L, 1));
3596 mark = MAY_LJMP(luaL_checkinteger(L, 2));
3597
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003598 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau07081fe2015-04-06 10:59:20 +02003599 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01003600#endif
3601 return 0;
3602}
3603
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003604/* This function is an Lua binding that send pending data
3605 * to the client, and close the stream interface.
3606 */
3607__LJMP static int hlua_txn_close(lua_State *L)
3608{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003609 struct hlua_txn *htxn;
Willy Tarreau81389672015-03-10 12:03:52 +01003610 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003611
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003612 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003613 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003614
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003615 ic = &htxn->s->req;
3616 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01003617
3618 channel_abort(ic);
3619 channel_auto_close(ic);
3620 channel_erase(ic);
3621 channel_auto_read(oc);
3622 channel_auto_close(oc);
3623 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003624
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01003625 return 0;
3626}
3627
3628__LJMP static int hlua_log(lua_State *L)
3629{
3630 int level;
3631 const char *msg;
3632
3633 MAY_LJMP(check_args(L, 2, "log"));
3634 level = MAY_LJMP(luaL_checkinteger(L, 1));
3635 msg = MAY_LJMP(luaL_checkstring(L, 2));
3636
3637 if (level < 0 || level >= NB_LOG_LEVELS)
3638 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
3639
3640 hlua_sendlog(NULL, level, msg);
3641 return 0;
3642}
3643
3644__LJMP static int hlua_log_debug(lua_State *L)
3645{
3646 const char *msg;
3647
3648 MAY_LJMP(check_args(L, 1, "debug"));
3649 msg = MAY_LJMP(luaL_checkstring(L, 1));
3650 hlua_sendlog(NULL, LOG_DEBUG, msg);
3651 return 0;
3652}
3653
3654__LJMP static int hlua_log_info(lua_State *L)
3655{
3656 const char *msg;
3657
3658 MAY_LJMP(check_args(L, 1, "info"));
3659 msg = MAY_LJMP(luaL_checkstring(L, 1));
3660 hlua_sendlog(NULL, LOG_INFO, msg);
3661 return 0;
3662}
3663
3664__LJMP static int hlua_log_warning(lua_State *L)
3665{
3666 const char *msg;
3667
3668 MAY_LJMP(check_args(L, 1, "warning"));
3669 msg = MAY_LJMP(luaL_checkstring(L, 1));
3670 hlua_sendlog(NULL, LOG_WARNING, msg);
3671 return 0;
3672}
3673
3674__LJMP static int hlua_log_alert(lua_State *L)
3675{
3676 const char *msg;
3677
3678 MAY_LJMP(check_args(L, 1, "alert"));
3679 msg = MAY_LJMP(luaL_checkstring(L, 1));
3680 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01003681 return 0;
3682}
3683
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003684__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003685{
3686 int wakeup_ms = lua_tointeger(L, -1);
3687 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003688 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003689 return 0;
3690}
3691
3692__LJMP static int hlua_sleep(lua_State *L)
3693{
3694 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003695 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003696
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003697 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003698
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003699 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003700 wakeup_ms = tick_add(now_ms, delay);
3701 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003702
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003703 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3704 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003705}
3706
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003707__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003708{
3709 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003710 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003711
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003712 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003713
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003714 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003715 wakeup_ms = tick_add(now_ms, delay);
3716 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003717
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003718 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
3719 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01003720}
3721
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003722/* This functionis an LUA binding. it permits to give back
3723 * the hand at the HAProxy scheduler. It is used when the
3724 * LUA processing consumes a lot of time.
3725 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003726__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003727{
3728 return 0;
3729}
3730
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003731__LJMP static int hlua_yield(lua_State *L)
3732{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01003733 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
3734 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01003735}
3736
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003737/* This function change the nice of the currently executed
3738 * task. It is used set low or high priority at the current
3739 * task.
3740 */
Willy Tarreau59551662015-03-10 14:23:13 +01003741__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003742{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003743 struct hlua *hlua;
3744 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003745
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003746 MAY_LJMP(check_args(L, 1, "set_nice"));
3747 hlua = hlua_gethlua(L);
3748 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003749
3750 /* If he task is not set, I'm in a start mode. */
3751 if (!hlua || !hlua->task)
3752 return 0;
3753
3754 if (nice < -1024)
3755 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003756 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01003757 nice = 1024;
3758
3759 hlua->task->nice = nice;
3760 return 0;
3761}
3762
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003763/* This function is used as a calback of a task. It is called by the
3764 * HAProxy task subsystem when the task is awaked. The LUA runtime can
3765 * return an E_AGAIN signal, the emmiter of this signal must set a
3766 * signal to wake the task.
3767 */
3768static struct task *hlua_process_task(struct task *task)
3769{
3770 struct hlua *hlua = task->context;
3771 enum hlua_exec status;
3772
3773 /* We need to remove the task from the wait queue before executing
3774 * the Lua code because we don't know if it needs to wait for
3775 * another timer or not in the case of E_AGAIN.
3776 */
3777 task_delete(task);
3778
Thierry FOURNIERbd413492015-03-03 16:52:26 +01003779 /* If it is the first call to the task, we must initialize the
3780 * execution timeouts.
3781 */
3782 if (!HLUA_IS_RUNNING(hlua))
3783 hlua->expire = tick_add(now_ms, hlua_timeout_task);
3784
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003785 /* Execute the Lua code. */
3786 status = hlua_ctx_resume(hlua, 1);
3787
3788 switch (status) {
3789 /* finished or yield */
3790 case HLUA_E_OK:
3791 hlua_ctx_destroy(hlua);
3792 task_delete(task);
3793 task_free(task);
3794 break;
3795
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01003796 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
3797 if (hlua->wake_time != TICK_ETERNITY)
3798 task_schedule(task, hlua->wake_time);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003799 break;
3800
3801 /* finished with error. */
3802 case HLUA_E_ERRMSG:
3803 send_log(NULL, LOG_ERR, "Lua task: %s.", lua_tostring(hlua->T, -1));
3804 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3805 Alert("Lua task: %s.\n", lua_tostring(hlua->T, -1));
3806 hlua_ctx_destroy(hlua);
3807 task_delete(task);
3808 task_free(task);
3809 break;
3810
3811 case HLUA_E_ERR:
3812 default:
3813 send_log(NULL, LOG_ERR, "Lua task: unknown error.");
3814 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3815 Alert("Lua task: unknown error.\n");
3816 hlua_ctx_destroy(hlua);
3817 task_delete(task);
3818 task_free(task);
3819 break;
3820 }
3821 return NULL;
3822}
3823
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01003824/* This function is an LUA binding that register LUA function to be
3825 * executed after the HAProxy configuration parsing and before the
3826 * HAProxy scheduler starts. This function expect only one LUA
3827 * argument that is a function. This function returns nothing, but
3828 * throws if an error is encountered.
3829 */
3830__LJMP static int hlua_register_init(lua_State *L)
3831{
3832 struct hlua_init_function *init;
3833 int ref;
3834
3835 MAY_LJMP(check_args(L, 1, "register_init"));
3836
3837 ref = MAY_LJMP(hlua_checkfunction(L, 1));
3838
3839 init = malloc(sizeof(*init));
3840 if (!init)
3841 WILL_LJMP(luaL_error(L, "lua out of memory error."));
3842
3843 init->function_ref = ref;
3844 LIST_ADDQ(&hlua_init_functions, &init->l);
3845 return 0;
3846}
3847
Thierry FOURNIER24f33532015-01-23 12:13:00 +01003848/* This functio is an LUA binding. It permits to register a task
3849 * executed in parallel of the main HAroxy activity. The task is
3850 * created and it is set in the HAProxy scheduler. It can be called
3851 * from the "init" section, "post init" or during the runtime.
3852 *
3853 * Lua prototype:
3854 *
3855 * <none> core.register_task(<function>)
3856 */
3857static int hlua_register_task(lua_State *L)
3858{
3859 struct hlua *hlua;
3860 struct task *task;
3861 int ref;
3862
3863 MAY_LJMP(check_args(L, 1, "register_task"));
3864
3865 ref = MAY_LJMP(hlua_checkfunction(L, 1));
3866
3867 hlua = malloc(sizeof(*hlua));
3868 if (!hlua)
3869 WILL_LJMP(luaL_error(L, "lua out of memory error."));
3870
3871 task = task_new();
3872 task->context = hlua;
3873 task->process = hlua_process_task;
3874
3875 if (!hlua_ctx_init(hlua, task))
3876 WILL_LJMP(luaL_error(L, "lua out of memory error."));
3877
3878 /* Restore the function in the stack. */
3879 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
3880 hlua->nargs = 0;
3881
3882 /* Schedule task. */
3883 task_schedule(task, now_ms);
3884
3885 return 0;
3886}
3887
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003888/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
3889 * doesn't allow "yield" functions because the HAProxy engine cannot
3890 * resume converters.
3891 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003892static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003893{
3894 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003895 struct stream *stream = smp->strm;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003896
Willy Tarreau87b09662015-04-03 00:22:06 +02003897 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01003898 * Lua context can be not initialized. This behavior
3899 * permits to save performances because a systematic
3900 * Lua initialization cause 5% performances loss.
3901 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003902 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
3903 send_log(stream->be, LOG_ERR, "Lua converter '%s': can't initialize Lua context.", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01003904 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3905 Alert("Lua converter '%s': can't initialize Lua context.\n", fcn->name);
3906 return 0;
3907 }
3908
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003909 /* If it is the first run, initialize the data for the call. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003910 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003911 /* Check stack available size. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003912 if (!lua_checkstack(stream->hlua.T, 1)) {
3913 send_log(stream->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003914 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3915 Alert("Lua converter '%s': full stack.\n", fcn->name);
3916 return 0;
3917 }
3918
3919 /* Restore the function in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003920 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003921
3922 /* convert input sample and pust-it in the stack. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003923 if (!lua_checkstack(stream->hlua.T, 1)) {
3924 send_log(stream->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003925 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3926 Alert("Lua converter '%s': full stack.\n", fcn->name);
3927 return 0;
3928 }
Willy Tarreau87b09662015-04-03 00:22:06 +02003929 hlua_smp2lua(stream->hlua.T, smp);
3930 stream->hlua.nargs = 2;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003931
3932 /* push keywords in the stack. */
3933 if (arg_p) {
3934 for (; arg_p->type != ARGT_STOP; arg_p++) {
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_arg2lua(stream->hlua.T, arg_p);
3942 stream->hlua.nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003943 }
3944 }
3945
Thierry FOURNIERbd413492015-03-03 16:52:26 +01003946 /* We must initialize the execution timeouts. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003947 stream->hlua.expire = tick_add(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01003948
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003949 /* Set the currently running flag. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003950 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003951 }
3952
3953 /* Execute the function. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003954 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003955 /* finished. */
3956 case HLUA_E_OK:
3957 /* Convert the returned value in sample. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003958 hlua_lua2smp(stream->hlua.T, -1, smp);
3959 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003960 return 1;
3961
3962 /* yield. */
3963 case HLUA_E_AGAIN:
Willy Tarreau87b09662015-04-03 00:22:06 +02003964 send_log(stream->be, LOG_ERR, "Lua converter '%s': cannot use yielded functions.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003965 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3966 Alert("Lua converter '%s': cannot use yielded functions.\n", fcn->name);
3967 return 0;
3968
3969 /* finished with error. */
3970 case HLUA_E_ERRMSG:
3971 /* Display log. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003972 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 +01003973 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
Willy Tarreau87b09662015-04-03 00:22:06 +02003974 Alert("Lua converter '%s': %s.\n", fcn->name, lua_tostring(stream->hlua.T, -1));
3975 lua_pop(stream->hlua.T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003976 return 0;
3977
3978 case HLUA_E_ERR:
3979 /* Display log. */
Willy Tarreau87b09662015-04-03 00:22:06 +02003980 send_log(stream->be, LOG_ERR, "Lua converter '%s' returns an unknown error.", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01003981 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
3982 Alert("Lua converter '%s' returns an unknown error.\n", fcn->name);
3983
3984 default:
3985 return 0;
3986 }
3987}
3988
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01003989/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
3990 * doesn't allow "yield" functions because the HAProxy engine cannot
3991 * resume sample-fetches.
3992 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003993static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
3994 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01003995{
3996 struct hlua_function *fcn = (struct hlua_function *)private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003997 struct stream *stream = smp->strm;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01003998
Willy Tarreau87b09662015-04-03 00:22:06 +02003999 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004000 * Lua context can be not initialized. This behavior
4001 * permits to save performances because a systematic
4002 * Lua initialization cause 5% performances loss.
4003 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004004 if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) {
4005 send_log(stream->be, LOG_ERR, "Lua sample-fetch '%s': can't initialize Lua context.", fcn->name);
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004006 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4007 Alert("Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
4008 return 0;
4009 }
4010
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004011 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004012 if (!HLUA_IS_RUNNING(&stream->hlua)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004013 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004014 if (!lua_checkstack(stream->hlua.T, 2)) {
4015 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004016 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4017 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
4018 return 0;
4019 }
4020
4021 /* Restore the function in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004022 lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004023
4024 /* push arguments in the stack. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004025 if (!hlua_txn_new(stream->hlua.T, stream, smp->px)) {
4026 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004027 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4028 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
4029 return 0;
4030 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004031 stream->hlua.nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004032
4033 /* push keywords in the stack. */
4034 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
4035 /* Check stack available size. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004036 if (!lua_checkstack(stream->hlua.T, 1)) {
4037 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004038 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4039 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
4040 return 0;
4041 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004042 if (!lua_checkstack(stream->hlua.T, 1)) {
4043 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004044 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4045 Alert("Lua sample-fetch '%s': full stack.\n", fcn->name);
4046 return 0;
4047 }
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004048 hlua_arg2lua(stream->hlua.T, arg_p);
4049 stream->hlua.nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004050 }
4051
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004052 /* We must initialize the execution timeouts. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004053 stream->hlua.expire = tick_add(now_ms, hlua_timeout_session);
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004054
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004055 /* Set the currently running flag. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004056 HLUA_SET_RUN(&stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004057 }
4058
4059 /* Execute the function. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004060 switch (hlua_ctx_resume(&stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004061 /* finished. */
4062 case HLUA_E_OK:
4063 /* Convert the returned value in sample. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004064 hlua_lua2smp(stream->hlua.T, -1, smp);
4065 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004066
4067 /* Set the end of execution flag. */
4068 smp->flags &= ~SMP_F_MAY_CHANGE;
4069 return 1;
4070
4071 /* yield. */
4072 case HLUA_E_AGAIN:
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004073 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': cannot use yielded functions.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004074 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4075 Alert("Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
4076 return 0;
4077
4078 /* finished with error. */
4079 case HLUA_E_ERRMSG:
4080 /* Display log. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004081 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 +01004082 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004083 Alert("Lua sample-fetch '%s': %s.\n", fcn->name, lua_tostring(stream->hlua.T, -1));
4084 lua_pop(stream->hlua.T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004085 return 0;
4086
4087 case HLUA_E_ERR:
4088 /* Display log. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02004089 send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s' returns an unknown error.", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004090 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4091 Alert("Lua sample-fetch '%s': returns an unknown error.\n", fcn->name);
4092
4093 default:
4094 return 0;
4095 }
4096}
4097
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004098/* This function is an LUA binding used for registering
4099 * "sample-conv" functions. It expects a converter name used
4100 * in the haproxy configuration file, and an LUA function.
4101 */
4102__LJMP static int hlua_register_converters(lua_State *L)
4103{
4104 struct sample_conv_kw_list *sck;
4105 const char *name;
4106 int ref;
4107 int len;
4108 struct hlua_function *fcn;
4109
4110 MAY_LJMP(check_args(L, 2, "register_converters"));
4111
4112 /* First argument : converter name. */
4113 name = MAY_LJMP(luaL_checkstring(L, 1));
4114
4115 /* Second argument : lua function. */
4116 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4117
4118 /* Allocate and fill the sample fetch keyword struct. */
Willy Tarreau07081fe2015-04-06 10:59:20 +02004119 sck = malloc(sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004120 if (!sck)
4121 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4122 fcn = malloc(sizeof(*fcn));
4123 if (!fcn)
4124 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4125
4126 /* Fill fcn. */
4127 fcn->name = strdup(name);
4128 if (!fcn->name)
4129 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4130 fcn->function_ref = ref;
4131
4132 /* List head */
4133 sck->list.n = sck->list.p = NULL;
4134
4135 /* converter keyword. */
4136 len = strlen("lua.") + strlen(name) + 1;
4137 sck->kw[0].kw = malloc(len);
4138 if (!sck->kw[0].kw)
4139 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4140
4141 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
4142 sck->kw[0].process = hlua_sample_conv_wrapper;
4143 sck->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4144 sck->kw[0].val_args = NULL;
4145 sck->kw[0].in_type = SMP_T_STR;
4146 sck->kw[0].out_type = SMP_T_STR;
4147 sck->kw[0].private = fcn;
4148
4149 /* End of array. */
4150 memset(&sck->kw[1], 0, sizeof(struct sample_conv));
4151
4152 /* Register this new converter */
4153 sample_register_convs(sck);
4154
4155 return 0;
4156}
4157
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004158/* This fucntion is an LUA binding used for registering
4159 * "sample-fetch" functions. It expects a converter name used
4160 * in the haproxy configuration file, and an LUA function.
4161 */
4162__LJMP static int hlua_register_fetches(lua_State *L)
4163{
4164 const char *name;
4165 int ref;
4166 int len;
4167 struct sample_fetch_kw_list *sfk;
4168 struct hlua_function *fcn;
4169
4170 MAY_LJMP(check_args(L, 2, "register_fetches"));
4171
4172 /* First argument : sample-fetch name. */
4173 name = MAY_LJMP(luaL_checkstring(L, 1));
4174
4175 /* Second argument : lua function. */
4176 ref = MAY_LJMP(hlua_checkfunction(L, 2));
4177
4178 /* Allocate and fill the sample fetch keyword struct. */
Willy Tarreau07081fe2015-04-06 10:59:20 +02004179 sfk = malloc(sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004180 if (!sfk)
4181 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4182 fcn = malloc(sizeof(*fcn));
4183 if (!fcn)
4184 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4185
4186 /* Fill fcn. */
4187 fcn->name = strdup(name);
4188 if (!fcn->name)
4189 WILL_LJMP(luaL_error(L, "lua out of memory error."));
4190 fcn->function_ref = ref;
4191
4192 /* List head */
4193 sfk->list.n = sfk->list.p = NULL;
4194
4195 /* sample-fetch keyword. */
4196 len = strlen("lua.") + strlen(name) + 1;
4197 sfk->kw[0].kw = malloc(len);
4198 if (!sfk->kw[0].kw)
4199 return luaL_error(L, "lua out of memory error.");
4200
4201 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
4202 sfk->kw[0].process = hlua_sample_fetch_wrapper;
4203 sfk->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR);
4204 sfk->kw[0].val_args = NULL;
4205 sfk->kw[0].out_type = SMP_T_STR;
4206 sfk->kw[0].use = SMP_USE_HTTP_ANY;
4207 sfk->kw[0].val = 0;
4208 sfk->kw[0].private = fcn;
4209
4210 /* End of array. */
4211 memset(&sfk->kw[1], 0, sizeof(struct sample_fetch));
4212
4213 /* Register this new fetch. */
4214 sample_register_fetches(sfk);
4215
4216 return 0;
4217}
4218
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004219/* global {tcp|http}-request parser. Return 1 in succes case, else return 0. */
4220static int hlua_parse_rule(const char **args, int *cur_arg, struct proxy *px,
4221 struct hlua_rule **rule_p, char **err)
4222{
4223 struct hlua_rule *rule;
4224
4225 /* Memory for the rule. */
4226 rule = malloc(sizeof(*rule));
4227 if (!rule) {
4228 memprintf(err, "out of memory error");
4229 return 0;
4230 }
4231 *rule_p = rule;
4232
4233 /* The requiered arg is a function name. */
4234 if (!args[*cur_arg]) {
4235 memprintf(err, "expect Lua function name");
4236 return 0;
4237 }
4238
4239 /* Lookup for the symbol, and check if it is a function. */
4240 lua_getglobal(gL.T, args[*cur_arg]);
4241 if (lua_isnil(gL.T, -1)) {
4242 lua_pop(gL.T, 1);
4243 memprintf(err, "Lua function '%s' not found", args[*cur_arg]);
4244 return 0;
4245 }
4246 if (!lua_isfunction(gL.T, -1)) {
4247 lua_pop(gL.T, 1);
4248 memprintf(err, "'%s' is not a function", args[*cur_arg]);
4249 return 0;
4250 }
4251
4252 /* Reference the Lua function and store the reference. */
4253 rule->fcn.function_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
4254 rule->fcn.name = strdup(args[*cur_arg]);
4255 if (!rule->fcn.name) {
4256 memprintf(err, "out of memory error.");
4257 return 0;
4258 }
4259 (*cur_arg)++;
4260
4261 /* TODO: later accept arguments. */
4262 rule->args = NULL;
4263
4264 return 1;
4265}
4266
4267/* This function is a wrapper to execute each LUA function declared
4268 * as an action wrapper during the initialisation period. This function
4269 * return 1 if the processing is finished (with oe without error) and
4270 * return 0 if the function must be called again because the LUA
4271 * returns a yield.
4272 */
4273static int hlua_request_act_wrapper(struct hlua_rule *rule, struct proxy *px,
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004274 struct stream *s, unsigned int analyzer)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004275{
4276 char **arg;
4277
Willy Tarreau87b09662015-04-03 00:22:06 +02004278 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01004279 * Lua context can be not initialized. This behavior
4280 * permits to save performances because a systematic
4281 * Lua initialization cause 5% performances loss.
4282 */
4283 if (!s->hlua.T && !hlua_ctx_init(&s->hlua, s->task)) {
4284 send_log(px, LOG_ERR, "Lua action '%s': can't initialize Lua context.", rule->fcn.name);
4285 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4286 Alert("Lua action '%s': can't initialize Lua context.\n", rule->fcn.name);
4287 return 0;
4288 }
4289
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004290 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004291 if (!HLUA_IS_RUNNING(&s->hlua)) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004292 /* Check stack available size. */
4293 if (!lua_checkstack(s->hlua.T, 1)) {
4294 send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name);
4295 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4296 Alert("Lua function '%s': full stack.\n", rule->fcn.name);
4297 return 0;
4298 }
4299
4300 /* Restore the function in the stack. */
4301 lua_rawgeti(s->hlua.T, LUA_REGISTRYINDEX, rule->fcn.function_ref);
4302
Willy Tarreau87b09662015-04-03 00:22:06 +02004303 /* Create and and push object stream in the stack. */
Willy Tarreau15e91e12015-04-04 00:52:09 +02004304 if (!hlua_txn_new(s->hlua.T, s, px)) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004305 send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name);
4306 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4307 Alert("Lua function '%s': full stack.\n", rule->fcn.name);
4308 return 0;
4309 }
4310 s->hlua.nargs = 1;
4311
4312 /* push keywords in the stack. */
4313 for (arg = rule->args; arg && *arg; arg++) {
4314 if (!lua_checkstack(s->hlua.T, 1)) {
4315 send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name);
4316 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4317 Alert("Lua function '%s': full stack.\n", rule->fcn.name);
4318 return 0;
4319 }
4320 lua_pushstring(s->hlua.T, *arg);
4321 s->hlua.nargs++;
4322 }
4323
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004324 /* We must initialize the execution timeouts. */
4325 s->hlua.expire = tick_add(now_ms, hlua_timeout_session);
4326
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004327 /* Set the currently running flag. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004328 HLUA_SET_RUN(&s->hlua);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004329 }
4330
4331 /* Execute the function. */
4332 switch (hlua_ctx_resume(&s->hlua, 1)) {
4333 /* finished. */
4334 case HLUA_E_OK:
4335 return 1;
4336
4337 /* yield. */
4338 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004339 /* Set timeout in the required channel. */
4340 if (s->hlua.wake_time != TICK_ETERNITY) {
4341 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004342 s->req.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004343 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004344 s->res.analyse_exp = s->hlua.wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01004345 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004346 /* Some actions can be wake up when a "write" event
4347 * is detected on a response channel. This is useful
4348 * only for actions targetted on the requests.
4349 */
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01004350 if (HLUA_IS_WAKERESWR(&s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004351 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01004352 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004353 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004354 }
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01004355 if (HLUA_IS_WAKEREQWR(&s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004356 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004357 return 0;
4358
4359 /* finished with error. */
4360 case HLUA_E_ERRMSG:
4361 /* Display log. */
4362 send_log(px, LOG_ERR, "Lua function '%s': %s.", rule->fcn.name, lua_tostring(s->hlua.T, -1));
4363 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4364 Alert("Lua function '%s': %s.\n", rule->fcn.name, lua_tostring(s->hlua.T, -1));
4365 lua_pop(s->hlua.T, 1);
4366 return 1;
4367
4368 case HLUA_E_ERR:
4369 /* Display log. */
4370 send_log(px, LOG_ERR, "Lua function '%s' return an unknown error.", rule->fcn.name);
4371 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
4372 Alert("Lua function '%s' return an unknown error.\n", rule->fcn.name);
4373
4374 default:
4375 return 1;
4376 }
4377}
4378
4379/* Lua execution wrapper for "tcp-request". This function uses
4380 * "hlua_request_act_wrapper" for executing the LUA code.
4381 */
4382int hlua_tcp_req_act_wrapper(struct tcp_rule *tcp_rule, struct proxy *px,
Willy Tarreau87b09662015-04-03 00:22:06 +02004383 struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004384{
Thierry FOURNIERfbdb7752015-06-03 19:32:04 +02004385 return hlua_request_act_wrapper((struct hlua_rule *)tcp_rule->act_prm.data[0],
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004386 px, s, AN_REQ_INSPECT_FE);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004387}
4388
4389/* Lua execution wrapper for "tcp-response". This function uses
4390 * "hlua_request_act_wrapper" for executing the LUA code.
4391 */
4392int hlua_tcp_res_act_wrapper(struct tcp_rule *tcp_rule, struct proxy *px,
Willy Tarreau87b09662015-04-03 00:22:06 +02004393 struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004394{
Thierry FOURNIERfbdb7752015-06-03 19:32:04 +02004395 return hlua_request_act_wrapper((struct hlua_rule *)tcp_rule->act_prm.data[0],
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004396 px, s, AN_RES_INSPECT);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004397}
4398
4399/* Lua execution wrapper for http-request.
4400 * This function uses "hlua_request_act_wrapper" for executing
4401 * the LUA code.
4402 */
4403int hlua_http_req_act_wrapper(struct http_req_rule *rule, struct proxy *px,
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004404 struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004405{
4406 return hlua_request_act_wrapper((struct hlua_rule *)rule->arg.data, px,
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004407 s, AN_REQ_HTTP_PROCESS_FE);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004408}
4409
4410/* Lua execution wrapper for http-response.
4411 * This function uses "hlua_request_act_wrapper" for executing
4412 * the LUA code.
4413 */
4414int hlua_http_res_act_wrapper(struct http_res_rule *rule, struct proxy *px,
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004415 struct stream *s)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004416{
4417 return hlua_request_act_wrapper((struct hlua_rule *)rule->arg.data, px,
Willy Tarreaufdcd2ae2015-04-04 01:11:28 +02004418 s, AN_RES_HTTP_PROCESS_BE);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004419}
4420
4421/* tcp-request <*> configuration wrapper. */
4422static int tcp_req_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4423 struct tcp_rule *rule, char **err)
4424{
Thierry FOURNIERfbdb7752015-06-03 19:32:04 +02004425 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->act_prm.data[0], err))
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01004426 return 0;
Thierry FOURNIER79318d72015-05-29 17:31:12 +02004427 rule->action = TCP_ACT_CUSTOM_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004428 rule->action_ptr = hlua_tcp_req_act_wrapper;
4429 return 1;
4430}
4431
4432/* tcp-response <*> configuration wrapper. */
4433static int tcp_res_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4434 struct tcp_rule *rule, char **err)
4435{
Thierry FOURNIERfbdb7752015-06-03 19:32:04 +02004436 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->act_prm.data[0], err))
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01004437 return 0;
Thierry FOURNIER79318d72015-05-29 17:31:12 +02004438 rule->action = TCP_ACT_CUSTOM_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004439 rule->action_ptr = hlua_tcp_res_act_wrapper;
4440 return 1;
4441}
4442
4443/* http-request <*> configuration wrapper. */
4444static int http_req_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4445 struct http_req_rule *rule, char **err)
4446{
4447 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->arg.data, err))
4448 return -1;
4449 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
4450 rule->action_ptr = hlua_http_req_act_wrapper;
4451 return 1;
4452}
4453
4454/* http-response <*> configuration wrapper. */
4455static int http_res_action_register_lua(const char **args, int *cur_arg, struct proxy *px,
4456 struct http_res_rule *rule, char **err)
4457{
4458 if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->arg.data, err))
4459 return -1;
4460 rule->action = HTTP_RES_ACT_CUSTOM_CONT;
4461 rule->action_ptr = hlua_http_res_act_wrapper;
4462 return 1;
4463}
4464
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004465static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
4466 struct proxy *defpx, const char *file, int line,
4467 char **err, unsigned int *timeout)
4468{
4469 const char *error;
4470
4471 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
4472 if (error && *error != '\0') {
4473 memprintf(err, "%s: invalid timeout", args[0]);
4474 return -1;
4475 }
4476 return 0;
4477}
4478
4479static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
4480 struct proxy *defpx, const char *file, int line,
4481 char **err)
4482{
4483 return hlua_read_timeout(args, section_type, curpx, defpx,
4484 file, line, err, &hlua_timeout_session);
4485}
4486
4487static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
4488 struct proxy *defpx, const char *file, int line,
4489 char **err)
4490{
4491 return hlua_read_timeout(args, section_type, curpx, defpx,
4492 file, line, err, &hlua_timeout_task);
4493}
4494
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004495static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
4496 struct proxy *defpx, const char *file, int line,
4497 char **err)
4498{
4499 char *error;
4500
4501 hlua_nb_instruction = strtoll(args[1], &error, 10);
4502 if (*error != '\0') {
4503 memprintf(err, "%s: invalid number", args[0]);
4504 return -1;
4505 }
4506 return 0;
4507}
4508
Willy Tarreau32f61e22015-03-18 17:54:59 +01004509static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
4510 struct proxy *defpx, const char *file, int line,
4511 char **err)
4512{
4513 char *error;
4514
4515 if (*(args[1]) == 0) {
4516 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
4517 return -1;
4518 }
4519 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
4520 if (*error != '\0') {
4521 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
4522 return -1;
4523 }
4524 return 0;
4525}
4526
4527
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004528/* This function is called by the main configuration key "lua-load". It loads and
4529 * execute an lua file during the parsing of the HAProxy configuration file. It is
4530 * the main lua entry point.
4531 *
4532 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
4533 * occured, otherwise it returns 0.
4534 *
4535 * In some error case, LUA set an error message in top of the stack. This function
4536 * returns this error message in the HAProxy logs and pop it from the stack.
4537 */
4538static int hlua_load(char **args, int section_type, struct proxy *curpx,
4539 struct proxy *defpx, const char *file, int line,
4540 char **err)
4541{
4542 int error;
4543
4544 /* Just load and compile the file. */
4545 error = luaL_loadfile(gL.T, args[1]);
4546 if (error) {
4547 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
4548 lua_pop(gL.T, 1);
4549 return -1;
4550 }
4551
4552 /* If no syntax error where detected, execute the code. */
4553 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
4554 switch (error) {
4555 case LUA_OK:
4556 break;
4557 case LUA_ERRRUN:
4558 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
4559 lua_pop(gL.T, 1);
4560 return -1;
4561 case LUA_ERRMEM:
4562 memprintf(err, "lua out of memory error\n");
4563 return -1;
4564 case LUA_ERRERR:
4565 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
4566 lua_pop(gL.T, 1);
4567 return -1;
4568 case LUA_ERRGCMM:
4569 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
4570 lua_pop(gL.T, 1);
4571 return -1;
4572 default:
4573 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
4574 lua_pop(gL.T, 1);
4575 return -1;
4576 }
4577
4578 return 0;
4579}
4580
4581/* configuration keywords declaration */
4582static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01004583 { CFG_GLOBAL, "lua-load", hlua_load },
4584 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
4585 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01004586 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01004587 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004588 { 0, NULL, NULL },
4589}};
4590
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004591static struct http_req_action_kw_list http_req_kws = {"lua", { }, {
4592 { "lua", http_req_action_register_lua },
4593 { NULL, NULL }
4594}};
4595
4596static struct http_res_action_kw_list http_res_kws = {"lua", { }, {
4597 { "lua", http_res_action_register_lua },
4598 { NULL, NULL }
4599}};
4600
4601static struct tcp_action_kw_list tcp_req_cont_kws = {"lua", { }, {
4602 { "lua", tcp_req_action_register_lua },
4603 { NULL, NULL }
4604}};
4605
4606static struct tcp_action_kw_list tcp_res_cont_kws = {"lua", { }, {
4607 { "lua", tcp_res_action_register_lua },
4608 { NULL, NULL }
4609}};
4610
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004611int hlua_post_init()
4612{
4613 struct hlua_init_function *init;
4614 const char *msg;
4615 enum hlua_exec ret;
4616
4617 list_for_each_entry(init, &hlua_init_functions, l) {
4618 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
4619 ret = hlua_ctx_resume(&gL, 0);
4620 switch (ret) {
4621 case HLUA_E_OK:
4622 lua_pop(gL.T, -1);
4623 return 1;
4624 case HLUA_E_AGAIN:
4625 Alert("lua init: yield not allowed.\n");
4626 return 0;
4627 case HLUA_E_ERRMSG:
4628 msg = lua_tostring(gL.T, -1);
4629 Alert("lua init: %s.\n", msg);
4630 return 0;
4631 case HLUA_E_ERR:
4632 default:
4633 Alert("lua init: unknown runtime error.\n");
4634 return 0;
4635 }
4636 }
4637 return 1;
4638}
4639
Willy Tarreau32f61e22015-03-18 17:54:59 +01004640/* The memory allocator used by the Lua stack. <ud> is a pointer to the
4641 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
4642 * is the previously allocated size or the kind of object in case of a new
4643 * allocation. <nsize> is the requested new size.
4644 */
4645static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
4646{
4647 struct hlua_mem_allocator *zone = ud;
4648
4649 if (nsize == 0) {
4650 /* it's a free */
4651 if (ptr)
4652 zone->allocated -= osize;
4653 free(ptr);
4654 return NULL;
4655 }
4656
4657 if (!ptr) {
4658 /* it's a new allocation */
4659 if (zone->limit && zone->allocated + nsize > zone->limit)
4660 return NULL;
4661
4662 ptr = malloc(nsize);
4663 if (ptr)
4664 zone->allocated += nsize;
4665 return ptr;
4666 }
4667
4668 /* it's a realloc */
4669 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
4670 return NULL;
4671
4672 ptr = realloc(ptr, nsize);
4673 if (ptr)
4674 zone->allocated += nsize - osize;
4675 return ptr;
4676}
4677
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01004678void hlua_init(void)
4679{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004680 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004681 int idx;
4682 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004683 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004684 char *p;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004685#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004686 struct srv_kw *kw;
4687 int tmp_error;
4688 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01004689 char *args[] = { /* SSL client configuration. */
4690 "ssl",
4691 "verify",
4692 "none",
4693 "force-sslv3",
4694 NULL
4695 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004696#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004697
Willy Tarreau87b09662015-04-03 00:22:06 +02004698 /* Initialise com signals pool */
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004699 pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED);
4700
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01004701 /* Register configuration keywords. */
4702 cfg_register_keywords(&cfg_kws);
4703
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01004704 /* Register custom HTTP rules. */
4705 http_req_keywords_register(&http_req_kws);
4706 http_res_keywords_register(&http_res_kws);
4707 tcp_req_cont_keywords_register(&tcp_req_cont_kws);
4708 tcp_res_cont_keywords_register(&tcp_res_cont_kws);
4709
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004710 /* Init main lua stack. */
4711 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01004712 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01004713 LIST_INIT(&gL.com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004714 gL.T = luaL_newstate();
4715 hlua_sethlua(&gL);
4716 gL.Tref = LUA_REFNIL;
4717 gL.task = NULL;
4718
Willy Tarreau32f61e22015-03-18 17:54:59 +01004719 /* change the memory allocators to track memory usage */
4720 lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator);
4721
Thierry FOURNIER380d0932015-01-23 14:27:52 +01004722 /* Initialise lua. */
4723 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004724
4725 /*
4726 *
4727 * Create "core" object.
4728 *
4729 */
4730
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01004731 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004732 lua_newtable(gL.T);
4733
4734 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004735 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004736 hlua_class_const_int(gL.T, log_levels[i], i);
4737
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004738 /* Register special functions. */
4739 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01004740 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01004741 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01004742 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01004743 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01004744 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01004745 hlua_class_function(gL.T, "sleep", hlua_sleep);
4746 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01004747 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
4748 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
4749 hlua_class_function(gL.T, "set_map", hlua_set_map);
4750 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01004751 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01004752 hlua_class_function(gL.T, "log", hlua_log);
4753 hlua_class_function(gL.T, "Debug", hlua_log_debug);
4754 hlua_class_function(gL.T, "Info", hlua_log_info);
4755 hlua_class_function(gL.T, "Warning", hlua_log_warning);
4756 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01004757
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01004758 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004759
4760 /*
4761 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02004762 * Register class Map
4763 *
4764 */
4765
4766 /* This table entry is the object "Map" base. */
4767 lua_newtable(gL.T);
4768
4769 /* register pattern types. */
4770 for (i=0; i<PAT_MATCH_NUM; i++)
4771 hlua_class_const_int(gL.T, pat_match_names[i], i);
4772
4773 /* register constructor. */
4774 hlua_class_function(gL.T, "new", hlua_map_new);
4775
4776 /* Create and fill the metatable. */
4777 lua_newtable(gL.T);
4778
4779 /* Create and fille the __index entry. */
4780 lua_pushstring(gL.T, "__index");
4781 lua_newtable(gL.T);
4782
4783 /* Register . */
4784 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
4785 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
4786
4787 lua_settable(gL.T, -3);
4788
4789 /* Register previous table in the registry with reference and named entry. */
4790 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4791 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4792 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_MAP); /* register class session. */
4793 class_map_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4794
4795 /* Assign the metatable to the mai Map object. */
4796 lua_setmetatable(gL.T, -2);
4797
4798 /* Set a name to the table. */
4799 lua_setglobal(gL.T, "Map");
4800
4801 /*
4802 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01004803 * Register class Channel
4804 *
4805 */
4806
4807 /* Create and fill the metatable. */
4808 lua_newtable(gL.T);
4809
4810 /* Create and fille the __index entry. */
4811 lua_pushstring(gL.T, "__index");
4812 lua_newtable(gL.T);
4813
4814 /* Register . */
4815 hlua_class_function(gL.T, "get", hlua_channel_get);
4816 hlua_class_function(gL.T, "dup", hlua_channel_dup);
4817 hlua_class_function(gL.T, "getline", hlua_channel_getline);
4818 hlua_class_function(gL.T, "set", hlua_channel_set);
4819 hlua_class_function(gL.T, "append", hlua_channel_append);
4820 hlua_class_function(gL.T, "send", hlua_channel_send);
4821 hlua_class_function(gL.T, "forward", hlua_channel_forward);
4822 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
4823 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
4824
4825 lua_settable(gL.T, -3);
4826
4827 /* Register previous table in the registry with reference and named entry. */
4828 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4829 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CHANNEL); /* register class session. */
4830 class_channel_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4831
4832 /*
4833 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004834 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004835 *
4836 */
4837
4838 /* Create and fill the metatable. */
4839 lua_newtable(gL.T);
4840
4841 /* Create and fille the __index entry. */
4842 lua_pushstring(gL.T, "__index");
4843 lua_newtable(gL.T);
4844
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004845 /* Browse existing fetches and create the associated
4846 * object method.
4847 */
4848 sf = NULL;
4849 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
4850
4851 /* Dont register the keywork if the arguments check function are
4852 * not safe during the runtime.
4853 */
4854 if ((sf->val_args != NULL) &&
4855 (sf->val_args != val_payload_lv) &&
4856 (sf->val_args != val_hdr))
4857 continue;
4858
4859 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
4860 * by an underscore.
4861 */
4862 strncpy(trash.str, sf->kw, trash.size);
4863 trash.str[trash.size - 1] = '\0';
4864 for (p = trash.str; *p; p++)
4865 if (*p == '.' || *p == '-' || *p == '+')
4866 *p = '_';
4867
4868 /* Register the function. */
4869 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01004870 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01004871 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
4872 lua_settable(gL.T, -3);
4873 }
4874
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004875 lua_settable(gL.T, -3);
4876
4877 /* Register previous table in the registry with reference and named entry. */
4878 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4879 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_FETCHES); /* register class session. */
4880 class_fetches_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4881
4882 /*
4883 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004884 * Register class Converters
4885 *
4886 */
4887
4888 /* Create and fill the metatable. */
4889 lua_newtable(gL.T);
4890
4891 /* Create and fill the __index entry. */
4892 lua_pushstring(gL.T, "__index");
4893 lua_newtable(gL.T);
4894
4895 /* Browse existing converters and create the associated
4896 * object method.
4897 */
4898 sc = NULL;
4899 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
4900 /* Dont register the keywork if the arguments check function are
4901 * not safe during the runtime.
4902 */
4903 if (sc->val_args != NULL)
4904 continue;
4905
4906 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
4907 * by an underscore.
4908 */
4909 strncpy(trash.str, sc->kw, trash.size);
4910 trash.str[trash.size - 1] = '\0';
4911 for (p = trash.str; *p; p++)
4912 if (*p == '.' || *p == '-' || *p == '+')
4913 *p = '_';
4914
4915 /* Register the function. */
4916 lua_pushstring(gL.T, trash.str);
4917 lua_pushlightuserdata(gL.T, sc);
4918 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
4919 lua_settable(gL.T, -3);
4920 }
4921
4922 lua_settable(gL.T, -3);
4923
4924 /* Register previous table in the registry with reference and named entry. */
4925 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4926 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CONVERTERS); /* register class session. */
4927 class_converters_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4928
4929 /*
4930 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004931 * Register class HTTP
4932 *
4933 */
4934
4935 /* Create and fill the metatable. */
4936 lua_newtable(gL.T);
4937
4938 /* Create and fille the __index entry. */
4939 lua_pushstring(gL.T, "__index");
4940 lua_newtable(gL.T);
4941
4942 /* Register Lua functions. */
4943 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
4944 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
4945 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
4946 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
4947 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
4948 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
4949 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
4950 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
4951 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
4952 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
4953
4954 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
4955 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
4956 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
4957 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
4958 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
4959 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
4960
4961 lua_settable(gL.T, -3);
4962
4963 /* Register previous table in the registry with reference and named entry. */
4964 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
4965 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_HTTP); /* register class session. */
4966 class_http_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
4967
4968 /*
4969 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004970 * Register class TXN
4971 *
4972 */
4973
4974 /* Create and fill the metatable. */
4975 lua_newtable(gL.T);
4976
4977 /* Create and fille the __index entry. */
4978 lua_pushstring(gL.T, "__index");
4979 lua_newtable(gL.T);
4980
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004981 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01004982 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
4983 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004984 hlua_class_function(gL.T, "set_var", hlua_set_var);
4985 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01004986 hlua_class_function(gL.T, "close", hlua_txn_close);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01004987 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
4988 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
4989 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01004990 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
4991 hlua_class_function(gL.T, "log", hlua_txn_log);
4992 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
4993 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
4994 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
4995 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004996
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004997 lua_settable(gL.T, -3);
4998
4999 /* Register previous table in the registry with reference and named entry. */
5000 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5001 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_TXN); /* register class session. */
5002 class_txn_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005003
5004 /*
5005 *
5006 * Register class Socket
5007 *
5008 */
5009
5010 /* Create and fill the metatable. */
5011 lua_newtable(gL.T);
5012
5013 /* Create and fille the __index entry. */
5014 lua_pushstring(gL.T, "__index");
5015 lua_newtable(gL.T);
5016
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005017#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005018 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01005019#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005020 hlua_class_function(gL.T, "connect", hlua_socket_connect);
5021 hlua_class_function(gL.T, "send", hlua_socket_send);
5022 hlua_class_function(gL.T, "receive", hlua_socket_receive);
5023 hlua_class_function(gL.T, "close", hlua_socket_close);
5024 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
5025 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
5026 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
5027 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
5028
5029 lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
5030
5031 /* Register the garbage collector entry. */
5032 lua_pushstring(gL.T, "__gc");
5033 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
5034 lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
5035
5036 /* Register previous table in the registry with reference and named entry. */
5037 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5038 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
5039 lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_SOCKET); /* register class socket. */
5040 class_socket_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class socket. */
5041
5042 /* Proxy and server configuration initialisation. */
5043 memset(&socket_proxy, 0, sizeof(socket_proxy));
5044 init_new_proxy(&socket_proxy);
5045 socket_proxy.parent = NULL;
5046 socket_proxy.last_change = now.tv_sec;
5047 socket_proxy.id = "LUA-SOCKET";
5048 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
5049 socket_proxy.maxconn = 0;
5050 socket_proxy.accept = NULL;
5051 socket_proxy.options2 |= PR_O2_INDEPSTR;
5052 socket_proxy.srv = NULL;
5053 socket_proxy.conn_retries = 0;
5054 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
5055
5056 /* Init TCP server: unchanged parameters */
5057 memset(&socket_tcp, 0, sizeof(socket_tcp));
5058 socket_tcp.next = NULL;
5059 socket_tcp.proxy = &socket_proxy;
5060 socket_tcp.obj_type = OBJ_TYPE_SERVER;
5061 LIST_INIT(&socket_tcp.actconns);
5062 LIST_INIT(&socket_tcp.pendconns);
5063 socket_tcp.state = SRV_ST_RUNNING; /* early server setup */
5064 socket_tcp.last_change = 0;
5065 socket_tcp.id = "LUA-TCP-CONN";
5066 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5067 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5068 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
5069
5070 /* XXX: Copy default parameter from default server,
5071 * but the default server is not initialized.
5072 */
5073 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
5074 socket_tcp.minconn = socket_proxy.defsrv.minconn;
5075 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
5076 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
5077 socket_tcp.onerror = socket_proxy.defsrv.onerror;
5078 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5079 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
5080 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5081 socket_tcp.uweight = socket_proxy.defsrv.iweight;
5082 socket_tcp.iweight = socket_proxy.defsrv.iweight;
5083
5084 socket_tcp.check.status = HCHK_STATUS_INI;
5085 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
5086 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
5087 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
5088 socket_tcp.check.server = &socket_tcp;
5089
5090 socket_tcp.agent.status = HCHK_STATUS_INI;
5091 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
5092 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
5093 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
5094 socket_tcp.agent.server = &socket_tcp;
5095
5096 socket_tcp.xprt = &raw_sock;
5097
5098#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005099 /* Init TCP server: unchanged parameters */
5100 memset(&socket_ssl, 0, sizeof(socket_ssl));
5101 socket_ssl.next = NULL;
5102 socket_ssl.proxy = &socket_proxy;
5103 socket_ssl.obj_type = OBJ_TYPE_SERVER;
5104 LIST_INIT(&socket_ssl.actconns);
5105 LIST_INIT(&socket_ssl.pendconns);
5106 socket_ssl.state = SRV_ST_RUNNING; /* early server setup */
5107 socket_ssl.last_change = 0;
5108 socket_ssl.id = "LUA-SSL-CONN";
5109 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5110 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
5111 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
5112
5113 /* XXX: Copy default parameter from default server,
5114 * but the default server is not initialized.
5115 */
5116 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
5117 socket_ssl.minconn = socket_proxy.defsrv.minconn;
5118 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
5119 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
5120 socket_ssl.onerror = socket_proxy.defsrv.onerror;
5121 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
5122 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
5123 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
5124 socket_ssl.uweight = socket_proxy.defsrv.iweight;
5125 socket_ssl.iweight = socket_proxy.defsrv.iweight;
5126
5127 socket_ssl.check.status = HCHK_STATUS_INI;
5128 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
5129 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
5130 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
5131 socket_ssl.check.server = &socket_ssl;
5132
5133 socket_ssl.agent.status = HCHK_STATUS_INI;
5134 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
5135 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
5136 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
5137 socket_ssl.agent.server = &socket_ssl;
5138
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005139 socket_ssl.use_ssl = 1;
5140 socket_ssl.xprt = &ssl_sock;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005141
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005142 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005143 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
5144 /*
5145 *
5146 * If the keyword is not known, we can search in the registered
5147 * server keywords. This is usefull to configure special SSL
5148 * features like client certificates and ssl_verify.
5149 *
5150 */
5151 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
5152 if (tmp_error != 0) {
5153 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
5154 abort(); /* This must be never arrives because the command line
5155 not editable by the user. */
5156 }
5157 idx += kw->skip;
5158 }
5159 }
5160
5161 /* Initialize SSL server. */
Thierry FOURNIER36d13742015-03-17 16:48:53 +01005162 ssl_sock_prepare_srv_ctx(&socket_ssl, &socket_proxy);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01005163#endif
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01005164}