blob: 0ed8ec99f3f105d3d0669fe6799afc30dab40b1c [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
2 * Lua unsafe core engine
3 *
4 * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010013#include <ctype.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020014#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010015
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010016#include <lauxlib.h>
17#include <lua.h>
18#include <lualib.h>
19
Thierry FOURNIER463119c2015-03-10 00:35:36 +010020#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
21#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010022#endif
23
Thierry FOURNIER380d0932015-01-23 14:27:52 +010024#include <ebpttree.h>
25
26#include <common/cfgparse.h>
27
William Lallemand9ed62032016-11-21 17:49:11 +010028#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010029#include <types/hlua.h>
30#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010031#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010032
Thierry FOURNIER55da1652015-01-23 11:36:30 +010033#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020034#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010035#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010036#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010037#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010038#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010039#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010040#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010041#include <proto/hlua_fcn.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020042#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010043#include <proto/obj_type.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010044#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010045#include <proto/payload.h>
46#include <proto/proto_http.h>
47#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010048#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020049#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020050#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010051#include <proto/ssl_sock.h>
52#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010053#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010054#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020055#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010056
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010057/* Lua uses longjmp to perform yield or throwing errors. This
58 * macro is used only for identifying the function that can
59 * not return because a longjmp is executed.
60 * __LJMP marks a prototype of hlua file that can use longjmp.
61 * WILL_LJMP() marks an lua function that will use longjmp.
62 * MAY_LJMP() marks an lua function that may use longjmp.
63 */
64#define __LJMP
65#define WILL_LJMP(func) func
66#define MAY_LJMP(func) func
67
Thierry FOURNIERbabae282015-09-17 11:36:37 +020068/* This couple of function executes securely some Lua calls outside of
69 * the lua runtime environment. Each Lua call can return a longjmp
70 * if it encounter a memory error.
71 *
72 * Lua documentation extract:
73 *
74 * If an error happens outside any protected environment, Lua calls
75 * a panic function (see lua_atpanic) and then calls abort, thus
76 * exiting the host application. Your panic function can avoid this
77 * exit by never returning (e.g., doing a long jump to your own
78 * recovery point outside Lua).
79 *
80 * The panic function runs as if it were a message handler (see
81 * §2.3); in particular, the error message is at the top of the
82 * stack. However, there is no guarantee about stack space. To push
83 * anything on the stack, the panic function must first check the
84 * available space (see §4.2).
85 *
86 * We must check all the Lua entry point. This includes:
87 * - The include/proto/hlua.h exported functions
88 * - the task wrapper function
89 * - The action wrapper function
90 * - The converters wrapper function
91 * - The sample-fetch wrapper functions
92 *
93 * It is tolerated that the initilisation function returns an abort.
94 * Before each Lua abort, an error message is writed on stderr.
95 *
96 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
97 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
98 * because they must be exists in the program stack when the longjmp
99 * is called.
100 */
101jmp_buf safe_ljmp_env;
102static int hlua_panic_safe(lua_State *L) { return 0; }
103static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
104
105#define SET_SAFE_LJMP(__L) \
106 ({ \
107 int ret; \
108 if (setjmp(safe_ljmp_env) != 0) { \
109 lua_atpanic(__L, hlua_panic_safe); \
110 ret = 0; \
111 } else { \
112 lua_atpanic(__L, hlua_panic_ljmp); \
113 ret = 1; \
114 } \
115 ret; \
116 })
117
118/* If we are the last function catching Lua errors, we
119 * must reset the panic function.
120 */
121#define RESET_SAFE_LJMP(__L) \
122 do { \
123 lua_atpanic(__L, hlua_panic_safe); \
124 } while(0)
125
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200126/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200127#define APPLET_DONE 0x01 /* applet processing is done. */
128#define APPLET_100C 0x02 /* 100 continue expected. */
129#define APPLET_HDR_SENT 0x04 /* Response header sent. */
130#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
131#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100132#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200133
134#define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n"
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200135
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100136/* The main Lua execution context. */
137struct hlua gL;
138
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100139/* This is the memory pool containing struct lua for applets
140 * (including cli).
141 */
142struct pool_head *pool2_hlua;
143
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100144/* This is the memory pool containing all the signal structs. These
145 * struct are used to store each requiered signal between two tasks.
146 */
147struct pool_head *pool2_hlua_com;
148
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100149/* Used for Socket connection. */
150static struct proxy socket_proxy;
151static struct server socket_tcp;
152#ifdef USE_OPENSSL
153static struct server socket_ssl;
154#endif
155
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100156/* List head of the function called at the initialisation time. */
157struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
158
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100159/* The following variables contains the reference of the different
160 * Lua classes. These references are useful for identify metadata
161 * associated with an object.
162 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100163static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100164static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100165static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100166static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100167static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100168static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200169static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200170static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200171static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100172
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100173/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200174 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100175 * short timeout. Lua linked with tasks doesn't have a timeout
176 * because a task may remain alive during all the haproxy execution.
177 */
178static unsigned int hlua_timeout_session = 4000; /* session timeout. */
179static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200180static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100181
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100182/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
183 * it is used for preventing infinite loops.
184 *
185 * I test the scheer with an infinite loop containing one incrementation
186 * and one test. I run this loop between 10 seconds, I raise a ceil of
187 * 710M loops from one interrupt each 9000 instructions, so I fix the value
188 * to one interrupt each 10 000 instructions.
189 *
190 * configured | Number of
191 * instructions | loops executed
192 * between two | in milions
193 * forced yields |
194 * ---------------+---------------
195 * 10 | 160
196 * 500 | 670
197 * 1000 | 680
198 * 5000 | 700
199 * 7000 | 700
200 * 8000 | 700
201 * 9000 | 710 <- ceil
202 * 10000 | 710
203 * 100000 | 710
204 * 1000000 | 710
205 *
206 */
207static unsigned int hlua_nb_instruction = 10000;
208
Willy Tarreau32f61e22015-03-18 17:54:59 +0100209/* Descriptor for the memory allocation state. If limit is not null, it will
210 * be enforced on any memory allocation.
211 */
212struct hlua_mem_allocator {
213 size_t allocated;
214 size_t limit;
215};
216
217static struct hlua_mem_allocator hlua_global_allocator;
218
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200219static const char error_500[] =
220 "HTTP/1.0 500 Server Error\r\n"
221 "Cache-Control: no-cache\r\n"
222 "Connection: close\r\n"
223 "Content-Type: text/html\r\n"
224 "\r\n"
225 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n";
226
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100227/* These functions converts types between HAProxy internal args or
228 * sample and LUA types. Another function permits to check if the
229 * LUA stack contains arguments according with an required ARG_T
230 * format.
231 */
232static int hlua_arg2lua(lua_State *L, const struct arg *arg);
233static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100234__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100235 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100236static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100237static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100238static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
239
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200240__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
241
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200242#define SEND_ERR(__be, __fmt, __args...) \
243 do { \
244 send_log(__be, LOG_ERR, __fmt, ## __args); \
245 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
246 Alert(__fmt, ## __args); \
247 } while (0)
248
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100249/* Used to check an Lua function type in the stack. It creates and
250 * returns a reference of the function. This function throws an
251 * error if the rgument is not a "function".
252 */
253__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
254{
255 if (!lua_isfunction(L, argno)) {
256 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1));
257 WILL_LJMP(luaL_argerror(L, argno, msg));
258 }
259 lua_pushvalue(L, argno);
260 return luaL_ref(L, LUA_REGISTRYINDEX);
261}
262
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200263/* Return the string that is of the top of the stack. */
264const char *hlua_get_top_error_string(lua_State *L)
265{
266 if (lua_gettop(L) < 1)
267 return "unknown error";
268 if (lua_type(L, -1) != LUA_TSTRING)
269 return "unknown error";
270 return lua_tostring(L, -1);
271}
272
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100273/* This function check the number of arguments available in the
274 * stack. If the number of arguments available is not the same
275 * then <nb> an error is throwed.
276 */
277__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
278{
279 if (lua_gettop(L) == nb)
280 return;
281 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
282}
283
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100284/* This fucntion push an error string prefixed by the file name
285 * and the line number where the error is encountered.
286 */
287static int hlua_pusherror(lua_State *L, const char *fmt, ...)
288{
289 va_list argp;
290 va_start(argp, fmt);
291 luaL_where(L, 1);
292 lua_pushvfstring(L, fmt, argp);
293 va_end(argp);
294 lua_concat(L, 2);
295 return 1;
296}
297
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100298/* This function register a new signal. "lua" is the current lua
299 * execution context. It contains a pointer to the associated task.
300 * "link" is a list head attached to an other task that must be wake
301 * the lua task if an event occurs. This is useful with external
302 * events like TCP I/O or sleep functions. This funcion allocate
303 * memory for the signal.
304 */
Thierry FOURNIER847ca662016-12-16 13:07:22 +0100305static struct hlua_com *hlua_com_new(struct list *purge, struct list *event, struct task *wakeup)
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100306{
307 struct hlua_com *com = pool_alloc2(pool2_hlua_com);
308 if (!com)
Thierry FOURNIER847ca662016-12-16 13:07:22 +0100309 return NULL;
310 LIST_ADDQ(purge, &com->purge_me);
311 LIST_ADDQ(event, &com->wake_me);
312 com->task = wakeup;
313 return com;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100314}
315
316/* This function purge all the pending signals when the LUA execution
317 * is finished. This prevent than a coprocess try to wake a deleted
318 * task. This function remove the memory associated to the signal.
319 */
Thierry FOURNIER847ca662016-12-16 13:07:22 +0100320static void hlua_com_purge(struct list *purge)
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100321{
322 struct hlua_com *com, *back;
323
324 /* Delete all pending communication signals. */
Thierry FOURNIER847ca662016-12-16 13:07:22 +0100325 list_for_each_entry_safe(com, back, purge, purge_me) {
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100326 LIST_DEL(&com->purge_me);
327 LIST_DEL(&com->wake_me);
328 pool_free2(pool2_hlua_com, com);
329 }
330}
331
332/* This function sends signals. It wakes all the tasks attached
333 * to a list head, and remove the signal, and free the used
334 * memory.
335 */
336static void hlua_com_wake(struct list *wake)
337{
338 struct hlua_com *com, *back;
339
340 /* Wake task and delete all pending communication signals. */
341 list_for_each_entry_safe(com, back, wake, wake_me) {
342 LIST_DEL(&com->purge_me);
343 LIST_DEL(&com->wake_me);
344 task_wakeup(com->task, TASK_WOKEN_MSG);
345 pool_free2(pool2_hlua_com, com);
346 }
347}
348
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100349/* This functions is used with sample fetch and converters. It
350 * converts the HAProxy configuration argument in a lua stack
351 * values.
352 *
353 * It takes an array of "arg", and each entry of the array is
354 * converted and pushed in the LUA stack.
355 */
356static int hlua_arg2lua(lua_State *L, const struct arg *arg)
357{
358 switch (arg->type) {
359 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100360 case ARGT_TIME:
361 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100362 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100363 break;
364
365 case ARGT_STR:
366 lua_pushlstring(L, arg->data.str.str, arg->data.str.len);
367 break;
368
369 case ARGT_IPV4:
370 case ARGT_IPV6:
371 case ARGT_MSK4:
372 case ARGT_MSK6:
373 case ARGT_FE:
374 case ARGT_BE:
375 case ARGT_TAB:
376 case ARGT_SRV:
377 case ARGT_USR:
378 case ARGT_MAP:
379 default:
380 lua_pushnil(L);
381 break;
382 }
383 return 1;
384}
385
386/* This function take one entrie in an LUA stack at the index "ud",
387 * and try to convert it in an HAProxy argument entry. This is useful
388 * with sample fetch wrappers. The input arguments are gived to the
389 * lua wrapper and converted as arg list by thi function.
390 */
391static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
392{
393 switch (lua_type(L, ud)) {
394
395 case LUA_TNUMBER:
396 case LUA_TBOOLEAN:
397 arg->type = ARGT_SINT;
398 arg->data.sint = lua_tointeger(L, ud);
399 break;
400
401 case LUA_TSTRING:
402 arg->type = ARGT_STR;
403 arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len);
404 break;
405
406 case LUA_TUSERDATA:
407 case LUA_TNIL:
408 case LUA_TTABLE:
409 case LUA_TFUNCTION:
410 case LUA_TTHREAD:
411 case LUA_TLIGHTUSERDATA:
412 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200413 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100414 break;
415 }
416 return 1;
417}
418
419/* the following functions are used to convert a struct sample
420 * in Lua type. This useful to convert the return of the
421 * fetchs or converters.
422 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100423static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100424{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200425 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100426 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100427 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200428 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100429 break;
430
431 case SMP_T_BIN:
432 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200433 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100434 break;
435
436 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200437 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100438 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
439 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
440 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
441 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
442 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
443 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
444 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
445 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
446 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200447 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100448 break;
449 default:
450 lua_pushnil(L);
451 break;
452 }
453 break;
454
455 case SMP_T_IPV4:
456 case SMP_T_IPV6:
457 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200458 if (sample_casts[smp->data.type][SMP_T_STR] &&
459 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200460 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100461 else
462 lua_pushnil(L);
463 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100464 default:
465 lua_pushnil(L);
466 break;
467 }
468 return 1;
469}
470
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100471/* the following functions are used to convert a struct sample
472 * in Lua strings. This is useful to convert the return of the
473 * fetchs or converters.
474 */
475static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
476{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200477 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100478
479 case SMP_T_BIN:
480 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200481 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100482 break;
483
484 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200485 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100486 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
487 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
488 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
489 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
490 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
491 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
492 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
493 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
494 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200495 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100496 break;
497 default:
498 lua_pushstring(L, "");
499 break;
500 }
501 break;
502
503 case SMP_T_SINT:
504 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505 case SMP_T_IPV4:
506 case SMP_T_IPV6:
507 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200508 if (sample_casts[smp->data.type][SMP_T_STR] &&
509 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200510 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100511 else
512 lua_pushstring(L, "");
513 break;
514 default:
515 lua_pushstring(L, "");
516 break;
517 }
518 return 1;
519}
520
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100521/* the following functions are used to convert an Lua type in a
522 * struct sample. This is useful to provide data from a converter
523 * to the LUA code.
524 */
525static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
526{
527 switch (lua_type(L, ud)) {
528
529 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200530 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200531 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100532 break;
533
534
535 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200536 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200537 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100538 break;
539
540 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200541 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100542 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200543 smp->data.u.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100544 break;
545
546 case LUA_TUSERDATA:
547 case LUA_TNIL:
548 case LUA_TTABLE:
549 case LUA_TFUNCTION:
550 case LUA_TTHREAD:
551 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200552 case LUA_TNONE:
553 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200554 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200555 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100556 break;
557 }
558 return 1;
559}
560
561/* This function check the "argp" builded by another conversion function
562 * is in accord with the expected argp defined by the "mask". The fucntion
563 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100564 *
565 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
566 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100567 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100568__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100569 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100570{
571 int min_arg;
572 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100573 struct proxy *px;
574 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100575
576 idx = 0;
577 min_arg = ARGM(mask);
578 mask >>= ARGM_BITS;
579
580 while (1) {
581
582 /* Check oversize. */
583 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100584 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100585 }
586
587 /* Check for mandatory arguments. */
588 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100589 if (idx < min_arg) {
590
591 /* If miss other argument than the first one, we return an error. */
592 if (idx > 0)
593 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
594
595 /* If first argument have a certain type, some default values
596 * may be used. See the function smp_resolve_args().
597 */
598 switch (mask & ARGT_MASK) {
599
600 case ARGT_FE:
601 if (!(p->cap & PR_CAP_FE))
602 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
603 argp[idx].data.prx = p;
604 argp[idx].type = ARGT_FE;
605 argp[idx+1].type = ARGT_STOP;
606 break;
607
608 case ARGT_BE:
609 if (!(p->cap & PR_CAP_BE))
610 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
611 argp[idx].data.prx = p;
612 argp[idx].type = ARGT_BE;
613 argp[idx+1].type = ARGT_STOP;
614 break;
615
616 case ARGT_TAB:
617 argp[idx].data.prx = p;
618 argp[idx].type = ARGT_TAB;
619 argp[idx+1].type = ARGT_STOP;
620 break;
621
622 default:
623 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
624 break;
625 }
626 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100627 return 0;
628 }
629
630 /* Check for exceed the number of requiered argument. */
631 if ((mask & ARGT_MASK) == ARGT_STOP &&
632 argp[idx].type != ARGT_STOP) {
633 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
634 }
635
636 if ((mask & ARGT_MASK) == ARGT_STOP &&
637 argp[idx].type == ARGT_STOP) {
638 return 0;
639 }
640
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100641 /* Convert some argument types. */
642 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100643 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100644 if (argp[idx].type != ARGT_SINT)
645 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
646 argp[idx].type = ARGT_SINT;
647 break;
648
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100649 case ARGT_TIME:
650 if (argp[idx].type != ARGT_SINT)
651 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200652 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100653 break;
654
655 case ARGT_SIZE:
656 if (argp[idx].type != ARGT_SINT)
657 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200658 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100659 break;
660
661 case ARGT_FE:
662 if (argp[idx].type != ARGT_STR)
663 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
664 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
665 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200666 argp[idx].data.prx = proxy_fe_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100667 if (!argp[idx].data.prx)
668 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
669 argp[idx].type = ARGT_FE;
670 break;
671
672 case ARGT_BE:
673 if (argp[idx].type != ARGT_STR)
674 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
675 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
676 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200677 argp[idx].data.prx = proxy_be_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100678 if (!argp[idx].data.prx)
679 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
680 argp[idx].type = ARGT_BE;
681 break;
682
683 case ARGT_TAB:
684 if (argp[idx].type != ARGT_STR)
685 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
686 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
687 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200688 argp[idx].data.prx = proxy_tbl_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100689 if (!argp[idx].data.prx)
690 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
691 argp[idx].type = ARGT_TAB;
692 break;
693
694 case ARGT_SRV:
695 if (argp[idx].type != ARGT_STR)
696 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
697 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
698 trash.str[argp[idx].data.str.len] = 0;
699 sname = strrchr(trash.str, '/');
700 if (sname) {
701 *sname++ = '\0';
702 pname = trash.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200703 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100704 if (!px)
705 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
706 }
707 else {
708 sname = trash.str;
709 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100710 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100711 argp[idx].data.srv = findserver(px, sname);
712 if (!argp[idx].data.srv)
713 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
714 argp[idx].type = ARGT_SRV;
715 break;
716
717 case ARGT_IPV4:
718 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
719 trash.str[argp[idx].data.str.len] = 0;
720 if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4))
721 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
722 argp[idx].type = ARGT_IPV4;
723 break;
724
725 case ARGT_MSK4:
726 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
727 trash.str[argp[idx].data.str.len] = 0;
728 if (!str2mask(trash.str, &argp[idx].data.ipv4))
729 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
730 argp[idx].type = ARGT_MSK4;
731 break;
732
733 case ARGT_IPV6:
734 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
735 trash.str[argp[idx].data.str.len] = 0;
736 if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6))
737 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
738 argp[idx].type = ARGT_IPV6;
739 break;
740
741 case ARGT_MSK6:
742 case ARGT_MAP:
743 case ARGT_REG:
744 case ARGT_USR:
745 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100746 break;
747 }
748
749 /* Check for type of argument. */
750 if ((mask & ARGT_MASK) != argp[idx].type) {
751 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
752 arg_type_names[(mask & ARGT_MASK)],
753 arg_type_names[argp[idx].type & ARGT_MASK]);
754 WILL_LJMP(luaL_argerror(L, first + idx, msg));
755 }
756
757 /* Next argument. */
758 mask >>= ARGT_BITS;
759 idx++;
760 }
761}
762
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100763/*
764 * The following functions are used to make correspondance between the the
765 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100766 *
767 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100768 * - hlua_sethlua : create the association between hlua context and lua_state.
769 */
770static inline struct hlua *hlua_gethlua(lua_State *L)
771{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100772 struct hlua **hlua = lua_getextraspace(L);
773 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100774}
775static inline void hlua_sethlua(struct hlua *hlua)
776{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100777 struct hlua **hlua_store = lua_getextraspace(hlua->T);
778 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100779}
780
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100781/* This function is used to send logs. It try to send on screen (stderr)
782 * and on the default syslog server.
783 */
784static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
785{
786 struct tm tm;
787 char *p;
788
789 /* Cleanup the log message. */
790 p = trash.str;
791 for (; *msg != '\0'; msg++, p++) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200792 if (p >= trash.str + trash.size - 1) {
793 /* Break the message if exceed the buffer size. */
794 *(p-4) = ' ';
795 *(p-3) = '.';
796 *(p-2) = '.';
797 *(p-1) = '.';
798 break;
799 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100800 if (isprint(*msg))
801 *p = *msg;
802 else
803 *p = '.';
804 }
805 *p = '\0';
806
Thierry FOURNIER5554e292015-09-09 11:21:37 +0200807 send_log(px, level, "%s\n", trash.str);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100808 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200809 get_localtime(date.tv_sec, &tm);
810 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100811 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
812 (int)getpid(), trash.str);
813 fflush(stderr);
814 }
815}
816
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100817/* This function just ensure that the yield will be always
818 * returned with a timeout and permit to set some flags
819 */
820__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100821 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100822{
823 struct hlua *hlua = hlua_gethlua(L);
824
825 /* Set the wake timeout. If timeout is required, we set
826 * the expiration time.
827 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200828 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100829
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100830 hlua->flags |= flags;
831
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100832 /* Process the yield. */
833 WILL_LJMP(lua_yieldk(L, nresults, ctx, k));
834}
835
Willy Tarreau87b09662015-04-03 00:22:06 +0200836/* This function initialises the Lua environment stored in the stream.
837 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100838 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200839 *
840 * This function is particular. it initialises a new Lua thread. If the
841 * initialisation fails (example: out of memory error), the lua function
842 * throws an error (longjmp).
843 *
844 * This function manipulates two Lua stack: the main and the thread. Only
845 * the main stack can fail. The thread is not manipulated. This function
846 * MUST NOT manipulate the created thread stack state, because is not
847 * proctected agains error throwed by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100848 */
849int hlua_ctx_init(struct hlua *lua, struct task *task)
850{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200851 if (!SET_SAFE_LJMP(gL.T)) {
852 lua->Tref = LUA_REFNIL;
853 return 0;
854 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100855 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100856 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100857 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100858 lua->T = lua_newthread(gL.T);
859 if (!lua->T) {
860 lua->Tref = LUA_REFNIL;
861 return 0;
862 }
863 hlua_sethlua(lua);
864 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
865 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200866 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100867 return 1;
868}
869
Willy Tarreau87b09662015-04-03 00:22:06 +0200870/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100871 * is destroyed. The destroy also the memory context. The struct "lua"
872 * is not freed.
873 */
874void hlua_ctx_destroy(struct hlua *lua)
875{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100876 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100877 return;
878
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100879 if (!lua->T)
880 goto end;
881
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100882 /* Purge all the pending signals. */
Thierry FOURNIER847ca662016-12-16 13:07:22 +0100883 hlua_com_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100884
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100885 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
886 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200887
888 /* Forces a garbage collecting process. If the Lua program is finished
889 * without error, we run the GC on the thread pointer. Its freed all
890 * the unused memory.
891 * If the thread is finnish with an error or is currently yielded,
892 * it seems that the GC applied on the thread doesn't clean anything,
893 * so e run the GC on the main thread.
894 * NOTE: maybe this action locks all the Lua threads untiml the en of
895 * the garbage collection.
896 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200897 if (lua->flags & HLUA_MUST_GC) {
898 lua_gc(lua->T, LUA_GCCOLLECT, 0);
899 if (lua_status(lua->T) != LUA_OK)
900 lua_gc(gL.T, LUA_GCCOLLECT, 0);
901 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200902
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200903 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100904
905end:
906 pool_free2(pool2_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100907}
908
909/* This function is used to restore the Lua context when a coroutine
910 * fails. This function copy the common memory between old coroutine
911 * and the new coroutine. The old coroutine is destroyed, and its
912 * replaced by the new coroutine.
913 * If the flag "keep_msg" is set, the last entry of the old is assumed
914 * as string error message and it is copied in the new stack.
915 */
916static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
917{
918 lua_State *T;
919 int new_ref;
920
921 /* Renew the main LUA stack doesn't have sense. */
922 if (lua == &gL)
923 return 0;
924
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100925 /* New Lua coroutine. */
926 T = lua_newthread(gL.T);
927 if (!T)
928 return 0;
929
930 /* Copy last error message. */
931 if (keep_msg)
932 lua_xmove(lua->T, T, 1);
933
934 /* Copy data between the coroutines. */
935 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
936 lua_xmove(lua->T, T, 1);
937 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
938
939 /* Destroy old data. */
940 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
941
942 /* The thread is garbage collected by Lua. */
943 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
944
945 /* Fill the struct with the new coroutine values. */
946 lua->Mref = new_ref;
947 lua->T = T;
948 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
949
950 /* Set context. */
951 hlua_sethlua(lua);
952
953 return 1;
954}
955
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100956void hlua_hook(lua_State *L, lua_Debug *ar)
957{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100958 struct hlua *hlua = hlua_gethlua(L);
959
960 /* Lua cannot yield when its returning from a function,
961 * so, we can fix the interrupt hook to 1 instruction,
962 * expecting that the function is finnished.
963 */
964 if (lua_gethookmask(L) & LUA_MASKRET) {
965 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
966 return;
967 }
968
969 /* restore the interrupt condition. */
970 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
971
972 /* If we interrupt the Lua processing in yieldable state, we yield.
973 * If the state is not yieldable, trying yield causes an error.
974 */
975 if (lua_isyieldable(L))
976 WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
977
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +0100978 /* If we cannot yield, update the clock and check the timeout. */
979 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200980 hlua->run_time += now_ms - hlua->start_time;
981 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100982 lua_pushfstring(L, "execution timeout");
983 WILL_LJMP(lua_error(L));
984 }
985
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200986 /* Update the start time. */
987 hlua->start_time = now_ms;
988
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100989 /* Try to interrupt the process at the end of the current
990 * unyieldable function.
991 */
992 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100993}
994
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100995/* This function start or resumes the Lua stack execution. If the flag
996 * "yield_allowed" if no set and the LUA stack execution returns a yield
997 * The function return an error.
998 *
999 * The function can returns 4 values:
1000 * - HLUA_E_OK : The execution is terminated without any errors.
1001 * - HLUA_E_AGAIN : The execution must continue at the next associated
1002 * task wakeup.
1003 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
1004 * the top of the stack.
1005 * - HLUA_E_ERR : An error has occured without error message.
1006 *
1007 * If an error occured, the stack is renewed and it is ready to run new
1008 * LUA code.
1009 */
1010static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1011{
1012 int ret;
1013 const char *msg;
1014
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001015 /* Initialise run time counter. */
1016 if (!HLUA_IS_RUNNING(lua))
1017 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001018
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001019resume_execution:
1020
1021 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1022 * instructions. it is used for preventing infinite loops.
1023 */
1024 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1025
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001026 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001027 HLUA_SET_RUN(lua);
1028 HLUA_CLR_CTRLYIELD(lua);
1029 HLUA_CLR_WAKERESWR(lua);
1030 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001031
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001032 /* Update the start time. */
1033 lua->start_time = now_ms;
1034
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001035 /* Call the function. */
1036 ret = lua_resume(lua->T, gL.T, lua->nargs);
1037 switch (ret) {
1038
1039 case LUA_OK:
1040 ret = HLUA_E_OK;
1041 break;
1042
1043 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001044 /* Check if the execution timeout is expired. It it is the case, we
1045 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001046 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001047 tv_update_date(0, 1);
1048 lua->run_time += now_ms - lua->start_time;
1049 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001050 lua_settop(lua->T, 0); /* Empty the stack. */
1051 if (!lua_checkstack(lua->T, 1)) {
1052 ret = HLUA_E_ERR;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001053 break;
1054 }
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001055 lua_pushfstring(lua->T, "execution timeout");
1056 ret = HLUA_E_ERRMSG;
1057 break;
1058 }
1059 /* Process the forced yield. if the general yield is not allowed or
1060 * if no task were associated this the current Lua execution
1061 * coroutine, we resume the execution. Else we want to return in the
1062 * scheduler and we want to be waked up again, to continue the
1063 * current Lua execution. So we schedule our own task.
1064 */
1065 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001066 if (!yield_allowed || !lua->task)
1067 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001068 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001069 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001070 if (!yield_allowed) {
1071 lua_settop(lua->T, 0); /* Empty the stack. */
1072 if (!lua_checkstack(lua->T, 1)) {
1073 ret = HLUA_E_ERR;
1074 break;
1075 }
1076 lua_pushfstring(lua->T, "yield not allowed");
1077 ret = HLUA_E_ERRMSG;
1078 break;
1079 }
1080 ret = HLUA_E_AGAIN;
1081 break;
1082
1083 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001084
1085 /* Special exit case. The traditionnal exit is returned as an error
1086 * because the errors ares the only one mean to return immediately
1087 * from and lua execution.
1088 */
1089 if (lua->flags & HLUA_EXIT) {
1090 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001091 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001092 break;
1093 }
1094
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001095 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001096 if (!lua_checkstack(lua->T, 1)) {
1097 ret = HLUA_E_ERR;
1098 break;
1099 }
1100 msg = lua_tostring(lua->T, -1);
1101 lua_settop(lua->T, 0); /* Empty the stack. */
1102 lua_pop(lua->T, 1);
1103 if (msg)
1104 lua_pushfstring(lua->T, "runtime error: %s", msg);
1105 else
1106 lua_pushfstring(lua->T, "unknown runtime error");
1107 ret = HLUA_E_ERRMSG;
1108 break;
1109
1110 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001111 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001112 lua_settop(lua->T, 0); /* Empty the stack. */
1113 if (!lua_checkstack(lua->T, 1)) {
1114 ret = HLUA_E_ERR;
1115 break;
1116 }
1117 lua_pushfstring(lua->T, "out of memory error");
1118 ret = HLUA_E_ERRMSG;
1119 break;
1120
1121 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001122 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001123 if (!lua_checkstack(lua->T, 1)) {
1124 ret = HLUA_E_ERR;
1125 break;
1126 }
1127 msg = lua_tostring(lua->T, -1);
1128 lua_settop(lua->T, 0); /* Empty the stack. */
1129 lua_pop(lua->T, 1);
1130 if (msg)
1131 lua_pushfstring(lua->T, "message handler error: %s", msg);
1132 else
1133 lua_pushfstring(lua->T, "message handler error");
1134 ret = HLUA_E_ERRMSG;
1135 break;
1136
1137 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001138 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001139 lua_settop(lua->T, 0); /* Empty the stack. */
1140 if (!lua_checkstack(lua->T, 1)) {
1141 ret = HLUA_E_ERR;
1142 break;
1143 }
1144 lua_pushfstring(lua->T, "unknonwn error");
1145 ret = HLUA_E_ERRMSG;
1146 break;
1147 }
1148
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001149 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001150 if (lua->flags & HLUA_MUST_GC &&
1151 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001152 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1153
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001154 switch (ret) {
1155 case HLUA_E_AGAIN:
1156 break;
1157
1158 case HLUA_E_ERRMSG:
Thierry FOURNIER847ca662016-12-16 13:07:22 +01001159 hlua_com_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001160 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001161 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001162 break;
1163
1164 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001165 HLUA_CLR_RUN(lua);
Thierry FOURNIER847ca662016-12-16 13:07:22 +01001166 hlua_com_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001167 hlua_ctx_renew(lua, 0);
1168 break;
1169
1170 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001171 HLUA_CLR_RUN(lua);
Thierry FOURNIER847ca662016-12-16 13:07:22 +01001172 hlua_com_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001173 break;
1174 }
1175
1176 return ret;
1177}
1178
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001179/* This function exit the current code. */
1180__LJMP static int hlua_done(lua_State *L)
1181{
1182 struct hlua *hlua = hlua_gethlua(L);
1183
1184 hlua->flags |= HLUA_EXIT;
1185 WILL_LJMP(lua_error(L));
1186
1187 return 0;
1188}
1189
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001190/* This function is an LUA binding. It provides a function
1191 * for deleting ACL from a referenced ACL file.
1192 */
1193__LJMP static int hlua_del_acl(lua_State *L)
1194{
1195 const char *name;
1196 const char *key;
1197 struct pat_ref *ref;
1198
1199 MAY_LJMP(check_args(L, 2, "del_acl"));
1200
1201 name = MAY_LJMP(luaL_checkstring(L, 1));
1202 key = MAY_LJMP(luaL_checkstring(L, 2));
1203
1204 ref = pat_ref_lookup(name);
1205 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001206 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001207
1208 pat_ref_delete(ref, key);
1209 return 0;
1210}
1211
1212/* This function is an LUA binding. It provides a function
1213 * for deleting map entry from a referenced map file.
1214 */
1215static int hlua_del_map(lua_State *L)
1216{
1217 const char *name;
1218 const char *key;
1219 struct pat_ref *ref;
1220
1221 MAY_LJMP(check_args(L, 2, "del_map"));
1222
1223 name = MAY_LJMP(luaL_checkstring(L, 1));
1224 key = MAY_LJMP(luaL_checkstring(L, 2));
1225
1226 ref = pat_ref_lookup(name);
1227 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001228 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001229
1230 pat_ref_delete(ref, key);
1231 return 0;
1232}
1233
1234/* This function is an LUA binding. It provides a function
1235 * for adding ACL pattern from a referenced ACL file.
1236 */
1237static int hlua_add_acl(lua_State *L)
1238{
1239 const char *name;
1240 const char *key;
1241 struct pat_ref *ref;
1242
1243 MAY_LJMP(check_args(L, 2, "add_acl"));
1244
1245 name = MAY_LJMP(luaL_checkstring(L, 1));
1246 key = MAY_LJMP(luaL_checkstring(L, 2));
1247
1248 ref = pat_ref_lookup(name);
1249 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001250 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001251
1252 if (pat_ref_find_elt(ref, key) == NULL)
1253 pat_ref_add(ref, key, NULL, NULL);
1254 return 0;
1255}
1256
1257/* This function is an LUA binding. It provides a function
1258 * for setting map pattern and sample from a referenced map
1259 * file.
1260 */
1261static int hlua_set_map(lua_State *L)
1262{
1263 const char *name;
1264 const char *key;
1265 const char *value;
1266 struct pat_ref *ref;
1267
1268 MAY_LJMP(check_args(L, 3, "set_map"));
1269
1270 name = MAY_LJMP(luaL_checkstring(L, 1));
1271 key = MAY_LJMP(luaL_checkstring(L, 2));
1272 value = MAY_LJMP(luaL_checkstring(L, 3));
1273
1274 ref = pat_ref_lookup(name);
1275 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001276 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001277
1278 if (pat_ref_find_elt(ref, key) != NULL)
1279 pat_ref_set(ref, key, value, NULL);
1280 else
1281 pat_ref_add(ref, key, value, NULL);
1282 return 0;
1283}
1284
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001285/* A class is a lot of memory that contain data. This data can be a table,
1286 * an integer or user data. This data is associated with a metatable. This
1287 * metatable have an original version registred in the global context with
1288 * the name of the object (_G[<name>] = <metable> ).
1289 *
1290 * A metable is a table that modify the standard behavior of a standard
1291 * access to the associated data. The entries of this new metatable are
1292 * defined as is:
1293 *
1294 * http://lua-users.org/wiki/MetatableEvents
1295 *
1296 * __index
1297 *
1298 * we access an absent field in a table, the result is nil. This is
1299 * true, but it is not the whole truth. Actually, such access triggers
1300 * the interpreter to look for an __index metamethod: If there is no
1301 * such method, as usually happens, then the access results in nil;
1302 * otherwise, the metamethod will provide the result.
1303 *
1304 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1305 * the key does not appear in the table, but the metatable has an __index
1306 * property:
1307 *
1308 * - if the value is a function, the function is called, passing in the
1309 * table and the key; the return value of that function is returned as
1310 * the result.
1311 *
1312 * - if the value is another table, the value of the key in that table is
1313 * asked for and returned (and if it doesn't exist in that table, but that
1314 * table's metatable has an __index property, then it continues on up)
1315 *
1316 * - Use "rawget(myTable,key)" to skip this metamethod.
1317 *
1318 * http://www.lua.org/pil/13.4.1.html
1319 *
1320 * __newindex
1321 *
1322 * Like __index, but control property assignment.
1323 *
1324 * __mode - Control weak references. A string value with one or both
1325 * of the characters 'k' and 'v' which specifies that the the
1326 * keys and/or values in the table are weak references.
1327 *
1328 * __call - Treat a table like a function. When a table is followed by
1329 * parenthesis such as "myTable( 'foo' )" and the metatable has
1330 * a __call key pointing to a function, that function is invoked
1331 * (passing any specified arguments) and the return value is
1332 * returned.
1333 *
1334 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1335 * called, if the metatable for myTable has a __metatable
1336 * key, the value of that key is returned instead of the
1337 * actual metatable.
1338 *
1339 * __tostring - Control string representation. When the builtin
1340 * "tostring( myTable )" function is called, if the metatable
1341 * for myTable has a __tostring property set to a function,
1342 * that function is invoked (passing myTable to it) and the
1343 * return value is used as the string representation.
1344 *
1345 * __len - Control table length. When the table length is requested using
1346 * the length operator ( '#' ), if the metatable for myTable has
1347 * a __len key pointing to a function, that function is invoked
1348 * (passing myTable to it) and the return value used as the value
1349 * of "#myTable".
1350 *
1351 * __gc - Userdata finalizer code. When userdata is set to be garbage
1352 * collected, if the metatable has a __gc field pointing to a
1353 * function, that function is first invoked, passing the userdata
1354 * to it. The __gc metamethod is not called for tables.
1355 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1356 *
1357 * Special metamethods for redefining standard operators:
1358 * http://www.lua.org/pil/13.1.html
1359 *
1360 * __add "+"
1361 * __sub "-"
1362 * __mul "*"
1363 * __div "/"
1364 * __unm "!"
1365 * __pow "^"
1366 * __concat ".."
1367 *
1368 * Special methods for redfining standar relations
1369 * http://www.lua.org/pil/13.2.html
1370 *
1371 * __eq "=="
1372 * __lt "<"
1373 * __le "<="
1374 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001375
1376/*
1377 *
1378 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001379 * Class Map
1380 *
1381 *
1382 */
1383
1384/* Returns a struct hlua_map if the stack entry "ud" is
1385 * a class session, otherwise it throws an error.
1386 */
1387__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1388{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001389 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001390}
1391
1392/* This function is the map constructor. It don't need
1393 * the class Map object. It creates and return a new Map
1394 * object. It must be called only during "body" or "init"
1395 * context because it process some filesystem accesses.
1396 */
1397__LJMP static int hlua_map_new(struct lua_State *L)
1398{
1399 const char *fn;
1400 int match = PAT_MATCH_STR;
1401 struct sample_conv conv;
1402 const char *file = "";
1403 int line = 0;
1404 lua_Debug ar;
1405 char *err = NULL;
1406 struct arg args[2];
1407
1408 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1409 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1410
1411 fn = MAY_LJMP(luaL_checkstring(L, 1));
1412
1413 if (lua_gettop(L) >= 2) {
1414 match = MAY_LJMP(luaL_checkinteger(L, 2));
1415 if (match < 0 || match >= PAT_MATCH_NUM)
1416 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1417 }
1418
1419 /* Get Lua filename and line number. */
1420 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1421 lua_getinfo(L, "Sl", &ar); /* get info about it */
1422 if (ar.currentline > 0) { /* is there info? */
1423 file = ar.short_src;
1424 line = ar.currentline;
1425 }
1426 }
1427
1428 /* fill fake sample_conv struct. */
1429 conv.kw = ""; /* unused. */
1430 conv.process = NULL; /* unused. */
1431 conv.arg_mask = 0; /* unused. */
1432 conv.val_args = NULL; /* unused. */
1433 conv.out_type = SMP_T_STR;
1434 conv.private = (void *)(long)match;
1435 switch (match) {
1436 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1437 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1438 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1439 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1440 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1441 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1442 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001443 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001444 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1445 default:
1446 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1447 }
1448
1449 /* fill fake args. */
1450 args[0].type = ARGT_STR;
1451 args[0].data.str.str = (char *)fn;
1452 args[1].type = ARGT_STOP;
1453
1454 /* load the map. */
1455 if (!sample_load_map(args, &conv, file, line, &err)) {
1456 /* error case: we cant use luaL_error because we must
1457 * free the err variable.
1458 */
1459 luaL_where(L, 1);
1460 lua_pushfstring(L, "'new': %s.", err);
1461 lua_concat(L, 2);
1462 free(err);
1463 WILL_LJMP(lua_error(L));
1464 }
1465
1466 /* create the lua object. */
1467 lua_newtable(L);
1468 lua_pushlightuserdata(L, args[0].data.map);
1469 lua_rawseti(L, -2, 0);
1470
1471 /* Pop a class Map metatable and affect it to the userdata. */
1472 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1473 lua_setmetatable(L, -2);
1474
1475
1476 return 1;
1477}
1478
1479__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1480{
1481 struct map_descriptor *desc;
1482 struct pattern *pat;
1483 struct sample smp;
1484
1485 MAY_LJMP(check_args(L, 2, "lookup"));
1486 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001487 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001488 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001489 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001490 }
1491 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001492 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001493 smp.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001494 smp.data.u.str.str = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.len));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001495 }
1496
1497 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001498 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001499 if (str)
1500 lua_pushstring(L, "");
1501 else
1502 lua_pushnil(L);
1503 return 1;
1504 }
1505
1506 /* The Lua pattern must return a string, so we can't check the returned type */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001507 lua_pushlstring(L, pat->data->u.str.str, pat->data->u.str.len);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001508 return 1;
1509}
1510
1511__LJMP static int hlua_map_lookup(struct lua_State *L)
1512{
1513 return _hlua_map_lookup(L, 0);
1514}
1515
1516__LJMP static int hlua_map_slookup(struct lua_State *L)
1517{
1518 return _hlua_map_lookup(L, 1);
1519}
1520
1521/*
1522 *
1523 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001524 * Class Socket
1525 *
1526 *
1527 */
1528
1529__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1530{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001531 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001532}
1533
1534/* This function is the handler called for each I/O on the established
1535 * connection. It is used for notify space avalaible to send or data
1536 * received.
1537 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001538static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001539{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001540 struct stream_interface *si = appctx->owner;
Willy Tarreau50fe03b2014-11-28 13:59:31 +01001541 struct connection *c = objt_conn(si_opposite(si)->end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001542
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001543 /* If the connection object is not avalaible, close all the
1544 * streams and wakeup everithing waiting for.
1545 */
1546 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001547 si_shutw(si);
1548 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001549 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001550 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1551 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001552 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001553 }
1554
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001555 /* If we cant write, wakeup the pending write signals. */
1556 if (channel_output_closed(si_ic(si)))
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001557 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001558
1559 /* If we cant read, wakeup the pending read signals. */
1560 if (channel_input_closed(si_oc(si)))
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001561 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001562
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001563 /* if the connection is not estabkished, inform the stream that we want
1564 * to be notified whenever the connection completes.
1565 */
1566 if (!(c->flags & CO_FL_CONNECTED)) {
1567 si_applet_cant_get(si);
1568 si_applet_cant_put(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001569 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001570 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001571
1572 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001573 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001574
1575 /* Wake the tasks which wants to write if the buffer have avalaible space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001576 if (channel_may_recv(si_ic(si)))
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001577 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001578
1579 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001580 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001581 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001582}
1583
Willy Tarreau87b09662015-04-03 00:22:06 +02001584/* This function is called when the "struct stream" is destroyed.
1585 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001586 * Wake all the pending signals.
1587 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001588static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001589{
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001590 /* Remove my link in the original object. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001591 if (appctx->ctx.hlua_cosocket.socket)
1592 appctx->ctx.hlua_cosocket.socket->s = NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001593
1594 /* Wake all the task waiting for me. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001595 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1596 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001597}
1598
1599/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001600 * uses this object. If the stream does not exists, just quit.
1601 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001602 * pending signal can rest in the read and write lists. destroy
1603 * it.
1604 */
1605__LJMP static int hlua_socket_gc(lua_State *L)
1606{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001607 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001608 struct appctx *appctx;
1609
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001610 MAY_LJMP(check_args(L, 1, "__gc"));
1611
1612 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001613 if (!socket->s)
1614 return 0;
1615
Willy Tarreau87b09662015-04-03 00:22:06 +02001616 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001617 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001618 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001619 socket->s = NULL;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001620 appctx->ctx.hlua_cosocket.socket = NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001621
1622 return 0;
1623}
1624
1625/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001626 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001627 */
1628__LJMP static int hlua_socket_close(lua_State *L)
1629{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001630 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001631 struct appctx *appctx;
1632
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001633 MAY_LJMP(check_args(L, 1, "close"));
1634
1635 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001636 if (!socket->s)
1637 return 0;
1638
Willy Tarreau87b09662015-04-03 00:22:06 +02001639 /* Close the stream and remove the associated stop task. */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001640 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001641 appctx = objt_appctx(socket->s->si[0].end);
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001642 appctx->ctx.hlua_cosocket.socket = NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001643 socket->s = NULL;
1644
1645 return 0;
1646}
1647
1648/* This Lua function assumes that the stack contain three parameters.
1649 * 1 - USERDATA containing a struct socket
1650 * 2 - INTEGER with values of the macro defined below
1651 * If the integer is -1, we must read at most one line.
1652 * If the integer is -2, we ust read all the data until the
1653 * end of the stream.
1654 * If the integer is positive value, we must read a number of
1655 * bytes corresponding to this value.
1656 */
1657#define HLSR_READ_LINE (-1)
1658#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001659__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001660{
1661 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1662 int wanted = lua_tointeger(L, 2);
1663 struct hlua *hlua = hlua_gethlua(L);
1664 struct appctx *appctx;
1665 int len;
1666 int nblk;
1667 char *blk1;
1668 int len1;
1669 char *blk2;
1670 int len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001671 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001672 struct channel *oc;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001673
1674 /* Check if this lua stack is schedulable. */
1675 if (!hlua || !hlua->task)
1676 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1677 "'frontend', 'backend' or 'task'"));
1678
1679 /* check for connection closed. If some data where read, return it. */
1680 if (!socket->s)
1681 goto connection_closed;
1682
Willy Tarreau94aa6172015-03-13 14:19:06 +01001683 oc = &socket->s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001684 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685 /* Read line. */
Willy Tarreau81389672015-03-10 12:03:52 +01001686 nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001687 if (nblk < 0) /* Connection close. */
1688 goto connection_closed;
1689 if (nblk == 0) /* No data avalaible. */
1690 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001691
1692 /* remove final \r\n. */
1693 if (nblk == 1) {
1694 if (blk1[len1-1] == '\n') {
1695 len1--;
1696 skip_at_end++;
1697 if (blk1[len1-1] == '\r') {
1698 len1--;
1699 skip_at_end++;
1700 }
1701 }
1702 }
1703 else {
1704 if (blk2[len2-1] == '\n') {
1705 len2--;
1706 skip_at_end++;
1707 if (blk2[len2-1] == '\r') {
1708 len2--;
1709 skip_at_end++;
1710 }
1711 }
1712 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001713 }
1714
1715 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001716 /* Read all the available data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001717 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001718 if (nblk < 0) /* Connection close. */
1719 goto connection_closed;
1720 if (nblk == 0) /* No data avalaible. */
1721 goto connection_empty;
1722 }
1723
1724 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001725 /* Read a block of data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001726 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001727 if (nblk < 0) /* Connection close. */
1728 goto connection_closed;
1729 if (nblk == 0) /* No data avalaible. */
1730 goto connection_empty;
1731
1732 if (len1 > wanted) {
1733 nblk = 1;
1734 len1 = wanted;
1735 } if (nblk == 2 && len1 + len2 > wanted)
1736 len2 = wanted - len1;
1737 }
1738
1739 len = len1;
1740
1741 luaL_addlstring(&socket->b, blk1, len1);
1742 if (nblk == 2) {
1743 len += len2;
1744 luaL_addlstring(&socket->b, blk2, len2);
1745 }
1746
1747 /* Consume data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001748 bo_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001749
1750 /* Don't wait anything. */
Willy Tarreaude70fa12015-09-26 11:25:05 +02001751 stream_int_notify(&socket->s->si[0]);
1752 stream_int_update_applet(&socket->s->si[0]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001753
1754 /* If the pattern reclaim to read all the data
1755 * in the connection, got out.
1756 */
1757 if (wanted == HLSR_READ_ALL)
1758 goto connection_empty;
1759 else if (wanted >= 0 && len < wanted)
1760 goto connection_empty;
1761
1762 /* Return result. */
1763 luaL_pushresult(&socket->b);
1764 return 1;
1765
1766connection_closed:
1767
1768 /* If the buffer containds data. */
1769 if (socket->b.n > 0) {
1770 luaL_pushresult(&socket->b);
1771 return 1;
1772 }
1773 lua_pushnil(L);
1774 lua_pushstring(L, "connection closed.");
1775 return 2;
1776
1777connection_empty:
1778
1779 appctx = objt_appctx(socket->s->si[0].end);
Thierry FOURNIER847ca662016-12-16 13:07:22 +01001780 if (!hlua_com_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001781 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001782 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001783 return 0;
1784}
1785
1786/* This Lus function gets two parameters. The first one can be string
1787 * or a number. If the string is "*l", the user require one line. If
1788 * the string is "*a", the user require all the content of the stream.
1789 * If the value is a number, the user require a number of bytes equal
1790 * to the value. The default value is "*l" (a line).
1791 *
1792 * This paraeter with a variable type is converted in integer. This
1793 * integer takes this values:
1794 * -1 : read a line
1795 * -2 : read all the stream
1796 * >0 : amount if bytes.
1797 *
1798 * The second parameter is optinal. It contains a string that must be
1799 * concatenated with the read data.
1800 */
1801__LJMP static int hlua_socket_receive(struct lua_State *L)
1802{
1803 int wanted = HLSR_READ_LINE;
1804 const char *pattern;
1805 int type;
1806 char *error;
1807 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001808 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001809
1810 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1811 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1812
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001813 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001814
1815 /* check for pattern. */
1816 if (lua_gettop(L) >= 2) {
1817 type = lua_type(L, 2);
1818 if (type == LUA_TSTRING) {
1819 pattern = lua_tostring(L, 2);
1820 if (strcmp(pattern, "*a") == 0)
1821 wanted = HLSR_READ_ALL;
1822 else if (strcmp(pattern, "*l") == 0)
1823 wanted = HLSR_READ_LINE;
1824 else {
1825 wanted = strtoll(pattern, &error, 10);
1826 if (*error != '\0')
1827 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1828 }
1829 }
1830 else if (type == LUA_TNUMBER) {
1831 wanted = lua_tointeger(L, 2);
1832 if (wanted < 0)
1833 WILL_LJMP(luaL_error(L, "Unsupported size."));
1834 }
1835 }
1836
1837 /* Set pattern. */
1838 lua_pushinteger(L, wanted);
1839 lua_replace(L, 2);
1840
1841 /* init bufffer, and fiil it wih prefix. */
1842 luaL_buffinit(L, &socket->b);
1843
1844 /* Check prefix. */
1845 if (lua_gettop(L) >= 3) {
1846 if (lua_type(L, 3) != LUA_TSTRING)
1847 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1848 pattern = lua_tolstring(L, 3, &len);
1849 luaL_addlstring(&socket->b, pattern, len);
1850 }
1851
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001852 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001853}
1854
1855/* Write the Lua input string in the output buffer.
1856 * This fucntion returns a yield if no space are available.
1857 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001858static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001859{
1860 struct hlua_socket *socket;
1861 struct hlua *hlua = hlua_gethlua(L);
1862 struct appctx *appctx;
1863 size_t buf_len;
1864 const char *buf;
1865 int len;
1866 int send_len;
1867 int sent;
1868
1869 /* Check if this lua stack is schedulable. */
1870 if (!hlua || !hlua->task)
1871 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1872 "'frontend', 'backend' or 'task'"));
1873
1874 /* Get object */
1875 socket = MAY_LJMP(hlua_checksocket(L, 1));
1876 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001877 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001878
1879 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001880 if (!socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001881 lua_pushinteger(L, -1);
1882 return 1;
1883 }
1884
1885 /* Update the input buffer data. */
1886 buf += sent;
1887 send_len = buf_len - sent;
1888
1889 /* All the data are sent. */
1890 if (sent >= buf_len)
1891 return 1; /* Implicitly return the length sent. */
1892
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001893 /* Check if the buffer is avalaible because HAProxy doesn't allocate
1894 * the request buffer if its not required.
1895 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001896 if (socket->s->req.buf->size == 0) {
Christopher Faulet33834b12016-12-19 09:29:06 +01001897 appctx = hlua->task->context;
1898 if (!channel_alloc_buffer(&socket->s->req, &appctx->buffer_wait))
1899 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001900 }
1901
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001902 /* Check for avalaible space. */
Willy Tarreau94aa6172015-03-13 14:19:06 +01001903 len = buffer_total_space(socket->s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01001904 if (len <= 0) {
1905 appctx = objt_appctx(socket->s->si[0].end);
1906 if (!hlua_com_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task))
1907 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001908 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01001909 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001910
1911 /* send data */
1912 if (len < send_len)
1913 send_len = len;
Willy Tarreau94aa6172015-03-13 14:19:06 +01001914 len = bi_putblk(&socket->s->req, buf+sent, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001915
1916 /* "Not enough space" (-1), "Buffer too little to contain
1917 * the data" (-2) are not expected because the available length
1918 * is tested.
1919 * Other unknown error are also not expected.
1920 */
1921 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01001922 if (len == -1)
Willy Tarreau94aa6172015-03-13 14:19:06 +01001923 socket->s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001924
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001925 MAY_LJMP(hlua_socket_close(L));
1926 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001927 lua_pushinteger(L, -1);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001928 return 1;
1929 }
1930
1931 /* update buffers. */
Willy Tarreaude70fa12015-09-26 11:25:05 +02001932 stream_int_notify(&socket->s->si[0]);
1933 stream_int_update_applet(&socket->s->si[0]);
1934
Willy Tarreau94aa6172015-03-13 14:19:06 +01001935 socket->s->req.rex = TICK_ETERNITY;
1936 socket->s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001937
1938 /* Update length sent. */
1939 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001940 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001941
1942 /* All the data buffer is sent ? */
1943 if (sent + len >= buf_len)
1944 return 1;
1945
1946hlua_socket_write_yield_return:
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001947 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001948 return 0;
1949}
1950
1951/* This function initiate the send of data. It just check the input
1952 * parameters and push an integer in the Lua stack that contain the
1953 * amount of data writed in the buffer. This is used by the function
1954 * "hlua_socket_write_yield" that can yield.
1955 *
1956 * The Lua function gets between 3 and 4 parameters. The first one is
1957 * the associated object. The second is a string buffer. The third is
1958 * a facultative integer that represents where is the buffer position
1959 * of the start of the data that can send. The first byte is the
1960 * position "1". The default value is "1". The fourth argument is a
1961 * facultative integer that represents where is the buffer position
1962 * of the end of the data that can send. The default is the last byte.
1963 */
1964static int hlua_socket_send(struct lua_State *L)
1965{
1966 int i;
1967 int j;
1968 const char *buf;
1969 size_t buf_len;
1970
1971 /* Check number of arguments. */
1972 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
1973 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
1974
1975 /* Get the string. */
1976 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
1977
1978 /* Get and check j. */
1979 if (lua_gettop(L) == 4) {
1980 j = MAY_LJMP(luaL_checkinteger(L, 4));
1981 if (j < 0)
1982 j = buf_len + j + 1;
1983 if (j > buf_len)
1984 j = buf_len + 1;
1985 lua_pop(L, 1);
1986 }
1987 else
1988 j = buf_len;
1989
1990 /* Get and check i. */
1991 if (lua_gettop(L) == 3) {
1992 i = MAY_LJMP(luaL_checkinteger(L, 3));
1993 if (i < 0)
1994 i = buf_len + i + 1;
1995 if (i > buf_len)
1996 i = buf_len + 1;
1997 lua_pop(L, 1);
1998 } else
1999 i = 1;
2000
2001 /* Check bth i and j. */
2002 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002003 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002004 return 1;
2005 }
2006 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002007 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002008 return 1;
2009 }
2010 if (i == 0)
2011 i = 1;
2012 if (j == 0)
2013 j = 1;
2014
2015 /* Pop the string. */
2016 lua_pop(L, 1);
2017
2018 /* Update the buffer length. */
2019 buf += i - 1;
2020 buf_len = j - i + 1;
2021 lua_pushlstring(L, buf, buf_len);
2022
2023 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002024 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002025
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002026 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002027}
2028
Willy Tarreau22b0a682015-06-17 19:43:49 +02002029#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002030__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2031{
2032 static char buffer[SOCKET_INFO_MAX_LEN];
2033 int ret;
2034 int len;
2035 char *p;
2036
2037 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2038 if (ret <= 0) {
2039 lua_pushnil(L);
2040 return 1;
2041 }
2042
2043 if (ret == AF_UNIX) {
2044 lua_pushstring(L, buffer+1);
2045 return 1;
2046 }
2047 else if (ret == AF_INET6) {
2048 buffer[0] = '[';
2049 len = strlen(buffer);
2050 buffer[len] = ']';
2051 len++;
2052 buffer[len] = ':';
2053 len++;
2054 p = buffer;
2055 }
2056 else if (ret == AF_INET) {
2057 p = buffer + 1;
2058 len = strlen(p);
2059 p[len] = ':';
2060 len++;
2061 }
2062 else {
2063 lua_pushnil(L);
2064 return 1;
2065 }
2066
2067 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2068 lua_pushnil(L);
2069 return 1;
2070 }
2071
2072 lua_pushstring(L, p);
2073 return 1;
2074}
2075
2076/* Returns information about the peer of the connection. */
2077__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2078{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002079 struct hlua_socket *socket;
2080 struct connection *conn;
2081
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002082 MAY_LJMP(check_args(L, 1, "getpeername"));
2083
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002084 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002085
2086 /* Check if the tcp object is avalaible. */
2087 if (!socket->s) {
2088 lua_pushnil(L);
2089 return 1;
2090 }
2091
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002092 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002093 if (!conn) {
2094 lua_pushnil(L);
2095 return 1;
2096 }
2097
Willy Tarreaua71f6422016-11-16 17:00:14 +01002098 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002099 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Willy Tarreaua71f6422016-11-16 17:00:14 +01002100 lua_pushnil(L);
2101 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002102 }
2103
2104 return MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2105}
2106
2107/* Returns information about my connection side. */
2108static int hlua_socket_getsockname(struct lua_State *L)
2109{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002110 struct hlua_socket *socket;
2111 struct connection *conn;
2112
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002113 MAY_LJMP(check_args(L, 1, "getsockname"));
2114
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002115 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002116
2117 /* Check if the tcp object is avalaible. */
2118 if (!socket->s) {
2119 lua_pushnil(L);
2120 return 1;
2121 }
2122
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002123 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002124 if (!conn) {
2125 lua_pushnil(L);
2126 return 1;
2127 }
2128
Willy Tarreaua71f6422016-11-16 17:00:14 +01002129 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002130 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Willy Tarreaua71f6422016-11-16 17:00:14 +01002131 lua_pushnil(L);
2132 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002133 }
2134
2135 return hlua_socket_info(L, &conn->addr.from);
2136}
2137
2138/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002139static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002140 .obj_type = OBJ_TYPE_APPLET,
2141 .name = "<LUA_TCP>",
2142 .fct = hlua_socket_handler,
2143 .release = hlua_socket_release,
2144};
2145
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002146__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002147{
2148 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2149 struct hlua *hlua = hlua_gethlua(L);
2150 struct appctx *appctx;
2151
2152 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002153 if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002154 lua_pushnil(L);
2155 lua_pushstring(L, "Can't connect");
2156 return 2;
2157 }
2158
2159 appctx = objt_appctx(socket->s->si[0].end);
2160
2161 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002162 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002163 lua_pushinteger(L, 1);
2164 return 1;
2165 }
2166
Thierry FOURNIER847ca662016-12-16 13:07:22 +01002167 if (!hlua_com_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002168 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002169 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002170 return 0;
2171}
2172
2173/* This function fail or initite the connection. */
2174__LJMP static int hlua_socket_connect(struct lua_State *L)
2175{
2176 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002177 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002178 const char *ip;
2179 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002180 struct hlua *hlua;
2181 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002182 int low, high;
2183 struct sockaddr_storage *addr;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002184
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002185 if (lua_gettop(L) < 2)
2186 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002187
2188 /* Get args. */
2189 socket = MAY_LJMP(hlua_checksocket(L, 1));
2190 ip = MAY_LJMP(luaL_checkstring(L, 2));
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002191 if (lua_gettop(L) >= 3)
2192 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002193
Willy Tarreau973a5422015-08-05 21:47:23 +02002194 conn = si_alloc_conn(&socket->s->si[1]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002195 if (!conn)
2196 WILL_LJMP(luaL_error(L, "connect: internal error"));
2197
Willy Tarreau3adac082015-09-26 17:51:09 +02002198 /* needed for the connection not to be closed */
2199 conn->target = socket->s->target;
2200
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002201 /* Parse ip address. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002202 addr = str2sa_range(ip, &low, &high, NULL, NULL, NULL, 0);
2203 if (!addr)
2204 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
2205 if (low != high)
2206 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
2207 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002208
2209 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002210 if (low == 0) {
2211 if (conn->addr.to.ss_family == AF_INET) {
2212 if (port == -1)
2213 WILL_LJMP(luaL_error(L, "connect: port missing"));
2214 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2215 } else if (conn->addr.to.ss_family == AF_INET6) {
2216 if (port == -1)
2217 WILL_LJMP(luaL_error(L, "connect: port missing"));
2218 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2219 }
2220 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002221
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002222 hlua = hlua_gethlua(L);
2223 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002224
2225 /* inform the stream that we want to be notified whenever the
2226 * connection completes.
2227 */
2228 si_applet_cant_get(&socket->s->si[0]);
2229 si_applet_cant_put(&socket->s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002230 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002231
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002232 hlua->flags |= HLUA_MUST_GC;
2233
Thierry FOURNIER847ca662016-12-16 13:07:22 +01002234 if (!hlua_com_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task))
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002235 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002236 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002237
2238 return 0;
2239}
2240
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002241#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002242__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2243{
2244 struct hlua_socket *socket;
2245
2246 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2247 socket = MAY_LJMP(hlua_checksocket(L, 1));
2248 socket->s->target = &socket_ssl.obj_type;
2249 return MAY_LJMP(hlua_socket_connect(L));
2250}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002251#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002252
2253__LJMP static int hlua_socket_setoption(struct lua_State *L)
2254{
2255 return 0;
2256}
2257
2258__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2259{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002260 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002261 int tmout;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002262
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002263 MAY_LJMP(check_args(L, 2, "settimeout"));
2264
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002265 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002266 tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002267
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002268 socket->s->req.rto = tmout;
2269 socket->s->req.wto = tmout;
2270 socket->s->res.rto = tmout;
2271 socket->s->res.wto = tmout;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002272
2273 return 0;
2274}
2275
2276__LJMP static int hlua_socket_new(lua_State *L)
2277{
2278 struct hlua_socket *socket;
2279 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002280 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002281 struct stream *strm;
Willy Tarreaud420a972015-04-06 00:39:18 +02002282 struct task *task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002283
2284 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002285 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002286 hlua_pusherror(L, "socket: full stack");
2287 goto out_fail_conf;
2288 }
2289
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002290 /* Create the object: obj[0] = userdata. */
2291 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002292 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002293 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294 memset(socket, 0, sizeof(*socket));
2295
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002296 /* Check if the various memory pools are intialized. */
Willy Tarreau87b09662015-04-03 00:22:06 +02002297 if (!pool2_stream || !pool2_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002298 hlua_pusherror(L, "socket: uninitialized pools.");
2299 goto out_fail_conf;
2300 }
2301
Willy Tarreau87b09662015-04-03 00:22:06 +02002302 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2304 lua_setmetatable(L, -2);
2305
Willy Tarreaud420a972015-04-06 00:39:18 +02002306 /* Create the applet context */
2307 appctx = appctx_new(&update_applet);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002308 if (!appctx) {
2309 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002310 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002311 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002312
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002313 appctx->ctx.hlua_cosocket.socket = socket;
2314 appctx->ctx.hlua_cosocket.connected = 0;
2315 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2316 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002317
Willy Tarreaud420a972015-04-06 00:39:18 +02002318 /* Now create a session, task and stream for this applet */
2319 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2320 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002321 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002322 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002323 }
2324
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002325 task = task_new();
2326 if (!task) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002327 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002328 goto out_fail_task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002329 }
Willy Tarreaud420a972015-04-06 00:39:18 +02002330 task->nice = 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002331
Willy Tarreau73b65ac2015-04-08 18:26:29 +02002332 strm = stream_new(sess, task, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002333 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002334 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002335 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002336 }
2337
Willy Tarreaud420a972015-04-06 00:39:18 +02002338 /* Configure an empty Lua for the stream. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002339 socket->s = strm;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01002340 strm->hlua = NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002341
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002342 /* Configure "right" stream interface. this "si" is used to connect
2343 * and retrieve data from the server. The connection is initialized
2344 * with the "struct server".
2345 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002346 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002347
2348 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002349 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2350 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002351
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002352 /* Update statistics counters. */
2353 socket_proxy.feconn++; /* beconn will be increased later */
2354 jobs++;
2355 totalconn++;
2356
2357 /* Return yield waiting for connection. */
2358 return 1;
2359
Willy Tarreaud420a972015-04-06 00:39:18 +02002360 out_fail_stream:
2361 task_free(task);
2362 out_fail_task:
Willy Tarreau11c36242015-04-04 15:54:03 +02002363 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002364 out_fail_sess:
2365 appctx_free(appctx);
2366 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002367 WILL_LJMP(lua_error(L));
2368 return 0;
2369}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002370
2371/*
2372 *
2373 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002374 * Class Channel
2375 *
2376 *
2377 */
2378
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002379/* The state between the channel data and the HTTP parser state can be
2380 * unconsistent, so reset the parser and call it again. Warning, this
2381 * action not revalidate the request and not send a 400 if the modified
2382 * resuest is not valid.
2383 *
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002384 * This function never fails. The direction is set using dir, which equals
2385 * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002386 */
2387static void hlua_resynchonize_proto(struct stream *stream, int dir)
2388{
2389 /* Protocol HTTP. */
2390 if (stream->be->mode == PR_MODE_HTTP) {
2391
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002392 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002393 http_txn_reset_req(stream->txn);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002394 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002395 http_txn_reset_res(stream->txn);
2396
2397 if (stream->txn->hdr_idx.v)
2398 hdr_idx_init(&stream->txn->hdr_idx);
2399
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002400 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002401 http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002402 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002403 http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx);
2404 }
2405}
2406
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002407/* This function is called before the Lua execution. It stores
2408 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002409 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002410static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002411{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002412 c->mode = stream->be->mode;
2413 switch (c->mode) {
2414 case PR_MODE_HTTP:
2415 c->data.http.dir = opt & SMP_OPT_DIR;
2416 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2417 c->data.http.state = stream->txn->req.msg_state;
2418 else
2419 c->data.http.state = stream->txn->rsp.msg_state;
2420 break;
2421 default:
2422 break;
2423 }
2424}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002425
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002426/* This function is called after the Lua execution. it
2427 * returns true if the parser state is consistent, otherwise,
2428 * it return false.
2429 *
2430 * In HTTP mode, the parser state must be in the same state
2431 * or greater when we exit the function. Even if we do a
2432 * control yield. This prevent to break the HTTP message
2433 * from the Lua code.
2434 */
2435static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2436{
2437 if (c->mode != stream->be->mode)
2438 return 0;
2439
2440 switch (c->mode) {
2441 case PR_MODE_HTTP:
2442 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002443 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002444 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2445 return stream->txn->req.msg_state >= c->data.http.state;
2446 else
2447 return stream->txn->rsp.msg_state >= c->data.http.state;
2448 default:
2449 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002450 }
2451 return 1;
2452}
2453
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002454/* Returns the struct hlua_channel join to the class channel in the
2455 * stack entry "ud" or throws an argument error.
2456 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002457__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002458{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002459 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002460}
2461
Willy Tarreau47860ed2015-03-10 14:07:50 +01002462/* Pushes the channel onto the top of the stack. If the stask does not have a
2463 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002464 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002465static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002466{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002467 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002468 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002469 return 0;
2470
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002471 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002472 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002473 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002474
2475 /* Pop a class sesison metatable and affect it to the userdata. */
2476 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2477 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002478 return 1;
2479}
2480
2481/* Duplicate all the data present in the input channel and put it
2482 * in a string LUA variables. Returns -1 and push a nil value in
2483 * the stack if the channel is closed and all the data are consumed,
2484 * returns 0 if no data are available, otherwise it returns the length
2485 * of the builded string.
2486 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002487static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002488{
2489 char *blk1;
2490 char *blk2;
2491 int len1;
2492 int len2;
2493 int ret;
2494 luaL_Buffer b;
2495
Willy Tarreau47860ed2015-03-10 14:07:50 +01002496 ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002497 if (unlikely(ret == 0))
2498 return 0;
2499
2500 if (unlikely(ret < 0)) {
2501 lua_pushnil(L);
2502 return -1;
2503 }
2504
2505 luaL_buffinit(L, &b);
2506 luaL_addlstring(&b, blk1, len1);
2507 if (unlikely(ret == 2))
2508 luaL_addlstring(&b, blk2, len2);
2509 luaL_pushresult(&b);
2510
2511 if (unlikely(ret == 2))
2512 return len1 + len2;
2513 return len1;
2514}
2515
2516/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2517 * a yield. This function keep the data in the buffer.
2518 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002519__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002520{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002521 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002522
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002523 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2524
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002525 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002526 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002527 return 1;
2528}
2529
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002530/* Check arguments for the function "hlua_channel_dup_yield". */
2531__LJMP static int hlua_channel_dup(lua_State *L)
2532{
2533 MAY_LJMP(check_args(L, 1, "dup"));
2534 MAY_LJMP(hlua_checkchannel(L, 1));
2535 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2536}
2537
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002538/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2539 * a yield. This function consumes the data in the buffer. It returns
2540 * a string containing the data or a nil pointer if no data are available
2541 * and the channel is closed.
2542 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002543__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002544{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002545 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002546 int ret;
2547
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002548 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002549
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002550 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002551 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002552 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002553
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002554 if (unlikely(ret == -1))
2555 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002556
Willy Tarreau47860ed2015-03-10 14:07:50 +01002557 chn->buf->i -= ret;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002558 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002559 return 1;
2560}
2561
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002562/* Check arguments for the fucntion "hlua_channel_get_yield". */
2563__LJMP static int hlua_channel_get(lua_State *L)
2564{
2565 MAY_LJMP(check_args(L, 1, "get"));
2566 MAY_LJMP(hlua_checkchannel(L, 1));
2567 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2568}
2569
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002570/* This functions consumes and returns one line. If the channel is closed,
2571 * and the last data does not contains a final '\n', the data are returned
2572 * without the final '\n'. When no more data are avalaible, it returns nil
2573 * value.
2574 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002575__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002576{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002577 char *blk1;
2578 char *blk2;
2579 int len1;
2580 int len2;
2581 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002582 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002583 int ret;
2584 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002585
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002586 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2587
Willy Tarreau47860ed2015-03-10 14:07:50 +01002588 ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002589 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002590 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002591
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002592 if (ret == -1) {
2593 lua_pushnil(L);
2594 return 1;
2595 }
2596
2597 luaL_buffinit(L, &b);
2598 luaL_addlstring(&b, blk1, len1);
2599 len = len1;
2600 if (unlikely(ret == 2)) {
2601 luaL_addlstring(&b, blk2, len2);
2602 len += len2;
2603 }
2604 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002605 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002606 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002607 return 1;
2608}
2609
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002610/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2611__LJMP static int hlua_channel_getline(lua_State *L)
2612{
2613 MAY_LJMP(check_args(L, 1, "getline"));
2614 MAY_LJMP(hlua_checkchannel(L, 1));
2615 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2616}
2617
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002618/* This function takes a string as input, and append it at the
2619 * input side of channel. If the data is too big, but a space
2620 * is probably available after sending some data, the function
2621 * yield. If the data is bigger than the buffer, or if the
2622 * channel is closed, it returns -1. otherwise, it returns the
2623 * amount of data writed.
2624 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002625__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002626{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002627 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002628 size_t len;
2629 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2630 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2631 int ret;
2632 int max;
2633
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002634 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2635 * the request buffer if its not required.
2636 */
2637 if (chn->buf->size == 0) {
2638 si_applet_cant_put(chn_prod(chn));
2639 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
2640 }
2641
Willy Tarreau47860ed2015-03-10 14:07:50 +01002642 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002643 if (max > len - l)
2644 max = len - l;
2645
Willy Tarreau47860ed2015-03-10 14:07:50 +01002646 ret = bi_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002647 if (ret == -2 || ret == -3) {
2648 lua_pushinteger(L, -1);
2649 return 1;
2650 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002651 if (ret == -1) {
2652 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002653 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002654 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002655 l += ret;
2656 lua_pop(L, 1);
2657 lua_pushinteger(L, l);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002658 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002659
Willy Tarreau47860ed2015-03-10 14:07:50 +01002660 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2661 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002662 /* There are no space avalaible, and the output buffer is empty.
2663 * in this case, we cannot add more data, so we cannot yield,
2664 * we return the amount of copyied data.
2665 */
2666 return 1;
2667 }
2668 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002669 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002670 return 1;
2671}
2672
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002673/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002674 * of the writed string, or -1 if the channel is closed or if the
2675 * buffer size is too little for the data.
2676 */
2677__LJMP static int hlua_channel_append(lua_State *L)
2678{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002679 size_t len;
2680
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002681 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002682 MAY_LJMP(hlua_checkchannel(L, 1));
2683 MAY_LJMP(luaL_checklstring(L, 2, &len));
2684 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002685 lua_pushinteger(L, 0);
2686
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002687 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002688}
2689
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002690/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002691 * his process by cleaning the buffer. The result is a replacement
2692 * of the current data. It returns the length of the writed string,
2693 * or -1 if the channel is closed or if the buffer size is too
2694 * little for the data.
2695 */
2696__LJMP static int hlua_channel_set(lua_State *L)
2697{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002698 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002699
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002700 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002701 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002702 lua_pushinteger(L, 0);
2703
Willy Tarreau47860ed2015-03-10 14:07:50 +01002704 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002705
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002706 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002707}
2708
2709/* Append data in the output side of the buffer. This data is immediatly
2710 * sent. The fcuntion returns the ammount of data writed. If the buffer
2711 * cannot contains the data, the function yield. The function returns -1
2712 * if the channel is closed.
2713 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002714__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002715{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002716 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002717 size_t len;
2718 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2719 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2720 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002721 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002722
Willy Tarreau47860ed2015-03-10 14:07:50 +01002723 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002724 lua_pushinteger(L, -1);
2725 return 1;
2726 }
2727
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002728 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2729 * the request buffer if its not required.
2730 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002731 if (chn->buf->size == 0) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002732 si_applet_cant_put(chn_prod(chn));
2733 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002734 }
2735
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002736 /* the writed data will be immediatly sent, so we can check
2737 * the avalaible space without taking in account the reserve.
2738 * The reserve is guaranted for the processing of incoming
2739 * data, because the buffer will be flushed.
2740 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002741 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002742
2743 /* If there are no space avalaible, and the output buffer is empty.
2744 * in this case, we cannot add more data, so we cannot yield,
2745 * we return the amount of copyied data.
2746 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002747 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002748 return 1;
2749
2750 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002751 if (max > len - l)
2752 max = len - l;
2753
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002754 /* The buffer avalaible size may be not contiguous. This test
2755 * detects a non contiguous buffer and realign it.
2756 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002757 if (bi_space_for_replace(chn->buf) < max)
2758 buffer_slow_realign(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002759
2760 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002761 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002762
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002763 /* buffer replace considers that the input part is filled.
2764 * so, I must forward these new data in the output part.
2765 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002766 b_adv(chn->buf, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002767
2768 l += max;
2769 lua_pop(L, 1);
2770 lua_pushinteger(L, l);
2771
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002772 /* If there are no space avalaible, and the output buffer is empty.
2773 * in this case, we cannot add more data, so we cannot yield,
2774 * we return the amount of copyied data.
2775 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002776 max = chn->buf->size - buffer_len(chn->buf);
2777 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002778 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002779
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002780 if (l < len) {
2781 /* If we are waiting for space in the response buffer, we
2782 * must set the flag WAKERESWR. This flag required the task
2783 * wake up if any activity is detected on the response buffer.
2784 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002785 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002786 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002787 else
2788 HLUA_SET_WAKEREQWR(hlua);
2789 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002790 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002791
2792 return 1;
2793}
2794
2795/* Just a wraper of "_hlua_channel_send". This wrapper permits
2796 * yield the LUA process, and resume it without checking the
2797 * input arguments.
2798 */
2799__LJMP static int hlua_channel_send(lua_State *L)
2800{
2801 MAY_LJMP(check_args(L, 2, "send"));
2802 lua_pushinteger(L, 0);
2803
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002804 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002805}
2806
2807/* This function forward and amount of butes. The data pass from
2808 * the input side of the buffer to the output side, and can be
2809 * forwarded. This function never fails.
2810 *
2811 * The Lua function takes an amount of bytes to be forwarded in
2812 * imput. It returns the number of bytes forwarded.
2813 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002814__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002815{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002816 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002817 int len;
2818 int l;
2819 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002820 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002821
2822 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2823 len = MAY_LJMP(luaL_checkinteger(L, 2));
2824 l = MAY_LJMP(luaL_checkinteger(L, -1));
2825
2826 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002827 if (max > chn->buf->i)
2828 max = chn->buf->i;
2829 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002830 l += max;
2831
2832 lua_pop(L, 1);
2833 lua_pushinteger(L, l);
2834
2835 /* Check if it miss bytes to forward. */
2836 if (l < len) {
2837 /* The the input channel or the output channel are closed, we
2838 * must return the amount of data forwarded.
2839 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002840 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002841 return 1;
2842
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002843 /* If we are waiting for space data in the response buffer, we
2844 * must set the flag WAKERESWR. This flag required the task
2845 * wake up if any activity is detected on the response buffer.
2846 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002847 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002848 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002849 else
2850 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002851
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002852 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002853 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002854 }
2855
2856 return 1;
2857}
2858
2859/* Just check the input and prepare the stack for the previous
2860 * function "hlua_channel_forward_yield"
2861 */
2862__LJMP static int hlua_channel_forward(lua_State *L)
2863{
2864 MAY_LJMP(check_args(L, 2, "forward"));
2865 MAY_LJMP(hlua_checkchannel(L, 1));
2866 MAY_LJMP(luaL_checkinteger(L, 2));
2867
2868 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002869 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002870}
2871
2872/* Just returns the number of bytes available in the input
2873 * side of the buffer. This function never fails.
2874 */
2875__LJMP static int hlua_channel_get_in_len(lua_State *L)
2876{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002877 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002878
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002879 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002880 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002881 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002882 return 1;
2883}
2884
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002885/* Returns true if the channel is full. */
2886__LJMP static int hlua_channel_is_full(lua_State *L)
2887{
2888 struct channel *chn;
2889 int rem;
2890
2891 MAY_LJMP(check_args(L, 1, "is_full"));
2892 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2893
2894 rem = chn->buf->size;
2895 rem -= chn->buf->o; /* Output size */
2896 rem -= chn->buf->i; /* Input size */
2897 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
2898
2899 lua_pushboolean(L, rem <= 0);
2900 return 1;
2901}
2902
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002903/* Just returns the number of bytes available in the output
2904 * side of the buffer. This function never fails.
2905 */
2906__LJMP static int hlua_channel_get_out_len(lua_State *L)
2907{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002908 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002909
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002910 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002911 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002912 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002913 return 1;
2914}
2915
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002916/*
2917 *
2918 *
2919 * Class Fetches
2920 *
2921 *
2922 */
2923
2924/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002925 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002926 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002927__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002928{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002929 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002930}
2931
2932/* This function creates and push in the stack a fetch object according
2933 * with a current TXN.
2934 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01002935static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002936{
Willy Tarreau7073c472015-04-06 11:15:40 +02002937 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002938
2939 /* Check stack size. */
2940 if (!lua_checkstack(L, 3))
2941 return 0;
2942
2943 /* Create the object: obj[0] = userdata.
2944 * Note that the base of the Fetches object is the
2945 * transaction object.
2946 */
2947 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002948 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002949 lua_rawseti(L, -2, 0);
2950
Willy Tarreau7073c472015-04-06 11:15:40 +02002951 hsmp->s = txn->s;
2952 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01002953 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01002954 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002955
2956 /* Pop a class sesison metatable and affect it to the userdata. */
2957 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
2958 lua_setmetatable(L, -2);
2959
2960 return 1;
2961}
2962
2963/* This function is an LUA binding. It is called with each sample-fetch.
2964 * It uses closure argument to store the associated sample-fetch. It
2965 * returns only one argument or throws an error. An error is thrown
2966 * only if an error is encountered during the argument parsing. If
2967 * the "sample-fetch" function fails, nil is returned.
2968 */
2969__LJMP static int hlua_run_sample_fetch(lua_State *L)
2970{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002971 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01002972 struct sample_fetch *f;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002973 struct arg args[ARGM_NBARGS + 1];
2974 int i;
2975 struct sample smp;
2976
2977 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002978 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002979
2980 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002981 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002982
Thierry FOURNIERca988662015-12-20 18:43:03 +01002983 /* Check execution authorization. */
2984 if (f->use & SMP_USE_HTTP_ANY &&
2985 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
2986 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
2987 "is not available in Lua services", f->kw);
2988 WILL_LJMP(lua_error(L));
2989 }
2990
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002991 /* Get extra arguments. */
2992 for (i = 0; i < lua_gettop(L) - 1; i++) {
2993 if (i >= ARGM_NBARGS)
2994 break;
2995 hlua_lua2arg(L, i + 2, &args[i]);
2996 }
2997 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01002998 args[i].data.str.str = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002999
3000 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003001 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003002
3003 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003004 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003005 lua_pushfstring(L, "error in arguments");
3006 WILL_LJMP(lua_error(L));
3007 }
3008
3009 /* Initialise the sample. */
3010 memset(&smp, 0, sizeof(smp));
3011
3012 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003013 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003014 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003015 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003016 lua_pushstring(L, "");
3017 else
3018 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003019 return 1;
3020 }
3021
3022 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003023 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003024 hlua_smp2lua_str(L, &smp);
3025 else
3026 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003027 return 1;
3028}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003029
3030/*
3031 *
3032 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003033 * Class Converters
3034 *
3035 *
3036 */
3037
3038/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003039 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003040 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003041__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003042{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003043 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003044}
3045
3046/* This function creates and push in the stack a Converters object
3047 * according with a current TXN.
3048 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003049static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003050{
Willy Tarreau7073c472015-04-06 11:15:40 +02003051 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003052
3053 /* Check stack size. */
3054 if (!lua_checkstack(L, 3))
3055 return 0;
3056
3057 /* Create the object: obj[0] = userdata.
3058 * Note that the base of the Converters object is the
3059 * same than the TXN object.
3060 */
3061 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003062 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003063 lua_rawseti(L, -2, 0);
3064
Willy Tarreau7073c472015-04-06 11:15:40 +02003065 hsmp->s = txn->s;
3066 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003067 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003068 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003069
Willy Tarreau87b09662015-04-03 00:22:06 +02003070 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003071 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3072 lua_setmetatable(L, -2);
3073
3074 return 1;
3075}
3076
3077/* This function is an LUA binding. It is called with each converter.
3078 * It uses closure argument to store the associated converter. It
3079 * returns only one argument or throws an error. An error is thrown
3080 * only if an error is encountered during the argument parsing. If
3081 * the converter function function fails, nil is returned.
3082 */
3083__LJMP static int hlua_run_sample_conv(lua_State *L)
3084{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003085 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003086 struct sample_conv *conv;
3087 struct arg args[ARGM_NBARGS + 1];
3088 int i;
3089 struct sample smp;
3090
3091 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003092 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003093
3094 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003095 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003096
3097 /* Get extra arguments. */
3098 for (i = 0; i < lua_gettop(L) - 2; i++) {
3099 if (i >= ARGM_NBARGS)
3100 break;
3101 hlua_lua2arg(L, i + 3, &args[i]);
3102 }
3103 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003104 args[i].data.str.str = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003105
3106 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003107 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003108
3109 /* Run the special args checker. */
3110 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3111 hlua_pusherror(L, "error in arguments");
3112 WILL_LJMP(lua_error(L));
3113 }
3114
3115 /* Initialise the sample. */
3116 if (!hlua_lua2smp(L, 2, &smp)) {
3117 hlua_pusherror(L, "error in the input argument");
3118 WILL_LJMP(lua_error(L));
3119 }
3120
Willy Tarreau1777ea62016-03-10 16:15:46 +01003121 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3122
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003123 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003124 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003125 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003126 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003127 WILL_LJMP(lua_error(L));
3128 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003129 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3130 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003131 hlua_pusherror(L, "error during the input argument casting");
3132 WILL_LJMP(lua_error(L));
3133 }
3134
3135 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003136 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003137 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003138 lua_pushstring(L, "");
3139 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003140 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003141 return 1;
3142 }
3143
3144 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003145 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003146 hlua_smp2lua_str(L, &smp);
3147 else
3148 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003149 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003150}
3151
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003152/*
3153 *
3154 *
3155 * Class AppletTCP
3156 *
3157 *
3158 */
3159
3160/* Returns a struct hlua_txn if the stack entry "ud" is
3161 * a class stream, otherwise it throws an error.
3162 */
3163__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3164{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003165 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003166}
3167
3168/* This function creates and push in the stack an Applet object
3169 * according with a current TXN.
3170 */
3171static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3172{
3173 struct hlua_appctx *appctx;
3174 struct stream_interface *si = ctx->owner;
3175 struct stream *s = si_strm(si);
3176 struct proxy *p = s->be;
3177
3178 /* Check stack size. */
3179 if (!lua_checkstack(L, 3))
3180 return 0;
3181
3182 /* Create the object: obj[0] = userdata.
3183 * Note that the base of the Converters object is the
3184 * same than the TXN object.
3185 */
3186 lua_newtable(L);
3187 appctx = lua_newuserdata(L, sizeof(*appctx));
3188 lua_rawseti(L, -2, 0);
3189 appctx->appctx = ctx;
3190 appctx->htxn.s = s;
3191 appctx->htxn.p = p;
3192
3193 /* Create the "f" field that contains a list of fetches. */
3194 lua_pushstring(L, "f");
3195 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3196 return 0;
3197 lua_settable(L, -3);
3198
3199 /* Create the "sf" field that contains a list of stringsafe fetches. */
3200 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003201 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003202 return 0;
3203 lua_settable(L, -3);
3204
3205 /* Create the "c" field that contains a list of converters. */
3206 lua_pushstring(L, "c");
3207 if (!hlua_converters_new(L, &appctx->htxn, 0))
3208 return 0;
3209 lua_settable(L, -3);
3210
3211 /* Create the "sc" field that contains a list of stringsafe converters. */
3212 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003213 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003214 return 0;
3215 lua_settable(L, -3);
3216
3217 /* Pop a class stream metatable and affect it to the table. */
3218 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3219 lua_setmetatable(L, -2);
3220
3221 return 1;
3222}
3223
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003224__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3225{
3226 struct hlua_appctx *appctx;
3227 struct stream *s;
3228 const char *name;
3229 size_t len;
3230 struct sample smp;
3231
3232 MAY_LJMP(check_args(L, 3, "set_var"));
3233
3234 /* It is useles to retrieve the stream, but this function
3235 * runs only in a stream context.
3236 */
3237 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3238 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3239 s = appctx->htxn.s;
3240
3241 /* Converts the third argument in a sample. */
3242 hlua_lua2smp(L, 3, &smp);
3243
3244 /* Store the sample in a variable. */
3245 smp_set_owner(&smp, s->be, s->sess, s, 0);
3246 vars_set_by_name(name, len, &smp);
3247 return 0;
3248}
3249
3250__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3251{
3252 struct hlua_appctx *appctx;
3253 struct stream *s;
3254 const char *name;
3255 size_t len;
3256 struct sample smp;
3257
3258 MAY_LJMP(check_args(L, 2, "unset_var"));
3259
3260 /* It is useles to retrieve the stream, but this function
3261 * runs only in a stream context.
3262 */
3263 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3264 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3265 s = appctx->htxn.s;
3266
3267 /* Unset the variable. */
3268 smp_set_owner(&smp, s->be, s->sess, s, 0);
3269 vars_unset_by_name(name, len, &smp);
3270 return 0;
3271}
3272
3273__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3274{
3275 struct hlua_appctx *appctx;
3276 struct stream *s;
3277 const char *name;
3278 size_t len;
3279 struct sample smp;
3280
3281 MAY_LJMP(check_args(L, 2, "get_var"));
3282
3283 /* It is useles to retrieve the stream, but this function
3284 * runs only in a stream context.
3285 */
3286 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3287 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3288 s = appctx->htxn.s;
3289
3290 smp_set_owner(&smp, s->be, s->sess, s, 0);
3291 if (!vars_get_by_name(name, len, &smp)) {
3292 lua_pushnil(L);
3293 return 1;
3294 }
3295
3296 return hlua_smp2lua(L, &smp);
3297}
3298
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003299__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3300{
3301 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3302 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003303 struct hlua *hlua;
3304
3305 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003306 if (!s->hlua)
3307 return 0;
3308 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003309
3310 MAY_LJMP(check_args(L, 2, "set_priv"));
3311
3312 /* Remove previous value. */
3313 if (hlua->Mref != -1)
3314 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
3315
3316 /* Get and store new value. */
3317 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3318 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3319
3320 return 0;
3321}
3322
3323__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3324{
3325 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3326 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003327 struct hlua *hlua;
3328
3329 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003330 if (!s->hlua) {
3331 lua_pushnil(L);
3332 return 1;
3333 }
3334 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003335
3336 /* Push configuration index in the stack. */
3337 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3338
3339 return 1;
3340}
3341
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003342/* If expected data not yet available, it returns a yield. This function
3343 * consumes the data in the buffer. It returns a string containing the
3344 * data. This string can be empty.
3345 */
3346__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3347{
3348 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3349 struct stream_interface *si = appctx->appctx->owner;
3350 int ret;
3351 char *blk1;
3352 int len1;
3353 char *blk2;
3354 int len2;
3355
3356 /* Read the maximum amount of data avalaible. */
3357 ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
3358
3359 /* Data not yet avalaible. return yield. */
3360 if (ret == 0) {
3361 si_applet_cant_get(si);
3362 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
3363 }
3364
3365 /* End of data: commit the total strings and return. */
3366 if (ret < 0) {
3367 luaL_pushresult(&appctx->b);
3368 return 1;
3369 }
3370
3371 /* Ensure that the block 2 length is usable. */
3372 if (ret == 1)
3373 len2 = 0;
3374
3375 /* dont check the max length read and dont check. */
3376 luaL_addlstring(&appctx->b, blk1, len1);
3377 luaL_addlstring(&appctx->b, blk2, len2);
3378
3379 /* Consume input channel output buffer data. */
3380 bo_skip(si_oc(si), len1 + len2);
3381 luaL_pushresult(&appctx->b);
3382 return 1;
3383}
3384
3385/* Check arguments for the fucntion "hlua_channel_get_yield". */
3386__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3387{
3388 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3389
3390 /* Initialise the string catenation. */
3391 luaL_buffinit(L, &appctx->b);
3392
3393 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3394}
3395
3396/* If expected data not yet available, it returns a yield. This function
3397 * consumes the data in the buffer. It returns a string containing the
3398 * data. This string can be empty.
3399 */
3400__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3401{
3402 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3403 struct stream_interface *si = appctx->appctx->owner;
3404 int len = MAY_LJMP(luaL_checkinteger(L, 2));
3405 int ret;
3406 char *blk1;
3407 int len1;
3408 char *blk2;
3409 int len2;
3410
3411 /* Read the maximum amount of data avalaible. */
3412 ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
3413
3414 /* Data not yet avalaible. return yield. */
3415 if (ret == 0) {
3416 si_applet_cant_get(si);
3417 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3418 }
3419
3420 /* End of data: commit the total strings and return. */
3421 if (ret < 0) {
3422 luaL_pushresult(&appctx->b);
3423 return 1;
3424 }
3425
3426 /* Ensure that the block 2 length is usable. */
3427 if (ret == 1)
3428 len2 = 0;
3429
3430 if (len == -1) {
3431
3432 /* If len == -1, catenate all the data avalaile and
3433 * yield because we want to get all the data until
3434 * the end of data stream.
3435 */
3436 luaL_addlstring(&appctx->b, blk1, len1);
3437 luaL_addlstring(&appctx->b, blk2, len2);
3438 bo_skip(si_oc(si), len1 + len2);
3439 si_applet_cant_get(si);
3440 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3441
3442 } else {
3443
3444 /* Copy the fisrt block caping to the length required. */
3445 if (len1 > len)
3446 len1 = len;
3447 luaL_addlstring(&appctx->b, blk1, len1);
3448 len -= len1;
3449
3450 /* Copy the second block. */
3451 if (len2 > len)
3452 len2 = len;
3453 luaL_addlstring(&appctx->b, blk2, len2);
3454 len -= len2;
3455
3456 /* Consume input channel output buffer data. */
3457 bo_skip(si_oc(si), len1 + len2);
3458
3459 /* If we are no other data avalaible, yield waiting for new data. */
3460 if (len > 0) {
3461 lua_pushinteger(L, len);
3462 lua_replace(L, 2);
3463 si_applet_cant_get(si);
3464 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3465 }
3466
3467 /* return the result. */
3468 luaL_pushresult(&appctx->b);
3469 return 1;
3470 }
3471
3472 /* we never executes this */
3473 hlua_pusherror(L, "Lua: internal error");
3474 WILL_LJMP(lua_error(L));
3475 return 0;
3476}
3477
3478/* Check arguments for the fucntion "hlua_channel_get_yield". */
3479__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3480{
3481 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3482 int len = -1;
3483
3484 if (lua_gettop(L) > 2)
3485 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3486 if (lua_gettop(L) >= 2) {
3487 len = MAY_LJMP(luaL_checkinteger(L, 2));
3488 lua_pop(L, 1);
3489 }
3490
3491 /* Confirm or set the required length */
3492 lua_pushinteger(L, len);
3493
3494 /* Initialise the string catenation. */
3495 luaL_buffinit(L, &appctx->b);
3496
3497 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3498}
3499
3500/* Append data in the output side of the buffer. This data is immediatly
3501 * sent. The fcuntion returns the ammount of data writed. If the buffer
3502 * cannot contains the data, the function yield. The function returns -1
3503 * if the channel is closed.
3504 */
3505__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3506{
3507 size_t len;
3508 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3509 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3510 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3511 struct stream_interface *si = appctx->appctx->owner;
3512 struct channel *chn = si_ic(si);
3513 int max;
3514
3515 /* Get the max amount of data which can write as input in the channel. */
3516 max = channel_recv_max(chn);
3517 if (max > (len - l))
3518 max = len - l;
3519
3520 /* Copy data. */
3521 bi_putblk(chn, str + l, max);
3522
3523 /* update counters. */
3524 l += max;
3525 lua_pop(L, 1);
3526 lua_pushinteger(L, l);
3527
3528 /* If some data is not send, declares the situation to the
3529 * applet, and returns a yield.
3530 */
3531 if (l < len) {
3532 si_applet_cant_put(si);
3533 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
3534 }
3535
3536 return 1;
3537}
3538
3539/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3540 * yield the LUA process, and resume it without checking the
3541 * input arguments.
3542 */
3543__LJMP static int hlua_applet_tcp_send(lua_State *L)
3544{
3545 MAY_LJMP(check_args(L, 2, "send"));
3546 lua_pushinteger(L, 0);
3547
3548 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3549}
3550
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003551/*
3552 *
3553 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003554 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003555 *
3556 *
3557 */
3558
3559/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003560 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003561 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003562__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003563{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003564 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003565}
3566
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003567/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003568 * according with a current TXN.
3569 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003570static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003571{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003572 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003573 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003574 struct stream_interface *si = ctx->owner;
3575 struct stream *s = si_strm(si);
3576 struct proxy *px = s->be;
3577 struct http_txn *txn = s->txn;
3578 const char *path;
3579 const char *end;
3580 const char *p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003581
3582 /* Check stack size. */
3583 if (!lua_checkstack(L, 3))
3584 return 0;
3585
3586 /* Create the object: obj[0] = userdata.
3587 * Note that the base of the Converters object is the
3588 * same than the TXN object.
3589 */
3590 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003591 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003592 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003593 appctx->appctx = ctx;
3594 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
3595 appctx->htxn.s = s;
3596 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003597
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003598 /* Create the "f" field that contains a list of fetches. */
3599 lua_pushstring(L, "f");
3600 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3601 return 0;
3602 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003603
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003604 /* Create the "sf" field that contains a list of stringsafe fetches. */
3605 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003606 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003607 return 0;
3608 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003609
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003610 /* Create the "c" field that contains a list of converters. */
3611 lua_pushstring(L, "c");
3612 if (!hlua_converters_new(L, &appctx->htxn, 0))
3613 return 0;
3614 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003615
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003616 /* Create the "sc" field that contains a list of stringsafe converters. */
3617 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003618 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003619 return 0;
3620 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003621
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003622 /* Stores the request method. */
3623 lua_pushstring(L, "method");
3624 lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l);
3625 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003626
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003627 /* Stores the http version. */
3628 lua_pushstring(L, "version");
3629 lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
3630 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003631
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003632 /* creates an array of headers. hlua_http_get_headers() crates and push
3633 * the array on the top of the stack.
3634 */
3635 lua_pushstring(L, "headers");
3636 htxn.s = s;
3637 htxn.p = px;
3638 htxn.dir = SMP_OPT_DIR_REQ;
3639 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3640 return 0;
3641 lua_settable(L, -3);
3642
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003643 /* Get path and qs */
3644 path = http_get_path(txn);
3645 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
3646 p = path;
3647 while (p < end && *p != '?')
3648 p++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003649
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003650 /* Stores the request path. */
3651 lua_pushstring(L, "path");
3652 lua_pushlstring(L, path, p - path);
3653 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003654
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003655 /* Stores the query string. */
3656 lua_pushstring(L, "qs");
3657 if (*p == '?')
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003658 p++;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003659 lua_pushlstring(L, p, end - p);
3660 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003661
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003662 /* Stores the request path. */
3663 lua_pushstring(L, "length");
3664 lua_pushinteger(L, txn->req.body_len);
3665 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003666
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003667 /* Create an array of HTTP request headers. */
3668 lua_pushstring(L, "headers");
3669 MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req));
3670 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003671
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003672 /* Create an empty array of HTTP request headers. */
3673 lua_pushstring(L, "response");
3674 lua_newtable(L);
3675 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003676
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003677 /* Pop a class stream metatable and affect it to the table. */
3678 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3679 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003680
3681 return 1;
3682}
3683
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003684__LJMP static int hlua_applet_http_set_var(lua_State *L)
3685{
3686 struct hlua_appctx *appctx;
3687 struct stream *s;
3688 const char *name;
3689 size_t len;
3690 struct sample smp;
3691
3692 MAY_LJMP(check_args(L, 3, "set_var"));
3693
3694 /* It is useles to retrieve the stream, but this function
3695 * runs only in a stream context.
3696 */
3697 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3698 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3699 s = appctx->htxn.s;
3700
3701 /* Converts the third argument in a sample. */
3702 hlua_lua2smp(L, 3, &smp);
3703
3704 /* Store the sample in a variable. */
3705 smp_set_owner(&smp, s->be, s->sess, s, 0);
3706 vars_set_by_name(name, len, &smp);
3707 return 0;
3708}
3709
3710__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3711{
3712 struct hlua_appctx *appctx;
3713 struct stream *s;
3714 const char *name;
3715 size_t len;
3716 struct sample smp;
3717
3718 MAY_LJMP(check_args(L, 2, "unset_var"));
3719
3720 /* It is useles to retrieve the stream, but this function
3721 * runs only in a stream context.
3722 */
3723 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3724 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3725 s = appctx->htxn.s;
3726
3727 /* Unset the variable. */
3728 smp_set_owner(&smp, s->be, s->sess, s, 0);
3729 vars_unset_by_name(name, len, &smp);
3730 return 0;
3731}
3732
3733__LJMP static int hlua_applet_http_get_var(lua_State *L)
3734{
3735 struct hlua_appctx *appctx;
3736 struct stream *s;
3737 const char *name;
3738 size_t len;
3739 struct sample smp;
3740
3741 MAY_LJMP(check_args(L, 2, "get_var"));
3742
3743 /* It is useles to retrieve the stream, but this function
3744 * runs only in a stream context.
3745 */
3746 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3747 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3748 s = appctx->htxn.s;
3749
3750 smp_set_owner(&smp, s->be, s->sess, s, 0);
3751 if (!vars_get_by_name(name, len, &smp)) {
3752 lua_pushnil(L);
3753 return 1;
3754 }
3755
3756 return hlua_smp2lua(L, &smp);
3757}
3758
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003759__LJMP static int hlua_applet_http_set_priv(lua_State *L)
3760{
3761 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3762 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003763 struct hlua *hlua;
3764
3765 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003766 if (!s->hlua)
3767 return 0;
3768 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003769
3770 MAY_LJMP(check_args(L, 2, "set_priv"));
3771
3772 /* Remove previous value. */
3773 if (hlua->Mref != -1)
3774 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
3775
3776 /* Get and store new value. */
3777 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3778 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3779
3780 return 0;
3781}
3782
3783__LJMP static int hlua_applet_http_get_priv(lua_State *L)
3784{
3785 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3786 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003787 struct hlua *hlua;
3788
3789 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003790 if (!s->hlua) {
3791 lua_pushnil(L);
3792 return 1;
3793 }
3794 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003795
3796 /* Push configuration index in the stack. */
3797 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3798
3799 return 1;
3800}
3801
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003802/* If expected data not yet available, it returns a yield. This function
3803 * consumes the data in the buffer. It returns a string containing the
3804 * data. This string can be empty.
3805 */
3806__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003807{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003808 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3809 struct stream_interface *si = appctx->appctx->owner;
3810 struct channel *chn = si_ic(si);
3811 int ret;
3812 char *blk1;
3813 int len1;
3814 char *blk2;
3815 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003816
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003817 /* Maybe we cant send a 100-continue ? */
3818 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
3819 ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C));
3820 /* if ret == -2 or -3 the channel closed or the message si too
3821 * big for the buffers. We cant send anything. So, we ignoring
3822 * the error, considers that the 100-continue is sent, and try
3823 * to receive.
3824 * If ret is -1, we dont have room in the buffer, so we yield.
3825 */
3826 if (ret == -1) {
3827 si_applet_cant_put(si);
3828 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
3829 }
3830 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
3831 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003832
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003833 /* Check for the end of the data. */
3834 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
3835 luaL_pushresult(&appctx->b);
3836 return 1;
3837 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003838
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003839 /* Read the maximum amount of data avalaible. */
3840 ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003841
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003842 /* Data not yet avalaible. return yield. */
3843 if (ret == 0) {
3844 si_applet_cant_get(si);
3845 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
3846 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003847
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003848 /* End of data: commit the total strings and return. */
3849 if (ret < 0) {
3850 luaL_pushresult(&appctx->b);
3851 return 1;
3852 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003853
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003854 /* Ensure that the block 2 length is usable. */
3855 if (ret == 1)
3856 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003857
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003858 /* Copy the fisrt block caping to the length required. */
3859 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
3860 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
3861 luaL_addlstring(&appctx->b, blk1, len1);
3862 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003863
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003864 /* Copy the second block. */
3865 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
3866 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
3867 luaL_addlstring(&appctx->b, blk2, len2);
3868 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003869
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003870 /* Consume input channel output buffer data. */
3871 bo_skip(si_oc(si), len1 + len2);
3872 luaL_pushresult(&appctx->b);
3873 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003874}
3875
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003876/* Check arguments for the fucntion "hlua_channel_get_yield". */
3877__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003878{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003879 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003880
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003881 /* Initialise the string catenation. */
3882 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003883
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003884 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003885}
3886
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003887/* If expected data not yet available, it returns a yield. This function
3888 * consumes the data in the buffer. It returns a string containing the
3889 * data. This string can be empty.
3890 */
3891__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003892{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003893 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3894 struct stream_interface *si = appctx->appctx->owner;
3895 int len = MAY_LJMP(luaL_checkinteger(L, 2));
3896 struct channel *chn = si_ic(si);
3897 int ret;
3898 char *blk1;
3899 int len1;
3900 char *blk2;
3901 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003902
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003903 /* Maybe we cant send a 100-continue ? */
3904 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
3905 ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C));
3906 /* if ret == -2 or -3 the channel closed or the message si too
3907 * big for the buffers. We cant send anything. So, we ignoring
3908 * the error, considers that the 100-continue is sent, and try
3909 * to receive.
3910 * If ret is -1, we dont have room in the buffer, so we yield.
3911 */
3912 if (ret == -1) {
3913 si_applet_cant_put(si);
3914 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
3915 }
3916 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
3917 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003918
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003919 /* Read the maximum amount of data avalaible. */
3920 ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003921
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003922 /* Data not yet avalaible. return yield. */
3923 if (ret == 0) {
3924 si_applet_cant_get(si);
3925 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
3926 }
3927
3928 /* End of data: commit the total strings and return. */
3929 if (ret < 0) {
3930 luaL_pushresult(&appctx->b);
3931 return 1;
3932 }
3933
3934 /* Ensure that the block 2 length is usable. */
3935 if (ret == 1)
3936 len2 = 0;
3937
3938 /* Copy the fisrt block caping to the length required. */
3939 if (len1 > len)
3940 len1 = len;
3941 luaL_addlstring(&appctx->b, blk1, len1);
3942 len -= len1;
3943
3944 /* Copy the second block. */
3945 if (len2 > len)
3946 len2 = len;
3947 luaL_addlstring(&appctx->b, blk2, len2);
3948 len -= len2;
3949
3950 /* Consume input channel output buffer data. */
3951 bo_skip(si_oc(si), len1 + len2);
3952 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
3953 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
3954
3955 /* If we are no other data avalaible, yield waiting for new data. */
3956 if (len > 0) {
3957 lua_pushinteger(L, len);
3958 lua_replace(L, 2);
3959 si_applet_cant_get(si);
3960 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
3961 }
3962
3963 /* return the result. */
3964 luaL_pushresult(&appctx->b);
3965 return 1;
3966}
3967
3968/* Check arguments for the fucntion "hlua_channel_get_yield". */
3969__LJMP static int hlua_applet_http_recv(lua_State *L)
3970{
3971 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3972 int len = -1;
3973
3974 /* Check arguments. */
3975 if (lua_gettop(L) > 2)
3976 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3977 if (lua_gettop(L) >= 2) {
3978 len = MAY_LJMP(luaL_checkinteger(L, 2));
3979 lua_pop(L, 1);
3980 }
3981
3982 /* Check the required length */
3983 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
3984 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
3985 lua_pushinteger(L, len);
3986
3987 /* Initialise the string catenation. */
3988 luaL_buffinit(L, &appctx->b);
3989
3990 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
3991}
3992
3993/* Append data in the output side of the buffer. This data is immediatly
3994 * sent. The fcuntion returns the ammount of data writed. If the buffer
3995 * cannot contains the data, the function yield. The function returns -1
3996 * if the channel is closed.
3997 */
3998__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
3999{
4000 size_t len;
4001 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4002 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4003 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4004 struct stream_interface *si = appctx->appctx->owner;
4005 struct channel *chn = si_ic(si);
4006 int max;
4007
4008 /* Get the max amount of data which can write as input in the channel. */
4009 max = channel_recv_max(chn);
4010 if (max > (len - l))
4011 max = len - l;
4012
4013 /* Copy data. */
4014 bi_putblk(chn, str + l, max);
4015
4016 /* update counters. */
4017 l += max;
4018 lua_pop(L, 1);
4019 lua_pushinteger(L, l);
4020
4021 /* If some data is not send, declares the situation to the
4022 * applet, and returns a yield.
4023 */
4024 if (l < len) {
4025 si_applet_cant_put(si);
4026 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
4027 }
4028
4029 return 1;
4030}
4031
4032/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4033 * yield the LUA process, and resume it without checking the
4034 * input arguments.
4035 */
4036__LJMP static int hlua_applet_http_send(lua_State *L)
4037{
4038 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4039 size_t len;
4040 char hex[10];
4041
4042 MAY_LJMP(luaL_checklstring(L, 2, &len));
4043
4044 /* If transfer encoding chunked is selected, we surround the data
4045 * by chunk data.
4046 */
4047 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4048 snprintf(hex, 9, "%x", (unsigned int)len);
4049 lua_pushfstring(L, "%s\r\n", hex);
4050 lua_insert(L, 2); /* swap the last 2 entries. */
4051 lua_pushstring(L, "\r\n");
4052 lua_concat(L, 3);
4053 }
4054
4055 /* This interger is used for followinf the amount of data sent. */
4056 lua_pushinteger(L, 0);
4057
4058 /* We want to send some data. Headers must be sent. */
4059 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4060 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4061 WILL_LJMP(lua_error(L));
4062 }
4063
4064 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4065}
4066
4067__LJMP static int hlua_applet_http_addheader(lua_State *L)
4068{
4069 const char *name;
4070 int ret;
4071
4072 MAY_LJMP(hlua_checkapplet_http(L, 1));
4073 name = MAY_LJMP(luaL_checkstring(L, 2));
4074 MAY_LJMP(luaL_checkstring(L, 3));
4075
4076 /* Push in the stack the "response" entry. */
4077 ret = lua_getfield(L, 1, "response");
4078 if (ret != LUA_TTABLE) {
4079 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4080 "is expected as an array. %s found", lua_typename(L, ret));
4081 WILL_LJMP(lua_error(L));
4082 }
4083
4084 /* check if the header is already registered if it is not
4085 * the case, register it.
4086 */
4087 ret = lua_getfield(L, -1, name);
4088 if (ret == LUA_TNIL) {
4089
4090 /* Entry not found. */
4091 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4092
4093 /* Insert the new header name in the array in the top of the stack.
4094 * It left the new array in the top of the stack.
4095 */
4096 lua_newtable(L);
4097 lua_pushvalue(L, 2);
4098 lua_pushvalue(L, -2);
4099 lua_settable(L, -4);
4100
4101 } else if (ret != LUA_TTABLE) {
4102
4103 /* corruption error. */
4104 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4105 "is expected as an array. %s found", name, lua_typename(L, ret));
4106 WILL_LJMP(lua_error(L));
4107 }
4108
4109 /* Now the top od thestack is an array of values. We push
4110 * the header value as new entry.
4111 */
4112 lua_pushvalue(L, 3);
4113 ret = lua_rawlen(L, -2);
4114 lua_rawseti(L, -2, ret + 1);
4115 lua_pushboolean(L, 1);
4116 return 1;
4117}
4118
4119__LJMP static int hlua_applet_http_status(lua_State *L)
4120{
4121 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4122 int status = MAY_LJMP(luaL_checkinteger(L, 2));
4123
4124 if (status < 100 || status > 599) {
4125 lua_pushboolean(L, 0);
4126 return 1;
4127 }
4128
4129 appctx->appctx->ctx.hlua_apphttp.status = status;
4130 lua_pushboolean(L, 1);
4131 return 1;
4132}
4133
4134/* We will build the status line and the headers of the HTTP response.
4135 * We will try send at once if its not possible, we give back the hand
4136 * waiting for more room.
4137 */
4138__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4139{
4140 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4141 struct stream_interface *si = appctx->appctx->owner;
4142 struct channel *chn = si_ic(si);
4143 int ret;
4144 size_t len;
4145 const char *msg;
4146
4147 /* Get the message as the first argument on the stack. */
4148 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4149
4150 /* Send the message at once. */
4151 ret = bi_putblk(chn, msg, len);
4152
4153 /* if ret == -2 or -3 the channel closed or the message si too
4154 * big for the buffers.
4155 */
4156 if (ret == -2 || ret == -3) {
4157 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4158 WILL_LJMP(lua_error(L));
4159 }
4160
4161 /* If ret is -1, we dont have room in the buffer, so we yield. */
4162 if (ret == -1) {
4163 si_applet_cant_put(si);
4164 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
4165 }
4166
4167 /* Headers sent, set the flag. */
4168 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4169 return 0;
4170}
4171
4172__LJMP static int hlua_applet_http_start_response(lua_State *L)
4173{
4174 struct chunk *tmp = get_trash_chunk();
4175 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004176 const char *name;
4177 const char *value;
4178 int id;
4179 int hdr_connection = 0;
4180 int hdr_contentlength = -1;
4181 int hdr_chunked = 0;
4182
4183 /* Use the same http version than the request. */
4184 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004185 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004186 appctx->appctx->ctx.hlua_apphttp.status,
4187 get_reason(appctx->appctx->ctx.hlua_apphttp.status));
4188
4189 /* Get the array associated to the field "response" in the object AppletHTTP. */
4190 lua_pushvalue(L, 0);
4191 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4192 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4193 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4194 WILL_LJMP(lua_error(L));
4195 }
4196
4197 /* Browse the list of headers. */
4198 lua_pushnil(L);
4199 while(lua_next(L, -2) != 0) {
4200
4201 /* We expect a string as -2. */
4202 if (lua_type(L, -2) != LUA_TSTRING) {
4203 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4204 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4205 lua_typename(L, lua_type(L, -2)));
4206 WILL_LJMP(lua_error(L));
4207 }
4208 name = lua_tostring(L, -2);
4209
4210 /* We expect an array as -1. */
4211 if (lua_type(L, -1) != LUA_TTABLE) {
4212 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4213 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4214 name,
4215 lua_typename(L, lua_type(L, -1)));
4216 WILL_LJMP(lua_error(L));
4217 }
4218
4219 /* Browse the table who is on the top of the stack. */
4220 lua_pushnil(L);
4221 while(lua_next(L, -2) != 0) {
4222
4223 /* We expect a number as -2. */
4224 if (lua_type(L, -2) != LUA_TNUMBER) {
4225 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4226 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4227 name,
4228 lua_typename(L, lua_type(L, -2)));
4229 WILL_LJMP(lua_error(L));
4230 }
4231 id = lua_tointeger(L, -2);
4232
4233 /* We expect a string as -2. */
4234 if (lua_type(L, -1) != LUA_TSTRING) {
4235 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4236 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4237 name, id,
4238 lua_typename(L, lua_type(L, -1)));
4239 WILL_LJMP(lua_error(L));
4240 }
4241 value = lua_tostring(L, -1);
4242
4243 /* Catenate a new header. */
4244 chunk_appendf(tmp, "%s: %s\r\n", name, value);
4245
4246 /* Protocol checks. */
4247
4248 /* Check if the header conneciton is present. */
4249 if (strcasecmp("connection", name) == 0)
4250 hdr_connection = 1;
4251
4252 /* Copy the header content length. The length conversion
4253 * is done without control. If it contains a ad value, this
4254 * is not our problem.
4255 */
4256 if (strcasecmp("content-length", name) == 0)
4257 hdr_contentlength = atoi(value);
4258
4259 /* Check if the client annouces a transfer-encoding chunked it self. */
4260 if (strcasecmp("transfer-encoding", name) == 0 &&
4261 strcasecmp("chunked", value) == 0)
4262 hdr_chunked = 1;
4263
4264 /* Remove the array from the stack, and get next element with a remaining string. */
4265 lua_pop(L, 1);
4266 }
4267
4268 /* Remove the array from the stack, and get next element with a remaining string. */
4269 lua_pop(L, 1);
4270 }
4271
4272 /* If the http protocol version is 1.1, we expect an header "connection" set
4273 * to "close" to be HAProxy/keeplive compliant. Otherwise, we expect nothing.
4274 * If the header conneciton is present, don't change it, if it is not present,
4275 * we must set.
4276 *
4277 * we set a "connection: close" header for ensuring that the keepalive will be
4278 * respected by haproxy. HAProcy considers that the application cloe the connection
4279 * and it keep the connection from the client open.
4280 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004281 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 && !hdr_connection)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004282 chunk_appendf(tmp, "Connection: close\r\n");
4283
4284 /* If we dont have a content-length set, we must announce a transfer enconding
4285 * chunked. This is required by haproxy for the keepalive compliance.
4286 * If the applet annouce a transfer-encoding chunked itslef, don't
4287 * do anything.
4288 */
4289 if (hdr_contentlength == -1 && hdr_chunked == 0) {
4290 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
4291 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
4292 }
4293
4294 /* Finalize headers. */
4295 chunk_appendf(tmp, "\r\n");
4296
4297 /* Remove the last entry and the array of headers */
4298 lua_pop(L, 2);
4299
4300 /* Push the headers block. */
4301 lua_pushlstring(L, tmp->str, tmp->len);
4302
4303 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
4304}
4305
4306/*
4307 *
4308 *
4309 * Class HTTP
4310 *
4311 *
4312 */
4313
4314/* Returns a struct hlua_txn if the stack entry "ud" is
4315 * a class stream, otherwise it throws an error.
4316 */
4317__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
4318{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004319 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004320}
4321
4322/* This function creates and push in the stack a HTTP object
4323 * according with a current TXN.
4324 */
4325static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4326{
4327 struct hlua_txn *htxn;
4328
4329 /* Check stack size. */
4330 if (!lua_checkstack(L, 3))
4331 return 0;
4332
4333 /* Create the object: obj[0] = userdata.
4334 * Note that the base of the Converters object is the
4335 * same than the TXN object.
4336 */
4337 lua_newtable(L);
4338 htxn = lua_newuserdata(L, sizeof(*htxn));
4339 lua_rawseti(L, -2, 0);
4340
4341 htxn->s = txn->s;
4342 htxn->p = txn->p;
4343
4344 /* Pop a class stream metatable and affect it to the table. */
4345 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4346 lua_setmetatable(L, -2);
4347
4348 return 1;
4349}
4350
4351/* This function creates ans returns an array of HTTP headers.
4352 * This function does not fails. It is used as wrapper with the
4353 * 2 following functions.
4354 */
4355__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4356{
4357 const char *cur_ptr, *cur_next, *p;
4358 int old_idx, cur_idx;
4359 struct hdr_idx_elem *cur_hdr;
4360 const char *hn, *hv;
4361 int hnl, hvl;
4362 int type;
4363 const char *in;
4364 char *out;
4365 int len;
4366
4367 /* Create the table. */
4368 lua_newtable(L);
4369
4370 if (!htxn->s->txn)
4371 return 1;
4372
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004373 /* Check if a valid response is parsed */
4374 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4375 return 1;
4376
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004377 /* Build array of headers. */
4378 old_idx = 0;
4379 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
4380
4381 while (1) {
4382 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
4383 if (!cur_idx)
4384 break;
4385 old_idx = cur_idx;
4386
4387 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
4388 cur_ptr = cur_next;
4389 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
4390
4391 /* Now we have one full header at cur_ptr of len cur_hdr->len,
4392 * and the next header starts at cur_next. We'll check
4393 * this header in the list as well as against the default
4394 * rule.
4395 */
4396
4397 /* look for ': *'. */
4398 hn = cur_ptr;
4399 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
4400 if (p >= cur_ptr+cur_hdr->len)
4401 continue;
4402 hnl = p - hn;
4403 p++;
4404 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
4405 p++;
4406 if (p >= cur_ptr+cur_hdr->len)
4407 continue;
4408 hv = p;
4409 hvl = cur_ptr+cur_hdr->len-p;
4410
4411 /* Lowercase the key. Don't check the size of trash, it have
4412 * the size of one buffer and the input data contains in one
4413 * buffer.
4414 */
4415 out = trash.str;
4416 for (in=hn; in<hn+hnl; in++, out++)
4417 *out = tolower(*in);
4418 *out = '\0';
4419
4420 /* Check for existing entry:
4421 * assume that the table is on the top of the stack, and
4422 * push the key in the stack, the function lua_gettable()
4423 * perform the lookup.
4424 */
4425 lua_pushlstring(L, trash.str, hnl);
4426 lua_gettable(L, -2);
4427 type = lua_type(L, -1);
4428
4429 switch (type) {
4430 case LUA_TNIL:
4431 /* Table not found, create it. */
4432 lua_pop(L, 1); /* remove the nil value. */
4433 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
4434 lua_newtable(L); /* create and push empty table. */
4435 lua_pushlstring(L, hv, hvl); /* push header value. */
4436 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4437 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4438 break;
4439
4440 case LUA_TTABLE:
4441 /* Entry found: push the value in the table. */
4442 len = lua_rawlen(L, -1);
4443 lua_pushlstring(L, hv, hvl); /* push header value. */
4444 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4445 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4446 break;
4447
4448 default:
4449 /* Other cases are errors. */
4450 hlua_pusherror(L, "internal error during the parsing of headers.");
4451 WILL_LJMP(lua_error(L));
4452 }
4453 }
4454
4455 return 1;
4456}
4457
4458__LJMP static int hlua_http_req_get_headers(lua_State *L)
4459{
4460 struct hlua_txn *htxn;
4461
4462 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4463 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4464
4465 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4466}
4467
4468__LJMP static int hlua_http_res_get_headers(lua_State *L)
4469{
4470 struct hlua_txn *htxn;
4471
4472 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4473 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4474
4475 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4476}
4477
4478/* This function replace full header, or just a value in
4479 * the request or in the response. It is a wrapper fir the
4480 * 4 following functions.
4481 */
4482__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4483 struct http_msg *msg, int action)
4484{
4485 size_t name_len;
4486 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4487 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4488 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
4489 struct my_regex re;
4490
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004491 /* Check if a valid response is parsed */
4492 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4493 return 0;
4494
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004495 if (!regex_comp(reg, &re, 1, 1, NULL))
4496 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4497
4498 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
4499 regex_free(&re);
4500 return 0;
4501}
4502
4503__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4504{
4505 struct hlua_txn *htxn;
4506
4507 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4508 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4509
4510 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4511}
4512
4513__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4514{
4515 struct hlua_txn *htxn;
4516
4517 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4518 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4519
4520 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4521}
4522
4523__LJMP static int hlua_http_req_rep_val(lua_State *L)
4524{
4525 struct hlua_txn *htxn;
4526
4527 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4528 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4529
4530 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4531}
4532
4533__LJMP static int hlua_http_res_rep_val(lua_State *L)
4534{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004535 struct hlua_txn *htxn;
4536
4537 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4538 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4539
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004540 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004541}
4542
4543/* This function deletes all the occurences of an header.
4544 * It is a wrapper for the 2 following functions.
4545 */
4546__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4547{
4548 size_t len;
4549 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4550 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004551 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004552
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004553 /* Check if a valid response is parsed */
4554 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4555 return 0;
4556
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004557 ctx.idx = 0;
4558 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
4559 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4560 return 0;
4561}
4562
4563__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4564{
4565 struct hlua_txn *htxn;
4566
4567 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4568 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4569
Willy Tarreaueee5b512015-04-03 23:46:31 +02004570 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004571}
4572
4573__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4574{
4575 struct hlua_txn *htxn;
4576
4577 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4578 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4579
Willy Tarreaueee5b512015-04-03 23:46:31 +02004580 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004581}
4582
4583/* This function adds an header. It is a wrapper used by
4584 * the 2 following functions.
4585 */
4586__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4587{
4588 size_t name_len;
4589 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4590 size_t value_len;
4591 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
4592 char *p;
4593
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004594 /* Check if a valid message is parsed */
4595 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4596 return 0;
4597
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004598 /* Check length. */
4599 trash.len = value_len + name_len + 2;
4600 if (trash.len > trash.size)
4601 return 0;
4602
4603 /* Creates the header string. */
4604 p = trash.str;
4605 memcpy(p, name, name_len);
4606 p += name_len;
4607 *p = ':';
4608 p++;
4609 *p = ' ';
4610 p++;
4611 memcpy(p, value, value_len);
4612
Willy Tarreaueee5b512015-04-03 23:46:31 +02004613 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004614 trash.str, trash.len) != 0);
4615
4616 return 0;
4617}
4618
4619__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4620{
4621 struct hlua_txn *htxn;
4622
4623 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4624 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4625
Willy Tarreaueee5b512015-04-03 23:46:31 +02004626 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004627}
4628
4629__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4630{
4631 struct hlua_txn *htxn;
4632
4633 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4634 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4635
Willy Tarreaueee5b512015-04-03 23:46:31 +02004636 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004637}
4638
4639static int hlua_http_req_set_hdr(lua_State *L)
4640{
4641 struct hlua_txn *htxn;
4642
4643 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4644 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4645
Willy Tarreaueee5b512015-04-03 23:46:31 +02004646 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4647 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004648}
4649
4650static int hlua_http_res_set_hdr(lua_State *L)
4651{
4652 struct hlua_txn *htxn;
4653
4654 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4655 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4656
Willy Tarreaueee5b512015-04-03 23:46:31 +02004657 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4658 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004659}
4660
4661/* This function set the method. */
4662static int hlua_http_req_set_meth(lua_State *L)
4663{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004664 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004665 size_t name_len;
4666 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004667
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004668 /* Check if a valid request is parsed */
4669 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4670 lua_pushboolean(L, 0);
4671 return 1;
4672 }
4673
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004674 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004675 return 1;
4676}
4677
4678/* This function set the method. */
4679static int hlua_http_req_set_path(lua_State *L)
4680{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004681 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004682 size_t name_len;
4683 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004684
4685 /* Check if a valid request is parsed */
4686 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4687 lua_pushboolean(L, 0);
4688 return 1;
4689 }
4690
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004691 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004692 return 1;
4693}
4694
4695/* This function set the query-string. */
4696static int hlua_http_req_set_query(lua_State *L)
4697{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004698 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004699 size_t name_len;
4700 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004701
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004702 /* Check if a valid request is parsed */
4703 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4704 lua_pushboolean(L, 0);
4705 return 1;
4706 }
4707
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004708 /* Check length. */
4709 if (name_len > trash.size - 1) {
4710 lua_pushboolean(L, 0);
4711 return 1;
4712 }
4713
4714 /* Add the mark question as prefix. */
4715 chunk_reset(&trash);
4716 trash.str[trash.len++] = '?';
4717 memcpy(trash.str + trash.len, name, name_len);
4718 trash.len += name_len;
4719
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004720 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004721 return 1;
4722}
4723
4724/* This function set the uri. */
4725static int hlua_http_req_set_uri(lua_State *L)
4726{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004727 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004728 size_t name_len;
4729 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004730
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004731 /* Check if a valid request is parsed */
4732 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4733 lua_pushboolean(L, 0);
4734 return 1;
4735 }
4736
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004737 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004738 return 1;
4739}
4740
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004741/* This function set the response code. */
4742static int hlua_http_res_set_status(lua_State *L)
4743{
4744 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4745 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
4746
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004747 /* Check if a valid response is parsed */
4748 if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY))
4749 return 0;
4750
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004751 http_set_status(code, htxn->s);
4752 return 0;
4753}
4754
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004755/*
4756 *
4757 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004758 * Class TXN
4759 *
4760 *
4761 */
4762
4763/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004764 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004765 */
4766__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
4767{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004768 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004769}
4770
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004771__LJMP static int hlua_set_var(lua_State *L)
4772{
4773 struct hlua_txn *htxn;
4774 const char *name;
4775 size_t len;
4776 struct sample smp;
4777
4778 MAY_LJMP(check_args(L, 3, "set_var"));
4779
4780 /* It is useles to retrieve the stream, but this function
4781 * runs only in a stream context.
4782 */
4783 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4784 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4785
4786 /* Converts the third argument in a sample. */
4787 hlua_lua2smp(L, 3, &smp);
4788
4789 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01004790 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01004791 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004792 return 0;
4793}
4794
Christopher Faulet85d79c92016-11-09 16:54:56 +01004795__LJMP static int hlua_unset_var(lua_State *L)
4796{
4797 struct hlua_txn *htxn;
4798 const char *name;
4799 size_t len;
4800 struct sample smp;
4801
4802 MAY_LJMP(check_args(L, 2, "unset_var"));
4803
4804 /* It is useles to retrieve the stream, but this function
4805 * runs only in a stream context.
4806 */
4807 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4808 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4809
4810 /* Unset the variable. */
4811 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
4812 vars_unset_by_name(name, len, &smp);
4813 return 0;
4814}
4815
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004816__LJMP static int hlua_get_var(lua_State *L)
4817{
4818 struct hlua_txn *htxn;
4819 const char *name;
4820 size_t len;
4821 struct sample smp;
4822
4823 MAY_LJMP(check_args(L, 2, "get_var"));
4824
4825 /* It is useles to retrieve the stream, but this function
4826 * runs only in a stream context.
4827 */
4828 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4829 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4830
Willy Tarreau7560dd42016-03-10 16:28:58 +01004831 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01004832 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004833 lua_pushnil(L);
4834 return 1;
4835 }
4836
4837 return hlua_smp2lua(L, &smp);
4838}
4839
Willy Tarreau59551662015-03-10 14:23:13 +01004840__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004841{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004842 struct hlua *hlua;
4843
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004844 MAY_LJMP(check_args(L, 2, "set_priv"));
4845
Willy Tarreau87b09662015-04-03 00:22:06 +02004846 /* It is useles to retrieve the stream, but this function
4847 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004848 */
4849 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004850 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004851
4852 /* Remove previous value. */
4853 if (hlua->Mref != -1)
4854 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
4855
4856 /* Get and store new value. */
4857 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4858 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4859
4860 return 0;
4861}
4862
Willy Tarreau59551662015-03-10 14:23:13 +01004863__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004864{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004865 struct hlua *hlua;
4866
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004867 MAY_LJMP(check_args(L, 1, "get_priv"));
4868
Willy Tarreau87b09662015-04-03 00:22:06 +02004869 /* It is useles to retrieve the stream, but this function
4870 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004871 */
4872 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004873 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004874
4875 /* Push configuration index in the stack. */
4876 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4877
4878 return 1;
4879}
4880
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004881/* Create stack entry containing a class TXN. This function
4882 * return 0 if the stack does not contains free slots,
4883 * otherwise it returns 1.
4884 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02004885static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004886{
Willy Tarreaude491382015-04-06 11:04:28 +02004887 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004888
4889 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01004890 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004891 return 0;
4892
4893 /* NOTE: The allocation never fails. The failure
4894 * throw an error, and the function never returns.
4895 * if the throw is not avalaible, the process is aborted.
4896 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01004897 /* Create the object: obj[0] = userdata. */
4898 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02004899 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01004900 lua_rawseti(L, -2, 0);
4901
Willy Tarreaude491382015-04-06 11:04:28 +02004902 htxn->s = s;
4903 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01004904 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02004905 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004906
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004907 /* Create the "f" field that contains a list of fetches. */
4908 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01004909 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004910 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004911 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004912
4913 /* Create the "sf" field that contains a list of stringsafe fetches. */
4914 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01004915 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004916 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004917 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004918
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004919 /* Create the "c" field that contains a list of converters. */
4920 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02004921 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004922 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004923 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004924
4925 /* Create the "sc" field that contains a list of stringsafe converters. */
4926 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004927 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004928 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004929 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004930
Thierry FOURNIER397826a2015-03-11 19:39:09 +01004931 /* Create the "req" field that contains the request channel object. */
4932 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01004933 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01004934 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004935 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01004936
4937 /* Create the "res" field that contains the response channel object. */
4938 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01004939 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01004940 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004941 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01004942
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004943 /* Creates the HTTP object is the current proxy allows http. */
4944 lua_pushstring(L, "http");
4945 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02004946 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004947 return 0;
4948 }
4949 else
4950 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004951 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004952
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004953 /* Pop a class sesison metatable and affect it to the userdata. */
4954 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
4955 lua_setmetatable(L, -2);
4956
4957 return 1;
4958}
4959
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01004960__LJMP static int hlua_txn_deflog(lua_State *L)
4961{
4962 const char *msg;
4963 struct hlua_txn *htxn;
4964
4965 MAY_LJMP(check_args(L, 2, "deflog"));
4966 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4967 msg = MAY_LJMP(luaL_checkstring(L, 2));
4968
4969 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
4970 return 0;
4971}
4972
4973__LJMP static int hlua_txn_log(lua_State *L)
4974{
4975 int level;
4976 const char *msg;
4977 struct hlua_txn *htxn;
4978
4979 MAY_LJMP(check_args(L, 3, "log"));
4980 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4981 level = MAY_LJMP(luaL_checkinteger(L, 2));
4982 msg = MAY_LJMP(luaL_checkstring(L, 3));
4983
4984 if (level < 0 || level >= NB_LOG_LEVELS)
4985 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
4986
4987 hlua_sendlog(htxn->s->be, level, msg);
4988 return 0;
4989}
4990
4991__LJMP static int hlua_txn_log_debug(lua_State *L)
4992{
4993 const char *msg;
4994 struct hlua_txn *htxn;
4995
4996 MAY_LJMP(check_args(L, 2, "Debug"));
4997 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4998 msg = MAY_LJMP(luaL_checkstring(L, 2));
4999 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5000 return 0;
5001}
5002
5003__LJMP static int hlua_txn_log_info(lua_State *L)
5004{
5005 const char *msg;
5006 struct hlua_txn *htxn;
5007
5008 MAY_LJMP(check_args(L, 2, "Info"));
5009 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5010 msg = MAY_LJMP(luaL_checkstring(L, 2));
5011 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5012 return 0;
5013}
5014
5015__LJMP static int hlua_txn_log_warning(lua_State *L)
5016{
5017 const char *msg;
5018 struct hlua_txn *htxn;
5019
5020 MAY_LJMP(check_args(L, 2, "Warning"));
5021 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5022 msg = MAY_LJMP(luaL_checkstring(L, 2));
5023 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5024 return 0;
5025}
5026
5027__LJMP static int hlua_txn_log_alert(lua_State *L)
5028{
5029 const char *msg;
5030 struct hlua_txn *htxn;
5031
5032 MAY_LJMP(check_args(L, 2, "Alert"));
5033 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5034 msg = MAY_LJMP(luaL_checkstring(L, 2));
5035 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5036 return 0;
5037}
5038
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005039__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5040{
5041 struct hlua_txn *htxn;
5042 int ll;
5043
5044 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5045 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5046 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5047
5048 if (ll < 0 || ll > 7)
5049 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5050
5051 htxn->s->logs.level = ll;
5052 return 0;
5053}
5054
5055__LJMP static int hlua_txn_set_tos(lua_State *L)
5056{
5057 struct hlua_txn *htxn;
5058 struct connection *cli_conn;
5059 int tos;
5060
5061 MAY_LJMP(check_args(L, 2, "set_tos"));
5062 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5063 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5064
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005065 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02005066 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005067
5068 return 0;
5069}
5070
5071__LJMP static int hlua_txn_set_mark(lua_State *L)
5072{
5073#ifdef SO_MARK
5074 struct hlua_txn *htxn;
5075 struct connection *cli_conn;
5076 int mark;
5077
5078 MAY_LJMP(check_args(L, 2, "set_mark"));
5079 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5080 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5081
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005082 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau07081fe2015-04-06 10:59:20 +02005083 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005084#endif
5085 return 0;
5086}
5087
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005088/* This function is an Lua binding that send pending data
5089 * to the client, and close the stream interface.
5090 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005091__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005092{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005093 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005094 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005095 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005096
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005097 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005098 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005099 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005100
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005101 /* If the flags NOTERM is set, we cannot terminate the http
5102 * session, so we just end the execution of the current
5103 * lua code.
5104 */
5105 if (htxn->flags & HLUA_TXN_NOTERM) {
5106 WILL_LJMP(hlua_done(L));
5107 return 0;
5108 }
5109
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005110 ic = &htxn->s->req;
5111 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005112
Willy Tarreau630ef452015-08-28 10:06:15 +02005113 if (htxn->s->txn) {
5114 /* HTTP mode, let's stay in sync with the stream */
5115 bi_fast_delete(ic->buf, htxn->s->txn->req.sov);
5116 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
5117 htxn->s->txn->req.sov = 0;
5118 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
5119 oc->analysers = AN_RES_HTTP_XFER_BODY;
5120 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
5121 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
5122
Willy Tarreau630ef452015-08-28 10:06:15 +02005123 /* Note that if we want to support keep-alive, we need
5124 * to bypass the close/shutr_now calls below, but that
5125 * may only be done if the HTTP request was already
5126 * processed and the connection header is known (ie
5127 * not during TCP rules).
5128 */
5129 }
5130
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005131 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01005132 channel_abort(ic);
5133 channel_auto_close(ic);
5134 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005135
5136 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01005137 channel_auto_read(oc);
5138 channel_auto_close(oc);
5139 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005140
Willy Tarreau0458b082015-08-28 09:40:04 +02005141 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005142
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005143 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005144 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005145 return 0;
5146}
5147
5148__LJMP static int hlua_log(lua_State *L)
5149{
5150 int level;
5151 const char *msg;
5152
5153 MAY_LJMP(check_args(L, 2, "log"));
5154 level = MAY_LJMP(luaL_checkinteger(L, 1));
5155 msg = MAY_LJMP(luaL_checkstring(L, 2));
5156
5157 if (level < 0 || level >= NB_LOG_LEVELS)
5158 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5159
5160 hlua_sendlog(NULL, level, msg);
5161 return 0;
5162}
5163
5164__LJMP static int hlua_log_debug(lua_State *L)
5165{
5166 const char *msg;
5167
5168 MAY_LJMP(check_args(L, 1, "debug"));
5169 msg = MAY_LJMP(luaL_checkstring(L, 1));
5170 hlua_sendlog(NULL, LOG_DEBUG, msg);
5171 return 0;
5172}
5173
5174__LJMP static int hlua_log_info(lua_State *L)
5175{
5176 const char *msg;
5177
5178 MAY_LJMP(check_args(L, 1, "info"));
5179 msg = MAY_LJMP(luaL_checkstring(L, 1));
5180 hlua_sendlog(NULL, LOG_INFO, msg);
5181 return 0;
5182}
5183
5184__LJMP static int hlua_log_warning(lua_State *L)
5185{
5186 const char *msg;
5187
5188 MAY_LJMP(check_args(L, 1, "warning"));
5189 msg = MAY_LJMP(luaL_checkstring(L, 1));
5190 hlua_sendlog(NULL, LOG_WARNING, msg);
5191 return 0;
5192}
5193
5194__LJMP static int hlua_log_alert(lua_State *L)
5195{
5196 const char *msg;
5197
5198 MAY_LJMP(check_args(L, 1, "alert"));
5199 msg = MAY_LJMP(luaL_checkstring(L, 1));
5200 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005201 return 0;
5202}
5203
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005204__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005205{
5206 int wakeup_ms = lua_tointeger(L, -1);
5207 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005208 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005209 return 0;
5210}
5211
5212__LJMP static int hlua_sleep(lua_State *L)
5213{
5214 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005215 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005216
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005217 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005218
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005219 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005220 wakeup_ms = tick_add(now_ms, delay);
5221 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005222
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005223 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5224 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005225}
5226
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005227__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005228{
5229 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005230 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005231
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005232 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005233
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005234 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005235 wakeup_ms = tick_add(now_ms, delay);
5236 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005237
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005238 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5239 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005240}
5241
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005242/* This functionis an LUA binding. it permits to give back
5243 * the hand at the HAProxy scheduler. It is used when the
5244 * LUA processing consumes a lot of time.
5245 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005246__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005247{
5248 return 0;
5249}
5250
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005251__LJMP static int hlua_yield(lua_State *L)
5252{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005253 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
5254 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005255}
5256
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005257/* This function change the nice of the currently executed
5258 * task. It is used set low or high priority at the current
5259 * task.
5260 */
Willy Tarreau59551662015-03-10 14:23:13 +01005261__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005262{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005263 struct hlua *hlua;
5264 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005265
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005266 MAY_LJMP(check_args(L, 1, "set_nice"));
5267 hlua = hlua_gethlua(L);
5268 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005269
5270 /* If he task is not set, I'm in a start mode. */
5271 if (!hlua || !hlua->task)
5272 return 0;
5273
5274 if (nice < -1024)
5275 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005276 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005277 nice = 1024;
5278
5279 hlua->task->nice = nice;
5280 return 0;
5281}
5282
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005283/* This function is used as a calback of a task. It is called by the
5284 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5285 * return an E_AGAIN signal, the emmiter of this signal must set a
5286 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005287 *
5288 * Task wrapper are longjmp safe because the only one Lua code
5289 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005290 */
5291static struct task *hlua_process_task(struct task *task)
5292{
5293 struct hlua *hlua = task->context;
5294 enum hlua_exec status;
5295
5296 /* We need to remove the task from the wait queue before executing
5297 * the Lua code because we don't know if it needs to wait for
5298 * another timer or not in the case of E_AGAIN.
5299 */
5300 task_delete(task);
5301
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005302 /* If it is the first call to the task, we must initialize the
5303 * execution timeouts.
5304 */
5305 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005306 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005307
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005308 /* Execute the Lua code. */
5309 status = hlua_ctx_resume(hlua, 1);
5310
5311 switch (status) {
5312 /* finished or yield */
5313 case HLUA_E_OK:
5314 hlua_ctx_destroy(hlua);
5315 task_delete(task);
5316 task_free(task);
5317 break;
5318
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005319 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
5320 if (hlua->wake_time != TICK_ETERNITY)
5321 task_schedule(task, hlua->wake_time);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005322 break;
5323
5324 /* finished with error. */
5325 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005326 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005327 hlua_ctx_destroy(hlua);
5328 task_delete(task);
5329 task_free(task);
5330 break;
5331
5332 case HLUA_E_ERR:
5333 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005334 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005335 hlua_ctx_destroy(hlua);
5336 task_delete(task);
5337 task_free(task);
5338 break;
5339 }
5340 return NULL;
5341}
5342
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005343/* This function is an LUA binding that register LUA function to be
5344 * executed after the HAProxy configuration parsing and before the
5345 * HAProxy scheduler starts. This function expect only one LUA
5346 * argument that is a function. This function returns nothing, but
5347 * throws if an error is encountered.
5348 */
5349__LJMP static int hlua_register_init(lua_State *L)
5350{
5351 struct hlua_init_function *init;
5352 int ref;
5353
5354 MAY_LJMP(check_args(L, 1, "register_init"));
5355
5356 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5357
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005358 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005359 if (!init)
5360 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5361
5362 init->function_ref = ref;
5363 LIST_ADDQ(&hlua_init_functions, &init->l);
5364 return 0;
5365}
5366
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005367/* This functio is an LUA binding. It permits to register a task
5368 * executed in parallel of the main HAroxy activity. The task is
5369 * created and it is set in the HAProxy scheduler. It can be called
5370 * from the "init" section, "post init" or during the runtime.
5371 *
5372 * Lua prototype:
5373 *
5374 * <none> core.register_task(<function>)
5375 */
5376static int hlua_register_task(lua_State *L)
5377{
5378 struct hlua *hlua;
5379 struct task *task;
5380 int ref;
5381
5382 MAY_LJMP(check_args(L, 1, "register_task"));
5383
5384 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5385
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005386 hlua = pool_alloc2(pool2_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005387 if (!hlua)
5388 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5389
5390 task = task_new();
5391 task->context = hlua;
5392 task->process = hlua_process_task;
5393
5394 if (!hlua_ctx_init(hlua, task))
5395 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5396
5397 /* Restore the function in the stack. */
5398 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5399 hlua->nargs = 0;
5400
5401 /* Schedule task. */
5402 task_schedule(task, now_ms);
5403
5404 return 0;
5405}
5406
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005407/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5408 * doesn't allow "yield" functions because the HAProxy engine cannot
5409 * resume converters.
5410 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005411static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005412{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005413 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005414 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005415 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005416
Willy Tarreaube508f12016-03-10 11:47:01 +01005417 if (!stream)
5418 return 0;
5419
Willy Tarreau87b09662015-04-03 00:22:06 +02005420 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005421 * Lua context can be not initialized. This behavior
5422 * permits to save performances because a systematic
5423 * Lua initialization cause 5% performances loss.
5424 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005425 if (!stream->hlua) {
5426 stream->hlua = pool_alloc2(pool2_hlua);
5427 if (!stream->hlua) {
5428 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5429 return 0;
5430 }
5431 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5432 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5433 return 0;
5434 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005435 }
5436
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005437 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005438 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005439
5440 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005441 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5442 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5443 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005444 else
5445 error = "critical error";
5446 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005447 return 0;
5448 }
5449
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005450 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005451 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005452 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005453 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005454 return 0;
5455 }
5456
5457 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005458 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005459
5460 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005461 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005462 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005463 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005464 return 0;
5465 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005466 hlua_smp2lua(stream->hlua->T, smp);
5467 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005468
5469 /* push keywords in the stack. */
5470 if (arg_p) {
5471 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005472 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005473 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005474 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005475 return 0;
5476 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005477 hlua_arg2lua(stream->hlua->T, arg_p);
5478 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005479 }
5480 }
5481
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005482 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005483 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005484
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005485 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005486 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005487 }
5488
5489 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005490 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005491 /* finished. */
5492 case HLUA_E_OK:
5493 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005494 hlua_lua2smp(stream->hlua->T, -1, smp);
5495 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005496 return 1;
5497
5498 /* yield. */
5499 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005500 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005501 return 0;
5502
5503 /* finished with error. */
5504 case HLUA_E_ERRMSG:
5505 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005506 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005507 fcn->name, lua_tostring(stream->hlua->T, -1));
5508 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005509 return 0;
5510
5511 case HLUA_E_ERR:
5512 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005513 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005514
5515 default:
5516 return 0;
5517 }
5518}
5519
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005520/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5521 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005522 * resume sample-fetches. This function will be called by the sample
5523 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005524 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005525static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5526 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005527{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005528 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005529 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005530 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005531 const struct chunk msg = { .len = 0 };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005532
Willy Tarreaube508f12016-03-10 11:47:01 +01005533 if (!stream)
5534 return 0;
5535
Willy Tarreau87b09662015-04-03 00:22:06 +02005536 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005537 * Lua context can be not initialized. This behavior
5538 * permits to save performances because a systematic
5539 * Lua initialization cause 5% performances loss.
5540 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005541 if (!stream->hlua) {
5542 stream->hlua = pool_alloc2(pool2_hlua);
5543 if (!stream->hlua) {
5544 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5545 return 0;
5546 }
5547 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5548 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5549 return 0;
5550 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005551 }
5552
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005553 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005554
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005555 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005556 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005557
5558 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005559 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5560 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5561 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005562 else
5563 error = "critical error";
5564 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005565 return 0;
5566 }
5567
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005568 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005569 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005570 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005571 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005572 return 0;
5573 }
5574
5575 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005576 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005577
5578 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005579 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005580 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005581 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005582 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005583 return 0;
5584 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005585 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005586
5587 /* push keywords in the stack. */
5588 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5589 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005590 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005591 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005592 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005593 return 0;
5594 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005595 hlua_arg2lua(stream->hlua->T, arg_p);
5596 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005597 }
5598
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005599 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005600 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005601
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005602 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005603 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005604 }
5605
5606 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005607 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005608 /* finished. */
5609 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005610 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005611 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005612 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005613 }
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005614 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005615 hlua_lua2smp(stream->hlua->T, -1, smp);
5616 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005617
5618 /* Set the end of execution flag. */
5619 smp->flags &= ~SMP_F_MAY_CHANGE;
5620 return 1;
5621
5622 /* yield. */
5623 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005624 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005625 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005626 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005627 return 0;
5628
5629 /* finished with error. */
5630 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005631 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005632 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005633 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005634 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005635 fcn->name, lua_tostring(stream->hlua->T, -1));
5636 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005637 return 0;
5638
5639 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005640 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005641 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005642 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005643 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005644
5645 default:
5646 return 0;
5647 }
5648}
5649
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005650/* This function is an LUA binding used for registering
5651 * "sample-conv" functions. It expects a converter name used
5652 * in the haproxy configuration file, and an LUA function.
5653 */
5654__LJMP static int hlua_register_converters(lua_State *L)
5655{
5656 struct sample_conv_kw_list *sck;
5657 const char *name;
5658 int ref;
5659 int len;
5660 struct hlua_function *fcn;
5661
5662 MAY_LJMP(check_args(L, 2, "register_converters"));
5663
5664 /* First argument : converter name. */
5665 name = MAY_LJMP(luaL_checkstring(L, 1));
5666
5667 /* Second argument : lua function. */
5668 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5669
5670 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005671 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005672 if (!sck)
5673 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005674 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005675 if (!fcn)
5676 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5677
5678 /* Fill fcn. */
5679 fcn->name = strdup(name);
5680 if (!fcn->name)
5681 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5682 fcn->function_ref = ref;
5683
5684 /* List head */
5685 sck->list.n = sck->list.p = NULL;
5686
5687 /* converter keyword. */
5688 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005689 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005690 if (!sck->kw[0].kw)
5691 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5692
5693 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
5694 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01005695 sck->kw[0].arg_mask = ARG12(0,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005696 sck->kw[0].val_args = NULL;
5697 sck->kw[0].in_type = SMP_T_STR;
5698 sck->kw[0].out_type = SMP_T_STR;
5699 sck->kw[0].private = fcn;
5700
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005701 /* Register this new converter */
5702 sample_register_convs(sck);
5703
5704 return 0;
5705}
5706
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005707/* This fucntion is an LUA binding used for registering
5708 * "sample-fetch" functions. It expects a converter name used
5709 * in the haproxy configuration file, and an LUA function.
5710 */
5711__LJMP static int hlua_register_fetches(lua_State *L)
5712{
5713 const char *name;
5714 int ref;
5715 int len;
5716 struct sample_fetch_kw_list *sfk;
5717 struct hlua_function *fcn;
5718
5719 MAY_LJMP(check_args(L, 2, "register_fetches"));
5720
5721 /* First argument : sample-fetch name. */
5722 name = MAY_LJMP(luaL_checkstring(L, 1));
5723
5724 /* Second argument : lua function. */
5725 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5726
5727 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005728 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005729 if (!sfk)
5730 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005731 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005732 if (!fcn)
5733 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5734
5735 /* Fill fcn. */
5736 fcn->name = strdup(name);
5737 if (!fcn->name)
5738 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5739 fcn->function_ref = ref;
5740
5741 /* List head */
5742 sfk->list.n = sfk->list.p = NULL;
5743
5744 /* sample-fetch keyword. */
5745 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005746 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005747 if (!sfk->kw[0].kw)
5748 return luaL_error(L, "lua out of memory error.");
5749
5750 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
5751 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01005752 sfk->kw[0].arg_mask = ARG12(0,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005753 sfk->kw[0].val_args = NULL;
5754 sfk->kw[0].out_type = SMP_T_STR;
5755 sfk->kw[0].use = SMP_USE_HTTP_ANY;
5756 sfk->kw[0].val = 0;
5757 sfk->kw[0].private = fcn;
5758
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005759 /* Register this new fetch. */
5760 sample_register_fetches(sfk);
5761
5762 return 0;
5763}
5764
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005765/* This function is a wrapper to execute each LUA function declared
5766 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005767 * return ACT_RET_CONT if the processing is finished (with or without
5768 * error) and return ACT_RET_YIELD if the function must be called again
5769 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005770 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005771static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02005772 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005773{
5774 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005775 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005776 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005777 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005778 const struct chunk msg = { .len = 0 };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005779
5780 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01005781 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
5782 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
5783 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
5784 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005785 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005786 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005787 return ACT_RET_CONT;
5788 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005789
Willy Tarreau87b09662015-04-03 00:22:06 +02005790 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005791 * Lua context can be not initialized. This behavior
5792 * permits to save performances because a systematic
5793 * Lua initialization cause 5% performances loss.
5794 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005795 if (!s->hlua) {
5796 s->hlua = pool_alloc2(pool2_hlua);
5797 if (!s->hlua) {
5798 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
5799 rule->arg.hlua_rule->fcn.name);
5800 return ACT_RET_CONT;
5801 }
5802 if (!hlua_ctx_init(s->hlua, s->task)) {
5803 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
5804 rule->arg.hlua_rule->fcn.name);
5805 return ACT_RET_CONT;
5806 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005807 }
5808
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005809 consistency_set(s, dir, &s->hlua->cons);
5810
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005811 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005812 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005813
5814 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005815 if (!SET_SAFE_LJMP(s->hlua->T)) {
5816 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
5817 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005818 else
5819 error = "critical error";
5820 SEND_ERR(px, "Lua function '%s': %s.\n",
5821 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005822 return ACT_RET_CONT;
5823 }
5824
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005825 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005826 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005827 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005828 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005829 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005830 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005831 }
5832
5833 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005834 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005835
Willy Tarreau87b09662015-04-03 00:22:06 +02005836 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005837 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005838 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005839 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005840 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005841 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005842 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005843 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005844
5845 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005846 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005847 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005848 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005849 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005850 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005851 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005852 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005853 lua_pushstring(s->hlua->T, *arg);
5854 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005855 }
5856
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005857 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005858 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005859
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005860 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005861 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005862 }
5863
5864 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005865 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005866 /* finished. */
5867 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005868 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005869 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005870 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005871 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005872 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005873 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005874 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005875
5876 /* yield. */
5877 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005878 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005879 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005880 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005881 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005882 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005883 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005884 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005885 /* Some actions can be wake up when a "write" event
5886 * is detected on a response channel. This is useful
5887 * only for actions targetted on the requests.
5888 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005889 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005890 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01005891 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005892 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005893 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005894 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005895 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005896 /* We can quit the fcuntion without consistency check
5897 * because HAProxy is not able to manipulate data, it
5898 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005899 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005900
5901 /* finished with error. */
5902 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005903 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005904 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005905 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005906 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005907 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005908 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005909 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
5910 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005911 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005912
5913 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005914 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005915 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005916 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005917 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005918 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005919 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005920 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005921
5922 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005923 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005924 }
5925}
5926
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005927struct task *hlua_applet_wakeup(struct task *t)
5928{
5929 struct appctx *ctx = t->context;
5930 struct stream_interface *si = ctx->owner;
5931
5932 /* If the applet is wake up without any expected work, the sheduler
5933 * remove it from the run queue. This flag indicate that the applet
5934 * is waiting for write. If the buffer is full, the main processing
5935 * will send some data and after call the applet, otherwise it call
5936 * the applet ASAP.
5937 */
5938 si_applet_cant_put(si);
5939 appctx_wakeup(ctx);
5940 return NULL;
5941}
5942
5943static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
5944{
5945 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01005946 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005947 struct task *task;
5948 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005949 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005950
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01005951 hlua = pool_alloc2(pool2_hlua);
5952 if (!hlua) {
5953 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
5954 ctx->rule->arg.hlua_rule->fcn.name);
5955 return 0;
5956 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005957 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01005958 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005959 ctx->ctx.hlua_apptcp.flags = 0;
5960
5961 /* Create task used by signal to wakeup applets. */
5962 task = task_new();
5963 if (!task) {
5964 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
5965 ctx->rule->arg.hlua_rule->fcn.name);
5966 return 0;
5967 }
5968 task->nice = 0;
5969 task->context = ctx;
5970 task->process = hlua_applet_wakeup;
5971 ctx->ctx.hlua_apptcp.task = task;
5972
5973 /* In the execution wrappers linked with a stream, the
5974 * Lua context can be not initialized. This behavior
5975 * permits to save performances because a systematic
5976 * Lua initialization cause 5% performances loss.
5977 */
5978 if (!hlua_ctx_init(hlua, task)) {
5979 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
5980 ctx->rule->arg.hlua_rule->fcn.name);
5981 return 0;
5982 }
5983
5984 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005985 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005986
5987 /* The following Lua calls can fail. */
5988 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01005989 if (lua_type(hlua->T, -1) == LUA_TSTRING)
5990 error = lua_tostring(hlua->T, -1);
5991 else
5992 error = "critical error";
5993 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
5994 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005995 RESET_SAFE_LJMP(hlua->T);
5996 return 0;
5997 }
5998
5999 /* Check stack available size. */
6000 if (!lua_checkstack(hlua->T, 1)) {
6001 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6002 ctx->rule->arg.hlua_rule->fcn.name);
6003 RESET_SAFE_LJMP(hlua->T);
6004 return 0;
6005 }
6006
6007 /* Restore the function in the stack. */
6008 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6009
6010 /* Create and and push object stream in the stack. */
6011 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6012 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6013 ctx->rule->arg.hlua_rule->fcn.name);
6014 RESET_SAFE_LJMP(hlua->T);
6015 return 0;
6016 }
6017 hlua->nargs = 1;
6018
6019 /* push keywords in the stack. */
6020 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6021 if (!lua_checkstack(hlua->T, 1)) {
6022 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6023 ctx->rule->arg.hlua_rule->fcn.name);
6024 RESET_SAFE_LJMP(hlua->T);
6025 return 0;
6026 }
6027 lua_pushstring(hlua->T, *arg);
6028 hlua->nargs++;
6029 }
6030
6031 RESET_SAFE_LJMP(hlua->T);
6032
6033 /* Wakeup the applet ASAP. */
6034 si_applet_cant_get(si);
6035 si_applet_cant_put(si);
6036
6037 return 1;
6038}
6039
6040static void hlua_applet_tcp_fct(struct appctx *ctx)
6041{
6042 struct stream_interface *si = ctx->owner;
6043 struct stream *strm = si_strm(si);
6044 struct channel *res = si_ic(si);
6045 struct act_rule *rule = ctx->rule;
6046 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006047 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006048
6049 /* The applet execution is already done. */
6050 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE)
6051 return;
6052
6053 /* If the stream is disconnect or closed, ldo nothing. */
6054 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6055 return;
6056
6057 /* Execute the function. */
6058 switch (hlua_ctx_resume(hlua, 1)) {
6059 /* finished. */
6060 case HLUA_E_OK:
6061 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6062
6063 /* log time */
6064 strm->logs.tv_request = now;
6065
6066 /* eat the whole request */
6067 bo_skip(si_oc(si), si_ob(si)->o);
6068 res->flags |= CF_READ_NULL;
6069 si_shutr(si);
6070 return;
6071
6072 /* yield. */
6073 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006074 if (hlua->wake_time != TICK_ETERNITY)
6075 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006076 return;
6077
6078 /* finished with error. */
6079 case HLUA_E_ERRMSG:
6080 /* Display log. */
6081 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6082 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6083 lua_pop(hlua->T, 1);
6084 goto error;
6085
6086 case HLUA_E_ERR:
6087 /* Display log. */
6088 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6089 rule->arg.hlua_rule->fcn.name);
6090 goto error;
6091
6092 default:
6093 goto error;
6094 }
6095
6096error:
6097
6098 /* For all other cases, just close the stream. */
6099 si_shutw(si);
6100 si_shutr(si);
6101 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6102}
6103
6104static void hlua_applet_tcp_release(struct appctx *ctx)
6105{
6106 task_free(ctx->ctx.hlua_apptcp.task);
6107 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006108 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006109 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006110}
6111
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006112/* The function returns 1 if the initialisation is complete, 0 if
6113 * an errors occurs and -1 if more data are required for initializing
6114 * the applet.
6115 */
6116static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6117{
6118 struct stream_interface *si = ctx->owner;
6119 struct channel *req = si_oc(si);
6120 struct http_msg *msg;
6121 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006122 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006123 char **arg;
6124 struct hdr_ctx hdr;
6125 struct task *task;
6126 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006127 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006128
6129 /* Wait for a full HTTP request. */
6130 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6131 if (smp.flags & SMP_F_MAY_CHANGE)
6132 return -1;
6133 return 0;
6134 }
6135 txn = strm->txn;
6136 msg = &txn->req;
6137
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006138 /* We want two things in HTTP mode :
6139 * - enforce server-close mode if we were in keep-alive, so that the
6140 * applet is released after each response ;
6141 * - enable request body transfer to the applet in order to resync
6142 * with the response body.
6143 */
6144 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6145 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006146
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006147 hlua = pool_alloc2(pool2_hlua);
6148 if (!hlua) {
6149 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6150 ctx->rule->arg.hlua_rule->fcn.name);
6151 return 0;
6152 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006153 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006154 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006155 ctx->ctx.hlua_apphttp.left_bytes = -1;
6156 ctx->ctx.hlua_apphttp.flags = 0;
6157
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006158 if (txn->req.flags & HTTP_MSGF_VER_11)
6159 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6160
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006161 /* Create task used by signal to wakeup applets. */
6162 task = task_new();
6163 if (!task) {
6164 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6165 ctx->rule->arg.hlua_rule->fcn.name);
6166 return 0;
6167 }
6168 task->nice = 0;
6169 task->context = ctx;
6170 task->process = hlua_applet_wakeup;
6171 ctx->ctx.hlua_apphttp.task = task;
6172
6173 /* In the execution wrappers linked with a stream, the
6174 * Lua context can be not initialized. This behavior
6175 * permits to save performances because a systematic
6176 * Lua initialization cause 5% performances loss.
6177 */
6178 if (!hlua_ctx_init(hlua, task)) {
6179 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6180 ctx->rule->arg.hlua_rule->fcn.name);
6181 return 0;
6182 }
6183
6184 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006185 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006186
6187 /* The following Lua calls can fail. */
6188 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006189 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6190 error = lua_tostring(hlua->T, -1);
6191 else
6192 error = "critical error";
6193 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6194 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006195 return 0;
6196 }
6197
6198 /* Check stack available size. */
6199 if (!lua_checkstack(hlua->T, 1)) {
6200 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6201 ctx->rule->arg.hlua_rule->fcn.name);
6202 RESET_SAFE_LJMP(hlua->T);
6203 return 0;
6204 }
6205
6206 /* Restore the function in the stack. */
6207 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6208
6209 /* Create and and push object stream in the stack. */
6210 if (!hlua_applet_http_new(hlua->T, ctx)) {
6211 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6212 ctx->rule->arg.hlua_rule->fcn.name);
6213 RESET_SAFE_LJMP(hlua->T);
6214 return 0;
6215 }
6216 hlua->nargs = 1;
6217
6218 /* Look for a 100-continue expected. */
6219 if (msg->flags & HTTP_MSGF_VER_11) {
6220 hdr.idx = 0;
6221 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) &&
6222 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6223 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6224 }
6225
6226 /* push keywords in the stack. */
6227 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6228 if (!lua_checkstack(hlua->T, 1)) {
6229 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6230 ctx->rule->arg.hlua_rule->fcn.name);
6231 RESET_SAFE_LJMP(hlua->T);
6232 return 0;
6233 }
6234 lua_pushstring(hlua->T, *arg);
6235 hlua->nargs++;
6236 }
6237
6238 RESET_SAFE_LJMP(hlua->T);
6239
6240 /* Wakeup the applet when data is ready for read. */
6241 si_applet_cant_get(si);
6242
6243 return 1;
6244}
6245
6246static void hlua_applet_http_fct(struct appctx *ctx)
6247{
6248 struct stream_interface *si = ctx->owner;
6249 struct stream *strm = si_strm(si);
6250 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006251 struct act_rule *rule = ctx->rule;
6252 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006253 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006254 char *blk1;
6255 int len1;
6256 char *blk2;
6257 int len2;
6258 int ret;
6259
6260 /* If the stream is disconnect or closed, ldo nothing. */
6261 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6262 return;
6263
6264 /* Set the currently running flag. */
6265 if (!HLUA_IS_RUNNING(hlua) &&
6266 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6267
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006268 /* Wait for full HTTP analysys. */
6269 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
6270 si_applet_cant_get(si);
6271 return;
6272 }
6273
6274 /* Store the max amount of bytes that we can read. */
6275 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6276
6277 /* We need to flush the request header. This left the body
6278 * for the Lua.
6279 */
6280
6281 /* Read the maximum amount of data avalaible. */
6282 ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
6283 if (ret == -1)
6284 return;
6285
6286 /* No data available, ask for more data. */
6287 if (ret == 1)
6288 len2 = 0;
6289 if (ret == 0)
6290 len1 = 0;
6291 if (len1 + len2 < strm->txn->req.eoh + 2) {
6292 si_applet_cant_get(si);
6293 return;
6294 }
6295
6296 /* skip the requests bytes. */
6297 bo_skip(si_oc(si), strm->txn->req.eoh + 2);
6298 }
6299
6300 /* Executes The applet if it is not done. */
6301 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6302
6303 /* Execute the function. */
6304 switch (hlua_ctx_resume(hlua, 1)) {
6305 /* finished. */
6306 case HLUA_E_OK:
6307 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6308 break;
6309
6310 /* yield. */
6311 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006312 if (hlua->wake_time != TICK_ETERNITY)
6313 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006314 return;
6315
6316 /* finished with error. */
6317 case HLUA_E_ERRMSG:
6318 /* Display log. */
6319 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6320 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6321 lua_pop(hlua->T, 1);
6322 goto error;
6323
6324 case HLUA_E_ERR:
6325 /* Display log. */
6326 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6327 rule->arg.hlua_rule->fcn.name);
6328 goto error;
6329
6330 default:
6331 goto error;
6332 }
6333 }
6334
6335 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6336
6337 /* We must send the final chunk. */
6338 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6339 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6340
6341 /* sent last chunk at once. */
6342 ret = bi_putblk(res, "0\r\n\r\n", 5);
6343
6344 /* critical error. */
6345 if (ret == -2 || ret == -3) {
6346 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6347 rule->arg.hlua_rule->fcn.name);
6348 goto error;
6349 }
6350
6351 /* no enough space error. */
6352 if (ret == -1) {
6353 si_applet_cant_put(si);
6354 return;
6355 }
6356
6357 /* set the last chunk sent. */
6358 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6359 }
6360
6361 /* close the connection. */
6362
6363 /* status / log */
6364 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6365 strm->logs.tv_request = now;
6366
6367 /* eat the whole request */
6368 bo_skip(si_oc(si), si_ob(si)->o);
6369 res->flags |= CF_READ_NULL;
6370 si_shutr(si);
6371
6372 return;
6373 }
6374
6375error:
6376
6377 /* If we are in HTTP mode, and we are not send any
6378 * data, return a 500 server error in best effort:
6379 * if there are no room avalaible in the buffer,
6380 * just close the connection.
6381 */
6382 bi_putblk(res, error_500, strlen(error_500));
6383 if (!(strm->flags & SF_ERR_MASK))
6384 strm->flags |= SF_ERR_RESOURCE;
6385 si_shutw(si);
6386 si_shutr(si);
6387 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6388}
6389
6390static void hlua_applet_http_release(struct appctx *ctx)
6391{
6392 task_free(ctx->ctx.hlua_apphttp.task);
6393 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006394 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006395 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006396}
6397
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006398/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6399 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006400 *
6401 * This function can fail with an abort() due to an Lua critical error.
6402 * We are in the configuration parsing process of HAProxy, this abort() is
6403 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006404 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006405static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6406 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006407{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006408 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006409 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006410
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006411 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006412 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006413 if (!rule->arg.hlua_rule) {
6414 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006415 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006416 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006417
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006418 /* Memory for arguments. */
6419 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6420 if (!rule->arg.hlua_rule->args) {
6421 memprintf(err, "out of memory error");
6422 return ACT_RET_PRS_ERR;
6423 }
6424
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006425 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006426 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006427
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006428 /* Expect some arguments */
6429 for (i = 0; i < fcn->nargs; i++) {
6430 if (*args[i+1] == '\0') {
6431 memprintf(err, "expect %d arguments", fcn->nargs);
6432 return ACT_RET_PRS_ERR;
6433 }
6434 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6435 if (!rule->arg.hlua_rule->args[i]) {
6436 memprintf(err, "out of memory error");
6437 return ACT_RET_PRS_ERR;
6438 }
6439 (*cur_arg)++;
6440 }
6441 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006442
Thierry FOURNIER42148732015-09-02 17:17:33 +02006443 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006444 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006445 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006446}
6447
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006448static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6449 struct act_rule *rule, char **err)
6450{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006451 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006452
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006453 /* HTTP applets are forbidden in tcp-request rules.
6454 * HTTP applet request requires everything initilized by
6455 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6456 * The applet will be immediately initilized, but its before
6457 * the call of this analyzer.
6458 */
6459 if (rule->from != ACT_F_HTTP_REQ) {
6460 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6461 return ACT_RET_PRS_ERR;
6462 }
6463
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006464 /* Memory for the rule. */
6465 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6466 if (!rule->arg.hlua_rule) {
6467 memprintf(err, "out of memory error");
6468 return ACT_RET_PRS_ERR;
6469 }
6470
6471 /* Reference the Lua function and store the reference. */
6472 rule->arg.hlua_rule->fcn = *fcn;
6473
6474 /* TODO: later accept arguments. */
6475 rule->arg.hlua_rule->args = NULL;
6476
6477 /* Add applet pointer in the rule. */
6478 rule->applet.obj_type = OBJ_TYPE_APPLET;
6479 rule->applet.name = fcn->name;
6480 rule->applet.init = hlua_applet_http_init;
6481 rule->applet.fct = hlua_applet_http_fct;
6482 rule->applet.release = hlua_applet_http_release;
6483 rule->applet.timeout = hlua_timeout_applet;
6484
6485 return ACT_RET_PRS_OK;
6486}
6487
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006488/* This function is an LUA binding used for registering
6489 * "sample-conv" functions. It expects a converter name used
6490 * in the haproxy configuration file, and an LUA function.
6491 */
6492__LJMP static int hlua_register_action(lua_State *L)
6493{
6494 struct action_kw_list *akl;
6495 const char *name;
6496 int ref;
6497 int len;
6498 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006499 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006500
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006501 /* Initialise the number of expected arguments at 0. */
6502 nargs = 0;
6503
6504 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6505 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006506
6507 /* First argument : converter name. */
6508 name = MAY_LJMP(luaL_checkstring(L, 1));
6509
6510 /* Second argument : environment. */
6511 if (lua_type(L, 2) != LUA_TTABLE)
6512 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6513
6514 /* Third argument : lua function. */
6515 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6516
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006517 /* Fouth argument : number of mandatories arguments expected on the configuration line. */
6518 if (lua_gettop(L) >= 4)
6519 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6520
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006521 /* browse the second argulent as an array. */
6522 lua_pushnil(L);
6523 while (lua_next(L, 2) != 0) {
6524 if (lua_type(L, -1) != LUA_TSTRING)
6525 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6526
6527 /* Check required environment. Only accepted "http" or "tcp". */
6528 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006529 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006530 if (!akl)
6531 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006532 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006533 if (!fcn)
6534 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6535
6536 /* Fill fcn. */
6537 fcn->name = strdup(name);
6538 if (!fcn->name)
6539 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6540 fcn->function_ref = ref;
6541
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006542 /* Set the expected number od arguments. */
6543 fcn->nargs = nargs;
6544
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006545 /* List head */
6546 akl->list.n = akl->list.p = NULL;
6547
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006548 /* action keyword. */
6549 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006550 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006551 if (!akl->kw[0].kw)
6552 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6553
6554 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6555
6556 akl->kw[0].match_pfx = 0;
6557 akl->kw[0].private = fcn;
6558 akl->kw[0].parse = action_register_lua;
6559
6560 /* select the action registering point. */
6561 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6562 tcp_req_cont_keywords_register(akl);
6563 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6564 tcp_res_cont_keywords_register(akl);
6565 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6566 http_req_keywords_register(akl);
6567 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6568 http_res_keywords_register(akl);
6569 else
6570 WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. "
6571 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6572 "are expected.", lua_tostring(L, -1)));
6573
6574 /* pop the environment string. */
6575 lua_pop(L, 1);
6576 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006577 return ACT_RET_PRS_OK;
6578}
6579
6580static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6581 struct act_rule *rule, char **err)
6582{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006583 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006584
6585 /* Memory for the rule. */
6586 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6587 if (!rule->arg.hlua_rule) {
6588 memprintf(err, "out of memory error");
6589 return ACT_RET_PRS_ERR;
6590 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006591
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006592 /* Reference the Lua function and store the reference. */
6593 rule->arg.hlua_rule->fcn = *fcn;
6594
6595 /* TODO: later accept arguments. */
6596 rule->arg.hlua_rule->args = NULL;
6597
6598 /* Add applet pointer in the rule. */
6599 rule->applet.obj_type = OBJ_TYPE_APPLET;
6600 rule->applet.name = fcn->name;
6601 rule->applet.init = hlua_applet_tcp_init;
6602 rule->applet.fct = hlua_applet_tcp_fct;
6603 rule->applet.release = hlua_applet_tcp_release;
6604 rule->applet.timeout = hlua_timeout_applet;
6605
6606 return 0;
6607}
6608
6609/* This function is an LUA binding used for registering
6610 * "sample-conv" functions. It expects a converter name used
6611 * in the haproxy configuration file, and an LUA function.
6612 */
6613__LJMP static int hlua_register_service(lua_State *L)
6614{
6615 struct action_kw_list *akl;
6616 const char *name;
6617 const char *env;
6618 int ref;
6619 int len;
6620 struct hlua_function *fcn;
6621
6622 MAY_LJMP(check_args(L, 3, "register_service"));
6623
6624 /* First argument : converter name. */
6625 name = MAY_LJMP(luaL_checkstring(L, 1));
6626
6627 /* Second argument : environment. */
6628 env = MAY_LJMP(luaL_checkstring(L, 2));
6629
6630 /* Third argument : lua function. */
6631 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6632
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006633 /* Allocate and fill the sample fetch keyword struct. */
6634 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
6635 if (!akl)
6636 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6637 fcn = calloc(1, sizeof(*fcn));
6638 if (!fcn)
6639 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6640
6641 /* Fill fcn. */
6642 len = strlen("<lua.>") + strlen(name) + 1;
6643 fcn->name = calloc(1, len);
6644 if (!fcn->name)
6645 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6646 snprintf((char *)fcn->name, len, "<lua.%s>", name);
6647 fcn->function_ref = ref;
6648
6649 /* List head */
6650 akl->list.n = akl->list.p = NULL;
6651
6652 /* converter keyword. */
6653 len = strlen("lua.") + strlen(name) + 1;
6654 akl->kw[0].kw = calloc(1, len);
6655 if (!akl->kw[0].kw)
6656 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6657
6658 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6659
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01006660 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006661 if (strcmp(env, "tcp") == 0)
6662 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006663 else if (strcmp(env, "http") == 0)
6664 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006665 else
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006666 WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. "
6667 "'tcp' or 'http' are expected."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006668
6669 akl->kw[0].match_pfx = 0;
6670 akl->kw[0].private = fcn;
6671
6672 /* End of array. */
6673 memset(&akl->kw[1], 0, sizeof(*akl->kw));
6674
6675 /* Register this new converter */
6676 service_keywords_register(akl);
6677
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006678 return 0;
6679}
6680
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006681/* This function initialises Lua cli handler. It copies the
6682 * arguments in the Lua stack and create channel IO objects.
6683 */
6684static int hlua_cli_parse_fct(char **args, struct appctx *appctx, void *private)
6685{
6686 struct hlua *hlua;
6687 struct hlua_function *fcn;
6688 int i;
6689 const char *error;
6690
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006691 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006692 appctx->ctx.hlua_cli.fcn = private;
6693
6694 hlua = pool_alloc2(pool2_hlua);
6695 if (!hlua) {
6696 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006697 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006698 }
6699 HLUA_INIT(hlua);
6700 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006701
6702 /* Create task used by signal to wakeup applets.
6703 * We use the same wakeup fonction than the Lua applet_tcp and
6704 * applet_http. It is absolutely compatible.
6705 */
6706 appctx->ctx.hlua_cli.task = task_new();
6707 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01006708 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006709 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006710 }
6711 appctx->ctx.hlua_cli.task->nice = 0;
6712 appctx->ctx.hlua_cli.task->context = appctx;
6713 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
6714
6715 /* Initialises the Lua context */
6716 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
6717 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006718 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006719 }
6720
6721 /* The following Lua calls can fail. */
6722 if (!SET_SAFE_LJMP(hlua->T)) {
6723 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6724 error = lua_tostring(hlua->T, -1);
6725 else
6726 error = "critical error";
6727 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
6728 goto error;
6729 }
6730
6731 /* Check stack available size. */
6732 if (!lua_checkstack(hlua->T, 2)) {
6733 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6734 goto error;
6735 }
6736
6737 /* Restore the function in the stack. */
6738 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
6739
6740 /* Once the arguments parsed, the CLI is like an AppletTCP,
6741 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006742 */
6743 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
6744 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6745 goto error;
6746 }
6747 hlua->nargs = 1;
6748
6749 /* push keywords in the stack. */
6750 for (i = 0; *args[i]; i++) {
6751 /* Check stack available size. */
6752 if (!lua_checkstack(hlua->T, 1)) {
6753 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6754 goto error;
6755 }
6756 lua_pushstring(hlua->T, args[i]);
6757 hlua->nargs++;
6758 }
6759
6760 /* We must initialize the execution timeouts. */
6761 hlua->max_time = hlua_timeout_session;
6762
6763 /* At this point the execution is safe. */
6764 RESET_SAFE_LJMP(hlua->T);
6765
6766 /* It's ok */
6767 return 0;
6768
6769 /* It's not ok. */
6770error:
6771 RESET_SAFE_LJMP(hlua->T);
6772 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006773 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006774 return 1;
6775}
6776
6777static int hlua_cli_io_handler_fct(struct appctx *appctx)
6778{
6779 struct hlua *hlua;
6780 struct stream_interface *si;
6781 struct hlua_function *fcn;
6782
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006783 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006784 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01006785 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006786
6787 /* If the stream is disconnect or closed, ldo nothing. */
6788 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6789 return 1;
6790
6791 /* Execute the function. */
6792 switch (hlua_ctx_resume(hlua, 1)) {
6793
6794 /* finished. */
6795 case HLUA_E_OK:
6796 return 1;
6797
6798 /* yield. */
6799 case HLUA_E_AGAIN:
6800 /* We want write. */
6801 if (HLUA_IS_WAKERESWR(hlua))
6802 si_applet_cant_put(si);
6803 /* Set the timeout. */
6804 if (hlua->wake_time != TICK_ETERNITY)
6805 task_schedule(hlua->task, hlua->wake_time);
6806 return 0;
6807
6808 /* finished with error. */
6809 case HLUA_E_ERRMSG:
6810 /* Display log. */
6811 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
6812 fcn->name, lua_tostring(hlua->T, -1));
6813 lua_pop(hlua->T, 1);
6814 return 1;
6815
6816 case HLUA_E_ERR:
6817 /* Display log. */
6818 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
6819 fcn->name);
6820 return 1;
6821
6822 default:
6823 return 1;
6824 }
6825
6826 return 1;
6827}
6828
6829static void hlua_cli_io_release_fct(struct appctx *appctx)
6830{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006831 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006832 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006833}
6834
6835/* This function is an LUA binding used for registering
6836 * new keywords in the cli. It expects a list of keywords
6837 * which are the "path". It is limited to 5 keywords. A
6838 * description of the command, a function to be executed
6839 * for the parsing and a function for io handlers.
6840 */
6841__LJMP static int hlua_register_cli(lua_State *L)
6842{
6843 struct cli_kw_list *cli_kws;
6844 const char *message;
6845 int ref_io;
6846 int len;
6847 struct hlua_function *fcn;
6848 int index;
6849 int i;
6850
6851 MAY_LJMP(check_args(L, 3, "register_cli"));
6852
6853 /* First argument : an array of maximum 5 keywords. */
6854 if (!lua_istable(L, 1))
6855 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
6856
6857 /* Second argument : string with contextual message. */
6858 message = MAY_LJMP(luaL_checkstring(L, 2));
6859
6860 /* Third and fourth argument : lua function. */
6861 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
6862
6863 /* Allocate and fill the sample fetch keyword struct. */
6864 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
6865 if (!cli_kws)
6866 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6867 fcn = calloc(1, sizeof(*fcn));
6868 if (!fcn)
6869 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6870
6871 /* Fill path. */
6872 index = 0;
6873 lua_pushnil(L);
6874 while(lua_next(L, 1) != 0) {
6875 if (index >= 5)
6876 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
6877 if (lua_type(L, -1) != LUA_TSTRING)
6878 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
6879 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
6880 if (!cli_kws->kw[0].str_kw[index])
6881 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6882 index++;
6883 lua_pop(L, 1);
6884 }
6885
6886 /* Copy help message. */
6887 cli_kws->kw[0].usage = strdup(message);
6888 if (!cli_kws->kw[0].usage)
6889 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6890
6891 /* Fill fcn io handler. */
6892 len = strlen("<lua.cli>") + 1;
6893 for (i = 0; i < index; i++)
6894 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
6895 fcn->name = calloc(1, len);
6896 if (!fcn->name)
6897 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6898 strncat((char *)fcn->name, "<lua.cli", len);
6899 for (i = 0; i < index; i++) {
6900 strncat((char *)fcn->name, ".", len);
6901 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
6902 }
6903 strncat((char *)fcn->name, ">", len);
6904 fcn->function_ref = ref_io;
6905
6906 /* Fill last entries. */
6907 cli_kws->kw[0].private = fcn;
6908 cli_kws->kw[0].parse = hlua_cli_parse_fct;
6909 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
6910 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
6911
6912 /* Register this new converter */
6913 cli_register_kw(cli_kws);
6914
6915 return 0;
6916}
6917
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006918static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
6919 struct proxy *defpx, const char *file, int line,
6920 char **err, unsigned int *timeout)
6921{
6922 const char *error;
6923
6924 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
6925 if (error && *error != '\0') {
6926 memprintf(err, "%s: invalid timeout", args[0]);
6927 return -1;
6928 }
6929 return 0;
6930}
6931
6932static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
6933 struct proxy *defpx, const char *file, int line,
6934 char **err)
6935{
6936 return hlua_read_timeout(args, section_type, curpx, defpx,
6937 file, line, err, &hlua_timeout_session);
6938}
6939
6940static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
6941 struct proxy *defpx, const char *file, int line,
6942 char **err)
6943{
6944 return hlua_read_timeout(args, section_type, curpx, defpx,
6945 file, line, err, &hlua_timeout_task);
6946}
6947
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006948static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
6949 struct proxy *defpx, const char *file, int line,
6950 char **err)
6951{
6952 return hlua_read_timeout(args, section_type, curpx, defpx,
6953 file, line, err, &hlua_timeout_applet);
6954}
6955
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01006956static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
6957 struct proxy *defpx, const char *file, int line,
6958 char **err)
6959{
6960 char *error;
6961
6962 hlua_nb_instruction = strtoll(args[1], &error, 10);
6963 if (*error != '\0') {
6964 memprintf(err, "%s: invalid number", args[0]);
6965 return -1;
6966 }
6967 return 0;
6968}
6969
Willy Tarreau32f61e22015-03-18 17:54:59 +01006970static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
6971 struct proxy *defpx, const char *file, int line,
6972 char **err)
6973{
6974 char *error;
6975
6976 if (*(args[1]) == 0) {
6977 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
6978 return -1;
6979 }
6980 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
6981 if (*error != '\0') {
6982 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
6983 return -1;
6984 }
6985 return 0;
6986}
6987
6988
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01006989/* This function is called by the main configuration key "lua-load". It loads and
6990 * execute an lua file during the parsing of the HAProxy configuration file. It is
6991 * the main lua entry point.
6992 *
6993 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
6994 * occured, otherwise it returns 0.
6995 *
6996 * In some error case, LUA set an error message in top of the stack. This function
6997 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006998 *
6999 * This function can fail with an abort() due to an Lua critical error.
7000 * We are in the configuration parsing process of HAProxy, this abort() is
7001 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007002 */
7003static int hlua_load(char **args, int section_type, struct proxy *curpx,
7004 struct proxy *defpx, const char *file, int line,
7005 char **err)
7006{
7007 int error;
7008
7009 /* Just load and compile the file. */
7010 error = luaL_loadfile(gL.T, args[1]);
7011 if (error) {
7012 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
7013 lua_pop(gL.T, 1);
7014 return -1;
7015 }
7016
7017 /* If no syntax error where detected, execute the code. */
7018 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7019 switch (error) {
7020 case LUA_OK:
7021 break;
7022 case LUA_ERRRUN:
7023 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
7024 lua_pop(gL.T, 1);
7025 return -1;
7026 case LUA_ERRMEM:
7027 memprintf(err, "lua out of memory error\n");
7028 return -1;
7029 case LUA_ERRERR:
7030 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
7031 lua_pop(gL.T, 1);
7032 return -1;
7033 case LUA_ERRGCMM:
7034 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
7035 lua_pop(gL.T, 1);
7036 return -1;
7037 default:
7038 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
7039 lua_pop(gL.T, 1);
7040 return -1;
7041 }
7042
7043 return 0;
7044}
7045
7046/* configuration keywords declaration */
7047static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007048 { CFG_GLOBAL, "lua-load", hlua_load },
7049 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7050 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007051 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007052 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007053 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007054 { 0, NULL, NULL },
7055}};
7056
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007057/* This function can fail with an abort() due to an Lua critical error.
7058 * We are in the initialisation process of HAProxy, this abort() is
7059 * tolerated.
7060 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007061int hlua_post_init()
7062{
7063 struct hlua_init_function *init;
7064 const char *msg;
7065 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007066 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007067
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007068 /* Call post initialisation function in safe environement. */
7069 if (!SET_SAFE_LJMP(gL.T)) {
7070 if (lua_type(gL.T, -1) == LUA_TSTRING)
7071 error = lua_tostring(gL.T, -1);
7072 else
7073 error = "critical error";
7074 fprintf(stderr, "Lua post-init: %s.\n", error);
7075 exit(1);
7076 }
7077 hlua_fcn_post_init(gL.T);
7078 RESET_SAFE_LJMP(gL.T);
7079
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007080 list_for_each_entry(init, &hlua_init_functions, l) {
7081 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7082 ret = hlua_ctx_resume(&gL, 0);
7083 switch (ret) {
7084 case HLUA_E_OK:
7085 lua_pop(gL.T, -1);
7086 return 1;
7087 case HLUA_E_AGAIN:
7088 Alert("lua init: yield not allowed.\n");
7089 return 0;
7090 case HLUA_E_ERRMSG:
7091 msg = lua_tostring(gL.T, -1);
7092 Alert("lua init: %s.\n", msg);
7093 return 0;
7094 case HLUA_E_ERR:
7095 default:
7096 Alert("lua init: unknown runtime error.\n");
7097 return 0;
7098 }
7099 }
7100 return 1;
7101}
7102
Willy Tarreau32f61e22015-03-18 17:54:59 +01007103/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7104 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7105 * is the previously allocated size or the kind of object in case of a new
7106 * allocation. <nsize> is the requested new size.
7107 */
7108static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7109{
7110 struct hlua_mem_allocator *zone = ud;
7111
7112 if (nsize == 0) {
7113 /* it's a free */
7114 if (ptr)
7115 zone->allocated -= osize;
7116 free(ptr);
7117 return NULL;
7118 }
7119
7120 if (!ptr) {
7121 /* it's a new allocation */
7122 if (zone->limit && zone->allocated + nsize > zone->limit)
7123 return NULL;
7124
7125 ptr = malloc(nsize);
7126 if (ptr)
7127 zone->allocated += nsize;
7128 return ptr;
7129 }
7130
7131 /* it's a realloc */
7132 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7133 return NULL;
7134
7135 ptr = realloc(ptr, nsize);
7136 if (ptr)
7137 zone->allocated += nsize - osize;
7138 return ptr;
7139}
7140
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007141/* Ithis function can fail with an abort() due to an Lua critical error.
7142 * We are in the initialisation process of HAProxy, this abort() is
7143 * tolerated.
7144 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007145void hlua_init(void)
7146{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007147 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007148 int idx;
7149 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007150 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007151 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007152 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007153#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007154 struct srv_kw *kw;
7155 int tmp_error;
7156 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007157 char *args[] = { /* SSL client configuration. */
7158 "ssl",
7159 "verify",
7160 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007161 NULL
7162 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007163#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007164
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007165 /* Initialise struct hlua and com signals pool */
7166 pool2_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007167 pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED);
7168
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007169 /* Register configuration keywords. */
7170 cfg_register_keywords(&cfg_kws);
7171
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007172 /* Init main lua stack. */
7173 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007174 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007175 LIST_INIT(&gL.com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007176 gL.T = luaL_newstate();
7177 hlua_sethlua(&gL);
7178 gL.Tref = LUA_REFNIL;
7179 gL.task = NULL;
7180
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007181 /* From this point, until the end of the initialisation fucntion,
7182 * the Lua function can fail with an abort. We are in the initialisation
7183 * process of HAProxy, this abort() is tolerated.
7184 */
7185
Willy Tarreau32f61e22015-03-18 17:54:59 +01007186 /* change the memory allocators to track memory usage */
7187 lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator);
7188
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007189 /* Initialise lua. */
7190 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007191
Thierry Fournier75933d42016-01-21 09:30:18 +01007192 /* Set safe environment for the initialisation. */
7193 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007194 if (lua_type(gL.T, -1) == LUA_TSTRING)
7195 error_msg = lua_tostring(gL.T, -1);
7196 else
7197 error_msg = "critical error";
7198 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007199 exit(1);
7200 }
7201
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007202 /*
7203 *
7204 * Create "core" object.
7205 *
7206 */
7207
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007208 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007209 lua_newtable(gL.T);
7210
7211 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007212 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007213 hlua_class_const_int(gL.T, log_levels[i], i);
7214
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007215 /* Register special functions. */
7216 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007217 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007218 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007219 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007220 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007221 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007222 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007223 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007224 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007225 hlua_class_function(gL.T, "sleep", hlua_sleep);
7226 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007227 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7228 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7229 hlua_class_function(gL.T, "set_map", hlua_set_map);
7230 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007231 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007232 hlua_class_function(gL.T, "log", hlua_log);
7233 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7234 hlua_class_function(gL.T, "Info", hlua_log_info);
7235 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7236 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007237 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007238 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007239
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007240 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007241
7242 /*
7243 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007244 * Register class Map
7245 *
7246 */
7247
7248 /* This table entry is the object "Map" base. */
7249 lua_newtable(gL.T);
7250
7251 /* register pattern types. */
7252 for (i=0; i<PAT_MATCH_NUM; i++)
7253 hlua_class_const_int(gL.T, pat_match_names[i], i);
7254
7255 /* register constructor. */
7256 hlua_class_function(gL.T, "new", hlua_map_new);
7257
7258 /* Create and fill the metatable. */
7259 lua_newtable(gL.T);
7260
7261 /* Create and fille the __index entry. */
7262 lua_pushstring(gL.T, "__index");
7263 lua_newtable(gL.T);
7264
7265 /* Register . */
7266 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7267 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7268
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007269 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007270
Thierry Fournier45e78d72016-02-19 18:34:46 +01007271 /* Register previous table in the registry with reference and named entry.
7272 * The function hlua_register_metatable() pops the stack, so we
7273 * previously create a copy of the table.
7274 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007275 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007276 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007277
7278 /* Assign the metatable to the mai Map object. */
7279 lua_setmetatable(gL.T, -2);
7280
7281 /* Set a name to the table. */
7282 lua_setglobal(gL.T, "Map");
7283
7284 /*
7285 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007286 * Register class Channel
7287 *
7288 */
7289
7290 /* Create and fill the metatable. */
7291 lua_newtable(gL.T);
7292
7293 /* Create and fille the __index entry. */
7294 lua_pushstring(gL.T, "__index");
7295 lua_newtable(gL.T);
7296
7297 /* Register . */
7298 hlua_class_function(gL.T, "get", hlua_channel_get);
7299 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7300 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7301 hlua_class_function(gL.T, "set", hlua_channel_set);
7302 hlua_class_function(gL.T, "append", hlua_channel_append);
7303 hlua_class_function(gL.T, "send", hlua_channel_send);
7304 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7305 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7306 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007307 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007308
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007309 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007310
7311 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007312 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007313
7314 /*
7315 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007316 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007317 *
7318 */
7319
7320 /* Create and fill the metatable. */
7321 lua_newtable(gL.T);
7322
7323 /* Create and fille the __index entry. */
7324 lua_pushstring(gL.T, "__index");
7325 lua_newtable(gL.T);
7326
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007327 /* Browse existing fetches and create the associated
7328 * object method.
7329 */
7330 sf = NULL;
7331 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7332
7333 /* Dont register the keywork if the arguments check function are
7334 * not safe during the runtime.
7335 */
7336 if ((sf->val_args != NULL) &&
7337 (sf->val_args != val_payload_lv) &&
7338 (sf->val_args != val_hdr))
7339 continue;
7340
7341 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7342 * by an underscore.
7343 */
7344 strncpy(trash.str, sf->kw, trash.size);
7345 trash.str[trash.size - 1] = '\0';
7346 for (p = trash.str; *p; p++)
7347 if (*p == '.' || *p == '-' || *p == '+')
7348 *p = '_';
7349
7350 /* Register the function. */
7351 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007352 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007353 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007354 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007355 }
7356
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007357 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007358
7359 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007360 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007361
7362 /*
7363 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007364 * Register class Converters
7365 *
7366 */
7367
7368 /* Create and fill the metatable. */
7369 lua_newtable(gL.T);
7370
7371 /* Create and fill the __index entry. */
7372 lua_pushstring(gL.T, "__index");
7373 lua_newtable(gL.T);
7374
7375 /* Browse existing converters and create the associated
7376 * object method.
7377 */
7378 sc = NULL;
7379 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7380 /* Dont register the keywork if the arguments check function are
7381 * not safe during the runtime.
7382 */
7383 if (sc->val_args != NULL)
7384 continue;
7385
7386 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7387 * by an underscore.
7388 */
7389 strncpy(trash.str, sc->kw, trash.size);
7390 trash.str[trash.size - 1] = '\0';
7391 for (p = trash.str; *p; p++)
7392 if (*p == '.' || *p == '-' || *p == '+')
7393 *p = '_';
7394
7395 /* Register the function. */
7396 lua_pushstring(gL.T, trash.str);
7397 lua_pushlightuserdata(gL.T, sc);
7398 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007399 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007400 }
7401
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007402 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007403
7404 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007405 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007406
7407 /*
7408 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007409 * Register class HTTP
7410 *
7411 */
7412
7413 /* Create and fill the metatable. */
7414 lua_newtable(gL.T);
7415
7416 /* Create and fille the __index entry. */
7417 lua_pushstring(gL.T, "__index");
7418 lua_newtable(gL.T);
7419
7420 /* Register Lua functions. */
7421 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7422 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7423 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7424 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7425 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7426 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7427 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7428 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7429 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7430 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7431
7432 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7433 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7434 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7435 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7436 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7437 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007438 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007439
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007440 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007441
7442 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007443 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007444
7445 /*
7446 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007447 * Register class AppletTCP
7448 *
7449 */
7450
7451 /* Create and fill the metatable. */
7452 lua_newtable(gL.T);
7453
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007454 /* Create and fille the __index entry. */
7455 lua_pushstring(gL.T, "__index");
7456 lua_newtable(gL.T);
7457
7458 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007459 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7460 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7461 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7462 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7463 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007464 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7465 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7466 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007467
7468 lua_settable(gL.T, -3);
7469
7470 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007471 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007472
7473 /*
7474 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007475 * Register class AppletHTTP
7476 *
7477 */
7478
7479 /* Create and fill the metatable. */
7480 lua_newtable(gL.T);
7481
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007482 /* Create and fille the __index entry. */
7483 lua_pushstring(gL.T, "__index");
7484 lua_newtable(gL.T);
7485
7486 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007487 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7488 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007489 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7490 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7491 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007492 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7493 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7494 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7495 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7496 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7497 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7498
7499 lua_settable(gL.T, -3);
7500
7501 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007502 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007503
7504 /*
7505 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007506 * Register class TXN
7507 *
7508 */
7509
7510 /* Create and fill the metatable. */
7511 lua_newtable(gL.T);
7512
7513 /* Create and fille the __index entry. */
7514 lua_pushstring(gL.T, "__index");
7515 lua_newtable(gL.T);
7516
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007517 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01007518 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7519 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007520 hlua_class_function(gL.T, "set_var", hlua_set_var);
Christopher Faulet85d79c92016-11-09 16:54:56 +01007521 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007522 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007523 hlua_class_function(gL.T, "done", hlua_txn_done);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007524 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
7525 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7526 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007527 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7528 hlua_class_function(gL.T, "log", hlua_txn_log);
7529 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7530 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7531 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7532 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007533
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007534 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007535
7536 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007537 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007538
7539 /*
7540 *
7541 * Register class Socket
7542 *
7543 */
7544
7545 /* Create and fill the metatable. */
7546 lua_newtable(gL.T);
7547
7548 /* Create and fille the __index entry. */
7549 lua_pushstring(gL.T, "__index");
7550 lua_newtable(gL.T);
7551
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007552#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007553 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007554#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007555 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7556 hlua_class_function(gL.T, "send", hlua_socket_send);
7557 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7558 hlua_class_function(gL.T, "close", hlua_socket_close);
7559 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7560 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7561 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7562 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7563
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007564 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007565
7566 /* Register the garbage collector entry. */
7567 lua_pushstring(gL.T, "__gc");
7568 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007569 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007570
7571 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007572 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007573
7574 /* Proxy and server configuration initialisation. */
7575 memset(&socket_proxy, 0, sizeof(socket_proxy));
7576 init_new_proxy(&socket_proxy);
7577 socket_proxy.parent = NULL;
7578 socket_proxy.last_change = now.tv_sec;
7579 socket_proxy.id = "LUA-SOCKET";
7580 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
7581 socket_proxy.maxconn = 0;
7582 socket_proxy.accept = NULL;
7583 socket_proxy.options2 |= PR_O2_INDEPSTR;
7584 socket_proxy.srv = NULL;
7585 socket_proxy.conn_retries = 0;
7586 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
7587
7588 /* Init TCP server: unchanged parameters */
7589 memset(&socket_tcp, 0, sizeof(socket_tcp));
7590 socket_tcp.next = NULL;
7591 socket_tcp.proxy = &socket_proxy;
7592 socket_tcp.obj_type = OBJ_TYPE_SERVER;
7593 LIST_INIT(&socket_tcp.actconns);
7594 LIST_INIT(&socket_tcp.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02007595 LIST_INIT(&socket_tcp.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02007596 LIST_INIT(&socket_tcp.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02007597 LIST_INIT(&socket_tcp.safe_conns);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007598 socket_tcp.state = SRV_ST_RUNNING; /* early server setup */
7599 socket_tcp.last_change = 0;
7600 socket_tcp.id = "LUA-TCP-CONN";
7601 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7602 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7603 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
7604
7605 /* XXX: Copy default parameter from default server,
7606 * but the default server is not initialized.
7607 */
7608 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
7609 socket_tcp.minconn = socket_proxy.defsrv.minconn;
7610 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
7611 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
7612 socket_tcp.onerror = socket_proxy.defsrv.onerror;
7613 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
7614 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
7615 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
7616 socket_tcp.uweight = socket_proxy.defsrv.iweight;
7617 socket_tcp.iweight = socket_proxy.defsrv.iweight;
7618
7619 socket_tcp.check.status = HCHK_STATUS_INI;
7620 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
7621 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
7622 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
7623 socket_tcp.check.server = &socket_tcp;
7624
7625 socket_tcp.agent.status = HCHK_STATUS_INI;
7626 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
7627 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
7628 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
7629 socket_tcp.agent.server = &socket_tcp;
7630
Willy Tarreaua261e9b2016-12-22 20:44:00 +01007631 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007632
7633#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007634 /* Init TCP server: unchanged parameters */
7635 memset(&socket_ssl, 0, sizeof(socket_ssl));
7636 socket_ssl.next = NULL;
7637 socket_ssl.proxy = &socket_proxy;
7638 socket_ssl.obj_type = OBJ_TYPE_SERVER;
7639 LIST_INIT(&socket_ssl.actconns);
7640 LIST_INIT(&socket_ssl.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02007641 LIST_INIT(&socket_ssl.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02007642 LIST_INIT(&socket_ssl.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02007643 LIST_INIT(&socket_ssl.safe_conns);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007644 socket_ssl.state = SRV_ST_RUNNING; /* early server setup */
7645 socket_ssl.last_change = 0;
7646 socket_ssl.id = "LUA-SSL-CONN";
7647 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7648 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7649 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
7650
7651 /* XXX: Copy default parameter from default server,
7652 * but the default server is not initialized.
7653 */
7654 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
7655 socket_ssl.minconn = socket_proxy.defsrv.minconn;
7656 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
7657 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
7658 socket_ssl.onerror = socket_proxy.defsrv.onerror;
7659 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
7660 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
7661 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
7662 socket_ssl.uweight = socket_proxy.defsrv.iweight;
7663 socket_ssl.iweight = socket_proxy.defsrv.iweight;
7664
7665 socket_ssl.check.status = HCHK_STATUS_INI;
7666 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
7667 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
7668 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
7669 socket_ssl.check.server = &socket_ssl;
7670
7671 socket_ssl.agent.status = HCHK_STATUS_INI;
7672 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
7673 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
7674 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
7675 socket_ssl.agent.server = &socket_ssl;
7676
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007677 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01007678 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007679
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007680 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007681 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
7682 /*
7683 *
7684 * If the keyword is not known, we can search in the registered
7685 * server keywords. This is usefull to configure special SSL
7686 * features like client certificates and ssl_verify.
7687 *
7688 */
7689 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
7690 if (tmp_error != 0) {
7691 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
7692 abort(); /* This must be never arrives because the command line
7693 not editable by the user. */
7694 }
7695 idx += kw->skip;
7696 }
7697 }
7698
7699 /* Initialize SSL server. */
Willy Tarreau03209342016-12-22 17:08:28 +01007700 ssl_sock_prepare_srv_ctx(&socket_ssl);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007701#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01007702
7703 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007704}
Willy Tarreaubb57d942016-12-21 19:04:56 +01007705
7706__attribute__((constructor))
7707static void __hlua_init(void)
7708{
7709 char *ptr = NULL;
7710 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
7711 hap_register_build_opts(ptr, 1);
7712}