blob: c343a7b39e696734d0ce27e0219da1db9f79e5c6 [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/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010052#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010053#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020054#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010055
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010056/* Lua uses longjmp to perform yield or throwing errors. This
57 * macro is used only for identifying the function that can
58 * not return because a longjmp is executed.
59 * __LJMP marks a prototype of hlua file that can use longjmp.
60 * WILL_LJMP() marks an lua function that will use longjmp.
61 * MAY_LJMP() marks an lua function that may use longjmp.
62 */
63#define __LJMP
64#define WILL_LJMP(func) func
65#define MAY_LJMP(func) func
66
Thierry FOURNIERbabae282015-09-17 11:36:37 +020067/* This couple of function executes securely some Lua calls outside of
68 * the lua runtime environment. Each Lua call can return a longjmp
69 * if it encounter a memory error.
70 *
71 * Lua documentation extract:
72 *
73 * If an error happens outside any protected environment, Lua calls
74 * a panic function (see lua_atpanic) and then calls abort, thus
75 * exiting the host application. Your panic function can avoid this
76 * exit by never returning (e.g., doing a long jump to your own
77 * recovery point outside Lua).
78 *
79 * The panic function runs as if it were a message handler (see
80 * §2.3); in particular, the error message is at the top of the
81 * stack. However, there is no guarantee about stack space. To push
82 * anything on the stack, the panic function must first check the
83 * available space (see §4.2).
84 *
85 * We must check all the Lua entry point. This includes:
86 * - The include/proto/hlua.h exported functions
87 * - the task wrapper function
88 * - The action wrapper function
89 * - The converters wrapper function
90 * - The sample-fetch wrapper functions
91 *
92 * It is tolerated that the initilisation function returns an abort.
93 * Before each Lua abort, an error message is writed on stderr.
94 *
95 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
96 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
97 * because they must be exists in the program stack when the longjmp
98 * is called.
99 */
100jmp_buf safe_ljmp_env;
101static int hlua_panic_safe(lua_State *L) { return 0; }
102static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
103
104#define SET_SAFE_LJMP(__L) \
105 ({ \
106 int ret; \
107 if (setjmp(safe_ljmp_env) != 0) { \
108 lua_atpanic(__L, hlua_panic_safe); \
109 ret = 0; \
110 } else { \
111 lua_atpanic(__L, hlua_panic_ljmp); \
112 ret = 1; \
113 } \
114 ret; \
115 })
116
117/* If we are the last function catching Lua errors, we
118 * must reset the panic function.
119 */
120#define RESET_SAFE_LJMP(__L) \
121 do { \
122 lua_atpanic(__L, hlua_panic_safe); \
123 } while(0)
124
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200125/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200126#define APPLET_DONE 0x01 /* applet processing is done. */
127#define APPLET_100C 0x02 /* 100 continue expected. */
128#define APPLET_HDR_SENT 0x04 /* Response header sent. */
129#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
130#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100131#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200132
133#define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n"
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200134
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100135/* The main Lua execution context. */
136struct hlua gL;
137
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100138/* This is the memory pool containing struct lua for applets
139 * (including cli).
140 */
141struct pool_head *pool2_hlua;
142
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100143/* This is the memory pool containing all the signal structs. These
144 * struct are used to store each requiered signal between two tasks.
145 */
146struct pool_head *pool2_hlua_com;
147
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100148/* Used for Socket connection. */
149static struct proxy socket_proxy;
150static struct server socket_tcp;
151#ifdef USE_OPENSSL
152static struct server socket_ssl;
153#endif
154
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100155/* List head of the function called at the initialisation time. */
156struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
157
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100158/* The following variables contains the reference of the different
159 * Lua classes. These references are useful for identify metadata
160 * associated with an object.
161 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100162static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100163static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100164static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100165static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100166static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100167static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200168static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200169static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200170static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100171
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100172/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200173 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100174 * short timeout. Lua linked with tasks doesn't have a timeout
175 * because a task may remain alive during all the haproxy execution.
176 */
177static unsigned int hlua_timeout_session = 4000; /* session timeout. */
178static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200179static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100180
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100181/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
182 * it is used for preventing infinite loops.
183 *
184 * I test the scheer with an infinite loop containing one incrementation
185 * and one test. I run this loop between 10 seconds, I raise a ceil of
186 * 710M loops from one interrupt each 9000 instructions, so I fix the value
187 * to one interrupt each 10 000 instructions.
188 *
189 * configured | Number of
190 * instructions | loops executed
191 * between two | in milions
192 * forced yields |
193 * ---------------+---------------
194 * 10 | 160
195 * 500 | 670
196 * 1000 | 680
197 * 5000 | 700
198 * 7000 | 700
199 * 8000 | 700
200 * 9000 | 710 <- ceil
201 * 10000 | 710
202 * 100000 | 710
203 * 1000000 | 710
204 *
205 */
206static unsigned int hlua_nb_instruction = 10000;
207
Willy Tarreau32f61e22015-03-18 17:54:59 +0100208/* Descriptor for the memory allocation state. If limit is not null, it will
209 * be enforced on any memory allocation.
210 */
211struct hlua_mem_allocator {
212 size_t allocated;
213 size_t limit;
214};
215
216static struct hlua_mem_allocator hlua_global_allocator;
217
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200218static const char error_500[] =
219 "HTTP/1.0 500 Server Error\r\n"
220 "Cache-Control: no-cache\r\n"
221 "Connection: close\r\n"
222 "Content-Type: text/html\r\n"
223 "\r\n"
224 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n";
225
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100226/* These functions converts types between HAProxy internal args or
227 * sample and LUA types. Another function permits to check if the
228 * LUA stack contains arguments according with an required ARG_T
229 * format.
230 */
231static int hlua_arg2lua(lua_State *L, const struct arg *arg);
232static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100233__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100234 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100235static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100236static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100237static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
238
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200239__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
240
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200241#define SEND_ERR(__be, __fmt, __args...) \
242 do { \
243 send_log(__be, LOG_ERR, __fmt, ## __args); \
244 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
245 Alert(__fmt, ## __args); \
246 } while (0)
247
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100248/* Used to check an Lua function type in the stack. It creates and
249 * returns a reference of the function. This function throws an
250 * error if the rgument is not a "function".
251 */
252__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
253{
254 if (!lua_isfunction(L, argno)) {
255 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1));
256 WILL_LJMP(luaL_argerror(L, argno, msg));
257 }
258 lua_pushvalue(L, argno);
259 return luaL_ref(L, LUA_REGISTRYINDEX);
260}
261
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200262/* Return the string that is of the top of the stack. */
263const char *hlua_get_top_error_string(lua_State *L)
264{
265 if (lua_gettop(L) < 1)
266 return "unknown error";
267 if (lua_type(L, -1) != LUA_TSTRING)
268 return "unknown error";
269 return lua_tostring(L, -1);
270}
271
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100272/* This function check the number of arguments available in the
273 * stack. If the number of arguments available is not the same
274 * then <nb> an error is throwed.
275 */
276__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
277{
278 if (lua_gettop(L) == nb)
279 return;
280 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
281}
282
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100283/* This fucntion push an error string prefixed by the file name
284 * and the line number where the error is encountered.
285 */
286static int hlua_pusherror(lua_State *L, const char *fmt, ...)
287{
288 va_list argp;
289 va_start(argp, fmt);
290 luaL_where(L, 1);
291 lua_pushvfstring(L, fmt, argp);
292 va_end(argp);
293 lua_concat(L, 2);
294 return 1;
295}
296
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100297/* This function register a new signal. "lua" is the current lua
298 * execution context. It contains a pointer to the associated task.
299 * "link" is a list head attached to an other task that must be wake
300 * the lua task if an event occurs. This is useful with external
301 * events like TCP I/O or sleep functions. This funcion allocate
302 * memory for the signal.
303 */
Thierry FOURNIER847ca662016-12-16 13:07:22 +0100304static struct hlua_com *hlua_com_new(struct list *purge, struct list *event, struct task *wakeup)
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100305{
306 struct hlua_com *com = pool_alloc2(pool2_hlua_com);
307 if (!com)
Thierry FOURNIER847ca662016-12-16 13:07:22 +0100308 return NULL;
309 LIST_ADDQ(purge, &com->purge_me);
310 LIST_ADDQ(event, &com->wake_me);
311 com->task = wakeup;
312 return com;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100313}
314
315/* This function purge all the pending signals when the LUA execution
316 * is finished. This prevent than a coprocess try to wake a deleted
317 * task. This function remove the memory associated to the signal.
318 */
Thierry FOURNIER847ca662016-12-16 13:07:22 +0100319static void hlua_com_purge(struct list *purge)
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100320{
321 struct hlua_com *com, *back;
322
323 /* Delete all pending communication signals. */
Thierry FOURNIER847ca662016-12-16 13:07:22 +0100324 list_for_each_entry_safe(com, back, purge, purge_me) {
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100325 LIST_DEL(&com->purge_me);
326 LIST_DEL(&com->wake_me);
327 pool_free2(pool2_hlua_com, com);
328 }
329}
330
331/* This function sends signals. It wakes all the tasks attached
332 * to a list head, and remove the signal, and free the used
333 * memory.
334 */
335static void hlua_com_wake(struct list *wake)
336{
337 struct hlua_com *com, *back;
338
339 /* Wake task and delete all pending communication signals. */
340 list_for_each_entry_safe(com, back, wake, wake_me) {
341 LIST_DEL(&com->purge_me);
342 LIST_DEL(&com->wake_me);
343 task_wakeup(com->task, TASK_WOKEN_MSG);
344 pool_free2(pool2_hlua_com, com);
345 }
346}
347
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100348/* This functions is used with sample fetch and converters. It
349 * converts the HAProxy configuration argument in a lua stack
350 * values.
351 *
352 * It takes an array of "arg", and each entry of the array is
353 * converted and pushed in the LUA stack.
354 */
355static int hlua_arg2lua(lua_State *L, const struct arg *arg)
356{
357 switch (arg->type) {
358 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100359 case ARGT_TIME:
360 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100361 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100362 break;
363
364 case ARGT_STR:
365 lua_pushlstring(L, arg->data.str.str, arg->data.str.len);
366 break;
367
368 case ARGT_IPV4:
369 case ARGT_IPV6:
370 case ARGT_MSK4:
371 case ARGT_MSK6:
372 case ARGT_FE:
373 case ARGT_BE:
374 case ARGT_TAB:
375 case ARGT_SRV:
376 case ARGT_USR:
377 case ARGT_MAP:
378 default:
379 lua_pushnil(L);
380 break;
381 }
382 return 1;
383}
384
385/* This function take one entrie in an LUA stack at the index "ud",
386 * and try to convert it in an HAProxy argument entry. This is useful
387 * with sample fetch wrappers. The input arguments are gived to the
388 * lua wrapper and converted as arg list by thi function.
389 */
390static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
391{
392 switch (lua_type(L, ud)) {
393
394 case LUA_TNUMBER:
395 case LUA_TBOOLEAN:
396 arg->type = ARGT_SINT;
397 arg->data.sint = lua_tointeger(L, ud);
398 break;
399
400 case LUA_TSTRING:
401 arg->type = ARGT_STR;
402 arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len);
403 break;
404
405 case LUA_TUSERDATA:
406 case LUA_TNIL:
407 case LUA_TTABLE:
408 case LUA_TFUNCTION:
409 case LUA_TTHREAD:
410 case LUA_TLIGHTUSERDATA:
411 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200412 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100413 break;
414 }
415 return 1;
416}
417
418/* the following functions are used to convert a struct sample
419 * in Lua type. This useful to convert the return of the
420 * fetchs or converters.
421 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100422static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100423{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200424 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100425 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100426 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200427 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100428 break;
429
430 case SMP_T_BIN:
431 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200432 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100433 break;
434
435 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200436 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100437 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
438 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
439 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
440 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
441 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
442 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
443 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
444 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
445 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200446 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447 break;
448 default:
449 lua_pushnil(L);
450 break;
451 }
452 break;
453
454 case SMP_T_IPV4:
455 case SMP_T_IPV6:
456 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200457 if (sample_casts[smp->data.type][SMP_T_STR] &&
458 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200459 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100460 else
461 lua_pushnil(L);
462 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100463 default:
464 lua_pushnil(L);
465 break;
466 }
467 return 1;
468}
469
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100470/* the following functions are used to convert a struct sample
471 * in Lua strings. This is useful to convert the return of the
472 * fetchs or converters.
473 */
474static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
475{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200476 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100477
478 case SMP_T_BIN:
479 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200480 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100481 break;
482
483 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200484 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100485 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
486 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
487 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
488 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
489 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
490 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
491 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
492 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
493 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200494 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100495 break;
496 default:
497 lua_pushstring(L, "");
498 break;
499 }
500 break;
501
502 case SMP_T_SINT:
503 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100504 case SMP_T_IPV4:
505 case SMP_T_IPV6:
506 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200507 if (sample_casts[smp->data.type][SMP_T_STR] &&
508 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200509 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100510 else
511 lua_pushstring(L, "");
512 break;
513 default:
514 lua_pushstring(L, "");
515 break;
516 }
517 return 1;
518}
519
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100520/* the following functions are used to convert an Lua type in a
521 * struct sample. This is useful to provide data from a converter
522 * to the LUA code.
523 */
524static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
525{
526 switch (lua_type(L, ud)) {
527
528 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200529 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200530 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100531 break;
532
533
534 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200535 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200536 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100537 break;
538
539 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200540 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100541 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200542 smp->data.u.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100543 break;
544
545 case LUA_TUSERDATA:
546 case LUA_TNIL:
547 case LUA_TTABLE:
548 case LUA_TFUNCTION:
549 case LUA_TTHREAD:
550 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200551 case LUA_TNONE:
552 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200553 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200554 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100555 break;
556 }
557 return 1;
558}
559
560/* This function check the "argp" builded by another conversion function
561 * is in accord with the expected argp defined by the "mask". The fucntion
562 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100563 *
564 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
565 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100566 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100567__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100568 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100569{
570 int min_arg;
571 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100572 struct proxy *px;
573 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100574
575 idx = 0;
576 min_arg = ARGM(mask);
577 mask >>= ARGM_BITS;
578
579 while (1) {
580
581 /* Check oversize. */
582 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100583 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100584 }
585
586 /* Check for mandatory arguments. */
587 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100588 if (idx < min_arg) {
589
590 /* If miss other argument than the first one, we return an error. */
591 if (idx > 0)
592 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
593
594 /* If first argument have a certain type, some default values
595 * may be used. See the function smp_resolve_args().
596 */
597 switch (mask & ARGT_MASK) {
598
599 case ARGT_FE:
600 if (!(p->cap & PR_CAP_FE))
601 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
602 argp[idx].data.prx = p;
603 argp[idx].type = ARGT_FE;
604 argp[idx+1].type = ARGT_STOP;
605 break;
606
607 case ARGT_BE:
608 if (!(p->cap & PR_CAP_BE))
609 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
610 argp[idx].data.prx = p;
611 argp[idx].type = ARGT_BE;
612 argp[idx+1].type = ARGT_STOP;
613 break;
614
615 case ARGT_TAB:
616 argp[idx].data.prx = p;
617 argp[idx].type = ARGT_TAB;
618 argp[idx+1].type = ARGT_STOP;
619 break;
620
621 default:
622 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
623 break;
624 }
625 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100626 return 0;
627 }
628
629 /* Check for exceed the number of requiered argument. */
630 if ((mask & ARGT_MASK) == ARGT_STOP &&
631 argp[idx].type != ARGT_STOP) {
632 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
633 }
634
635 if ((mask & ARGT_MASK) == ARGT_STOP &&
636 argp[idx].type == ARGT_STOP) {
637 return 0;
638 }
639
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100640 /* Convert some argument types. */
641 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100642 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100643 if (argp[idx].type != ARGT_SINT)
644 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
645 argp[idx].type = ARGT_SINT;
646 break;
647
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100648 case ARGT_TIME:
649 if (argp[idx].type != ARGT_SINT)
650 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200651 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100652 break;
653
654 case ARGT_SIZE:
655 if (argp[idx].type != ARGT_SINT)
656 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200657 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100658 break;
659
660 case ARGT_FE:
661 if (argp[idx].type != ARGT_STR)
662 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
663 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
664 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200665 argp[idx].data.prx = proxy_fe_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100666 if (!argp[idx].data.prx)
667 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
668 argp[idx].type = ARGT_FE;
669 break;
670
671 case ARGT_BE:
672 if (argp[idx].type != ARGT_STR)
673 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
674 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
675 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200676 argp[idx].data.prx = proxy_be_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100677 if (!argp[idx].data.prx)
678 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
679 argp[idx].type = ARGT_BE;
680 break;
681
682 case ARGT_TAB:
683 if (argp[idx].type != ARGT_STR)
684 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
685 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
686 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200687 argp[idx].data.prx = proxy_tbl_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100688 if (!argp[idx].data.prx)
689 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
690 argp[idx].type = ARGT_TAB;
691 break;
692
693 case ARGT_SRV:
694 if (argp[idx].type != ARGT_STR)
695 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
696 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
697 trash.str[argp[idx].data.str.len] = 0;
698 sname = strrchr(trash.str, '/');
699 if (sname) {
700 *sname++ = '\0';
701 pname = trash.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200702 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100703 if (!px)
704 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
705 }
706 else {
707 sname = trash.str;
708 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100709 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100710 argp[idx].data.srv = findserver(px, sname);
711 if (!argp[idx].data.srv)
712 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
713 argp[idx].type = ARGT_SRV;
714 break;
715
716 case ARGT_IPV4:
717 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
718 trash.str[argp[idx].data.str.len] = 0;
719 if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4))
720 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
721 argp[idx].type = ARGT_IPV4;
722 break;
723
724 case ARGT_MSK4:
725 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
726 trash.str[argp[idx].data.str.len] = 0;
727 if (!str2mask(trash.str, &argp[idx].data.ipv4))
728 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
729 argp[idx].type = ARGT_MSK4;
730 break;
731
732 case ARGT_IPV6:
733 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
734 trash.str[argp[idx].data.str.len] = 0;
735 if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6))
736 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
737 argp[idx].type = ARGT_IPV6;
738 break;
739
740 case ARGT_MSK6:
741 case ARGT_MAP:
742 case ARGT_REG:
743 case ARGT_USR:
744 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100745 break;
746 }
747
748 /* Check for type of argument. */
749 if ((mask & ARGT_MASK) != argp[idx].type) {
750 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
751 arg_type_names[(mask & ARGT_MASK)],
752 arg_type_names[argp[idx].type & ARGT_MASK]);
753 WILL_LJMP(luaL_argerror(L, first + idx, msg));
754 }
755
756 /* Next argument. */
757 mask >>= ARGT_BITS;
758 idx++;
759 }
760}
761
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100762/*
763 * The following functions are used to make correspondance between the the
764 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100765 *
766 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100767 * - hlua_sethlua : create the association between hlua context and lua_state.
768 */
769static inline struct hlua *hlua_gethlua(lua_State *L)
770{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100771 struct hlua **hlua = lua_getextraspace(L);
772 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100773}
774static inline void hlua_sethlua(struct hlua *hlua)
775{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100776 struct hlua **hlua_store = lua_getextraspace(hlua->T);
777 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100778}
779
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100780/* This function is used to send logs. It try to send on screen (stderr)
781 * and on the default syslog server.
782 */
783static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
784{
785 struct tm tm;
786 char *p;
787
788 /* Cleanup the log message. */
789 p = trash.str;
790 for (; *msg != '\0'; msg++, p++) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200791 if (p >= trash.str + trash.size - 1) {
792 /* Break the message if exceed the buffer size. */
793 *(p-4) = ' ';
794 *(p-3) = '.';
795 *(p-2) = '.';
796 *(p-1) = '.';
797 break;
798 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100799 if (isprint(*msg))
800 *p = *msg;
801 else
802 *p = '.';
803 }
804 *p = '\0';
805
Thierry FOURNIER5554e292015-09-09 11:21:37 +0200806 send_log(px, level, "%s\n", trash.str);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100807 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200808 get_localtime(date.tv_sec, &tm);
809 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100810 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
811 (int)getpid(), trash.str);
812 fflush(stderr);
813 }
814}
815
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100816/* This function just ensure that the yield will be always
817 * returned with a timeout and permit to set some flags
818 */
819__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100820 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100821{
822 struct hlua *hlua = hlua_gethlua(L);
823
824 /* Set the wake timeout. If timeout is required, we set
825 * the expiration time.
826 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200827 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100828
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100829 hlua->flags |= flags;
830
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100831 /* Process the yield. */
832 WILL_LJMP(lua_yieldk(L, nresults, ctx, k));
833}
834
Willy Tarreau87b09662015-04-03 00:22:06 +0200835/* This function initialises the Lua environment stored in the stream.
836 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100837 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200838 *
839 * This function is particular. it initialises a new Lua thread. If the
840 * initialisation fails (example: out of memory error), the lua function
841 * throws an error (longjmp).
842 *
843 * This function manipulates two Lua stack: the main and the thread. Only
844 * the main stack can fail. The thread is not manipulated. This function
845 * MUST NOT manipulate the created thread stack state, because is not
846 * proctected agains error throwed by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100847 */
848int hlua_ctx_init(struct hlua *lua, struct task *task)
849{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200850 if (!SET_SAFE_LJMP(gL.T)) {
851 lua->Tref = LUA_REFNIL;
852 return 0;
853 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100854 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100855 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100856 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100857 lua->T = lua_newthread(gL.T);
858 if (!lua->T) {
859 lua->Tref = LUA_REFNIL;
860 return 0;
861 }
862 hlua_sethlua(lua);
863 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
864 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200865 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100866 return 1;
867}
868
Willy Tarreau87b09662015-04-03 00:22:06 +0200869/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100870 * is destroyed. The destroy also the memory context. The struct "lua"
871 * is not freed.
872 */
873void hlua_ctx_destroy(struct hlua *lua)
874{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100875 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100876 return;
877
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100878 if (!lua->T)
879 goto end;
880
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100881 /* Purge all the pending signals. */
Thierry FOURNIER847ca662016-12-16 13:07:22 +0100882 hlua_com_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100883
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100884 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
885 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200886
887 /* Forces a garbage collecting process. If the Lua program is finished
888 * without error, we run the GC on the thread pointer. Its freed all
889 * the unused memory.
890 * If the thread is finnish with an error or is currently yielded,
891 * it seems that the GC applied on the thread doesn't clean anything,
892 * so e run the GC on the main thread.
893 * NOTE: maybe this action locks all the Lua threads untiml the en of
894 * the garbage collection.
895 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200896 if (lua->flags & HLUA_MUST_GC) {
897 lua_gc(lua->T, LUA_GCCOLLECT, 0);
898 if (lua_status(lua->T) != LUA_OK)
899 lua_gc(gL.T, LUA_GCCOLLECT, 0);
900 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200901
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200902 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100903
904end:
905 pool_free2(pool2_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100906}
907
908/* This function is used to restore the Lua context when a coroutine
909 * fails. This function copy the common memory between old coroutine
910 * and the new coroutine. The old coroutine is destroyed, and its
911 * replaced by the new coroutine.
912 * If the flag "keep_msg" is set, the last entry of the old is assumed
913 * as string error message and it is copied in the new stack.
914 */
915static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
916{
917 lua_State *T;
918 int new_ref;
919
920 /* Renew the main LUA stack doesn't have sense. */
921 if (lua == &gL)
922 return 0;
923
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100924 /* New Lua coroutine. */
925 T = lua_newthread(gL.T);
926 if (!T)
927 return 0;
928
929 /* Copy last error message. */
930 if (keep_msg)
931 lua_xmove(lua->T, T, 1);
932
933 /* Copy data between the coroutines. */
934 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
935 lua_xmove(lua->T, T, 1);
936 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
937
938 /* Destroy old data. */
939 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
940
941 /* The thread is garbage collected by Lua. */
942 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
943
944 /* Fill the struct with the new coroutine values. */
945 lua->Mref = new_ref;
946 lua->T = T;
947 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
948
949 /* Set context. */
950 hlua_sethlua(lua);
951
952 return 1;
953}
954
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100955void hlua_hook(lua_State *L, lua_Debug *ar)
956{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100957 struct hlua *hlua = hlua_gethlua(L);
958
959 /* Lua cannot yield when its returning from a function,
960 * so, we can fix the interrupt hook to 1 instruction,
961 * expecting that the function is finnished.
962 */
963 if (lua_gethookmask(L) & LUA_MASKRET) {
964 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
965 return;
966 }
967
968 /* restore the interrupt condition. */
969 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
970
971 /* If we interrupt the Lua processing in yieldable state, we yield.
972 * If the state is not yieldable, trying yield causes an error.
973 */
974 if (lua_isyieldable(L))
975 WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
976
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +0100977 /* If we cannot yield, update the clock and check the timeout. */
978 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200979 hlua->run_time += now_ms - hlua->start_time;
980 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100981 lua_pushfstring(L, "execution timeout");
982 WILL_LJMP(lua_error(L));
983 }
984
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200985 /* Update the start time. */
986 hlua->start_time = now_ms;
987
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100988 /* Try to interrupt the process at the end of the current
989 * unyieldable function.
990 */
991 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100992}
993
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100994/* This function start or resumes the Lua stack execution. If the flag
995 * "yield_allowed" if no set and the LUA stack execution returns a yield
996 * The function return an error.
997 *
998 * The function can returns 4 values:
999 * - HLUA_E_OK : The execution is terminated without any errors.
1000 * - HLUA_E_AGAIN : The execution must continue at the next associated
1001 * task wakeup.
1002 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
1003 * the top of the stack.
1004 * - HLUA_E_ERR : An error has occured without error message.
1005 *
1006 * If an error occured, the stack is renewed and it is ready to run new
1007 * LUA code.
1008 */
1009static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1010{
1011 int ret;
1012 const char *msg;
1013
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001014 /* Initialise run time counter. */
1015 if (!HLUA_IS_RUNNING(lua))
1016 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001017
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001018resume_execution:
1019
1020 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1021 * instructions. it is used for preventing infinite loops.
1022 */
1023 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1024
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001025 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001026 HLUA_SET_RUN(lua);
1027 HLUA_CLR_CTRLYIELD(lua);
1028 HLUA_CLR_WAKERESWR(lua);
1029 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001030
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001031 /* Update the start time. */
1032 lua->start_time = now_ms;
1033
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001034 /* Call the function. */
1035 ret = lua_resume(lua->T, gL.T, lua->nargs);
1036 switch (ret) {
1037
1038 case LUA_OK:
1039 ret = HLUA_E_OK;
1040 break;
1041
1042 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001043 /* Check if the execution timeout is expired. It it is the case, we
1044 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001045 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001046 tv_update_date(0, 1);
1047 lua->run_time += now_ms - lua->start_time;
1048 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001049 lua_settop(lua->T, 0); /* Empty the stack. */
1050 if (!lua_checkstack(lua->T, 1)) {
1051 ret = HLUA_E_ERR;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001052 break;
1053 }
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001054 lua_pushfstring(lua->T, "execution timeout");
1055 ret = HLUA_E_ERRMSG;
1056 break;
1057 }
1058 /* Process the forced yield. if the general yield is not allowed or
1059 * if no task were associated this the current Lua execution
1060 * coroutine, we resume the execution. Else we want to return in the
1061 * scheduler and we want to be waked up again, to continue the
1062 * current Lua execution. So we schedule our own task.
1063 */
1064 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001065 if (!yield_allowed || !lua->task)
1066 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001067 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001068 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001069 if (!yield_allowed) {
1070 lua_settop(lua->T, 0); /* Empty the stack. */
1071 if (!lua_checkstack(lua->T, 1)) {
1072 ret = HLUA_E_ERR;
1073 break;
1074 }
1075 lua_pushfstring(lua->T, "yield not allowed");
1076 ret = HLUA_E_ERRMSG;
1077 break;
1078 }
1079 ret = HLUA_E_AGAIN;
1080 break;
1081
1082 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001083
1084 /* Special exit case. The traditionnal exit is returned as an error
1085 * because the errors ares the only one mean to return immediately
1086 * from and lua execution.
1087 */
1088 if (lua->flags & HLUA_EXIT) {
1089 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001090 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001091 break;
1092 }
1093
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001094 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001095 if (!lua_checkstack(lua->T, 1)) {
1096 ret = HLUA_E_ERR;
1097 break;
1098 }
1099 msg = lua_tostring(lua->T, -1);
1100 lua_settop(lua->T, 0); /* Empty the stack. */
1101 lua_pop(lua->T, 1);
1102 if (msg)
1103 lua_pushfstring(lua->T, "runtime error: %s", msg);
1104 else
1105 lua_pushfstring(lua->T, "unknown runtime error");
1106 ret = HLUA_E_ERRMSG;
1107 break;
1108
1109 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001110 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001111 lua_settop(lua->T, 0); /* Empty the stack. */
1112 if (!lua_checkstack(lua->T, 1)) {
1113 ret = HLUA_E_ERR;
1114 break;
1115 }
1116 lua_pushfstring(lua->T, "out of memory error");
1117 ret = HLUA_E_ERRMSG;
1118 break;
1119
1120 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001121 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001122 if (!lua_checkstack(lua->T, 1)) {
1123 ret = HLUA_E_ERR;
1124 break;
1125 }
1126 msg = lua_tostring(lua->T, -1);
1127 lua_settop(lua->T, 0); /* Empty the stack. */
1128 lua_pop(lua->T, 1);
1129 if (msg)
1130 lua_pushfstring(lua->T, "message handler error: %s", msg);
1131 else
1132 lua_pushfstring(lua->T, "message handler error");
1133 ret = HLUA_E_ERRMSG;
1134 break;
1135
1136 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001137 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001138 lua_settop(lua->T, 0); /* Empty the stack. */
1139 if (!lua_checkstack(lua->T, 1)) {
1140 ret = HLUA_E_ERR;
1141 break;
1142 }
1143 lua_pushfstring(lua->T, "unknonwn error");
1144 ret = HLUA_E_ERRMSG;
1145 break;
1146 }
1147
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001148 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001149 if (lua->flags & HLUA_MUST_GC &&
1150 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001151 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1152
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001153 switch (ret) {
1154 case HLUA_E_AGAIN:
1155 break;
1156
1157 case HLUA_E_ERRMSG:
Thierry FOURNIER847ca662016-12-16 13:07:22 +01001158 hlua_com_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001159 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001160 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001161 break;
1162
1163 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001164 HLUA_CLR_RUN(lua);
Thierry FOURNIER847ca662016-12-16 13:07:22 +01001165 hlua_com_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001166 hlua_ctx_renew(lua, 0);
1167 break;
1168
1169 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001170 HLUA_CLR_RUN(lua);
Thierry FOURNIER847ca662016-12-16 13:07:22 +01001171 hlua_com_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001172 break;
1173 }
1174
1175 return ret;
1176}
1177
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001178/* This function exit the current code. */
1179__LJMP static int hlua_done(lua_State *L)
1180{
1181 struct hlua *hlua = hlua_gethlua(L);
1182
1183 hlua->flags |= HLUA_EXIT;
1184 WILL_LJMP(lua_error(L));
1185
1186 return 0;
1187}
1188
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001189/* This function is an LUA binding. It provides a function
1190 * for deleting ACL from a referenced ACL file.
1191 */
1192__LJMP static int hlua_del_acl(lua_State *L)
1193{
1194 const char *name;
1195 const char *key;
1196 struct pat_ref *ref;
1197
1198 MAY_LJMP(check_args(L, 2, "del_acl"));
1199
1200 name = MAY_LJMP(luaL_checkstring(L, 1));
1201 key = MAY_LJMP(luaL_checkstring(L, 2));
1202
1203 ref = pat_ref_lookup(name);
1204 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001205 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001206
1207 pat_ref_delete(ref, key);
1208 return 0;
1209}
1210
1211/* This function is an LUA binding. It provides a function
1212 * for deleting map entry from a referenced map file.
1213 */
1214static int hlua_del_map(lua_State *L)
1215{
1216 const char *name;
1217 const char *key;
1218 struct pat_ref *ref;
1219
1220 MAY_LJMP(check_args(L, 2, "del_map"));
1221
1222 name = MAY_LJMP(luaL_checkstring(L, 1));
1223 key = MAY_LJMP(luaL_checkstring(L, 2));
1224
1225 ref = pat_ref_lookup(name);
1226 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001227 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001228
1229 pat_ref_delete(ref, key);
1230 return 0;
1231}
1232
1233/* This function is an LUA binding. It provides a function
1234 * for adding ACL pattern from a referenced ACL file.
1235 */
1236static int hlua_add_acl(lua_State *L)
1237{
1238 const char *name;
1239 const char *key;
1240 struct pat_ref *ref;
1241
1242 MAY_LJMP(check_args(L, 2, "add_acl"));
1243
1244 name = MAY_LJMP(luaL_checkstring(L, 1));
1245 key = MAY_LJMP(luaL_checkstring(L, 2));
1246
1247 ref = pat_ref_lookup(name);
1248 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001249 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001250
1251 if (pat_ref_find_elt(ref, key) == NULL)
1252 pat_ref_add(ref, key, NULL, NULL);
1253 return 0;
1254}
1255
1256/* This function is an LUA binding. It provides a function
1257 * for setting map pattern and sample from a referenced map
1258 * file.
1259 */
1260static int hlua_set_map(lua_State *L)
1261{
1262 const char *name;
1263 const char *key;
1264 const char *value;
1265 struct pat_ref *ref;
1266
1267 MAY_LJMP(check_args(L, 3, "set_map"));
1268
1269 name = MAY_LJMP(luaL_checkstring(L, 1));
1270 key = MAY_LJMP(luaL_checkstring(L, 2));
1271 value = MAY_LJMP(luaL_checkstring(L, 3));
1272
1273 ref = pat_ref_lookup(name);
1274 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001275 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001276
1277 if (pat_ref_find_elt(ref, key) != NULL)
1278 pat_ref_set(ref, key, value, NULL);
1279 else
1280 pat_ref_add(ref, key, value, NULL);
1281 return 0;
1282}
1283
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001284/* A class is a lot of memory that contain data. This data can be a table,
1285 * an integer or user data. This data is associated with a metatable. This
1286 * metatable have an original version registred in the global context with
1287 * the name of the object (_G[<name>] = <metable> ).
1288 *
1289 * A metable is a table that modify the standard behavior of a standard
1290 * access to the associated data. The entries of this new metatable are
1291 * defined as is:
1292 *
1293 * http://lua-users.org/wiki/MetatableEvents
1294 *
1295 * __index
1296 *
1297 * we access an absent field in a table, the result is nil. This is
1298 * true, but it is not the whole truth. Actually, such access triggers
1299 * the interpreter to look for an __index metamethod: If there is no
1300 * such method, as usually happens, then the access results in nil;
1301 * otherwise, the metamethod will provide the result.
1302 *
1303 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1304 * the key does not appear in the table, but the metatable has an __index
1305 * property:
1306 *
1307 * - if the value is a function, the function is called, passing in the
1308 * table and the key; the return value of that function is returned as
1309 * the result.
1310 *
1311 * - if the value is another table, the value of the key in that table is
1312 * asked for and returned (and if it doesn't exist in that table, but that
1313 * table's metatable has an __index property, then it continues on up)
1314 *
1315 * - Use "rawget(myTable,key)" to skip this metamethod.
1316 *
1317 * http://www.lua.org/pil/13.4.1.html
1318 *
1319 * __newindex
1320 *
1321 * Like __index, but control property assignment.
1322 *
1323 * __mode - Control weak references. A string value with one or both
1324 * of the characters 'k' and 'v' which specifies that the the
1325 * keys and/or values in the table are weak references.
1326 *
1327 * __call - Treat a table like a function. When a table is followed by
1328 * parenthesis such as "myTable( 'foo' )" and the metatable has
1329 * a __call key pointing to a function, that function is invoked
1330 * (passing any specified arguments) and the return value is
1331 * returned.
1332 *
1333 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1334 * called, if the metatable for myTable has a __metatable
1335 * key, the value of that key is returned instead of the
1336 * actual metatable.
1337 *
1338 * __tostring - Control string representation. When the builtin
1339 * "tostring( myTable )" function is called, if the metatable
1340 * for myTable has a __tostring property set to a function,
1341 * that function is invoked (passing myTable to it) and the
1342 * return value is used as the string representation.
1343 *
1344 * __len - Control table length. When the table length is requested using
1345 * the length operator ( '#' ), if the metatable for myTable has
1346 * a __len key pointing to a function, that function is invoked
1347 * (passing myTable to it) and the return value used as the value
1348 * of "#myTable".
1349 *
1350 * __gc - Userdata finalizer code. When userdata is set to be garbage
1351 * collected, if the metatable has a __gc field pointing to a
1352 * function, that function is first invoked, passing the userdata
1353 * to it. The __gc metamethod is not called for tables.
1354 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1355 *
1356 * Special metamethods for redefining standard operators:
1357 * http://www.lua.org/pil/13.1.html
1358 *
1359 * __add "+"
1360 * __sub "-"
1361 * __mul "*"
1362 * __div "/"
1363 * __unm "!"
1364 * __pow "^"
1365 * __concat ".."
1366 *
1367 * Special methods for redfining standar relations
1368 * http://www.lua.org/pil/13.2.html
1369 *
1370 * __eq "=="
1371 * __lt "<"
1372 * __le "<="
1373 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001374
1375/*
1376 *
1377 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001378 * Class Map
1379 *
1380 *
1381 */
1382
1383/* Returns a struct hlua_map if the stack entry "ud" is
1384 * a class session, otherwise it throws an error.
1385 */
1386__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1387{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001388 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001389}
1390
1391/* This function is the map constructor. It don't need
1392 * the class Map object. It creates and return a new Map
1393 * object. It must be called only during "body" or "init"
1394 * context because it process some filesystem accesses.
1395 */
1396__LJMP static int hlua_map_new(struct lua_State *L)
1397{
1398 const char *fn;
1399 int match = PAT_MATCH_STR;
1400 struct sample_conv conv;
1401 const char *file = "";
1402 int line = 0;
1403 lua_Debug ar;
1404 char *err = NULL;
1405 struct arg args[2];
1406
1407 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1408 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1409
1410 fn = MAY_LJMP(luaL_checkstring(L, 1));
1411
1412 if (lua_gettop(L) >= 2) {
1413 match = MAY_LJMP(luaL_checkinteger(L, 2));
1414 if (match < 0 || match >= PAT_MATCH_NUM)
1415 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1416 }
1417
1418 /* Get Lua filename and line number. */
1419 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1420 lua_getinfo(L, "Sl", &ar); /* get info about it */
1421 if (ar.currentline > 0) { /* is there info? */
1422 file = ar.short_src;
1423 line = ar.currentline;
1424 }
1425 }
1426
1427 /* fill fake sample_conv struct. */
1428 conv.kw = ""; /* unused. */
1429 conv.process = NULL; /* unused. */
1430 conv.arg_mask = 0; /* unused. */
1431 conv.val_args = NULL; /* unused. */
1432 conv.out_type = SMP_T_STR;
1433 conv.private = (void *)(long)match;
1434 switch (match) {
1435 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1436 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1437 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1438 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1439 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1440 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1441 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001442 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001443 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1444 default:
1445 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1446 }
1447
1448 /* fill fake args. */
1449 args[0].type = ARGT_STR;
1450 args[0].data.str.str = (char *)fn;
1451 args[1].type = ARGT_STOP;
1452
1453 /* load the map. */
1454 if (!sample_load_map(args, &conv, file, line, &err)) {
1455 /* error case: we cant use luaL_error because we must
1456 * free the err variable.
1457 */
1458 luaL_where(L, 1);
1459 lua_pushfstring(L, "'new': %s.", err);
1460 lua_concat(L, 2);
1461 free(err);
1462 WILL_LJMP(lua_error(L));
1463 }
1464
1465 /* create the lua object. */
1466 lua_newtable(L);
1467 lua_pushlightuserdata(L, args[0].data.map);
1468 lua_rawseti(L, -2, 0);
1469
1470 /* Pop a class Map metatable and affect it to the userdata. */
1471 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1472 lua_setmetatable(L, -2);
1473
1474
1475 return 1;
1476}
1477
1478__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1479{
1480 struct map_descriptor *desc;
1481 struct pattern *pat;
1482 struct sample smp;
1483
1484 MAY_LJMP(check_args(L, 2, "lookup"));
1485 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001486 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001487 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001488 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001489 }
1490 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001491 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001492 smp.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001493 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 +02001494 }
1495
1496 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001497 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001498 if (str)
1499 lua_pushstring(L, "");
1500 else
1501 lua_pushnil(L);
1502 return 1;
1503 }
1504
1505 /* The Lua pattern must return a string, so we can't check the returned type */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001506 lua_pushlstring(L, pat->data->u.str.str, pat->data->u.str.len);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001507 return 1;
1508}
1509
1510__LJMP static int hlua_map_lookup(struct lua_State *L)
1511{
1512 return _hlua_map_lookup(L, 0);
1513}
1514
1515__LJMP static int hlua_map_slookup(struct lua_State *L)
1516{
1517 return _hlua_map_lookup(L, 1);
1518}
1519
1520/*
1521 *
1522 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001523 * Class Socket
1524 *
1525 *
1526 */
1527
1528__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1529{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001530 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001531}
1532
1533/* This function is the handler called for each I/O on the established
1534 * connection. It is used for notify space avalaible to send or data
1535 * received.
1536 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001537static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001538{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001539 struct stream_interface *si = appctx->owner;
Willy Tarreau50fe03b2014-11-28 13:59:31 +01001540 struct connection *c = objt_conn(si_opposite(si)->end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001541
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001542 /* If the connection object is not avalaible, close all the
1543 * streams and wakeup everithing waiting for.
1544 */
1545 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001546 si_shutw(si);
1547 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001548 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001549 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1550 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001551 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001552 }
1553
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001554 /* If we cant write, wakeup the pending write signals. */
1555 if (channel_output_closed(si_ic(si)))
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001556 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001557
1558 /* If we cant read, wakeup the pending read signals. */
1559 if (channel_input_closed(si_oc(si)))
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001560 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001561
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001562 /* if the connection is not estabkished, inform the stream that we want
1563 * to be notified whenever the connection completes.
1564 */
1565 if (!(c->flags & CO_FL_CONNECTED)) {
1566 si_applet_cant_get(si);
1567 si_applet_cant_put(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001568 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001569 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001570
1571 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001572 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001573
1574 /* Wake the tasks which wants to write if the buffer have avalaible space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001575 if (channel_may_recv(si_ic(si)))
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001576 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001577
1578 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001579 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001580 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001581}
1582
Willy Tarreau87b09662015-04-03 00:22:06 +02001583/* This function is called when the "struct stream" is destroyed.
1584 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001585 * Wake all the pending signals.
1586 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001587static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001588{
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001589 /* Remove my link in the original object. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001590 if (appctx->ctx.hlua_cosocket.socket)
1591 appctx->ctx.hlua_cosocket.socket->s = NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001592
1593 /* Wake all the task waiting for me. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001594 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1595 hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001596}
1597
1598/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001599 * uses this object. If the stream does not exists, just quit.
1600 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001601 * pending signal can rest in the read and write lists. destroy
1602 * it.
1603 */
1604__LJMP static int hlua_socket_gc(lua_State *L)
1605{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001606 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001607 struct appctx *appctx;
1608
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001609 MAY_LJMP(check_args(L, 1, "__gc"));
1610
1611 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001612 if (!socket->s)
1613 return 0;
1614
Willy Tarreau87b09662015-04-03 00:22:06 +02001615 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001616 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001617 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001618 socket->s = NULL;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001619 appctx->ctx.hlua_cosocket.socket = NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001620
1621 return 0;
1622}
1623
1624/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001625 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001626 */
1627__LJMP static int hlua_socket_close(lua_State *L)
1628{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001629 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001630 struct appctx *appctx;
1631
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001632 MAY_LJMP(check_args(L, 1, "close"));
1633
1634 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001635 if (!socket->s)
1636 return 0;
1637
Willy Tarreau87b09662015-04-03 00:22:06 +02001638 /* Close the stream and remove the associated stop task. */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001639 stream_shutdown(socket->s, SF_ERR_KILLED);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001640 appctx = objt_appctx(socket->s->si[0].end);
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001641 appctx->ctx.hlua_cosocket.socket = NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001642 socket->s = NULL;
1643
1644 return 0;
1645}
1646
1647/* This Lua function assumes that the stack contain three parameters.
1648 * 1 - USERDATA containing a struct socket
1649 * 2 - INTEGER with values of the macro defined below
1650 * If the integer is -1, we must read at most one line.
1651 * If the integer is -2, we ust read all the data until the
1652 * end of the stream.
1653 * If the integer is positive value, we must read a number of
1654 * bytes corresponding to this value.
1655 */
1656#define HLSR_READ_LINE (-1)
1657#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001658__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001659{
1660 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1661 int wanted = lua_tointeger(L, 2);
1662 struct hlua *hlua = hlua_gethlua(L);
1663 struct appctx *appctx;
1664 int len;
1665 int nblk;
1666 char *blk1;
1667 int len1;
1668 char *blk2;
1669 int len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001670 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001671 struct channel *oc;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001672
1673 /* Check if this lua stack is schedulable. */
1674 if (!hlua || !hlua->task)
1675 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1676 "'frontend', 'backend' or 'task'"));
1677
1678 /* check for connection closed. If some data where read, return it. */
1679 if (!socket->s)
1680 goto connection_closed;
1681
Willy Tarreau94aa6172015-03-13 14:19:06 +01001682 oc = &socket->s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001683 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001684 /* Read line. */
Willy Tarreau81389672015-03-10 12:03:52 +01001685 nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001686 if (nblk < 0) /* Connection close. */
1687 goto connection_closed;
1688 if (nblk == 0) /* No data avalaible. */
1689 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001690
1691 /* remove final \r\n. */
1692 if (nblk == 1) {
1693 if (blk1[len1-1] == '\n') {
1694 len1--;
1695 skip_at_end++;
1696 if (blk1[len1-1] == '\r') {
1697 len1--;
1698 skip_at_end++;
1699 }
1700 }
1701 }
1702 else {
1703 if (blk2[len2-1] == '\n') {
1704 len2--;
1705 skip_at_end++;
1706 if (blk2[len2-1] == '\r') {
1707 len2--;
1708 skip_at_end++;
1709 }
1710 }
1711 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001712 }
1713
1714 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001715 /* Read all the available data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001716 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001717 if (nblk < 0) /* Connection close. */
1718 goto connection_closed;
1719 if (nblk == 0) /* No data avalaible. */
1720 goto connection_empty;
1721 }
1722
1723 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001724 /* Read a block of data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001725 nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001726 if (nblk < 0) /* Connection close. */
1727 goto connection_closed;
1728 if (nblk == 0) /* No data avalaible. */
1729 goto connection_empty;
1730
1731 if (len1 > wanted) {
1732 nblk = 1;
1733 len1 = wanted;
1734 } if (nblk == 2 && len1 + len2 > wanted)
1735 len2 = wanted - len1;
1736 }
1737
1738 len = len1;
1739
1740 luaL_addlstring(&socket->b, blk1, len1);
1741 if (nblk == 2) {
1742 len += len2;
1743 luaL_addlstring(&socket->b, blk2, len2);
1744 }
1745
1746 /* Consume data. */
Willy Tarreau81389672015-03-10 12:03:52 +01001747 bo_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001748
1749 /* Don't wait anything. */
Willy Tarreaude70fa12015-09-26 11:25:05 +02001750 stream_int_notify(&socket->s->si[0]);
1751 stream_int_update_applet(&socket->s->si[0]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001752
1753 /* If the pattern reclaim to read all the data
1754 * in the connection, got out.
1755 */
1756 if (wanted == HLSR_READ_ALL)
1757 goto connection_empty;
1758 else if (wanted >= 0 && len < wanted)
1759 goto connection_empty;
1760
1761 /* Return result. */
1762 luaL_pushresult(&socket->b);
1763 return 1;
1764
1765connection_closed:
1766
1767 /* If the buffer containds data. */
1768 if (socket->b.n > 0) {
1769 luaL_pushresult(&socket->b);
1770 return 1;
1771 }
1772 lua_pushnil(L);
1773 lua_pushstring(L, "connection closed.");
1774 return 2;
1775
1776connection_empty:
1777
1778 appctx = objt_appctx(socket->s->si[0].end);
Thierry FOURNIER847ca662016-12-16 13:07:22 +01001779 if (!hlua_com_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001780 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001781 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001782 return 0;
1783}
1784
1785/* This Lus function gets two parameters. The first one can be string
1786 * or a number. If the string is "*l", the user require one line. If
1787 * the string is "*a", the user require all the content of the stream.
1788 * If the value is a number, the user require a number of bytes equal
1789 * to the value. The default value is "*l" (a line).
1790 *
1791 * This paraeter with a variable type is converted in integer. This
1792 * integer takes this values:
1793 * -1 : read a line
1794 * -2 : read all the stream
1795 * >0 : amount if bytes.
1796 *
1797 * The second parameter is optinal. It contains a string that must be
1798 * concatenated with the read data.
1799 */
1800__LJMP static int hlua_socket_receive(struct lua_State *L)
1801{
1802 int wanted = HLSR_READ_LINE;
1803 const char *pattern;
1804 int type;
1805 char *error;
1806 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001807 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001808
1809 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1810 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1811
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001812 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001813
1814 /* check for pattern. */
1815 if (lua_gettop(L) >= 2) {
1816 type = lua_type(L, 2);
1817 if (type == LUA_TSTRING) {
1818 pattern = lua_tostring(L, 2);
1819 if (strcmp(pattern, "*a") == 0)
1820 wanted = HLSR_READ_ALL;
1821 else if (strcmp(pattern, "*l") == 0)
1822 wanted = HLSR_READ_LINE;
1823 else {
1824 wanted = strtoll(pattern, &error, 10);
1825 if (*error != '\0')
1826 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1827 }
1828 }
1829 else if (type == LUA_TNUMBER) {
1830 wanted = lua_tointeger(L, 2);
1831 if (wanted < 0)
1832 WILL_LJMP(luaL_error(L, "Unsupported size."));
1833 }
1834 }
1835
1836 /* Set pattern. */
1837 lua_pushinteger(L, wanted);
1838 lua_replace(L, 2);
1839
1840 /* init bufffer, and fiil it wih prefix. */
1841 luaL_buffinit(L, &socket->b);
1842
1843 /* Check prefix. */
1844 if (lua_gettop(L) >= 3) {
1845 if (lua_type(L, 3) != LUA_TSTRING)
1846 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1847 pattern = lua_tolstring(L, 3, &len);
1848 luaL_addlstring(&socket->b, pattern, len);
1849 }
1850
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001851 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001852}
1853
1854/* Write the Lua input string in the output buffer.
1855 * This fucntion returns a yield if no space are available.
1856 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001857static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001858{
1859 struct hlua_socket *socket;
1860 struct hlua *hlua = hlua_gethlua(L);
1861 struct appctx *appctx;
1862 size_t buf_len;
1863 const char *buf;
1864 int len;
1865 int send_len;
1866 int sent;
1867
1868 /* Check if this lua stack is schedulable. */
1869 if (!hlua || !hlua->task)
1870 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1871 "'frontend', 'backend' or 'task'"));
1872
1873 /* Get object */
1874 socket = MAY_LJMP(hlua_checksocket(L, 1));
1875 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001876 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001877
1878 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001879 if (!socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001880 lua_pushinteger(L, -1);
1881 return 1;
1882 }
1883
1884 /* Update the input buffer data. */
1885 buf += sent;
1886 send_len = buf_len - sent;
1887
1888 /* All the data are sent. */
1889 if (sent >= buf_len)
1890 return 1; /* Implicitly return the length sent. */
1891
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001892 /* Check if the buffer is avalaible because HAProxy doesn't allocate
1893 * the request buffer if its not required.
1894 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001895 if (socket->s->req.buf->size == 0) {
Christopher Faulet33834b12016-12-19 09:29:06 +01001896 appctx = hlua->task->context;
1897 if (!channel_alloc_buffer(&socket->s->req, &appctx->buffer_wait))
1898 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01001899 }
1900
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001901 /* Check for avalaible space. */
Willy Tarreau94aa6172015-03-13 14:19:06 +01001902 len = buffer_total_space(socket->s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01001903 if (len <= 0) {
1904 appctx = objt_appctx(socket->s->si[0].end);
1905 if (!hlua_com_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task))
1906 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001907 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01001908 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909
1910 /* send data */
1911 if (len < send_len)
1912 send_len = len;
Willy Tarreau94aa6172015-03-13 14:19:06 +01001913 len = bi_putblk(&socket->s->req, buf+sent, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001914
1915 /* "Not enough space" (-1), "Buffer too little to contain
1916 * the data" (-2) are not expected because the available length
1917 * is tested.
1918 * Other unknown error are also not expected.
1919 */
1920 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01001921 if (len == -1)
Willy Tarreau94aa6172015-03-13 14:19:06 +01001922 socket->s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01001923
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001924 MAY_LJMP(hlua_socket_close(L));
1925 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001926 lua_pushinteger(L, -1);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001927 return 1;
1928 }
1929
1930 /* update buffers. */
Willy Tarreaude70fa12015-09-26 11:25:05 +02001931 stream_int_notify(&socket->s->si[0]);
1932 stream_int_update_applet(&socket->s->si[0]);
1933
Willy Tarreau94aa6172015-03-13 14:19:06 +01001934 socket->s->req.rex = TICK_ETERNITY;
1935 socket->s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001936
1937 /* Update length sent. */
1938 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001939 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001940
1941 /* All the data buffer is sent ? */
1942 if (sent + len >= buf_len)
1943 return 1;
1944
1945hlua_socket_write_yield_return:
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001946 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001947 return 0;
1948}
1949
1950/* This function initiate the send of data. It just check the input
1951 * parameters and push an integer in the Lua stack that contain the
1952 * amount of data writed in the buffer. This is used by the function
1953 * "hlua_socket_write_yield" that can yield.
1954 *
1955 * The Lua function gets between 3 and 4 parameters. The first one is
1956 * the associated object. The second is a string buffer. The third is
1957 * a facultative integer that represents where is the buffer position
1958 * of the start of the data that can send. The first byte is the
1959 * position "1". The default value is "1". The fourth argument is a
1960 * facultative integer that represents where is the buffer position
1961 * of the end of the data that can send. The default is the last byte.
1962 */
1963static int hlua_socket_send(struct lua_State *L)
1964{
1965 int i;
1966 int j;
1967 const char *buf;
1968 size_t buf_len;
1969
1970 /* Check number of arguments. */
1971 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
1972 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
1973
1974 /* Get the string. */
1975 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
1976
1977 /* Get and check j. */
1978 if (lua_gettop(L) == 4) {
1979 j = MAY_LJMP(luaL_checkinteger(L, 4));
1980 if (j < 0)
1981 j = buf_len + j + 1;
1982 if (j > buf_len)
1983 j = buf_len + 1;
1984 lua_pop(L, 1);
1985 }
1986 else
1987 j = buf_len;
1988
1989 /* Get and check i. */
1990 if (lua_gettop(L) == 3) {
1991 i = MAY_LJMP(luaL_checkinteger(L, 3));
1992 if (i < 0)
1993 i = buf_len + i + 1;
1994 if (i > buf_len)
1995 i = buf_len + 1;
1996 lua_pop(L, 1);
1997 } else
1998 i = 1;
1999
2000 /* Check bth i and j. */
2001 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002002 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002003 return 1;
2004 }
2005 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002006 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002007 return 1;
2008 }
2009 if (i == 0)
2010 i = 1;
2011 if (j == 0)
2012 j = 1;
2013
2014 /* Pop the string. */
2015 lua_pop(L, 1);
2016
2017 /* Update the buffer length. */
2018 buf += i - 1;
2019 buf_len = j - i + 1;
2020 lua_pushlstring(L, buf, buf_len);
2021
2022 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002023 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002024
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002025 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002026}
2027
Willy Tarreau22b0a682015-06-17 19:43:49 +02002028#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002029__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2030{
2031 static char buffer[SOCKET_INFO_MAX_LEN];
2032 int ret;
2033 int len;
2034 char *p;
2035
2036 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2037 if (ret <= 0) {
2038 lua_pushnil(L);
2039 return 1;
2040 }
2041
2042 if (ret == AF_UNIX) {
2043 lua_pushstring(L, buffer+1);
2044 return 1;
2045 }
2046 else if (ret == AF_INET6) {
2047 buffer[0] = '[';
2048 len = strlen(buffer);
2049 buffer[len] = ']';
2050 len++;
2051 buffer[len] = ':';
2052 len++;
2053 p = buffer;
2054 }
2055 else if (ret == AF_INET) {
2056 p = buffer + 1;
2057 len = strlen(p);
2058 p[len] = ':';
2059 len++;
2060 }
2061 else {
2062 lua_pushnil(L);
2063 return 1;
2064 }
2065
2066 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2067 lua_pushnil(L);
2068 return 1;
2069 }
2070
2071 lua_pushstring(L, p);
2072 return 1;
2073}
2074
2075/* Returns information about the peer of the connection. */
2076__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2077{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002078 struct hlua_socket *socket;
2079 struct connection *conn;
2080
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002081 MAY_LJMP(check_args(L, 1, "getpeername"));
2082
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002083 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002084
2085 /* Check if the tcp object is avalaible. */
2086 if (!socket->s) {
2087 lua_pushnil(L);
2088 return 1;
2089 }
2090
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002091 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002092 if (!conn) {
2093 lua_pushnil(L);
2094 return 1;
2095 }
2096
Willy Tarreaua71f6422016-11-16 17:00:14 +01002097 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002098 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Willy Tarreaua71f6422016-11-16 17:00:14 +01002099 lua_pushnil(L);
2100 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002101 }
2102
2103 return MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2104}
2105
2106/* Returns information about my connection side. */
2107static int hlua_socket_getsockname(struct lua_State *L)
2108{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002109 struct hlua_socket *socket;
2110 struct connection *conn;
2111
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002112 MAY_LJMP(check_args(L, 1, "getsockname"));
2113
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002114 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002115
2116 /* Check if the tcp object is avalaible. */
2117 if (!socket->s) {
2118 lua_pushnil(L);
2119 return 1;
2120 }
2121
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002122 conn = objt_conn(socket->s->si[1].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002123 if (!conn) {
2124 lua_pushnil(L);
2125 return 1;
2126 }
2127
Willy Tarreaua71f6422016-11-16 17:00:14 +01002128 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002129 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Willy Tarreaua71f6422016-11-16 17:00:14 +01002130 lua_pushnil(L);
2131 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002132 }
2133
2134 return hlua_socket_info(L, &conn->addr.from);
2135}
2136
2137/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002138static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002139 .obj_type = OBJ_TYPE_APPLET,
2140 .name = "<LUA_TCP>",
2141 .fct = hlua_socket_handler,
2142 .release = hlua_socket_release,
2143};
2144
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002145__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002146{
2147 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2148 struct hlua *hlua = hlua_gethlua(L);
2149 struct appctx *appctx;
2150
2151 /* Check for connection close. */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002152 if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002153 lua_pushnil(L);
2154 lua_pushstring(L, "Can't connect");
2155 return 2;
2156 }
2157
2158 appctx = objt_appctx(socket->s->si[0].end);
2159
2160 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002161 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002162 lua_pushinteger(L, 1);
2163 return 1;
2164 }
2165
Thierry FOURNIER847ca662016-12-16 13:07:22 +01002166 if (!hlua_com_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task))
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002167 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002168 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002169 return 0;
2170}
2171
2172/* This function fail or initite the connection. */
2173__LJMP static int hlua_socket_connect(struct lua_State *L)
2174{
2175 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002176 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002177 const char *ip;
2178 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002179 struct hlua *hlua;
2180 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002181 int low, high;
2182 struct sockaddr_storage *addr;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002183
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002184 if (lua_gettop(L) < 2)
2185 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002186
2187 /* Get args. */
2188 socket = MAY_LJMP(hlua_checksocket(L, 1));
2189 ip = MAY_LJMP(luaL_checkstring(L, 2));
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002190 if (lua_gettop(L) >= 3)
2191 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002192
Willy Tarreau973a5422015-08-05 21:47:23 +02002193 conn = si_alloc_conn(&socket->s->si[1]);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002194 if (!conn)
2195 WILL_LJMP(luaL_error(L, "connect: internal error"));
2196
Willy Tarreau3adac082015-09-26 17:51:09 +02002197 /* needed for the connection not to be closed */
2198 conn->target = socket->s->target;
2199
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002200 /* Parse ip address. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002201 addr = str2sa_range(ip, &low, &high, NULL, NULL, NULL, 0);
2202 if (!addr)
2203 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
2204 if (low != high)
2205 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
2206 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002207
2208 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002209 if (low == 0) {
2210 if (conn->addr.to.ss_family == AF_INET) {
2211 if (port == -1)
2212 WILL_LJMP(luaL_error(L, "connect: port missing"));
2213 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2214 } else if (conn->addr.to.ss_family == AF_INET6) {
2215 if (port == -1)
2216 WILL_LJMP(luaL_error(L, "connect: port missing"));
2217 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2218 }
2219 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002220
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002221 hlua = hlua_gethlua(L);
2222 appctx = objt_appctx(socket->s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002223
2224 /* inform the stream that we want to be notified whenever the
2225 * connection completes.
2226 */
2227 si_applet_cant_get(&socket->s->si[0]);
2228 si_applet_cant_put(&socket->s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002229 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002230
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002231 hlua->flags |= HLUA_MUST_GC;
2232
Thierry FOURNIER847ca662016-12-16 13:07:22 +01002233 if (!hlua_com_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task))
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002234 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002235 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002236
2237 return 0;
2238}
2239
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002240#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002241__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2242{
2243 struct hlua_socket *socket;
2244
2245 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2246 socket = MAY_LJMP(hlua_checksocket(L, 1));
2247 socket->s->target = &socket_ssl.obj_type;
2248 return MAY_LJMP(hlua_socket_connect(L));
2249}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002250#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002251
2252__LJMP static int hlua_socket_setoption(struct lua_State *L)
2253{
2254 return 0;
2255}
2256
2257__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2258{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002259 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002260 int tmout;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002261
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002262 MAY_LJMP(check_args(L, 2, "settimeout"));
2263
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002264 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002265 tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002266
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002267 socket->s->req.rto = tmout;
2268 socket->s->req.wto = tmout;
2269 socket->s->res.rto = tmout;
2270 socket->s->res.wto = tmout;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002271
2272 return 0;
2273}
2274
2275__LJMP static int hlua_socket_new(lua_State *L)
2276{
2277 struct hlua_socket *socket;
2278 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002279 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002280 struct stream *strm;
Willy Tarreaud420a972015-04-06 00:39:18 +02002281 struct task *task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002282
2283 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002284 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002285 hlua_pusherror(L, "socket: full stack");
2286 goto out_fail_conf;
2287 }
2288
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002289 /* Create the object: obj[0] = userdata. */
2290 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002291 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002292 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002293 memset(socket, 0, sizeof(*socket));
2294
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002295 /* Check if the various memory pools are intialized. */
Willy Tarreau87b09662015-04-03 00:22:06 +02002296 if (!pool2_stream || !pool2_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002297 hlua_pusherror(L, "socket: uninitialized pools.");
2298 goto out_fail_conf;
2299 }
2300
Willy Tarreau87b09662015-04-03 00:22:06 +02002301 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002302 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2303 lua_setmetatable(L, -2);
2304
Willy Tarreaud420a972015-04-06 00:39:18 +02002305 /* Create the applet context */
2306 appctx = appctx_new(&update_applet);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002307 if (!appctx) {
2308 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002309 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002310 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002311
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002312 appctx->ctx.hlua_cosocket.socket = socket;
2313 appctx->ctx.hlua_cosocket.connected = 0;
2314 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2315 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002316
Willy Tarreaud420a972015-04-06 00:39:18 +02002317 /* Now create a session, task and stream for this applet */
2318 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2319 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002320 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002321 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002322 }
2323
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002324 task = task_new();
2325 if (!task) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002326 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002327 goto out_fail_task;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002328 }
Willy Tarreaud420a972015-04-06 00:39:18 +02002329 task->nice = 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002330
Willy Tarreau73b65ac2015-04-08 18:26:29 +02002331 strm = stream_new(sess, task, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002332 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002333 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002334 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002335 }
2336
Willy Tarreaud420a972015-04-06 00:39:18 +02002337 /* Configure an empty Lua for the stream. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002338 socket->s = strm;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01002339 strm->hlua = NULL;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002340
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002341 /* Configure "right" stream interface. this "si" is used to connect
2342 * and retrieve data from the server. The connection is initialized
2343 * with the "struct server".
2344 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002345 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002346
2347 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002348 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2349 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002351 /* Update statistics counters. */
2352 socket_proxy.feconn++; /* beconn will be increased later */
2353 jobs++;
2354 totalconn++;
2355
2356 /* Return yield waiting for connection. */
2357 return 1;
2358
Willy Tarreaud420a972015-04-06 00:39:18 +02002359 out_fail_stream:
2360 task_free(task);
2361 out_fail_task:
Willy Tarreau11c36242015-04-04 15:54:03 +02002362 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002363 out_fail_sess:
2364 appctx_free(appctx);
2365 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002366 WILL_LJMP(lua_error(L));
2367 return 0;
2368}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002369
2370/*
2371 *
2372 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002373 * Class Channel
2374 *
2375 *
2376 */
2377
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002378/* The state between the channel data and the HTTP parser state can be
2379 * unconsistent, so reset the parser and call it again. Warning, this
2380 * action not revalidate the request and not send a 400 if the modified
2381 * resuest is not valid.
2382 *
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002383 * This function never fails. The direction is set using dir, which equals
2384 * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002385 */
2386static void hlua_resynchonize_proto(struct stream *stream, int dir)
2387{
2388 /* Protocol HTTP. */
2389 if (stream->be->mode == PR_MODE_HTTP) {
2390
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002391 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002392 http_txn_reset_req(stream->txn);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002393 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002394 http_txn_reset_res(stream->txn);
2395
2396 if (stream->txn->hdr_idx.v)
2397 hdr_idx_init(&stream->txn->hdr_idx);
2398
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002399 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002400 http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002401 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002402 http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx);
2403 }
2404}
2405
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002406/* This function is called before the Lua execution. It stores
2407 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002408 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002409static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002410{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002411 c->mode = stream->be->mode;
2412 switch (c->mode) {
2413 case PR_MODE_HTTP:
2414 c->data.http.dir = opt & SMP_OPT_DIR;
2415 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2416 c->data.http.state = stream->txn->req.msg_state;
2417 else
2418 c->data.http.state = stream->txn->rsp.msg_state;
2419 break;
2420 default:
2421 break;
2422 }
2423}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002424
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002425/* This function is called after the Lua execution. it
2426 * returns true if the parser state is consistent, otherwise,
2427 * it return false.
2428 *
2429 * In HTTP mode, the parser state must be in the same state
2430 * or greater when we exit the function. Even if we do a
2431 * control yield. This prevent to break the HTTP message
2432 * from the Lua code.
2433 */
2434static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2435{
2436 if (c->mode != stream->be->mode)
2437 return 0;
2438
2439 switch (c->mode) {
2440 case PR_MODE_HTTP:
2441 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002442 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002443 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2444 return stream->txn->req.msg_state >= c->data.http.state;
2445 else
2446 return stream->txn->rsp.msg_state >= c->data.http.state;
2447 default:
2448 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002449 }
2450 return 1;
2451}
2452
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002453/* Returns the struct hlua_channel join to the class channel in the
2454 * stack entry "ud" or throws an argument error.
2455 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002456__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002457{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002458 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002459}
2460
Willy Tarreau47860ed2015-03-10 14:07:50 +01002461/* Pushes the channel onto the top of the stack. If the stask does not have a
2462 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002463 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002464static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002465{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002466 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002467 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002468 return 0;
2469
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002470 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002471 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002472 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002473
2474 /* Pop a class sesison metatable and affect it to the userdata. */
2475 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2476 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002477 return 1;
2478}
2479
2480/* Duplicate all the data present in the input channel and put it
2481 * in a string LUA variables. Returns -1 and push a nil value in
2482 * the stack if the channel is closed and all the data are consumed,
2483 * returns 0 if no data are available, otherwise it returns the length
2484 * of the builded string.
2485 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002486static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002487{
2488 char *blk1;
2489 char *blk2;
2490 int len1;
2491 int len2;
2492 int ret;
2493 luaL_Buffer b;
2494
Willy Tarreau47860ed2015-03-10 14:07:50 +01002495 ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002496 if (unlikely(ret == 0))
2497 return 0;
2498
2499 if (unlikely(ret < 0)) {
2500 lua_pushnil(L);
2501 return -1;
2502 }
2503
2504 luaL_buffinit(L, &b);
2505 luaL_addlstring(&b, blk1, len1);
2506 if (unlikely(ret == 2))
2507 luaL_addlstring(&b, blk2, len2);
2508 luaL_pushresult(&b);
2509
2510 if (unlikely(ret == 2))
2511 return len1 + len2;
2512 return len1;
2513}
2514
2515/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2516 * a yield. This function keep the data in the buffer.
2517 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002518__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002519{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002520 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002521
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002522 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2523
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002524 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002525 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002526 return 1;
2527}
2528
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002529/* Check arguments for the function "hlua_channel_dup_yield". */
2530__LJMP static int hlua_channel_dup(lua_State *L)
2531{
2532 MAY_LJMP(check_args(L, 1, "dup"));
2533 MAY_LJMP(hlua_checkchannel(L, 1));
2534 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2535}
2536
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002537/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2538 * a yield. This function consumes the data in the buffer. It returns
2539 * a string containing the data or a nil pointer if no data are available
2540 * and the channel is closed.
2541 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002542__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002543{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002544 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002545 int ret;
2546
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002547 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002548
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002549 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002550 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002551 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002552
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002553 if (unlikely(ret == -1))
2554 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002555
Willy Tarreau47860ed2015-03-10 14:07:50 +01002556 chn->buf->i -= ret;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002557 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002558 return 1;
2559}
2560
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002561/* Check arguments for the fucntion "hlua_channel_get_yield". */
2562__LJMP static int hlua_channel_get(lua_State *L)
2563{
2564 MAY_LJMP(check_args(L, 1, "get"));
2565 MAY_LJMP(hlua_checkchannel(L, 1));
2566 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2567}
2568
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002569/* This functions consumes and returns one line. If the channel is closed,
2570 * and the last data does not contains a final '\n', the data are returned
2571 * without the final '\n'. When no more data are avalaible, it returns nil
2572 * value.
2573 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002574__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002575{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002576 char *blk1;
2577 char *blk2;
2578 int len1;
2579 int len2;
2580 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002581 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002582 int ret;
2583 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002584
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002585 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2586
Willy Tarreau47860ed2015-03-10 14:07:50 +01002587 ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002588 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002589 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002590
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002591 if (ret == -1) {
2592 lua_pushnil(L);
2593 return 1;
2594 }
2595
2596 luaL_buffinit(L, &b);
2597 luaL_addlstring(&b, blk1, len1);
2598 len = len1;
2599 if (unlikely(ret == 2)) {
2600 luaL_addlstring(&b, blk2, len2);
2601 len += len2;
2602 }
2603 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002604 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002605 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002606 return 1;
2607}
2608
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002609/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2610__LJMP static int hlua_channel_getline(lua_State *L)
2611{
2612 MAY_LJMP(check_args(L, 1, "getline"));
2613 MAY_LJMP(hlua_checkchannel(L, 1));
2614 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2615}
2616
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002617/* This function takes a string as input, and append it at the
2618 * input side of channel. If the data is too big, but a space
2619 * is probably available after sending some data, the function
2620 * yield. If the data is bigger than the buffer, or if the
2621 * channel is closed, it returns -1. otherwise, it returns the
2622 * amount of data writed.
2623 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002624__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002625{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002626 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002627 size_t len;
2628 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2629 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2630 int ret;
2631 int max;
2632
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002633 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2634 * the request buffer if its not required.
2635 */
2636 if (chn->buf->size == 0) {
2637 si_applet_cant_put(chn_prod(chn));
2638 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
2639 }
2640
Willy Tarreau47860ed2015-03-10 14:07:50 +01002641 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002642 if (max > len - l)
2643 max = len - l;
2644
Willy Tarreau47860ed2015-03-10 14:07:50 +01002645 ret = bi_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002646 if (ret == -2 || ret == -3) {
2647 lua_pushinteger(L, -1);
2648 return 1;
2649 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002650 if (ret == -1) {
2651 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002652 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002653 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002654 l += ret;
2655 lua_pop(L, 1);
2656 lua_pushinteger(L, l);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002657 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002658
Willy Tarreau47860ed2015-03-10 14:07:50 +01002659 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2660 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002661 /* There are no space avalaible, and the output buffer is empty.
2662 * in this case, we cannot add more data, so we cannot yield,
2663 * we return the amount of copyied data.
2664 */
2665 return 1;
2666 }
2667 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002668 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002669 return 1;
2670}
2671
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002672/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002673 * of the writed string, or -1 if the channel is closed or if the
2674 * buffer size is too little for the data.
2675 */
2676__LJMP static int hlua_channel_append(lua_State *L)
2677{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002678 size_t len;
2679
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002680 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002681 MAY_LJMP(hlua_checkchannel(L, 1));
2682 MAY_LJMP(luaL_checklstring(L, 2, &len));
2683 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002684 lua_pushinteger(L, 0);
2685
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002686 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002687}
2688
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002689/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002690 * his process by cleaning the buffer. The result is a replacement
2691 * of the current data. It returns the length of the writed string,
2692 * or -1 if the channel is closed or if the buffer size is too
2693 * little for the data.
2694 */
2695__LJMP static int hlua_channel_set(lua_State *L)
2696{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002697 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002698
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002699 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002700 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002701 lua_pushinteger(L, 0);
2702
Willy Tarreau47860ed2015-03-10 14:07:50 +01002703 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002704
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002705 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002706}
2707
2708/* Append data in the output side of the buffer. This data is immediatly
2709 * sent. The fcuntion returns the ammount of data writed. If the buffer
2710 * cannot contains the data, the function yield. The function returns -1
2711 * if the channel is closed.
2712 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002713__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002714{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002715 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002716 size_t len;
2717 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2718 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2719 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002720 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002721
Willy Tarreau47860ed2015-03-10 14:07:50 +01002722 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002723 lua_pushinteger(L, -1);
2724 return 1;
2725 }
2726
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002727 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2728 * the request buffer if its not required.
2729 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002730 if (chn->buf->size == 0) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002731 si_applet_cant_put(chn_prod(chn));
2732 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01002733 }
2734
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002735 /* the writed data will be immediatly sent, so we can check
2736 * the avalaible space without taking in account the reserve.
2737 * The reserve is guaranted for the processing of incoming
2738 * data, because the buffer will be flushed.
2739 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002740 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002741
2742 /* If there are no space avalaible, and the output buffer is empty.
2743 * in this case, we cannot add more data, so we cannot yield,
2744 * we return the amount of copyied data.
2745 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002746 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002747 return 1;
2748
2749 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002750 if (max > len - l)
2751 max = len - l;
2752
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002753 /* The buffer avalaible size may be not contiguous. This test
2754 * detects a non contiguous buffer and realign it.
2755 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002756 if (bi_space_for_replace(chn->buf) < max)
2757 buffer_slow_realign(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002758
2759 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002760 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002761
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002762 /* buffer replace considers that the input part is filled.
2763 * so, I must forward these new data in the output part.
2764 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002765 b_adv(chn->buf, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002766
2767 l += max;
2768 lua_pop(L, 1);
2769 lua_pushinteger(L, l);
2770
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002771 /* If there are no space avalaible, and the output buffer is empty.
2772 * in this case, we cannot add more data, so we cannot yield,
2773 * we return the amount of copyied data.
2774 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002775 max = chn->buf->size - buffer_len(chn->buf);
2776 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002777 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01002778
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002779 if (l < len) {
2780 /* If we are waiting for space in the response buffer, we
2781 * must set the flag WAKERESWR. This flag required the task
2782 * wake up if any activity is detected on the response buffer.
2783 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002784 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002785 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002786 else
2787 HLUA_SET_WAKEREQWR(hlua);
2788 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002789 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002790
2791 return 1;
2792}
2793
2794/* Just a wraper of "_hlua_channel_send". This wrapper permits
2795 * yield the LUA process, and resume it without checking the
2796 * input arguments.
2797 */
2798__LJMP static int hlua_channel_send(lua_State *L)
2799{
2800 MAY_LJMP(check_args(L, 2, "send"));
2801 lua_pushinteger(L, 0);
2802
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002803 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002804}
2805
2806/* This function forward and amount of butes. The data pass from
2807 * the input side of the buffer to the output side, and can be
2808 * forwarded. This function never fails.
2809 *
2810 * The Lua function takes an amount of bytes to be forwarded in
2811 * imput. It returns the number of bytes forwarded.
2812 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002813__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002814{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002815 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002816 int len;
2817 int l;
2818 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002819 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002820
2821 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2822 len = MAY_LJMP(luaL_checkinteger(L, 2));
2823 l = MAY_LJMP(luaL_checkinteger(L, -1));
2824
2825 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002826 if (max > chn->buf->i)
2827 max = chn->buf->i;
2828 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002829 l += max;
2830
2831 lua_pop(L, 1);
2832 lua_pushinteger(L, l);
2833
2834 /* Check if it miss bytes to forward. */
2835 if (l < len) {
2836 /* The the input channel or the output channel are closed, we
2837 * must return the amount of data forwarded.
2838 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002839 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002840 return 1;
2841
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002842 /* If we are waiting for space data in the response buffer, we
2843 * must set the flag WAKERESWR. This flag required the task
2844 * wake up if any activity is detected on the response buffer.
2845 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002846 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002847 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01002848 else
2849 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01002850
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002851 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002852 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002853 }
2854
2855 return 1;
2856}
2857
2858/* Just check the input and prepare the stack for the previous
2859 * function "hlua_channel_forward_yield"
2860 */
2861__LJMP static int hlua_channel_forward(lua_State *L)
2862{
2863 MAY_LJMP(check_args(L, 2, "forward"));
2864 MAY_LJMP(hlua_checkchannel(L, 1));
2865 MAY_LJMP(luaL_checkinteger(L, 2));
2866
2867 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002868 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002869}
2870
2871/* Just returns the number of bytes available in the input
2872 * side of the buffer. This function never fails.
2873 */
2874__LJMP static int hlua_channel_get_in_len(lua_State *L)
2875{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002876 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002877
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002878 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002879 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002880 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002881 return 1;
2882}
2883
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002884/* Returns true if the channel is full. */
2885__LJMP static int hlua_channel_is_full(lua_State *L)
2886{
2887 struct channel *chn;
2888 int rem;
2889
2890 MAY_LJMP(check_args(L, 1, "is_full"));
2891 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2892
2893 rem = chn->buf->size;
2894 rem -= chn->buf->o; /* Output size */
2895 rem -= chn->buf->i; /* Input size */
2896 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
2897
2898 lua_pushboolean(L, rem <= 0);
2899 return 1;
2900}
2901
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002902/* Just returns the number of bytes available in the output
2903 * side of the buffer. This function never fails.
2904 */
2905__LJMP static int hlua_channel_get_out_len(lua_State *L)
2906{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002907 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002908
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002909 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002910 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01002911 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912 return 1;
2913}
2914
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002915/*
2916 *
2917 *
2918 * Class Fetches
2919 *
2920 *
2921 */
2922
2923/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02002924 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002925 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01002926__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002927{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002928 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002929}
2930
2931/* This function creates and push in the stack a fetch object according
2932 * with a current TXN.
2933 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01002934static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002935{
Willy Tarreau7073c472015-04-06 11:15:40 +02002936 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002937
2938 /* Check stack size. */
2939 if (!lua_checkstack(L, 3))
2940 return 0;
2941
2942 /* Create the object: obj[0] = userdata.
2943 * Note that the base of the Fetches object is the
2944 * transaction object.
2945 */
2946 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02002947 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002948 lua_rawseti(L, -2, 0);
2949
Willy Tarreau7073c472015-04-06 11:15:40 +02002950 hsmp->s = txn->s;
2951 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01002952 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01002953 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002954
2955 /* Pop a class sesison metatable and affect it to the userdata. */
2956 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
2957 lua_setmetatable(L, -2);
2958
2959 return 1;
2960}
2961
2962/* This function is an LUA binding. It is called with each sample-fetch.
2963 * It uses closure argument to store the associated sample-fetch. It
2964 * returns only one argument or throws an error. An error is thrown
2965 * only if an error is encountered during the argument parsing. If
2966 * the "sample-fetch" function fails, nil is returned.
2967 */
2968__LJMP static int hlua_run_sample_fetch(lua_State *L)
2969{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002970 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01002971 struct sample_fetch *f;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002972 struct arg args[ARGM_NBARGS + 1];
2973 int i;
2974 struct sample smp;
2975
2976 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002977 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002978
2979 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02002980 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002981
Thierry FOURNIERca988662015-12-20 18:43:03 +01002982 /* Check execution authorization. */
2983 if (f->use & SMP_USE_HTTP_ANY &&
2984 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
2985 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
2986 "is not available in Lua services", f->kw);
2987 WILL_LJMP(lua_error(L));
2988 }
2989
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002990 /* Get extra arguments. */
2991 for (i = 0; i < lua_gettop(L) - 1; i++) {
2992 if (i >= ARGM_NBARGS)
2993 break;
2994 hlua_lua2arg(L, i + 2, &args[i]);
2995 }
2996 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01002997 args[i].data.str.str = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01002998
2999 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003000 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003001
3002 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003003 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003004 lua_pushfstring(L, "error in arguments");
3005 WILL_LJMP(lua_error(L));
3006 }
3007
3008 /* Initialise the sample. */
3009 memset(&smp, 0, sizeof(smp));
3010
3011 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003012 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003013 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003014 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003015 lua_pushstring(L, "");
3016 else
3017 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003018 return 1;
3019 }
3020
3021 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003022 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003023 hlua_smp2lua_str(L, &smp);
3024 else
3025 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003026 return 1;
3027}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003028
3029/*
3030 *
3031 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003032 * Class Converters
3033 *
3034 *
3035 */
3036
3037/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003038 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003039 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003040__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003041{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003042 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003043}
3044
3045/* This function creates and push in the stack a Converters object
3046 * according with a current TXN.
3047 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003048static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003049{
Willy Tarreau7073c472015-04-06 11:15:40 +02003050 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003051
3052 /* Check stack size. */
3053 if (!lua_checkstack(L, 3))
3054 return 0;
3055
3056 /* Create the object: obj[0] = userdata.
3057 * Note that the base of the Converters object is the
3058 * same than the TXN object.
3059 */
3060 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003061 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003062 lua_rawseti(L, -2, 0);
3063
Willy Tarreau7073c472015-04-06 11:15:40 +02003064 hsmp->s = txn->s;
3065 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003066 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003067 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003068
Willy Tarreau87b09662015-04-03 00:22:06 +02003069 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003070 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3071 lua_setmetatable(L, -2);
3072
3073 return 1;
3074}
3075
3076/* This function is an LUA binding. It is called with each converter.
3077 * It uses closure argument to store the associated converter. It
3078 * returns only one argument or throws an error. An error is thrown
3079 * only if an error is encountered during the argument parsing. If
3080 * the converter function function fails, nil is returned.
3081 */
3082__LJMP static int hlua_run_sample_conv(lua_State *L)
3083{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003084 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003085 struct sample_conv *conv;
3086 struct arg args[ARGM_NBARGS + 1];
3087 int i;
3088 struct sample smp;
3089
3090 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003091 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003092
3093 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003094 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003095
3096 /* Get extra arguments. */
3097 for (i = 0; i < lua_gettop(L) - 2; i++) {
3098 if (i >= ARGM_NBARGS)
3099 break;
3100 hlua_lua2arg(L, i + 3, &args[i]);
3101 }
3102 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003103 args[i].data.str.str = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003104
3105 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003106 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003107
3108 /* Run the special args checker. */
3109 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3110 hlua_pusherror(L, "error in arguments");
3111 WILL_LJMP(lua_error(L));
3112 }
3113
3114 /* Initialise the sample. */
3115 if (!hlua_lua2smp(L, 2, &smp)) {
3116 hlua_pusherror(L, "error in the input argument");
3117 WILL_LJMP(lua_error(L));
3118 }
3119
Willy Tarreau1777ea62016-03-10 16:15:46 +01003120 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3121
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003122 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003123 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003124 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003125 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003126 WILL_LJMP(lua_error(L));
3127 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003128 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3129 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003130 hlua_pusherror(L, "error during the input argument casting");
3131 WILL_LJMP(lua_error(L));
3132 }
3133
3134 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003135 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003136 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003137 lua_pushstring(L, "");
3138 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003139 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003140 return 1;
3141 }
3142
3143 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003144 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003145 hlua_smp2lua_str(L, &smp);
3146 else
3147 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003148 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003149}
3150
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003151/*
3152 *
3153 *
3154 * Class AppletTCP
3155 *
3156 *
3157 */
3158
3159/* Returns a struct hlua_txn if the stack entry "ud" is
3160 * a class stream, otherwise it throws an error.
3161 */
3162__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3163{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003164 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003165}
3166
3167/* This function creates and push in the stack an Applet object
3168 * according with a current TXN.
3169 */
3170static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3171{
3172 struct hlua_appctx *appctx;
3173 struct stream_interface *si = ctx->owner;
3174 struct stream *s = si_strm(si);
3175 struct proxy *p = s->be;
3176
3177 /* Check stack size. */
3178 if (!lua_checkstack(L, 3))
3179 return 0;
3180
3181 /* Create the object: obj[0] = userdata.
3182 * Note that the base of the Converters object is the
3183 * same than the TXN object.
3184 */
3185 lua_newtable(L);
3186 appctx = lua_newuserdata(L, sizeof(*appctx));
3187 lua_rawseti(L, -2, 0);
3188 appctx->appctx = ctx;
3189 appctx->htxn.s = s;
3190 appctx->htxn.p = p;
3191
3192 /* Create the "f" field that contains a list of fetches. */
3193 lua_pushstring(L, "f");
3194 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3195 return 0;
3196 lua_settable(L, -3);
3197
3198 /* Create the "sf" field that contains a list of stringsafe fetches. */
3199 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003200 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003201 return 0;
3202 lua_settable(L, -3);
3203
3204 /* Create the "c" field that contains a list of converters. */
3205 lua_pushstring(L, "c");
3206 if (!hlua_converters_new(L, &appctx->htxn, 0))
3207 return 0;
3208 lua_settable(L, -3);
3209
3210 /* Create the "sc" field that contains a list of stringsafe converters. */
3211 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003212 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003213 return 0;
3214 lua_settable(L, -3);
3215
3216 /* Pop a class stream metatable and affect it to the table. */
3217 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3218 lua_setmetatable(L, -2);
3219
3220 return 1;
3221}
3222
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003223__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3224{
3225 struct hlua_appctx *appctx;
3226 struct stream *s;
3227 const char *name;
3228 size_t len;
3229 struct sample smp;
3230
3231 MAY_LJMP(check_args(L, 3, "set_var"));
3232
3233 /* It is useles to retrieve the stream, but this function
3234 * runs only in a stream context.
3235 */
3236 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3237 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3238 s = appctx->htxn.s;
3239
3240 /* Converts the third argument in a sample. */
3241 hlua_lua2smp(L, 3, &smp);
3242
3243 /* Store the sample in a variable. */
3244 smp_set_owner(&smp, s->be, s->sess, s, 0);
3245 vars_set_by_name(name, len, &smp);
3246 return 0;
3247}
3248
3249__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3250{
3251 struct hlua_appctx *appctx;
3252 struct stream *s;
3253 const char *name;
3254 size_t len;
3255 struct sample smp;
3256
3257 MAY_LJMP(check_args(L, 2, "unset_var"));
3258
3259 /* It is useles to retrieve the stream, but this function
3260 * runs only in a stream context.
3261 */
3262 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3263 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3264 s = appctx->htxn.s;
3265
3266 /* Unset the variable. */
3267 smp_set_owner(&smp, s->be, s->sess, s, 0);
3268 vars_unset_by_name(name, len, &smp);
3269 return 0;
3270}
3271
3272__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3273{
3274 struct hlua_appctx *appctx;
3275 struct stream *s;
3276 const char *name;
3277 size_t len;
3278 struct sample smp;
3279
3280 MAY_LJMP(check_args(L, 2, "get_var"));
3281
3282 /* It is useles to retrieve the stream, but this function
3283 * runs only in a stream context.
3284 */
3285 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3286 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3287 s = appctx->htxn.s;
3288
3289 smp_set_owner(&smp, s->be, s->sess, s, 0);
3290 if (!vars_get_by_name(name, len, &smp)) {
3291 lua_pushnil(L);
3292 return 1;
3293 }
3294
3295 return hlua_smp2lua(L, &smp);
3296}
3297
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003298__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3299{
3300 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3301 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003302 struct hlua *hlua;
3303
3304 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003305 if (!s->hlua)
3306 return 0;
3307 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003308
3309 MAY_LJMP(check_args(L, 2, "set_priv"));
3310
3311 /* Remove previous value. */
3312 if (hlua->Mref != -1)
3313 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
3314
3315 /* Get and store new value. */
3316 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3317 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3318
3319 return 0;
3320}
3321
3322__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3323{
3324 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3325 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003326 struct hlua *hlua;
3327
3328 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003329 if (!s->hlua) {
3330 lua_pushnil(L);
3331 return 1;
3332 }
3333 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003334
3335 /* Push configuration index in the stack. */
3336 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3337
3338 return 1;
3339}
3340
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003341/* If expected data not yet available, it returns a yield. This function
3342 * consumes the data in the buffer. It returns a string containing the
3343 * data. This string can be empty.
3344 */
3345__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3346{
3347 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3348 struct stream_interface *si = appctx->appctx->owner;
3349 int ret;
3350 char *blk1;
3351 int len1;
3352 char *blk2;
3353 int len2;
3354
3355 /* Read the maximum amount of data avalaible. */
3356 ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
3357
3358 /* Data not yet avalaible. return yield. */
3359 if (ret == 0) {
3360 si_applet_cant_get(si);
3361 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
3362 }
3363
3364 /* End of data: commit the total strings and return. */
3365 if (ret < 0) {
3366 luaL_pushresult(&appctx->b);
3367 return 1;
3368 }
3369
3370 /* Ensure that the block 2 length is usable. */
3371 if (ret == 1)
3372 len2 = 0;
3373
3374 /* dont check the max length read and dont check. */
3375 luaL_addlstring(&appctx->b, blk1, len1);
3376 luaL_addlstring(&appctx->b, blk2, len2);
3377
3378 /* Consume input channel output buffer data. */
3379 bo_skip(si_oc(si), len1 + len2);
3380 luaL_pushresult(&appctx->b);
3381 return 1;
3382}
3383
3384/* Check arguments for the fucntion "hlua_channel_get_yield". */
3385__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3386{
3387 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3388
3389 /* Initialise the string catenation. */
3390 luaL_buffinit(L, &appctx->b);
3391
3392 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3393}
3394
3395/* If expected data not yet available, it returns a yield. This function
3396 * consumes the data in the buffer. It returns a string containing the
3397 * data. This string can be empty.
3398 */
3399__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3400{
3401 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3402 struct stream_interface *si = appctx->appctx->owner;
3403 int len = MAY_LJMP(luaL_checkinteger(L, 2));
3404 int ret;
3405 char *blk1;
3406 int len1;
3407 char *blk2;
3408 int len2;
3409
3410 /* Read the maximum amount of data avalaible. */
3411 ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
3412
3413 /* Data not yet avalaible. return yield. */
3414 if (ret == 0) {
3415 si_applet_cant_get(si);
3416 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3417 }
3418
3419 /* End of data: commit the total strings and return. */
3420 if (ret < 0) {
3421 luaL_pushresult(&appctx->b);
3422 return 1;
3423 }
3424
3425 /* Ensure that the block 2 length is usable. */
3426 if (ret == 1)
3427 len2 = 0;
3428
3429 if (len == -1) {
3430
3431 /* If len == -1, catenate all the data avalaile and
3432 * yield because we want to get all the data until
3433 * the end of data stream.
3434 */
3435 luaL_addlstring(&appctx->b, blk1, len1);
3436 luaL_addlstring(&appctx->b, blk2, len2);
3437 bo_skip(si_oc(si), len1 + len2);
3438 si_applet_cant_get(si);
3439 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3440
3441 } else {
3442
3443 /* Copy the fisrt block caping to the length required. */
3444 if (len1 > len)
3445 len1 = len;
3446 luaL_addlstring(&appctx->b, blk1, len1);
3447 len -= len1;
3448
3449 /* Copy the second block. */
3450 if (len2 > len)
3451 len2 = len;
3452 luaL_addlstring(&appctx->b, blk2, len2);
3453 len -= len2;
3454
3455 /* Consume input channel output buffer data. */
3456 bo_skip(si_oc(si), len1 + len2);
3457
3458 /* If we are no other data avalaible, yield waiting for new data. */
3459 if (len > 0) {
3460 lua_pushinteger(L, len);
3461 lua_replace(L, 2);
3462 si_applet_cant_get(si);
3463 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3464 }
3465
3466 /* return the result. */
3467 luaL_pushresult(&appctx->b);
3468 return 1;
3469 }
3470
3471 /* we never executes this */
3472 hlua_pusherror(L, "Lua: internal error");
3473 WILL_LJMP(lua_error(L));
3474 return 0;
3475}
3476
3477/* Check arguments for the fucntion "hlua_channel_get_yield". */
3478__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3479{
3480 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3481 int len = -1;
3482
3483 if (lua_gettop(L) > 2)
3484 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3485 if (lua_gettop(L) >= 2) {
3486 len = MAY_LJMP(luaL_checkinteger(L, 2));
3487 lua_pop(L, 1);
3488 }
3489
3490 /* Confirm or set the required length */
3491 lua_pushinteger(L, len);
3492
3493 /* Initialise the string catenation. */
3494 luaL_buffinit(L, &appctx->b);
3495
3496 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3497}
3498
3499/* Append data in the output side of the buffer. This data is immediatly
3500 * sent. The fcuntion returns the ammount of data writed. If the buffer
3501 * cannot contains the data, the function yield. The function returns -1
3502 * if the channel is closed.
3503 */
3504__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3505{
3506 size_t len;
3507 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3508 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3509 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3510 struct stream_interface *si = appctx->appctx->owner;
3511 struct channel *chn = si_ic(si);
3512 int max;
3513
3514 /* Get the max amount of data which can write as input in the channel. */
3515 max = channel_recv_max(chn);
3516 if (max > (len - l))
3517 max = len - l;
3518
3519 /* Copy data. */
3520 bi_putblk(chn, str + l, max);
3521
3522 /* update counters. */
3523 l += max;
3524 lua_pop(L, 1);
3525 lua_pushinteger(L, l);
3526
3527 /* If some data is not send, declares the situation to the
3528 * applet, and returns a yield.
3529 */
3530 if (l < len) {
3531 si_applet_cant_put(si);
3532 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
3533 }
3534
3535 return 1;
3536}
3537
3538/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3539 * yield the LUA process, and resume it without checking the
3540 * input arguments.
3541 */
3542__LJMP static int hlua_applet_tcp_send(lua_State *L)
3543{
3544 MAY_LJMP(check_args(L, 2, "send"));
3545 lua_pushinteger(L, 0);
3546
3547 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3548}
3549
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003550/*
3551 *
3552 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003553 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003554 *
3555 *
3556 */
3557
3558/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003559 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003560 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003561__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003562{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003563 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003564}
3565
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003566/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003567 * according with a current TXN.
3568 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003569static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003570{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003571 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003572 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003573 struct stream_interface *si = ctx->owner;
3574 struct stream *s = si_strm(si);
3575 struct proxy *px = s->be;
3576 struct http_txn *txn = s->txn;
3577 const char *path;
3578 const char *end;
3579 const char *p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003580
3581 /* Check stack size. */
3582 if (!lua_checkstack(L, 3))
3583 return 0;
3584
3585 /* Create the object: obj[0] = userdata.
3586 * Note that the base of the Converters object is the
3587 * same than the TXN object.
3588 */
3589 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003590 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003591 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003592 appctx->appctx = ctx;
3593 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
3594 appctx->htxn.s = s;
3595 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003596
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003597 /* Create the "f" field that contains a list of fetches. */
3598 lua_pushstring(L, "f");
3599 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3600 return 0;
3601 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003602
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003603 /* Create the "sf" field that contains a list of stringsafe fetches. */
3604 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003605 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003606 return 0;
3607 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003608
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003609 /* Create the "c" field that contains a list of converters. */
3610 lua_pushstring(L, "c");
3611 if (!hlua_converters_new(L, &appctx->htxn, 0))
3612 return 0;
3613 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003614
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003615 /* Create the "sc" field that contains a list of stringsafe converters. */
3616 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003617 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003618 return 0;
3619 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003620
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003621 /* Stores the request method. */
3622 lua_pushstring(L, "method");
3623 lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l);
3624 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003625
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003626 /* Stores the http version. */
3627 lua_pushstring(L, "version");
3628 lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
3629 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003630
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003631 /* creates an array of headers. hlua_http_get_headers() crates and push
3632 * the array on the top of the stack.
3633 */
3634 lua_pushstring(L, "headers");
3635 htxn.s = s;
3636 htxn.p = px;
3637 htxn.dir = SMP_OPT_DIR_REQ;
3638 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3639 return 0;
3640 lua_settable(L, -3);
3641
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003642 /* Get path and qs */
3643 path = http_get_path(txn);
3644 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
3645 p = path;
3646 while (p < end && *p != '?')
3647 p++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003648
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003649 /* Stores the request path. */
3650 lua_pushstring(L, "path");
3651 lua_pushlstring(L, path, p - path);
3652 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003653
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003654 /* Stores the query string. */
3655 lua_pushstring(L, "qs");
3656 if (*p == '?')
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003657 p++;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003658 lua_pushlstring(L, p, end - p);
3659 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003660
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003661 /* Stores the request path. */
3662 lua_pushstring(L, "length");
3663 lua_pushinteger(L, txn->req.body_len);
3664 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003665
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003666 /* Create an array of HTTP request headers. */
3667 lua_pushstring(L, "headers");
3668 MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req));
3669 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003670
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003671 /* Create an empty array of HTTP request headers. */
3672 lua_pushstring(L, "response");
3673 lua_newtable(L);
3674 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003675
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003676 /* Pop a class stream metatable and affect it to the table. */
3677 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3678 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003679
3680 return 1;
3681}
3682
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003683__LJMP static int hlua_applet_http_set_var(lua_State *L)
3684{
3685 struct hlua_appctx *appctx;
3686 struct stream *s;
3687 const char *name;
3688 size_t len;
3689 struct sample smp;
3690
3691 MAY_LJMP(check_args(L, 3, "set_var"));
3692
3693 /* It is useles to retrieve the stream, but this function
3694 * runs only in a stream context.
3695 */
3696 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3697 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3698 s = appctx->htxn.s;
3699
3700 /* Converts the third argument in a sample. */
3701 hlua_lua2smp(L, 3, &smp);
3702
3703 /* Store the sample in a variable. */
3704 smp_set_owner(&smp, s->be, s->sess, s, 0);
3705 vars_set_by_name(name, len, &smp);
3706 return 0;
3707}
3708
3709__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3710{
3711 struct hlua_appctx *appctx;
3712 struct stream *s;
3713 const char *name;
3714 size_t len;
3715 struct sample smp;
3716
3717 MAY_LJMP(check_args(L, 2, "unset_var"));
3718
3719 /* It is useles to retrieve the stream, but this function
3720 * runs only in a stream context.
3721 */
3722 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3723 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3724 s = appctx->htxn.s;
3725
3726 /* Unset the variable. */
3727 smp_set_owner(&smp, s->be, s->sess, s, 0);
3728 vars_unset_by_name(name, len, &smp);
3729 return 0;
3730}
3731
3732__LJMP static int hlua_applet_http_get_var(lua_State *L)
3733{
3734 struct hlua_appctx *appctx;
3735 struct stream *s;
3736 const char *name;
3737 size_t len;
3738 struct sample smp;
3739
3740 MAY_LJMP(check_args(L, 2, "get_var"));
3741
3742 /* It is useles to retrieve the stream, but this function
3743 * runs only in a stream context.
3744 */
3745 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3746 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3747 s = appctx->htxn.s;
3748
3749 smp_set_owner(&smp, s->be, s->sess, s, 0);
3750 if (!vars_get_by_name(name, len, &smp)) {
3751 lua_pushnil(L);
3752 return 1;
3753 }
3754
3755 return hlua_smp2lua(L, &smp);
3756}
3757
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003758__LJMP static int hlua_applet_http_set_priv(lua_State *L)
3759{
3760 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3761 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003762 struct hlua *hlua;
3763
3764 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003765 if (!s->hlua)
3766 return 0;
3767 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003768
3769 MAY_LJMP(check_args(L, 2, "set_priv"));
3770
3771 /* Remove previous value. */
3772 if (hlua->Mref != -1)
3773 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
3774
3775 /* Get and store new value. */
3776 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3777 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3778
3779 return 0;
3780}
3781
3782__LJMP static int hlua_applet_http_get_priv(lua_State *L)
3783{
3784 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3785 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003786 struct hlua *hlua;
3787
3788 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003789 if (!s->hlua) {
3790 lua_pushnil(L);
3791 return 1;
3792 }
3793 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003794
3795 /* Push configuration index in the stack. */
3796 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3797
3798 return 1;
3799}
3800
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003801/* If expected data not yet available, it returns a yield. This function
3802 * consumes the data in the buffer. It returns a string containing the
3803 * data. This string can be empty.
3804 */
3805__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003806{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003807 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3808 struct stream_interface *si = appctx->appctx->owner;
3809 struct channel *chn = si_ic(si);
3810 int ret;
3811 char *blk1;
3812 int len1;
3813 char *blk2;
3814 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003815
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003816 /* Maybe we cant send a 100-continue ? */
3817 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
3818 ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C));
3819 /* if ret == -2 or -3 the channel closed or the message si too
3820 * big for the buffers. We cant send anything. So, we ignoring
3821 * the error, considers that the 100-continue is sent, and try
3822 * to receive.
3823 * If ret is -1, we dont have room in the buffer, so we yield.
3824 */
3825 if (ret == -1) {
3826 si_applet_cant_put(si);
3827 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
3828 }
3829 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
3830 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003831
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003832 /* Check for the end of the data. */
3833 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
3834 luaL_pushresult(&appctx->b);
3835 return 1;
3836 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003837
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003838 /* Read the maximum amount of data avalaible. */
3839 ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003840
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003841 /* Data not yet avalaible. return yield. */
3842 if (ret == 0) {
3843 si_applet_cant_get(si);
3844 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
3845 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003846
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003847 /* End of data: commit the total strings and return. */
3848 if (ret < 0) {
3849 luaL_pushresult(&appctx->b);
3850 return 1;
3851 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003852
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003853 /* Ensure that the block 2 length is usable. */
3854 if (ret == 1)
3855 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003856
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003857 /* Copy the fisrt block caping to the length required. */
3858 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
3859 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
3860 luaL_addlstring(&appctx->b, blk1, len1);
3861 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003862
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003863 /* Copy the second block. */
3864 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
3865 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
3866 luaL_addlstring(&appctx->b, blk2, len2);
3867 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003868
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003869 /* Consume input channel output buffer data. */
3870 bo_skip(si_oc(si), len1 + len2);
3871 luaL_pushresult(&appctx->b);
3872 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003873}
3874
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003875/* Check arguments for the fucntion "hlua_channel_get_yield". */
3876__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003877{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003878 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003879
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003880 /* Initialise the string catenation. */
3881 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003882
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003883 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003884}
3885
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003886/* If expected data not yet available, it returns a yield. This function
3887 * consumes the data in the buffer. It returns a string containing the
3888 * data. This string can be empty.
3889 */
3890__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003891{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003892 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3893 struct stream_interface *si = appctx->appctx->owner;
3894 int len = MAY_LJMP(luaL_checkinteger(L, 2));
3895 struct channel *chn = si_ic(si);
3896 int ret;
3897 char *blk1;
3898 int len1;
3899 char *blk2;
3900 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003901
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003902 /* Maybe we cant send a 100-continue ? */
3903 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
3904 ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C));
3905 /* if ret == -2 or -3 the channel closed or the message si too
3906 * big for the buffers. We cant send anything. So, we ignoring
3907 * the error, considers that the 100-continue is sent, and try
3908 * to receive.
3909 * If ret is -1, we dont have room in the buffer, so we yield.
3910 */
3911 if (ret == -1) {
3912 si_applet_cant_put(si);
3913 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
3914 }
3915 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
3916 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003917
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003918 /* Read the maximum amount of data avalaible. */
3919 ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003920
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003921 /* Data not yet avalaible. return yield. */
3922 if (ret == 0) {
3923 si_applet_cant_get(si);
3924 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
3925 }
3926
3927 /* End of data: commit the total strings and return. */
3928 if (ret < 0) {
3929 luaL_pushresult(&appctx->b);
3930 return 1;
3931 }
3932
3933 /* Ensure that the block 2 length is usable. */
3934 if (ret == 1)
3935 len2 = 0;
3936
3937 /* Copy the fisrt block caping to the length required. */
3938 if (len1 > len)
3939 len1 = len;
3940 luaL_addlstring(&appctx->b, blk1, len1);
3941 len -= len1;
3942
3943 /* Copy the second block. */
3944 if (len2 > len)
3945 len2 = len;
3946 luaL_addlstring(&appctx->b, blk2, len2);
3947 len -= len2;
3948
3949 /* Consume input channel output buffer data. */
3950 bo_skip(si_oc(si), len1 + len2);
3951 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
3952 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
3953
3954 /* If we are no other data avalaible, yield waiting for new data. */
3955 if (len > 0) {
3956 lua_pushinteger(L, len);
3957 lua_replace(L, 2);
3958 si_applet_cant_get(si);
3959 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
3960 }
3961
3962 /* return the result. */
3963 luaL_pushresult(&appctx->b);
3964 return 1;
3965}
3966
3967/* Check arguments for the fucntion "hlua_channel_get_yield". */
3968__LJMP static int hlua_applet_http_recv(lua_State *L)
3969{
3970 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3971 int len = -1;
3972
3973 /* Check arguments. */
3974 if (lua_gettop(L) > 2)
3975 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3976 if (lua_gettop(L) >= 2) {
3977 len = MAY_LJMP(luaL_checkinteger(L, 2));
3978 lua_pop(L, 1);
3979 }
3980
3981 /* Check the required length */
3982 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
3983 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
3984 lua_pushinteger(L, len);
3985
3986 /* Initialise the string catenation. */
3987 luaL_buffinit(L, &appctx->b);
3988
3989 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
3990}
3991
3992/* Append data in the output side of the buffer. This data is immediatly
3993 * sent. The fcuntion returns the ammount of data writed. If the buffer
3994 * cannot contains the data, the function yield. The function returns -1
3995 * if the channel is closed.
3996 */
3997__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
3998{
3999 size_t len;
4000 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4001 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4002 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4003 struct stream_interface *si = appctx->appctx->owner;
4004 struct channel *chn = si_ic(si);
4005 int max;
4006
4007 /* Get the max amount of data which can write as input in the channel. */
4008 max = channel_recv_max(chn);
4009 if (max > (len - l))
4010 max = len - l;
4011
4012 /* Copy data. */
4013 bi_putblk(chn, str + l, max);
4014
4015 /* update counters. */
4016 l += max;
4017 lua_pop(L, 1);
4018 lua_pushinteger(L, l);
4019
4020 /* If some data is not send, declares the situation to the
4021 * applet, and returns a yield.
4022 */
4023 if (l < len) {
4024 si_applet_cant_put(si);
4025 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
4026 }
4027
4028 return 1;
4029}
4030
4031/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4032 * yield the LUA process, and resume it without checking the
4033 * input arguments.
4034 */
4035__LJMP static int hlua_applet_http_send(lua_State *L)
4036{
4037 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4038 size_t len;
4039 char hex[10];
4040
4041 MAY_LJMP(luaL_checklstring(L, 2, &len));
4042
4043 /* If transfer encoding chunked is selected, we surround the data
4044 * by chunk data.
4045 */
4046 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4047 snprintf(hex, 9, "%x", (unsigned int)len);
4048 lua_pushfstring(L, "%s\r\n", hex);
4049 lua_insert(L, 2); /* swap the last 2 entries. */
4050 lua_pushstring(L, "\r\n");
4051 lua_concat(L, 3);
4052 }
4053
4054 /* This interger is used for followinf the amount of data sent. */
4055 lua_pushinteger(L, 0);
4056
4057 /* We want to send some data. Headers must be sent. */
4058 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4059 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4060 WILL_LJMP(lua_error(L));
4061 }
4062
4063 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4064}
4065
4066__LJMP static int hlua_applet_http_addheader(lua_State *L)
4067{
4068 const char *name;
4069 int ret;
4070
4071 MAY_LJMP(hlua_checkapplet_http(L, 1));
4072 name = MAY_LJMP(luaL_checkstring(L, 2));
4073 MAY_LJMP(luaL_checkstring(L, 3));
4074
4075 /* Push in the stack the "response" entry. */
4076 ret = lua_getfield(L, 1, "response");
4077 if (ret != LUA_TTABLE) {
4078 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4079 "is expected as an array. %s found", lua_typename(L, ret));
4080 WILL_LJMP(lua_error(L));
4081 }
4082
4083 /* check if the header is already registered if it is not
4084 * the case, register it.
4085 */
4086 ret = lua_getfield(L, -1, name);
4087 if (ret == LUA_TNIL) {
4088
4089 /* Entry not found. */
4090 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4091
4092 /* Insert the new header name in the array in the top of the stack.
4093 * It left the new array in the top of the stack.
4094 */
4095 lua_newtable(L);
4096 lua_pushvalue(L, 2);
4097 lua_pushvalue(L, -2);
4098 lua_settable(L, -4);
4099
4100 } else if (ret != LUA_TTABLE) {
4101
4102 /* corruption error. */
4103 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4104 "is expected as an array. %s found", name, lua_typename(L, ret));
4105 WILL_LJMP(lua_error(L));
4106 }
4107
4108 /* Now the top od thestack is an array of values. We push
4109 * the header value as new entry.
4110 */
4111 lua_pushvalue(L, 3);
4112 ret = lua_rawlen(L, -2);
4113 lua_rawseti(L, -2, ret + 1);
4114 lua_pushboolean(L, 1);
4115 return 1;
4116}
4117
4118__LJMP static int hlua_applet_http_status(lua_State *L)
4119{
4120 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4121 int status = MAY_LJMP(luaL_checkinteger(L, 2));
4122
4123 if (status < 100 || status > 599) {
4124 lua_pushboolean(L, 0);
4125 return 1;
4126 }
4127
4128 appctx->appctx->ctx.hlua_apphttp.status = status;
4129 lua_pushboolean(L, 1);
4130 return 1;
4131}
4132
4133/* We will build the status line and the headers of the HTTP response.
4134 * We will try send at once if its not possible, we give back the hand
4135 * waiting for more room.
4136 */
4137__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4138{
4139 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4140 struct stream_interface *si = appctx->appctx->owner;
4141 struct channel *chn = si_ic(si);
4142 int ret;
4143 size_t len;
4144 const char *msg;
4145
4146 /* Get the message as the first argument on the stack. */
4147 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4148
4149 /* Send the message at once. */
4150 ret = bi_putblk(chn, msg, len);
4151
4152 /* if ret == -2 or -3 the channel closed or the message si too
4153 * big for the buffers.
4154 */
4155 if (ret == -2 || ret == -3) {
4156 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4157 WILL_LJMP(lua_error(L));
4158 }
4159
4160 /* If ret is -1, we dont have room in the buffer, so we yield. */
4161 if (ret == -1) {
4162 si_applet_cant_put(si);
4163 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
4164 }
4165
4166 /* Headers sent, set the flag. */
4167 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4168 return 0;
4169}
4170
4171__LJMP static int hlua_applet_http_start_response(lua_State *L)
4172{
4173 struct chunk *tmp = get_trash_chunk();
4174 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004175 const char *name;
4176 const char *value;
4177 int id;
4178 int hdr_connection = 0;
4179 int hdr_contentlength = -1;
4180 int hdr_chunked = 0;
4181
4182 /* Use the same http version than the request. */
4183 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004184 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004185 appctx->appctx->ctx.hlua_apphttp.status,
4186 get_reason(appctx->appctx->ctx.hlua_apphttp.status));
4187
4188 /* Get the array associated to the field "response" in the object AppletHTTP. */
4189 lua_pushvalue(L, 0);
4190 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4191 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4192 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4193 WILL_LJMP(lua_error(L));
4194 }
4195
4196 /* Browse the list of headers. */
4197 lua_pushnil(L);
4198 while(lua_next(L, -2) != 0) {
4199
4200 /* We expect a string as -2. */
4201 if (lua_type(L, -2) != LUA_TSTRING) {
4202 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4203 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4204 lua_typename(L, lua_type(L, -2)));
4205 WILL_LJMP(lua_error(L));
4206 }
4207 name = lua_tostring(L, -2);
4208
4209 /* We expect an array as -1. */
4210 if (lua_type(L, -1) != LUA_TTABLE) {
4211 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4212 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4213 name,
4214 lua_typename(L, lua_type(L, -1)));
4215 WILL_LJMP(lua_error(L));
4216 }
4217
4218 /* Browse the table who is on the top of the stack. */
4219 lua_pushnil(L);
4220 while(lua_next(L, -2) != 0) {
4221
4222 /* We expect a number as -2. */
4223 if (lua_type(L, -2) != LUA_TNUMBER) {
4224 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4225 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4226 name,
4227 lua_typename(L, lua_type(L, -2)));
4228 WILL_LJMP(lua_error(L));
4229 }
4230 id = lua_tointeger(L, -2);
4231
4232 /* We expect a string as -2. */
4233 if (lua_type(L, -1) != LUA_TSTRING) {
4234 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4235 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4236 name, id,
4237 lua_typename(L, lua_type(L, -1)));
4238 WILL_LJMP(lua_error(L));
4239 }
4240 value = lua_tostring(L, -1);
4241
4242 /* Catenate a new header. */
4243 chunk_appendf(tmp, "%s: %s\r\n", name, value);
4244
4245 /* Protocol checks. */
4246
4247 /* Check if the header conneciton is present. */
4248 if (strcasecmp("connection", name) == 0)
4249 hdr_connection = 1;
4250
4251 /* Copy the header content length. The length conversion
4252 * is done without control. If it contains a ad value, this
4253 * is not our problem.
4254 */
4255 if (strcasecmp("content-length", name) == 0)
4256 hdr_contentlength = atoi(value);
4257
4258 /* Check if the client annouces a transfer-encoding chunked it self. */
4259 if (strcasecmp("transfer-encoding", name) == 0 &&
4260 strcasecmp("chunked", value) == 0)
4261 hdr_chunked = 1;
4262
4263 /* Remove the array from the stack, and get next element with a remaining string. */
4264 lua_pop(L, 1);
4265 }
4266
4267 /* Remove the array from the stack, and get next element with a remaining string. */
4268 lua_pop(L, 1);
4269 }
4270
4271 /* If the http protocol version is 1.1, we expect an header "connection" set
4272 * to "close" to be HAProxy/keeplive compliant. Otherwise, we expect nothing.
4273 * If the header conneciton is present, don't change it, if it is not present,
4274 * we must set.
4275 *
4276 * we set a "connection: close" header for ensuring that the keepalive will be
4277 * respected by haproxy. HAProcy considers that the application cloe the connection
4278 * and it keep the connection from the client open.
4279 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004280 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 && !hdr_connection)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004281 chunk_appendf(tmp, "Connection: close\r\n");
4282
4283 /* If we dont have a content-length set, we must announce a transfer enconding
4284 * chunked. This is required by haproxy for the keepalive compliance.
4285 * If the applet annouce a transfer-encoding chunked itslef, don't
4286 * do anything.
4287 */
4288 if (hdr_contentlength == -1 && hdr_chunked == 0) {
4289 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
4290 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
4291 }
4292
4293 /* Finalize headers. */
4294 chunk_appendf(tmp, "\r\n");
4295
4296 /* Remove the last entry and the array of headers */
4297 lua_pop(L, 2);
4298
4299 /* Push the headers block. */
4300 lua_pushlstring(L, tmp->str, tmp->len);
4301
4302 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
4303}
4304
4305/*
4306 *
4307 *
4308 * Class HTTP
4309 *
4310 *
4311 */
4312
4313/* Returns a struct hlua_txn if the stack entry "ud" is
4314 * a class stream, otherwise it throws an error.
4315 */
4316__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
4317{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004318 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004319}
4320
4321/* This function creates and push in the stack a HTTP object
4322 * according with a current TXN.
4323 */
4324static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4325{
4326 struct hlua_txn *htxn;
4327
4328 /* Check stack size. */
4329 if (!lua_checkstack(L, 3))
4330 return 0;
4331
4332 /* Create the object: obj[0] = userdata.
4333 * Note that the base of the Converters object is the
4334 * same than the TXN object.
4335 */
4336 lua_newtable(L);
4337 htxn = lua_newuserdata(L, sizeof(*htxn));
4338 lua_rawseti(L, -2, 0);
4339
4340 htxn->s = txn->s;
4341 htxn->p = txn->p;
4342
4343 /* Pop a class stream metatable and affect it to the table. */
4344 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4345 lua_setmetatable(L, -2);
4346
4347 return 1;
4348}
4349
4350/* This function creates ans returns an array of HTTP headers.
4351 * This function does not fails. It is used as wrapper with the
4352 * 2 following functions.
4353 */
4354__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4355{
4356 const char *cur_ptr, *cur_next, *p;
4357 int old_idx, cur_idx;
4358 struct hdr_idx_elem *cur_hdr;
4359 const char *hn, *hv;
4360 int hnl, hvl;
4361 int type;
4362 const char *in;
4363 char *out;
4364 int len;
4365
4366 /* Create the table. */
4367 lua_newtable(L);
4368
4369 if (!htxn->s->txn)
4370 return 1;
4371
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004372 /* Check if a valid response is parsed */
4373 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4374 return 1;
4375
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004376 /* Build array of headers. */
4377 old_idx = 0;
4378 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
4379
4380 while (1) {
4381 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
4382 if (!cur_idx)
4383 break;
4384 old_idx = cur_idx;
4385
4386 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
4387 cur_ptr = cur_next;
4388 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
4389
4390 /* Now we have one full header at cur_ptr of len cur_hdr->len,
4391 * and the next header starts at cur_next. We'll check
4392 * this header in the list as well as against the default
4393 * rule.
4394 */
4395
4396 /* look for ': *'. */
4397 hn = cur_ptr;
4398 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
4399 if (p >= cur_ptr+cur_hdr->len)
4400 continue;
4401 hnl = p - hn;
4402 p++;
4403 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
4404 p++;
4405 if (p >= cur_ptr+cur_hdr->len)
4406 continue;
4407 hv = p;
4408 hvl = cur_ptr+cur_hdr->len-p;
4409
4410 /* Lowercase the key. Don't check the size of trash, it have
4411 * the size of one buffer and the input data contains in one
4412 * buffer.
4413 */
4414 out = trash.str;
4415 for (in=hn; in<hn+hnl; in++, out++)
4416 *out = tolower(*in);
4417 *out = '\0';
4418
4419 /* Check for existing entry:
4420 * assume that the table is on the top of the stack, and
4421 * push the key in the stack, the function lua_gettable()
4422 * perform the lookup.
4423 */
4424 lua_pushlstring(L, trash.str, hnl);
4425 lua_gettable(L, -2);
4426 type = lua_type(L, -1);
4427
4428 switch (type) {
4429 case LUA_TNIL:
4430 /* Table not found, create it. */
4431 lua_pop(L, 1); /* remove the nil value. */
4432 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
4433 lua_newtable(L); /* create and push empty table. */
4434 lua_pushlstring(L, hv, hvl); /* push header value. */
4435 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4436 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4437 break;
4438
4439 case LUA_TTABLE:
4440 /* Entry found: push the value in the table. */
4441 len = lua_rawlen(L, -1);
4442 lua_pushlstring(L, hv, hvl); /* push header value. */
4443 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4444 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4445 break;
4446
4447 default:
4448 /* Other cases are errors. */
4449 hlua_pusherror(L, "internal error during the parsing of headers.");
4450 WILL_LJMP(lua_error(L));
4451 }
4452 }
4453
4454 return 1;
4455}
4456
4457__LJMP static int hlua_http_req_get_headers(lua_State *L)
4458{
4459 struct hlua_txn *htxn;
4460
4461 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4462 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4463
4464 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4465}
4466
4467__LJMP static int hlua_http_res_get_headers(lua_State *L)
4468{
4469 struct hlua_txn *htxn;
4470
4471 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4472 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4473
4474 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4475}
4476
4477/* This function replace full header, or just a value in
4478 * the request or in the response. It is a wrapper fir the
4479 * 4 following functions.
4480 */
4481__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4482 struct http_msg *msg, int action)
4483{
4484 size_t name_len;
4485 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4486 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4487 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
4488 struct my_regex re;
4489
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004490 /* Check if a valid response is parsed */
4491 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4492 return 0;
4493
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004494 if (!regex_comp(reg, &re, 1, 1, NULL))
4495 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4496
4497 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
4498 regex_free(&re);
4499 return 0;
4500}
4501
4502__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4503{
4504 struct hlua_txn *htxn;
4505
4506 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4507 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4508
4509 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4510}
4511
4512__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4513{
4514 struct hlua_txn *htxn;
4515
4516 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4517 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4518
4519 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4520}
4521
4522__LJMP static int hlua_http_req_rep_val(lua_State *L)
4523{
4524 struct hlua_txn *htxn;
4525
4526 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4527 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4528
4529 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4530}
4531
4532__LJMP static int hlua_http_res_rep_val(lua_State *L)
4533{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004534 struct hlua_txn *htxn;
4535
4536 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4537 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4538
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004539 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004540}
4541
4542/* This function deletes all the occurences of an header.
4543 * It is a wrapper for the 2 following functions.
4544 */
4545__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4546{
4547 size_t len;
4548 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4549 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004550 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004551
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004552 /* Check if a valid response is parsed */
4553 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4554 return 0;
4555
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004556 ctx.idx = 0;
4557 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
4558 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4559 return 0;
4560}
4561
4562__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4563{
4564 struct hlua_txn *htxn;
4565
4566 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4567 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4568
Willy Tarreaueee5b512015-04-03 23:46:31 +02004569 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004570}
4571
4572__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4573{
4574 struct hlua_txn *htxn;
4575
4576 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4577 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4578
Willy Tarreaueee5b512015-04-03 23:46:31 +02004579 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004580}
4581
4582/* This function adds an header. It is a wrapper used by
4583 * the 2 following functions.
4584 */
4585__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4586{
4587 size_t name_len;
4588 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4589 size_t value_len;
4590 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
4591 char *p;
4592
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004593 /* Check if a valid message is parsed */
4594 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4595 return 0;
4596
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004597 /* Check length. */
4598 trash.len = value_len + name_len + 2;
4599 if (trash.len > trash.size)
4600 return 0;
4601
4602 /* Creates the header string. */
4603 p = trash.str;
4604 memcpy(p, name, name_len);
4605 p += name_len;
4606 *p = ':';
4607 p++;
4608 *p = ' ';
4609 p++;
4610 memcpy(p, value, value_len);
4611
Willy Tarreaueee5b512015-04-03 23:46:31 +02004612 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004613 trash.str, trash.len) != 0);
4614
4615 return 0;
4616}
4617
4618__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4619{
4620 struct hlua_txn *htxn;
4621
4622 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4623 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4624
Willy Tarreaueee5b512015-04-03 23:46:31 +02004625 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004626}
4627
4628__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4629{
4630 struct hlua_txn *htxn;
4631
4632 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4633 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4634
Willy Tarreaueee5b512015-04-03 23:46:31 +02004635 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004636}
4637
4638static int hlua_http_req_set_hdr(lua_State *L)
4639{
4640 struct hlua_txn *htxn;
4641
4642 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4643 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4644
Willy Tarreaueee5b512015-04-03 23:46:31 +02004645 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4646 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004647}
4648
4649static int hlua_http_res_set_hdr(lua_State *L)
4650{
4651 struct hlua_txn *htxn;
4652
4653 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4654 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4655
Willy Tarreaueee5b512015-04-03 23:46:31 +02004656 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4657 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004658}
4659
4660/* This function set the method. */
4661static int hlua_http_req_set_meth(lua_State *L)
4662{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004663 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004664 size_t name_len;
4665 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004666
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004667 /* Check if a valid request is parsed */
4668 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4669 lua_pushboolean(L, 0);
4670 return 1;
4671 }
4672
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004673 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004674 return 1;
4675}
4676
4677/* This function set the method. */
4678static int hlua_http_req_set_path(lua_State *L)
4679{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004680 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004681 size_t name_len;
4682 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004683
4684 /* Check if a valid request is parsed */
4685 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4686 lua_pushboolean(L, 0);
4687 return 1;
4688 }
4689
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004690 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004691 return 1;
4692}
4693
4694/* This function set the query-string. */
4695static int hlua_http_req_set_query(lua_State *L)
4696{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004697 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004698 size_t name_len;
4699 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004700
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004701 /* Check if a valid request is parsed */
4702 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4703 lua_pushboolean(L, 0);
4704 return 1;
4705 }
4706
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004707 /* Check length. */
4708 if (name_len > trash.size - 1) {
4709 lua_pushboolean(L, 0);
4710 return 1;
4711 }
4712
4713 /* Add the mark question as prefix. */
4714 chunk_reset(&trash);
4715 trash.str[trash.len++] = '?';
4716 memcpy(trash.str + trash.len, name, name_len);
4717 trash.len += name_len;
4718
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004719 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004720 return 1;
4721}
4722
4723/* This function set the uri. */
4724static int hlua_http_req_set_uri(lua_State *L)
4725{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004726 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004727 size_t name_len;
4728 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004729
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004730 /* Check if a valid request is parsed */
4731 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4732 lua_pushboolean(L, 0);
4733 return 1;
4734 }
4735
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004736 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004737 return 1;
4738}
4739
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004740/* This function set the response code. */
4741static int hlua_http_res_set_status(lua_State *L)
4742{
4743 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4744 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
4745
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004746 /* Check if a valid response is parsed */
4747 if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY))
4748 return 0;
4749
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02004750 http_set_status(code, htxn->s);
4751 return 0;
4752}
4753
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004754/*
4755 *
4756 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004757 * Class TXN
4758 *
4759 *
4760 */
4761
4762/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004763 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004764 */
4765__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
4766{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004767 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004768}
4769
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004770__LJMP static int hlua_set_var(lua_State *L)
4771{
4772 struct hlua_txn *htxn;
4773 const char *name;
4774 size_t len;
4775 struct sample smp;
4776
4777 MAY_LJMP(check_args(L, 3, "set_var"));
4778
4779 /* It is useles to retrieve the stream, but this function
4780 * runs only in a stream context.
4781 */
4782 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4783 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4784
4785 /* Converts the third argument in a sample. */
4786 hlua_lua2smp(L, 3, &smp);
4787
4788 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01004789 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01004790 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004791 return 0;
4792}
4793
Christopher Faulet85d79c92016-11-09 16:54:56 +01004794__LJMP static int hlua_unset_var(lua_State *L)
4795{
4796 struct hlua_txn *htxn;
4797 const char *name;
4798 size_t len;
4799 struct sample smp;
4800
4801 MAY_LJMP(check_args(L, 2, "unset_var"));
4802
4803 /* It is useles to retrieve the stream, but this function
4804 * runs only in a stream context.
4805 */
4806 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4807 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4808
4809 /* Unset the variable. */
4810 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
4811 vars_unset_by_name(name, len, &smp);
4812 return 0;
4813}
4814
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004815__LJMP static int hlua_get_var(lua_State *L)
4816{
4817 struct hlua_txn *htxn;
4818 const char *name;
4819 size_t len;
4820 struct sample smp;
4821
4822 MAY_LJMP(check_args(L, 2, "get_var"));
4823
4824 /* It is useles to retrieve the stream, but this function
4825 * runs only in a stream context.
4826 */
4827 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4828 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4829
Willy Tarreau7560dd42016-03-10 16:28:58 +01004830 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01004831 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02004832 lua_pushnil(L);
4833 return 1;
4834 }
4835
4836 return hlua_smp2lua(L, &smp);
4837}
4838
Willy Tarreau59551662015-03-10 14:23:13 +01004839__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004840{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004841 struct hlua *hlua;
4842
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004843 MAY_LJMP(check_args(L, 2, "set_priv"));
4844
Willy Tarreau87b09662015-04-03 00:22:06 +02004845 /* It is useles to retrieve the stream, but this function
4846 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004847 */
4848 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004849 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004850
4851 /* Remove previous value. */
4852 if (hlua->Mref != -1)
4853 luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX);
4854
4855 /* Get and store new value. */
4856 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4857 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4858
4859 return 0;
4860}
4861
Willy Tarreau59551662015-03-10 14:23:13 +01004862__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004863{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004864 struct hlua *hlua;
4865
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004866 MAY_LJMP(check_args(L, 1, "get_priv"));
4867
Willy Tarreau87b09662015-04-03 00:22:06 +02004868 /* It is useles to retrieve the stream, but this function
4869 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004870 */
4871 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01004872 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01004873
4874 /* Push configuration index in the stack. */
4875 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4876
4877 return 1;
4878}
4879
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004880/* Create stack entry containing a class TXN. This function
4881 * return 0 if the stack does not contains free slots,
4882 * otherwise it returns 1.
4883 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02004884static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004885{
Willy Tarreaude491382015-04-06 11:04:28 +02004886 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004887
4888 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01004889 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004890 return 0;
4891
4892 /* NOTE: The allocation never fails. The failure
4893 * throw an error, and the function never returns.
4894 * if the throw is not avalaible, the process is aborted.
4895 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01004896 /* Create the object: obj[0] = userdata. */
4897 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02004898 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01004899 lua_rawseti(L, -2, 0);
4900
Willy Tarreaude491382015-04-06 11:04:28 +02004901 htxn->s = s;
4902 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01004903 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02004904 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004905
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004906 /* Create the "f" field that contains a list of fetches. */
4907 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01004908 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004909 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004910 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004911
4912 /* Create the "sf" field that contains a list of stringsafe fetches. */
4913 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01004914 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004915 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004916 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01004917
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004918 /* Create the "c" field that contains a list of converters. */
4919 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02004920 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004921 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004922 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01004923
4924 /* Create the "sc" field that contains a list of stringsafe converters. */
4925 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004926 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004927 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004928 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004929
Thierry FOURNIER397826a2015-03-11 19:39:09 +01004930 /* Create the "req" field that contains the request channel object. */
4931 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01004932 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01004933 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004934 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01004935
4936 /* Create the "res" field that contains the response channel object. */
4937 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01004938 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01004939 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004940 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01004941
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004942 /* Creates the HTTP object is the current proxy allows http. */
4943 lua_pushstring(L, "http");
4944 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02004945 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004946 return 0;
4947 }
4948 else
4949 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02004950 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004951
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01004952 /* Pop a class sesison metatable and affect it to the userdata. */
4953 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
4954 lua_setmetatable(L, -2);
4955
4956 return 1;
4957}
4958
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01004959__LJMP static int hlua_txn_deflog(lua_State *L)
4960{
4961 const char *msg;
4962 struct hlua_txn *htxn;
4963
4964 MAY_LJMP(check_args(L, 2, "deflog"));
4965 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4966 msg = MAY_LJMP(luaL_checkstring(L, 2));
4967
4968 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
4969 return 0;
4970}
4971
4972__LJMP static int hlua_txn_log(lua_State *L)
4973{
4974 int level;
4975 const char *msg;
4976 struct hlua_txn *htxn;
4977
4978 MAY_LJMP(check_args(L, 3, "log"));
4979 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4980 level = MAY_LJMP(luaL_checkinteger(L, 2));
4981 msg = MAY_LJMP(luaL_checkstring(L, 3));
4982
4983 if (level < 0 || level >= NB_LOG_LEVELS)
4984 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
4985
4986 hlua_sendlog(htxn->s->be, level, msg);
4987 return 0;
4988}
4989
4990__LJMP static int hlua_txn_log_debug(lua_State *L)
4991{
4992 const char *msg;
4993 struct hlua_txn *htxn;
4994
4995 MAY_LJMP(check_args(L, 2, "Debug"));
4996 htxn = MAY_LJMP(hlua_checktxn(L, 1));
4997 msg = MAY_LJMP(luaL_checkstring(L, 2));
4998 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
4999 return 0;
5000}
5001
5002__LJMP static int hlua_txn_log_info(lua_State *L)
5003{
5004 const char *msg;
5005 struct hlua_txn *htxn;
5006
5007 MAY_LJMP(check_args(L, 2, "Info"));
5008 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5009 msg = MAY_LJMP(luaL_checkstring(L, 2));
5010 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5011 return 0;
5012}
5013
5014__LJMP static int hlua_txn_log_warning(lua_State *L)
5015{
5016 const char *msg;
5017 struct hlua_txn *htxn;
5018
5019 MAY_LJMP(check_args(L, 2, "Warning"));
5020 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5021 msg = MAY_LJMP(luaL_checkstring(L, 2));
5022 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5023 return 0;
5024}
5025
5026__LJMP static int hlua_txn_log_alert(lua_State *L)
5027{
5028 const char *msg;
5029 struct hlua_txn *htxn;
5030
5031 MAY_LJMP(check_args(L, 2, "Alert"));
5032 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5033 msg = MAY_LJMP(luaL_checkstring(L, 2));
5034 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5035 return 0;
5036}
5037
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005038__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5039{
5040 struct hlua_txn *htxn;
5041 int ll;
5042
5043 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5044 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5045 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5046
5047 if (ll < 0 || ll > 7)
5048 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5049
5050 htxn->s->logs.level = ll;
5051 return 0;
5052}
5053
5054__LJMP static int hlua_txn_set_tos(lua_State *L)
5055{
5056 struct hlua_txn *htxn;
5057 struct connection *cli_conn;
5058 int tos;
5059
5060 MAY_LJMP(check_args(L, 2, "set_tos"));
5061 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5062 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5063
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005064 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02005065 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005066
5067 return 0;
5068}
5069
5070__LJMP static int hlua_txn_set_mark(lua_State *L)
5071{
5072#ifdef SO_MARK
5073 struct hlua_txn *htxn;
5074 struct connection *cli_conn;
5075 int mark;
5076
5077 MAY_LJMP(check_args(L, 2, "set_mark"));
5078 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5079 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5080
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005081 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau07081fe2015-04-06 10:59:20 +02005082 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005083#endif
5084 return 0;
5085}
5086
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005087/* This function is an Lua binding that send pending data
5088 * to the client, and close the stream interface.
5089 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005090__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005091{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005092 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005093 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005094 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005095
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005096 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005097 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005098 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005099
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005100 /* If the flags NOTERM is set, we cannot terminate the http
5101 * session, so we just end the execution of the current
5102 * lua code.
5103 */
5104 if (htxn->flags & HLUA_TXN_NOTERM) {
5105 WILL_LJMP(hlua_done(L));
5106 return 0;
5107 }
5108
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005109 ic = &htxn->s->req;
5110 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005111
Willy Tarreau630ef452015-08-28 10:06:15 +02005112 if (htxn->s->txn) {
5113 /* HTTP mode, let's stay in sync with the stream */
5114 bi_fast_delete(ic->buf, htxn->s->txn->req.sov);
5115 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
5116 htxn->s->txn->req.sov = 0;
5117 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
5118 oc->analysers = AN_RES_HTTP_XFER_BODY;
5119 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
5120 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
5121
Willy Tarreau630ef452015-08-28 10:06:15 +02005122 /* Note that if we want to support keep-alive, we need
5123 * to bypass the close/shutr_now calls below, but that
5124 * may only be done if the HTTP request was already
5125 * processed and the connection header is known (ie
5126 * not during TCP rules).
5127 */
5128 }
5129
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005130 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01005131 channel_abort(ic);
5132 channel_auto_close(ic);
5133 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005134
5135 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01005136 channel_auto_read(oc);
5137 channel_auto_close(oc);
5138 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005139
Willy Tarreau0458b082015-08-28 09:40:04 +02005140 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005141
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005142 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005143 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005144 return 0;
5145}
5146
5147__LJMP static int hlua_log(lua_State *L)
5148{
5149 int level;
5150 const char *msg;
5151
5152 MAY_LJMP(check_args(L, 2, "log"));
5153 level = MAY_LJMP(luaL_checkinteger(L, 1));
5154 msg = MAY_LJMP(luaL_checkstring(L, 2));
5155
5156 if (level < 0 || level >= NB_LOG_LEVELS)
5157 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5158
5159 hlua_sendlog(NULL, level, msg);
5160 return 0;
5161}
5162
5163__LJMP static int hlua_log_debug(lua_State *L)
5164{
5165 const char *msg;
5166
5167 MAY_LJMP(check_args(L, 1, "debug"));
5168 msg = MAY_LJMP(luaL_checkstring(L, 1));
5169 hlua_sendlog(NULL, LOG_DEBUG, msg);
5170 return 0;
5171}
5172
5173__LJMP static int hlua_log_info(lua_State *L)
5174{
5175 const char *msg;
5176
5177 MAY_LJMP(check_args(L, 1, "info"));
5178 msg = MAY_LJMP(luaL_checkstring(L, 1));
5179 hlua_sendlog(NULL, LOG_INFO, msg);
5180 return 0;
5181}
5182
5183__LJMP static int hlua_log_warning(lua_State *L)
5184{
5185 const char *msg;
5186
5187 MAY_LJMP(check_args(L, 1, "warning"));
5188 msg = MAY_LJMP(luaL_checkstring(L, 1));
5189 hlua_sendlog(NULL, LOG_WARNING, msg);
5190 return 0;
5191}
5192
5193__LJMP static int hlua_log_alert(lua_State *L)
5194{
5195 const char *msg;
5196
5197 MAY_LJMP(check_args(L, 1, "alert"));
5198 msg = MAY_LJMP(luaL_checkstring(L, 1));
5199 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005200 return 0;
5201}
5202
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005203__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005204{
5205 int wakeup_ms = lua_tointeger(L, -1);
5206 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005207 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005208 return 0;
5209}
5210
5211__LJMP static int hlua_sleep(lua_State *L)
5212{
5213 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005214 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005215
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005216 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005217
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005218 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005219 wakeup_ms = tick_add(now_ms, delay);
5220 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005221
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005222 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5223 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005224}
5225
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005226__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005227{
5228 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005229 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005230
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005231 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005232
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005233 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005234 wakeup_ms = tick_add(now_ms, delay);
5235 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005236
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005237 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5238 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005239}
5240
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005241/* This functionis an LUA binding. it permits to give back
5242 * the hand at the HAProxy scheduler. It is used when the
5243 * LUA processing consumes a lot of time.
5244 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005245__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005246{
5247 return 0;
5248}
5249
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005250__LJMP static int hlua_yield(lua_State *L)
5251{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005252 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
5253 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005254}
5255
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005256/* This function change the nice of the currently executed
5257 * task. It is used set low or high priority at the current
5258 * task.
5259 */
Willy Tarreau59551662015-03-10 14:23:13 +01005260__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005261{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005262 struct hlua *hlua;
5263 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005264
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005265 MAY_LJMP(check_args(L, 1, "set_nice"));
5266 hlua = hlua_gethlua(L);
5267 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005268
5269 /* If he task is not set, I'm in a start mode. */
5270 if (!hlua || !hlua->task)
5271 return 0;
5272
5273 if (nice < -1024)
5274 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005275 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005276 nice = 1024;
5277
5278 hlua->task->nice = nice;
5279 return 0;
5280}
5281
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005282/* This function is used as a calback of a task. It is called by the
5283 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5284 * return an E_AGAIN signal, the emmiter of this signal must set a
5285 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005286 *
5287 * Task wrapper are longjmp safe because the only one Lua code
5288 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005289 */
5290static struct task *hlua_process_task(struct task *task)
5291{
5292 struct hlua *hlua = task->context;
5293 enum hlua_exec status;
5294
5295 /* We need to remove the task from the wait queue before executing
5296 * the Lua code because we don't know if it needs to wait for
5297 * another timer or not in the case of E_AGAIN.
5298 */
5299 task_delete(task);
5300
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005301 /* If it is the first call to the task, we must initialize the
5302 * execution timeouts.
5303 */
5304 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005305 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005306
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005307 /* Execute the Lua code. */
5308 status = hlua_ctx_resume(hlua, 1);
5309
5310 switch (status) {
5311 /* finished or yield */
5312 case HLUA_E_OK:
5313 hlua_ctx_destroy(hlua);
5314 task_delete(task);
5315 task_free(task);
5316 break;
5317
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005318 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
5319 if (hlua->wake_time != TICK_ETERNITY)
5320 task_schedule(task, hlua->wake_time);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005321 break;
5322
5323 /* finished with error. */
5324 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005325 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005326 hlua_ctx_destroy(hlua);
5327 task_delete(task);
5328 task_free(task);
5329 break;
5330
5331 case HLUA_E_ERR:
5332 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005333 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005334 hlua_ctx_destroy(hlua);
5335 task_delete(task);
5336 task_free(task);
5337 break;
5338 }
5339 return NULL;
5340}
5341
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005342/* This function is an LUA binding that register LUA function to be
5343 * executed after the HAProxy configuration parsing and before the
5344 * HAProxy scheduler starts. This function expect only one LUA
5345 * argument that is a function. This function returns nothing, but
5346 * throws if an error is encountered.
5347 */
5348__LJMP static int hlua_register_init(lua_State *L)
5349{
5350 struct hlua_init_function *init;
5351 int ref;
5352
5353 MAY_LJMP(check_args(L, 1, "register_init"));
5354
5355 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5356
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005357 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005358 if (!init)
5359 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5360
5361 init->function_ref = ref;
5362 LIST_ADDQ(&hlua_init_functions, &init->l);
5363 return 0;
5364}
5365
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005366/* This functio is an LUA binding. It permits to register a task
5367 * executed in parallel of the main HAroxy activity. The task is
5368 * created and it is set in the HAProxy scheduler. It can be called
5369 * from the "init" section, "post init" or during the runtime.
5370 *
5371 * Lua prototype:
5372 *
5373 * <none> core.register_task(<function>)
5374 */
5375static int hlua_register_task(lua_State *L)
5376{
5377 struct hlua *hlua;
5378 struct task *task;
5379 int ref;
5380
5381 MAY_LJMP(check_args(L, 1, "register_task"));
5382
5383 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5384
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005385 hlua = pool_alloc2(pool2_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005386 if (!hlua)
5387 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5388
5389 task = task_new();
5390 task->context = hlua;
5391 task->process = hlua_process_task;
5392
5393 if (!hlua_ctx_init(hlua, task))
5394 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5395
5396 /* Restore the function in the stack. */
5397 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5398 hlua->nargs = 0;
5399
5400 /* Schedule task. */
5401 task_schedule(task, now_ms);
5402
5403 return 0;
5404}
5405
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005406/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5407 * doesn't allow "yield" functions because the HAProxy engine cannot
5408 * resume converters.
5409 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005410static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005411{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005412 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005413 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005414 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005415
Willy Tarreaube508f12016-03-10 11:47:01 +01005416 if (!stream)
5417 return 0;
5418
Willy Tarreau87b09662015-04-03 00:22:06 +02005419 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005420 * Lua context can be not initialized. This behavior
5421 * permits to save performances because a systematic
5422 * Lua initialization cause 5% performances loss.
5423 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005424 if (!stream->hlua) {
5425 stream->hlua = pool_alloc2(pool2_hlua);
5426 if (!stream->hlua) {
5427 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5428 return 0;
5429 }
5430 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5431 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5432 return 0;
5433 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005434 }
5435
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005436 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005437 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005438
5439 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005440 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5441 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5442 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005443 else
5444 error = "critical error";
5445 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005446 return 0;
5447 }
5448
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005449 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005450 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005451 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005452 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005453 return 0;
5454 }
5455
5456 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005457 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005458
5459 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005460 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005461 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005462 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005463 return 0;
5464 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005465 hlua_smp2lua(stream->hlua->T, smp);
5466 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005467
5468 /* push keywords in the stack. */
5469 if (arg_p) {
5470 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005471 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005472 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005473 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005474 return 0;
5475 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005476 hlua_arg2lua(stream->hlua->T, arg_p);
5477 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005478 }
5479 }
5480
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005481 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005482 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005483
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005484 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005485 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005486 }
5487
5488 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005489 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005490 /* finished. */
5491 case HLUA_E_OK:
5492 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005493 hlua_lua2smp(stream->hlua->T, -1, smp);
5494 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005495 return 1;
5496
5497 /* yield. */
5498 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005499 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005500 return 0;
5501
5502 /* finished with error. */
5503 case HLUA_E_ERRMSG:
5504 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005505 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005506 fcn->name, lua_tostring(stream->hlua->T, -1));
5507 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005508 return 0;
5509
5510 case HLUA_E_ERR:
5511 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005512 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005513
5514 default:
5515 return 0;
5516 }
5517}
5518
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005519/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5520 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005521 * resume sample-fetches. This function will be called by the sample
5522 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005523 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005524static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5525 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005526{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005527 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005528 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005529 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005530 const struct chunk msg = { .len = 0 };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005531
Willy Tarreaube508f12016-03-10 11:47:01 +01005532 if (!stream)
5533 return 0;
5534
Willy Tarreau87b09662015-04-03 00:22:06 +02005535 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005536 * Lua context can be not initialized. This behavior
5537 * permits to save performances because a systematic
5538 * Lua initialization cause 5% performances loss.
5539 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005540 if (!stream->hlua) {
5541 stream->hlua = pool_alloc2(pool2_hlua);
5542 if (!stream->hlua) {
5543 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5544 return 0;
5545 }
5546 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5547 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5548 return 0;
5549 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005550 }
5551
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005552 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005553
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005554 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005555 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005556
5557 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005558 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5559 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5560 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005561 else
5562 error = "critical error";
5563 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005564 return 0;
5565 }
5566
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005567 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005568 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005569 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005570 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005571 return 0;
5572 }
5573
5574 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005575 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005576
5577 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005578 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005579 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005580 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005581 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005582 return 0;
5583 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005584 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005585
5586 /* push keywords in the stack. */
5587 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5588 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005589 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005590 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005591 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005592 return 0;
5593 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005594 hlua_arg2lua(stream->hlua->T, arg_p);
5595 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005596 }
5597
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005598 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005599 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005600
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005601 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005602 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005603 }
5604
5605 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005606 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005607 /* finished. */
5608 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005609 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005610 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005611 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005612 }
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005613 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005614 hlua_lua2smp(stream->hlua->T, -1, smp);
5615 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005616
5617 /* Set the end of execution flag. */
5618 smp->flags &= ~SMP_F_MAY_CHANGE;
5619 return 1;
5620
5621 /* yield. */
5622 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005623 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005624 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005625 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005626 return 0;
5627
5628 /* finished with error. */
5629 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005630 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005631 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005632 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005633 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005634 fcn->name, lua_tostring(stream->hlua->T, -1));
5635 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005636 return 0;
5637
5638 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005639 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005640 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005641 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005642 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005643
5644 default:
5645 return 0;
5646 }
5647}
5648
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005649/* This function is an LUA binding used for registering
5650 * "sample-conv" functions. It expects a converter name used
5651 * in the haproxy configuration file, and an LUA function.
5652 */
5653__LJMP static int hlua_register_converters(lua_State *L)
5654{
5655 struct sample_conv_kw_list *sck;
5656 const char *name;
5657 int ref;
5658 int len;
5659 struct hlua_function *fcn;
5660
5661 MAY_LJMP(check_args(L, 2, "register_converters"));
5662
5663 /* First argument : converter name. */
5664 name = MAY_LJMP(luaL_checkstring(L, 1));
5665
5666 /* Second argument : lua function. */
5667 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5668
5669 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005670 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005671 if (!sck)
5672 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005673 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005674 if (!fcn)
5675 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5676
5677 /* Fill fcn. */
5678 fcn->name = strdup(name);
5679 if (!fcn->name)
5680 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5681 fcn->function_ref = ref;
5682
5683 /* List head */
5684 sck->list.n = sck->list.p = NULL;
5685
5686 /* converter keyword. */
5687 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005688 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005689 if (!sck->kw[0].kw)
5690 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5691
5692 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
5693 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01005694 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 +01005695 sck->kw[0].val_args = NULL;
5696 sck->kw[0].in_type = SMP_T_STR;
5697 sck->kw[0].out_type = SMP_T_STR;
5698 sck->kw[0].private = fcn;
5699
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005700 /* Register this new converter */
5701 sample_register_convs(sck);
5702
5703 return 0;
5704}
5705
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005706/* This fucntion is an LUA binding used for registering
5707 * "sample-fetch" functions. It expects a converter name used
5708 * in the haproxy configuration file, and an LUA function.
5709 */
5710__LJMP static int hlua_register_fetches(lua_State *L)
5711{
5712 const char *name;
5713 int ref;
5714 int len;
5715 struct sample_fetch_kw_list *sfk;
5716 struct hlua_function *fcn;
5717
5718 MAY_LJMP(check_args(L, 2, "register_fetches"));
5719
5720 /* First argument : sample-fetch name. */
5721 name = MAY_LJMP(luaL_checkstring(L, 1));
5722
5723 /* Second argument : lua function. */
5724 ref = MAY_LJMP(hlua_checkfunction(L, 2));
5725
5726 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005727 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005728 if (!sfk)
5729 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005730 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005731 if (!fcn)
5732 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5733
5734 /* Fill fcn. */
5735 fcn->name = strdup(name);
5736 if (!fcn->name)
5737 WILL_LJMP(luaL_error(L, "lua out of memory error."));
5738 fcn->function_ref = ref;
5739
5740 /* List head */
5741 sfk->list.n = sfk->list.p = NULL;
5742
5743 /* sample-fetch keyword. */
5744 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005745 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005746 if (!sfk->kw[0].kw)
5747 return luaL_error(L, "lua out of memory error.");
5748
5749 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
5750 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01005751 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 +01005752 sfk->kw[0].val_args = NULL;
5753 sfk->kw[0].out_type = SMP_T_STR;
5754 sfk->kw[0].use = SMP_USE_HTTP_ANY;
5755 sfk->kw[0].val = 0;
5756 sfk->kw[0].private = fcn;
5757
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005758 /* Register this new fetch. */
5759 sample_register_fetches(sfk);
5760
5761 return 0;
5762}
5763
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005764/* This function is a wrapper to execute each LUA function declared
5765 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005766 * return ACT_RET_CONT if the processing is finished (with or without
5767 * error) and return ACT_RET_YIELD if the function must be called again
5768 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005769 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005770static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02005771 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005772{
5773 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005774 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005775 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005776 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005777 const struct chunk msg = { .len = 0 };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005778
5779 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01005780 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
5781 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
5782 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
5783 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005784 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005785 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005786 return ACT_RET_CONT;
5787 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005788
Willy Tarreau87b09662015-04-03 00:22:06 +02005789 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005790 * Lua context can be not initialized. This behavior
5791 * permits to save performances because a systematic
5792 * Lua initialization cause 5% performances loss.
5793 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005794 if (!s->hlua) {
5795 s->hlua = pool_alloc2(pool2_hlua);
5796 if (!s->hlua) {
5797 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
5798 rule->arg.hlua_rule->fcn.name);
5799 return ACT_RET_CONT;
5800 }
5801 if (!hlua_ctx_init(s->hlua, s->task)) {
5802 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
5803 rule->arg.hlua_rule->fcn.name);
5804 return ACT_RET_CONT;
5805 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005806 }
5807
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005808 consistency_set(s, dir, &s->hlua->cons);
5809
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005810 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005811 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005812
5813 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005814 if (!SET_SAFE_LJMP(s->hlua->T)) {
5815 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
5816 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005817 else
5818 error = "critical error";
5819 SEND_ERR(px, "Lua function '%s': %s.\n",
5820 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005821 return ACT_RET_CONT;
5822 }
5823
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005824 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005825 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005826 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005827 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005828 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005829 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005830 }
5831
5832 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005833 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005834
Willy Tarreau87b09662015-04-03 00:22:06 +02005835 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005836 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005837 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005838 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005839 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005840 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005841 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005842 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005843
5844 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005845 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005846 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005847 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005848 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005849 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005850 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005851 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005852 lua_pushstring(s->hlua->T, *arg);
5853 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005854 }
5855
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005856 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005857 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005858
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005859 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005860 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005861 }
5862
5863 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005864 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005865 /* finished. */
5866 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005867 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005868 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005869 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005870 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005871 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005872 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005873 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005874
5875 /* yield. */
5876 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005877 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005878 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005879 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005880 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005881 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005882 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005883 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005884 /* Some actions can be wake up when a "write" event
5885 * is detected on a response channel. This is useful
5886 * only for actions targetted on the requests.
5887 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005888 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005889 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01005890 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005891 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005892 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005893 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005894 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005895 /* We can quit the fcuntion without consistency check
5896 * because HAProxy is not able to manipulate data, it
5897 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005898 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005899
5900 /* finished with error. */
5901 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005902 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005903 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005904 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005905 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005906 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005907 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005908 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
5909 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005910 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005911
5912 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005913 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005914 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005915 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005916 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005917 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005918 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02005919 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005920
5921 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02005922 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01005923 }
5924}
5925
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005926struct task *hlua_applet_wakeup(struct task *t)
5927{
5928 struct appctx *ctx = t->context;
5929 struct stream_interface *si = ctx->owner;
5930
5931 /* If the applet is wake up without any expected work, the sheduler
5932 * remove it from the run queue. This flag indicate that the applet
5933 * is waiting for write. If the buffer is full, the main processing
5934 * will send some data and after call the applet, otherwise it call
5935 * the applet ASAP.
5936 */
5937 si_applet_cant_put(si);
5938 appctx_wakeup(ctx);
5939 return NULL;
5940}
5941
5942static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
5943{
5944 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01005945 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005946 struct task *task;
5947 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005948 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005949
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01005950 hlua = pool_alloc2(pool2_hlua);
5951 if (!hlua) {
5952 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
5953 ctx->rule->arg.hlua_rule->fcn.name);
5954 return 0;
5955 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005956 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01005957 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005958 ctx->ctx.hlua_apptcp.flags = 0;
5959
5960 /* Create task used by signal to wakeup applets. */
5961 task = task_new();
5962 if (!task) {
5963 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
5964 ctx->rule->arg.hlua_rule->fcn.name);
5965 return 0;
5966 }
5967 task->nice = 0;
5968 task->context = ctx;
5969 task->process = hlua_applet_wakeup;
5970 ctx->ctx.hlua_apptcp.task = task;
5971
5972 /* In the execution wrappers linked with a stream, the
5973 * Lua context can be not initialized. This behavior
5974 * permits to save performances because a systematic
5975 * Lua initialization cause 5% performances loss.
5976 */
5977 if (!hlua_ctx_init(hlua, task)) {
5978 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
5979 ctx->rule->arg.hlua_rule->fcn.name);
5980 return 0;
5981 }
5982
5983 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005984 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005985
5986 /* The following Lua calls can fail. */
5987 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01005988 if (lua_type(hlua->T, -1) == LUA_TSTRING)
5989 error = lua_tostring(hlua->T, -1);
5990 else
5991 error = "critical error";
5992 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
5993 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02005994 RESET_SAFE_LJMP(hlua->T);
5995 return 0;
5996 }
5997
5998 /* Check stack available size. */
5999 if (!lua_checkstack(hlua->T, 1)) {
6000 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6001 ctx->rule->arg.hlua_rule->fcn.name);
6002 RESET_SAFE_LJMP(hlua->T);
6003 return 0;
6004 }
6005
6006 /* Restore the function in the stack. */
6007 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6008
6009 /* Create and and push object stream in the stack. */
6010 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6011 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6012 ctx->rule->arg.hlua_rule->fcn.name);
6013 RESET_SAFE_LJMP(hlua->T);
6014 return 0;
6015 }
6016 hlua->nargs = 1;
6017
6018 /* push keywords in the stack. */
6019 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6020 if (!lua_checkstack(hlua->T, 1)) {
6021 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6022 ctx->rule->arg.hlua_rule->fcn.name);
6023 RESET_SAFE_LJMP(hlua->T);
6024 return 0;
6025 }
6026 lua_pushstring(hlua->T, *arg);
6027 hlua->nargs++;
6028 }
6029
6030 RESET_SAFE_LJMP(hlua->T);
6031
6032 /* Wakeup the applet ASAP. */
6033 si_applet_cant_get(si);
6034 si_applet_cant_put(si);
6035
6036 return 1;
6037}
6038
6039static void hlua_applet_tcp_fct(struct appctx *ctx)
6040{
6041 struct stream_interface *si = ctx->owner;
6042 struct stream *strm = si_strm(si);
6043 struct channel *res = si_ic(si);
6044 struct act_rule *rule = ctx->rule;
6045 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006046 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006047
6048 /* The applet execution is already done. */
6049 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE)
6050 return;
6051
6052 /* If the stream is disconnect or closed, ldo nothing. */
6053 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6054 return;
6055
6056 /* Execute the function. */
6057 switch (hlua_ctx_resume(hlua, 1)) {
6058 /* finished. */
6059 case HLUA_E_OK:
6060 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6061
6062 /* log time */
6063 strm->logs.tv_request = now;
6064
6065 /* eat the whole request */
6066 bo_skip(si_oc(si), si_ob(si)->o);
6067 res->flags |= CF_READ_NULL;
6068 si_shutr(si);
6069 return;
6070
6071 /* yield. */
6072 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006073 if (hlua->wake_time != TICK_ETERNITY)
6074 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006075 return;
6076
6077 /* finished with error. */
6078 case HLUA_E_ERRMSG:
6079 /* Display log. */
6080 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6081 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6082 lua_pop(hlua->T, 1);
6083 goto error;
6084
6085 case HLUA_E_ERR:
6086 /* Display log. */
6087 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6088 rule->arg.hlua_rule->fcn.name);
6089 goto error;
6090
6091 default:
6092 goto error;
6093 }
6094
6095error:
6096
6097 /* For all other cases, just close the stream. */
6098 si_shutw(si);
6099 si_shutr(si);
6100 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6101}
6102
6103static void hlua_applet_tcp_release(struct appctx *ctx)
6104{
6105 task_free(ctx->ctx.hlua_apptcp.task);
6106 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006107 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006108 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006109}
6110
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006111/* The function returns 1 if the initialisation is complete, 0 if
6112 * an errors occurs and -1 if more data are required for initializing
6113 * the applet.
6114 */
6115static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6116{
6117 struct stream_interface *si = ctx->owner;
6118 struct channel *req = si_oc(si);
6119 struct http_msg *msg;
6120 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006121 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006122 char **arg;
6123 struct hdr_ctx hdr;
6124 struct task *task;
6125 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006126 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006127
6128 /* Wait for a full HTTP request. */
6129 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6130 if (smp.flags & SMP_F_MAY_CHANGE)
6131 return -1;
6132 return 0;
6133 }
6134 txn = strm->txn;
6135 msg = &txn->req;
6136
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006137 /* We want two things in HTTP mode :
6138 * - enforce server-close mode if we were in keep-alive, so that the
6139 * applet is released after each response ;
6140 * - enable request body transfer to the applet in order to resync
6141 * with the response body.
6142 */
6143 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6144 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006145
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006146 hlua = pool_alloc2(pool2_hlua);
6147 if (!hlua) {
6148 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6149 ctx->rule->arg.hlua_rule->fcn.name);
6150 return 0;
6151 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006152 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006153 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006154 ctx->ctx.hlua_apphttp.left_bytes = -1;
6155 ctx->ctx.hlua_apphttp.flags = 0;
6156
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006157 if (txn->req.flags & HTTP_MSGF_VER_11)
6158 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6159
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006160 /* Create task used by signal to wakeup applets. */
6161 task = task_new();
6162 if (!task) {
6163 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6164 ctx->rule->arg.hlua_rule->fcn.name);
6165 return 0;
6166 }
6167 task->nice = 0;
6168 task->context = ctx;
6169 task->process = hlua_applet_wakeup;
6170 ctx->ctx.hlua_apphttp.task = task;
6171
6172 /* In the execution wrappers linked with a stream, the
6173 * Lua context can be not initialized. This behavior
6174 * permits to save performances because a systematic
6175 * Lua initialization cause 5% performances loss.
6176 */
6177 if (!hlua_ctx_init(hlua, task)) {
6178 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6179 ctx->rule->arg.hlua_rule->fcn.name);
6180 return 0;
6181 }
6182
6183 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006184 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006185
6186 /* The following Lua calls can fail. */
6187 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006188 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6189 error = lua_tostring(hlua->T, -1);
6190 else
6191 error = "critical error";
6192 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6193 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006194 return 0;
6195 }
6196
6197 /* Check stack available size. */
6198 if (!lua_checkstack(hlua->T, 1)) {
6199 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6200 ctx->rule->arg.hlua_rule->fcn.name);
6201 RESET_SAFE_LJMP(hlua->T);
6202 return 0;
6203 }
6204
6205 /* Restore the function in the stack. */
6206 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6207
6208 /* Create and and push object stream in the stack. */
6209 if (!hlua_applet_http_new(hlua->T, ctx)) {
6210 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6211 ctx->rule->arg.hlua_rule->fcn.name);
6212 RESET_SAFE_LJMP(hlua->T);
6213 return 0;
6214 }
6215 hlua->nargs = 1;
6216
6217 /* Look for a 100-continue expected. */
6218 if (msg->flags & HTTP_MSGF_VER_11) {
6219 hdr.idx = 0;
6220 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) &&
6221 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6222 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6223 }
6224
6225 /* push keywords in the stack. */
6226 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6227 if (!lua_checkstack(hlua->T, 1)) {
6228 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6229 ctx->rule->arg.hlua_rule->fcn.name);
6230 RESET_SAFE_LJMP(hlua->T);
6231 return 0;
6232 }
6233 lua_pushstring(hlua->T, *arg);
6234 hlua->nargs++;
6235 }
6236
6237 RESET_SAFE_LJMP(hlua->T);
6238
6239 /* Wakeup the applet when data is ready for read. */
6240 si_applet_cant_get(si);
6241
6242 return 1;
6243}
6244
6245static void hlua_applet_http_fct(struct appctx *ctx)
6246{
6247 struct stream_interface *si = ctx->owner;
6248 struct stream *strm = si_strm(si);
6249 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006250 struct act_rule *rule = ctx->rule;
6251 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006252 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006253 char *blk1;
6254 int len1;
6255 char *blk2;
6256 int len2;
6257 int ret;
6258
6259 /* If the stream is disconnect or closed, ldo nothing. */
6260 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6261 return;
6262
6263 /* Set the currently running flag. */
6264 if (!HLUA_IS_RUNNING(hlua) &&
6265 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6266
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006267 /* Wait for full HTTP analysys. */
6268 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
6269 si_applet_cant_get(si);
6270 return;
6271 }
6272
6273 /* Store the max amount of bytes that we can read. */
6274 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6275
6276 /* We need to flush the request header. This left the body
6277 * for the Lua.
6278 */
6279
6280 /* Read the maximum amount of data avalaible. */
6281 ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
6282 if (ret == -1)
6283 return;
6284
6285 /* No data available, ask for more data. */
6286 if (ret == 1)
6287 len2 = 0;
6288 if (ret == 0)
6289 len1 = 0;
6290 if (len1 + len2 < strm->txn->req.eoh + 2) {
6291 si_applet_cant_get(si);
6292 return;
6293 }
6294
6295 /* skip the requests bytes. */
6296 bo_skip(si_oc(si), strm->txn->req.eoh + 2);
6297 }
6298
6299 /* Executes The applet if it is not done. */
6300 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6301
6302 /* Execute the function. */
6303 switch (hlua_ctx_resume(hlua, 1)) {
6304 /* finished. */
6305 case HLUA_E_OK:
6306 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6307 break;
6308
6309 /* yield. */
6310 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006311 if (hlua->wake_time != TICK_ETERNITY)
6312 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006313 return;
6314
6315 /* finished with error. */
6316 case HLUA_E_ERRMSG:
6317 /* Display log. */
6318 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6319 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6320 lua_pop(hlua->T, 1);
6321 goto error;
6322
6323 case HLUA_E_ERR:
6324 /* Display log. */
6325 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6326 rule->arg.hlua_rule->fcn.name);
6327 goto error;
6328
6329 default:
6330 goto error;
6331 }
6332 }
6333
6334 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6335
6336 /* We must send the final chunk. */
6337 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6338 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6339
6340 /* sent last chunk at once. */
6341 ret = bi_putblk(res, "0\r\n\r\n", 5);
6342
6343 /* critical error. */
6344 if (ret == -2 || ret == -3) {
6345 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6346 rule->arg.hlua_rule->fcn.name);
6347 goto error;
6348 }
6349
6350 /* no enough space error. */
6351 if (ret == -1) {
6352 si_applet_cant_put(si);
6353 return;
6354 }
6355
6356 /* set the last chunk sent. */
6357 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6358 }
6359
6360 /* close the connection. */
6361
6362 /* status / log */
6363 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6364 strm->logs.tv_request = now;
6365
6366 /* eat the whole request */
6367 bo_skip(si_oc(si), si_ob(si)->o);
6368 res->flags |= CF_READ_NULL;
6369 si_shutr(si);
6370
6371 return;
6372 }
6373
6374error:
6375
6376 /* If we are in HTTP mode, and we are not send any
6377 * data, return a 500 server error in best effort:
6378 * if there are no room avalaible in the buffer,
6379 * just close the connection.
6380 */
6381 bi_putblk(res, error_500, strlen(error_500));
6382 if (!(strm->flags & SF_ERR_MASK))
6383 strm->flags |= SF_ERR_RESOURCE;
6384 si_shutw(si);
6385 si_shutr(si);
6386 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6387}
6388
6389static void hlua_applet_http_release(struct appctx *ctx)
6390{
6391 task_free(ctx->ctx.hlua_apphttp.task);
6392 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006393 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006394 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006395}
6396
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006397/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6398 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006399 *
6400 * This function can fail with an abort() due to an Lua critical error.
6401 * We are in the configuration parsing process of HAProxy, this abort() is
6402 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006403 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006404static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6405 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006406{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006407 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006408 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006409
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006410 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006411 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006412 if (!rule->arg.hlua_rule) {
6413 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006414 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006415 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006416
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006417 /* Memory for arguments. */
6418 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6419 if (!rule->arg.hlua_rule->args) {
6420 memprintf(err, "out of memory error");
6421 return ACT_RET_PRS_ERR;
6422 }
6423
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006424 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006425 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006426
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006427 /* Expect some arguments */
6428 for (i = 0; i < fcn->nargs; i++) {
6429 if (*args[i+1] == '\0') {
6430 memprintf(err, "expect %d arguments", fcn->nargs);
6431 return ACT_RET_PRS_ERR;
6432 }
6433 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6434 if (!rule->arg.hlua_rule->args[i]) {
6435 memprintf(err, "out of memory error");
6436 return ACT_RET_PRS_ERR;
6437 }
6438 (*cur_arg)++;
6439 }
6440 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006441
Thierry FOURNIER42148732015-09-02 17:17:33 +02006442 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006443 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006444 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006445}
6446
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006447static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6448 struct act_rule *rule, char **err)
6449{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006450 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006451
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006452 /* HTTP applets are forbidden in tcp-request rules.
6453 * HTTP applet request requires everything initilized by
6454 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6455 * The applet will be immediately initilized, but its before
6456 * the call of this analyzer.
6457 */
6458 if (rule->from != ACT_F_HTTP_REQ) {
6459 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6460 return ACT_RET_PRS_ERR;
6461 }
6462
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006463 /* Memory for the rule. */
6464 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6465 if (!rule->arg.hlua_rule) {
6466 memprintf(err, "out of memory error");
6467 return ACT_RET_PRS_ERR;
6468 }
6469
6470 /* Reference the Lua function and store the reference. */
6471 rule->arg.hlua_rule->fcn = *fcn;
6472
6473 /* TODO: later accept arguments. */
6474 rule->arg.hlua_rule->args = NULL;
6475
6476 /* Add applet pointer in the rule. */
6477 rule->applet.obj_type = OBJ_TYPE_APPLET;
6478 rule->applet.name = fcn->name;
6479 rule->applet.init = hlua_applet_http_init;
6480 rule->applet.fct = hlua_applet_http_fct;
6481 rule->applet.release = hlua_applet_http_release;
6482 rule->applet.timeout = hlua_timeout_applet;
6483
6484 return ACT_RET_PRS_OK;
6485}
6486
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006487/* This function is an LUA binding used for registering
6488 * "sample-conv" functions. It expects a converter name used
6489 * in the haproxy configuration file, and an LUA function.
6490 */
6491__LJMP static int hlua_register_action(lua_State *L)
6492{
6493 struct action_kw_list *akl;
6494 const char *name;
6495 int ref;
6496 int len;
6497 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006498 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006499
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006500 /* Initialise the number of expected arguments at 0. */
6501 nargs = 0;
6502
6503 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6504 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006505
6506 /* First argument : converter name. */
6507 name = MAY_LJMP(luaL_checkstring(L, 1));
6508
6509 /* Second argument : environment. */
6510 if (lua_type(L, 2) != LUA_TTABLE)
6511 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6512
6513 /* Third argument : lua function. */
6514 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6515
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006516 /* Fouth argument : number of mandatories arguments expected on the configuration line. */
6517 if (lua_gettop(L) >= 4)
6518 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6519
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006520 /* browse the second argulent as an array. */
6521 lua_pushnil(L);
6522 while (lua_next(L, 2) != 0) {
6523 if (lua_type(L, -1) != LUA_TSTRING)
6524 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6525
6526 /* Check required environment. Only accepted "http" or "tcp". */
6527 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006528 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006529 if (!akl)
6530 WILL_LJMP(luaL_error(L, "lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006531 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006532 if (!fcn)
6533 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6534
6535 /* Fill fcn. */
6536 fcn->name = strdup(name);
6537 if (!fcn->name)
6538 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6539 fcn->function_ref = ref;
6540
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006541 /* Set the expected number od arguments. */
6542 fcn->nargs = nargs;
6543
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006544 /* List head */
6545 akl->list.n = akl->list.p = NULL;
6546
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006547 /* action keyword. */
6548 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006549 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006550 if (!akl->kw[0].kw)
6551 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6552
6553 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6554
6555 akl->kw[0].match_pfx = 0;
6556 akl->kw[0].private = fcn;
6557 akl->kw[0].parse = action_register_lua;
6558
6559 /* select the action registering point. */
6560 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6561 tcp_req_cont_keywords_register(akl);
6562 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6563 tcp_res_cont_keywords_register(akl);
6564 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6565 http_req_keywords_register(akl);
6566 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6567 http_res_keywords_register(akl);
6568 else
6569 WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. "
6570 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6571 "are expected.", lua_tostring(L, -1)));
6572
6573 /* pop the environment string. */
6574 lua_pop(L, 1);
6575 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006576 return ACT_RET_PRS_OK;
6577}
6578
6579static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6580 struct act_rule *rule, char **err)
6581{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006582 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006583
6584 /* Memory for the rule. */
6585 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6586 if (!rule->arg.hlua_rule) {
6587 memprintf(err, "out of memory error");
6588 return ACT_RET_PRS_ERR;
6589 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006590
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006591 /* Reference the Lua function and store the reference. */
6592 rule->arg.hlua_rule->fcn = *fcn;
6593
6594 /* TODO: later accept arguments. */
6595 rule->arg.hlua_rule->args = NULL;
6596
6597 /* Add applet pointer in the rule. */
6598 rule->applet.obj_type = OBJ_TYPE_APPLET;
6599 rule->applet.name = fcn->name;
6600 rule->applet.init = hlua_applet_tcp_init;
6601 rule->applet.fct = hlua_applet_tcp_fct;
6602 rule->applet.release = hlua_applet_tcp_release;
6603 rule->applet.timeout = hlua_timeout_applet;
6604
6605 return 0;
6606}
6607
6608/* This function is an LUA binding used for registering
6609 * "sample-conv" functions. It expects a converter name used
6610 * in the haproxy configuration file, and an LUA function.
6611 */
6612__LJMP static int hlua_register_service(lua_State *L)
6613{
6614 struct action_kw_list *akl;
6615 const char *name;
6616 const char *env;
6617 int ref;
6618 int len;
6619 struct hlua_function *fcn;
6620
6621 MAY_LJMP(check_args(L, 3, "register_service"));
6622
6623 /* First argument : converter name. */
6624 name = MAY_LJMP(luaL_checkstring(L, 1));
6625
6626 /* Second argument : environment. */
6627 env = MAY_LJMP(luaL_checkstring(L, 2));
6628
6629 /* Third argument : lua function. */
6630 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6631
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006632 /* Allocate and fill the sample fetch keyword struct. */
6633 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
6634 if (!akl)
6635 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6636 fcn = calloc(1, sizeof(*fcn));
6637 if (!fcn)
6638 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6639
6640 /* Fill fcn. */
6641 len = strlen("<lua.>") + strlen(name) + 1;
6642 fcn->name = calloc(1, len);
6643 if (!fcn->name)
6644 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6645 snprintf((char *)fcn->name, len, "<lua.%s>", name);
6646 fcn->function_ref = ref;
6647
6648 /* List head */
6649 akl->list.n = akl->list.p = NULL;
6650
6651 /* converter keyword. */
6652 len = strlen("lua.") + strlen(name) + 1;
6653 akl->kw[0].kw = calloc(1, len);
6654 if (!akl->kw[0].kw)
6655 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6656
6657 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6658
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01006659 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006660 if (strcmp(env, "tcp") == 0)
6661 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006662 else if (strcmp(env, "http") == 0)
6663 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006664 else
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006665 WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. "
6666 "'tcp' or 'http' are expected."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006667
6668 akl->kw[0].match_pfx = 0;
6669 akl->kw[0].private = fcn;
6670
6671 /* End of array. */
6672 memset(&akl->kw[1], 0, sizeof(*akl->kw));
6673
6674 /* Register this new converter */
6675 service_keywords_register(akl);
6676
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006677 return 0;
6678}
6679
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006680/* This function initialises Lua cli handler. It copies the
6681 * arguments in the Lua stack and create channel IO objects.
6682 */
6683static int hlua_cli_parse_fct(char **args, struct appctx *appctx, void *private)
6684{
6685 struct hlua *hlua;
6686 struct hlua_function *fcn;
6687 int i;
6688 const char *error;
6689
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006690 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006691 appctx->ctx.hlua_cli.fcn = private;
6692
6693 hlua = pool_alloc2(pool2_hlua);
6694 if (!hlua) {
6695 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006696 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006697 }
6698 HLUA_INIT(hlua);
6699 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006700
6701 /* Create task used by signal to wakeup applets.
6702 * We use the same wakeup fonction than the Lua applet_tcp and
6703 * applet_http. It is absolutely compatible.
6704 */
6705 appctx->ctx.hlua_cli.task = task_new();
6706 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01006707 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006708 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006709 }
6710 appctx->ctx.hlua_cli.task->nice = 0;
6711 appctx->ctx.hlua_cli.task->context = appctx;
6712 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
6713
6714 /* Initialises the Lua context */
6715 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
6716 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006717 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006718 }
6719
6720 /* The following Lua calls can fail. */
6721 if (!SET_SAFE_LJMP(hlua->T)) {
6722 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6723 error = lua_tostring(hlua->T, -1);
6724 else
6725 error = "critical error";
6726 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
6727 goto error;
6728 }
6729
6730 /* Check stack available size. */
6731 if (!lua_checkstack(hlua->T, 2)) {
6732 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6733 goto error;
6734 }
6735
6736 /* Restore the function in the stack. */
6737 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
6738
6739 /* Once the arguments parsed, the CLI is like an AppletTCP,
6740 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006741 */
6742 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
6743 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6744 goto error;
6745 }
6746 hlua->nargs = 1;
6747
6748 /* push keywords in the stack. */
6749 for (i = 0; *args[i]; i++) {
6750 /* Check stack available size. */
6751 if (!lua_checkstack(hlua->T, 1)) {
6752 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
6753 goto error;
6754 }
6755 lua_pushstring(hlua->T, args[i]);
6756 hlua->nargs++;
6757 }
6758
6759 /* We must initialize the execution timeouts. */
6760 hlua->max_time = hlua_timeout_session;
6761
6762 /* At this point the execution is safe. */
6763 RESET_SAFE_LJMP(hlua->T);
6764
6765 /* It's ok */
6766 return 0;
6767
6768 /* It's not ok. */
6769error:
6770 RESET_SAFE_LJMP(hlua->T);
6771 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01006772 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006773 return 1;
6774}
6775
6776static int hlua_cli_io_handler_fct(struct appctx *appctx)
6777{
6778 struct hlua *hlua;
6779 struct stream_interface *si;
6780 struct hlua_function *fcn;
6781
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006782 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006783 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01006784 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006785
6786 /* If the stream is disconnect or closed, ldo nothing. */
6787 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6788 return 1;
6789
6790 /* Execute the function. */
6791 switch (hlua_ctx_resume(hlua, 1)) {
6792
6793 /* finished. */
6794 case HLUA_E_OK:
6795 return 1;
6796
6797 /* yield. */
6798 case HLUA_E_AGAIN:
6799 /* We want write. */
6800 if (HLUA_IS_WAKERESWR(hlua))
6801 si_applet_cant_put(si);
6802 /* Set the timeout. */
6803 if (hlua->wake_time != TICK_ETERNITY)
6804 task_schedule(hlua->task, hlua->wake_time);
6805 return 0;
6806
6807 /* finished with error. */
6808 case HLUA_E_ERRMSG:
6809 /* Display log. */
6810 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
6811 fcn->name, lua_tostring(hlua->T, -1));
6812 lua_pop(hlua->T, 1);
6813 return 1;
6814
6815 case HLUA_E_ERR:
6816 /* Display log. */
6817 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
6818 fcn->name);
6819 return 1;
6820
6821 default:
6822 return 1;
6823 }
6824
6825 return 1;
6826}
6827
6828static void hlua_cli_io_release_fct(struct appctx *appctx)
6829{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006830 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006831 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01006832}
6833
6834/* This function is an LUA binding used for registering
6835 * new keywords in the cli. It expects a list of keywords
6836 * which are the "path". It is limited to 5 keywords. A
6837 * description of the command, a function to be executed
6838 * for the parsing and a function for io handlers.
6839 */
6840__LJMP static int hlua_register_cli(lua_State *L)
6841{
6842 struct cli_kw_list *cli_kws;
6843 const char *message;
6844 int ref_io;
6845 int len;
6846 struct hlua_function *fcn;
6847 int index;
6848 int i;
6849
6850 MAY_LJMP(check_args(L, 3, "register_cli"));
6851
6852 /* First argument : an array of maximum 5 keywords. */
6853 if (!lua_istable(L, 1))
6854 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
6855
6856 /* Second argument : string with contextual message. */
6857 message = MAY_LJMP(luaL_checkstring(L, 2));
6858
6859 /* Third and fourth argument : lua function. */
6860 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
6861
6862 /* Allocate and fill the sample fetch keyword struct. */
6863 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
6864 if (!cli_kws)
6865 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6866 fcn = calloc(1, sizeof(*fcn));
6867 if (!fcn)
6868 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6869
6870 /* Fill path. */
6871 index = 0;
6872 lua_pushnil(L);
6873 while(lua_next(L, 1) != 0) {
6874 if (index >= 5)
6875 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
6876 if (lua_type(L, -1) != LUA_TSTRING)
6877 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
6878 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
6879 if (!cli_kws->kw[0].str_kw[index])
6880 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6881 index++;
6882 lua_pop(L, 1);
6883 }
6884
6885 /* Copy help message. */
6886 cli_kws->kw[0].usage = strdup(message);
6887 if (!cli_kws->kw[0].usage)
6888 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6889
6890 /* Fill fcn io handler. */
6891 len = strlen("<lua.cli>") + 1;
6892 for (i = 0; i < index; i++)
6893 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
6894 fcn->name = calloc(1, len);
6895 if (!fcn->name)
6896 WILL_LJMP(luaL_error(L, "lua out of memory error."));
6897 strncat((char *)fcn->name, "<lua.cli", len);
6898 for (i = 0; i < index; i++) {
6899 strncat((char *)fcn->name, ".", len);
6900 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
6901 }
6902 strncat((char *)fcn->name, ">", len);
6903 fcn->function_ref = ref_io;
6904
6905 /* Fill last entries. */
6906 cli_kws->kw[0].private = fcn;
6907 cli_kws->kw[0].parse = hlua_cli_parse_fct;
6908 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
6909 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
6910
6911 /* Register this new converter */
6912 cli_register_kw(cli_kws);
6913
6914 return 0;
6915}
6916
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006917static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
6918 struct proxy *defpx, const char *file, int line,
6919 char **err, unsigned int *timeout)
6920{
6921 const char *error;
6922
6923 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
6924 if (error && *error != '\0') {
6925 memprintf(err, "%s: invalid timeout", args[0]);
6926 return -1;
6927 }
6928 return 0;
6929}
6930
6931static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
6932 struct proxy *defpx, const char *file, int line,
6933 char **err)
6934{
6935 return hlua_read_timeout(args, section_type, curpx, defpx,
6936 file, line, err, &hlua_timeout_session);
6937}
6938
6939static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
6940 struct proxy *defpx, const char *file, int line,
6941 char **err)
6942{
6943 return hlua_read_timeout(args, section_type, curpx, defpx,
6944 file, line, err, &hlua_timeout_task);
6945}
6946
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006947static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
6948 struct proxy *defpx, const char *file, int line,
6949 char **err)
6950{
6951 return hlua_read_timeout(args, section_type, curpx, defpx,
6952 file, line, err, &hlua_timeout_applet);
6953}
6954
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01006955static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
6956 struct proxy *defpx, const char *file, int line,
6957 char **err)
6958{
6959 char *error;
6960
6961 hlua_nb_instruction = strtoll(args[1], &error, 10);
6962 if (*error != '\0') {
6963 memprintf(err, "%s: invalid number", args[0]);
6964 return -1;
6965 }
6966 return 0;
6967}
6968
Willy Tarreau32f61e22015-03-18 17:54:59 +01006969static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
6970 struct proxy *defpx, const char *file, int line,
6971 char **err)
6972{
6973 char *error;
6974
6975 if (*(args[1]) == 0) {
6976 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
6977 return -1;
6978 }
6979 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
6980 if (*error != '\0') {
6981 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
6982 return -1;
6983 }
6984 return 0;
6985}
6986
6987
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01006988/* This function is called by the main configuration key "lua-load". It loads and
6989 * execute an lua file during the parsing of the HAProxy configuration file. It is
6990 * the main lua entry point.
6991 *
6992 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
6993 * occured, otherwise it returns 0.
6994 *
6995 * In some error case, LUA set an error message in top of the stack. This function
6996 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006997 *
6998 * This function can fail with an abort() due to an Lua critical error.
6999 * We are in the configuration parsing process of HAProxy, this abort() is
7000 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007001 */
7002static int hlua_load(char **args, int section_type, struct proxy *curpx,
7003 struct proxy *defpx, const char *file, int line,
7004 char **err)
7005{
7006 int error;
7007
7008 /* Just load and compile the file. */
7009 error = luaL_loadfile(gL.T, args[1]);
7010 if (error) {
7011 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
7012 lua_pop(gL.T, 1);
7013 return -1;
7014 }
7015
7016 /* If no syntax error where detected, execute the code. */
7017 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7018 switch (error) {
7019 case LUA_OK:
7020 break;
7021 case LUA_ERRRUN:
7022 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
7023 lua_pop(gL.T, 1);
7024 return -1;
7025 case LUA_ERRMEM:
7026 memprintf(err, "lua out of memory error\n");
7027 return -1;
7028 case LUA_ERRERR:
7029 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
7030 lua_pop(gL.T, 1);
7031 return -1;
7032 case LUA_ERRGCMM:
7033 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
7034 lua_pop(gL.T, 1);
7035 return -1;
7036 default:
7037 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
7038 lua_pop(gL.T, 1);
7039 return -1;
7040 }
7041
7042 return 0;
7043}
7044
7045/* configuration keywords declaration */
7046static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007047 { CFG_GLOBAL, "lua-load", hlua_load },
7048 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7049 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007050 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007051 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007052 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007053 { 0, NULL, NULL },
7054}};
7055
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007056/* This function can fail with an abort() due to an Lua critical error.
7057 * We are in the initialisation process of HAProxy, this abort() is
7058 * tolerated.
7059 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007060int hlua_post_init()
7061{
7062 struct hlua_init_function *init;
7063 const char *msg;
7064 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007065 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007066
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007067 /* Call post initialisation function in safe environement. */
7068 if (!SET_SAFE_LJMP(gL.T)) {
7069 if (lua_type(gL.T, -1) == LUA_TSTRING)
7070 error = lua_tostring(gL.T, -1);
7071 else
7072 error = "critical error";
7073 fprintf(stderr, "Lua post-init: %s.\n", error);
7074 exit(1);
7075 }
7076 hlua_fcn_post_init(gL.T);
7077 RESET_SAFE_LJMP(gL.T);
7078
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007079 list_for_each_entry(init, &hlua_init_functions, l) {
7080 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7081 ret = hlua_ctx_resume(&gL, 0);
7082 switch (ret) {
7083 case HLUA_E_OK:
7084 lua_pop(gL.T, -1);
7085 return 1;
7086 case HLUA_E_AGAIN:
7087 Alert("lua init: yield not allowed.\n");
7088 return 0;
7089 case HLUA_E_ERRMSG:
7090 msg = lua_tostring(gL.T, -1);
7091 Alert("lua init: %s.\n", msg);
7092 return 0;
7093 case HLUA_E_ERR:
7094 default:
7095 Alert("lua init: unknown runtime error.\n");
7096 return 0;
7097 }
7098 }
7099 return 1;
7100}
7101
Willy Tarreau32f61e22015-03-18 17:54:59 +01007102/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7103 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7104 * is the previously allocated size or the kind of object in case of a new
7105 * allocation. <nsize> is the requested new size.
7106 */
7107static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7108{
7109 struct hlua_mem_allocator *zone = ud;
7110
7111 if (nsize == 0) {
7112 /* it's a free */
7113 if (ptr)
7114 zone->allocated -= osize;
7115 free(ptr);
7116 return NULL;
7117 }
7118
7119 if (!ptr) {
7120 /* it's a new allocation */
7121 if (zone->limit && zone->allocated + nsize > zone->limit)
7122 return NULL;
7123
7124 ptr = malloc(nsize);
7125 if (ptr)
7126 zone->allocated += nsize;
7127 return ptr;
7128 }
7129
7130 /* it's a realloc */
7131 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7132 return NULL;
7133
7134 ptr = realloc(ptr, nsize);
7135 if (ptr)
7136 zone->allocated += nsize - osize;
7137 return ptr;
7138}
7139
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007140/* Ithis function can fail with an abort() due to an Lua critical error.
7141 * We are in the initialisation process of HAProxy, this abort() is
7142 * tolerated.
7143 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007144void hlua_init(void)
7145{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007146 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007147 int idx;
7148 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007149 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007150 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007151 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007152#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007153 struct srv_kw *kw;
7154 int tmp_error;
7155 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007156 char *args[] = { /* SSL client configuration. */
7157 "ssl",
7158 "verify",
7159 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007160 NULL
7161 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007162#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007163
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007164 /* Initialise struct hlua and com signals pool */
7165 pool2_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007166 pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED);
7167
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007168 /* Register configuration keywords. */
7169 cfg_register_keywords(&cfg_kws);
7170
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007171 /* Init main lua stack. */
7172 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007173 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007174 LIST_INIT(&gL.com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007175 gL.T = luaL_newstate();
7176 hlua_sethlua(&gL);
7177 gL.Tref = LUA_REFNIL;
7178 gL.task = NULL;
7179
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007180 /* From this point, until the end of the initialisation fucntion,
7181 * the Lua function can fail with an abort. We are in the initialisation
7182 * process of HAProxy, this abort() is tolerated.
7183 */
7184
Willy Tarreau32f61e22015-03-18 17:54:59 +01007185 /* change the memory allocators to track memory usage */
7186 lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator);
7187
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007188 /* Initialise lua. */
7189 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007190
Thierry Fournier75933d42016-01-21 09:30:18 +01007191 /* Set safe environment for the initialisation. */
7192 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007193 if (lua_type(gL.T, -1) == LUA_TSTRING)
7194 error_msg = lua_tostring(gL.T, -1);
7195 else
7196 error_msg = "critical error";
7197 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007198 exit(1);
7199 }
7200
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007201 /*
7202 *
7203 * Create "core" object.
7204 *
7205 */
7206
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007207 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007208 lua_newtable(gL.T);
7209
7210 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007211 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007212 hlua_class_const_int(gL.T, log_levels[i], i);
7213
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007214 /* Register special functions. */
7215 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007216 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007217 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007218 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007219 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007220 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007221 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007222 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007223 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007224 hlua_class_function(gL.T, "sleep", hlua_sleep);
7225 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007226 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7227 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7228 hlua_class_function(gL.T, "set_map", hlua_set_map);
7229 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007230 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007231 hlua_class_function(gL.T, "log", hlua_log);
7232 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7233 hlua_class_function(gL.T, "Info", hlua_log_info);
7234 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7235 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007236 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007237 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007238
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007239 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007240
7241 /*
7242 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007243 * Register class Map
7244 *
7245 */
7246
7247 /* This table entry is the object "Map" base. */
7248 lua_newtable(gL.T);
7249
7250 /* register pattern types. */
7251 for (i=0; i<PAT_MATCH_NUM; i++)
7252 hlua_class_const_int(gL.T, pat_match_names[i], i);
7253
7254 /* register constructor. */
7255 hlua_class_function(gL.T, "new", hlua_map_new);
7256
7257 /* Create and fill the metatable. */
7258 lua_newtable(gL.T);
7259
7260 /* Create and fille the __index entry. */
7261 lua_pushstring(gL.T, "__index");
7262 lua_newtable(gL.T);
7263
7264 /* Register . */
7265 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7266 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7267
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007268 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007269
Thierry Fournier45e78d72016-02-19 18:34:46 +01007270 /* Register previous table in the registry with reference and named entry.
7271 * The function hlua_register_metatable() pops the stack, so we
7272 * previously create a copy of the table.
7273 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007274 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007275 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007276
7277 /* Assign the metatable to the mai Map object. */
7278 lua_setmetatable(gL.T, -2);
7279
7280 /* Set a name to the table. */
7281 lua_setglobal(gL.T, "Map");
7282
7283 /*
7284 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007285 * Register class Channel
7286 *
7287 */
7288
7289 /* Create and fill the metatable. */
7290 lua_newtable(gL.T);
7291
7292 /* Create and fille the __index entry. */
7293 lua_pushstring(gL.T, "__index");
7294 lua_newtable(gL.T);
7295
7296 /* Register . */
7297 hlua_class_function(gL.T, "get", hlua_channel_get);
7298 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7299 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7300 hlua_class_function(gL.T, "set", hlua_channel_set);
7301 hlua_class_function(gL.T, "append", hlua_channel_append);
7302 hlua_class_function(gL.T, "send", hlua_channel_send);
7303 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7304 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7305 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007306 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007307
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007308 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007309
7310 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007311 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007312
7313 /*
7314 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007315 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007316 *
7317 */
7318
7319 /* Create and fill the metatable. */
7320 lua_newtable(gL.T);
7321
7322 /* Create and fille the __index entry. */
7323 lua_pushstring(gL.T, "__index");
7324 lua_newtable(gL.T);
7325
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007326 /* Browse existing fetches and create the associated
7327 * object method.
7328 */
7329 sf = NULL;
7330 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7331
7332 /* Dont register the keywork if the arguments check function are
7333 * not safe during the runtime.
7334 */
7335 if ((sf->val_args != NULL) &&
7336 (sf->val_args != val_payload_lv) &&
7337 (sf->val_args != val_hdr))
7338 continue;
7339
7340 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7341 * by an underscore.
7342 */
7343 strncpy(trash.str, sf->kw, trash.size);
7344 trash.str[trash.size - 1] = '\0';
7345 for (p = trash.str; *p; p++)
7346 if (*p == '.' || *p == '-' || *p == '+')
7347 *p = '_';
7348
7349 /* Register the function. */
7350 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007351 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007352 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007353 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007354 }
7355
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007356 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007357
7358 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007359 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007360
7361 /*
7362 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007363 * Register class Converters
7364 *
7365 */
7366
7367 /* Create and fill the metatable. */
7368 lua_newtable(gL.T);
7369
7370 /* Create and fill the __index entry. */
7371 lua_pushstring(gL.T, "__index");
7372 lua_newtable(gL.T);
7373
7374 /* Browse existing converters and create the associated
7375 * object method.
7376 */
7377 sc = NULL;
7378 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7379 /* Dont register the keywork if the arguments check function are
7380 * not safe during the runtime.
7381 */
7382 if (sc->val_args != NULL)
7383 continue;
7384
7385 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7386 * by an underscore.
7387 */
7388 strncpy(trash.str, sc->kw, trash.size);
7389 trash.str[trash.size - 1] = '\0';
7390 for (p = trash.str; *p; p++)
7391 if (*p == '.' || *p == '-' || *p == '+')
7392 *p = '_';
7393
7394 /* Register the function. */
7395 lua_pushstring(gL.T, trash.str);
7396 lua_pushlightuserdata(gL.T, sc);
7397 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007398 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007399 }
7400
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007401 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007402
7403 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007404 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007405
7406 /*
7407 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007408 * Register class HTTP
7409 *
7410 */
7411
7412 /* Create and fill the metatable. */
7413 lua_newtable(gL.T);
7414
7415 /* Create and fille the __index entry. */
7416 lua_pushstring(gL.T, "__index");
7417 lua_newtable(gL.T);
7418
7419 /* Register Lua functions. */
7420 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7421 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7422 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7423 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7424 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7425 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7426 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7427 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7428 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7429 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7430
7431 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7432 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7433 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7434 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7435 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7436 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007437 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007438
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007439 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007440
7441 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007442 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007443
7444 /*
7445 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007446 * Register class AppletTCP
7447 *
7448 */
7449
7450 /* Create and fill the metatable. */
7451 lua_newtable(gL.T);
7452
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007453 /* Create and fille the __index entry. */
7454 lua_pushstring(gL.T, "__index");
7455 lua_newtable(gL.T);
7456
7457 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007458 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7459 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7460 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7461 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7462 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007463 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7464 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7465 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007466
7467 lua_settable(gL.T, -3);
7468
7469 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007470 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007471
7472 /*
7473 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007474 * Register class AppletHTTP
7475 *
7476 */
7477
7478 /* Create and fill the metatable. */
7479 lua_newtable(gL.T);
7480
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007481 /* Create and fille the __index entry. */
7482 lua_pushstring(gL.T, "__index");
7483 lua_newtable(gL.T);
7484
7485 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007486 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7487 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007488 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7489 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7490 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007491 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7492 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7493 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7494 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7495 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7496 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7497
7498 lua_settable(gL.T, -3);
7499
7500 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007501 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007502
7503 /*
7504 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007505 * Register class TXN
7506 *
7507 */
7508
7509 /* Create and fill the metatable. */
7510 lua_newtable(gL.T);
7511
7512 /* Create and fille the __index entry. */
7513 lua_pushstring(gL.T, "__index");
7514 lua_newtable(gL.T);
7515
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007516 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01007517 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7518 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007519 hlua_class_function(gL.T, "set_var", hlua_set_var);
Christopher Faulet85d79c92016-11-09 16:54:56 +01007520 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007521 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007522 hlua_class_function(gL.T, "done", hlua_txn_done);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007523 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
7524 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7525 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007526 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7527 hlua_class_function(gL.T, "log", hlua_txn_log);
7528 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7529 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7530 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7531 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007532
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007533 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007534
7535 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007536 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007537
7538 /*
7539 *
7540 * Register class Socket
7541 *
7542 */
7543
7544 /* Create and fill the metatable. */
7545 lua_newtable(gL.T);
7546
7547 /* Create and fille the __index entry. */
7548 lua_pushstring(gL.T, "__index");
7549 lua_newtable(gL.T);
7550
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007551#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007552 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007553#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007554 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7555 hlua_class_function(gL.T, "send", hlua_socket_send);
7556 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7557 hlua_class_function(gL.T, "close", hlua_socket_close);
7558 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7559 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7560 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7561 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7562
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007563 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007564
7565 /* Register the garbage collector entry. */
7566 lua_pushstring(gL.T, "__gc");
7567 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007568 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007569
7570 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007571 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007572
7573 /* Proxy and server configuration initialisation. */
7574 memset(&socket_proxy, 0, sizeof(socket_proxy));
7575 init_new_proxy(&socket_proxy);
7576 socket_proxy.parent = NULL;
7577 socket_proxy.last_change = now.tv_sec;
7578 socket_proxy.id = "LUA-SOCKET";
7579 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
7580 socket_proxy.maxconn = 0;
7581 socket_proxy.accept = NULL;
7582 socket_proxy.options2 |= PR_O2_INDEPSTR;
7583 socket_proxy.srv = NULL;
7584 socket_proxy.conn_retries = 0;
7585 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
7586
7587 /* Init TCP server: unchanged parameters */
7588 memset(&socket_tcp, 0, sizeof(socket_tcp));
7589 socket_tcp.next = NULL;
7590 socket_tcp.proxy = &socket_proxy;
7591 socket_tcp.obj_type = OBJ_TYPE_SERVER;
7592 LIST_INIT(&socket_tcp.actconns);
7593 LIST_INIT(&socket_tcp.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02007594 LIST_INIT(&socket_tcp.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02007595 LIST_INIT(&socket_tcp.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02007596 LIST_INIT(&socket_tcp.safe_conns);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007597 socket_tcp.state = SRV_ST_RUNNING; /* early server setup */
7598 socket_tcp.last_change = 0;
7599 socket_tcp.id = "LUA-TCP-CONN";
7600 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7601 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7602 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
7603
7604 /* XXX: Copy default parameter from default server,
7605 * but the default server is not initialized.
7606 */
7607 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
7608 socket_tcp.minconn = socket_proxy.defsrv.minconn;
7609 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
7610 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
7611 socket_tcp.onerror = socket_proxy.defsrv.onerror;
7612 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
7613 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
7614 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
7615 socket_tcp.uweight = socket_proxy.defsrv.iweight;
7616 socket_tcp.iweight = socket_proxy.defsrv.iweight;
7617
7618 socket_tcp.check.status = HCHK_STATUS_INI;
7619 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
7620 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
7621 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
7622 socket_tcp.check.server = &socket_tcp;
7623
7624 socket_tcp.agent.status = HCHK_STATUS_INI;
7625 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
7626 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
7627 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
7628 socket_tcp.agent.server = &socket_tcp;
7629
Willy Tarreaua261e9b2016-12-22 20:44:00 +01007630 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007631
7632#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007633 /* Init TCP server: unchanged parameters */
7634 memset(&socket_ssl, 0, sizeof(socket_ssl));
7635 socket_ssl.next = NULL;
7636 socket_ssl.proxy = &socket_proxy;
7637 socket_ssl.obj_type = OBJ_TYPE_SERVER;
7638 LIST_INIT(&socket_ssl.actconns);
7639 LIST_INIT(&socket_ssl.pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02007640 LIST_INIT(&socket_ssl.priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02007641 LIST_INIT(&socket_ssl.idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02007642 LIST_INIT(&socket_ssl.safe_conns);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007643 socket_ssl.state = SRV_ST_RUNNING; /* early server setup */
7644 socket_ssl.last_change = 0;
7645 socket_ssl.id = "LUA-SSL-CONN";
7646 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7647 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
7648 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
7649
7650 /* XXX: Copy default parameter from default server,
7651 * but the default server is not initialized.
7652 */
7653 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
7654 socket_ssl.minconn = socket_proxy.defsrv.minconn;
7655 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
7656 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
7657 socket_ssl.onerror = socket_proxy.defsrv.onerror;
7658 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
7659 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
7660 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
7661 socket_ssl.uweight = socket_proxy.defsrv.iweight;
7662 socket_ssl.iweight = socket_proxy.defsrv.iweight;
7663
7664 socket_ssl.check.status = HCHK_STATUS_INI;
7665 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
7666 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
7667 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
7668 socket_ssl.check.server = &socket_ssl;
7669
7670 socket_ssl.agent.status = HCHK_STATUS_INI;
7671 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
7672 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
7673 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
7674 socket_ssl.agent.server = &socket_ssl;
7675
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007676 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01007677 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007678
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007679 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007680 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
7681 /*
7682 *
7683 * If the keyword is not known, we can search in the registered
7684 * server keywords. This is usefull to configure special SSL
7685 * features like client certificates and ssl_verify.
7686 *
7687 */
7688 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
7689 if (tmp_error != 0) {
7690 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
7691 abort(); /* This must be never arrives because the command line
7692 not editable by the user. */
7693 }
7694 idx += kw->skip;
7695 }
7696 }
7697
7698 /* Initialize SSL server. */
Willy Tarreau17d45382016-12-22 21:16:08 +01007699 if (socket_ssl.xprt->prepare_srv)
7700 socket_ssl.xprt->prepare_srv(&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}