blob: 967f05e3ab66592ecb544bc6703c0955d01a1367 [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>
Mark Lakes56cc1252018-03-27 09:48:06 +020014#include <limits.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020015#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010016
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010017#include <lauxlib.h>
18#include <lua.h>
19#include <lualib.h>
20
Thierry FOURNIER463119c2015-03-10 00:35:36 +010021#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
22#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010023#endif
24
Thierry FOURNIER380d0932015-01-23 14:27:52 +010025#include <ebpttree.h>
26
27#include <common/cfgparse.h>
Thierry FOURNIER2da788e2017-09-11 18:37:23 +020028#include <common/xref.h>
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +020029#include <common/hathreads.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010030
William Lallemand9ed62032016-11-21 17:49:11 +010031#include <types/cli.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010032#include <types/hlua.h>
33#include <types/proxy.h>
William Lallemand9ed62032016-11-21 17:49:11 +010034#include <types/stats.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010035
Thierry FOURNIER55da1652015-01-23 11:36:30 +010036#include <proto/arg.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020037#include <proto/applet.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010038#include <proto/channel.h>
William Lallemand9ed62032016-11-21 17:49:11 +010039#include <proto/cli.h>
Willy Tarreaua71f6422016-11-16 17:00:14 +010040#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010041#include <proto/stats.h>
Thierry FOURNIER9a819e72015-02-16 20:22:55 +010042#include <proto/hdr_idx.h>
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010043#include <proto/hlua.h>
Thierry Fournierfb0b5462016-01-21 09:28:58 +010044#include <proto/hlua_fcn.h>
Thierry FOURNIER3def3932015-04-07 11:27:54 +020045#include <proto/map.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010046#include <proto/obj_type.h>
Thierry FOURNIER83758bb2015-02-04 13:21:04 +010047#include <proto/pattern.h>
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +010048#include <proto/payload.h>
49#include <proto/proto_http.h>
50#include <proto/sample.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010051#include <proto/server.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020052#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020053#include <proto/stream.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010054#include <proto/stream_interface.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010055#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010056#include <proto/tcp_rules.h>
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +020057#include <proto/vars.h>
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010058
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010059/* Lua uses longjmp to perform yield or throwing errors. This
60 * macro is used only for identifying the function that can
61 * not return because a longjmp is executed.
62 * __LJMP marks a prototype of hlua file that can use longjmp.
63 * WILL_LJMP() marks an lua function that will use longjmp.
64 * MAY_LJMP() marks an lua function that may use longjmp.
65 */
66#define __LJMP
67#define WILL_LJMP(func) func
68#define MAY_LJMP(func) func
69
Thierry FOURNIERbabae282015-09-17 11:36:37 +020070/* This couple of function executes securely some Lua calls outside of
71 * the lua runtime environment. Each Lua call can return a longjmp
72 * if it encounter a memory error.
73 *
74 * Lua documentation extract:
75 *
76 * If an error happens outside any protected environment, Lua calls
77 * a panic function (see lua_atpanic) and then calls abort, thus
78 * exiting the host application. Your panic function can avoid this
79 * exit by never returning (e.g., doing a long jump to your own
80 * recovery point outside Lua).
81 *
82 * The panic function runs as if it were a message handler (see
83 * §2.3); in particular, the error message is at the top of the
84 * stack. However, there is no guarantee about stack space. To push
85 * anything on the stack, the panic function must first check the
86 * available space (see §4.2).
87 *
88 * We must check all the Lua entry point. This includes:
89 * - The include/proto/hlua.h exported functions
90 * - the task wrapper function
91 * - The action wrapper function
92 * - The converters wrapper function
93 * - The sample-fetch wrapper functions
94 *
95 * It is tolerated that the initilisation function returns an abort.
96 * Before each Lua abort, an error message is writed on stderr.
97 *
98 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
99 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
100 * because they must be exists in the program stack when the longjmp
101 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200102 *
103 * Note that the Lua processing is not really thread safe. It provides
104 * heavy system which consists to add our own lock function in the Lua
105 * code and recompile the library. This system will probably not accepted
106 * by maintainers of various distribs.
107 *
108 * Our main excution point of the Lua is the function lua_resume(). A
109 * quick looking on the Lua sources displays a lua_lock() a the start
110 * of function and a lua_unlock() at the end of the function. So I
111 * conclude that the Lua thread safe mode just perform a mutex around
112 * all execution. So I prefer to do this in the HAProxy code, it will be
113 * easier for distro maintainers.
114 *
115 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
116 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
117 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200118 */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100119__decl_hathreads(HA_SPINLOCK_T hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200120THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200121static int hlua_panic_safe(lua_State *L) { return 0; }
122static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); }
123
124#define SET_SAFE_LJMP(__L) \
125 ({ \
126 int ret; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100127 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128 if (setjmp(safe_ljmp_env) != 0) { \
129 lua_atpanic(__L, hlua_panic_safe); \
130 ret = 0; \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100131 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200132 } else { \
133 lua_atpanic(__L, hlua_panic_ljmp); \
134 ret = 1; \
135 } \
136 ret; \
137 })
138
139/* If we are the last function catching Lua errors, we
140 * must reset the panic function.
141 */
142#define RESET_SAFE_LJMP(__L) \
143 do { \
144 lua_atpanic(__L, hlua_panic_safe); \
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100145 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200146 } while(0)
147
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200148/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200149#define APPLET_DONE 0x01 /* applet processing is done. */
150#define APPLET_100C 0x02 /* 100 continue expected. */
151#define APPLET_HDR_SENT 0x04 /* Response header sent. */
152#define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */
153#define APPLET_LAST_CHK 0x10 /* Last chunk sent. */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100154#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200155
156#define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n"
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200157
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100158/* The main Lua execution context. */
159struct hlua gL;
160
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100161/* This is the memory pool containing struct lua for applets
162 * (including cli).
163 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100164struct pool_head *pool_head_hlua;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100165
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100166/* Used for Socket connection. */
167static struct proxy socket_proxy;
168static struct server socket_tcp;
169#ifdef USE_OPENSSL
170static struct server socket_ssl;
171#endif
172
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100173/* List head of the function called at the initialisation time. */
174struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions);
175
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100176/* The following variables contains the reference of the different
177 * Lua classes. These references are useful for identify metadata
178 * associated with an object.
179 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100180static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100181static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100182static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100183static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100184static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100185static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200186static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200187static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200188static int class_applet_http_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100189
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100190/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200191 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100192 * short timeout. Lua linked with tasks doesn't have a timeout
193 * because a task may remain alive during all the haproxy execution.
194 */
195static unsigned int hlua_timeout_session = 4000; /* session timeout. */
196static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200197static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100198
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100199/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
200 * it is used for preventing infinite loops.
201 *
202 * I test the scheer with an infinite loop containing one incrementation
203 * and one test. I run this loop between 10 seconds, I raise a ceil of
204 * 710M loops from one interrupt each 9000 instructions, so I fix the value
205 * to one interrupt each 10 000 instructions.
206 *
207 * configured | Number of
208 * instructions | loops executed
209 * between two | in milions
210 * forced yields |
211 * ---------------+---------------
212 * 10 | 160
213 * 500 | 670
214 * 1000 | 680
215 * 5000 | 700
216 * 7000 | 700
217 * 8000 | 700
218 * 9000 | 710 <- ceil
219 * 10000 | 710
220 * 100000 | 710
221 * 1000000 | 710
222 *
223 */
224static unsigned int hlua_nb_instruction = 10000;
225
Willy Tarreau32f61e22015-03-18 17:54:59 +0100226/* Descriptor for the memory allocation state. If limit is not null, it will
227 * be enforced on any memory allocation.
228 */
229struct hlua_mem_allocator {
230 size_t allocated;
231 size_t limit;
232};
233
234static struct hlua_mem_allocator hlua_global_allocator;
235
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200236static const char error_500[] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200237 "HTTP/1.0 500 Internal Server Error\r\n"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200238 "Cache-Control: no-cache\r\n"
239 "Connection: close\r\n"
240 "Content-Type: text/html\r\n"
241 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200242 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n";
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200243
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100244/* These functions converts types between HAProxy internal args or
245 * sample and LUA types. Another function permits to check if the
246 * LUA stack contains arguments according with an required ARG_T
247 * format.
248 */
249static int hlua_arg2lua(lua_State *L, const struct arg *arg);
250static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100251__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100252 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100253static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100254static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100255static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
256
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200257__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg);
258
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200259#define SEND_ERR(__be, __fmt, __args...) \
260 do { \
261 send_log(__be, LOG_ERR, __fmt, ## __args); \
262 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100263 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200264 } while (0)
265
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100266/* Used to check an Lua function type in the stack. It creates and
267 * returns a reference of the function. This function throws an
268 * error if the rgument is not a "function".
269 */
270__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
271{
272 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100273 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100274 WILL_LJMP(luaL_argerror(L, argno, msg));
275 }
276 lua_pushvalue(L, argno);
277 return luaL_ref(L, LUA_REGISTRYINDEX);
278}
279
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200280/* Return the string that is of the top of the stack. */
281const char *hlua_get_top_error_string(lua_State *L)
282{
283 if (lua_gettop(L) < 1)
284 return "unknown error";
285 if (lua_type(L, -1) != LUA_TSTRING)
286 return "unknown error";
287 return lua_tostring(L, -1);
288}
289
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200290__LJMP static const char *hlua_traceback(lua_State *L)
291{
292 lua_Debug ar;
293 int level = 0;
294 struct chunk *msg = get_trash_chunk();
295 int filled = 0;
296
297 while (lua_getstack(L, level++, &ar)) {
298
299 /* Add separator */
300 if (filled)
301 chunk_appendf(msg, ", ");
302 filled = 1;
303
304 /* Fill fields:
305 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
306 * 'l': fills in the field currentline;
307 * 'n': fills in the field name and namewhat;
308 * 't': fills in the field istailcall;
309 */
310 lua_getinfo(L, "Slnt", &ar);
311
312 /* Append code localisation */
313 if (ar.currentline > 0)
314 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
315 else
316 chunk_appendf(msg, "%s ", ar.short_src);
317
318 /*
319 * Get function name
320 *
321 * if namewhat is no empty, name is defined.
322 * what contains "Lua" for Lua function, "C" for C function,
323 * or "main" for main code.
324 */
325 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
326 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
327
328 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
329 chunk_appendf(msg, "main chunk");
330
331 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
332 chunk_appendf(msg, "C function line %d", ar.linedefined);
333
334 else /* nothing left... */
335 chunk_appendf(msg, "?");
336
337
338 /* Display tailed call */
339 if (ar.istailcall)
340 chunk_appendf(msg, " ...");
341 }
342
343 return msg->str;
344}
345
346
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100347/* This function check the number of arguments available in the
348 * stack. If the number of arguments available is not the same
349 * then <nb> an error is throwed.
350 */
351__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
352{
353 if (lua_gettop(L) == nb)
354 return;
355 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
356}
357
Mark Lakes22154b42018-01-29 14:38:40 -0800358/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100359 * and the line number where the error is encountered.
360 */
361static int hlua_pusherror(lua_State *L, const char *fmt, ...)
362{
363 va_list argp;
364 va_start(argp, fmt);
365 luaL_where(L, 1);
366 lua_pushvfstring(L, fmt, argp);
367 va_end(argp);
368 lua_concat(L, 2);
369 return 1;
370}
371
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100372/* This functions is used with sample fetch and converters. It
373 * converts the HAProxy configuration argument in a lua stack
374 * values.
375 *
376 * It takes an array of "arg", and each entry of the array is
377 * converted and pushed in the LUA stack.
378 */
379static int hlua_arg2lua(lua_State *L, const struct arg *arg)
380{
381 switch (arg->type) {
382 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100383 case ARGT_TIME:
384 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100385 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100386 break;
387
388 case ARGT_STR:
389 lua_pushlstring(L, arg->data.str.str, arg->data.str.len);
390 break;
391
392 case ARGT_IPV4:
393 case ARGT_IPV6:
394 case ARGT_MSK4:
395 case ARGT_MSK6:
396 case ARGT_FE:
397 case ARGT_BE:
398 case ARGT_TAB:
399 case ARGT_SRV:
400 case ARGT_USR:
401 case ARGT_MAP:
402 default:
403 lua_pushnil(L);
404 break;
405 }
406 return 1;
407}
408
409/* This function take one entrie in an LUA stack at the index "ud",
410 * and try to convert it in an HAProxy argument entry. This is useful
411 * with sample fetch wrappers. The input arguments are gived to the
412 * lua wrapper and converted as arg list by thi function.
413 */
414static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
415{
416 switch (lua_type(L, ud)) {
417
418 case LUA_TNUMBER:
419 case LUA_TBOOLEAN:
420 arg->type = ARGT_SINT;
421 arg->data.sint = lua_tointeger(L, ud);
422 break;
423
424 case LUA_TSTRING:
425 arg->type = ARGT_STR;
426 arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len);
427 break;
428
429 case LUA_TUSERDATA:
430 case LUA_TNIL:
431 case LUA_TTABLE:
432 case LUA_TFUNCTION:
433 case LUA_TTHREAD:
434 case LUA_TLIGHTUSERDATA:
435 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200436 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100437 break;
438 }
439 return 1;
440}
441
442/* the following functions are used to convert a struct sample
443 * in Lua type. This useful to convert the return of the
444 * fetchs or converters.
445 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100446static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100447{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200448 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100450 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200451 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452 break;
453
454 case SMP_T_BIN:
455 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200456 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100457 break;
458
459 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200460 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100461 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
462 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
463 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
464 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
465 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
466 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
467 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
468 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
469 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200470 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100471 break;
472 default:
473 lua_pushnil(L);
474 break;
475 }
476 break;
477
478 case SMP_T_IPV4:
479 case SMP_T_IPV6:
480 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200481 if (sample_casts[smp->data.type][SMP_T_STR] &&
482 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200483 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100484 else
485 lua_pushnil(L);
486 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100487 default:
488 lua_pushnil(L);
489 break;
490 }
491 return 1;
492}
493
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100494/* the following functions are used to convert a struct sample
495 * in Lua strings. This is useful to convert the return of the
496 * fetchs or converters.
497 */
498static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
499{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200500 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100501
502 case SMP_T_BIN:
503 case SMP_T_STR:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200504 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100505 break;
506
507 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200508 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100509 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
510 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
511 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
512 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
513 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
514 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
515 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
516 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
517 case HTTP_METH_OTHER:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200518 lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100519 break;
520 default:
521 lua_pushstring(L, "");
522 break;
523 }
524 break;
525
526 case SMP_T_SINT:
527 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100528 case SMP_T_IPV4:
529 case SMP_T_IPV6:
530 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200531 if (sample_casts[smp->data.type][SMP_T_STR] &&
532 sample_casts[smp->data.type][SMP_T_STR](smp))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200533 lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100534 else
535 lua_pushstring(L, "");
536 break;
537 default:
538 lua_pushstring(L, "");
539 break;
540 }
541 return 1;
542}
543
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100544/* the following functions are used to convert an Lua type in a
545 * struct sample. This is useful to provide data from a converter
546 * to the LUA code.
547 */
548static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
549{
550 switch (lua_type(L, ud)) {
551
552 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200553 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200554 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100555 break;
556
557
558 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200559 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200560 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100561 break;
562
563 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200564 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100565 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200566 smp->data.u.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.len);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100567 break;
568
569 case LUA_TUSERDATA:
570 case LUA_TNIL:
571 case LUA_TTABLE:
572 case LUA_TFUNCTION:
573 case LUA_TTHREAD:
574 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200575 case LUA_TNONE:
576 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200577 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200578 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100579 break;
580 }
581 return 1;
582}
583
584/* This function check the "argp" builded by another conversion function
585 * is in accord with the expected argp defined by the "mask". The fucntion
586 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100587 *
588 * This function assumes thant the argp argument contains ARGM_NBARGS + 1
589 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100590 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100591__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100592 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100593{
594 int min_arg;
595 int idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100596 struct proxy *px;
597 char *sname, *pname;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100598
599 idx = 0;
600 min_arg = ARGM(mask);
601 mask >>= ARGM_BITS;
602
603 while (1) {
604
605 /* Check oversize. */
606 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Cyril Bonté577a36a2015-03-02 00:08:38 +0100607 WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100608 }
609
610 /* Check for mandatory arguments. */
611 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100612 if (idx < min_arg) {
613
614 /* If miss other argument than the first one, we return an error. */
615 if (idx > 0)
616 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
617
618 /* If first argument have a certain type, some default values
619 * may be used. See the function smp_resolve_args().
620 */
621 switch (mask & ARGT_MASK) {
622
623 case ARGT_FE:
624 if (!(p->cap & PR_CAP_FE))
625 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
626 argp[idx].data.prx = p;
627 argp[idx].type = ARGT_FE;
628 argp[idx+1].type = ARGT_STOP;
629 break;
630
631 case ARGT_BE:
632 if (!(p->cap & PR_CAP_BE))
633 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
634 argp[idx].data.prx = p;
635 argp[idx].type = ARGT_BE;
636 argp[idx+1].type = ARGT_STOP;
637 break;
638
639 case ARGT_TAB:
640 argp[idx].data.prx = p;
641 argp[idx].type = ARGT_TAB;
642 argp[idx+1].type = ARGT_STOP;
643 break;
644
645 default:
646 WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected"));
647 break;
648 }
649 }
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100650 return 0;
651 }
652
653 /* Check for exceed the number of requiered argument. */
654 if ((mask & ARGT_MASK) == ARGT_STOP &&
655 argp[idx].type != ARGT_STOP) {
656 WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected"));
657 }
658
659 if ((mask & ARGT_MASK) == ARGT_STOP &&
660 argp[idx].type == ARGT_STOP) {
661 return 0;
662 }
663
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100664 /* Convert some argument types. */
665 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100666 case ARGT_SINT:
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100667 if (argp[idx].type != ARGT_SINT)
668 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
669 argp[idx].type = ARGT_SINT;
670 break;
671
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100672 case ARGT_TIME:
673 if (argp[idx].type != ARGT_SINT)
674 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200675 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100676 break;
677
678 case ARGT_SIZE:
679 if (argp[idx].type != ARGT_SINT)
680 WILL_LJMP(luaL_argerror(L, first + idx, "integer expected"));
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200681 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100682 break;
683
684 case ARGT_FE:
685 if (argp[idx].type != ARGT_STR)
686 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
687 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
688 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200689 argp[idx].data.prx = proxy_fe_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100690 if (!argp[idx].data.prx)
691 WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
692 argp[idx].type = ARGT_FE;
693 break;
694
695 case ARGT_BE:
696 if (argp[idx].type != ARGT_STR)
697 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
698 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
699 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200700 argp[idx].data.prx = proxy_be_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100701 if (!argp[idx].data.prx)
702 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
703 argp[idx].type = ARGT_BE;
704 break;
705
706 case ARGT_TAB:
707 if (argp[idx].type != ARGT_STR)
708 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
709 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
710 trash.str[argp[idx].data.str.len] = 0;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200711 argp[idx].data.prx = proxy_tbl_by_name(trash.str);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100712 if (!argp[idx].data.prx)
713 WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist"));
714 argp[idx].type = ARGT_TAB;
715 break;
716
717 case ARGT_SRV:
718 if (argp[idx].type != ARGT_STR)
719 WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
720 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
721 trash.str[argp[idx].data.str.len] = 0;
722 sname = strrchr(trash.str, '/');
723 if (sname) {
724 *sname++ = '\0';
725 pname = trash.str;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200726 px = proxy_be_by_name(pname);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100727 if (!px)
728 WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
729 }
730 else {
731 sname = trash.str;
732 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100733 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100734 argp[idx].data.srv = findserver(px, sname);
735 if (!argp[idx].data.srv)
736 WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist"));
737 argp[idx].type = ARGT_SRV;
738 break;
739
740 case ARGT_IPV4:
741 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
742 trash.str[argp[idx].data.str.len] = 0;
743 if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4))
744 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address"));
745 argp[idx].type = ARGT_IPV4;
746 break;
747
748 case ARGT_MSK4:
749 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
750 trash.str[argp[idx].data.str.len] = 0;
751 if (!str2mask(trash.str, &argp[idx].data.ipv4))
752 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask"));
753 argp[idx].type = ARGT_MSK4;
754 break;
755
756 case ARGT_IPV6:
757 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
758 trash.str[argp[idx].data.str.len] = 0;
759 if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6))
760 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address"));
761 argp[idx].type = ARGT_IPV6;
762 break;
763
764 case ARGT_MSK6:
Tim Duesterhusb814da62018-01-25 16:24:50 +0100765 memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
766 trash.str[argp[idx].data.str.len] = 0;
767 if (!str2mask6(trash.str, &argp[idx].data.ipv6))
768 WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask"));
769 argp[idx].type = ARGT_MSK6;
770 break;
771
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100772 case ARGT_MAP:
773 case ARGT_REG:
774 case ARGT_USR:
775 WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported"));
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100776 break;
777 }
778
779 /* Check for type of argument. */
780 if ((mask & ARGT_MASK) != argp[idx].type) {
781 const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'",
782 arg_type_names[(mask & ARGT_MASK)],
783 arg_type_names[argp[idx].type & ARGT_MASK]);
784 WILL_LJMP(luaL_argerror(L, first + idx, msg));
785 }
786
787 /* Next argument. */
788 mask >>= ARGT_BITS;
789 idx++;
790 }
791}
792
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100793/*
794 * The following functions are used to make correspondance between the the
795 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100796 *
797 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100798 * - hlua_sethlua : create the association between hlua context and lua_state.
799 */
800static inline struct hlua *hlua_gethlua(lua_State *L)
801{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100802 struct hlua **hlua = lua_getextraspace(L);
803 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100804}
805static inline void hlua_sethlua(struct hlua *hlua)
806{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100807 struct hlua **hlua_store = lua_getextraspace(hlua->T);
808 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100809}
810
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100811/* This function is used to send logs. It try to send on screen (stderr)
812 * and on the default syslog server.
813 */
814static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
815{
816 struct tm tm;
817 char *p;
818
819 /* Cleanup the log message. */
820 p = trash.str;
821 for (; *msg != '\0'; msg++, p++) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +0200822 if (p >= trash.str + trash.size - 1) {
823 /* Break the message if exceed the buffer size. */
824 *(p-4) = ' ';
825 *(p-3) = '.';
826 *(p-2) = '.';
827 *(p-1) = '.';
828 break;
829 }
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100830 if (isprint(*msg))
831 *p = *msg;
832 else
833 *p = '.';
834 }
835 *p = '\0';
836
Thierry FOURNIER5554e292015-09-09 11:21:37 +0200837 send_log(px, level, "%s\n", trash.str);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100838 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Willy Tarreaua678b432015-08-28 10:14:59 +0200839 get_localtime(date.tv_sec, &tm);
840 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100841 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
842 (int)getpid(), trash.str);
843 fflush(stderr);
844 }
845}
846
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100847/* This function just ensure that the yield will be always
848 * returned with a timeout and permit to set some flags
849 */
850__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100851 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100852{
853 struct hlua *hlua = hlua_gethlua(L);
854
855 /* Set the wake timeout. If timeout is required, we set
856 * the expiration time.
857 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +0200858 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100859
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +0100860 hlua->flags |= flags;
861
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +0100862 /* Process the yield. */
863 WILL_LJMP(lua_yieldk(L, nresults, ctx, k));
864}
865
Willy Tarreau87b09662015-04-03 00:22:06 +0200866/* This function initialises the Lua environment stored in the stream.
867 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100868 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200869 *
870 * This function is particular. it initialises a new Lua thread. If the
871 * initialisation fails (example: out of memory error), the lua function
872 * throws an error (longjmp).
873 *
874 * This function manipulates two Lua stack: the main and the thread. Only
875 * the main stack can fail. The thread is not manipulated. This function
876 * MUST NOT manipulate the created thread stack state, because is not
877 * proctected agains error throwed by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100878 */
879int hlua_ctx_init(struct hlua *lua, struct task *task)
880{
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200881 if (!SET_SAFE_LJMP(gL.T)) {
882 lua->Tref = LUA_REFNIL;
883 return 0;
884 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100885 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +0100886 lua->flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100887 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100888 lua->T = lua_newthread(gL.T);
889 if (!lua->T) {
890 lua->Tref = LUA_REFNIL;
Thierry FOURNIER0a976202017-07-12 11:18:00 +0200891 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100892 return 0;
893 }
894 hlua_sethlua(lua);
895 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
896 lua->task = task;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200897 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100898 return 1;
899}
900
Willy Tarreau87b09662015-04-03 00:22:06 +0200901/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100902 * is destroyed. The destroy also the memory context. The struct "lua"
903 * is not freed.
904 */
905void hlua_ctx_destroy(struct hlua *lua)
906{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100907 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100908 return;
909
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100910 if (!lua->T)
911 goto end;
912
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100913 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +0200914 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100915
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200916 if (!SET_SAFE_LJMP(lua->T))
917 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100918 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200919 RESET_SAFE_LJMP(lua->T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200920
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200921 if (!SET_SAFE_LJMP(gL.T))
922 return;
923 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
924 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200925 /* Forces a garbage collecting process. If the Lua program is finished
926 * without error, we run the GC on the thread pointer. Its freed all
927 * the unused memory.
928 * If the thread is finnish with an error or is currently yielded,
929 * it seems that the GC applied on the thread doesn't clean anything,
930 * so e run the GC on the main thread.
931 * NOTE: maybe this action locks all the Lua threads untiml the en of
932 * the garbage collection.
933 */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200934 if (lua->flags & HLUA_MUST_GC) {
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200935 if (!SET_SAFE_LJMP(gL.T))
Thierry FOURNIER75d02082017-07-12 13:41:33 +0200936 return;
Thierry FOURNIER7bd10d52017-07-17 00:44:40 +0200937 lua_gc(gL.T, LUA_GCCOLLECT, 0);
938 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +0200939 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +0200940
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +0200941 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100942
943end:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100944 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100945}
946
947/* This function is used to restore the Lua context when a coroutine
948 * fails. This function copy the common memory between old coroutine
949 * and the new coroutine. The old coroutine is destroyed, and its
950 * replaced by the new coroutine.
951 * If the flag "keep_msg" is set, the last entry of the old is assumed
952 * as string error message and it is copied in the new stack.
953 */
954static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
955{
956 lua_State *T;
957 int new_ref;
958
959 /* Renew the main LUA stack doesn't have sense. */
960 if (lua == &gL)
961 return 0;
962
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100963 /* New Lua coroutine. */
964 T = lua_newthread(gL.T);
965 if (!T)
966 return 0;
967
968 /* Copy last error message. */
969 if (keep_msg)
970 lua_xmove(lua->T, T, 1);
971
972 /* Copy data between the coroutines. */
973 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
974 lua_xmove(lua->T, T, 1);
975 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
976
977 /* Destroy old data. */
978 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
979
980 /* The thread is garbage collected by Lua. */
981 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
982
983 /* Fill the struct with the new coroutine values. */
984 lua->Mref = new_ref;
985 lua->T = T;
986 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
987
988 /* Set context. */
989 hlua_sethlua(lua);
990
991 return 1;
992}
993
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100994void hlua_hook(lua_State *L, lua_Debug *ar)
995{
Thierry FOURNIERcae49c92015-03-06 14:05:24 +0100996 struct hlua *hlua = hlua_gethlua(L);
997
998 /* Lua cannot yield when its returning from a function,
999 * so, we can fix the interrupt hook to 1 instruction,
1000 * expecting that the function is finnished.
1001 */
1002 if (lua_gethookmask(L) & LUA_MASKRET) {
1003 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1004 return;
1005 }
1006
1007 /* restore the interrupt condition. */
1008 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1009
1010 /* If we interrupt the Lua processing in yieldable state, we yield.
1011 * If the state is not yieldable, trying yield causes an error.
1012 */
1013 if (lua_isyieldable(L))
1014 WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
1015
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001016 /* If we cannot yield, update the clock and check the timeout. */
1017 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001018 hlua->run_time += now_ms - hlua->start_time;
1019 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001020 lua_pushfstring(L, "execution timeout");
1021 WILL_LJMP(lua_error(L));
1022 }
1023
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001024 /* Update the start time. */
1025 hlua->start_time = now_ms;
1026
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001027 /* Try to interrupt the process at the end of the current
1028 * unyieldable function.
1029 */
1030 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001031}
1032
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001033/* This function start or resumes the Lua stack execution. If the flag
1034 * "yield_allowed" if no set and the LUA stack execution returns a yield
1035 * The function return an error.
1036 *
1037 * The function can returns 4 values:
1038 * - HLUA_E_OK : The execution is terminated without any errors.
1039 * - HLUA_E_AGAIN : The execution must continue at the next associated
1040 * task wakeup.
1041 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
1042 * the top of the stack.
1043 * - HLUA_E_ERR : An error has occured without error message.
1044 *
1045 * If an error occured, the stack is renewed and it is ready to run new
1046 * LUA code.
1047 */
1048static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1049{
1050 int ret;
1051 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001052 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001053
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001054 /* Initialise run time counter. */
1055 if (!HLUA_IS_RUNNING(lua))
1056 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001057
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001058 /* Lock the whole Lua execution. This lock must be before the
1059 * label "resume_execution".
1060 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001061 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001062
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001063resume_execution:
1064
1065 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1066 * instructions. it is used for preventing infinite loops.
1067 */
1068 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1069
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001070 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001071 HLUA_SET_RUN(lua);
1072 HLUA_CLR_CTRLYIELD(lua);
1073 HLUA_CLR_WAKERESWR(lua);
1074 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001075
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001076 /* Update the start time. */
1077 lua->start_time = now_ms;
1078
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001079 /* Call the function. */
1080 ret = lua_resume(lua->T, gL.T, lua->nargs);
1081 switch (ret) {
1082
1083 case LUA_OK:
1084 ret = HLUA_E_OK;
1085 break;
1086
1087 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001088 /* Check if the execution timeout is expired. It it is the case, we
1089 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001090 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001091 tv_update_date(0, 1);
1092 lua->run_time += now_ms - lua->start_time;
1093 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001094 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001095 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001096 break;
1097 }
1098 /* Process the forced yield. if the general yield is not allowed or
1099 * if no task were associated this the current Lua execution
1100 * coroutine, we resume the execution. Else we want to return in the
1101 * scheduler and we want to be waked up again, to continue the
1102 * current Lua execution. So we schedule our own task.
1103 */
1104 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001105 if (!yield_allowed || !lua->task)
1106 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001107 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001108 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001109 if (!yield_allowed) {
1110 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001111 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001112 break;
1113 }
1114 ret = HLUA_E_AGAIN;
1115 break;
1116
1117 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001118
1119 /* Special exit case. The traditionnal exit is returned as an error
1120 * because the errors ares the only one mean to return immediately
1121 * from and lua execution.
1122 */
1123 if (lua->flags & HLUA_EXIT) {
1124 ret = HLUA_E_OK;
Thierry FOURNIERe1587b32015-08-28 09:54:13 +02001125 hlua_ctx_renew(lua, 0);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001126 break;
1127 }
1128
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001129 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001130 if (!lua_checkstack(lua->T, 1)) {
1131 ret = HLUA_E_ERR;
1132 break;
1133 }
1134 msg = lua_tostring(lua->T, -1);
1135 lua_settop(lua->T, 0); /* Empty the stack. */
1136 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001137 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001138 if (msg)
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001139 lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001140 else
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001141 lua_pushfstring(lua->T, "unknown runtime error from %s", trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001142 ret = HLUA_E_ERRMSG;
1143 break;
1144
1145 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001146 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001147 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001148 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001149 break;
1150
1151 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001152 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001153 if (!lua_checkstack(lua->T, 1)) {
1154 ret = HLUA_E_ERR;
1155 break;
1156 }
1157 msg = lua_tostring(lua->T, -1);
1158 lua_settop(lua->T, 0); /* Empty the stack. */
1159 lua_pop(lua->T, 1);
1160 if (msg)
1161 lua_pushfstring(lua->T, "message handler error: %s", msg);
1162 else
1163 lua_pushfstring(lua->T, "message handler error");
1164 ret = HLUA_E_ERRMSG;
1165 break;
1166
1167 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001168 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001169 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001170 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001171 break;
1172 }
1173
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001174 /* This GC permits to destroy some object when a Lua timeout strikes. */
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001175 if (lua->flags & HLUA_MUST_GC &&
1176 ret != HLUA_E_AGAIN)
Thierry FOURNIER6ab4d8e2015-09-27 22:17:19 +02001177 lua_gc(lua->T, LUA_GCCOLLECT, 0);
1178
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001179 switch (ret) {
1180 case HLUA_E_AGAIN:
1181 break;
1182
1183 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001184 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001185 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001186 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001187 break;
1188
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001189 case HLUA_E_ETMOUT:
1190 case HLUA_E_NOMEM:
1191 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001192 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001193 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001194 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001195 hlua_ctx_renew(lua, 0);
1196 break;
1197
1198 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001199 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001200 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001201 break;
1202 }
1203
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001204 /* This is the main exit point, remove the Lua lock. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001205 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001206
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001207 return ret;
1208}
1209
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001210/* This function exit the current code. */
1211__LJMP static int hlua_done(lua_State *L)
1212{
1213 struct hlua *hlua = hlua_gethlua(L);
1214
1215 hlua->flags |= HLUA_EXIT;
1216 WILL_LJMP(lua_error(L));
1217
1218 return 0;
1219}
1220
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001221/* This function is an LUA binding. It provides a function
1222 * for deleting ACL from a referenced ACL file.
1223 */
1224__LJMP static int hlua_del_acl(lua_State *L)
1225{
1226 const char *name;
1227 const char *key;
1228 struct pat_ref *ref;
1229
1230 MAY_LJMP(check_args(L, 2, "del_acl"));
1231
1232 name = MAY_LJMP(luaL_checkstring(L, 1));
1233 key = MAY_LJMP(luaL_checkstring(L, 2));
1234
1235 ref = pat_ref_lookup(name);
1236 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001237 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001238
1239 pat_ref_delete(ref, key);
1240 return 0;
1241}
1242
1243/* This function is an LUA binding. It provides a function
1244 * for deleting map entry from a referenced map file.
1245 */
1246static int hlua_del_map(lua_State *L)
1247{
1248 const char *name;
1249 const char *key;
1250 struct pat_ref *ref;
1251
1252 MAY_LJMP(check_args(L, 2, "del_map"));
1253
1254 name = MAY_LJMP(luaL_checkstring(L, 1));
1255 key = MAY_LJMP(luaL_checkstring(L, 2));
1256
1257 ref = pat_ref_lookup(name);
1258 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001259 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001260
1261 pat_ref_delete(ref, key);
1262 return 0;
1263}
1264
1265/* This function is an LUA binding. It provides a function
1266 * for adding ACL pattern from a referenced ACL file.
1267 */
1268static int hlua_add_acl(lua_State *L)
1269{
1270 const char *name;
1271 const char *key;
1272 struct pat_ref *ref;
1273
1274 MAY_LJMP(check_args(L, 2, "add_acl"));
1275
1276 name = MAY_LJMP(luaL_checkstring(L, 1));
1277 key = MAY_LJMP(luaL_checkstring(L, 2));
1278
1279 ref = pat_ref_lookup(name);
1280 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001281 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001282
1283 if (pat_ref_find_elt(ref, key) == NULL)
1284 pat_ref_add(ref, key, NULL, NULL);
1285 return 0;
1286}
1287
1288/* This function is an LUA binding. It provides a function
1289 * for setting map pattern and sample from a referenced map
1290 * file.
1291 */
1292static int hlua_set_map(lua_State *L)
1293{
1294 const char *name;
1295 const char *key;
1296 const char *value;
1297 struct pat_ref *ref;
1298
1299 MAY_LJMP(check_args(L, 3, "set_map"));
1300
1301 name = MAY_LJMP(luaL_checkstring(L, 1));
1302 key = MAY_LJMP(luaL_checkstring(L, 2));
1303 value = MAY_LJMP(luaL_checkstring(L, 3));
1304
1305 ref = pat_ref_lookup(name);
1306 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001307 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001308
1309 if (pat_ref_find_elt(ref, key) != NULL)
1310 pat_ref_set(ref, key, value, NULL);
1311 else
1312 pat_ref_add(ref, key, value, NULL);
1313 return 0;
1314}
1315
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001316/* A class is a lot of memory that contain data. This data can be a table,
1317 * an integer or user data. This data is associated with a metatable. This
1318 * metatable have an original version registred in the global context with
1319 * the name of the object (_G[<name>] = <metable> ).
1320 *
1321 * A metable is a table that modify the standard behavior of a standard
1322 * access to the associated data. The entries of this new metatable are
1323 * defined as is:
1324 *
1325 * http://lua-users.org/wiki/MetatableEvents
1326 *
1327 * __index
1328 *
1329 * we access an absent field in a table, the result is nil. This is
1330 * true, but it is not the whole truth. Actually, such access triggers
1331 * the interpreter to look for an __index metamethod: If there is no
1332 * such method, as usually happens, then the access results in nil;
1333 * otherwise, the metamethod will provide the result.
1334 *
1335 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1336 * the key does not appear in the table, but the metatable has an __index
1337 * property:
1338 *
1339 * - if the value is a function, the function is called, passing in the
1340 * table and the key; the return value of that function is returned as
1341 * the result.
1342 *
1343 * - if the value is another table, the value of the key in that table is
1344 * asked for and returned (and if it doesn't exist in that table, but that
1345 * table's metatable has an __index property, then it continues on up)
1346 *
1347 * - Use "rawget(myTable,key)" to skip this metamethod.
1348 *
1349 * http://www.lua.org/pil/13.4.1.html
1350 *
1351 * __newindex
1352 *
1353 * Like __index, but control property assignment.
1354 *
1355 * __mode - Control weak references. A string value with one or both
1356 * of the characters 'k' and 'v' which specifies that the the
1357 * keys and/or values in the table are weak references.
1358 *
1359 * __call - Treat a table like a function. When a table is followed by
1360 * parenthesis such as "myTable( 'foo' )" and the metatable has
1361 * a __call key pointing to a function, that function is invoked
1362 * (passing any specified arguments) and the return value is
1363 * returned.
1364 *
1365 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1366 * called, if the metatable for myTable has a __metatable
1367 * key, the value of that key is returned instead of the
1368 * actual metatable.
1369 *
1370 * __tostring - Control string representation. When the builtin
1371 * "tostring( myTable )" function is called, if the metatable
1372 * for myTable has a __tostring property set to a function,
1373 * that function is invoked (passing myTable to it) and the
1374 * return value is used as the string representation.
1375 *
1376 * __len - Control table length. When the table length is requested using
1377 * the length operator ( '#' ), if the metatable for myTable has
1378 * a __len key pointing to a function, that function is invoked
1379 * (passing myTable to it) and the return value used as the value
1380 * of "#myTable".
1381 *
1382 * __gc - Userdata finalizer code. When userdata is set to be garbage
1383 * collected, if the metatable has a __gc field pointing to a
1384 * function, that function is first invoked, passing the userdata
1385 * to it. The __gc metamethod is not called for tables.
1386 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1387 *
1388 * Special metamethods for redefining standard operators:
1389 * http://www.lua.org/pil/13.1.html
1390 *
1391 * __add "+"
1392 * __sub "-"
1393 * __mul "*"
1394 * __div "/"
1395 * __unm "!"
1396 * __pow "^"
1397 * __concat ".."
1398 *
1399 * Special methods for redfining standar relations
1400 * http://www.lua.org/pil/13.2.html
1401 *
1402 * __eq "=="
1403 * __lt "<"
1404 * __le "<="
1405 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001406
1407/*
1408 *
1409 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001410 * Class Map
1411 *
1412 *
1413 */
1414
1415/* Returns a struct hlua_map if the stack entry "ud" is
1416 * a class session, otherwise it throws an error.
1417 */
1418__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1419{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001420 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001421}
1422
1423/* This function is the map constructor. It don't need
1424 * the class Map object. It creates and return a new Map
1425 * object. It must be called only during "body" or "init"
1426 * context because it process some filesystem accesses.
1427 */
1428__LJMP static int hlua_map_new(struct lua_State *L)
1429{
1430 const char *fn;
1431 int match = PAT_MATCH_STR;
1432 struct sample_conv conv;
1433 const char *file = "";
1434 int line = 0;
1435 lua_Debug ar;
1436 char *err = NULL;
1437 struct arg args[2];
1438
1439 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1440 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1441
1442 fn = MAY_LJMP(luaL_checkstring(L, 1));
1443
1444 if (lua_gettop(L) >= 2) {
1445 match = MAY_LJMP(luaL_checkinteger(L, 2));
1446 if (match < 0 || match >= PAT_MATCH_NUM)
1447 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1448 }
1449
1450 /* Get Lua filename and line number. */
1451 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1452 lua_getinfo(L, "Sl", &ar); /* get info about it */
1453 if (ar.currentline > 0) { /* is there info? */
1454 file = ar.short_src;
1455 line = ar.currentline;
1456 }
1457 }
1458
1459 /* fill fake sample_conv struct. */
1460 conv.kw = ""; /* unused. */
1461 conv.process = NULL; /* unused. */
1462 conv.arg_mask = 0; /* unused. */
1463 conv.val_args = NULL; /* unused. */
1464 conv.out_type = SMP_T_STR;
1465 conv.private = (void *)(long)match;
1466 switch (match) {
1467 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1468 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1469 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1470 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1471 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1472 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1473 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001474 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001475 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1476 default:
1477 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1478 }
1479
1480 /* fill fake args. */
1481 args[0].type = ARGT_STR;
1482 args[0].data.str.str = (char *)fn;
1483 args[1].type = ARGT_STOP;
1484
1485 /* load the map. */
1486 if (!sample_load_map(args, &conv, file, line, &err)) {
1487 /* error case: we cant use luaL_error because we must
1488 * free the err variable.
1489 */
1490 luaL_where(L, 1);
1491 lua_pushfstring(L, "'new': %s.", err);
1492 lua_concat(L, 2);
1493 free(err);
1494 WILL_LJMP(lua_error(L));
1495 }
1496
1497 /* create the lua object. */
1498 lua_newtable(L);
1499 lua_pushlightuserdata(L, args[0].data.map);
1500 lua_rawseti(L, -2, 0);
1501
1502 /* Pop a class Map metatable and affect it to the userdata. */
1503 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1504 lua_setmetatable(L, -2);
1505
1506
1507 return 1;
1508}
1509
1510__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1511{
1512 struct map_descriptor *desc;
1513 struct pattern *pat;
1514 struct sample smp;
1515
1516 MAY_LJMP(check_args(L, 2, "lookup"));
1517 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001518 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001519 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001520 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001521 }
1522 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001523 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001524 smp.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001525 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 +02001526 }
1527
1528 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001529 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001530 if (str)
1531 lua_pushstring(L, "");
1532 else
1533 lua_pushnil(L);
1534 return 1;
1535 }
1536
1537 /* The Lua pattern must return a string, so we can't check the returned type */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001538 lua_pushlstring(L, pat->data->u.str.str, pat->data->u.str.len);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001539 return 1;
1540}
1541
1542__LJMP static int hlua_map_lookup(struct lua_State *L)
1543{
1544 return _hlua_map_lookup(L, 0);
1545}
1546
1547__LJMP static int hlua_map_slookup(struct lua_State *L)
1548{
1549 return _hlua_map_lookup(L, 1);
1550}
1551
1552/*
1553 *
1554 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001555 * Class Socket
1556 *
1557 *
1558 */
1559
1560__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1561{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001562 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001563}
1564
1565/* This function is the handler called for each I/O on the established
1566 * connection. It is used for notify space avalaible to send or data
1567 * received.
1568 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001569static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001570{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001571 struct stream_interface *si = appctx->owner;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001572 struct connection *c = cs_conn(objt_cs(si_opposite(si)->end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001573
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001574 if (appctx->ctx.hlua_cosocket.die) {
1575 si_shutw(si);
1576 si_shutr(si);
1577 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001578 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1579 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001580 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1581 }
1582
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001583 /* If the connection object is not avalaible, close all the
1584 * streams and wakeup everithing waiting for.
1585 */
1586 if (!c) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001587 si_shutw(si);
1588 si_shutr(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001589 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001590 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1591 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001592 return;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001593 }
1594
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001595 /* If we cant write, wakeup the pending write signals. */
1596 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001597 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001598
1599 /* If we cant read, wakeup the pending read signals. */
1600 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001601 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001602
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001603 /* if the connection is not estabkished, inform the stream that we want
1604 * to be notified whenever the connection completes.
1605 */
1606 if (!(c->flags & CO_FL_CONNECTED)) {
1607 si_applet_cant_get(si);
1608 si_applet_cant_put(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001609 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001610 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001611
1612 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001613 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001614
1615 /* Wake the tasks which wants to write if the buffer have avalaible space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001616 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001617 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001618
1619 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001620 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001621 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001622
1623 /* Some data were injected in the buffer, notify the stream
1624 * interface.
1625 */
1626 if (!channel_is_empty(si_ic(si)))
1627 stream_int_update(si);
1628
1629 /* If write notifications are registered, we considers we want
1630 * to write, so we set the flag cant put
1631 */
1632 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
1633 si_applet_cant_put(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001634}
1635
Willy Tarreau87b09662015-04-03 00:22:06 +02001636/* This function is called when the "struct stream" is destroyed.
1637 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001638 * Wake all the pending signals.
1639 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001640static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001641{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001642 struct xref *peer;
1643
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001644 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001645 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1646 if (peer)
1647 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001648
1649 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001650 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1651 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001652}
1653
1654/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001655 * uses this object. If the stream does not exists, just quit.
1656 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001657 * pending signal can rest in the read and write lists. destroy
1658 * it.
1659 */
1660__LJMP static int hlua_socket_gc(lua_State *L)
1661{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001662 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001663 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001664 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001665
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001666 MAY_LJMP(check_args(L, 1, "__gc"));
1667
1668 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001669 peer = xref_get_peer_and_lock(&socket->xref);
1670 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001671 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001672 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001673
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001674 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001675 appctx->ctx.hlua_cosocket.die = 1;
1676 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001677
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001678 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001679 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001680 return 0;
1681}
1682
1683/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001684 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001685 */
sada05ed3302018-05-11 11:48:18 -07001686__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001687{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001688 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001689 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001690 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001691
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001692 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001693
1694 /* Check if we run on the same thread than the xreator thread.
1695 * We cannot access to the socket if the thread is different.
1696 */
1697 if (socket->tid != tid)
1698 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1699
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001700 peer = xref_get_peer_and_lock(&socket->xref);
1701 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001702 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001703 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001704
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001705 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001706 appctx->ctx.hlua_cosocket.die = 1;
1707 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001708
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001709 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001710 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001711 return 0;
1712}
1713
sada05ed3302018-05-11 11:48:18 -07001714/* The close function calls close_helper.
1715 */
1716__LJMP static int hlua_socket_close(lua_State *L)
1717{
1718 MAY_LJMP(check_args(L, 1, "close"));
1719 return hlua_socket_close_helper(L);
1720}
1721
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001722/* This Lua function assumes that the stack contain three parameters.
1723 * 1 - USERDATA containing a struct socket
1724 * 2 - INTEGER with values of the macro defined below
1725 * If the integer is -1, we must read at most one line.
1726 * If the integer is -2, we ust read all the data until the
1727 * end of the stream.
1728 * If the integer is positive value, we must read a number of
1729 * bytes corresponding to this value.
1730 */
1731#define HLSR_READ_LINE (-1)
1732#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001733__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001734{
1735 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1736 int wanted = lua_tointeger(L, 2);
1737 struct hlua *hlua = hlua_gethlua(L);
1738 struct appctx *appctx;
1739 int len;
1740 int nblk;
1741 char *blk1;
1742 int len1;
1743 char *blk2;
1744 int len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001745 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001746 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001747 struct stream_interface *si;
1748 struct stream *s;
1749 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001750 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001751
1752 /* Check if this lua stack is schedulable. */
1753 if (!hlua || !hlua->task)
1754 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1755 "'frontend', 'backend' or 'task'"));
1756
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001757 /* Check if we run on the same thread than the xreator thread.
1758 * We cannot access to the socket if the thread is different.
1759 */
1760 if (socket->tid != tid)
1761 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1762
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001763 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001764 peer = xref_get_peer_and_lock(&socket->xref);
1765 if (!peer)
1766 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001767 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1768 si = appctx->owner;
1769 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001770
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001771 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001772 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001773 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001774 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001775 if (nblk < 0) /* Connection close. */
1776 goto connection_closed;
1777 if (nblk == 0) /* No data avalaible. */
1778 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001779
1780 /* remove final \r\n. */
1781 if (nblk == 1) {
1782 if (blk1[len1-1] == '\n') {
1783 len1--;
1784 skip_at_end++;
1785 if (blk1[len1-1] == '\r') {
1786 len1--;
1787 skip_at_end++;
1788 }
1789 }
1790 }
1791 else {
1792 if (blk2[len2-1] == '\n') {
1793 len2--;
1794 skip_at_end++;
1795 if (blk2[len2-1] == '\r') {
1796 len2--;
1797 skip_at_end++;
1798 }
1799 }
1800 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001801 }
1802
1803 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001804 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001805 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001806 if (nblk < 0) /* Connection close. */
1807 goto connection_closed;
1808 if (nblk == 0) /* No data avalaible. */
1809 goto connection_empty;
1810 }
1811
1812 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001813 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001814 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001815 if (nblk < 0) /* Connection close. */
1816 goto connection_closed;
1817 if (nblk == 0) /* No data avalaible. */
1818 goto connection_empty;
1819
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001820 missing_bytes = wanted - socket->b.n;
1821 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001822 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001823 len1 = missing_bytes;
1824 } if (nblk == 2 && len1 + len2 > missing_bytes)
1825 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001826 }
1827
1828 len = len1;
1829
1830 luaL_addlstring(&socket->b, blk1, len1);
1831 if (nblk == 2) {
1832 len += len2;
1833 luaL_addlstring(&socket->b, blk2, len2);
1834 }
1835
1836 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001837 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001838
1839 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02001840 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001841
1842 /* If the pattern reclaim to read all the data
1843 * in the connection, got out.
1844 */
1845 if (wanted == HLSR_READ_ALL)
1846 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001847 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001848 goto connection_empty;
1849
1850 /* Return result. */
1851 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001852 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001853 return 1;
1854
1855connection_closed:
1856
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001857 xref_unlock(&socket->xref, peer);
1858
1859no_peer:
1860
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001861 /* If the buffer containds data. */
1862 if (socket->b.n > 0) {
1863 luaL_pushresult(&socket->b);
1864 return 1;
1865 }
1866 lua_pushnil(L);
1867 lua_pushstring(L, "connection closed.");
1868 return 2;
1869
1870connection_empty:
1871
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001872 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
1873 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001874 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001875 }
1876 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001877 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001878 return 0;
1879}
1880
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001881/* This Lua function gets two parameters. The first one can be string
1882 * or a number. If the string is "*l", the user requires one line. If
1883 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001884 * If the value is a number, the user require a number of bytes equal
1885 * to the value. The default value is "*l" (a line).
1886 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001887 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001888 * integer takes this values:
1889 * -1 : read a line
1890 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001891 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001892 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001893 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001894 * concatenated with the read data.
1895 */
1896__LJMP static int hlua_socket_receive(struct lua_State *L)
1897{
1898 int wanted = HLSR_READ_LINE;
1899 const char *pattern;
1900 int type;
1901 char *error;
1902 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001903 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904
1905 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
1906 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
1907
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001908 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001909
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001910 /* Check if we run on the same thread than the xreator thread.
1911 * We cannot access to the socket if the thread is different.
1912 */
1913 if (socket->tid != tid)
1914 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1915
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001916 /* check for pattern. */
1917 if (lua_gettop(L) >= 2) {
1918 type = lua_type(L, 2);
1919 if (type == LUA_TSTRING) {
1920 pattern = lua_tostring(L, 2);
1921 if (strcmp(pattern, "*a") == 0)
1922 wanted = HLSR_READ_ALL;
1923 else if (strcmp(pattern, "*l") == 0)
1924 wanted = HLSR_READ_LINE;
1925 else {
1926 wanted = strtoll(pattern, &error, 10);
1927 if (*error != '\0')
1928 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
1929 }
1930 }
1931 else if (type == LUA_TNUMBER) {
1932 wanted = lua_tointeger(L, 2);
1933 if (wanted < 0)
1934 WILL_LJMP(luaL_error(L, "Unsupported size."));
1935 }
1936 }
1937
1938 /* Set pattern. */
1939 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01001940
1941 /* Check if we would replace the top by itself. */
1942 if (lua_gettop(L) != 2)
1943 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001944
Tim Duesterhusb33754c2018-01-04 19:32:14 +01001945 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001946 luaL_buffinit(L, &socket->b);
1947
1948 /* Check prefix. */
1949 if (lua_gettop(L) >= 3) {
1950 if (lua_type(L, 3) != LUA_TSTRING)
1951 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
1952 pattern = lua_tolstring(L, 3, &len);
1953 luaL_addlstring(&socket->b, pattern, len);
1954 }
1955
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001956 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001957}
1958
1959/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08001960 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001961 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001962static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001963{
1964 struct hlua_socket *socket;
1965 struct hlua *hlua = hlua_gethlua(L);
1966 struct appctx *appctx;
1967 size_t buf_len;
1968 const char *buf;
1969 int len;
1970 int send_len;
1971 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001972 struct xref *peer;
1973 struct stream_interface *si;
1974 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001975
1976 /* Check if this lua stack is schedulable. */
1977 if (!hlua || !hlua->task)
1978 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
1979 "'frontend', 'backend' or 'task'"));
1980
1981 /* Get object */
1982 socket = MAY_LJMP(hlua_checksocket(L, 1));
1983 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001984 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001985
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001986 /* Check if we run on the same thread than the xreator thread.
1987 * We cannot access to the socket if the thread is different.
1988 */
1989 if (socket->tid != tid)
1990 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1991
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001992 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001993 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001994 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001995 lua_pushinteger(L, -1);
1996 return 1;
1997 }
1998 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1999 si = appctx->owner;
2000 s = si_strm(si);
2001
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002002 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002003 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002004 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002005 lua_pushinteger(L, -1);
2006 return 1;
2007 }
2008
2009 /* Update the input buffer data. */
2010 buf += sent;
2011 send_len = buf_len - sent;
2012
2013 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002014 if (sent >= buf_len) {
2015 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002016 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002017 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002018
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002019 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2020 * the request buffer if its not required.
2021 */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002022 if (s->req.buf->size == 0) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002023 if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002024 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002025 }
2026
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002027 /* Check for avalaible space. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002028 len = buffer_total_space(s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002029 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002030 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002031 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002032
2033 /* send data */
2034 if (len < send_len)
2035 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002036 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037
2038 /* "Not enough space" (-1), "Buffer too little to contain
2039 * the data" (-2) are not expected because the available length
2040 * is tested.
2041 * Other unknown error are also not expected.
2042 */
2043 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002044 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002045 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002046
sada05ed3302018-05-11 11:48:18 -07002047 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002048 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002049 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002050 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002051 return 1;
2052 }
2053
2054 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002055 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002056
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002057 s->req.rex = TICK_ETERNITY;
2058 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002059
2060 /* Update length sent. */
2061 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002062 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002063
2064 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002065 if (sent + len >= buf_len) {
2066 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002067 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002068 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002069
2070hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002071 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2072 xref_unlock(&socket->xref, peer);
2073 WILL_LJMP(luaL_error(L, "out of memory"));
2074 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002075 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002076 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002077 return 0;
2078}
2079
2080/* This function initiate the send of data. It just check the input
2081 * parameters and push an integer in the Lua stack that contain the
2082 * amount of data writed in the buffer. This is used by the function
2083 * "hlua_socket_write_yield" that can yield.
2084 *
2085 * The Lua function gets between 3 and 4 parameters. The first one is
2086 * the associated object. The second is a string buffer. The third is
2087 * a facultative integer that represents where is the buffer position
2088 * of the start of the data that can send. The first byte is the
2089 * position "1". The default value is "1". The fourth argument is a
2090 * facultative integer that represents where is the buffer position
2091 * of the end of the data that can send. The default is the last byte.
2092 */
2093static int hlua_socket_send(struct lua_State *L)
2094{
2095 int i;
2096 int j;
2097 const char *buf;
2098 size_t buf_len;
2099
2100 /* Check number of arguments. */
2101 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2102 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2103
2104 /* Get the string. */
2105 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2106
2107 /* Get and check j. */
2108 if (lua_gettop(L) == 4) {
2109 j = MAY_LJMP(luaL_checkinteger(L, 4));
2110 if (j < 0)
2111 j = buf_len + j + 1;
2112 if (j > buf_len)
2113 j = buf_len + 1;
2114 lua_pop(L, 1);
2115 }
2116 else
2117 j = buf_len;
2118
2119 /* Get and check i. */
2120 if (lua_gettop(L) == 3) {
2121 i = MAY_LJMP(luaL_checkinteger(L, 3));
2122 if (i < 0)
2123 i = buf_len + i + 1;
2124 if (i > buf_len)
2125 i = buf_len + 1;
2126 lua_pop(L, 1);
2127 } else
2128 i = 1;
2129
2130 /* Check bth i and j. */
2131 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002132 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002133 return 1;
2134 }
2135 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002136 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002137 return 1;
2138 }
2139 if (i == 0)
2140 i = 1;
2141 if (j == 0)
2142 j = 1;
2143
2144 /* Pop the string. */
2145 lua_pop(L, 1);
2146
2147 /* Update the buffer length. */
2148 buf += i - 1;
2149 buf_len = j - i + 1;
2150 lua_pushlstring(L, buf, buf_len);
2151
2152 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002153 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002154
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002155 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002156}
2157
Willy Tarreau22b0a682015-06-17 19:43:49 +02002158#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002159__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2160{
2161 static char buffer[SOCKET_INFO_MAX_LEN];
2162 int ret;
2163 int len;
2164 char *p;
2165
2166 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2167 if (ret <= 0) {
2168 lua_pushnil(L);
2169 return 1;
2170 }
2171
2172 if (ret == AF_UNIX) {
2173 lua_pushstring(L, buffer+1);
2174 return 1;
2175 }
2176 else if (ret == AF_INET6) {
2177 buffer[0] = '[';
2178 len = strlen(buffer);
2179 buffer[len] = ']';
2180 len++;
2181 buffer[len] = ':';
2182 len++;
2183 p = buffer;
2184 }
2185 else if (ret == AF_INET) {
2186 p = buffer + 1;
2187 len = strlen(p);
2188 p[len] = ':';
2189 len++;
2190 }
2191 else {
2192 lua_pushnil(L);
2193 return 1;
2194 }
2195
2196 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2197 lua_pushnil(L);
2198 return 1;
2199 }
2200
2201 lua_pushstring(L, p);
2202 return 1;
2203}
2204
2205/* Returns information about the peer of the connection. */
2206__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2207{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002208 struct hlua_socket *socket;
2209 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002210 struct xref *peer;
2211 struct appctx *appctx;
2212 struct stream_interface *si;
2213 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002214 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002215
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002216 MAY_LJMP(check_args(L, 1, "getpeername"));
2217
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002218 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002219
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002220 /* Check if we run on the same thread than the xreator thread.
2221 * We cannot access to the socket if the thread is different.
2222 */
2223 if (socket->tid != tid)
2224 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2225
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002226 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002227 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002228 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002229 lua_pushnil(L);
2230 return 1;
2231 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002232 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2233 si = appctx->owner;
2234 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002235
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002236 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002237 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002238 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002239 lua_pushnil(L);
2240 return 1;
2241 }
2242
Willy Tarreaua71f6422016-11-16 17:00:14 +01002243 conn_get_to_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002244 if (!(conn->flags & CO_FL_ADDR_TO_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002245 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002246 lua_pushnil(L);
2247 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002248 }
2249
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002250 ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to));
2251 xref_unlock(&socket->xref, peer);
2252 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002253}
2254
2255/* Returns information about my connection side. */
2256static int hlua_socket_getsockname(struct lua_State *L)
2257{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002258 struct hlua_socket *socket;
2259 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002260 struct appctx *appctx;
2261 struct xref *peer;
2262 struct stream_interface *si;
2263 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002264 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002265
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002266 MAY_LJMP(check_args(L, 1, "getsockname"));
2267
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002268 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002269
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002270 /* Check if we run on the same thread than the xreator thread.
2271 * We cannot access to the socket if the thread is different.
2272 */
2273 if (socket->tid != tid)
2274 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2275
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002276 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002277 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002278 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002279 lua_pushnil(L);
2280 return 1;
2281 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002282 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2283 si = appctx->owner;
2284 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002285
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002286 conn = cs_conn(objt_cs(s->si[1].end));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002287 if (!conn) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002288 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002289 lua_pushnil(L);
2290 return 1;
2291 }
2292
Willy Tarreaua71f6422016-11-16 17:00:14 +01002293 conn_get_from_addr(conn);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294 if (!(conn->flags & CO_FL_ADDR_FROM_SET)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002295 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002296 lua_pushnil(L);
2297 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002298 }
2299
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002300 ret = hlua_socket_info(L, &conn->addr.from);
2301 xref_unlock(&socket->xref, peer);
2302 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002303}
2304
2305/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002306static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002307 .obj_type = OBJ_TYPE_APPLET,
2308 .name = "<LUA_TCP>",
2309 .fct = hlua_socket_handler,
2310 .release = hlua_socket_release,
2311};
2312
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002313__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002314{
2315 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
2316 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002317 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002318 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002319 struct stream_interface *si;
2320 struct stream *s;
2321
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002322 /* Check if we run on the same thread than the xreator thread.
2323 * We cannot access to the socket if the thread is different.
2324 */
2325 if (socket->tid != tid)
2326 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2327
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002328 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002329 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002330 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002331 lua_pushnil(L);
2332 lua_pushstring(L, "Can't connect");
2333 return 2;
2334 }
2335 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2336 si = appctx->owner;
2337 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002338
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002339 /* Check if we run on the same thread than the xreator thread.
2340 * We cannot access to the socket if the thread is different.
2341 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002342 if (socket->tid != tid) {
2343 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002344 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002345 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002346
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002347 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002348 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002349 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002350 lua_pushnil(L);
2351 lua_pushstring(L, "Can't connect");
2352 return 2;
2353 }
2354
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002355 appctx = objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002356
2357 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002358 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002359 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002360 lua_pushinteger(L, 1);
2361 return 1;
2362 }
2363
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002364 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2365 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002366 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002367 }
2368 xref_unlock(&socket->xref, peer);
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002369 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002370 return 0;
2371}
2372
2373/* This function fail or initite the connection. */
2374__LJMP static int hlua_socket_connect(struct lua_State *L)
2375{
2376 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002377 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002378 const char *ip;
2379 struct connection *conn;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002380 struct hlua *hlua;
2381 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002382 int low, high;
2383 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002384 struct xref *peer;
2385 struct stream_interface *si;
2386 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002387
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002388 if (lua_gettop(L) < 2)
2389 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002390
2391 /* Get args. */
2392 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002393
2394 /* Check if we run on the same thread than the xreator thread.
2395 * We cannot access to the socket if the thread is different.
2396 */
2397 if (socket->tid != tid)
2398 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2399
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002400 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002401 if (lua_gettop(L) >= 3) {
2402 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002403 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002404
Tim Duesterhus6edab862018-01-06 19:04:45 +01002405 /* Force the ip to end with a colon, to support IPv6 addresses
2406 * that are not enclosed within square brackets.
2407 */
2408 if (port > 0) {
2409 luaL_buffinit(L, &b);
2410 luaL_addstring(&b, ip);
2411 luaL_addchar(&b, ':');
2412 luaL_pushresult(&b);
2413 ip = lua_tolstring(L, lua_gettop(L), NULL);
2414 }
2415 }
2416
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002417 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002418 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002419 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002420 lua_pushnil(L);
2421 return 1;
2422 }
2423 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2424 si = appctx->owner;
2425 s = si_strm(si);
2426
2427 /* Initialise connection. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002428 conn = cs_conn(si_alloc_cs(&s->si[1], NULL));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002429 if (!conn) {
2430 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002431 WILL_LJMP(luaL_error(L, "connect: internal error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002432 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002433
Willy Tarreau3adac082015-09-26 17:51:09 +02002434 /* needed for the connection not to be closed */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002435 conn->target = s->target;
Willy Tarreau3adac082015-09-26 17:51:09 +02002436
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002437 /* Parse ip address. */
Willy Tarreau48ef4c92017-01-06 18:32:38 +01002438 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002439 if (!addr) {
2440 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002441 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002442 }
2443 if (low != high) {
2444 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002445 WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002446 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002447 memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002448
2449 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002450 if (low == 0) {
2451 if (conn->addr.to.ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002452 if (port == -1) {
2453 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002454 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002455 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002456 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port);
2457 } else if (conn->addr.to.ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002458 if (port == -1) {
2459 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002460 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002461 }
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002462 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port);
2463 }
2464 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002465
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002466 hlua = hlua_gethlua(L);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002467 appctx = objt_appctx(s->si[0].end);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002468
2469 /* inform the stream that we want to be notified whenever the
2470 * connection completes.
2471 */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002472 si_applet_cant_get(&s->si[0]);
2473 si_applet_cant_put(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002474 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002475
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002476 hlua->flags |= HLUA_MUST_GC;
2477
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002478 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2479 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002480 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002481 }
2482 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002483
2484 task_wakeup(s->task, TASK_WOKEN_INIT);
2485 /* Return yield waiting for connection. */
2486
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01002487 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002488
2489 return 0;
2490}
2491
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002492#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002493__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2494{
2495 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002496 struct xref *peer;
2497 struct appctx *appctx;
2498 struct stream_interface *si;
2499 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002500
2501 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2502 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002503
2504 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002505 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002506 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002507 lua_pushnil(L);
2508 return 1;
2509 }
2510 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2511 si = appctx->owner;
2512 s = si_strm(si);
2513
2514 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002515 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002516 return MAY_LJMP(hlua_socket_connect(L));
2517}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002518#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002519
2520__LJMP static int hlua_socket_setoption(struct lua_State *L)
2521{
2522 return 0;
2523}
2524
2525__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2526{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002527 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002528 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002529 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002530 struct xref *peer;
2531 struct appctx *appctx;
2532 struct stream_interface *si;
2533 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002534
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002535 MAY_LJMP(check_args(L, 2, "settimeout"));
2536
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002537 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002538
2539 /* round up for inputs that are fractions and convert to millis */
2540 dtmout = (0.5 + MAY_LJMP(luaL_checknumber(L, 2))) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002541
Thierry Fournier17a921b2018-03-08 09:59:02 +01002542 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002543 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002544 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2545
Mark Lakes56cc1252018-03-27 09:48:06 +02002546 if (dtmout > INT_MAX) /* overflow check */
2547 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d", INT_MAX));
2548
2549 tmout = MS_TO_TICKS((int)dtmout);
2550
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002551 /* Check if we run on the same thread than the xreator thread.
2552 * We cannot access to the socket if the thread is different.
2553 */
2554 if (socket->tid != tid)
2555 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2556
Mark Lakes56cc1252018-03-27 09:48:06 +02002557 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002558 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002559 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002560 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2561 WILL_LJMP(lua_error(L));
2562 return 0;
2563 }
2564 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2565 si = appctx->owner;
2566 s = si_strm(si);
2567
2568 s->req.rto = tmout;
2569 s->req.wto = tmout;
2570 s->res.rto = tmout;
2571 s->res.wto = tmout;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002572 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002573
Thierry Fourniere9636f12018-03-08 09:54:32 +01002574 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002575 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002576}
2577
2578__LJMP static int hlua_socket_new(lua_State *L)
2579{
2580 struct hlua_socket *socket;
2581 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002582 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002583 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002584
2585 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002586 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002587 hlua_pusherror(L, "socket: full stack");
2588 goto out_fail_conf;
2589 }
2590
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002591 /* Create the object: obj[0] = userdata. */
2592 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002593 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002594 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002595 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002596 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002597
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002598 /* Check if the various memory pools are intialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002599 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002600 hlua_pusherror(L, "socket: uninitialized pools.");
2601 goto out_fail_conf;
2602 }
2603
Willy Tarreau87b09662015-04-03 00:22:06 +02002604 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002605 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2606 lua_setmetatable(L, -2);
2607
Willy Tarreaud420a972015-04-06 00:39:18 +02002608 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002609 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002610 if (!appctx) {
2611 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002612 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002613 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002614
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002615 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002616 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002617 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2618 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002619
Willy Tarreaud420a972015-04-06 00:39:18 +02002620 /* Now create a session, task and stream for this applet */
2621 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2622 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002623 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002624 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002625 }
2626
Willy Tarreau87787ac2017-08-28 16:22:54 +02002627 strm = stream_new(sess, &appctx->obj_type);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002628 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002629 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002630 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002631 }
2632
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002633 /* Initialise cross reference between stream and Lua socket object. */
2634 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002635
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002636 /* Configure "right" stream interface. this "si" is used to connect
2637 * and retrieve data from the server. The connection is initialized
2638 * with the "struct server".
2639 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002640 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002641
2642 /* Force destination server. */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002643 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED;
2644 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002645
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002646 return 1;
2647
Willy Tarreaud420a972015-04-06 00:39:18 +02002648 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002649 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002650 out_fail_sess:
2651 appctx_free(appctx);
2652 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002653 WILL_LJMP(lua_error(L));
2654 return 0;
2655}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002656
2657/*
2658 *
2659 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002660 * Class Channel
2661 *
2662 *
2663 */
2664
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002665/* The state between the channel data and the HTTP parser state can be
2666 * unconsistent, so reset the parser and call it again. Warning, this
2667 * action not revalidate the request and not send a 400 if the modified
2668 * resuest is not valid.
2669 *
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002670 * This function never fails. The direction is set using dir, which equals
2671 * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002672 */
2673static void hlua_resynchonize_proto(struct stream *stream, int dir)
2674{
2675 /* Protocol HTTP. */
2676 if (stream->be->mode == PR_MODE_HTTP) {
2677
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002678 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002679 http_txn_reset_req(stream->txn);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002680 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002681 http_txn_reset_res(stream->txn);
2682
2683 if (stream->txn->hdr_idx.v)
2684 hdr_idx_init(&stream->txn->hdr_idx);
2685
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002686 if (dir == SMP_OPT_DIR_REQ)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002687 http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx);
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01002688 else if (dir == SMP_OPT_DIR_RES)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002689 http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx);
2690 }
2691}
2692
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002693/* This function is called before the Lua execution. It stores
2694 * the differents parsers state before executing some Lua code.
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002695 */
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002696static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c)
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002697{
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002698 c->mode = stream->be->mode;
2699 switch (c->mode) {
2700 case PR_MODE_HTTP:
2701 c->data.http.dir = opt & SMP_OPT_DIR;
2702 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2703 c->data.http.state = stream->txn->req.msg_state;
2704 else
2705 c->data.http.state = stream->txn->rsp.msg_state;
2706 break;
2707 default:
2708 break;
2709 }
2710}
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002711
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002712/* This function is called after the Lua execution. it
2713 * returns true if the parser state is consistent, otherwise,
2714 * it return false.
2715 *
2716 * In HTTP mode, the parser state must be in the same state
2717 * or greater when we exit the function. Even if we do a
2718 * control yield. This prevent to break the HTTP message
2719 * from the Lua code.
2720 */
2721static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c)
2722{
2723 if (c->mode != stream->be->mode)
2724 return 0;
2725
2726 switch (c->mode) {
2727 case PR_MODE_HTTP:
2728 if (c->data.http.dir != (opt & SMP_OPT_DIR))
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002729 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01002730 if (c->data.http.dir == SMP_OPT_DIR_REQ)
2731 return stream->txn->req.msg_state >= c->data.http.state;
2732 else
2733 return stream->txn->rsp.msg_state >= c->data.http.state;
2734 default:
2735 return 1;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002736 }
2737 return 1;
2738}
2739
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002740/* Returns the struct hlua_channel join to the class channel in the
2741 * stack entry "ud" or throws an argument error.
2742 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002743__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002744{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002745 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002746}
2747
Willy Tarreau47860ed2015-03-10 14:07:50 +01002748/* Pushes the channel onto the top of the stack. If the stask does not have a
2749 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002750 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002751static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002752{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002753 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002754 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002755 return 0;
2756
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002757 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002758 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002759 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002760
2761 /* Pop a class sesison metatable and affect it to the userdata. */
2762 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2763 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002764 return 1;
2765}
2766
2767/* Duplicate all the data present in the input channel and put it
2768 * in a string LUA variables. Returns -1 and push a nil value in
2769 * the stack if the channel is closed and all the data are consumed,
2770 * returns 0 if no data are available, otherwise it returns the length
2771 * of the builded string.
2772 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002773static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002774{
2775 char *blk1;
2776 char *blk2;
2777 int len1;
2778 int len2;
2779 int ret;
2780 luaL_Buffer b;
2781
Willy Tarreau06d80a92017-10-19 14:32:15 +02002782 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002783 if (unlikely(ret == 0))
2784 return 0;
2785
2786 if (unlikely(ret < 0)) {
2787 lua_pushnil(L);
2788 return -1;
2789 }
2790
2791 luaL_buffinit(L, &b);
2792 luaL_addlstring(&b, blk1, len1);
2793 if (unlikely(ret == 2))
2794 luaL_addlstring(&b, blk2, len2);
2795 luaL_pushresult(&b);
2796
2797 if (unlikely(ret == 2))
2798 return len1 + len2;
2799 return len1;
2800}
2801
2802/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2803 * a yield. This function keep the data in the buffer.
2804 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002805__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002806{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002807 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002808
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002809 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2810
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002811 if (_hlua_channel_dup(chn, L) == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002812 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002813 return 1;
2814}
2815
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002816/* Check arguments for the function "hlua_channel_dup_yield". */
2817__LJMP static int hlua_channel_dup(lua_State *L)
2818{
2819 MAY_LJMP(check_args(L, 1, "dup"));
2820 MAY_LJMP(hlua_checkchannel(L, 1));
2821 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2822}
2823
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002824/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2825 * a yield. This function consumes the data in the buffer. It returns
2826 * a string containing the data or a nil pointer if no data are available
2827 * and the channel is closed.
2828 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002829__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002830{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002831 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002832 int ret;
2833
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002834 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002835
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002836 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002837 if (unlikely(ret == 0))
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002838 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002839
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002840 if (unlikely(ret == -1))
2841 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002842
Willy Tarreau47860ed2015-03-10 14:07:50 +01002843 chn->buf->i -= ret;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002844 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002845 return 1;
2846}
2847
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002848/* Check arguments for the fucntion "hlua_channel_get_yield". */
2849__LJMP static int hlua_channel_get(lua_State *L)
2850{
2851 MAY_LJMP(check_args(L, 1, "get"));
2852 MAY_LJMP(hlua_checkchannel(L, 1));
2853 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
2854}
2855
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002856/* This functions consumes and returns one line. If the channel is closed,
2857 * and the last data does not contains a final '\n', the data are returned
2858 * without the final '\n'. When no more data are avalaible, it returns nil
2859 * value.
2860 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002861__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002862{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002863 char *blk1;
2864 char *blk2;
2865 int len1;
2866 int len2;
2867 int len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01002868 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002869 int ret;
2870 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002871
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002872 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2873
Willy Tarreau06d80a92017-10-19 14:32:15 +02002874 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002875 if (ret == 0)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002876 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002877
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002878 if (ret == -1) {
2879 lua_pushnil(L);
2880 return 1;
2881 }
2882
2883 luaL_buffinit(L, &b);
2884 luaL_addlstring(&b, blk1, len1);
2885 len = len1;
2886 if (unlikely(ret == 2)) {
2887 luaL_addlstring(&b, blk2, len2);
2888 len += len2;
2889 }
2890 luaL_pushresult(&b);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002891 buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002892 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002893 return 1;
2894}
2895
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002896/* Check arguments for the fucntion "hlua_channel_getline_yield". */
2897__LJMP static int hlua_channel_getline(lua_State *L)
2898{
2899 MAY_LJMP(check_args(L, 1, "getline"));
2900 MAY_LJMP(hlua_checkchannel(L, 1));
2901 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
2902}
2903
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002904/* This function takes a string as input, and append it at the
2905 * input side of channel. If the data is too big, but a space
2906 * is probably available after sending some data, the function
2907 * yield. If the data is bigger than the buffer, or if the
2908 * channel is closed, it returns -1. otherwise, it returns the
2909 * amount of data writed.
2910 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002911__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002912{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002913 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002914 size_t len;
2915 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
2916 int l = MAY_LJMP(luaL_checkinteger(L, 3));
2917 int ret;
2918 int max;
2919
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002920 /* Check if the buffer is avalaible because HAProxy doesn't allocate
2921 * the request buffer if its not required.
2922 */
2923 if (chn->buf->size == 0) {
2924 si_applet_cant_put(chn_prod(chn));
2925 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
2926 }
2927
Willy Tarreau47860ed2015-03-10 14:07:50 +01002928 max = channel_recv_limit(chn) - buffer_len(chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002929 if (max > len - l)
2930 max = len - l;
2931
Willy Tarreau06d80a92017-10-19 14:32:15 +02002932 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002933 if (ret == -2 || ret == -3) {
2934 lua_pushinteger(L, -1);
2935 return 1;
2936 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002937 if (ret == -1) {
2938 chn->flags |= CF_WAKE_WRITE;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002939 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01002940 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002941 l += ret;
2942 lua_pop(L, 1);
2943 lua_pushinteger(L, l);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02002944 hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002945
Willy Tarreau47860ed2015-03-10 14:07:50 +01002946 max = channel_recv_limit(chn) - buffer_len(chn->buf);
2947 if (max == 0 && chn->buf->o == 0) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002948 /* There are no space avalaible, and the output buffer is empty.
2949 * in this case, we cannot add more data, so we cannot yield,
2950 * we return the amount of copyied data.
2951 */
2952 return 1;
2953 }
2954 if (l < len)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002955 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002956 return 1;
2957}
2958
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002959/* just a wrapper of "hlua_channel_append_yield". It returns the length
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002960 * of the writed string, or -1 if the channel is closed or if the
2961 * buffer size is too little for the data.
2962 */
2963__LJMP static int hlua_channel_append(lua_State *L)
2964{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002965 size_t len;
2966
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002967 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002968 MAY_LJMP(hlua_checkchannel(L, 1));
2969 MAY_LJMP(luaL_checklstring(L, 2, &len));
2970 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002971 lua_pushinteger(L, 0);
2972
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002973 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002974}
2975
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002976/* just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002977 * his process by cleaning the buffer. The result is a replacement
2978 * of the current data. It returns the length of the writed string,
2979 * or -1 if the channel is closed or if the buffer size is too
2980 * little for the data.
2981 */
2982__LJMP static int hlua_channel_set(lua_State *L)
2983{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002984 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002985
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002986 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002987 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002988 lua_pushinteger(L, 0);
2989
Willy Tarreau47860ed2015-03-10 14:07:50 +01002990 chn->buf->i = 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002991
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002992 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002993}
2994
2995/* Append data in the output side of the buffer. This data is immediatly
2996 * sent. The fcuntion returns the ammount of data writed. If the buffer
2997 * cannot contains the data, the function yield. The function returns -1
2998 * if the channel is closed.
2999 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003000__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003001{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003002 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003003 size_t len;
3004 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3005 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3006 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003007 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003008
Willy Tarreau47860ed2015-03-10 14:07:50 +01003009 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003010 lua_pushinteger(L, -1);
3011 return 1;
3012 }
3013
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003014 /* Check if the buffer is avalaible because HAProxy doesn't allocate
3015 * the request buffer if its not required.
3016 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003017 if (chn->buf->size == 0) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003018 si_applet_cant_put(chn_prod(chn));
3019 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003020 }
3021
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003022 /* the writed data will be immediatly sent, so we can check
3023 * the avalaible space without taking in account the reserve.
3024 * The reserve is guaranted for the processing of incoming
3025 * data, because the buffer will be flushed.
3026 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003027 max = chn->buf->size - buffer_len(chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003028
3029 /* If there are no space avalaible, and the output buffer is empty.
3030 * in this case, we cannot add more data, so we cannot yield,
3031 * we return the amount of copyied data.
3032 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003033 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003034 return 1;
3035
3036 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003037 if (max > len - l)
3038 max = len - l;
3039
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003040 /* The buffer avalaible size may be not contiguous. This test
3041 * detects a non contiguous buffer and realign it.
3042 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003043 if (bi_space_for_replace(chn->buf) < max)
Willy Tarreaufd8d42f2018-07-12 10:57:15 +02003044 channel_slow_realign(chn, trash.str);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003045
3046 /* Copy input data in the buffer. */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003047 max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003048
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003049 /* buffer replace considers that the input part is filled.
3050 * so, I must forward these new data in the output part.
3051 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003052 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003053
3054 l += max;
3055 lua_pop(L, 1);
3056 lua_pushinteger(L, l);
3057
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003058 /* If there are no space avalaible, and the output buffer is empty.
3059 * in this case, we cannot add more data, so we cannot yield,
3060 * we return the amount of copyied data.
3061 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003062 max = chn->buf->size - buffer_len(chn->buf);
3063 if (max == 0 && chn->buf->o == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003064 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003065
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003066 if (l < len) {
3067 /* If we are waiting for space in the response buffer, we
3068 * must set the flag WAKERESWR. This flag required the task
3069 * wake up if any activity is detected on the response buffer.
3070 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003071 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003072 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003073 else
3074 HLUA_SET_WAKEREQWR(hlua);
3075 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003076 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003077
3078 return 1;
3079}
3080
3081/* Just a wraper of "_hlua_channel_send". This wrapper permits
3082 * yield the LUA process, and resume it without checking the
3083 * input arguments.
3084 */
3085__LJMP static int hlua_channel_send(lua_State *L)
3086{
3087 MAY_LJMP(check_args(L, 2, "send"));
3088 lua_pushinteger(L, 0);
3089
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003090 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003091}
3092
3093/* This function forward and amount of butes. The data pass from
3094 * the input side of the buffer to the output side, and can be
3095 * forwarded. This function never fails.
3096 *
3097 * The Lua function takes an amount of bytes to be forwarded in
3098 * imput. It returns the number of bytes forwarded.
3099 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003100__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003101{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003102 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003103 int len;
3104 int l;
3105 int max;
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003106 struct hlua *hlua = hlua_gethlua(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003107
3108 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3109 len = MAY_LJMP(luaL_checkinteger(L, 2));
3110 l = MAY_LJMP(luaL_checkinteger(L, -1));
3111
3112 max = len - l;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003113 if (max > chn->buf->i)
3114 max = chn->buf->i;
3115 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003116 l += max;
3117
3118 lua_pop(L, 1);
3119 lua_pushinteger(L, l);
3120
3121 /* Check if it miss bytes to forward. */
3122 if (l < len) {
3123 /* The the input channel or the output channel are closed, we
3124 * must return the amount of data forwarded.
3125 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003126 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003127 return 1;
3128
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003129 /* If we are waiting for space data in the response buffer, we
3130 * must set the flag WAKERESWR. This flag required the task
3131 * wake up if any activity is detected on the response buffer.
3132 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003133 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003134 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003135 else
3136 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003137
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003138 /* Otherwise, we can yield waiting for new data in the inpout side. */
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01003139 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003140 }
3141
3142 return 1;
3143}
3144
3145/* Just check the input and prepare the stack for the previous
3146 * function "hlua_channel_forward_yield"
3147 */
3148__LJMP static int hlua_channel_forward(lua_State *L)
3149{
3150 MAY_LJMP(check_args(L, 2, "forward"));
3151 MAY_LJMP(hlua_checkchannel(L, 1));
3152 MAY_LJMP(luaL_checkinteger(L, 2));
3153
3154 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003155 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003156}
3157
3158/* Just returns the number of bytes available in the input
3159 * side of the buffer. This function never fails.
3160 */
3161__LJMP static int hlua_channel_get_in_len(lua_State *L)
3162{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003163 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003164
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003165 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003166 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01003167 lua_pushinteger(L, chn->buf->i);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003168 return 1;
3169}
3170
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003171/* Returns true if the channel is full. */
3172__LJMP static int hlua_channel_is_full(lua_State *L)
3173{
3174 struct channel *chn;
3175 int rem;
3176
3177 MAY_LJMP(check_args(L, 1, "is_full"));
3178 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3179
3180 rem = chn->buf->size;
3181 rem -= chn->buf->o; /* Output size */
3182 rem -= chn->buf->i; /* Input size */
3183 rem -= global.tune.maxrewrite; /* Rewrite reserved size */
3184
3185 lua_pushboolean(L, rem <= 0);
3186 return 1;
3187}
3188
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003189/* Just returns the number of bytes available in the output
3190 * side of the buffer. This function never fails.
3191 */
3192__LJMP static int hlua_channel_get_out_len(lua_State *L)
3193{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003194 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003195
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003196 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003197 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreau47860ed2015-03-10 14:07:50 +01003198 lua_pushinteger(L, chn->buf->o);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003199 return 1;
3200}
3201
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003202/*
3203 *
3204 *
3205 * Class Fetches
3206 *
3207 *
3208 */
3209
3210/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003211 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003212 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003213__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003214{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003215 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003216}
3217
3218/* This function creates and push in the stack a fetch object according
3219 * with a current TXN.
3220 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003221static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003222{
Willy Tarreau7073c472015-04-06 11:15:40 +02003223 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003224
3225 /* Check stack size. */
3226 if (!lua_checkstack(L, 3))
3227 return 0;
3228
3229 /* Create the object: obj[0] = userdata.
3230 * Note that the base of the Fetches object is the
3231 * transaction object.
3232 */
3233 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003234 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003235 lua_rawseti(L, -2, 0);
3236
Willy Tarreau7073c472015-04-06 11:15:40 +02003237 hsmp->s = txn->s;
3238 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003239 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003240 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003241
3242 /* Pop a class sesison metatable and affect it to the userdata. */
3243 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3244 lua_setmetatable(L, -2);
3245
3246 return 1;
3247}
3248
3249/* This function is an LUA binding. It is called with each sample-fetch.
3250 * It uses closure argument to store the associated sample-fetch. It
3251 * returns only one argument or throws an error. An error is thrown
3252 * only if an error is encountered during the argument parsing. If
3253 * the "sample-fetch" function fails, nil is returned.
3254 */
3255__LJMP static int hlua_run_sample_fetch(lua_State *L)
3256{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003257 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003258 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003259 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003260 int i;
3261 struct sample smp;
3262
3263 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003264 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003265
3266 /* Get traditionnal arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003267 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003268
Thierry FOURNIERca988662015-12-20 18:43:03 +01003269 /* Check execution authorization. */
3270 if (f->use & SMP_USE_HTTP_ANY &&
3271 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3272 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3273 "is not available in Lua services", f->kw);
3274 WILL_LJMP(lua_error(L));
3275 }
3276
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003277 /* Get extra arguments. */
3278 for (i = 0; i < lua_gettop(L) - 1; i++) {
3279 if (i >= ARGM_NBARGS)
3280 break;
3281 hlua_lua2arg(L, i + 2, &args[i]);
3282 }
3283 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003284 args[i].data.str.str = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003285
3286 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003287 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003288
3289 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003290 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003291 lua_pushfstring(L, "error in arguments");
3292 WILL_LJMP(lua_error(L));
3293 }
3294
3295 /* Initialise the sample. */
3296 memset(&smp, 0, sizeof(smp));
3297
3298 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003299 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003300 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003301 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003302 lua_pushstring(L, "");
3303 else
3304 lua_pushnil(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003305 return 1;
3306 }
3307
3308 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003309 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003310 hlua_smp2lua_str(L, &smp);
3311 else
3312 hlua_smp2lua(L, &smp);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003313 return 1;
3314}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003315
3316/*
3317 *
3318 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003319 * Class Converters
3320 *
3321 *
3322 */
3323
3324/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003325 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003326 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003327__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003328{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003329 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003330}
3331
3332/* This function creates and push in the stack a Converters object
3333 * according with a current TXN.
3334 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003335static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003336{
Willy Tarreau7073c472015-04-06 11:15:40 +02003337 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003338
3339 /* Check stack size. */
3340 if (!lua_checkstack(L, 3))
3341 return 0;
3342
3343 /* Create the object: obj[0] = userdata.
3344 * Note that the base of the Converters object is the
3345 * same than the TXN object.
3346 */
3347 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003348 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003349 lua_rawseti(L, -2, 0);
3350
Willy Tarreau7073c472015-04-06 11:15:40 +02003351 hsmp->s = txn->s;
3352 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003353 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003354 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003355
Willy Tarreau87b09662015-04-03 00:22:06 +02003356 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003357 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3358 lua_setmetatable(L, -2);
3359
3360 return 1;
3361}
3362
3363/* This function is an LUA binding. It is called with each converter.
3364 * It uses closure argument to store the associated converter. It
3365 * returns only one argument or throws an error. An error is thrown
3366 * only if an error is encountered during the argument parsing. If
3367 * the converter function function fails, nil is returned.
3368 */
3369__LJMP static int hlua_run_sample_conv(lua_State *L)
3370{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003371 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003372 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003373 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003374 int i;
3375 struct sample smp;
3376
3377 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003378 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003379
3380 /* Get traditionnal arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003381 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003382
3383 /* Get extra arguments. */
3384 for (i = 0; i < lua_gettop(L) - 2; i++) {
3385 if (i >= ARGM_NBARGS)
3386 break;
3387 hlua_lua2arg(L, i + 3, &args[i]);
3388 }
3389 args[i].type = ARGT_STOP;
David Carlierabdb00f2016-04-27 16:14:50 +01003390 args[i].data.str.str = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003391
3392 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003393 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003394
3395 /* Run the special args checker. */
3396 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3397 hlua_pusherror(L, "error in arguments");
3398 WILL_LJMP(lua_error(L));
3399 }
3400
3401 /* Initialise the sample. */
3402 if (!hlua_lua2smp(L, 2, &smp)) {
3403 hlua_pusherror(L, "error in the input argument");
3404 WILL_LJMP(lua_error(L));
3405 }
3406
Willy Tarreau1777ea62016-03-10 16:15:46 +01003407 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3408
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003409 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003410 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003411 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003412 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003413 WILL_LJMP(lua_error(L));
3414 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003415 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3416 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003417 hlua_pusherror(L, "error during the input argument casting");
3418 WILL_LJMP(lua_error(L));
3419 }
3420
3421 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003422 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003423 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003424 lua_pushstring(L, "");
3425 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003426 lua_pushnil(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003427 return 1;
3428 }
3429
3430 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003431 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003432 hlua_smp2lua_str(L, &smp);
3433 else
3434 hlua_smp2lua(L, &smp);
Willy Tarreaua678b432015-08-28 10:14:59 +02003435 return 1;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003436}
3437
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003438/*
3439 *
3440 *
3441 * Class AppletTCP
3442 *
3443 *
3444 */
3445
3446/* Returns a struct hlua_txn if the stack entry "ud" is
3447 * a class stream, otherwise it throws an error.
3448 */
3449__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3450{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003451 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003452}
3453
3454/* This function creates and push in the stack an Applet object
3455 * according with a current TXN.
3456 */
3457static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3458{
3459 struct hlua_appctx *appctx;
3460 struct stream_interface *si = ctx->owner;
3461 struct stream *s = si_strm(si);
3462 struct proxy *p = s->be;
3463
3464 /* Check stack size. */
3465 if (!lua_checkstack(L, 3))
3466 return 0;
3467
3468 /* Create the object: obj[0] = userdata.
3469 * Note that the base of the Converters object is the
3470 * same than the TXN object.
3471 */
3472 lua_newtable(L);
3473 appctx = lua_newuserdata(L, sizeof(*appctx));
3474 lua_rawseti(L, -2, 0);
3475 appctx->appctx = ctx;
3476 appctx->htxn.s = s;
3477 appctx->htxn.p = p;
3478
3479 /* Create the "f" field that contains a list of fetches. */
3480 lua_pushstring(L, "f");
3481 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3482 return 0;
3483 lua_settable(L, -3);
3484
3485 /* Create the "sf" field that contains a list of stringsafe fetches. */
3486 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003487 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003488 return 0;
3489 lua_settable(L, -3);
3490
3491 /* Create the "c" field that contains a list of converters. */
3492 lua_pushstring(L, "c");
3493 if (!hlua_converters_new(L, &appctx->htxn, 0))
3494 return 0;
3495 lua_settable(L, -3);
3496
3497 /* Create the "sc" field that contains a list of stringsafe converters. */
3498 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003499 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003500 return 0;
3501 lua_settable(L, -3);
3502
3503 /* Pop a class stream metatable and affect it to the table. */
3504 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3505 lua_setmetatable(L, -2);
3506
3507 return 1;
3508}
3509
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003510__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3511{
3512 struct hlua_appctx *appctx;
3513 struct stream *s;
3514 const char *name;
3515 size_t len;
3516 struct sample smp;
3517
3518 MAY_LJMP(check_args(L, 3, "set_var"));
3519
3520 /* It is useles to retrieve the stream, but this function
3521 * runs only in a stream context.
3522 */
3523 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3524 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3525 s = appctx->htxn.s;
3526
3527 /* Converts the third argument in a sample. */
3528 hlua_lua2smp(L, 3, &smp);
3529
3530 /* Store the sample in a variable. */
3531 smp_set_owner(&smp, s->be, s->sess, s, 0);
3532 vars_set_by_name(name, len, &smp);
3533 return 0;
3534}
3535
3536__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3537{
3538 struct hlua_appctx *appctx;
3539 struct stream *s;
3540 const char *name;
3541 size_t len;
3542 struct sample smp;
3543
3544 MAY_LJMP(check_args(L, 2, "unset_var"));
3545
3546 /* It is useles to retrieve the stream, but this function
3547 * runs only in a stream context.
3548 */
3549 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3550 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3551 s = appctx->htxn.s;
3552
3553 /* Unset the variable. */
3554 smp_set_owner(&smp, s->be, s->sess, s, 0);
3555 vars_unset_by_name(name, len, &smp);
3556 return 0;
3557}
3558
3559__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3560{
3561 struct hlua_appctx *appctx;
3562 struct stream *s;
3563 const char *name;
3564 size_t len;
3565 struct sample smp;
3566
3567 MAY_LJMP(check_args(L, 2, "get_var"));
3568
3569 /* It is useles to retrieve the stream, but this function
3570 * runs only in a stream context.
3571 */
3572 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3573 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3574 s = appctx->htxn.s;
3575
3576 smp_set_owner(&smp, s->be, s->sess, s, 0);
3577 if (!vars_get_by_name(name, len, &smp)) {
3578 lua_pushnil(L);
3579 return 1;
3580 }
3581
3582 return hlua_smp2lua(L, &smp);
3583}
3584
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003585__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3586{
3587 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3588 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003589 struct hlua *hlua;
3590
3591 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003592 if (!s->hlua)
3593 return 0;
3594 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003595
3596 MAY_LJMP(check_args(L, 2, "set_priv"));
3597
3598 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003599 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003600
3601 /* Get and store new value. */
3602 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3603 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3604
3605 return 0;
3606}
3607
3608__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3609{
3610 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3611 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003612 struct hlua *hlua;
3613
3614 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003615 if (!s->hlua) {
3616 lua_pushnil(L);
3617 return 1;
3618 }
3619 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003620
3621 /* Push configuration index in the stack. */
3622 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3623
3624 return 1;
3625}
3626
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003627/* If expected data not yet available, it returns a yield. This function
3628 * consumes the data in the buffer. It returns a string containing the
3629 * data. This string can be empty.
3630 */
3631__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3632{
3633 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3634 struct stream_interface *si = appctx->appctx->owner;
3635 int ret;
3636 char *blk1;
3637 int len1;
3638 char *blk2;
3639 int len2;
3640
3641 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003642 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003643
3644 /* Data not yet avalaible. return yield. */
3645 if (ret == 0) {
3646 si_applet_cant_get(si);
3647 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
3648 }
3649
3650 /* End of data: commit the total strings and return. */
3651 if (ret < 0) {
3652 luaL_pushresult(&appctx->b);
3653 return 1;
3654 }
3655
3656 /* Ensure that the block 2 length is usable. */
3657 if (ret == 1)
3658 len2 = 0;
3659
3660 /* dont check the max length read and dont check. */
3661 luaL_addlstring(&appctx->b, blk1, len1);
3662 luaL_addlstring(&appctx->b, blk2, len2);
3663
3664 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003665 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003666 luaL_pushresult(&appctx->b);
3667 return 1;
3668}
3669
3670/* Check arguments for the fucntion "hlua_channel_get_yield". */
3671__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3672{
3673 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3674
3675 /* Initialise the string catenation. */
3676 luaL_buffinit(L, &appctx->b);
3677
3678 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3679}
3680
3681/* If expected data not yet available, it returns a yield. This function
3682 * consumes the data in the buffer. It returns a string containing the
3683 * data. This string can be empty.
3684 */
3685__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3686{
3687 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3688 struct stream_interface *si = appctx->appctx->owner;
3689 int len = MAY_LJMP(luaL_checkinteger(L, 2));
3690 int ret;
3691 char *blk1;
3692 int len1;
3693 char *blk2;
3694 int len2;
3695
3696 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003697 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003698
3699 /* Data not yet avalaible. return yield. */
3700 if (ret == 0) {
3701 si_applet_cant_get(si);
3702 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3703 }
3704
3705 /* End of data: commit the total strings and return. */
3706 if (ret < 0) {
3707 luaL_pushresult(&appctx->b);
3708 return 1;
3709 }
3710
3711 /* Ensure that the block 2 length is usable. */
3712 if (ret == 1)
3713 len2 = 0;
3714
3715 if (len == -1) {
3716
3717 /* If len == -1, catenate all the data avalaile and
3718 * yield because we want to get all the data until
3719 * the end of data stream.
3720 */
3721 luaL_addlstring(&appctx->b, blk1, len1);
3722 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003723 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003724 si_applet_cant_get(si);
3725 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3726
3727 } else {
3728
3729 /* Copy the fisrt block caping to the length required. */
3730 if (len1 > len)
3731 len1 = len;
3732 luaL_addlstring(&appctx->b, blk1, len1);
3733 len -= len1;
3734
3735 /* Copy the second block. */
3736 if (len2 > len)
3737 len2 = len;
3738 luaL_addlstring(&appctx->b, blk2, len2);
3739 len -= len2;
3740
3741 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003742 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003743
3744 /* If we are no other data avalaible, yield waiting for new data. */
3745 if (len > 0) {
3746 lua_pushinteger(L, len);
3747 lua_replace(L, 2);
3748 si_applet_cant_get(si);
3749 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
3750 }
3751
3752 /* return the result. */
3753 luaL_pushresult(&appctx->b);
3754 return 1;
3755 }
3756
3757 /* we never executes this */
3758 hlua_pusherror(L, "Lua: internal error");
3759 WILL_LJMP(lua_error(L));
3760 return 0;
3761}
3762
3763/* Check arguments for the fucntion "hlua_channel_get_yield". */
3764__LJMP static int hlua_applet_tcp_recv(lua_State *L)
3765{
3766 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3767 int len = -1;
3768
3769 if (lua_gettop(L) > 2)
3770 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
3771 if (lua_gettop(L) >= 2) {
3772 len = MAY_LJMP(luaL_checkinteger(L, 2));
3773 lua_pop(L, 1);
3774 }
3775
3776 /* Confirm or set the required length */
3777 lua_pushinteger(L, len);
3778
3779 /* Initialise the string catenation. */
3780 luaL_buffinit(L, &appctx->b);
3781
3782 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
3783}
3784
3785/* Append data in the output side of the buffer. This data is immediatly
3786 * sent. The fcuntion returns the ammount of data writed. If the buffer
3787 * cannot contains the data, the function yield. The function returns -1
3788 * if the channel is closed.
3789 */
3790__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
3791{
3792 size_t len;
3793 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3794 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3795 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3796 struct stream_interface *si = appctx->appctx->owner;
3797 struct channel *chn = si_ic(si);
3798 int max;
3799
3800 /* Get the max amount of data which can write as input in the channel. */
3801 max = channel_recv_max(chn);
3802 if (max > (len - l))
3803 max = len - l;
3804
3805 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003806 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003807
3808 /* update counters. */
3809 l += max;
3810 lua_pop(L, 1);
3811 lua_pushinteger(L, l);
3812
3813 /* If some data is not send, declares the situation to the
3814 * applet, and returns a yield.
3815 */
3816 if (l < len) {
3817 si_applet_cant_put(si);
3818 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
3819 }
3820
3821 return 1;
3822}
3823
3824/* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits
3825 * yield the LUA process, and resume it without checking the
3826 * input arguments.
3827 */
3828__LJMP static int hlua_applet_tcp_send(lua_State *L)
3829{
3830 MAY_LJMP(check_args(L, 2, "send"));
3831 lua_pushinteger(L, 0);
3832
3833 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
3834}
3835
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003836/*
3837 *
3838 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003839 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003840 *
3841 *
3842 */
3843
3844/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003845 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003846 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003847__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003848{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003849 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003850}
3851
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003852/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003853 * according with a current TXN.
3854 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003855static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003856{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003857 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003858 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003859 struct stream_interface *si = ctx->owner;
3860 struct stream *s = si_strm(si);
3861 struct proxy *px = s->be;
3862 struct http_txn *txn = s->txn;
3863 const char *path;
3864 const char *end;
3865 const char *p;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003866
3867 /* Check stack size. */
3868 if (!lua_checkstack(L, 3))
3869 return 0;
3870
3871 /* Create the object: obj[0] = userdata.
3872 * Note that the base of the Converters object is the
3873 * same than the TXN object.
3874 */
3875 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003876 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003877 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003878 appctx->appctx = ctx;
3879 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003880 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003881 appctx->htxn.s = s;
3882 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003883
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003884 /* Create the "f" field that contains a list of fetches. */
3885 lua_pushstring(L, "f");
3886 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3887 return 0;
3888 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003889
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003890 /* Create the "sf" field that contains a list of stringsafe fetches. */
3891 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003892 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003893 return 0;
3894 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003895
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003896 /* Create the "c" field that contains a list of converters. */
3897 lua_pushstring(L, "c");
3898 if (!hlua_converters_new(L, &appctx->htxn, 0))
3899 return 0;
3900 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003901
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003902 /* Create the "sc" field that contains a list of stringsafe converters. */
3903 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003904 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003905 return 0;
3906 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003907
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003908 /* Stores the request method. */
3909 lua_pushstring(L, "method");
3910 lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l);
3911 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003912
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003913 /* Stores the http version. */
3914 lua_pushstring(L, "version");
3915 lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l);
3916 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003917
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003918 /* creates an array of headers. hlua_http_get_headers() crates and push
3919 * the array on the top of the stack.
3920 */
3921 lua_pushstring(L, "headers");
3922 htxn.s = s;
3923 htxn.p = px;
3924 htxn.dir = SMP_OPT_DIR_REQ;
3925 if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req))
3926 return 0;
3927 lua_settable(L, -3);
3928
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003929 /* Get path and qs */
3930 path = http_get_path(txn);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003931 if (path) {
3932 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
3933 p = path;
3934 while (p < end && *p != '?')
3935 p++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003936
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003937 /* Stores the request path. */
3938 lua_pushstring(L, "path");
3939 lua_pushlstring(L, path, p - path);
3940 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003941
Thierry FOURNIER7d388632017-02-22 02:06:16 +01003942 /* Stores the query string. */
3943 lua_pushstring(L, "qs");
3944 if (*p == '?')
3945 p++;
3946 lua_pushlstring(L, p, end - p);
3947 lua_settable(L, -3);
3948 }
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003949
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003950 /* Stores the request path. */
3951 lua_pushstring(L, "length");
3952 lua_pushinteger(L, txn->req.body_len);
3953 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003954
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003955 /* Create an array of HTTP request headers. */
3956 lua_pushstring(L, "headers");
3957 MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req));
3958 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003959
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003960 /* Create an empty array of HTTP request headers. */
3961 lua_pushstring(L, "response");
3962 lua_newtable(L);
3963 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01003964
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02003965 /* Pop a class stream metatable and affect it to the table. */
3966 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
3967 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01003968
3969 return 1;
3970}
3971
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003972__LJMP static int hlua_applet_http_set_var(lua_State *L)
3973{
3974 struct hlua_appctx *appctx;
3975 struct stream *s;
3976 const char *name;
3977 size_t len;
3978 struct sample smp;
3979
3980 MAY_LJMP(check_args(L, 3, "set_var"));
3981
3982 /* It is useles to retrieve the stream, but this function
3983 * runs only in a stream context.
3984 */
3985 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
3986 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3987 s = appctx->htxn.s;
3988
3989 /* Converts the third argument in a sample. */
3990 hlua_lua2smp(L, 3, &smp);
3991
3992 /* Store the sample in a variable. */
3993 smp_set_owner(&smp, s->be, s->sess, s, 0);
3994 vars_set_by_name(name, len, &smp);
3995 return 0;
3996}
3997
3998__LJMP static int hlua_applet_http_unset_var(lua_State *L)
3999{
4000 struct hlua_appctx *appctx;
4001 struct stream *s;
4002 const char *name;
4003 size_t len;
4004 struct sample smp;
4005
4006 MAY_LJMP(check_args(L, 2, "unset_var"));
4007
4008 /* It is useles to retrieve the stream, but this function
4009 * runs only in a stream context.
4010 */
4011 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4012 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4013 s = appctx->htxn.s;
4014
4015 /* Unset the variable. */
4016 smp_set_owner(&smp, s->be, s->sess, s, 0);
4017 vars_unset_by_name(name, len, &smp);
4018 return 0;
4019}
4020
4021__LJMP static int hlua_applet_http_get_var(lua_State *L)
4022{
4023 struct hlua_appctx *appctx;
4024 struct stream *s;
4025 const char *name;
4026 size_t len;
4027 struct sample smp;
4028
4029 MAY_LJMP(check_args(L, 2, "get_var"));
4030
4031 /* It is useles to retrieve the stream, but this function
4032 * runs only in a stream context.
4033 */
4034 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4035 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4036 s = appctx->htxn.s;
4037
4038 smp_set_owner(&smp, s->be, s->sess, s, 0);
4039 if (!vars_get_by_name(name, len, &smp)) {
4040 lua_pushnil(L);
4041 return 1;
4042 }
4043
4044 return hlua_smp2lua(L, &smp);
4045}
4046
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004047__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4048{
4049 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4050 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004051 struct hlua *hlua;
4052
4053 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004054 if (!s->hlua)
4055 return 0;
4056 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004057
4058 MAY_LJMP(check_args(L, 2, "set_priv"));
4059
4060 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004061 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004062
4063 /* Get and store new value. */
4064 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4065 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4066
4067 return 0;
4068}
4069
4070__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4071{
4072 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4073 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004074 struct hlua *hlua;
4075
4076 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004077 if (!s->hlua) {
4078 lua_pushnil(L);
4079 return 1;
4080 }
4081 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004082
4083 /* Push configuration index in the stack. */
4084 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4085
4086 return 1;
4087}
4088
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004089/* If expected data not yet available, it returns a yield. This function
4090 * consumes the data in the buffer. It returns a string containing the
4091 * data. This string can be empty.
4092 */
4093__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004094{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004095 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4096 struct stream_interface *si = appctx->appctx->owner;
4097 struct channel *chn = si_ic(si);
4098 int ret;
4099 char *blk1;
4100 int len1;
4101 char *blk2;
4102 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004103
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004104 /* Maybe we cant send a 100-continue ? */
4105 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004106 ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004107 /* if ret == -2 or -3 the channel closed or the message si too
4108 * big for the buffers. We cant send anything. So, we ignoring
4109 * the error, considers that the 100-continue is sent, and try
4110 * to receive.
4111 * If ret is -1, we dont have room in the buffer, so we yield.
4112 */
4113 if (ret == -1) {
4114 si_applet_cant_put(si);
4115 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
4116 }
4117 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4118 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004119
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004120 /* Check for the end of the data. */
4121 if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) {
4122 luaL_pushresult(&appctx->b);
4123 return 1;
4124 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004125
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004126 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004127 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004128
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004129 /* Data not yet avalaible. return yield. */
4130 if (ret == 0) {
4131 si_applet_cant_get(si);
4132 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
4133 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004134
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004135 /* End of data: commit the total strings and return. */
4136 if (ret < 0) {
4137 luaL_pushresult(&appctx->b);
4138 return 1;
4139 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004140
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004141 /* Ensure that the block 2 length is usable. */
4142 if (ret == 1)
4143 len2 = 0;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004144
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004145 /* Copy the fisrt block caping to the length required. */
4146 if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4147 len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4148 luaL_addlstring(&appctx->b, blk1, len1);
4149 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004150
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004151 /* Copy the second block. */
4152 if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4153 len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4154 luaL_addlstring(&appctx->b, blk2, len2);
4155 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004156
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004157 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004158 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004159 luaL_pushresult(&appctx->b);
4160 return 1;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004161}
4162
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004163/* Check arguments for the fucntion "hlua_channel_get_yield". */
4164__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004165{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004166 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004167
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004168 /* Initialise the string catenation. */
4169 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004170
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004171 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004172}
4173
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004174/* If expected data not yet available, it returns a yield. This function
4175 * consumes the data in the buffer. It returns a string containing the
4176 * data. This string can be empty.
4177 */
4178__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004179{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004180 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4181 struct stream_interface *si = appctx->appctx->owner;
4182 int len = MAY_LJMP(luaL_checkinteger(L, 2));
4183 struct channel *chn = si_ic(si);
4184 int ret;
4185 char *blk1;
4186 int len1;
4187 char *blk2;
4188 int len2;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004189
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004190 /* Maybe we cant send a 100-continue ? */
4191 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004192 ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004193 /* if ret == -2 or -3 the channel closed or the message si too
4194 * big for the buffers. We cant send anything. So, we ignoring
4195 * the error, considers that the 100-continue is sent, and try
4196 * to receive.
4197 * If ret is -1, we dont have room in the buffer, so we yield.
4198 */
4199 if (ret == -1) {
4200 si_applet_cant_put(si);
4201 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4202 }
4203 appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C;
4204 }
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004205
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004206 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004207 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004208
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004209 /* Data not yet avalaible. return yield. */
4210 if (ret == 0) {
4211 si_applet_cant_get(si);
4212 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4213 }
4214
4215 /* End of data: commit the total strings and return. */
4216 if (ret < 0) {
4217 luaL_pushresult(&appctx->b);
4218 return 1;
4219 }
4220
4221 /* Ensure that the block 2 length is usable. */
4222 if (ret == 1)
4223 len2 = 0;
4224
4225 /* Copy the fisrt block caping to the length required. */
4226 if (len1 > len)
4227 len1 = len;
4228 luaL_addlstring(&appctx->b, blk1, len1);
4229 len -= len1;
4230
4231 /* Copy the second block. */
4232 if (len2 > len)
4233 len2 = len;
4234 luaL_addlstring(&appctx->b, blk2, len2);
4235 len -= len2;
4236
4237 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004238 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004239 if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1)
4240 appctx->appctx->ctx.hlua_apphttp.left_bytes -= len;
4241
4242 /* If we are no other data avalaible, yield waiting for new data. */
4243 if (len > 0) {
4244 lua_pushinteger(L, len);
4245 lua_replace(L, 2);
4246 si_applet_cant_get(si);
4247 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
4248 }
4249
4250 /* return the result. */
4251 luaL_pushresult(&appctx->b);
4252 return 1;
4253}
4254
4255/* Check arguments for the fucntion "hlua_channel_get_yield". */
4256__LJMP static int hlua_applet_http_recv(lua_State *L)
4257{
4258 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4259 int len = -1;
4260
4261 /* Check arguments. */
4262 if (lua_gettop(L) > 2)
4263 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4264 if (lua_gettop(L) >= 2) {
4265 len = MAY_LJMP(luaL_checkinteger(L, 2));
4266 lua_pop(L, 1);
4267 }
4268
4269 /* Check the required length */
4270 if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes)
4271 len = appctx->appctx->ctx.hlua_apphttp.left_bytes;
4272 lua_pushinteger(L, len);
4273
4274 /* Initialise the string catenation. */
4275 luaL_buffinit(L, &appctx->b);
4276
4277 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
4278}
4279
4280/* Append data in the output side of the buffer. This data is immediatly
4281 * sent. The fcuntion returns the ammount of data writed. If the buffer
4282 * cannot contains the data, the function yield. The function returns -1
4283 * if the channel is closed.
4284 */
4285__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
4286{
4287 size_t len;
4288 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4289 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4290 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4291 struct stream_interface *si = appctx->appctx->owner;
4292 struct channel *chn = si_ic(si);
4293 int max;
4294
4295 /* Get the max amount of data which can write as input in the channel. */
4296 max = channel_recv_max(chn);
4297 if (max > (len - l))
4298 max = len - l;
4299
4300 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004301 ci_putblk(chn, str + l, max);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004302
4303 /* update counters. */
4304 l += max;
4305 lua_pop(L, 1);
4306 lua_pushinteger(L, l);
4307
4308 /* If some data is not send, declares the situation to the
4309 * applet, and returns a yield.
4310 */
4311 if (l < len) {
4312 si_applet_cant_put(si);
4313 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
4314 }
4315
4316 return 1;
4317}
4318
4319/* Just a wraper of "hlua_applet_send_yield". This wrapper permits
4320 * yield the LUA process, and resume it without checking the
4321 * input arguments.
4322 */
4323__LJMP static int hlua_applet_http_send(lua_State *L)
4324{
4325 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4326 size_t len;
4327 char hex[10];
4328
4329 MAY_LJMP(luaL_checklstring(L, 2, &len));
4330
4331 /* If transfer encoding chunked is selected, we surround the data
4332 * by chunk data.
4333 */
4334 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) {
4335 snprintf(hex, 9, "%x", (unsigned int)len);
4336 lua_pushfstring(L, "%s\r\n", hex);
4337 lua_insert(L, 2); /* swap the last 2 entries. */
4338 lua_pushstring(L, "\r\n");
4339 lua_concat(L, 3);
4340 }
4341
4342 /* This interger is used for followinf the amount of data sent. */
4343 lua_pushinteger(L, 0);
4344
4345 /* We want to send some data. Headers must be sent. */
4346 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4347 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4348 WILL_LJMP(lua_error(L));
4349 }
4350
4351 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
4352}
4353
4354__LJMP static int hlua_applet_http_addheader(lua_State *L)
4355{
4356 const char *name;
4357 int ret;
4358
4359 MAY_LJMP(hlua_checkapplet_http(L, 1));
4360 name = MAY_LJMP(luaL_checkstring(L, 2));
4361 MAY_LJMP(luaL_checkstring(L, 3));
4362
4363 /* Push in the stack the "response" entry. */
4364 ret = lua_getfield(L, 1, "response");
4365 if (ret != LUA_TTABLE) {
4366 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4367 "is expected as an array. %s found", lua_typename(L, ret));
4368 WILL_LJMP(lua_error(L));
4369 }
4370
4371 /* check if the header is already registered if it is not
4372 * the case, register it.
4373 */
4374 ret = lua_getfield(L, -1, name);
4375 if (ret == LUA_TNIL) {
4376
4377 /* Entry not found. */
4378 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4379
4380 /* Insert the new header name in the array in the top of the stack.
4381 * It left the new array in the top of the stack.
4382 */
4383 lua_newtable(L);
4384 lua_pushvalue(L, 2);
4385 lua_pushvalue(L, -2);
4386 lua_settable(L, -4);
4387
4388 } else if (ret != LUA_TTABLE) {
4389
4390 /* corruption error. */
4391 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4392 "is expected as an array. %s found", name, lua_typename(L, ret));
4393 WILL_LJMP(lua_error(L));
4394 }
4395
4396 /* Now the top od thestack is an array of values. We push
4397 * the header value as new entry.
4398 */
4399 lua_pushvalue(L, 3);
4400 ret = lua_rawlen(L, -2);
4401 lua_rawseti(L, -2, ret + 1);
4402 lua_pushboolean(L, 1);
4403 return 1;
4404}
4405
4406__LJMP static int hlua_applet_http_status(lua_State *L)
4407{
4408 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4409 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004410 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004411
4412 if (status < 100 || status > 599) {
4413 lua_pushboolean(L, 0);
4414 return 1;
4415 }
4416
4417 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004418 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004419 lua_pushboolean(L, 1);
4420 return 1;
4421}
4422
4423/* We will build the status line and the headers of the HTTP response.
4424 * We will try send at once if its not possible, we give back the hand
4425 * waiting for more room.
4426 */
4427__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
4428{
4429 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4430 struct stream_interface *si = appctx->appctx->owner;
4431 struct channel *chn = si_ic(si);
4432 int ret;
4433 size_t len;
4434 const char *msg;
4435
4436 /* Get the message as the first argument on the stack. */
4437 msg = MAY_LJMP(luaL_checklstring(L, 2, &len));
4438
4439 /* Send the message at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004440 ret = ci_putblk(chn, msg, len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004441
4442 /* if ret == -2 or -3 the channel closed or the message si too
4443 * big for the buffers.
4444 */
4445 if (ret == -2 || ret == -3) {
4446 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4447 WILL_LJMP(lua_error(L));
4448 }
4449
4450 /* If ret is -1, we dont have room in the buffer, so we yield. */
4451 if (ret == -1) {
4452 si_applet_cant_put(si);
4453 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
4454 }
4455
4456 /* Headers sent, set the flag. */
4457 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4458 return 0;
4459}
4460
4461__LJMP static int hlua_applet_http_start_response(lua_State *L)
4462{
4463 struct chunk *tmp = get_trash_chunk();
4464 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004465 const char *name;
Willy Tarreaua3294632017-08-23 11:24:47 +02004466 size_t name_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004467 const char *value;
Willy Tarreaua3294632017-08-23 11:24:47 +02004468 size_t value_len;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004469 int id;
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004470 long long hdr_contentlength = -1;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004471 int hdr_chunked = 0;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004472 const char *reason = appctx->appctx->ctx.hlua_apphttp.reason;
4473
4474 if (reason == NULL)
4475 reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004476
4477 /* Use the same http version than the request. */
4478 chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n",
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01004479 appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0',
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004480 appctx->appctx->ctx.hlua_apphttp.status,
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004481 reason);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004482
4483 /* Get the array associated to the field "response" in the object AppletHTTP. */
4484 lua_pushvalue(L, 0);
4485 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4486 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
4487 appctx->appctx->rule->arg.hlua_rule->fcn.name);
4488 WILL_LJMP(lua_error(L));
4489 }
4490
4491 /* Browse the list of headers. */
4492 lua_pushnil(L);
4493 while(lua_next(L, -2) != 0) {
4494
4495 /* We expect a string as -2. */
4496 if (lua_type(L, -2) != LUA_TSTRING) {
4497 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
4498 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4499 lua_typename(L, lua_type(L, -2)));
4500 WILL_LJMP(lua_error(L));
4501 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004502 name = lua_tolstring(L, -2, &name_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004503
4504 /* We expect an array as -1. */
4505 if (lua_type(L, -1) != LUA_TTABLE) {
4506 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
4507 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4508 name,
4509 lua_typename(L, lua_type(L, -1)));
4510 WILL_LJMP(lua_error(L));
4511 }
4512
4513 /* Browse the table who is on the top of the stack. */
4514 lua_pushnil(L);
4515 while(lua_next(L, -2) != 0) {
4516
4517 /* We expect a number as -2. */
4518 if (lua_type(L, -2) != LUA_TNUMBER) {
4519 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
4520 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4521 name,
4522 lua_typename(L, lua_type(L, -2)));
4523 WILL_LJMP(lua_error(L));
4524 }
4525 id = lua_tointeger(L, -2);
4526
4527 /* We expect a string as -2. */
4528 if (lua_type(L, -1) != LUA_TSTRING) {
4529 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
4530 appctx->appctx->rule->arg.hlua_rule->fcn.name,
4531 name, id,
4532 lua_typename(L, lua_type(L, -1)));
4533 WILL_LJMP(lua_error(L));
4534 }
Willy Tarreaua3294632017-08-23 11:24:47 +02004535 value = lua_tolstring(L, -1, &value_len);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004536
4537 /* Catenate a new header. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004538 if (tmp->len + name_len + 2 + value_len + 2 < tmp->size) {
4539 memcpy(tmp->str + tmp->len, name, name_len);
4540 tmp->len += name_len;
4541 tmp->str[tmp->len++] = ':';
4542 tmp->str[tmp->len++] = ' ';
4543
4544 memcpy(tmp->str + tmp->len, value, value_len);
4545 tmp->len += value_len;
4546 tmp->str[tmp->len++] = '\r';
4547 tmp->str[tmp->len++] = '\n';
4548 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004549
4550 /* Protocol checks. */
4551
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004552 /* Copy the header content length. The length conversion
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004553 * is done without control. If it contains a bad value,
4554 * the content-length remains negative so that we can
4555 * switch to either chunked encoding or close.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004556 */
Willy Tarreaua3294632017-08-23 11:24:47 +02004557 if (name_len == 14 && strcasecmp("content-length", name) == 0)
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004558 strl2llrc(value, strlen(value), &hdr_contentlength);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004559
4560 /* Check if the client annouces a transfer-encoding chunked it self. */
Willy Tarreaua3294632017-08-23 11:24:47 +02004561 if (name_len == 17 && value_len == 7 &&
4562 strcasecmp("transfer-encoding", name) == 0 &&
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004563 strcasecmp("chunked", value) == 0)
4564 hdr_chunked = 1;
4565
4566 /* Remove the array from the stack, and get next element with a remaining string. */
4567 lua_pop(L, 1);
4568 }
4569
4570 /* Remove the array from the stack, and get next element with a remaining string. */
4571 lua_pop(L, 1);
4572 }
4573
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004574 /* If we dont have a content-length set, and the HTTP version is 1.1
4575 * and the status code implies the presence of a message body, we must
4576 * announce a transfer encoding chunked. This is required by haproxy
4577 * for the keepalive compliance. If the applet annouces a transfer-encoding
4578 * chunked itslef, don't do anything.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004579 */
Willy Tarreauc9f4ea02017-08-23 09:32:06 +02004580 if (hdr_contentlength < 0 && hdr_chunked == 0 &&
Willy Tarreau06c75fe2017-08-23 09:10:38 +02004581 (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) &&
4582 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4583 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4584 appctx->appctx->ctx.hlua_apphttp.status != 304) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004585 chunk_appendf(tmp, "Transfer-encoding: chunked\r\n");
4586 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED;
4587 }
4588
4589 /* Finalize headers. */
4590 chunk_appendf(tmp, "\r\n");
4591
4592 /* Remove the last entry and the array of headers */
4593 lua_pop(L, 2);
4594
4595 /* Push the headers block. */
4596 lua_pushlstring(L, tmp->str, tmp->len);
4597
4598 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
4599}
4600
4601/*
4602 *
4603 *
4604 * Class HTTP
4605 *
4606 *
4607 */
4608
4609/* Returns a struct hlua_txn if the stack entry "ud" is
4610 * a class stream, otherwise it throws an error.
4611 */
4612__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
4613{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004614 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004615}
4616
4617/* This function creates and push in the stack a HTTP object
4618 * according with a current TXN.
4619 */
4620static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4621{
4622 struct hlua_txn *htxn;
4623
4624 /* Check stack size. */
4625 if (!lua_checkstack(L, 3))
4626 return 0;
4627
4628 /* Create the object: obj[0] = userdata.
4629 * Note that the base of the Converters object is the
4630 * same than the TXN object.
4631 */
4632 lua_newtable(L);
4633 htxn = lua_newuserdata(L, sizeof(*htxn));
4634 lua_rawseti(L, -2, 0);
4635
4636 htxn->s = txn->s;
4637 htxn->p = txn->p;
4638
4639 /* Pop a class stream metatable and affect it to the table. */
4640 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4641 lua_setmetatable(L, -2);
4642
4643 return 1;
4644}
4645
4646/* This function creates ans returns an array of HTTP headers.
4647 * This function does not fails. It is used as wrapper with the
4648 * 2 following functions.
4649 */
4650__LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4651{
4652 const char *cur_ptr, *cur_next, *p;
4653 int old_idx, cur_idx;
4654 struct hdr_idx_elem *cur_hdr;
4655 const char *hn, *hv;
4656 int hnl, hvl;
4657 int type;
4658 const char *in;
4659 char *out;
4660 int len;
4661
4662 /* Create the table. */
4663 lua_newtable(L);
4664
4665 if (!htxn->s->txn)
4666 return 1;
4667
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004668 /* Check if a valid response is parsed */
4669 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4670 return 1;
4671
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004672 /* Build array of headers. */
4673 old_idx = 0;
4674 cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx);
4675
4676 while (1) {
4677 cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next;
4678 if (!cur_idx)
4679 break;
4680 old_idx = cur_idx;
4681
4682 cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx];
4683 cur_ptr = cur_next;
4684 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
4685
4686 /* Now we have one full header at cur_ptr of len cur_hdr->len,
4687 * and the next header starts at cur_next. We'll check
4688 * this header in the list as well as against the default
4689 * rule.
4690 */
4691
4692 /* look for ': *'. */
4693 hn = cur_ptr;
4694 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
4695 if (p >= cur_ptr+cur_hdr->len)
4696 continue;
4697 hnl = p - hn;
4698 p++;
4699 while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' ))
4700 p++;
4701 if (p >= cur_ptr+cur_hdr->len)
4702 continue;
4703 hv = p;
4704 hvl = cur_ptr+cur_hdr->len-p;
4705
4706 /* Lowercase the key. Don't check the size of trash, it have
4707 * the size of one buffer and the input data contains in one
4708 * buffer.
4709 */
4710 out = trash.str;
4711 for (in=hn; in<hn+hnl; in++, out++)
4712 *out = tolower(*in);
4713 *out = '\0';
4714
4715 /* Check for existing entry:
4716 * assume that the table is on the top of the stack, and
4717 * push the key in the stack, the function lua_gettable()
4718 * perform the lookup.
4719 */
4720 lua_pushlstring(L, trash.str, hnl);
4721 lua_gettable(L, -2);
4722 type = lua_type(L, -1);
4723
4724 switch (type) {
4725 case LUA_TNIL:
4726 /* Table not found, create it. */
4727 lua_pop(L, 1); /* remove the nil value. */
4728 lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */
4729 lua_newtable(L); /* create and push empty table. */
4730 lua_pushlstring(L, hv, hvl); /* push header value. */
4731 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4732 lua_rawset(L, -3); /* index new table with header name (pop the values). */
4733 break;
4734
4735 case LUA_TTABLE:
4736 /* Entry found: push the value in the table. */
4737 len = lua_rawlen(L, -1);
4738 lua_pushlstring(L, hv, hvl); /* push header value. */
4739 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4740 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
4741 break;
4742
4743 default:
4744 /* Other cases are errors. */
4745 hlua_pusherror(L, "internal error during the parsing of headers.");
4746 WILL_LJMP(lua_error(L));
4747 }
4748 }
4749
4750 return 1;
4751}
4752
4753__LJMP static int hlua_http_req_get_headers(lua_State *L)
4754{
4755 struct hlua_txn *htxn;
4756
4757 MAY_LJMP(check_args(L, 1, "req_get_headers"));
4758 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4759
4760 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
4761}
4762
4763__LJMP static int hlua_http_res_get_headers(lua_State *L)
4764{
4765 struct hlua_txn *htxn;
4766
4767 MAY_LJMP(check_args(L, 1, "res_get_headers"));
4768 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4769
4770 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
4771}
4772
4773/* This function replace full header, or just a value in
4774 * the request or in the response. It is a wrapper fir the
4775 * 4 following functions.
4776 */
4777__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn,
4778 struct http_msg *msg, int action)
4779{
4780 size_t name_len;
4781 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4782 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
4783 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
4784 struct my_regex re;
4785
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004786 /* Check if a valid response is parsed */
4787 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4788 return 0;
4789
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004790 if (!regex_comp(reg, &re, 1, 1, NULL))
4791 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
4792
4793 http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action);
4794 regex_free(&re);
4795 return 0;
4796}
4797
4798__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
4799{
4800 struct hlua_txn *htxn;
4801
4802 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4803 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4804
4805 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
4806}
4807
4808__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
4809{
4810 struct hlua_txn *htxn;
4811
4812 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
4813 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4814
4815 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
4816}
4817
4818__LJMP static int hlua_http_req_rep_val(lua_State *L)
4819{
4820 struct hlua_txn *htxn;
4821
4822 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
4823 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4824
4825 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
4826}
4827
4828__LJMP static int hlua_http_res_rep_val(lua_State *L)
4829{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004830 struct hlua_txn *htxn;
4831
4832 MAY_LJMP(check_args(L, 4, "res_rep_val"));
4833 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4834
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02004835 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004836}
4837
4838/* This function deletes all the occurences of an header.
4839 * It is a wrapper for the 2 following functions.
4840 */
4841__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4842{
4843 size_t len;
4844 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4845 struct hdr_ctx ctx;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004846 struct http_txn *txn = htxn->s->txn;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004847
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004848 /* Check if a valid response is parsed */
4849 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4850 return 0;
4851
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004852 ctx.idx = 0;
4853 while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx))
4854 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4855 return 0;
4856}
4857
4858__LJMP static int hlua_http_req_del_hdr(lua_State *L)
4859{
4860 struct hlua_txn *htxn;
4861
4862 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4863 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4864
Willy Tarreaueee5b512015-04-03 23:46:31 +02004865 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004866}
4867
4868__LJMP static int hlua_http_res_del_hdr(lua_State *L)
4869{
4870 struct hlua_txn *htxn;
4871
4872 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
4873 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4874
Willy Tarreaueee5b512015-04-03 23:46:31 +02004875 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004876}
4877
4878/* This function adds an header. It is a wrapper used by
4879 * the 2 following functions.
4880 */
4881__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg)
4882{
4883 size_t name_len;
4884 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
4885 size_t value_len;
4886 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
4887 char *p;
4888
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004889 /* Check if a valid message is parsed */
4890 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4891 return 0;
4892
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004893 /* Check length. */
4894 trash.len = value_len + name_len + 2;
4895 if (trash.len > trash.size)
4896 return 0;
4897
4898 /* Creates the header string. */
4899 p = trash.str;
4900 memcpy(p, name, name_len);
4901 p += name_len;
4902 *p = ':';
4903 p++;
4904 *p = ' ';
4905 p++;
4906 memcpy(p, value, value_len);
4907
Willy Tarreaueee5b512015-04-03 23:46:31 +02004908 lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx,
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004909 trash.str, trash.len) != 0);
4910
4911 return 0;
4912}
4913
4914__LJMP static int hlua_http_req_add_hdr(lua_State *L)
4915{
4916 struct hlua_txn *htxn;
4917
4918 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
4919 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4920
Willy Tarreaueee5b512015-04-03 23:46:31 +02004921 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004922}
4923
4924__LJMP static int hlua_http_res_add_hdr(lua_State *L)
4925{
4926 struct hlua_txn *htxn;
4927
4928 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
4929 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4930
Willy Tarreaueee5b512015-04-03 23:46:31 +02004931 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004932}
4933
4934static int hlua_http_req_set_hdr(lua_State *L)
4935{
4936 struct hlua_txn *htxn;
4937
4938 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
4939 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4940
Willy Tarreaueee5b512015-04-03 23:46:31 +02004941 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
4942 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004943}
4944
4945static int hlua_http_res_set_hdr(lua_State *L)
4946{
4947 struct hlua_txn *htxn;
4948
4949 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
4950 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
4951
Willy Tarreaueee5b512015-04-03 23:46:31 +02004952 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
4953 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004954}
4955
4956/* This function set the method. */
4957static int hlua_http_req_set_meth(lua_State *L)
4958{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004959 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004960 size_t name_len;
4961 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004962
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004963 /* Check if a valid request is parsed */
4964 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4965 lua_pushboolean(L, 0);
4966 return 1;
4967 }
4968
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004969 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004970 return 1;
4971}
4972
4973/* This function set the method. */
4974static int hlua_http_req_set_path(lua_State *L)
4975{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004976 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004977 size_t name_len;
4978 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004979
4980 /* Check if a valid request is parsed */
4981 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4982 lua_pushboolean(L, 0);
4983 return 1;
4984 }
4985
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004986 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004987 return 1;
4988}
4989
4990/* This function set the query-string. */
4991static int hlua_http_req_set_query(lua_State *L)
4992{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02004993 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004994 size_t name_len;
4995 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004996
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02004997 /* Check if a valid request is parsed */
4998 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
4999 lua_pushboolean(L, 0);
5000 return 1;
5001 }
5002
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005003 /* Check length. */
5004 if (name_len > trash.size - 1) {
5005 lua_pushboolean(L, 0);
5006 return 1;
5007 }
5008
5009 /* Add the mark question as prefix. */
5010 chunk_reset(&trash);
5011 trash.str[trash.len++] = '?';
5012 memcpy(trash.str + trash.len, name, name_len);
5013 trash.len += name_len;
5014
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005015 lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005016 return 1;
5017}
5018
5019/* This function set the uri. */
5020static int hlua_http_req_set_uri(lua_State *L)
5021{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005022 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005023 size_t name_len;
5024 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005025
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005026 /* Check if a valid request is parsed */
5027 if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) {
5028 lua_pushboolean(L, 0);
5029 return 1;
5030 }
5031
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005032 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005033 return 1;
5034}
5035
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005036/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005037static int hlua_http_res_set_status(lua_State *L)
5038{
5039 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5040 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005041 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005042
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005043 /* Check if a valid response is parsed */
5044 if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY))
5045 return 0;
5046
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005047 http_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005048 return 0;
5049}
5050
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005051/*
5052 *
5053 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005054 * Class TXN
5055 *
5056 *
5057 */
5058
5059/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005060 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005061 */
5062__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5063{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005064 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005065}
5066
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005067__LJMP static int hlua_set_var(lua_State *L)
5068{
5069 struct hlua_txn *htxn;
5070 const char *name;
5071 size_t len;
5072 struct sample smp;
5073
5074 MAY_LJMP(check_args(L, 3, "set_var"));
5075
5076 /* It is useles to retrieve the stream, but this function
5077 * runs only in a stream context.
5078 */
5079 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5080 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5081
5082 /* Converts the third argument in a sample. */
5083 hlua_lua2smp(L, 3, &smp);
5084
5085 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005086 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005087 vars_set_by_name(name, len, &smp);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005088 return 0;
5089}
5090
Christopher Faulet85d79c92016-11-09 16:54:56 +01005091__LJMP static int hlua_unset_var(lua_State *L)
5092{
5093 struct hlua_txn *htxn;
5094 const char *name;
5095 size_t len;
5096 struct sample smp;
5097
5098 MAY_LJMP(check_args(L, 2, "unset_var"));
5099
5100 /* It is useles to retrieve the stream, but this function
5101 * runs only in a stream context.
5102 */
5103 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5104 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5105
5106 /* Unset the variable. */
5107 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
5108 vars_unset_by_name(name, len, &smp);
5109 return 0;
5110}
5111
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005112__LJMP static int hlua_get_var(lua_State *L)
5113{
5114 struct hlua_txn *htxn;
5115 const char *name;
5116 size_t len;
5117 struct sample smp;
5118
5119 MAY_LJMP(check_args(L, 2, "get_var"));
5120
5121 /* It is useles to retrieve the stream, but this function
5122 * runs only in a stream context.
5123 */
5124 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5125 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5126
Willy Tarreau7560dd42016-03-10 16:28:58 +01005127 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005128 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005129 lua_pushnil(L);
5130 return 1;
5131 }
5132
5133 return hlua_smp2lua(L, &smp);
5134}
5135
Willy Tarreau59551662015-03-10 14:23:13 +01005136__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005137{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005138 struct hlua *hlua;
5139
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005140 MAY_LJMP(check_args(L, 2, "set_priv"));
5141
Willy Tarreau87b09662015-04-03 00:22:06 +02005142 /* It is useles to retrieve the stream, but this function
5143 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005144 */
5145 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005146 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005147
5148 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005149 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005150
5151 /* Get and store new value. */
5152 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5153 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5154
5155 return 0;
5156}
5157
Willy Tarreau59551662015-03-10 14:23:13 +01005158__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005159{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005160 struct hlua *hlua;
5161
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005162 MAY_LJMP(check_args(L, 1, "get_priv"));
5163
Willy Tarreau87b09662015-04-03 00:22:06 +02005164 /* It is useles to retrieve the stream, but this function
5165 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005166 */
5167 MAY_LJMP(hlua_checktxn(L, 1));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005168 hlua = hlua_gethlua(L);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005169
5170 /* Push configuration index in the stack. */
5171 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5172
5173 return 1;
5174}
5175
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005176/* Create stack entry containing a class TXN. This function
5177 * return 0 if the stack does not contains free slots,
5178 * otherwise it returns 1.
5179 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005180static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005181{
Willy Tarreaude491382015-04-06 11:04:28 +02005182 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005183
5184 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005185 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005186 return 0;
5187
5188 /* NOTE: The allocation never fails. The failure
5189 * throw an error, and the function never returns.
5190 * if the throw is not avalaible, the process is aborted.
5191 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005192 /* Create the object: obj[0] = userdata. */
5193 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005194 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005195 lua_rawseti(L, -2, 0);
5196
Willy Tarreaude491382015-04-06 11:04:28 +02005197 htxn->s = s;
5198 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005199 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005200 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005201
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005202 /* Create the "f" field that contains a list of fetches. */
5203 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005204 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005205 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005206 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005207
5208 /* Create the "sf" field that contains a list of stringsafe fetches. */
5209 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005210 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005211 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005212 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005213
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005214 /* Create the "c" field that contains a list of converters. */
5215 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005216 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005217 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005218 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005219
5220 /* Create the "sc" field that contains a list of stringsafe converters. */
5221 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005222 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005223 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005224 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005225
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005226 /* Create the "req" field that contains the request channel object. */
5227 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005228 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005229 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005230 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005231
5232 /* Create the "res" field that contains the response channel object. */
5233 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005234 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005235 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005236 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005237
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005238 /* Creates the HTTP object is the current proxy allows http. */
5239 lua_pushstring(L, "http");
5240 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005241 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005242 return 0;
5243 }
5244 else
5245 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005246 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005247
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005248 /* Pop a class sesison metatable and affect it to the userdata. */
5249 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5250 lua_setmetatable(L, -2);
5251
5252 return 1;
5253}
5254
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005255__LJMP static int hlua_txn_deflog(lua_State *L)
5256{
5257 const char *msg;
5258 struct hlua_txn *htxn;
5259
5260 MAY_LJMP(check_args(L, 2, "deflog"));
5261 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5262 msg = MAY_LJMP(luaL_checkstring(L, 2));
5263
5264 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5265 return 0;
5266}
5267
5268__LJMP static int hlua_txn_log(lua_State *L)
5269{
5270 int level;
5271 const char *msg;
5272 struct hlua_txn *htxn;
5273
5274 MAY_LJMP(check_args(L, 3, "log"));
5275 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5276 level = MAY_LJMP(luaL_checkinteger(L, 2));
5277 msg = MAY_LJMP(luaL_checkstring(L, 3));
5278
5279 if (level < 0 || level >= NB_LOG_LEVELS)
5280 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5281
5282 hlua_sendlog(htxn->s->be, level, msg);
5283 return 0;
5284}
5285
5286__LJMP static int hlua_txn_log_debug(lua_State *L)
5287{
5288 const char *msg;
5289 struct hlua_txn *htxn;
5290
5291 MAY_LJMP(check_args(L, 2, "Debug"));
5292 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5293 msg = MAY_LJMP(luaL_checkstring(L, 2));
5294 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5295 return 0;
5296}
5297
5298__LJMP static int hlua_txn_log_info(lua_State *L)
5299{
5300 const char *msg;
5301 struct hlua_txn *htxn;
5302
5303 MAY_LJMP(check_args(L, 2, "Info"));
5304 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5305 msg = MAY_LJMP(luaL_checkstring(L, 2));
5306 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5307 return 0;
5308}
5309
5310__LJMP static int hlua_txn_log_warning(lua_State *L)
5311{
5312 const char *msg;
5313 struct hlua_txn *htxn;
5314
5315 MAY_LJMP(check_args(L, 2, "Warning"));
5316 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5317 msg = MAY_LJMP(luaL_checkstring(L, 2));
5318 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5319 return 0;
5320}
5321
5322__LJMP static int hlua_txn_log_alert(lua_State *L)
5323{
5324 const char *msg;
5325 struct hlua_txn *htxn;
5326
5327 MAY_LJMP(check_args(L, 2, "Alert"));
5328 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5329 msg = MAY_LJMP(luaL_checkstring(L, 2));
5330 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5331 return 0;
5332}
5333
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005334__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5335{
5336 struct hlua_txn *htxn;
5337 int ll;
5338
5339 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5340 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5341 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5342
5343 if (ll < 0 || ll > 7)
5344 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5345
5346 htxn->s->logs.level = ll;
5347 return 0;
5348}
5349
5350__LJMP static int hlua_txn_set_tos(lua_State *L)
5351{
5352 struct hlua_txn *htxn;
5353 struct connection *cli_conn;
5354 int tos;
5355
5356 MAY_LJMP(check_args(L, 2, "set_tos"));
5357 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5358 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5359
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005360 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005361 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005362
5363 return 0;
5364}
5365
5366__LJMP static int hlua_txn_set_mark(lua_State *L)
5367{
5368#ifdef SO_MARK
5369 struct hlua_txn *htxn;
5370 struct connection *cli_conn;
5371 int mark;
5372
5373 MAY_LJMP(check_args(L, 2, "set_mark"));
5374 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5375 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5376
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02005377 if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02005378 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005379#endif
5380 return 0;
5381}
5382
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005383/* This function is an Lua binding that send pending data
5384 * to the client, and close the stream interface.
5385 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005386__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005387{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005388 struct hlua_txn *htxn;
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005389 struct hlua *hlua;
Willy Tarreau81389672015-03-10 12:03:52 +01005390 struct channel *ic, *oc;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005391
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005392 MAY_LJMP(check_args(L, 1, "close"));
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005393 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005394 hlua = hlua_gethlua(L);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005395
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005396 /* If the flags NOTERM is set, we cannot terminate the http
5397 * session, so we just end the execution of the current
5398 * lua code.
5399 */
5400 if (htxn->flags & HLUA_TXN_NOTERM) {
5401 WILL_LJMP(hlua_done(L));
5402 return 0;
5403 }
5404
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005405 ic = &htxn->s->req;
5406 oc = &htxn->s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005407
Willy Tarreau630ef452015-08-28 10:06:15 +02005408 if (htxn->s->txn) {
5409 /* HTTP mode, let's stay in sync with the stream */
5410 bi_fast_delete(ic->buf, htxn->s->txn->req.sov);
5411 htxn->s->txn->req.next -= htxn->s->txn->req.sov;
5412 htxn->s->txn->req.sov = 0;
5413 ic->analysers &= AN_REQ_HTTP_XFER_BODY;
5414 oc->analysers = AN_RES_HTTP_XFER_BODY;
5415 htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED;
5416 htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE;
5417
Willy Tarreau630ef452015-08-28 10:06:15 +02005418 /* Note that if we want to support keep-alive, we need
5419 * to bypass the close/shutr_now calls below, but that
5420 * may only be done if the HTTP request was already
5421 * processed and the connection header is known (ie
5422 * not during TCP rules).
5423 */
5424 }
5425
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005426 channel_auto_read(ic);
Willy Tarreau81389672015-03-10 12:03:52 +01005427 channel_abort(ic);
5428 channel_auto_close(ic);
5429 channel_erase(ic);
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005430
5431 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau81389672015-03-10 12:03:52 +01005432 channel_auto_read(oc);
5433 channel_auto_close(oc);
5434 channel_shutr_now(oc);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005435
Willy Tarreau0458b082015-08-28 09:40:04 +02005436 ic->analysers = 0;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005437
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02005438 hlua->flags |= HLUA_STOP;
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005439 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005440 return 0;
5441}
5442
5443__LJMP static int hlua_log(lua_State *L)
5444{
5445 int level;
5446 const char *msg;
5447
5448 MAY_LJMP(check_args(L, 2, "log"));
5449 level = MAY_LJMP(luaL_checkinteger(L, 1));
5450 msg = MAY_LJMP(luaL_checkstring(L, 2));
5451
5452 if (level < 0 || level >= NB_LOG_LEVELS)
5453 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5454
5455 hlua_sendlog(NULL, level, msg);
5456 return 0;
5457}
5458
5459__LJMP static int hlua_log_debug(lua_State *L)
5460{
5461 const char *msg;
5462
5463 MAY_LJMP(check_args(L, 1, "debug"));
5464 msg = MAY_LJMP(luaL_checkstring(L, 1));
5465 hlua_sendlog(NULL, LOG_DEBUG, msg);
5466 return 0;
5467}
5468
5469__LJMP static int hlua_log_info(lua_State *L)
5470{
5471 const char *msg;
5472
5473 MAY_LJMP(check_args(L, 1, "info"));
5474 msg = MAY_LJMP(luaL_checkstring(L, 1));
5475 hlua_sendlog(NULL, LOG_INFO, msg);
5476 return 0;
5477}
5478
5479__LJMP static int hlua_log_warning(lua_State *L)
5480{
5481 const char *msg;
5482
5483 MAY_LJMP(check_args(L, 1, "warning"));
5484 msg = MAY_LJMP(luaL_checkstring(L, 1));
5485 hlua_sendlog(NULL, LOG_WARNING, msg);
5486 return 0;
5487}
5488
5489__LJMP static int hlua_log_alert(lua_State *L)
5490{
5491 const char *msg;
5492
5493 MAY_LJMP(check_args(L, 1, "alert"));
5494 msg = MAY_LJMP(luaL_checkstring(L, 1));
5495 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005496 return 0;
5497}
5498
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005499__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005500{
5501 int wakeup_ms = lua_tointeger(L, -1);
5502 if (now_ms < wakeup_ms)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005503 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005504 return 0;
5505}
5506
5507__LJMP static int hlua_sleep(lua_State *L)
5508{
5509 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005510 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005511
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005512 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005513
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005514 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005515 wakeup_ms = tick_add(now_ms, delay);
5516 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005517
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005518 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5519 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005520}
5521
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005522__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005523{
5524 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005525 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005526
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005527 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005528
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005529 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005530 wakeup_ms = tick_add(now_ms, delay);
5531 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005532
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005533 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
5534 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01005535}
5536
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005537/* This functionis an LUA binding. it permits to give back
5538 * the hand at the HAProxy scheduler. It is used when the
5539 * LUA processing consumes a lot of time.
5540 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01005541__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005542{
5543 return 0;
5544}
5545
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005546__LJMP static int hlua_yield(lua_State *L)
5547{
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01005548 WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
5549 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01005550}
5551
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005552/* This function change the nice of the currently executed
5553 * task. It is used set low or high priority at the current
5554 * task.
5555 */
Willy Tarreau59551662015-03-10 14:23:13 +01005556__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005557{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005558 struct hlua *hlua;
5559 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005560
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005561 MAY_LJMP(check_args(L, 1, "set_nice"));
5562 hlua = hlua_gethlua(L);
5563 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005564
5565 /* If he task is not set, I'm in a start mode. */
5566 if (!hlua || !hlua->task)
5567 return 0;
5568
5569 if (nice < -1024)
5570 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005571 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01005572 nice = 1024;
5573
5574 hlua->task->nice = nice;
5575 return 0;
5576}
5577
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005578/* This function is used as a calback of a task. It is called by the
5579 * HAProxy task subsystem when the task is awaked. The LUA runtime can
5580 * return an E_AGAIN signal, the emmiter of this signal must set a
5581 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005582 *
5583 * Task wrapper are longjmp safe because the only one Lua code
5584 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005585 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02005586static struct task *hlua_process_task(struct task *task, void *context, unsigned short state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005587{
Olivier Houchard9f6af332018-05-25 14:04:04 +02005588 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005589 enum hlua_exec status;
5590
Christopher Faulet5bc99722018-04-25 10:34:45 +02005591 if (task->thread_mask == MAX_THREADS_MASK)
5592 task_set_affinity(task, tid_bit);
5593
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005594 /* If it is the first call to the task, we must initialize the
5595 * execution timeouts.
5596 */
5597 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02005598 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005599
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005600 /* Execute the Lua code. */
5601 status = hlua_ctx_resume(hlua, 1);
5602
5603 switch (status) {
5604 /* finished or yield */
5605 case HLUA_E_OK:
5606 hlua_ctx_destroy(hlua);
5607 task_delete(task);
5608 task_free(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02005609 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005610 break;
5611
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01005612 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01005613 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02005614 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005615 break;
5616
5617 /* finished with error. */
5618 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005619 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005620 hlua_ctx_destroy(hlua);
5621 task_delete(task);
5622 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005623 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005624 break;
5625
5626 case HLUA_E_ERR:
5627 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005628 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005629 hlua_ctx_destroy(hlua);
5630 task_delete(task);
5631 task_free(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02005632 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005633 break;
5634 }
Emeric Brun253e53e2017-10-17 18:58:40 +02005635 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005636}
5637
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005638/* This function is an LUA binding that register LUA function to be
5639 * executed after the HAProxy configuration parsing and before the
5640 * HAProxy scheduler starts. This function expect only one LUA
5641 * argument that is a function. This function returns nothing, but
5642 * throws if an error is encountered.
5643 */
5644__LJMP static int hlua_register_init(lua_State *L)
5645{
5646 struct hlua_init_function *init;
5647 int ref;
5648
5649 MAY_LJMP(check_args(L, 1, "register_init"));
5650
5651 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5652
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02005653 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005654 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005655 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01005656
5657 init->function_ref = ref;
5658 LIST_ADDQ(&hlua_init_functions, &init->l);
5659 return 0;
5660}
5661
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005662/* This functio is an LUA binding. It permits to register a task
5663 * executed in parallel of the main HAroxy activity. The task is
5664 * created and it is set in the HAProxy scheduler. It can be called
5665 * from the "init" section, "post init" or during the runtime.
5666 *
5667 * Lua prototype:
5668 *
5669 * <none> core.register_task(<function>)
5670 */
5671static int hlua_register_task(lua_State *L)
5672{
5673 struct hlua *hlua;
5674 struct task *task;
5675 int ref;
5676
5677 MAY_LJMP(check_args(L, 1, "register_task"));
5678
5679 ref = MAY_LJMP(hlua_checkfunction(L, 1));
5680
Willy Tarreaubafbe012017-11-24 17:34:44 +01005681 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005682 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005683 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005684
Emeric Brunc60def82017-09-27 14:59:38 +02005685 task = task_new(MAX_THREADS_MASK);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005686 task->context = hlua;
5687 task->process = hlua_process_task;
5688
5689 if (!hlua_ctx_init(hlua, task))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01005690 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01005691
5692 /* Restore the function in the stack. */
5693 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
5694 hlua->nargs = 0;
5695
5696 /* Schedule task. */
5697 task_schedule(task, now_ms);
5698
5699 return 0;
5700}
5701
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005702/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
5703 * doesn't allow "yield" functions because the HAProxy engine cannot
5704 * resume converters.
5705 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005706static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005707{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005708 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005709 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005710 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005711
Willy Tarreaube508f12016-03-10 11:47:01 +01005712 if (!stream)
5713 return 0;
5714
Willy Tarreau87b09662015-04-03 00:22:06 +02005715 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005716 * Lua context can be not initialized. This behavior
5717 * permits to save performances because a systematic
5718 * Lua initialization cause 5% performances loss.
5719 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005720 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005721 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005722 if (!stream->hlua) {
5723 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5724 return 0;
5725 }
5726 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5727 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
5728 return 0;
5729 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005730 }
5731
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005732 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005733 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005734
5735 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005736 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5737 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5738 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005739 else
5740 error = "critical error";
5741 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005742 return 0;
5743 }
5744
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005745 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005746 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005747 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005748 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005749 return 0;
5750 }
5751
5752 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005753 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005754
5755 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005756 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005757 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005758 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005759 return 0;
5760 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005761 hlua_smp2lua(stream->hlua->T, smp);
5762 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005763
5764 /* push keywords in the stack. */
5765 if (arg_p) {
5766 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005767 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005768 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005769 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005770 return 0;
5771 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005772 hlua_arg2lua(stream->hlua->T, arg_p);
5773 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005774 }
5775 }
5776
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005777 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005778 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005779
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005780 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005781 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005782 }
5783
5784 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005785 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005786 /* finished. */
5787 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005788 /* If the stack is empty, the function fails. */
5789 if (lua_gettop(stream->hlua->T) <= 0)
5790 return 0;
5791
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005792 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005793 hlua_lua2smp(stream->hlua->T, -1, smp);
5794 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005795 return 1;
5796
5797 /* yield. */
5798 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005799 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005800 return 0;
5801
5802 /* finished with error. */
5803 case HLUA_E_ERRMSG:
5804 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005805 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005806 fcn->name, lua_tostring(stream->hlua->T, -1));
5807 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005808 return 0;
5809
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005810 case HLUA_E_ETMOUT:
5811 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
5812 return 0;
5813
5814 case HLUA_E_NOMEM:
5815 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
5816 return 0;
5817
5818 case HLUA_E_YIELD:
5819 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
5820 return 0;
5821
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005822 case HLUA_E_ERR:
5823 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005824 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005825
5826 default:
5827 return 0;
5828 }
5829}
5830
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005831/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
5832 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01005833 * resume sample-fetches. This function will be called by the sample
5834 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005835 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005836static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
5837 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005838{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005839 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005840 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01005841 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005842 const struct chunk msg = { .len = 0 };
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005843
Willy Tarreaube508f12016-03-10 11:47:01 +01005844 if (!stream)
5845 return 0;
5846
Willy Tarreau87b09662015-04-03 00:22:06 +02005847 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005848 * Lua context can be not initialized. This behavior
5849 * permits to save performances because a systematic
5850 * Lua initialization cause 5% performances loss.
5851 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005852 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005853 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005854 if (!stream->hlua) {
5855 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5856 return 0;
5857 }
5858 if (!hlua_ctx_init(stream->hlua, stream->task)) {
5859 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
5860 return 0;
5861 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01005862 }
5863
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005864 consistency_set(stream, smp->opt, &stream->hlua->cons);
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005865
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005866 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005867 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005868
5869 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005870 if (!SET_SAFE_LJMP(stream->hlua->T)) {
5871 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
5872 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01005873 else
5874 error = "critical error";
5875 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005876 return 0;
5877 }
5878
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005879 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005880 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005881 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005882 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005883 return 0;
5884 }
5885
5886 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005887 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005888
5889 /* push arguments in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005890 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR,
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005891 HLUA_TXN_NOTERM)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005892 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005893 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005894 return 0;
5895 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005896 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005897
5898 /* push keywords in the stack. */
5899 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
5900 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005901 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005902 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005903 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005904 return 0;
5905 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005906 hlua_arg2lua(stream->hlua->T, arg_p);
5907 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005908 }
5909
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005910 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005911 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01005912
Thierry FOURNIERbabae282015-09-17 11:36:37 +02005913 /* At this point the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005914 RESET_SAFE_LJMP(stream->hlua->T);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005915 }
5916
5917 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005918 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005919 /* finished. */
5920 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005921 if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005922 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02005923 return 0;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005924 }
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02005925 /* If the stack is empty, the function fails. */
5926 if (lua_gettop(stream->hlua->T) <= 0)
5927 return 0;
5928
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005929 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005930 hlua_lua2smp(stream->hlua->T, -1, smp);
5931 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005932
5933 /* Set the end of execution flag. */
5934 smp->flags &= ~SMP_F_MAY_CHANGE;
5935 return 1;
5936
5937 /* yield. */
5938 case HLUA_E_AGAIN:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005939 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005940 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005941 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005942 return 0;
5943
5944 /* finished with error. */
5945 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005946 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005947 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005948 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005949 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005950 fcn->name, lua_tostring(stream->hlua->T, -1));
5951 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005952 return 0;
5953
Thierry Fournierd5b073c2018-05-21 19:42:47 +02005954 case HLUA_E_ETMOUT:
5955 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
5956 stream_int_retnclose(&stream->si[0], &msg);
5957 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
5958 return 0;
5959
5960 case HLUA_E_NOMEM:
5961 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
5962 stream_int_retnclose(&stream->si[0], &msg);
5963 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
5964 return 0;
5965
5966 case HLUA_E_YIELD:
5967 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
5968 stream_int_retnclose(&stream->si[0], &msg);
5969 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
5970 return 0;
5971
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005972 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01005973 if (!consistency_check(stream, smp->opt, &stream->hlua->cons))
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01005974 stream_int_retnclose(&stream->si[0], &msg);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005975 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02005976 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01005977
5978 default:
5979 return 0;
5980 }
5981}
5982
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01005983/* This function is an LUA binding used for registering
5984 * "sample-conv" functions. It expects a converter name used
5985 * in the haproxy configuration file, and an LUA function.
5986 */
5987__LJMP static int hlua_register_converters(lua_State *L)
5988{
5989 struct sample_conv_kw_list *sck;
5990 const char *name;
5991 int ref;
5992 int len;
5993 struct hlua_function *fcn;
5994
5995 MAY_LJMP(check_args(L, 2, "register_converters"));
5996
5997 /* First argument : converter name. */
5998 name = MAY_LJMP(luaL_checkstring(L, 1));
5999
6000 /* Second argument : lua function. */
6001 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6002
6003 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006004 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006005 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006006 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006007 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006008 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006009 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006010
6011 /* Fill fcn. */
6012 fcn->name = strdup(name);
6013 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006014 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006015 fcn->function_ref = ref;
6016
6017 /* List head */
6018 sck->list.n = sck->list.p = NULL;
6019
6020 /* converter keyword. */
6021 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006022 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006023 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006024 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006025
6026 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6027 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006028 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 +01006029 sck->kw[0].val_args = NULL;
6030 sck->kw[0].in_type = SMP_T_STR;
6031 sck->kw[0].out_type = SMP_T_STR;
6032 sck->kw[0].private = fcn;
6033
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006034 /* Register this new converter */
6035 sample_register_convs(sck);
6036
6037 return 0;
6038}
6039
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006040/* This fucntion is an LUA binding used for registering
6041 * "sample-fetch" functions. It expects a converter name used
6042 * in the haproxy configuration file, and an LUA function.
6043 */
6044__LJMP static int hlua_register_fetches(lua_State *L)
6045{
6046 const char *name;
6047 int ref;
6048 int len;
6049 struct sample_fetch_kw_list *sfk;
6050 struct hlua_function *fcn;
6051
6052 MAY_LJMP(check_args(L, 2, "register_fetches"));
6053
6054 /* First argument : sample-fetch name. */
6055 name = MAY_LJMP(luaL_checkstring(L, 1));
6056
6057 /* Second argument : lua function. */
6058 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6059
6060 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006061 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006062 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006063 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006064 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006065 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006066 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006067
6068 /* Fill fcn. */
6069 fcn->name = strdup(name);
6070 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006071 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006072 fcn->function_ref = ref;
6073
6074 /* List head */
6075 sfk->list.n = sfk->list.p = NULL;
6076
6077 /* sample-fetch keyword. */
6078 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006079 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006080 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006081 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006082
6083 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6084 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006085 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 +01006086 sfk->kw[0].val_args = NULL;
6087 sfk->kw[0].out_type = SMP_T_STR;
6088 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6089 sfk->kw[0].val = 0;
6090 sfk->kw[0].private = fcn;
6091
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006092 /* Register this new fetch. */
6093 sample_register_fetches(sfk);
6094
6095 return 0;
6096}
6097
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006098/* This function is a wrapper to execute each LUA function declared
6099 * as an action wrapper during the initialisation period. This function
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006100 * return ACT_RET_CONT if the processing is finished (with or without
6101 * error) and return ACT_RET_YIELD if the function must be called again
6102 * because the LUA returns a yield.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006103 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006104static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006105 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006106{
6107 char **arg;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006108 unsigned int analyzer;
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006109 int dir;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006110 const char *error;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006111 const struct chunk msg = { .len = 0 };
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006112
6113 switch (rule->from) {
Thierry FOURNIER6e01f382015-11-02 09:52:54 +01006114 case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break;
6115 case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break;
6116 case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break;
6117 case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006118 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006119 SEND_ERR(px, "Lua: internal error while execute action.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006120 return ACT_RET_CONT;
6121 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006122
Willy Tarreau87b09662015-04-03 00:22:06 +02006123 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006124 * Lua context can be not initialized. This behavior
6125 * permits to save performances because a systematic
6126 * Lua initialization cause 5% performances loss.
6127 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006128 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006129 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006130 if (!s->hlua) {
6131 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6132 rule->arg.hlua_rule->fcn.name);
6133 return ACT_RET_CONT;
6134 }
6135 if (!hlua_ctx_init(s->hlua, s->task)) {
6136 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
6137 rule->arg.hlua_rule->fcn.name);
6138 return ACT_RET_CONT;
6139 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006140 }
6141
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006142 consistency_set(s, dir, &s->hlua->cons);
6143
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006144 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006145 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006146
6147 /* The following Lua calls can fail. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006148 if (!SET_SAFE_LJMP(s->hlua->T)) {
6149 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6150 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006151 else
6152 error = "critical error";
6153 SEND_ERR(px, "Lua function '%s': %s.\n",
6154 rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006155 return ACT_RET_CONT;
6156 }
6157
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006158 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006159 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006160 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006161 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006162 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006163 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006164 }
6165
6166 /* Restore the function in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006167 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006168
Willy Tarreau87b09662015-04-03 00:22:06 +02006169 /* Create and and push object stream in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006170 if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006171 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006172 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006173 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006174 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006175 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006176 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006177
6178 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006179 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006180 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006181 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006182 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006183 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006184 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006185 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006186 lua_pushstring(s->hlua->T, *arg);
6187 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006188 }
6189
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006190 /* Now the execution is safe. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006191 RESET_SAFE_LJMP(s->hlua->T);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006192
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006193 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006194 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006195 }
6196
6197 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006198 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006199 /* finished. */
6200 case HLUA_E_OK:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006201 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006202 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006203 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006204 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006205 if (s->hlua->flags & HLUA_STOP)
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +02006206 return ACT_RET_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006207 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006208
6209 /* yield. */
6210 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006211 /* Set timeout in the required channel. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006212 if (s->hlua->wake_time != TICK_ETERNITY) {
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006213 if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006214 s->req.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006215 else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE))
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006216 s->res.analyse_exp = s->hlua->wake_time;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006217 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006218 /* Some actions can be wake up when a "write" event
6219 * is detected on a response channel. This is useful
6220 * only for actions targetted on the requests.
6221 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006222 if (HLUA_IS_WAKERESWR(s->hlua)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006223 s->res.flags |= CF_WAKE_WRITE;
Willy Tarreau76bd97f2015-03-10 17:16:10 +01006224 if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006225 s->res.analysers |= analyzer;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006226 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006227 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006228 s->req.flags |= CF_WAKE_WRITE;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006229 /* We can quit the fcuntion without consistency check
6230 * because HAProxy is not able to manipulate data, it
6231 * is only allowed to call me again. */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006232 return ACT_RET_YIELD;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006233
6234 /* finished with error. */
6235 case HLUA_E_ERRMSG:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006236 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006237 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006238 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006239 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006240 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006241 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006242 rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1));
6243 lua_pop(s->hlua->T, 1);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006244 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006245
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006246 case HLUA_E_ETMOUT:
6247 if (!consistency_check(s, dir, &s->hlua->cons)) {
6248 stream_int_retnclose(&s->si[0], &msg);
6249 return ACT_RET_ERR;
6250 }
6251 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name);
6252 return 0;
6253
6254 case HLUA_E_NOMEM:
6255 if (!consistency_check(s, dir, &s->hlua->cons)) {
6256 stream_int_retnclose(&s->si[0], &msg);
6257 return ACT_RET_ERR;
6258 }
6259 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name);
6260 return 0;
6261
6262 case HLUA_E_YIELD:
6263 if (!consistency_check(s, dir, &s->hlua->cons)) {
6264 stream_int_retnclose(&s->si[0], &msg);
6265 return ACT_RET_ERR;
6266 }
6267 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
6268 rule->arg.hlua_rule->fcn.name);
6269 return 0;
6270
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006271 case HLUA_E_ERR:
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006272 if (!consistency_check(s, dir, &s->hlua->cons)) {
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006273 stream_int_retnclose(&s->si[0], &msg);
Thierry FOURNIERd75cb0f2015-09-25 19:22:44 +02006274 return ACT_RET_ERR;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +01006275 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006276 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006277 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006278 rule->arg.hlua_rule->fcn.name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006279
6280 default:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02006281 return ACT_RET_CONT;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006282 }
6283}
6284
Olivier Houchard9f6af332018-05-25 14:04:04 +02006285struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006286{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006287 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006288 struct stream_interface *si = ctx->owner;
6289
6290 /* If the applet is wake up without any expected work, the sheduler
6291 * remove it from the run queue. This flag indicate that the applet
6292 * is waiting for write. If the buffer is full, the main processing
6293 * will send some data and after call the applet, otherwise it call
6294 * the applet ASAP.
6295 */
6296 si_applet_cant_put(si);
6297 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02006298 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02006299 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006300}
6301
6302static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6303{
6304 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006305 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006306 struct task *task;
6307 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006308 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006309
Willy Tarreaubafbe012017-11-24 17:34:44 +01006310 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006311 if (!hlua) {
6312 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6313 ctx->rule->arg.hlua_rule->fcn.name);
6314 return 0;
6315 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006316 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006317 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006318 ctx->ctx.hlua_apptcp.flags = 0;
6319
6320 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006321 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006322 if (!task) {
6323 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
6324 ctx->rule->arg.hlua_rule->fcn.name);
6325 return 0;
6326 }
6327 task->nice = 0;
6328 task->context = ctx;
6329 task->process = hlua_applet_wakeup;
6330 ctx->ctx.hlua_apptcp.task = task;
6331
6332 /* In the execution wrappers linked with a stream, the
6333 * Lua context can be not initialized. This behavior
6334 * permits to save performances because a systematic
6335 * Lua initialization cause 5% performances loss.
6336 */
6337 if (!hlua_ctx_init(hlua, task)) {
6338 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
6339 ctx->rule->arg.hlua_rule->fcn.name);
6340 return 0;
6341 }
6342
6343 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006344 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006345
6346 /* The following Lua calls can fail. */
6347 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006348 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6349 error = lua_tostring(hlua->T, -1);
6350 else
6351 error = "critical error";
6352 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6353 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006354 RESET_SAFE_LJMP(hlua->T);
6355 return 0;
6356 }
6357
6358 /* Check stack available size. */
6359 if (!lua_checkstack(hlua->T, 1)) {
6360 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6361 ctx->rule->arg.hlua_rule->fcn.name);
6362 RESET_SAFE_LJMP(hlua->T);
6363 return 0;
6364 }
6365
6366 /* Restore the function in the stack. */
6367 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6368
6369 /* Create and and push object stream in the stack. */
6370 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
6371 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6372 ctx->rule->arg.hlua_rule->fcn.name);
6373 RESET_SAFE_LJMP(hlua->T);
6374 return 0;
6375 }
6376 hlua->nargs = 1;
6377
6378 /* push keywords in the stack. */
6379 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6380 if (!lua_checkstack(hlua->T, 1)) {
6381 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
6382 ctx->rule->arg.hlua_rule->fcn.name);
6383 RESET_SAFE_LJMP(hlua->T);
6384 return 0;
6385 }
6386 lua_pushstring(hlua->T, *arg);
6387 hlua->nargs++;
6388 }
6389
6390 RESET_SAFE_LJMP(hlua->T);
6391
6392 /* Wakeup the applet ASAP. */
6393 si_applet_cant_get(si);
6394 si_applet_cant_put(si);
6395
6396 return 1;
6397}
6398
6399static void hlua_applet_tcp_fct(struct appctx *ctx)
6400{
6401 struct stream_interface *si = ctx->owner;
6402 struct stream *strm = si_strm(si);
6403 struct channel *res = si_ic(si);
6404 struct act_rule *rule = ctx->rule;
6405 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006406 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006407
6408 /* The applet execution is already done. */
6409 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE)
6410 return;
6411
6412 /* If the stream is disconnect or closed, ldo nothing. */
6413 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6414 return;
6415
6416 /* Execute the function. */
6417 switch (hlua_ctx_resume(hlua, 1)) {
6418 /* finished. */
6419 case HLUA_E_OK:
6420 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6421
6422 /* log time */
6423 strm->logs.tv_request = now;
6424
6425 /* eat the whole request */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006426 co_skip(si_oc(si), si_ob(si)->o);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006427 res->flags |= CF_READ_NULL;
6428 si_shutr(si);
6429 return;
6430
6431 /* yield. */
6432 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006433 if (hlua->wake_time != TICK_ETERNITY)
6434 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006435 return;
6436
6437 /* finished with error. */
6438 case HLUA_E_ERRMSG:
6439 /* Display log. */
6440 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
6441 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6442 lua_pop(hlua->T, 1);
6443 goto error;
6444
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006445 case HLUA_E_ETMOUT:
6446 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
6447 rule->arg.hlua_rule->fcn.name);
6448 goto error;
6449
6450 case HLUA_E_NOMEM:
6451 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
6452 rule->arg.hlua_rule->fcn.name);
6453 goto error;
6454
6455 case HLUA_E_YIELD: /* unexpected */
6456 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
6457 rule->arg.hlua_rule->fcn.name);
6458 goto error;
6459
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006460 case HLUA_E_ERR:
6461 /* Display log. */
6462 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
6463 rule->arg.hlua_rule->fcn.name);
6464 goto error;
6465
6466 default:
6467 goto error;
6468 }
6469
6470error:
6471
6472 /* For all other cases, just close the stream. */
6473 si_shutw(si);
6474 si_shutr(si);
6475 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
6476}
6477
6478static void hlua_applet_tcp_release(struct appctx *ctx)
6479{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006480 task_delete(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006481 task_free(ctx->ctx.hlua_apptcp.task);
6482 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006483 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006484 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006485}
6486
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006487/* The function returns 1 if the initialisation is complete, 0 if
6488 * an errors occurs and -1 if more data are required for initializing
6489 * the applet.
6490 */
6491static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
6492{
6493 struct stream_interface *si = ctx->owner;
6494 struct channel *req = si_oc(si);
6495 struct http_msg *msg;
6496 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006497 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006498 char **arg;
6499 struct hdr_ctx hdr;
6500 struct task *task;
6501 struct sample smp; /* just used for a valid call to smp_prefetch_http. */
Thierry Fournierfd107a22016-02-19 19:57:23 +01006502 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006503
6504 /* Wait for a full HTTP request. */
6505 if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) {
6506 if (smp.flags & SMP_F_MAY_CHANGE)
6507 return -1;
6508 return 0;
6509 }
6510 txn = strm->txn;
6511 msg = &txn->req;
6512
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006513 /* We want two things in HTTP mode :
6514 * - enforce server-close mode if we were in keep-alive, so that the
6515 * applet is released after each response ;
6516 * - enable request body transfer to the applet in order to resync
6517 * with the response body.
6518 */
6519 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)
6520 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreau0078bfc2015-10-07 20:20:28 +02006521
Willy Tarreaubafbe012017-11-24 17:34:44 +01006522 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006523 if (!hlua) {
6524 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6525 ctx->rule->arg.hlua_rule->fcn.name);
6526 return 0;
6527 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006528 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006529 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006530 ctx->ctx.hlua_apphttp.left_bytes = -1;
6531 ctx->ctx.hlua_apphttp.flags = 0;
6532
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01006533 if (txn->req.flags & HTTP_MSGF_VER_11)
6534 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
6535
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006536 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01006537 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006538 if (!task) {
6539 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
6540 ctx->rule->arg.hlua_rule->fcn.name);
6541 return 0;
6542 }
6543 task->nice = 0;
6544 task->context = ctx;
6545 task->process = hlua_applet_wakeup;
6546 ctx->ctx.hlua_apphttp.task = task;
6547
6548 /* In the execution wrappers linked with a stream, the
6549 * Lua context can be not initialized. This behavior
6550 * permits to save performances because a systematic
6551 * Lua initialization cause 5% performances loss.
6552 */
6553 if (!hlua_ctx_init(hlua, task)) {
6554 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
6555 ctx->rule->arg.hlua_rule->fcn.name);
6556 return 0;
6557 }
6558
6559 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006560 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006561
6562 /* The following Lua calls can fail. */
6563 if (!SET_SAFE_LJMP(hlua->T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01006564 if (lua_type(hlua->T, -1) == LUA_TSTRING)
6565 error = lua_tostring(hlua->T, -1);
6566 else
6567 error = "critical error";
6568 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6569 ctx->rule->arg.hlua_rule->fcn.name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006570 return 0;
6571 }
6572
6573 /* Check stack available size. */
6574 if (!lua_checkstack(hlua->T, 1)) {
6575 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6576 ctx->rule->arg.hlua_rule->fcn.name);
6577 RESET_SAFE_LJMP(hlua->T);
6578 return 0;
6579 }
6580
6581 /* Restore the function in the stack. */
6582 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref);
6583
6584 /* Create and and push object stream in the stack. */
6585 if (!hlua_applet_http_new(hlua->T, ctx)) {
6586 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6587 ctx->rule->arg.hlua_rule->fcn.name);
6588 RESET_SAFE_LJMP(hlua->T);
6589 return 0;
6590 }
6591 hlua->nargs = 1;
6592
6593 /* Look for a 100-continue expected. */
6594 if (msg->flags & HTTP_MSGF_VER_11) {
6595 hdr.idx = 0;
6596 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) &&
6597 unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0))
6598 ctx->ctx.hlua_apphttp.flags |= APPLET_100C;
6599 }
6600
6601 /* push keywords in the stack. */
6602 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
6603 if (!lua_checkstack(hlua->T, 1)) {
6604 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
6605 ctx->rule->arg.hlua_rule->fcn.name);
6606 RESET_SAFE_LJMP(hlua->T);
6607 return 0;
6608 }
6609 lua_pushstring(hlua->T, *arg);
6610 hlua->nargs++;
6611 }
6612
6613 RESET_SAFE_LJMP(hlua->T);
6614
6615 /* Wakeup the applet when data is ready for read. */
6616 si_applet_cant_get(si);
6617
6618 return 1;
6619}
6620
6621static void hlua_applet_http_fct(struct appctx *ctx)
6622{
6623 struct stream_interface *si = ctx->owner;
6624 struct stream *strm = si_strm(si);
6625 struct channel *res = si_ic(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006626 struct act_rule *rule = ctx->rule;
6627 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006628 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006629 char *blk1;
6630 int len1;
6631 char *blk2;
6632 int len2;
6633 int ret;
6634
6635 /* If the stream is disconnect or closed, ldo nothing. */
6636 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
6637 return;
6638
6639 /* Set the currently running flag. */
6640 if (!HLUA_IS_RUNNING(hlua) &&
6641 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6642
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006643 /* Wait for full HTTP analysys. */
6644 if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) {
6645 si_applet_cant_get(si);
6646 return;
6647 }
6648
6649 /* Store the max amount of bytes that we can read. */
6650 ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len;
6651
6652 /* We need to flush the request header. This left the body
6653 * for the Lua.
6654 */
6655
6656 /* Read the maximum amount of data avalaible. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006657 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006658 if (ret == -1)
6659 return;
6660
6661 /* No data available, ask for more data. */
6662 if (ret == 1)
6663 len2 = 0;
6664 if (ret == 0)
6665 len1 = 0;
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006666 if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006667 si_applet_cant_get(si);
6668 return;
6669 }
6670
6671 /* skip the requests bytes. */
Thierry FOURNIER70d318c2018-06-30 10:37:33 +02006672 co_skip(si_oc(si), strm->txn->req.eoh + strm->txn->req.eol);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006673 }
6674
6675 /* Executes The applet if it is not done. */
6676 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
6677
6678 /* Execute the function. */
6679 switch (hlua_ctx_resume(hlua, 1)) {
6680 /* finished. */
6681 case HLUA_E_OK:
6682 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6683 break;
6684
6685 /* yield. */
6686 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01006687 if (hlua->wake_time != TICK_ETERNITY)
6688 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006689 return;
6690
6691 /* finished with error. */
6692 case HLUA_E_ERRMSG:
6693 /* Display log. */
6694 SEND_ERR(px, "Lua applet http '%s': %s.\n",
6695 rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1));
6696 lua_pop(hlua->T, 1);
6697 goto error;
6698
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006699 case HLUA_E_ETMOUT:
6700 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
6701 rule->arg.hlua_rule->fcn.name);
6702 goto error;
6703
6704 case HLUA_E_NOMEM:
6705 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
6706 rule->arg.hlua_rule->fcn.name);
6707 goto error;
6708
6709 case HLUA_E_YIELD: /* unexpected */
6710 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
6711 rule->arg.hlua_rule->fcn.name);
6712 goto error;
6713
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006714 case HLUA_E_ERR:
6715 /* Display log. */
6716 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
6717 rule->arg.hlua_rule->fcn.name);
6718 goto error;
6719
6720 default:
6721 goto error;
6722 }
6723 }
6724
6725 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
6726
6727 /* We must send the final chunk. */
6728 if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED &&
6729 !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) {
6730
6731 /* sent last chunk at once. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006732 ret = ci_putblk(res, "0\r\n\r\n", 5);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006733
6734 /* critical error. */
6735 if (ret == -2 || ret == -3) {
6736 SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n",
6737 rule->arg.hlua_rule->fcn.name);
6738 goto error;
6739 }
6740
6741 /* no enough space error. */
6742 if (ret == -1) {
6743 si_applet_cant_put(si);
6744 return;
6745 }
6746
6747 /* set the last chunk sent. */
6748 ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK;
6749 }
6750
6751 /* close the connection. */
6752
6753 /* status / log */
6754 strm->txn->status = ctx->ctx.hlua_apphttp.status;
6755 strm->logs.tv_request = now;
6756
6757 /* eat the whole request */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006758 co_skip(si_oc(si), si_ob(si)->o);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006759 res->flags |= CF_READ_NULL;
6760 si_shutr(si);
6761
6762 return;
6763 }
6764
6765error:
6766
6767 /* If we are in HTTP mode, and we are not send any
6768 * data, return a 500 server error in best effort:
6769 * if there are no room avalaible in the buffer,
6770 * just close the connection.
6771 */
Willy Tarreau06d80a92017-10-19 14:32:15 +02006772 ci_putblk(res, error_500, strlen(error_500));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006773 if (!(strm->flags & SF_ERR_MASK))
6774 strm->flags |= SF_ERR_RESOURCE;
6775 si_shutw(si);
6776 si_shutr(si);
6777 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
6778}
6779
6780static void hlua_applet_http_release(struct appctx *ctx)
6781{
Willy Tarreaubd7fc952017-07-24 17:35:27 +02006782 task_delete(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006783 task_free(ctx->ctx.hlua_apphttp.task);
6784 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006785 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01006786 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006787}
6788
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006789/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
6790 * succes case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006791 *
6792 * This function can fail with an abort() due to an Lua critical error.
6793 * We are in the configuration parsing process of HAProxy, this abort() is
6794 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006795 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006796static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
6797 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006798{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006799 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006800 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006801
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006802 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006803 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006804 if (!rule->arg.hlua_rule) {
6805 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006806 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006807 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006808
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006809 /* Memory for arguments. */
6810 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *));
6811 if (!rule->arg.hlua_rule->args) {
6812 memprintf(err, "out of memory error");
6813 return ACT_RET_PRS_ERR;
6814 }
6815
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006816 /* Reference the Lua function and store the reference. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006817 rule->arg.hlua_rule->fcn = *fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006818
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006819 /* Expect some arguments */
6820 for (i = 0; i < fcn->nargs; i++) {
6821 if (*args[i+1] == '\0') {
6822 memprintf(err, "expect %d arguments", fcn->nargs);
6823 return ACT_RET_PRS_ERR;
6824 }
6825 rule->arg.hlua_rule->args[i] = strdup(args[i + 1]);
6826 if (!rule->arg.hlua_rule->args[i]) {
6827 memprintf(err, "out of memory error");
6828 return ACT_RET_PRS_ERR;
6829 }
6830 (*cur_arg)++;
6831 }
6832 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006833
Thierry FOURNIER42148732015-09-02 17:17:33 +02006834 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006835 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02006836 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006837}
6838
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006839static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
6840 struct act_rule *rule, char **err)
6841{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006842 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006843
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01006844 /* HTTP applets are forbidden in tcp-request rules.
6845 * HTTP applet request requires everything initilized by
6846 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
6847 * The applet will be immediately initilized, but its before
6848 * the call of this analyzer.
6849 */
6850 if (rule->from != ACT_F_HTTP_REQ) {
6851 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
6852 return ACT_RET_PRS_ERR;
6853 }
6854
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02006855 /* Memory for the rule. */
6856 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6857 if (!rule->arg.hlua_rule) {
6858 memprintf(err, "out of memory error");
6859 return ACT_RET_PRS_ERR;
6860 }
6861
6862 /* Reference the Lua function and store the reference. */
6863 rule->arg.hlua_rule->fcn = *fcn;
6864
6865 /* TODO: later accept arguments. */
6866 rule->arg.hlua_rule->args = NULL;
6867
6868 /* Add applet pointer in the rule. */
6869 rule->applet.obj_type = OBJ_TYPE_APPLET;
6870 rule->applet.name = fcn->name;
6871 rule->applet.init = hlua_applet_http_init;
6872 rule->applet.fct = hlua_applet_http_fct;
6873 rule->applet.release = hlua_applet_http_release;
6874 rule->applet.timeout = hlua_timeout_applet;
6875
6876 return ACT_RET_PRS_OK;
6877}
6878
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006879/* This function is an LUA binding used for registering
6880 * "sample-conv" functions. It expects a converter name used
6881 * in the haproxy configuration file, and an LUA function.
6882 */
6883__LJMP static int hlua_register_action(lua_State *L)
6884{
6885 struct action_kw_list *akl;
6886 const char *name;
6887 int ref;
6888 int len;
6889 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006890 int nargs;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006891
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006892 /* Initialise the number of expected arguments at 0. */
6893 nargs = 0;
6894
6895 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
6896 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006897
6898 /* First argument : converter name. */
6899 name = MAY_LJMP(luaL_checkstring(L, 1));
6900
6901 /* Second argument : environment. */
6902 if (lua_type(L, 2) != LUA_TTABLE)
6903 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6904
6905 /* Third argument : lua function. */
6906 ref = MAY_LJMP(hlua_checkfunction(L, 3));
6907
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006908 /* Fouth argument : number of mandatories arguments expected on the configuration line. */
6909 if (lua_gettop(L) >= 4)
6910 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
6911
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006912 /* browse the second argulent as an array. */
6913 lua_pushnil(L);
6914 while (lua_next(L, 2) != 0) {
6915 if (lua_type(L, -1) != LUA_TSTRING)
6916 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
6917
6918 /* Check required environment. Only accepted "http" or "tcp". */
6919 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006920 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006921 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006922 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006923 fcn = calloc(1, sizeof(*fcn));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006924 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006925 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006926
6927 /* Fill fcn. */
6928 fcn->name = strdup(name);
6929 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006930 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006931 fcn->function_ref = ref;
6932
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01006933 /* Set the expected number od arguments. */
6934 fcn->nargs = nargs;
6935
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006936 /* List head */
6937 akl->list.n = akl->list.p = NULL;
6938
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006939 /* action keyword. */
6940 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006941 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006942 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006943 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006944
6945 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
6946
6947 akl->kw[0].match_pfx = 0;
6948 akl->kw[0].private = fcn;
6949 akl->kw[0].parse = action_register_lua;
6950
6951 /* select the action registering point. */
6952 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
6953 tcp_req_cont_keywords_register(akl);
6954 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
6955 tcp_res_cont_keywords_register(akl);
6956 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
6957 http_req_keywords_register(akl);
6958 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
6959 http_res_keywords_register(akl);
6960 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006961 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006962 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
6963 "are expected.", lua_tostring(L, -1)));
6964
6965 /* pop the environment string. */
6966 lua_pop(L, 1);
6967 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006968 return ACT_RET_PRS_OK;
6969}
6970
6971static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
6972 struct act_rule *rule, char **err)
6973{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006974 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006975
6976 /* Memory for the rule. */
6977 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
6978 if (!rule->arg.hlua_rule) {
6979 memprintf(err, "out of memory error");
6980 return ACT_RET_PRS_ERR;
6981 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02006982
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02006983 /* Reference the Lua function and store the reference. */
6984 rule->arg.hlua_rule->fcn = *fcn;
6985
6986 /* TODO: later accept arguments. */
6987 rule->arg.hlua_rule->args = NULL;
6988
6989 /* Add applet pointer in the rule. */
6990 rule->applet.obj_type = OBJ_TYPE_APPLET;
6991 rule->applet.name = fcn->name;
6992 rule->applet.init = hlua_applet_tcp_init;
6993 rule->applet.fct = hlua_applet_tcp_fct;
6994 rule->applet.release = hlua_applet_tcp_release;
6995 rule->applet.timeout = hlua_timeout_applet;
6996
6997 return 0;
6998}
6999
7000/* This function is an LUA binding used for registering
7001 * "sample-conv" functions. It expects a converter name used
7002 * in the haproxy configuration file, and an LUA function.
7003 */
7004__LJMP static int hlua_register_service(lua_State *L)
7005{
7006 struct action_kw_list *akl;
7007 const char *name;
7008 const char *env;
7009 int ref;
7010 int len;
7011 struct hlua_function *fcn;
7012
7013 MAY_LJMP(check_args(L, 3, "register_service"));
7014
7015 /* First argument : converter name. */
7016 name = MAY_LJMP(luaL_checkstring(L, 1));
7017
7018 /* Second argument : environment. */
7019 env = MAY_LJMP(luaL_checkstring(L, 2));
7020
7021 /* Third argument : lua function. */
7022 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7023
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007024 /* Allocate and fill the sample fetch keyword struct. */
7025 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7026 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007027 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007028 fcn = calloc(1, sizeof(*fcn));
7029 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007030 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007031
7032 /* Fill fcn. */
7033 len = strlen("<lua.>") + strlen(name) + 1;
7034 fcn->name = calloc(1, len);
7035 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007036 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007037 snprintf((char *)fcn->name, len, "<lua.%s>", name);
7038 fcn->function_ref = ref;
7039
7040 /* List head */
7041 akl->list.n = akl->list.p = NULL;
7042
7043 /* converter keyword. */
7044 len = strlen("lua.") + strlen(name) + 1;
7045 akl->kw[0].kw = calloc(1, len);
7046 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007047 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007048
7049 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7050
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007051 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007052 if (strcmp(env, "tcp") == 0)
7053 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007054 else if (strcmp(env, "http") == 0)
7055 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007056 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007057 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007058 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007059
7060 akl->kw[0].match_pfx = 0;
7061 akl->kw[0].private = fcn;
7062
7063 /* End of array. */
7064 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7065
7066 /* Register this new converter */
7067 service_keywords_register(akl);
7068
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007069 return 0;
7070}
7071
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007072/* This function initialises Lua cli handler. It copies the
7073 * arguments in the Lua stack and create channel IO objects.
7074 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007075static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007076{
7077 struct hlua *hlua;
7078 struct hlua_function *fcn;
7079 int i;
7080 const char *error;
7081
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007082 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007083 appctx->ctx.hlua_cli.fcn = private;
7084
Willy Tarreaubafbe012017-11-24 17:34:44 +01007085 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007086 if (!hlua) {
7087 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007088 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007089 }
7090 HLUA_INIT(hlua);
7091 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007092
7093 /* Create task used by signal to wakeup applets.
7094 * We use the same wakeup fonction than the Lua applet_tcp and
7095 * applet_http. It is absolutely compatible.
7096 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007097 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007098 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007099 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007100 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007101 }
7102 appctx->ctx.hlua_cli.task->nice = 0;
7103 appctx->ctx.hlua_cli.task->context = appctx;
7104 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7105
7106 /* Initialises the Lua context */
7107 if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) {
7108 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007109 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007110 }
7111
7112 /* The following Lua calls can fail. */
7113 if (!SET_SAFE_LJMP(hlua->T)) {
7114 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7115 error = lua_tostring(hlua->T, -1);
7116 else
7117 error = "critical error";
7118 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7119 goto error;
7120 }
7121
7122 /* Check stack available size. */
7123 if (!lua_checkstack(hlua->T, 2)) {
7124 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7125 goto error;
7126 }
7127
7128 /* Restore the function in the stack. */
7129 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref);
7130
7131 /* Once the arguments parsed, the CLI is like an AppletTCP,
7132 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007133 */
7134 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7135 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7136 goto error;
7137 }
7138 hlua->nargs = 1;
7139
7140 /* push keywords in the stack. */
7141 for (i = 0; *args[i]; i++) {
7142 /* Check stack available size. */
7143 if (!lua_checkstack(hlua->T, 1)) {
7144 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7145 goto error;
7146 }
7147 lua_pushstring(hlua->T, args[i]);
7148 hlua->nargs++;
7149 }
7150
7151 /* We must initialize the execution timeouts. */
7152 hlua->max_time = hlua_timeout_session;
7153
7154 /* At this point the execution is safe. */
7155 RESET_SAFE_LJMP(hlua->T);
7156
7157 /* It's ok */
7158 return 0;
7159
7160 /* It's not ok. */
7161error:
7162 RESET_SAFE_LJMP(hlua->T);
7163 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007164 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007165 return 1;
7166}
7167
7168static int hlua_cli_io_handler_fct(struct appctx *appctx)
7169{
7170 struct hlua *hlua;
7171 struct stream_interface *si;
7172 struct hlua_function *fcn;
7173
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007174 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007175 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007176 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007177
7178 /* If the stream is disconnect or closed, ldo nothing. */
7179 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7180 return 1;
7181
7182 /* Execute the function. */
7183 switch (hlua_ctx_resume(hlua, 1)) {
7184
7185 /* finished. */
7186 case HLUA_E_OK:
7187 return 1;
7188
7189 /* yield. */
7190 case HLUA_E_AGAIN:
7191 /* We want write. */
7192 if (HLUA_IS_WAKERESWR(hlua))
7193 si_applet_cant_put(si);
7194 /* Set the timeout. */
7195 if (hlua->wake_time != TICK_ETERNITY)
7196 task_schedule(hlua->task, hlua->wake_time);
7197 return 0;
7198
7199 /* finished with error. */
7200 case HLUA_E_ERRMSG:
7201 /* Display log. */
7202 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7203 fcn->name, lua_tostring(hlua->T, -1));
7204 lua_pop(hlua->T, 1);
7205 return 1;
7206
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007207 case HLUA_E_ETMOUT:
7208 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7209 fcn->name);
7210 return 1;
7211
7212 case HLUA_E_NOMEM:
7213 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7214 fcn->name);
7215 return 1;
7216
7217 case HLUA_E_YIELD: /* unexpected */
7218 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
7219 fcn->name);
7220 return 1;
7221
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007222 case HLUA_E_ERR:
7223 /* Display log. */
7224 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
7225 fcn->name);
7226 return 1;
7227
7228 default:
7229 return 1;
7230 }
7231
7232 return 1;
7233}
7234
7235static void hlua_cli_io_release_fct(struct appctx *appctx)
7236{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007237 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007238 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007239}
7240
7241/* This function is an LUA binding used for registering
7242 * new keywords in the cli. It expects a list of keywords
7243 * which are the "path". It is limited to 5 keywords. A
7244 * description of the command, a function to be executed
7245 * for the parsing and a function for io handlers.
7246 */
7247__LJMP static int hlua_register_cli(lua_State *L)
7248{
7249 struct cli_kw_list *cli_kws;
7250 const char *message;
7251 int ref_io;
7252 int len;
7253 struct hlua_function *fcn;
7254 int index;
7255 int i;
7256
7257 MAY_LJMP(check_args(L, 3, "register_cli"));
7258
7259 /* First argument : an array of maximum 5 keywords. */
7260 if (!lua_istable(L, 1))
7261 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
7262
7263 /* Second argument : string with contextual message. */
7264 message = MAY_LJMP(luaL_checkstring(L, 2));
7265
7266 /* Third and fourth argument : lua function. */
7267 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
7268
7269 /* Allocate and fill the sample fetch keyword struct. */
7270 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
7271 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007272 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007273 fcn = calloc(1, sizeof(*fcn));
7274 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007275 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007276
7277 /* Fill path. */
7278 index = 0;
7279 lua_pushnil(L);
7280 while(lua_next(L, 1) != 0) {
7281 if (index >= 5)
7282 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
7283 if (lua_type(L, -1) != LUA_TSTRING)
7284 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
7285 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
7286 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007287 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007288 index++;
7289 lua_pop(L, 1);
7290 }
7291
7292 /* Copy help message. */
7293 cli_kws->kw[0].usage = strdup(message);
7294 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007295 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007296
7297 /* Fill fcn io handler. */
7298 len = strlen("<lua.cli>") + 1;
7299 for (i = 0; i < index; i++)
7300 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
7301 fcn->name = calloc(1, len);
7302 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007303 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007304 strncat((char *)fcn->name, "<lua.cli", len);
7305 for (i = 0; i < index; i++) {
7306 strncat((char *)fcn->name, ".", len);
7307 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
7308 }
7309 strncat((char *)fcn->name, ">", len);
7310 fcn->function_ref = ref_io;
7311
7312 /* Fill last entries. */
7313 cli_kws->kw[0].private = fcn;
7314 cli_kws->kw[0].parse = hlua_cli_parse_fct;
7315 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
7316 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
7317
7318 /* Register this new converter */
7319 cli_register_kw(cli_kws);
7320
7321 return 0;
7322}
7323
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007324static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
7325 struct proxy *defpx, const char *file, int line,
7326 char **err, unsigned int *timeout)
7327{
7328 const char *error;
7329
7330 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
7331 if (error && *error != '\0') {
7332 memprintf(err, "%s: invalid timeout", args[0]);
7333 return -1;
7334 }
7335 return 0;
7336}
7337
7338static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
7339 struct proxy *defpx, const char *file, int line,
7340 char **err)
7341{
7342 return hlua_read_timeout(args, section_type, curpx, defpx,
7343 file, line, err, &hlua_timeout_session);
7344}
7345
7346static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
7347 struct proxy *defpx, const char *file, int line,
7348 char **err)
7349{
7350 return hlua_read_timeout(args, section_type, curpx, defpx,
7351 file, line, err, &hlua_timeout_task);
7352}
7353
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007354static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
7355 struct proxy *defpx, const char *file, int line,
7356 char **err)
7357{
7358 return hlua_read_timeout(args, section_type, curpx, defpx,
7359 file, line, err, &hlua_timeout_applet);
7360}
7361
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007362static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
7363 struct proxy *defpx, const char *file, int line,
7364 char **err)
7365{
7366 char *error;
7367
7368 hlua_nb_instruction = strtoll(args[1], &error, 10);
7369 if (*error != '\0') {
7370 memprintf(err, "%s: invalid number", args[0]);
7371 return -1;
7372 }
7373 return 0;
7374}
7375
Willy Tarreau32f61e22015-03-18 17:54:59 +01007376static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
7377 struct proxy *defpx, const char *file, int line,
7378 char **err)
7379{
7380 char *error;
7381
7382 if (*(args[1]) == 0) {
7383 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
7384 return -1;
7385 }
7386 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
7387 if (*error != '\0') {
7388 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
7389 return -1;
7390 }
7391 return 0;
7392}
7393
7394
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007395/* This function is called by the main configuration key "lua-load". It loads and
7396 * execute an lua file during the parsing of the HAProxy configuration file. It is
7397 * the main lua entry point.
7398 *
7399 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
7400 * occured, otherwise it returns 0.
7401 *
7402 * In some error case, LUA set an error message in top of the stack. This function
7403 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007404 *
7405 * This function can fail with an abort() due to an Lua critical error.
7406 * We are in the configuration parsing process of HAProxy, this abort() is
7407 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007408 */
7409static int hlua_load(char **args, int section_type, struct proxy *curpx,
7410 struct proxy *defpx, const char *file, int line,
7411 char **err)
7412{
7413 int error;
7414
7415 /* Just load and compile the file. */
7416 error = luaL_loadfile(gL.T, args[1]);
7417 if (error) {
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007418 memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007419 lua_pop(gL.T, 1);
7420 return -1;
7421 }
7422
7423 /* If no syntax error where detected, execute the code. */
7424 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
7425 switch (error) {
7426 case LUA_OK:
7427 break;
7428 case LUA_ERRRUN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007429 memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007430 lua_pop(gL.T, 1);
7431 return -1;
7432 case LUA_ERRMEM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007433 memprintf(err, "Lua out of memory error.n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007434 return -1;
7435 case LUA_ERRERR:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007436 memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007437 lua_pop(gL.T, 1);
7438 return -1;
7439 case LUA_ERRGCMM:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007440 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007441 lua_pop(gL.T, 1);
7442 return -1;
7443 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007444 memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007445 lua_pop(gL.T, 1);
7446 return -1;
7447 }
7448
7449 return 0;
7450}
7451
7452/* configuration keywords declaration */
7453static struct cfg_kw_list cfg_kws = {{ },{
Thierry FOURNIERbd413492015-03-03 16:52:26 +01007454 { CFG_GLOBAL, "lua-load", hlua_load },
7455 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
7456 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02007457 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01007458 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01007459 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007460 { 0, NULL, NULL },
7461}};
7462
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007463/* This function can fail with an abort() due to an Lua critical error.
7464 * We are in the initialisation process of HAProxy, this abort() is
7465 * tolerated.
7466 */
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007467int hlua_post_init()
7468{
7469 struct hlua_init_function *init;
7470 const char *msg;
7471 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007472 const char *error;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007473
Thierry Fournier3d4a6752016-02-19 20:53:30 +01007474 /* Call post initialisation function in safe environement. */
7475 if (!SET_SAFE_LJMP(gL.T)) {
7476 if (lua_type(gL.T, -1) == LUA_TSTRING)
7477 error = lua_tostring(gL.T, -1);
7478 else
7479 error = "critical error";
7480 fprintf(stderr, "Lua post-init: %s.\n", error);
7481 exit(1);
7482 }
7483 hlua_fcn_post_init(gL.T);
7484 RESET_SAFE_LJMP(gL.T);
7485
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007486 list_for_each_entry(init, &hlua_init_functions, l) {
7487 lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref);
7488 ret = hlua_ctx_resume(&gL, 0);
7489 switch (ret) {
7490 case HLUA_E_OK:
7491 lua_pop(gL.T, -1);
7492 return 1;
7493 case HLUA_E_AGAIN:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007494 ha_alert("Lua init: yield not allowed.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007495 return 0;
7496 case HLUA_E_ERRMSG:
7497 msg = lua_tostring(gL.T, -1);
Christopher Faulet767a84b2017-11-24 16:50:31 +01007498 ha_alert("lua init: %s.\n", msg);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007499 return 0;
7500 case HLUA_E_ERR:
7501 default:
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007502 ha_alert("Lua init: unknown runtime error.\n");
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007503 return 0;
7504 }
7505 }
7506 return 1;
7507}
7508
Willy Tarreau32f61e22015-03-18 17:54:59 +01007509/* The memory allocator used by the Lua stack. <ud> is a pointer to the
7510 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
7511 * is the previously allocated size or the kind of object in case of a new
7512 * allocation. <nsize> is the requested new size.
7513 */
7514static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
7515{
7516 struct hlua_mem_allocator *zone = ud;
7517
7518 if (nsize == 0) {
7519 /* it's a free */
7520 if (ptr)
7521 zone->allocated -= osize;
7522 free(ptr);
7523 return NULL;
7524 }
7525
7526 if (!ptr) {
7527 /* it's a new allocation */
7528 if (zone->limit && zone->allocated + nsize > zone->limit)
7529 return NULL;
7530
7531 ptr = malloc(nsize);
7532 if (ptr)
7533 zone->allocated += nsize;
7534 return ptr;
7535 }
7536
7537 /* it's a realloc */
7538 if (zone->limit && zone->allocated + nsize - osize > zone->limit)
7539 return NULL;
7540
7541 ptr = realloc(ptr, nsize);
7542 if (ptr)
7543 zone->allocated += nsize - osize;
7544 return ptr;
7545}
7546
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007547/* Ithis function can fail with an abort() due to an Lua critical error.
7548 * We are in the initialisation process of HAProxy, this abort() is
7549 * tolerated.
7550 */
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01007551void hlua_init(void)
7552{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007553 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007554 int idx;
7555 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007556 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007557 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007558 const char *error_msg;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007559#ifdef USE_OPENSSL
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007560 struct srv_kw *kw;
7561 int tmp_error;
7562 char *error;
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007563 char *args[] = { /* SSL client configuration. */
7564 "ssl",
7565 "verify",
7566 "none",
Thierry FOURNIER36d13742015-03-17 16:48:53 +01007567 NULL
7568 };
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007569#endif
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007570
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007571 HA_SPIN_INIT(&hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02007572
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007573 /* Initialise struct hlua and com signals pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007574 pool_head_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007575
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01007576 /* Register configuration keywords. */
7577 cfg_register_keywords(&cfg_kws);
7578
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007579 /* Init main lua stack. */
7580 gL.Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01007581 gL.flags = 0;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01007582 LIST_INIT(&gL.com);
Willy Tarreau42ef75f2017-04-12 21:40:29 +02007583 gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007584 hlua_sethlua(&gL);
7585 gL.Tref = LUA_REFNIL;
7586 gL.task = NULL;
7587
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007588 /* From this point, until the end of the initialisation fucntion,
7589 * the Lua function can fail with an abort. We are in the initialisation
7590 * process of HAProxy, this abort() is tolerated.
7591 */
7592
Thierry FOURNIER380d0932015-01-23 14:27:52 +01007593 /* Initialise lua. */
7594 luaL_openlibs(gL.T);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007595
Thierry Fournier75933d42016-01-21 09:30:18 +01007596 /* Set safe environment for the initialisation. */
7597 if (!SET_SAFE_LJMP(gL.T)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007598 if (lua_type(gL.T, -1) == LUA_TSTRING)
7599 error_msg = lua_tostring(gL.T, -1);
7600 else
7601 error_msg = "critical error";
7602 fprintf(stderr, "Lua init: %s.\n", error_msg);
Thierry Fournier75933d42016-01-21 09:30:18 +01007603 exit(1);
7604 }
7605
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007606 /*
7607 *
7608 * Create "core" object.
7609 *
7610 */
7611
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01007612 /* This table entry is the object "core" base. */
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007613 lua_newtable(gL.T);
7614
7615 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01007616 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007617 hlua_class_const_int(gL.T, log_levels[i], i);
7618
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007619 /* Register special functions. */
7620 hlua_class_function(gL.T, "register_init", hlua_register_init);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01007621 hlua_class_function(gL.T, "register_task", hlua_register_task);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01007622 hlua_class_function(gL.T, "register_fetches", hlua_register_fetches);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01007623 hlua_class_function(gL.T, "register_converters", hlua_register_converters);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007624 hlua_class_function(gL.T, "register_action", hlua_register_action);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007625 hlua_class_function(gL.T, "register_service", hlua_register_service);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007626 hlua_class_function(gL.T, "register_cli", hlua_register_cli);
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01007627 hlua_class_function(gL.T, "yield", hlua_yield);
Willy Tarreau59551662015-03-10 14:23:13 +01007628 hlua_class_function(gL.T, "set_nice", hlua_set_nice);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01007629 hlua_class_function(gL.T, "sleep", hlua_sleep);
7630 hlua_class_function(gL.T, "msleep", hlua_msleep);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01007631 hlua_class_function(gL.T, "add_acl", hlua_add_acl);
7632 hlua_class_function(gL.T, "del_acl", hlua_del_acl);
7633 hlua_class_function(gL.T, "set_map", hlua_set_map);
7634 hlua_class_function(gL.T, "del_map", hlua_del_map);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007635 hlua_class_function(gL.T, "tcp", hlua_socket_new);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007636 hlua_class_function(gL.T, "log", hlua_log);
7637 hlua_class_function(gL.T, "Debug", hlua_log_debug);
7638 hlua_class_function(gL.T, "Info", hlua_log_info);
7639 hlua_class_function(gL.T, "Warning", hlua_log_warning);
7640 hlua_class_function(gL.T, "Alert", hlua_log_alert);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02007641 hlua_class_function(gL.T, "done", hlua_done);
Thierry Fournierfb0b5462016-01-21 09:28:58 +01007642 hlua_fcn_reg_core_fcn(gL.T);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01007643
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01007644 lua_setglobal(gL.T, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007645
7646 /*
7647 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007648 * Register class Map
7649 *
7650 */
7651
7652 /* This table entry is the object "Map" base. */
7653 lua_newtable(gL.T);
7654
7655 /* register pattern types. */
7656 for (i=0; i<PAT_MATCH_NUM; i++)
7657 hlua_class_const_int(gL.T, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01007658 for (i=0; i<PAT_MATCH_NUM; i++) {
7659 snprintf(trash.str, trash.size, "_%s", pat_match_names[i]);
7660 hlua_class_const_int(gL.T, trash.str, i);
7661 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007662
7663 /* register constructor. */
7664 hlua_class_function(gL.T, "new", hlua_map_new);
7665
7666 /* Create and fill the metatable. */
7667 lua_newtable(gL.T);
7668
7669 /* Create and fille the __index entry. */
7670 lua_pushstring(gL.T, "__index");
7671 lua_newtable(gL.T);
7672
7673 /* Register . */
7674 hlua_class_function(gL.T, "lookup", hlua_map_lookup);
7675 hlua_class_function(gL.T, "slookup", hlua_map_slookup);
7676
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007677 lua_rawset(gL.T, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007678
Thierry Fournier45e78d72016-02-19 18:34:46 +01007679 /* Register previous table in the registry with reference and named entry.
7680 * The function hlua_register_metatable() pops the stack, so we
7681 * previously create a copy of the table.
7682 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007683 lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007684 class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02007685
7686 /* Assign the metatable to the mai Map object. */
7687 lua_setmetatable(gL.T, -2);
7688
7689 /* Set a name to the table. */
7690 lua_setglobal(gL.T, "Map");
7691
7692 /*
7693 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007694 * Register class Channel
7695 *
7696 */
7697
7698 /* Create and fill the metatable. */
7699 lua_newtable(gL.T);
7700
7701 /* Create and fille the __index entry. */
7702 lua_pushstring(gL.T, "__index");
7703 lua_newtable(gL.T);
7704
7705 /* Register . */
7706 hlua_class_function(gL.T, "get", hlua_channel_get);
7707 hlua_class_function(gL.T, "dup", hlua_channel_dup);
7708 hlua_class_function(gL.T, "getline", hlua_channel_getline);
7709 hlua_class_function(gL.T, "set", hlua_channel_set);
7710 hlua_class_function(gL.T, "append", hlua_channel_append);
7711 hlua_class_function(gL.T, "send", hlua_channel_send);
7712 hlua_class_function(gL.T, "forward", hlua_channel_forward);
7713 hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len);
7714 hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len);
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01007715 hlua_class_function(gL.T, "is_full", hlua_channel_is_full);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007716
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007717 lua_rawset(gL.T, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007718
7719 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007720 class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01007721
7722 /*
7723 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007724 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007725 *
7726 */
7727
7728 /* Create and fill the metatable. */
7729 lua_newtable(gL.T);
7730
7731 /* Create and fille the __index entry. */
7732 lua_pushstring(gL.T, "__index");
7733 lua_newtable(gL.T);
7734
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007735 /* Browse existing fetches and create the associated
7736 * object method.
7737 */
7738 sf = NULL;
7739 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
7740
7741 /* Dont register the keywork if the arguments check function are
7742 * not safe during the runtime.
7743 */
7744 if ((sf->val_args != NULL) &&
7745 (sf->val_args != val_payload_lv) &&
7746 (sf->val_args != val_hdr))
7747 continue;
7748
7749 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7750 * by an underscore.
7751 */
7752 strncpy(trash.str, sf->kw, trash.size);
7753 trash.str[trash.size - 1] = '\0';
7754 for (p = trash.str; *p; p++)
7755 if (*p == '.' || *p == '-' || *p == '+')
7756 *p = '_';
7757
7758 /* Register the function. */
7759 lua_pushstring(gL.T, trash.str);
Willy Tarreau2ec22742015-03-10 14:27:20 +01007760 lua_pushlightuserdata(gL.T, sf);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007761 lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007762 lua_rawset(gL.T, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01007763 }
7764
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007765 lua_rawset(gL.T, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007766
7767 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007768 class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007769
7770 /*
7771 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007772 * Register class Converters
7773 *
7774 */
7775
7776 /* Create and fill the metatable. */
7777 lua_newtable(gL.T);
7778
7779 /* Create and fill the __index entry. */
7780 lua_pushstring(gL.T, "__index");
7781 lua_newtable(gL.T);
7782
7783 /* Browse existing converters and create the associated
7784 * object method.
7785 */
7786 sc = NULL;
7787 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
7788 /* Dont register the keywork if the arguments check function are
7789 * not safe during the runtime.
7790 */
7791 if (sc->val_args != NULL)
7792 continue;
7793
7794 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
7795 * by an underscore.
7796 */
7797 strncpy(trash.str, sc->kw, trash.size);
7798 trash.str[trash.size - 1] = '\0';
7799 for (p = trash.str; *p; p++)
7800 if (*p == '.' || *p == '-' || *p == '+')
7801 *p = '_';
7802
7803 /* Register the function. */
7804 lua_pushstring(gL.T, trash.str);
7805 lua_pushlightuserdata(gL.T, sc);
7806 lua_pushcclosure(gL.T, hlua_run_sample_conv, 1);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007807 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007808 }
7809
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007810 lua_rawset(gL.T, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007811
7812 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007813 class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01007814
7815 /*
7816 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007817 * Register class HTTP
7818 *
7819 */
7820
7821 /* Create and fill the metatable. */
7822 lua_newtable(gL.T);
7823
7824 /* Create and fille the __index entry. */
7825 lua_pushstring(gL.T, "__index");
7826 lua_newtable(gL.T);
7827
7828 /* Register Lua functions. */
7829 hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers);
7830 hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr);
7831 hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr);
7832 hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val);
7833 hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr);
7834 hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr);
7835 hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth);
7836 hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path);
7837 hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query);
7838 hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri);
7839
7840 hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers);
7841 hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr);
7842 hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr);
7843 hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val);
7844 hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr);
7845 hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02007846 hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007847
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007848 lua_rawset(gL.T, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007849
7850 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007851 class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01007852
7853 /*
7854 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007855 * Register class AppletTCP
7856 *
7857 */
7858
7859 /* Create and fill the metatable. */
7860 lua_newtable(gL.T);
7861
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007862 /* Create and fille the __index entry. */
7863 lua_pushstring(gL.T, "__index");
7864 lua_newtable(gL.T);
7865
7866 /* Register Lua functions. */
Thierry FOURNIER / OZON.IO3e1d7912016-12-12 12:29:34 +01007867 hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline);
7868 hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv);
7869 hlua_class_function(gL.T, "send", hlua_applet_tcp_send);
7870 hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv);
7871 hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007872 hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var);
7873 hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var);
7874 hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007875
7876 lua_settable(gL.T, -3);
7877
7878 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007879 class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007880
7881 /*
7882 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007883 * Register class AppletHTTP
7884 *
7885 */
7886
7887 /* Create and fill the metatable. */
7888 lua_newtable(gL.T);
7889
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007890 /* Create and fille the __index entry. */
7891 lua_pushstring(gL.T, "__index");
7892 lua_newtable(gL.T);
7893
7894 /* Register Lua functions. */
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01007895 hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv);
7896 hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv);
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01007897 hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var);
7898 hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var);
7899 hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007900 hlua_class_function(gL.T, "getline", hlua_applet_http_getline);
7901 hlua_class_function(gL.T, "receive", hlua_applet_http_recv);
7902 hlua_class_function(gL.T, "send", hlua_applet_http_send);
7903 hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader);
7904 hlua_class_function(gL.T, "set_status", hlua_applet_http_status);
7905 hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response);
7906
7907 lua_settable(gL.T, -3);
7908
7909 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007910 class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007911
7912 /*
7913 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01007914 * Register class TXN
7915 *
7916 */
7917
7918 /* Create and fill the metatable. */
7919 lua_newtable(gL.T);
7920
7921 /* Create and fille the __index entry. */
7922 lua_pushstring(gL.T, "__index");
7923 lua_newtable(gL.T);
7924
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007925 /* Register Lua functions. */
Willy Tarreau59551662015-03-10 14:23:13 +01007926 hlua_class_function(gL.T, "set_priv", hlua_set_priv);
7927 hlua_class_function(gL.T, "get_priv", hlua_get_priv);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007928 hlua_class_function(gL.T, "set_var", hlua_set_var);
Christopher Faulet85d79c92016-11-09 16:54:56 +01007929 hlua_class_function(gL.T, "unset_var", hlua_unset_var);
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02007930 hlua_class_function(gL.T, "get_var", hlua_get_var);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02007931 hlua_class_function(gL.T, "done", hlua_txn_done);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01007932 hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel);
7933 hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos);
7934 hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01007935 hlua_class_function(gL.T, "deflog", hlua_txn_deflog);
7936 hlua_class_function(gL.T, "log", hlua_txn_log);
7937 hlua_class_function(gL.T, "Debug", hlua_txn_log_debug);
7938 hlua_class_function(gL.T, "Info", hlua_txn_log_info);
7939 hlua_class_function(gL.T, "Warning", hlua_txn_log_warning);
7940 hlua_class_function(gL.T, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01007941
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007942 lua_rawset(gL.T, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01007943
7944 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007945 class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007946
7947 /*
7948 *
7949 * Register class Socket
7950 *
7951 */
7952
7953 /* Create and fill the metatable. */
7954 lua_newtable(gL.T);
7955
7956 /* Create and fille the __index entry. */
7957 lua_pushstring(gL.T, "__index");
7958 lua_newtable(gL.T);
7959
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007960#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007961 hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01007962#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007963 hlua_class_function(gL.T, "connect", hlua_socket_connect);
7964 hlua_class_function(gL.T, "send", hlua_socket_send);
7965 hlua_class_function(gL.T, "receive", hlua_socket_receive);
7966 hlua_class_function(gL.T, "close", hlua_socket_close);
7967 hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername);
7968 hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname);
7969 hlua_class_function(gL.T, "setoption", hlua_socket_setoption);
7970 hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout);
7971
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007972 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007973
7974 /* Register the garbage collector entry. */
7975 lua_pushstring(gL.T, "__gc");
7976 lua_pushcclosure(gL.T, hlua_socket_gc, 0);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02007977 lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007978
7979 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier45e78d72016-02-19 18:34:46 +01007980 class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007981
7982 /* Proxy and server configuration initialisation. */
7983 memset(&socket_proxy, 0, sizeof(socket_proxy));
7984 init_new_proxy(&socket_proxy);
7985 socket_proxy.parent = NULL;
7986 socket_proxy.last_change = now.tv_sec;
7987 socket_proxy.id = "LUA-SOCKET";
7988 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
7989 socket_proxy.maxconn = 0;
7990 socket_proxy.accept = NULL;
7991 socket_proxy.options2 |= PR_O2_INDEPSTR;
7992 socket_proxy.srv = NULL;
7993 socket_proxy.conn_retries = 0;
7994 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
7995
7996 /* Init TCP server: unchanged parameters */
7997 memset(&socket_tcp, 0, sizeof(socket_tcp));
7998 socket_tcp.next = NULL;
7999 socket_tcp.proxy = &socket_proxy;
8000 socket_tcp.obj_type = OBJ_TYPE_SERVER;
8001 LIST_INIT(&socket_tcp.actconns);
8002 LIST_INIT(&socket_tcp.pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02008003 socket_tcp.priv_conns = NULL;
8004 socket_tcp.idle_conns = NULL;
8005 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008006 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008007 socket_tcp.last_change = 0;
8008 socket_tcp.id = "LUA-TCP-CONN";
8009 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8010 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8011 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
8012
8013 /* XXX: Copy default parameter from default server,
8014 * but the default server is not initialized.
8015 */
8016 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
8017 socket_tcp.minconn = socket_proxy.defsrv.minconn;
8018 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
8019 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
8020 socket_tcp.onerror = socket_proxy.defsrv.onerror;
8021 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8022 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
8023 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8024 socket_tcp.uweight = socket_proxy.defsrv.iweight;
8025 socket_tcp.iweight = socket_proxy.defsrv.iweight;
8026
8027 socket_tcp.check.status = HCHK_STATUS_INI;
8028 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
8029 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
8030 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
8031 socket_tcp.check.server = &socket_tcp;
8032
8033 socket_tcp.agent.status = HCHK_STATUS_INI;
8034 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
8035 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
8036 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
8037 socket_tcp.agent.server = &socket_tcp;
8038
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008039 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008040
8041#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008042 /* Init TCP server: unchanged parameters */
8043 memset(&socket_ssl, 0, sizeof(socket_ssl));
8044 socket_ssl.next = NULL;
8045 socket_ssl.proxy = &socket_proxy;
8046 socket_ssl.obj_type = OBJ_TYPE_SERVER;
8047 LIST_INIT(&socket_ssl.actconns);
8048 LIST_INIT(&socket_ssl.pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02008049 socket_tcp.priv_conns = NULL;
8050 socket_tcp.idle_conns = NULL;
8051 socket_tcp.safe_conns = NULL;
Emeric Brun52a91d32017-08-31 14:41:55 +02008052 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008053 socket_ssl.last_change = 0;
8054 socket_ssl.id = "LUA-SSL-CONN";
8055 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8056 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
8057 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
8058
8059 /* XXX: Copy default parameter from default server,
8060 * but the default server is not initialized.
8061 */
8062 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
8063 socket_ssl.minconn = socket_proxy.defsrv.minconn;
8064 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
8065 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
8066 socket_ssl.onerror = socket_proxy.defsrv.onerror;
8067 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
8068 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
8069 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
8070 socket_ssl.uweight = socket_proxy.defsrv.iweight;
8071 socket_ssl.iweight = socket_proxy.defsrv.iweight;
8072
8073 socket_ssl.check.status = HCHK_STATUS_INI;
8074 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
8075 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
8076 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
8077 socket_ssl.check.server = &socket_ssl;
8078
8079 socket_ssl.agent.status = HCHK_STATUS_INI;
8080 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
8081 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
8082 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
8083 socket_ssl.agent.server = &socket_ssl;
8084
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008085 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01008086 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008087
Thierry FOURNIER36d13742015-03-17 16:48:53 +01008088 for (idx = 0; args[idx] != NULL; idx++) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008089 if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */
8090 /*
8091 *
8092 * If the keyword is not known, we can search in the registered
8093 * server keywords. This is usefull to configure special SSL
8094 * features like client certificates and ssl_verify.
8095 *
8096 */
8097 tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error);
8098 if (tmp_error != 0) {
8099 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
8100 abort(); /* This must be never arrives because the command line
8101 not editable by the user. */
8102 }
8103 idx += kw->skip;
8104 }
8105 }
8106
8107 /* Initialize SSL server. */
Willy Tarreaucbe6da52018-05-18 17:08:28 +02008108 if (socket_ssl.xprt->prepare_srv) {
8109 int saved_used_backed = global.ssl_used_backend;
8110 // don't affect maxconn automatic computation
Willy Tarreau17d45382016-12-22 21:16:08 +01008111 socket_ssl.xprt->prepare_srv(&socket_ssl);
Willy Tarreaucbe6da52018-05-18 17:08:28 +02008112 global.ssl_used_backend = saved_used_backed;
8113 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01008114#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01008115
8116 RESET_SAFE_LJMP(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008117}
Willy Tarreaubb57d942016-12-21 19:04:56 +01008118
8119__attribute__((constructor))
8120static void __hlua_init(void)
8121{
8122 char *ptr = NULL;
8123 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
8124 hap_register_build_opts(ptr, 1);
8125}